Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -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 "method_compiler.h" |
| 18 | |
Logan Chien | fca7e87 | 2011-12-20 20:08:22 +0800 | [diff] [blame] | 19 | #include "backend_types.h" |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 20 | #include "compilation_unit.h" |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 21 | #include "compiler.h" |
Logan Chien | a78e3c8 | 2011-12-27 17:59:35 +0800 | [diff] [blame] | 22 | #include "inferred_reg_category_map.h" |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 23 | #include "ir_builder.h" |
| 24 | #include "logging.h" |
Logan Chien | 4dd96f5 | 2012-02-29 01:26:58 +0800 | [diff] [blame] | 25 | #include "oat_compilation_unit.h" |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 26 | #include "object.h" |
| 27 | #include "object_utils.h" |
Logan Chien | 42e0e15 | 2012-01-13 15:42:36 +0800 | [diff] [blame] | 28 | #include "runtime_support_func.h" |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 29 | #include "runtime_support_llvm.h" |
Logan Chien | 1b0a1b7 | 2012-03-15 06:20:17 +0800 | [diff] [blame] | 30 | #include "shadow_frame.h" |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 31 | #include "stl_util.h" |
Logan Chien | 0b82710 | 2011-12-20 19:46:14 +0800 | [diff] [blame] | 32 | #include "stringprintf.h" |
| 33 | #include "utils_llvm.h" |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 34 | #include "verifier/method_verifier.h" |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 35 | |
| 36 | #include <iomanip> |
| 37 | |
Logan Chien | c670a8d | 2011-12-20 21:25:56 +0800 | [diff] [blame] | 38 | #include <llvm/BasicBlock.h> |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 39 | #include <llvm/Function.h> |
Logan Chien | a85fb2f | 2012-01-16 12:52:56 +0800 | [diff] [blame] | 40 | #include <llvm/GlobalVariable.h> |
| 41 | #include <llvm/Intrinsics.h> |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 42 | |
Logan Chien | 8342616 | 2011-12-09 09:29:50 +0800 | [diff] [blame] | 43 | namespace art { |
| 44 | namespace compiler_llvm { |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 45 | |
Logan Chien | 42e0e15 | 2012-01-13 15:42:36 +0800 | [diff] [blame] | 46 | using namespace runtime_support; |
| 47 | |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 48 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 49 | MethodCompiler::MethodCompiler(CompilationUnit* cunit, |
Logan Chien | 8342616 | 2011-12-09 09:29:50 +0800 | [diff] [blame] | 50 | Compiler* compiler, |
Logan Chien | 4dd96f5 | 2012-02-29 01:26:58 +0800 | [diff] [blame] | 51 | OatCompilationUnit* oat_compilation_unit) |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 52 | : cunit_(cunit), compiler_(compiler), |
| 53 | class_linker_(oat_compilation_unit->class_linker_), |
| 54 | class_loader_(oat_compilation_unit->class_loader_), |
| 55 | dex_file_(oat_compilation_unit->dex_file_), |
| 56 | dex_cache_(oat_compilation_unit->dex_cache_), |
| 57 | code_item_(oat_compilation_unit->code_item_), |
| 58 | oat_compilation_unit_(oat_compilation_unit), |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 59 | method_idx_(oat_compilation_unit->method_idx_), |
| 60 | access_flags_(oat_compilation_unit->access_flags_), |
| 61 | module_(cunit->GetModule()), |
| 62 | context_(cunit->GetLLVMContext()), |
| 63 | irb_(*cunit->GetIRBuilder()), func_(NULL), retval_reg_(NULL), |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 64 | basic_block_stack_overflow_(NULL), |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 65 | basic_block_reg_alloca_(NULL), basic_block_shadow_frame_alloca_(NULL), |
Logan Chien | ef4a656 | 2012-04-24 18:02:24 +0800 | [diff] [blame] | 66 | basic_block_reg_arg_init_(NULL), |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 67 | basic_blocks_(code_item_->insns_size_in_code_units_), |
| 68 | basic_block_landing_pads_(code_item_->tries_size_, NULL), |
| 69 | basic_block_unwind_(NULL), basic_block_unreachable_(NULL), |
Logan Chien | 937105a | 2012-04-02 02:37:37 +0800 | [diff] [blame] | 70 | shadow_frame_(NULL), elf_func_idx_(cunit_->AcquireUniqueElfFuncIndex()) { |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | |
| 74 | MethodCompiler::~MethodCompiler() { |
Logan Chien | c670a8d | 2011-12-20 21:25:56 +0800 | [diff] [blame] | 75 | STLDeleteElements(®s_); |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | |
Logan Chien | 0b82710 | 2011-12-20 19:46:14 +0800 | [diff] [blame] | 79 | void MethodCompiler::CreateFunction() { |
| 80 | // LLVM function name |
Logan Chien | 937105a | 2012-04-02 02:37:37 +0800 | [diff] [blame] | 81 | std::string func_name(ElfFuncName(elf_func_idx_)); |
Logan Chien | 0b82710 | 2011-12-20 19:46:14 +0800 | [diff] [blame] | 82 | |
| 83 | // Get function type |
| 84 | llvm::FunctionType* func_type = |
Logan Chien | dd361c9 | 2012-04-10 23:40:37 +0800 | [diff] [blame] | 85 | GetFunctionType(method_idx_, oat_compilation_unit_->IsStatic()); |
Logan Chien | 0b82710 | 2011-12-20 19:46:14 +0800 | [diff] [blame] | 86 | |
| 87 | // Create function |
| 88 | func_ = llvm::Function::Create(func_type, llvm::Function::ExternalLinkage, |
| 89 | func_name, module_); |
| 90 | |
| 91 | // Set argument name |
| 92 | llvm::Function::arg_iterator arg_iter(func_->arg_begin()); |
| 93 | llvm::Function::arg_iterator arg_end(func_->arg_end()); |
| 94 | |
| 95 | DCHECK_NE(arg_iter, arg_end); |
| 96 | arg_iter->setName("method"); |
| 97 | ++arg_iter; |
| 98 | |
Logan Chien | dd361c9 | 2012-04-10 23:40:37 +0800 | [diff] [blame] | 99 | if (!oat_compilation_unit_->IsStatic()) { |
Logan Chien | 0b82710 | 2011-12-20 19:46:14 +0800 | [diff] [blame] | 100 | DCHECK_NE(arg_iter, arg_end); |
| 101 | arg_iter->setName("this"); |
| 102 | ++arg_iter; |
| 103 | } |
| 104 | |
| 105 | for (unsigned i = 0; arg_iter != arg_end; ++i, ++arg_iter) { |
| 106 | arg_iter->setName(StringPrintf("a%u", i)); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | |
| 111 | llvm::FunctionType* MethodCompiler::GetFunctionType(uint32_t method_idx, |
| 112 | bool is_static) { |
| 113 | // Get method signature |
| 114 | DexFile::MethodId const& method_id = dex_file_->GetMethodId(method_idx); |
| 115 | |
Logan Chien | 8faf802 | 2012-02-24 12:25:29 +0800 | [diff] [blame] | 116 | uint32_t shorty_size; |
Logan Chien | 0b82710 | 2011-12-20 19:46:14 +0800 | [diff] [blame] | 117 | char const* shorty = dex_file_->GetMethodShorty(method_id, &shorty_size); |
Logan Chien | 8faf802 | 2012-02-24 12:25:29 +0800 | [diff] [blame] | 118 | CHECK_GE(shorty_size, 1u); |
Logan Chien | 0b82710 | 2011-12-20 19:46:14 +0800 | [diff] [blame] | 119 | |
| 120 | // Get return type |
| 121 | llvm::Type* ret_type = irb_.getJType(shorty[0], kAccurate); |
| 122 | |
| 123 | // Get argument type |
| 124 | std::vector<llvm::Type*> args_type; |
| 125 | |
| 126 | args_type.push_back(irb_.getJObjectTy()); // method object pointer |
| 127 | |
| 128 | if (!is_static) { |
| 129 | args_type.push_back(irb_.getJType('L', kAccurate)); // "this" object pointer |
| 130 | } |
| 131 | |
Logan Chien | 8faf802 | 2012-02-24 12:25:29 +0800 | [diff] [blame] | 132 | for (uint32_t i = 1; i < shorty_size; ++i) { |
Logan Chien | 0b82710 | 2011-12-20 19:46:14 +0800 | [diff] [blame] | 133 | args_type.push_back(irb_.getJType(shorty[i], kAccurate)); |
| 134 | } |
| 135 | |
| 136 | return llvm::FunctionType::get(ret_type, args_type, false); |
| 137 | } |
| 138 | |
| 139 | |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 140 | void MethodCompiler::EmitPrologue() { |
Logan Chien | c670a8d | 2011-12-20 21:25:56 +0800 | [diff] [blame] | 141 | // Create basic blocks for prologue |
TDYa127 | 4165a83 | 2012-04-03 17:47:16 -0700 | [diff] [blame] | 142 | basic_block_stack_overflow_ = |
| 143 | llvm::BasicBlock::Create(*context_, "prologue.stack_overflow_check", func_); |
| 144 | |
Logan Chien | c670a8d | 2011-12-20 21:25:56 +0800 | [diff] [blame] | 145 | basic_block_reg_alloca_ = |
| 146 | llvm::BasicBlock::Create(*context_, "prologue.alloca", func_); |
| 147 | |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 148 | basic_block_shadow_frame_alloca_ = |
| 149 | llvm::BasicBlock::Create(*context_, "prologue.shadowframe", func_); |
| 150 | |
Logan Chien | d6ececa | 2011-12-27 16:20:15 +0800 | [diff] [blame] | 151 | basic_block_reg_arg_init_ = |
| 152 | llvm::BasicBlock::Create(*context_, "prologue.arginit", func_); |
| 153 | |
TDYa127 | 4165a83 | 2012-04-03 17:47:16 -0700 | [diff] [blame] | 154 | // Before alloca, check stack overflow. |
| 155 | EmitStackOverflowCheck(); |
| 156 | |
Logan Chien | c670a8d | 2011-12-20 21:25:56 +0800 | [diff] [blame] | 157 | // Create register array |
| 158 | for (uint16_t r = 0; r < code_item_->registers_size_; ++r) { |
| 159 | regs_.push_back(DalvikReg::CreateLocalVarReg(*this, r)); |
| 160 | } |
| 161 | |
| 162 | retval_reg_.reset(DalvikReg::CreateRetValReg(*this)); |
Logan Chien | d6ececa | 2011-12-27 16:20:15 +0800 | [diff] [blame] | 163 | |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 164 | // Create Shadow Frame |
| 165 | EmitPrologueAllocShadowFrame(); |
| 166 | |
Logan Chien | d6ececa | 2011-12-27 16:20:15 +0800 | [diff] [blame] | 167 | // Store argument to dalvik register |
| 168 | irb_.SetInsertPoint(basic_block_reg_arg_init_); |
| 169 | EmitPrologueAssignArgRegister(); |
| 170 | |
| 171 | // Branch to start address |
| 172 | irb_.CreateBr(GetBasicBlock(0)); |
Logan Chien | c670a8d | 2011-12-20 21:25:56 +0800 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | |
TDYa127 | 4165a83 | 2012-04-03 17:47:16 -0700 | [diff] [blame] | 176 | void MethodCompiler::EmitStackOverflowCheck() { |
| 177 | irb_.SetInsertPoint(basic_block_stack_overflow_); |
| 178 | |
| 179 | // Call llvm intrinsic function to get frame address. |
| 180 | llvm::Function* frameaddress = |
| 181 | llvm::Intrinsic::getDeclaration(module_, llvm::Intrinsic::frameaddress); |
| 182 | |
| 183 | // The type of llvm::frameaddress is: i8* @llvm.frameaddress(i32) |
| 184 | llvm::Value* frame_address = irb_.CreateCall(frameaddress, irb_.getInt32(0)); |
| 185 | |
| 186 | // Cast i8* to int |
| 187 | frame_address = irb_.CreatePtrToInt(frame_address, irb_.getPtrEquivIntTy()); |
| 188 | |
| 189 | // Get thread.stack_end_ |
| 190 | llvm::Value* thread_object_addr = |
| 191 | irb_.CreateCall(irb_.GetRuntime(GetCurrentThread)); |
| 192 | |
| 193 | llvm::Value* stack_end_addr = |
| 194 | irb_.CreatePtrDisp(thread_object_addr, |
| 195 | irb_.getPtrEquivInt(Thread::StackEndOffset().Int32Value()), |
| 196 | irb_.getPtrEquivIntTy()->getPointerTo()); |
| 197 | |
| 198 | llvm::Value* stack_end = irb_.CreateLoad(stack_end_addr); |
| 199 | |
| 200 | // Check the frame address < thread.stack_end_ ? |
| 201 | llvm::Value* is_stack_overflow = irb_.CreateICmpULT(frame_address, stack_end); |
| 202 | |
| 203 | llvm::BasicBlock* block_exception = |
| 204 | llvm::BasicBlock::Create(*context_, "stack_overflow", func_); |
| 205 | |
| 206 | llvm::BasicBlock* block_continue = |
| 207 | llvm::BasicBlock::Create(*context_, "stack_overflow_cont", func_); |
| 208 | |
| 209 | irb_.CreateCondBr(is_stack_overflow, block_exception, block_continue); |
| 210 | |
| 211 | // If stack overflow, throw exception. |
| 212 | irb_.SetInsertPoint(block_exception); |
| 213 | irb_.CreateCall(irb_.GetRuntime(ThrowStackOverflowException)); |
| 214 | |
| 215 | // Unwind. |
Logan Chien | dd361c9 | 2012-04-10 23:40:37 +0800 | [diff] [blame] | 216 | char ret_shorty = oat_compilation_unit_->GetShorty()[0]; |
TDYa127 | 4165a83 | 2012-04-03 17:47:16 -0700 | [diff] [blame] | 217 | if (ret_shorty == 'V') { |
| 218 | irb_.CreateRetVoid(); |
| 219 | } else { |
| 220 | irb_.CreateRet(irb_.getJZero(ret_shorty)); |
| 221 | } |
| 222 | |
| 223 | basic_block_stack_overflow_ = block_continue; |
| 224 | } |
| 225 | |
| 226 | |
Logan Chien | c670a8d | 2011-12-20 21:25:56 +0800 | [diff] [blame] | 227 | void MethodCompiler::EmitPrologueLastBranch() { |
TDYa127 | 4165a83 | 2012-04-03 17:47:16 -0700 | [diff] [blame] | 228 | irb_.SetInsertPoint(basic_block_stack_overflow_); |
| 229 | irb_.CreateBr(basic_block_reg_alloca_); |
| 230 | |
Logan Chien | c670a8d | 2011-12-20 21:25:56 +0800 | [diff] [blame] | 231 | irb_.SetInsertPoint(basic_block_reg_alloca_); |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 232 | irb_.CreateBr(basic_block_shadow_frame_alloca_); |
| 233 | |
| 234 | irb_.SetInsertPoint(basic_block_shadow_frame_alloca_); |
Logan Chien | d6ececa | 2011-12-27 16:20:15 +0800 | [diff] [blame] | 235 | irb_.CreateBr(basic_block_reg_arg_init_); |
| 236 | } |
| 237 | |
| 238 | |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 239 | void MethodCompiler::EmitPrologueAllocShadowFrame() { |
| 240 | irb_.SetInsertPoint(basic_block_shadow_frame_alloca_); |
| 241 | |
| 242 | // Allocate the shadow frame now! |
| 243 | uint32_t sirt_size = code_item_->registers_size_; |
| 244 | // TODO: registers_size_ is a bad approximation. Compute a |
| 245 | // tighter approximation at Dex verifier while performing data-flow |
| 246 | // analysis. |
| 247 | |
| 248 | llvm::StructType* shadow_frame_type = irb_.getShadowFrameTy(sirt_size); |
| 249 | shadow_frame_ = irb_.CreateAlloca(shadow_frame_type); |
| 250 | |
| 251 | // Zero-initialization of the shadow frame |
| 252 | llvm::ConstantAggregateZero* zero_initializer = |
| 253 | llvm::ConstantAggregateZero::get(shadow_frame_type); |
| 254 | |
| 255 | irb_.CreateStore(zero_initializer, shadow_frame_); |
| 256 | |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 257 | // Store the method pointer |
Logan Chien | 1b0a1b7 | 2012-03-15 06:20:17 +0800 | [diff] [blame] | 258 | llvm::Value* method_field_addr = |
| 259 | irb_.CreatePtrDisp(shadow_frame_, |
| 260 | irb_.getPtrEquivInt(ShadowFrame::MethodOffset()), |
| 261 | irb_.getJObjectTy()->getPointerTo()); |
| 262 | |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 263 | llvm::Value* method_object_addr = EmitLoadMethodObjectAddr(); |
| 264 | irb_.CreateStore(method_object_addr, method_field_addr); |
| 265 | |
| 266 | // Store the number of the pointer slots |
Logan Chien | 1b0a1b7 | 2012-03-15 06:20:17 +0800 | [diff] [blame] | 267 | llvm::ConstantInt* num_of_refs_offset = |
| 268 | irb_.getPtrEquivInt(ShadowFrame::NumberOfReferencesOffset()); |
| 269 | |
| 270 | llvm::Value* num_of_refs_field_addr = |
| 271 | irb_.CreatePtrDisp(shadow_frame_, num_of_refs_offset, |
| 272 | irb_.getJIntTy()->getPointerTo()); |
| 273 | |
| 274 | llvm::ConstantInt* num_of_refs_value = irb_.getJInt(sirt_size); |
| 275 | irb_.CreateStore(num_of_refs_value, num_of_refs_field_addr); |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 276 | |
| 277 | // Push the shadow frame |
| 278 | llvm::Value* shadow_frame_upcast = |
| 279 | irb_.CreateConstGEP2_32(shadow_frame_, 0, 0); |
| 280 | |
| 281 | irb_.CreateCall(irb_.GetRuntime(PushShadowFrame), shadow_frame_upcast); |
| 282 | } |
| 283 | |
| 284 | |
Logan Chien | d6ececa | 2011-12-27 16:20:15 +0800 | [diff] [blame] | 285 | void MethodCompiler::EmitPrologueAssignArgRegister() { |
| 286 | uint16_t arg_reg = code_item_->registers_size_ - code_item_->ins_size_; |
| 287 | |
| 288 | llvm::Function::arg_iterator arg_iter(func_->arg_begin()); |
| 289 | llvm::Function::arg_iterator arg_end(func_->arg_end()); |
| 290 | |
Logan Chien | dd361c9 | 2012-04-10 23:40:37 +0800 | [diff] [blame] | 291 | uint32_t shorty_size = 0; |
| 292 | char const* shorty = oat_compilation_unit_->GetShorty(&shorty_size); |
| 293 | CHECK_GE(shorty_size, 1u); |
Logan Chien | d6ececa | 2011-12-27 16:20:15 +0800 | [diff] [blame] | 294 | |
| 295 | ++arg_iter; // skip method object |
| 296 | |
Logan Chien | dd361c9 | 2012-04-10 23:40:37 +0800 | [diff] [blame] | 297 | if (!oat_compilation_unit_->IsStatic()) { |
Logan Chien | d6ececa | 2011-12-27 16:20:15 +0800 | [diff] [blame] | 298 | EmitStoreDalvikReg(arg_reg, kObject, kAccurate, arg_iter); |
| 299 | ++arg_iter; |
| 300 | ++arg_reg; |
| 301 | } |
| 302 | |
Logan Chien | dd361c9 | 2012-04-10 23:40:37 +0800 | [diff] [blame] | 303 | for (uint32_t i = 1; i < shorty_size; ++i, ++arg_iter) { |
Logan Chien | d6ececa | 2011-12-27 16:20:15 +0800 | [diff] [blame] | 304 | EmitStoreDalvikReg(arg_reg, shorty[i], kAccurate, arg_iter); |
| 305 | |
| 306 | ++arg_reg; |
| 307 | if (shorty[i] == 'J' || shorty[i] == 'D') { |
| 308 | // Wide types, such as long and double, are using a pair of registers |
| 309 | // to store the value, so we have to increase arg_reg again. |
| 310 | ++arg_reg; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | DCHECK_EQ(arg_end, arg_iter); |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | |
Logan Chien | 8342616 | 2011-12-09 09:29:50 +0800 | [diff] [blame] | 318 | void MethodCompiler::EmitInstructions() { |
Logan Chien | d6c239a | 2011-12-23 15:11:45 +0800 | [diff] [blame] | 319 | uint32_t dex_pc = 0; |
| 320 | while (dex_pc < code_item_->insns_size_in_code_units_) { |
| 321 | Instruction const* insn = Instruction::At(code_item_->insns_ + dex_pc); |
| 322 | EmitInstruction(dex_pc, insn); |
| 323 | dex_pc += insn->SizeInCodeUnits(); |
| 324 | } |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | |
Logan Chien | 8342616 | 2011-12-09 09:29:50 +0800 | [diff] [blame] | 328 | void MethodCompiler::EmitInstruction(uint32_t dex_pc, |
| 329 | Instruction const* insn) { |
Logan Chien | d6c239a | 2011-12-23 15:11:45 +0800 | [diff] [blame] | 330 | |
| 331 | // Set the IRBuilder insertion point |
| 332 | irb_.SetInsertPoint(GetBasicBlock(dex_pc)); |
| 333 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 334 | #define ARGS dex_pc, insn |
| 335 | |
| 336 | // Dispatch the instruction |
| 337 | switch (insn->Opcode()) { |
| 338 | case Instruction::NOP: |
| 339 | EmitInsn_Nop(ARGS); |
| 340 | break; |
| 341 | |
| 342 | case Instruction::MOVE: |
| 343 | case Instruction::MOVE_FROM16: |
| 344 | case Instruction::MOVE_16: |
| 345 | EmitInsn_Move(ARGS, kInt); |
| 346 | break; |
| 347 | |
| 348 | case Instruction::MOVE_WIDE: |
| 349 | case Instruction::MOVE_WIDE_FROM16: |
| 350 | case Instruction::MOVE_WIDE_16: |
| 351 | EmitInsn_Move(ARGS, kLong); |
| 352 | break; |
| 353 | |
| 354 | case Instruction::MOVE_OBJECT: |
| 355 | case Instruction::MOVE_OBJECT_FROM16: |
| 356 | case Instruction::MOVE_OBJECT_16: |
| 357 | EmitInsn_Move(ARGS, kObject); |
| 358 | break; |
| 359 | |
| 360 | case Instruction::MOVE_RESULT: |
| 361 | EmitInsn_MoveResult(ARGS, kInt); |
| 362 | break; |
| 363 | |
| 364 | case Instruction::MOVE_RESULT_WIDE: |
| 365 | EmitInsn_MoveResult(ARGS, kLong); |
| 366 | break; |
| 367 | |
| 368 | case Instruction::MOVE_RESULT_OBJECT: |
| 369 | EmitInsn_MoveResult(ARGS, kObject); |
| 370 | break; |
| 371 | |
| 372 | case Instruction::MOVE_EXCEPTION: |
| 373 | EmitInsn_MoveException(ARGS); |
| 374 | break; |
| 375 | |
| 376 | case Instruction::RETURN_VOID: |
| 377 | EmitInsn_ReturnVoid(ARGS); |
| 378 | break; |
| 379 | |
| 380 | case Instruction::RETURN: |
| 381 | case Instruction::RETURN_WIDE: |
| 382 | case Instruction::RETURN_OBJECT: |
| 383 | EmitInsn_Return(ARGS); |
| 384 | break; |
| 385 | |
| 386 | case Instruction::CONST_4: |
| 387 | case Instruction::CONST_16: |
| 388 | case Instruction::CONST: |
| 389 | case Instruction::CONST_HIGH16: |
| 390 | EmitInsn_LoadConstant(ARGS, kInt); |
| 391 | break; |
| 392 | |
| 393 | case Instruction::CONST_WIDE_16: |
| 394 | case Instruction::CONST_WIDE_32: |
| 395 | case Instruction::CONST_WIDE: |
| 396 | case Instruction::CONST_WIDE_HIGH16: |
| 397 | EmitInsn_LoadConstant(ARGS, kLong); |
| 398 | break; |
| 399 | |
| 400 | case Instruction::CONST_STRING: |
| 401 | case Instruction::CONST_STRING_JUMBO: |
| 402 | EmitInsn_LoadConstantString(ARGS); |
| 403 | break; |
| 404 | |
| 405 | case Instruction::CONST_CLASS: |
| 406 | EmitInsn_LoadConstantClass(ARGS); |
| 407 | break; |
| 408 | |
| 409 | case Instruction::MONITOR_ENTER: |
| 410 | EmitInsn_MonitorEnter(ARGS); |
| 411 | break; |
| 412 | |
| 413 | case Instruction::MONITOR_EXIT: |
| 414 | EmitInsn_MonitorExit(ARGS); |
| 415 | break; |
| 416 | |
| 417 | case Instruction::CHECK_CAST: |
| 418 | EmitInsn_CheckCast(ARGS); |
| 419 | break; |
| 420 | |
| 421 | case Instruction::INSTANCE_OF: |
| 422 | EmitInsn_InstanceOf(ARGS); |
| 423 | break; |
| 424 | |
| 425 | case Instruction::ARRAY_LENGTH: |
| 426 | EmitInsn_ArrayLength(ARGS); |
| 427 | break; |
| 428 | |
| 429 | case Instruction::NEW_INSTANCE: |
| 430 | EmitInsn_NewInstance(ARGS); |
| 431 | break; |
| 432 | |
| 433 | case Instruction::NEW_ARRAY: |
| 434 | EmitInsn_NewArray(ARGS); |
| 435 | break; |
| 436 | |
| 437 | case Instruction::FILLED_NEW_ARRAY: |
| 438 | EmitInsn_FilledNewArray(ARGS, false); |
| 439 | break; |
| 440 | |
| 441 | case Instruction::FILLED_NEW_ARRAY_RANGE: |
| 442 | EmitInsn_FilledNewArray(ARGS, true); |
| 443 | break; |
| 444 | |
| 445 | case Instruction::FILL_ARRAY_DATA: |
| 446 | EmitInsn_FillArrayData(ARGS); |
| 447 | break; |
| 448 | |
| 449 | case Instruction::THROW: |
| 450 | EmitInsn_ThrowException(ARGS); |
| 451 | break; |
| 452 | |
| 453 | case Instruction::GOTO: |
| 454 | case Instruction::GOTO_16: |
| 455 | case Instruction::GOTO_32: |
| 456 | EmitInsn_UnconditionalBranch(ARGS); |
| 457 | break; |
| 458 | |
| 459 | case Instruction::PACKED_SWITCH: |
| 460 | EmitInsn_PackedSwitch(ARGS); |
| 461 | break; |
| 462 | |
| 463 | case Instruction::SPARSE_SWITCH: |
| 464 | EmitInsn_SparseSwitch(ARGS); |
| 465 | break; |
| 466 | |
| 467 | case Instruction::CMPL_FLOAT: |
| 468 | EmitInsn_FPCompare(ARGS, kFloat, false); |
| 469 | break; |
| 470 | |
| 471 | case Instruction::CMPG_FLOAT: |
| 472 | EmitInsn_FPCompare(ARGS, kFloat, true); |
| 473 | break; |
| 474 | |
| 475 | case Instruction::CMPL_DOUBLE: |
| 476 | EmitInsn_FPCompare(ARGS, kDouble, false); |
| 477 | break; |
| 478 | |
| 479 | case Instruction::CMPG_DOUBLE: |
| 480 | EmitInsn_FPCompare(ARGS, kDouble, true); |
| 481 | break; |
| 482 | |
| 483 | case Instruction::CMP_LONG: |
| 484 | EmitInsn_LongCompare(ARGS); |
| 485 | break; |
| 486 | |
| 487 | case Instruction::IF_EQ: |
| 488 | EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_EQ); |
| 489 | break; |
| 490 | |
| 491 | case Instruction::IF_NE: |
| 492 | EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_NE); |
| 493 | break; |
| 494 | |
| 495 | case Instruction::IF_LT: |
| 496 | EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_LT); |
| 497 | break; |
| 498 | |
| 499 | case Instruction::IF_GE: |
| 500 | EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_GE); |
| 501 | break; |
| 502 | |
| 503 | case Instruction::IF_GT: |
| 504 | EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_GT); |
| 505 | break; |
| 506 | |
| 507 | case Instruction::IF_LE: |
| 508 | EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_LE); |
| 509 | break; |
| 510 | |
| 511 | case Instruction::IF_EQZ: |
| 512 | EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_EQ); |
| 513 | break; |
| 514 | |
| 515 | case Instruction::IF_NEZ: |
| 516 | EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_NE); |
| 517 | break; |
| 518 | |
| 519 | case Instruction::IF_LTZ: |
| 520 | EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_LT); |
| 521 | break; |
| 522 | |
| 523 | case Instruction::IF_GEZ: |
| 524 | EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_GE); |
| 525 | break; |
| 526 | |
| 527 | case Instruction::IF_GTZ: |
| 528 | EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_GT); |
| 529 | break; |
| 530 | |
| 531 | case Instruction::IF_LEZ: |
| 532 | EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_LE); |
| 533 | break; |
| 534 | |
| 535 | case Instruction::AGET: |
| 536 | EmitInsn_AGet(ARGS, kInt); |
| 537 | break; |
| 538 | |
| 539 | case Instruction::AGET_WIDE: |
| 540 | EmitInsn_AGet(ARGS, kLong); |
| 541 | break; |
| 542 | |
| 543 | case Instruction::AGET_OBJECT: |
| 544 | EmitInsn_AGet(ARGS, kObject); |
| 545 | break; |
| 546 | |
| 547 | case Instruction::AGET_BOOLEAN: |
| 548 | EmitInsn_AGet(ARGS, kBoolean); |
| 549 | break; |
| 550 | |
| 551 | case Instruction::AGET_BYTE: |
| 552 | EmitInsn_AGet(ARGS, kByte); |
| 553 | break; |
| 554 | |
| 555 | case Instruction::AGET_CHAR: |
| 556 | EmitInsn_AGet(ARGS, kChar); |
| 557 | break; |
| 558 | |
| 559 | case Instruction::AGET_SHORT: |
| 560 | EmitInsn_AGet(ARGS, kShort); |
| 561 | break; |
| 562 | |
| 563 | case Instruction::APUT: |
| 564 | EmitInsn_APut(ARGS, kInt); |
| 565 | break; |
| 566 | |
| 567 | case Instruction::APUT_WIDE: |
| 568 | EmitInsn_APut(ARGS, kLong); |
| 569 | break; |
| 570 | |
| 571 | case Instruction::APUT_OBJECT: |
| 572 | EmitInsn_APut(ARGS, kObject); |
| 573 | break; |
| 574 | |
| 575 | case Instruction::APUT_BOOLEAN: |
| 576 | EmitInsn_APut(ARGS, kBoolean); |
| 577 | break; |
| 578 | |
| 579 | case Instruction::APUT_BYTE: |
| 580 | EmitInsn_APut(ARGS, kByte); |
| 581 | break; |
| 582 | |
| 583 | case Instruction::APUT_CHAR: |
| 584 | EmitInsn_APut(ARGS, kChar); |
| 585 | break; |
| 586 | |
| 587 | case Instruction::APUT_SHORT: |
| 588 | EmitInsn_APut(ARGS, kShort); |
| 589 | break; |
| 590 | |
| 591 | case Instruction::IGET: |
| 592 | EmitInsn_IGet(ARGS, kInt); |
| 593 | break; |
| 594 | |
| 595 | case Instruction::IGET_WIDE: |
| 596 | EmitInsn_IGet(ARGS, kLong); |
| 597 | break; |
| 598 | |
| 599 | case Instruction::IGET_OBJECT: |
| 600 | EmitInsn_IGet(ARGS, kObject); |
| 601 | break; |
| 602 | |
| 603 | case Instruction::IGET_BOOLEAN: |
| 604 | EmitInsn_IGet(ARGS, kBoolean); |
| 605 | break; |
| 606 | |
| 607 | case Instruction::IGET_BYTE: |
| 608 | EmitInsn_IGet(ARGS, kByte); |
| 609 | break; |
| 610 | |
| 611 | case Instruction::IGET_CHAR: |
| 612 | EmitInsn_IGet(ARGS, kChar); |
| 613 | break; |
| 614 | |
| 615 | case Instruction::IGET_SHORT: |
| 616 | EmitInsn_IGet(ARGS, kShort); |
| 617 | break; |
| 618 | |
| 619 | case Instruction::IPUT: |
| 620 | EmitInsn_IPut(ARGS, kInt); |
| 621 | break; |
| 622 | |
| 623 | case Instruction::IPUT_WIDE: |
| 624 | EmitInsn_IPut(ARGS, kLong); |
| 625 | break; |
| 626 | |
| 627 | case Instruction::IPUT_OBJECT: |
| 628 | EmitInsn_IPut(ARGS, kObject); |
| 629 | break; |
| 630 | |
| 631 | case Instruction::IPUT_BOOLEAN: |
| 632 | EmitInsn_IPut(ARGS, kBoolean); |
| 633 | break; |
| 634 | |
| 635 | case Instruction::IPUT_BYTE: |
| 636 | EmitInsn_IPut(ARGS, kByte); |
| 637 | break; |
| 638 | |
| 639 | case Instruction::IPUT_CHAR: |
| 640 | EmitInsn_IPut(ARGS, kChar); |
| 641 | break; |
| 642 | |
| 643 | case Instruction::IPUT_SHORT: |
| 644 | EmitInsn_IPut(ARGS, kShort); |
| 645 | break; |
| 646 | |
| 647 | case Instruction::SGET: |
| 648 | EmitInsn_SGet(ARGS, kInt); |
| 649 | break; |
| 650 | |
| 651 | case Instruction::SGET_WIDE: |
| 652 | EmitInsn_SGet(ARGS, kLong); |
| 653 | break; |
| 654 | |
| 655 | case Instruction::SGET_OBJECT: |
| 656 | EmitInsn_SGet(ARGS, kObject); |
| 657 | break; |
| 658 | |
| 659 | case Instruction::SGET_BOOLEAN: |
| 660 | EmitInsn_SGet(ARGS, kBoolean); |
| 661 | break; |
| 662 | |
| 663 | case Instruction::SGET_BYTE: |
| 664 | EmitInsn_SGet(ARGS, kByte); |
| 665 | break; |
| 666 | |
| 667 | case Instruction::SGET_CHAR: |
| 668 | EmitInsn_SGet(ARGS, kChar); |
| 669 | break; |
| 670 | |
| 671 | case Instruction::SGET_SHORT: |
| 672 | EmitInsn_SGet(ARGS, kShort); |
| 673 | break; |
| 674 | |
| 675 | case Instruction::SPUT: |
| 676 | EmitInsn_SPut(ARGS, kInt); |
| 677 | break; |
| 678 | |
| 679 | case Instruction::SPUT_WIDE: |
| 680 | EmitInsn_SPut(ARGS, kLong); |
| 681 | break; |
| 682 | |
| 683 | case Instruction::SPUT_OBJECT: |
| 684 | EmitInsn_SPut(ARGS, kObject); |
| 685 | break; |
| 686 | |
| 687 | case Instruction::SPUT_BOOLEAN: |
| 688 | EmitInsn_SPut(ARGS, kBoolean); |
| 689 | break; |
| 690 | |
| 691 | case Instruction::SPUT_BYTE: |
| 692 | EmitInsn_SPut(ARGS, kByte); |
| 693 | break; |
| 694 | |
| 695 | case Instruction::SPUT_CHAR: |
| 696 | EmitInsn_SPut(ARGS, kChar); |
| 697 | break; |
| 698 | |
| 699 | case Instruction::SPUT_SHORT: |
| 700 | EmitInsn_SPut(ARGS, kShort); |
| 701 | break; |
| 702 | |
| 703 | |
| 704 | case Instruction::INVOKE_VIRTUAL: |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 705 | EmitInsn_Invoke(ARGS, kVirtual, kArgReg); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 706 | break; |
| 707 | |
| 708 | case Instruction::INVOKE_SUPER: |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 709 | EmitInsn_Invoke(ARGS, kSuper, kArgReg); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 710 | break; |
| 711 | |
| 712 | case Instruction::INVOKE_DIRECT: |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 713 | EmitInsn_Invoke(ARGS, kDirect, kArgReg); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 714 | break; |
| 715 | |
| 716 | case Instruction::INVOKE_STATIC: |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 717 | EmitInsn_Invoke(ARGS, kStatic, kArgReg); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 718 | break; |
| 719 | |
| 720 | case Instruction::INVOKE_INTERFACE: |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 721 | EmitInsn_Invoke(ARGS, kInterface, kArgReg); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 722 | break; |
| 723 | |
| 724 | case Instruction::INVOKE_VIRTUAL_RANGE: |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 725 | EmitInsn_Invoke(ARGS, kVirtual, kArgRange); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 726 | break; |
| 727 | |
| 728 | case Instruction::INVOKE_SUPER_RANGE: |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 729 | EmitInsn_Invoke(ARGS, kSuper, kArgRange); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 730 | break; |
| 731 | |
| 732 | case Instruction::INVOKE_DIRECT_RANGE: |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 733 | EmitInsn_Invoke(ARGS, kDirect, kArgRange); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 734 | break; |
| 735 | |
| 736 | case Instruction::INVOKE_STATIC_RANGE: |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 737 | EmitInsn_Invoke(ARGS, kStatic, kArgRange); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 738 | break; |
| 739 | |
| 740 | case Instruction::INVOKE_INTERFACE_RANGE: |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 741 | EmitInsn_Invoke(ARGS, kInterface, kArgRange); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 742 | break; |
| 743 | |
| 744 | case Instruction::NEG_INT: |
| 745 | EmitInsn_Neg(ARGS, kInt); |
| 746 | break; |
| 747 | |
| 748 | case Instruction::NOT_INT: |
| 749 | EmitInsn_Not(ARGS, kInt); |
| 750 | break; |
| 751 | |
| 752 | case Instruction::NEG_LONG: |
| 753 | EmitInsn_Neg(ARGS, kLong); |
| 754 | break; |
| 755 | |
| 756 | case Instruction::NOT_LONG: |
| 757 | EmitInsn_Not(ARGS, kLong); |
| 758 | break; |
| 759 | |
| 760 | case Instruction::NEG_FLOAT: |
| 761 | EmitInsn_FNeg(ARGS, kFloat); |
| 762 | break; |
| 763 | |
| 764 | case Instruction::NEG_DOUBLE: |
| 765 | EmitInsn_FNeg(ARGS, kDouble); |
| 766 | break; |
| 767 | |
| 768 | case Instruction::INT_TO_LONG: |
| 769 | EmitInsn_SExt(ARGS); |
| 770 | break; |
| 771 | |
| 772 | case Instruction::INT_TO_FLOAT: |
| 773 | EmitInsn_IntToFP(ARGS, kInt, kFloat); |
| 774 | break; |
| 775 | |
| 776 | case Instruction::INT_TO_DOUBLE: |
| 777 | EmitInsn_IntToFP(ARGS, kInt, kDouble); |
| 778 | break; |
| 779 | |
| 780 | case Instruction::LONG_TO_INT: |
| 781 | EmitInsn_Trunc(ARGS); |
| 782 | break; |
| 783 | |
| 784 | case Instruction::LONG_TO_FLOAT: |
| 785 | EmitInsn_IntToFP(ARGS, kLong, kFloat); |
| 786 | break; |
| 787 | |
| 788 | case Instruction::LONG_TO_DOUBLE: |
| 789 | EmitInsn_IntToFP(ARGS, kLong, kDouble); |
| 790 | break; |
| 791 | |
| 792 | case Instruction::FLOAT_TO_INT: |
TDYa127 | a474687 | 2012-04-11 23:48:55 -0700 | [diff] [blame] | 793 | EmitInsn_FPToInt(ARGS, kFloat, kInt, F2I); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 794 | break; |
| 795 | |
| 796 | case Instruction::FLOAT_TO_LONG: |
TDYa127 | a474687 | 2012-04-11 23:48:55 -0700 | [diff] [blame] | 797 | EmitInsn_FPToInt(ARGS, kFloat, kLong, F2L); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 798 | break; |
| 799 | |
| 800 | case Instruction::FLOAT_TO_DOUBLE: |
| 801 | EmitInsn_FExt(ARGS); |
| 802 | break; |
| 803 | |
| 804 | case Instruction::DOUBLE_TO_INT: |
TDYa127 | a474687 | 2012-04-11 23:48:55 -0700 | [diff] [blame] | 805 | EmitInsn_FPToInt(ARGS, kDouble, kInt, D2I); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 806 | break; |
| 807 | |
| 808 | case Instruction::DOUBLE_TO_LONG: |
TDYa127 | a474687 | 2012-04-11 23:48:55 -0700 | [diff] [blame] | 809 | EmitInsn_FPToInt(ARGS, kDouble, kLong, D2L); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 810 | break; |
| 811 | |
| 812 | case Instruction::DOUBLE_TO_FLOAT: |
| 813 | EmitInsn_FTrunc(ARGS); |
| 814 | break; |
| 815 | |
| 816 | case Instruction::INT_TO_BYTE: |
| 817 | EmitInsn_TruncAndSExt(ARGS, 8); |
| 818 | break; |
| 819 | |
| 820 | case Instruction::INT_TO_CHAR: |
| 821 | EmitInsn_TruncAndZExt(ARGS, 16); |
| 822 | break; |
| 823 | |
| 824 | case Instruction::INT_TO_SHORT: |
| 825 | EmitInsn_TruncAndSExt(ARGS, 16); |
| 826 | break; |
| 827 | |
| 828 | case Instruction::ADD_INT: |
| 829 | EmitInsn_IntArithm(ARGS, kIntArithm_Add, kInt, false); |
| 830 | break; |
| 831 | |
| 832 | case Instruction::SUB_INT: |
| 833 | EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kInt, false); |
| 834 | break; |
| 835 | |
| 836 | case Instruction::MUL_INT: |
| 837 | EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kInt, false); |
| 838 | break; |
| 839 | |
| 840 | case Instruction::DIV_INT: |
| 841 | EmitInsn_IntArithm(ARGS, kIntArithm_Div, kInt, false); |
| 842 | break; |
| 843 | |
| 844 | case Instruction::REM_INT: |
| 845 | EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kInt, false); |
| 846 | break; |
| 847 | |
| 848 | case Instruction::AND_INT: |
| 849 | EmitInsn_IntArithm(ARGS, kIntArithm_And, kInt, false); |
| 850 | break; |
| 851 | |
| 852 | case Instruction::OR_INT: |
| 853 | EmitInsn_IntArithm(ARGS, kIntArithm_Or, kInt, false); |
| 854 | break; |
| 855 | |
| 856 | case Instruction::XOR_INT: |
| 857 | EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kInt, false); |
| 858 | break; |
| 859 | |
| 860 | case Instruction::SHL_INT: |
Logan Chien | 5539ad0 | 2012-04-02 14:36:55 +0800 | [diff] [blame] | 861 | EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kInt, false); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 862 | break; |
| 863 | |
| 864 | case Instruction::SHR_INT: |
Logan Chien | 5539ad0 | 2012-04-02 14:36:55 +0800 | [diff] [blame] | 865 | EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kInt, false); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 866 | break; |
| 867 | |
| 868 | case Instruction::USHR_INT: |
Logan Chien | 5539ad0 | 2012-04-02 14:36:55 +0800 | [diff] [blame] | 869 | EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kInt, false); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 870 | break; |
| 871 | |
| 872 | case Instruction::ADD_LONG: |
| 873 | EmitInsn_IntArithm(ARGS, kIntArithm_Add, kLong, false); |
| 874 | break; |
| 875 | |
| 876 | case Instruction::SUB_LONG: |
| 877 | EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kLong, false); |
| 878 | break; |
| 879 | |
| 880 | case Instruction::MUL_LONG: |
| 881 | EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kLong, false); |
| 882 | break; |
| 883 | |
| 884 | case Instruction::DIV_LONG: |
| 885 | EmitInsn_IntArithm(ARGS, kIntArithm_Div, kLong, false); |
| 886 | break; |
| 887 | |
| 888 | case Instruction::REM_LONG: |
| 889 | EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kLong, false); |
| 890 | break; |
| 891 | |
| 892 | case Instruction::AND_LONG: |
| 893 | EmitInsn_IntArithm(ARGS, kIntArithm_And, kLong, false); |
| 894 | break; |
| 895 | |
| 896 | case Instruction::OR_LONG: |
| 897 | EmitInsn_IntArithm(ARGS, kIntArithm_Or, kLong, false); |
| 898 | break; |
| 899 | |
| 900 | case Instruction::XOR_LONG: |
| 901 | EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kLong, false); |
| 902 | break; |
| 903 | |
| 904 | case Instruction::SHL_LONG: |
Logan Chien | 5539ad0 | 2012-04-02 14:36:55 +0800 | [diff] [blame] | 905 | EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kLong, false); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 906 | break; |
| 907 | |
| 908 | case Instruction::SHR_LONG: |
Logan Chien | 5539ad0 | 2012-04-02 14:36:55 +0800 | [diff] [blame] | 909 | EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kLong, false); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 910 | break; |
| 911 | |
| 912 | case Instruction::USHR_LONG: |
Logan Chien | 5539ad0 | 2012-04-02 14:36:55 +0800 | [diff] [blame] | 913 | EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kLong, false); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 914 | break; |
| 915 | |
| 916 | case Instruction::ADD_FLOAT: |
| 917 | EmitInsn_FPArithm(ARGS, kFPArithm_Add, kFloat, false); |
| 918 | break; |
| 919 | |
| 920 | case Instruction::SUB_FLOAT: |
| 921 | EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kFloat, false); |
| 922 | break; |
| 923 | |
| 924 | case Instruction::MUL_FLOAT: |
| 925 | EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kFloat, false); |
| 926 | break; |
| 927 | |
| 928 | case Instruction::DIV_FLOAT: |
| 929 | EmitInsn_FPArithm(ARGS, kFPArithm_Div, kFloat, false); |
| 930 | break; |
| 931 | |
| 932 | case Instruction::REM_FLOAT: |
| 933 | EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kFloat, false); |
| 934 | break; |
| 935 | |
| 936 | case Instruction::ADD_DOUBLE: |
| 937 | EmitInsn_FPArithm(ARGS, kFPArithm_Add, kDouble, false); |
| 938 | break; |
| 939 | |
| 940 | case Instruction::SUB_DOUBLE: |
| 941 | EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kDouble, false); |
| 942 | break; |
| 943 | |
| 944 | case Instruction::MUL_DOUBLE: |
| 945 | EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kDouble, false); |
| 946 | break; |
| 947 | |
| 948 | case Instruction::DIV_DOUBLE: |
| 949 | EmitInsn_FPArithm(ARGS, kFPArithm_Div, kDouble, false); |
| 950 | break; |
| 951 | |
| 952 | case Instruction::REM_DOUBLE: |
| 953 | EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kDouble, false); |
| 954 | break; |
| 955 | |
| 956 | case Instruction::ADD_INT_2ADDR: |
| 957 | EmitInsn_IntArithm(ARGS, kIntArithm_Add, kInt, true); |
| 958 | break; |
| 959 | |
| 960 | case Instruction::SUB_INT_2ADDR: |
| 961 | EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kInt, true); |
| 962 | break; |
| 963 | |
| 964 | case Instruction::MUL_INT_2ADDR: |
| 965 | EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kInt, true); |
| 966 | break; |
| 967 | |
| 968 | case Instruction::DIV_INT_2ADDR: |
| 969 | EmitInsn_IntArithm(ARGS, kIntArithm_Div, kInt, true); |
| 970 | break; |
| 971 | |
| 972 | case Instruction::REM_INT_2ADDR: |
| 973 | EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kInt, true); |
| 974 | break; |
| 975 | |
| 976 | case Instruction::AND_INT_2ADDR: |
| 977 | EmitInsn_IntArithm(ARGS, kIntArithm_And, kInt, true); |
| 978 | break; |
| 979 | |
| 980 | case Instruction::OR_INT_2ADDR: |
| 981 | EmitInsn_IntArithm(ARGS, kIntArithm_Or, kInt, true); |
| 982 | break; |
| 983 | |
| 984 | case Instruction::XOR_INT_2ADDR: |
| 985 | EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kInt, true); |
| 986 | break; |
| 987 | |
| 988 | case Instruction::SHL_INT_2ADDR: |
Logan Chien | 5539ad0 | 2012-04-02 14:36:55 +0800 | [diff] [blame] | 989 | EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kInt, true); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 990 | break; |
| 991 | |
| 992 | case Instruction::SHR_INT_2ADDR: |
Logan Chien | 5539ad0 | 2012-04-02 14:36:55 +0800 | [diff] [blame] | 993 | EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kInt, true); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 994 | break; |
| 995 | |
| 996 | case Instruction::USHR_INT_2ADDR: |
Logan Chien | 5539ad0 | 2012-04-02 14:36:55 +0800 | [diff] [blame] | 997 | EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kInt, true); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 998 | break; |
| 999 | |
| 1000 | case Instruction::ADD_LONG_2ADDR: |
| 1001 | EmitInsn_IntArithm(ARGS, kIntArithm_Add, kLong, true); |
| 1002 | break; |
| 1003 | |
| 1004 | case Instruction::SUB_LONG_2ADDR: |
| 1005 | EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kLong, true); |
| 1006 | break; |
| 1007 | |
| 1008 | case Instruction::MUL_LONG_2ADDR: |
| 1009 | EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kLong, true); |
| 1010 | break; |
| 1011 | |
| 1012 | case Instruction::DIV_LONG_2ADDR: |
| 1013 | EmitInsn_IntArithm(ARGS, kIntArithm_Div, kLong, true); |
| 1014 | break; |
| 1015 | |
| 1016 | case Instruction::REM_LONG_2ADDR: |
| 1017 | EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kLong, true); |
| 1018 | break; |
| 1019 | |
| 1020 | case Instruction::AND_LONG_2ADDR: |
| 1021 | EmitInsn_IntArithm(ARGS, kIntArithm_And, kLong, true); |
| 1022 | break; |
| 1023 | |
| 1024 | case Instruction::OR_LONG_2ADDR: |
| 1025 | EmitInsn_IntArithm(ARGS, kIntArithm_Or, kLong, true); |
| 1026 | break; |
| 1027 | |
| 1028 | case Instruction::XOR_LONG_2ADDR: |
| 1029 | EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kLong, true); |
| 1030 | break; |
| 1031 | |
| 1032 | case Instruction::SHL_LONG_2ADDR: |
Logan Chien | 5539ad0 | 2012-04-02 14:36:55 +0800 | [diff] [blame] | 1033 | EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kLong, true); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1034 | break; |
| 1035 | |
| 1036 | case Instruction::SHR_LONG_2ADDR: |
Logan Chien | 5539ad0 | 2012-04-02 14:36:55 +0800 | [diff] [blame] | 1037 | EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kLong, true); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1038 | break; |
| 1039 | |
| 1040 | case Instruction::USHR_LONG_2ADDR: |
Logan Chien | 5539ad0 | 2012-04-02 14:36:55 +0800 | [diff] [blame] | 1041 | EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kLong, true); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1042 | break; |
| 1043 | |
| 1044 | case Instruction::ADD_FLOAT_2ADDR: |
| 1045 | EmitInsn_FPArithm(ARGS, kFPArithm_Add, kFloat, true); |
| 1046 | break; |
| 1047 | |
| 1048 | case Instruction::SUB_FLOAT_2ADDR: |
| 1049 | EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kFloat, true); |
| 1050 | break; |
| 1051 | |
| 1052 | case Instruction::MUL_FLOAT_2ADDR: |
| 1053 | EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kFloat, true); |
| 1054 | break; |
| 1055 | |
| 1056 | case Instruction::DIV_FLOAT_2ADDR: |
| 1057 | EmitInsn_FPArithm(ARGS, kFPArithm_Div, kFloat, true); |
| 1058 | break; |
| 1059 | |
| 1060 | case Instruction::REM_FLOAT_2ADDR: |
| 1061 | EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kFloat, true); |
| 1062 | break; |
| 1063 | |
| 1064 | case Instruction::ADD_DOUBLE_2ADDR: |
| 1065 | EmitInsn_FPArithm(ARGS, kFPArithm_Add, kDouble, true); |
| 1066 | break; |
| 1067 | |
| 1068 | case Instruction::SUB_DOUBLE_2ADDR: |
| 1069 | EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kDouble, true); |
| 1070 | break; |
| 1071 | |
| 1072 | case Instruction::MUL_DOUBLE_2ADDR: |
| 1073 | EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kDouble, true); |
| 1074 | break; |
| 1075 | |
| 1076 | case Instruction::DIV_DOUBLE_2ADDR: |
| 1077 | EmitInsn_FPArithm(ARGS, kFPArithm_Div, kDouble, true); |
| 1078 | break; |
| 1079 | |
| 1080 | case Instruction::REM_DOUBLE_2ADDR: |
| 1081 | EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kDouble, true); |
| 1082 | break; |
| 1083 | |
| 1084 | case Instruction::ADD_INT_LIT16: |
| 1085 | case Instruction::ADD_INT_LIT8: |
| 1086 | EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Add); |
| 1087 | break; |
| 1088 | |
| 1089 | case Instruction::RSUB_INT: |
| 1090 | case Instruction::RSUB_INT_LIT8: |
| 1091 | EmitInsn_RSubImmediate(ARGS); |
| 1092 | break; |
| 1093 | |
| 1094 | case Instruction::MUL_INT_LIT16: |
| 1095 | case Instruction::MUL_INT_LIT8: |
| 1096 | EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Mul); |
| 1097 | break; |
| 1098 | |
| 1099 | case Instruction::DIV_INT_LIT16: |
| 1100 | case Instruction::DIV_INT_LIT8: |
| 1101 | EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Div); |
| 1102 | break; |
| 1103 | |
| 1104 | case Instruction::REM_INT_LIT16: |
| 1105 | case Instruction::REM_INT_LIT8: |
| 1106 | EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Rem); |
| 1107 | break; |
| 1108 | |
| 1109 | case Instruction::AND_INT_LIT16: |
| 1110 | case Instruction::AND_INT_LIT8: |
| 1111 | EmitInsn_IntArithmImmediate(ARGS, kIntArithm_And); |
| 1112 | break; |
| 1113 | |
| 1114 | case Instruction::OR_INT_LIT16: |
| 1115 | case Instruction::OR_INT_LIT8: |
| 1116 | EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Or); |
| 1117 | break; |
| 1118 | |
| 1119 | case Instruction::XOR_INT_LIT16: |
| 1120 | case Instruction::XOR_INT_LIT8: |
| 1121 | EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Xor); |
| 1122 | break; |
| 1123 | |
| 1124 | case Instruction::SHL_INT_LIT8: |
Logan Chien | 5539ad0 | 2012-04-02 14:36:55 +0800 | [diff] [blame] | 1125 | EmitInsn_IntShiftArithmImmediate(ARGS, kIntArithm_Shl); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1126 | break; |
| 1127 | |
| 1128 | case Instruction::SHR_INT_LIT8: |
Logan Chien | 5539ad0 | 2012-04-02 14:36:55 +0800 | [diff] [blame] | 1129 | EmitInsn_IntShiftArithmImmediate(ARGS, kIntArithm_Shr); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1130 | break; |
| 1131 | |
| 1132 | case Instruction::USHR_INT_LIT8: |
Logan Chien | 5539ad0 | 2012-04-02 14:36:55 +0800 | [diff] [blame] | 1133 | EmitInsn_IntShiftArithmImmediate(ARGS, kIntArithm_UShr); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1134 | break; |
| 1135 | |
Logan Chien | 9e5f5c1 | 2012-04-10 13:51:45 +0800 | [diff] [blame] | 1136 | case Instruction::THROW_VERIFICATION_ERROR: |
| 1137 | EmitInsn_ThrowVerificationError(ARGS); |
| 1138 | break; |
| 1139 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1140 | case Instruction::UNUSED_3E: |
| 1141 | case Instruction::UNUSED_3F: |
| 1142 | case Instruction::UNUSED_40: |
| 1143 | case Instruction::UNUSED_41: |
| 1144 | case Instruction::UNUSED_42: |
| 1145 | case Instruction::UNUSED_43: |
| 1146 | case Instruction::UNUSED_73: |
| 1147 | case Instruction::UNUSED_79: |
| 1148 | case Instruction::UNUSED_7A: |
| 1149 | case Instruction::UNUSED_E3: |
| 1150 | case Instruction::UNUSED_E4: |
| 1151 | case Instruction::UNUSED_E5: |
| 1152 | case Instruction::UNUSED_E6: |
| 1153 | case Instruction::UNUSED_E7: |
| 1154 | case Instruction::UNUSED_E8: |
| 1155 | case Instruction::UNUSED_E9: |
| 1156 | case Instruction::UNUSED_EA: |
| 1157 | case Instruction::UNUSED_EB: |
| 1158 | case Instruction::UNUSED_EC: |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1159 | case Instruction::UNUSED_EE: |
| 1160 | case Instruction::UNUSED_EF: |
| 1161 | case Instruction::UNUSED_F0: |
| 1162 | case Instruction::UNUSED_F1: |
| 1163 | case Instruction::UNUSED_F2: |
| 1164 | case Instruction::UNUSED_F3: |
| 1165 | case Instruction::UNUSED_F4: |
| 1166 | case Instruction::UNUSED_F5: |
| 1167 | case Instruction::UNUSED_F6: |
| 1168 | case Instruction::UNUSED_F7: |
| 1169 | case Instruction::UNUSED_F8: |
| 1170 | case Instruction::UNUSED_F9: |
| 1171 | case Instruction::UNUSED_FA: |
| 1172 | case Instruction::UNUSED_FB: |
| 1173 | case Instruction::UNUSED_FC: |
| 1174 | case Instruction::UNUSED_FD: |
| 1175 | case Instruction::UNUSED_FE: |
| 1176 | case Instruction::UNUSED_FF: |
| 1177 | LOG(FATAL) << "Dex file contains UNUSED bytecode: " << insn->Opcode(); |
| 1178 | break; |
| 1179 | } |
| 1180 | |
| 1181 | #undef ARGS |
| 1182 | } |
| 1183 | |
| 1184 | |
| 1185 | void MethodCompiler::EmitInsn_Nop(uint32_t dex_pc, |
| 1186 | Instruction const* insn) { |
Logan Chien | e09a6b7 | 2011-12-27 17:50:21 +0800 | [diff] [blame] | 1187 | |
| 1188 | uint16_t insn_signature = code_item_->insns_[dex_pc]; |
| 1189 | |
| 1190 | if (insn_signature == Instruction::kPackedSwitchSignature || |
| 1191 | insn_signature == Instruction::kSparseSwitchSignature || |
| 1192 | insn_signature == Instruction::kArrayDataSignature) { |
| 1193 | irb_.CreateUnreachable(); |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 1194 | } else { |
Logan Chien | e09a6b7 | 2011-12-27 17:50:21 +0800 | [diff] [blame] | 1195 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 1196 | } |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1200 | void MethodCompiler::EmitInsn_Move(uint32_t dex_pc, |
| 1201 | Instruction const* insn, |
| 1202 | JType jty) { |
Logan Chien | 4817313 | 2011-12-27 17:51:13 +0800 | [diff] [blame] | 1203 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1204 | DecodedInstruction dec_insn(insn); |
Logan Chien | 4817313 | 2011-12-27 17:51:13 +0800 | [diff] [blame] | 1205 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1206 | llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, jty, kReg); |
| 1207 | EmitStoreDalvikReg(dec_insn.vA, jty, kReg, src_value); |
Logan Chien | 4817313 | 2011-12-27 17:51:13 +0800 | [diff] [blame] | 1208 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1209 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 1210 | } |
| 1211 | |
| 1212 | |
| 1213 | void MethodCompiler::EmitInsn_MoveResult(uint32_t dex_pc, |
| 1214 | Instruction const* insn, |
| 1215 | JType jty) { |
Logan Chien | 4817313 | 2011-12-27 17:51:13 +0800 | [diff] [blame] | 1216 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1217 | DecodedInstruction dec_insn(insn); |
Logan Chien | 4817313 | 2011-12-27 17:51:13 +0800 | [diff] [blame] | 1218 | |
| 1219 | llvm::Value* src_value = EmitLoadDalvikRetValReg(jty, kReg); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1220 | EmitStoreDalvikReg(dec_insn.vA, jty, kReg, src_value); |
Logan Chien | 4817313 | 2011-12-27 17:51:13 +0800 | [diff] [blame] | 1221 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1222 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 1223 | } |
| 1224 | |
| 1225 | |
| 1226 | void MethodCompiler::EmitInsn_MoveException(uint32_t dex_pc, |
| 1227 | Instruction const* insn) { |
Logan Chien | 3354cec | 2012-01-13 14:29:03 +0800 | [diff] [blame] | 1228 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1229 | DecodedInstruction dec_insn(insn); |
Logan Chien | 3354cec | 2012-01-13 14:29:03 +0800 | [diff] [blame] | 1230 | |
| 1231 | // Get thread-local exception field address |
| 1232 | llvm::Constant* exception_field_offset = |
| 1233 | irb_.getPtrEquivInt(Thread::ExceptionOffset().Int32Value()); |
| 1234 | |
| 1235 | llvm::Value* thread_object_addr = |
| 1236 | irb_.CreateCall(irb_.GetRuntime(GetCurrentThread)); |
| 1237 | |
| 1238 | llvm::Value* exception_field_addr = |
| 1239 | irb_.CreatePtrDisp(thread_object_addr, exception_field_offset, |
| 1240 | irb_.getJObjectTy()->getPointerTo()); |
| 1241 | |
| 1242 | // Get exception object address |
| 1243 | llvm::Value* exception_object_addr = irb_.CreateLoad(exception_field_addr); |
| 1244 | |
| 1245 | // Set thread-local exception field address to NULL |
| 1246 | irb_.CreateStore(irb_.getJNull(), exception_field_addr); |
| 1247 | |
| 1248 | // Keep the exception object in the Dalvik register |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1249 | EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, exception_object_addr); |
Logan Chien | 3354cec | 2012-01-13 14:29:03 +0800 | [diff] [blame] | 1250 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1251 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 1252 | } |
| 1253 | |
| 1254 | |
| 1255 | void MethodCompiler::EmitInsn_ThrowException(uint32_t dex_pc, |
| 1256 | Instruction const* insn) { |
Logan Chien | 6c6f12d | 2012-01-13 19:26:27 +0800 | [diff] [blame] | 1257 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1258 | DecodedInstruction dec_insn(insn); |
Logan Chien | 6c6f12d | 2012-01-13 19:26:27 +0800 | [diff] [blame] | 1259 | |
| 1260 | llvm::Value* exception_addr = |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1261 | EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate); |
Logan Chien | 6c6f12d | 2012-01-13 19:26:27 +0800 | [diff] [blame] | 1262 | |
TDYa127 | c8dc101 | 2012-04-19 07:03:33 -0700 | [diff] [blame] | 1263 | EmitUpdateDexPC(dex_pc); |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 1264 | |
Logan Chien | 6c6f12d | 2012-01-13 19:26:27 +0800 | [diff] [blame] | 1265 | irb_.CreateCall(irb_.GetRuntime(ThrowException), exception_addr); |
| 1266 | |
| 1267 | EmitBranchExceptionLandingPad(dex_pc); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1268 | } |
| 1269 | |
| 1270 | |
Logan Chien | 9e5f5c1 | 2012-04-10 13:51:45 +0800 | [diff] [blame] | 1271 | void MethodCompiler::EmitInsn_ThrowVerificationError(uint32_t dex_pc, |
| 1272 | Instruction const* insn) { |
| 1273 | |
| 1274 | DecodedInstruction dec_insn(insn); |
| 1275 | |
TDYa127 | c8dc101 | 2012-04-19 07:03:33 -0700 | [diff] [blame] | 1276 | EmitUpdateDexPC(dex_pc); |
Logan Chien | 9e5f5c1 | 2012-04-10 13:51:45 +0800 | [diff] [blame] | 1277 | |
| 1278 | llvm::Value* method_object_addr = EmitLoadMethodObjectAddr(); |
| 1279 | llvm::Value* kind_value = irb_.getInt32(dec_insn.vA); |
| 1280 | llvm::Value* ref_value = irb_.getInt32(dec_insn.vB); |
| 1281 | |
| 1282 | irb_.CreateCall3(irb_.GetRuntime(ThrowVerificationError), |
| 1283 | method_object_addr, kind_value, ref_value); |
| 1284 | |
| 1285 | EmitBranchExceptionLandingPad(dex_pc); |
| 1286 | } |
| 1287 | |
| 1288 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1289 | void MethodCompiler::EmitInsn_ReturnVoid(uint32_t dex_pc, |
| 1290 | Instruction const* insn) { |
Logan Chien | 8898a27 | 2011-12-27 17:51:56 +0800 | [diff] [blame] | 1291 | // Garbage collection safe-point |
| 1292 | EmitGuard_GarbageCollectionSuspend(dex_pc); |
| 1293 | |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 1294 | // Pop the shadow frame |
| 1295 | EmitPopShadowFrame(); |
| 1296 | |
Logan Chien | 8898a27 | 2011-12-27 17:51:56 +0800 | [diff] [blame] | 1297 | // Return! |
| 1298 | irb_.CreateRetVoid(); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1299 | } |
| 1300 | |
| 1301 | |
| 1302 | void MethodCompiler::EmitInsn_Return(uint32_t dex_pc, |
| 1303 | Instruction const* insn) { |
Logan Chien | 8898a27 | 2011-12-27 17:51:56 +0800 | [diff] [blame] | 1304 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1305 | DecodedInstruction dec_insn(insn); |
Logan Chien | 8898a27 | 2011-12-27 17:51:56 +0800 | [diff] [blame] | 1306 | |
| 1307 | // Garbage collection safe-point |
| 1308 | EmitGuard_GarbageCollectionSuspend(dex_pc); |
| 1309 | |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 1310 | // Pop the shadow frame |
| 1311 | EmitPopShadowFrame(); |
| 1312 | // NOTE: It is important to keep this AFTER the GC safe-point. Otherwise, |
| 1313 | // the return value might be collected since the shadow stack is popped. |
| 1314 | |
Logan Chien | 8898a27 | 2011-12-27 17:51:56 +0800 | [diff] [blame] | 1315 | // Return! |
Logan Chien | dd361c9 | 2012-04-10 23:40:37 +0800 | [diff] [blame] | 1316 | char ret_shorty = oat_compilation_unit_->GetShorty()[0]; |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1317 | llvm::Value* retval = EmitLoadDalvikReg(dec_insn.vA, ret_shorty, kAccurate); |
Logan Chien | 8898a27 | 2011-12-27 17:51:56 +0800 | [diff] [blame] | 1318 | |
| 1319 | irb_.CreateRet(retval); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1320 | } |
| 1321 | |
| 1322 | |
| 1323 | void MethodCompiler::EmitInsn_LoadConstant(uint32_t dex_pc, |
| 1324 | Instruction const* insn, |
| 1325 | JType imm_jty) { |
Shih-wei Liao | 798366e | 2012-02-16 09:25:33 -0800 | [diff] [blame] | 1326 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1327 | DecodedInstruction dec_insn(insn); |
Shih-wei Liao | 798366e | 2012-02-16 09:25:33 -0800 | [diff] [blame] | 1328 | |
| 1329 | DCHECK(imm_jty == kInt || imm_jty == kLong) << imm_jty; |
| 1330 | |
| 1331 | int64_t imm = 0; |
| 1332 | |
| 1333 | switch (insn->Opcode()) { |
| 1334 | // 32-bit Immediate |
| 1335 | case Instruction::CONST_4: |
| 1336 | case Instruction::CONST_16: |
| 1337 | case Instruction::CONST: |
| 1338 | case Instruction::CONST_WIDE_16: |
| 1339 | case Instruction::CONST_WIDE_32: |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1340 | imm = static_cast<int64_t>(static_cast<int32_t>(dec_insn.vB)); |
Shih-wei Liao | 798366e | 2012-02-16 09:25:33 -0800 | [diff] [blame] | 1341 | break; |
| 1342 | |
| 1343 | case Instruction::CONST_HIGH16: |
| 1344 | imm = static_cast<int64_t>(static_cast<int32_t>( |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1345 | static_cast<uint32_t>(static_cast<uint16_t>(dec_insn.vB)) << 16)); |
Shih-wei Liao | 798366e | 2012-02-16 09:25:33 -0800 | [diff] [blame] | 1346 | break; |
| 1347 | |
| 1348 | // 64-bit Immediate |
| 1349 | case Instruction::CONST_WIDE: |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1350 | imm = static_cast<int64_t>(dec_insn.vB_wide); |
Shih-wei Liao | 798366e | 2012-02-16 09:25:33 -0800 | [diff] [blame] | 1351 | break; |
| 1352 | |
| 1353 | case Instruction::CONST_WIDE_HIGH16: |
| 1354 | imm = static_cast<int64_t>( |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1355 | static_cast<uint64_t>(static_cast<uint16_t>(dec_insn.vB)) << 48); |
Shih-wei Liao | 798366e | 2012-02-16 09:25:33 -0800 | [diff] [blame] | 1356 | break; |
| 1357 | |
| 1358 | // Unknown opcode for load constant (unreachable) |
| 1359 | default: |
| 1360 | LOG(FATAL) << "Unknown opcode for load constant: " << insn->Opcode(); |
| 1361 | break; |
| 1362 | } |
| 1363 | |
| 1364 | // Store the non-object register |
| 1365 | llvm::Type* imm_type = irb_.getJType(imm_jty, kAccurate); |
| 1366 | llvm::Constant* imm_value = llvm::ConstantInt::getSigned(imm_type, imm); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1367 | EmitStoreDalvikReg(dec_insn.vA, imm_jty, kAccurate, imm_value); |
Shih-wei Liao | 798366e | 2012-02-16 09:25:33 -0800 | [diff] [blame] | 1368 | |
| 1369 | // Store the object register if it is possible to be null. |
| 1370 | if (imm_jty == kInt && imm == 0) { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1371 | EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, irb_.getJNull()); |
Shih-wei Liao | 798366e | 2012-02-16 09:25:33 -0800 | [diff] [blame] | 1372 | } |
| 1373 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1374 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 1375 | } |
| 1376 | |
| 1377 | |
| 1378 | void MethodCompiler::EmitInsn_LoadConstantString(uint32_t dex_pc, |
| 1379 | Instruction const* insn) { |
Logan Chien | c3b4ba1 | 2012-01-16 19:52:53 +0800 | [diff] [blame] | 1380 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1381 | DecodedInstruction dec_insn(insn); |
Logan Chien | c3b4ba1 | 2012-01-16 19:52:53 +0800 | [diff] [blame] | 1382 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1383 | uint32_t string_idx = dec_insn.vB; |
Logan Chien | c3b4ba1 | 2012-01-16 19:52:53 +0800 | [diff] [blame] | 1384 | |
| 1385 | llvm::Value* string_field_addr = EmitLoadDexCacheStringFieldAddr(string_idx); |
| 1386 | |
| 1387 | llvm::Value* string_addr = irb_.CreateLoad(string_field_addr); |
| 1388 | |
| 1389 | if (!compiler_->CanAssumeStringIsPresentInDexCache(dex_cache_, string_idx)) { |
| 1390 | llvm::BasicBlock* block_str_exist = |
| 1391 | CreateBasicBlockWithDexPC(dex_pc, "str_exist"); |
| 1392 | |
| 1393 | llvm::BasicBlock* block_str_resolve = |
| 1394 | CreateBasicBlockWithDexPC(dex_pc, "str_resolve"); |
| 1395 | |
| 1396 | // Test: Is the string resolved and in the dex cache? |
| 1397 | llvm::Value* equal_null = irb_.CreateICmpEQ(string_addr, irb_.getJNull()); |
| 1398 | |
TDYa127 | a849cb6 | 2012-04-01 05:59:34 -0700 | [diff] [blame] | 1399 | irb_.CreateCondBr(equal_null, block_str_resolve, block_str_exist); |
Logan Chien | c3b4ba1 | 2012-01-16 19:52:53 +0800 | [diff] [blame] | 1400 | |
| 1401 | // String is resolved, go to next basic block. |
| 1402 | irb_.SetInsertPoint(block_str_exist); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1403 | EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, string_addr); |
Logan Chien | c3b4ba1 | 2012-01-16 19:52:53 +0800 | [diff] [blame] | 1404 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 1405 | |
| 1406 | // String is not resolved yet, resolve it now. |
| 1407 | irb_.SetInsertPoint(block_str_resolve); |
| 1408 | |
| 1409 | llvm::Function* runtime_func = irb_.GetRuntime(ResolveString); |
| 1410 | |
| 1411 | llvm::Value* method_object_addr = EmitLoadMethodObjectAddr(); |
| 1412 | |
| 1413 | llvm::Value* string_idx_value = irb_.getInt32(string_idx); |
| 1414 | |
TDYa127 | c8dc101 | 2012-04-19 07:03:33 -0700 | [diff] [blame] | 1415 | EmitUpdateDexPC(dex_pc); |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 1416 | |
| 1417 | string_addr = irb_.CreateCall2(runtime_func, method_object_addr, |
| 1418 | string_idx_value); |
Logan Chien | c3b4ba1 | 2012-01-16 19:52:53 +0800 | [diff] [blame] | 1419 | |
| 1420 | EmitGuard_ExceptionLandingPad(dex_pc); |
| 1421 | } |
| 1422 | |
| 1423 | // Store the string object to the Dalvik register |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1424 | EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, string_addr); |
Logan Chien | c3b4ba1 | 2012-01-16 19:52:53 +0800 | [diff] [blame] | 1425 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1426 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 1427 | } |
| 1428 | |
| 1429 | |
Logan Chien | 27b3025 | 2012-01-14 03:43:35 +0800 | [diff] [blame] | 1430 | llvm::Value* MethodCompiler::EmitLoadConstantClass(uint32_t dex_pc, |
| 1431 | uint32_t type_idx) { |
| 1432 | if (!compiler_->CanAccessTypeWithoutChecks(method_idx_, dex_cache_, |
| 1433 | *dex_file_, type_idx)) { |
| 1434 | llvm::Value* type_idx_value = irb_.getInt32(type_idx); |
| 1435 | |
| 1436 | llvm::Value* method_object_addr = EmitLoadMethodObjectAddr(); |
| 1437 | |
TDYa127 | 706e9b6 | 2012-04-19 12:24:26 -0700 | [diff] [blame] | 1438 | llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread)); |
| 1439 | |
Logan Chien | 27b3025 | 2012-01-14 03:43:35 +0800 | [diff] [blame] | 1440 | llvm::Function* runtime_func = |
| 1441 | irb_.GetRuntime(InitializeTypeAndVerifyAccess); |
| 1442 | |
TDYa127 | c8dc101 | 2012-04-19 07:03:33 -0700 | [diff] [blame] | 1443 | EmitUpdateDexPC(dex_pc); |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 1444 | |
Logan Chien | 27b3025 | 2012-01-14 03:43:35 +0800 | [diff] [blame] | 1445 | llvm::Value* type_object_addr = |
TDYa127 | 706e9b6 | 2012-04-19 12:24:26 -0700 | [diff] [blame] | 1446 | irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr); |
Logan Chien | 27b3025 | 2012-01-14 03:43:35 +0800 | [diff] [blame] | 1447 | |
| 1448 | EmitGuard_ExceptionLandingPad(dex_pc); |
| 1449 | |
| 1450 | return type_object_addr; |
| 1451 | |
| 1452 | } else { |
| 1453 | // Try to load the class (type) object from the test cache. |
| 1454 | llvm::Value* type_field_addr = |
| 1455 | EmitLoadDexCacheResolvedTypeFieldAddr(type_idx); |
| 1456 | |
| 1457 | llvm::Value* type_object_addr = irb_.CreateLoad(type_field_addr); |
| 1458 | |
| 1459 | if (compiler_->CanAssumeTypeIsPresentInDexCache(dex_cache_, type_idx)) { |
| 1460 | return type_object_addr; |
| 1461 | } |
| 1462 | |
| 1463 | llvm::BasicBlock* block_original = irb_.GetInsertBlock(); |
| 1464 | |
| 1465 | // Test whether class (type) object is in the dex cache or not |
| 1466 | llvm::Value* equal_null = |
| 1467 | irb_.CreateICmpEQ(type_object_addr, irb_.getJNull()); |
| 1468 | |
| 1469 | llvm::BasicBlock* block_cont = |
| 1470 | CreateBasicBlockWithDexPC(dex_pc, "cont"); |
| 1471 | |
| 1472 | llvm::BasicBlock* block_load_class = |
| 1473 | CreateBasicBlockWithDexPC(dex_pc, "load_class"); |
| 1474 | |
| 1475 | irb_.CreateCondBr(equal_null, block_load_class, block_cont); |
| 1476 | |
| 1477 | // Failback routine to load the class object |
| 1478 | irb_.SetInsertPoint(block_load_class); |
| 1479 | |
| 1480 | llvm::Function* runtime_func = irb_.GetRuntime(InitializeType); |
| 1481 | |
| 1482 | llvm::Constant* type_idx_value = irb_.getInt32(type_idx); |
| 1483 | |
| 1484 | llvm::Value* method_object_addr = EmitLoadMethodObjectAddr(); |
| 1485 | |
TDYa127 | 706e9b6 | 2012-04-19 12:24:26 -0700 | [diff] [blame] | 1486 | llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread)); |
| 1487 | |
TDYa127 | c8dc101 | 2012-04-19 07:03:33 -0700 | [diff] [blame] | 1488 | EmitUpdateDexPC(dex_pc); |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 1489 | |
Logan Chien | 27b3025 | 2012-01-14 03:43:35 +0800 | [diff] [blame] | 1490 | llvm::Value* loaded_type_object_addr = |
TDYa127 | 706e9b6 | 2012-04-19 12:24:26 -0700 | [diff] [blame] | 1491 | irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr); |
Logan Chien | 27b3025 | 2012-01-14 03:43:35 +0800 | [diff] [blame] | 1492 | |
| 1493 | EmitGuard_ExceptionLandingPad(dex_pc); |
| 1494 | |
| 1495 | llvm::BasicBlock* block_after_load_class = irb_.GetInsertBlock(); |
| 1496 | |
| 1497 | irb_.CreateBr(block_cont); |
| 1498 | |
| 1499 | // Now the class object must be loaded |
| 1500 | irb_.SetInsertPoint(block_cont); |
| 1501 | |
| 1502 | llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2); |
| 1503 | |
| 1504 | phi->addIncoming(type_object_addr, block_original); |
| 1505 | phi->addIncoming(loaded_type_object_addr, block_after_load_class); |
| 1506 | |
| 1507 | return phi; |
| 1508 | } |
| 1509 | } |
| 1510 | |
| 1511 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1512 | void MethodCompiler::EmitInsn_LoadConstantClass(uint32_t dex_pc, |
| 1513 | Instruction const* insn) { |
Logan Chien | 27b3025 | 2012-01-14 03:43:35 +0800 | [diff] [blame] | 1514 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1515 | DecodedInstruction dec_insn(insn); |
Logan Chien | 27b3025 | 2012-01-14 03:43:35 +0800 | [diff] [blame] | 1516 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1517 | llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, dec_insn.vB); |
| 1518 | EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, type_object_addr); |
Logan Chien | 27b3025 | 2012-01-14 03:43:35 +0800 | [diff] [blame] | 1519 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1520 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 1521 | } |
| 1522 | |
| 1523 | |
| 1524 | void MethodCompiler::EmitInsn_MonitorEnter(uint32_t dex_pc, |
| 1525 | Instruction const* insn) { |
Logan Chien | 9e0dbe4 | 2012-01-13 12:11:37 +0800 | [diff] [blame] | 1526 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1527 | DecodedInstruction dec_insn(insn); |
Logan Chien | 9e0dbe4 | 2012-01-13 12:11:37 +0800 | [diff] [blame] | 1528 | |
| 1529 | llvm::Value* object_addr = |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1530 | EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate); |
Logan Chien | 9e0dbe4 | 2012-01-13 12:11:37 +0800 | [diff] [blame] | 1531 | |
| 1532 | // TODO: Slow path always. May not need NullPointerException check. |
| 1533 | EmitGuard_NullPointerException(dex_pc, object_addr); |
| 1534 | |
TDYa127 | c8dc101 | 2012-04-19 07:03:33 -0700 | [diff] [blame] | 1535 | EmitUpdateDexPC(dex_pc); |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 1536 | |
TDYa127 | 706e9b6 | 2012-04-19 12:24:26 -0700 | [diff] [blame] | 1537 | llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread)); |
| 1538 | |
| 1539 | irb_.CreateCall2(irb_.GetRuntime(LockObject), object_addr, thread_object_addr); |
Logan Chien | 9e0dbe4 | 2012-01-13 12:11:37 +0800 | [diff] [blame] | 1540 | EmitGuard_ExceptionLandingPad(dex_pc); |
| 1541 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1542 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 1543 | } |
| 1544 | |
| 1545 | |
| 1546 | void MethodCompiler::EmitInsn_MonitorExit(uint32_t dex_pc, |
| 1547 | Instruction const* insn) { |
Logan Chien | 9e0dbe4 | 2012-01-13 12:11:37 +0800 | [diff] [blame] | 1548 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1549 | DecodedInstruction dec_insn(insn); |
Logan Chien | 9e0dbe4 | 2012-01-13 12:11:37 +0800 | [diff] [blame] | 1550 | |
| 1551 | llvm::Value* object_addr = |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1552 | EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate); |
Logan Chien | 9e0dbe4 | 2012-01-13 12:11:37 +0800 | [diff] [blame] | 1553 | |
| 1554 | EmitGuard_NullPointerException(dex_pc, object_addr); |
| 1555 | |
TDYa127 | c8dc101 | 2012-04-19 07:03:33 -0700 | [diff] [blame] | 1556 | EmitUpdateDexPC(dex_pc); |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 1557 | |
TDYa127 | 706e9b6 | 2012-04-19 12:24:26 -0700 | [diff] [blame] | 1558 | llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread)); |
| 1559 | |
| 1560 | irb_.CreateCall2(irb_.GetRuntime(UnlockObject), object_addr, thread_object_addr); |
Logan Chien | 9e0dbe4 | 2012-01-13 12:11:37 +0800 | [diff] [blame] | 1561 | EmitGuard_ExceptionLandingPad(dex_pc); |
| 1562 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1563 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 1564 | } |
| 1565 | |
| 1566 | |
| 1567 | void MethodCompiler::EmitInsn_CheckCast(uint32_t dex_pc, |
| 1568 | Instruction const* insn) { |
Logan Chien | fc88095 | 2012-01-15 23:53:10 +0800 | [diff] [blame] | 1569 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1570 | DecodedInstruction dec_insn(insn); |
Logan Chien | fc88095 | 2012-01-15 23:53:10 +0800 | [diff] [blame] | 1571 | |
| 1572 | llvm::BasicBlock* block_test_class = |
| 1573 | CreateBasicBlockWithDexPC(dex_pc, "test_class"); |
| 1574 | |
| 1575 | llvm::BasicBlock* block_test_sub_class = |
| 1576 | CreateBasicBlockWithDexPC(dex_pc, "test_sub_class"); |
| 1577 | |
| 1578 | llvm::Value* object_addr = |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1579 | EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate); |
Logan Chien | fc88095 | 2012-01-15 23:53:10 +0800 | [diff] [blame] | 1580 | |
| 1581 | // Test: Is the reference equal to null? Act as no-op when it is null. |
| 1582 | llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull()); |
| 1583 | |
| 1584 | irb_.CreateCondBr(equal_null, |
| 1585 | GetNextBasicBlock(dex_pc), |
| 1586 | block_test_class); |
| 1587 | |
| 1588 | // Test: Is the object instantiated from the given class? |
| 1589 | irb_.SetInsertPoint(block_test_class); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1590 | llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, dec_insn.vB); |
Logan Chien | fc88095 | 2012-01-15 23:53:10 +0800 | [diff] [blame] | 1591 | DCHECK_EQ(Object::ClassOffset().Int32Value(), 0); |
| 1592 | |
| 1593 | llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy(); |
| 1594 | |
| 1595 | llvm::Value* object_type_field_addr = |
| 1596 | irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo()); |
| 1597 | |
| 1598 | llvm::Value* object_type_object_addr = |
| 1599 | irb_.CreateLoad(object_type_field_addr); |
| 1600 | |
| 1601 | llvm::Value* equal_class = |
| 1602 | irb_.CreateICmpEQ(type_object_addr, object_type_object_addr); |
| 1603 | |
| 1604 | irb_.CreateCondBr(equal_class, |
| 1605 | GetNextBasicBlock(dex_pc), |
| 1606 | block_test_sub_class); |
| 1607 | |
| 1608 | // Test: Is the object instantiated from the subclass of the given class? |
| 1609 | irb_.SetInsertPoint(block_test_sub_class); |
| 1610 | |
TDYa127 | c8dc101 | 2012-04-19 07:03:33 -0700 | [diff] [blame] | 1611 | EmitUpdateDexPC(dex_pc); |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 1612 | |
Logan Chien | fc88095 | 2012-01-15 23:53:10 +0800 | [diff] [blame] | 1613 | irb_.CreateCall2(irb_.GetRuntime(CheckCast), |
| 1614 | type_object_addr, object_type_object_addr); |
| 1615 | |
| 1616 | EmitGuard_ExceptionLandingPad(dex_pc); |
| 1617 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1618 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 1619 | } |
| 1620 | |
| 1621 | |
| 1622 | void MethodCompiler::EmitInsn_InstanceOf(uint32_t dex_pc, |
| 1623 | Instruction const* insn) { |
Logan Chien | 68725e2 | 2012-01-15 22:25:34 +0800 | [diff] [blame] | 1624 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1625 | DecodedInstruction dec_insn(insn); |
Logan Chien | 68725e2 | 2012-01-15 22:25:34 +0800 | [diff] [blame] | 1626 | |
| 1627 | llvm::Constant* zero = irb_.getJInt(0); |
| 1628 | llvm::Constant* one = irb_.getJInt(1); |
| 1629 | |
| 1630 | llvm::BasicBlock* block_nullp = CreateBasicBlockWithDexPC(dex_pc, "nullp"); |
| 1631 | |
| 1632 | llvm::BasicBlock* block_test_class = |
| 1633 | CreateBasicBlockWithDexPC(dex_pc, "test_class"); |
| 1634 | |
| 1635 | llvm::BasicBlock* block_class_equals = |
| 1636 | CreateBasicBlockWithDexPC(dex_pc, "class_eq"); |
| 1637 | |
| 1638 | llvm::BasicBlock* block_test_sub_class = |
| 1639 | CreateBasicBlockWithDexPC(dex_pc, "test_sub_class"); |
| 1640 | |
| 1641 | llvm::Value* object_addr = |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1642 | EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate); |
Logan Chien | 68725e2 | 2012-01-15 22:25:34 +0800 | [diff] [blame] | 1643 | |
| 1644 | // Overview of the following code : |
| 1645 | // We check for null, if so, then false, otherwise check for class == . If so |
| 1646 | // then true, otherwise do callout slowpath. |
| 1647 | // |
| 1648 | // Test: Is the reference equal to null? Set 0 when it is null. |
| 1649 | llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull()); |
| 1650 | |
| 1651 | irb_.CreateCondBr(equal_null, block_nullp, block_test_class); |
| 1652 | |
| 1653 | irb_.SetInsertPoint(block_nullp); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1654 | EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, zero); |
Logan Chien | 68725e2 | 2012-01-15 22:25:34 +0800 | [diff] [blame] | 1655 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 1656 | |
| 1657 | // Test: Is the object instantiated from the given class? |
| 1658 | irb_.SetInsertPoint(block_test_class); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1659 | llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, dec_insn.vC); |
Logan Chien | 68725e2 | 2012-01-15 22:25:34 +0800 | [diff] [blame] | 1660 | DCHECK_EQ(Object::ClassOffset().Int32Value(), 0); |
| 1661 | |
| 1662 | llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy(); |
| 1663 | |
| 1664 | llvm::Value* object_type_field_addr = |
| 1665 | irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo()); |
| 1666 | |
| 1667 | llvm::Value* object_type_object_addr = |
| 1668 | irb_.CreateLoad(object_type_field_addr); |
| 1669 | |
| 1670 | llvm::Value* equal_class = |
| 1671 | irb_.CreateICmpEQ(type_object_addr, object_type_object_addr); |
| 1672 | |
| 1673 | irb_.CreateCondBr(equal_class, block_class_equals, block_test_sub_class); |
| 1674 | |
| 1675 | irb_.SetInsertPoint(block_class_equals); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1676 | EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, one); |
Logan Chien | 68725e2 | 2012-01-15 22:25:34 +0800 | [diff] [blame] | 1677 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 1678 | |
| 1679 | // Test: Is the object instantiated from the subclass of the given class? |
| 1680 | irb_.SetInsertPoint(block_test_sub_class); |
| 1681 | |
| 1682 | llvm::Value* result = |
| 1683 | irb_.CreateCall2(irb_.GetRuntime(IsAssignable), |
| 1684 | type_object_addr, object_type_object_addr); |
| 1685 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1686 | EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result); |
Logan Chien | 68725e2 | 2012-01-15 22:25:34 +0800 | [diff] [blame] | 1687 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1688 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 1689 | } |
| 1690 | |
| 1691 | |
Logan Chien | 61bb614 | 2012-02-03 15:34:53 +0800 | [diff] [blame] | 1692 | llvm::Value* MethodCompiler::EmitLoadArrayLength(llvm::Value* array) { |
| 1693 | // Load array length field address |
| 1694 | llvm::Constant* array_len_field_offset = |
| 1695 | irb_.getPtrEquivInt(Array::LengthOffset().Int32Value()); |
| 1696 | |
| 1697 | llvm::Value* array_len_field_addr = |
| 1698 | irb_.CreatePtrDisp(array, array_len_field_offset, |
| 1699 | irb_.getJIntTy()->getPointerTo()); |
| 1700 | |
| 1701 | // Load array length |
| 1702 | return irb_.CreateLoad(array_len_field_addr); |
| 1703 | } |
| 1704 | |
| 1705 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1706 | void MethodCompiler::EmitInsn_ArrayLength(uint32_t dex_pc, |
| 1707 | Instruction const* insn) { |
Logan Chien | 61bb614 | 2012-02-03 15:34:53 +0800 | [diff] [blame] | 1708 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1709 | DecodedInstruction dec_insn(insn); |
Logan Chien | 61bb614 | 2012-02-03 15:34:53 +0800 | [diff] [blame] | 1710 | |
| 1711 | // Get the array object address |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1712 | llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate); |
Logan Chien | 61bb614 | 2012-02-03 15:34:53 +0800 | [diff] [blame] | 1713 | EmitGuard_NullPointerException(dex_pc, array_addr); |
| 1714 | |
| 1715 | // Get the array length and store it to the register |
| 1716 | llvm::Value* array_len = EmitLoadArrayLength(array_addr); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1717 | EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, array_len); |
Logan Chien | 61bb614 | 2012-02-03 15:34:53 +0800 | [diff] [blame] | 1718 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1719 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 1720 | } |
| 1721 | |
| 1722 | |
| 1723 | void MethodCompiler::EmitInsn_NewInstance(uint32_t dex_pc, |
| 1724 | Instruction const* insn) { |
Logan Chien | 032bdad | 2012-01-16 09:59:23 +0800 | [diff] [blame] | 1725 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1726 | DecodedInstruction dec_insn(insn); |
Logan Chien | 032bdad | 2012-01-16 09:59:23 +0800 | [diff] [blame] | 1727 | |
| 1728 | llvm::Function* runtime_func; |
Logan Chien | 1a032b1 | 2012-04-11 11:43:04 +0800 | [diff] [blame] | 1729 | if (compiler_->CanAccessInstantiableTypeWithoutChecks( |
| 1730 | method_idx_, dex_cache_, *dex_file_, dec_insn.vB)) { |
Logan Chien | 032bdad | 2012-01-16 09:59:23 +0800 | [diff] [blame] | 1731 | runtime_func = irb_.GetRuntime(AllocObject); |
| 1732 | } else { |
| 1733 | runtime_func = irb_.GetRuntime(AllocObjectWithAccessCheck); |
| 1734 | } |
| 1735 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1736 | llvm::Constant* type_index_value = irb_.getInt32(dec_insn.vB); |
Logan Chien | 032bdad | 2012-01-16 09:59:23 +0800 | [diff] [blame] | 1737 | |
| 1738 | llvm::Value* method_object_addr = EmitLoadMethodObjectAddr(); |
| 1739 | |
TDYa127 | da83d97 | 2012-04-18 00:21:49 -0700 | [diff] [blame] | 1740 | llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread)); |
| 1741 | |
TDYa127 | c8dc101 | 2012-04-19 07:03:33 -0700 | [diff] [blame] | 1742 | EmitUpdateDexPC(dex_pc); |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 1743 | |
Logan Chien | 032bdad | 2012-01-16 09:59:23 +0800 | [diff] [blame] | 1744 | llvm::Value* object_addr = |
TDYa127 | da83d97 | 2012-04-18 00:21:49 -0700 | [diff] [blame] | 1745 | irb_.CreateCall3(runtime_func, type_index_value, method_object_addr, thread_object_addr); |
Logan Chien | 032bdad | 2012-01-16 09:59:23 +0800 | [diff] [blame] | 1746 | |
| 1747 | EmitGuard_ExceptionLandingPad(dex_pc); |
| 1748 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1749 | EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, object_addr); |
Logan Chien | 032bdad | 2012-01-16 09:59:23 +0800 | [diff] [blame] | 1750 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1751 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 1752 | } |
| 1753 | |
| 1754 | |
Logan Chien | a2cc6a3 | 2012-01-16 10:38:41 +0800 | [diff] [blame] | 1755 | llvm::Value* MethodCompiler::EmitAllocNewArray(uint32_t dex_pc, |
| 1756 | int32_t length, |
| 1757 | uint32_t type_idx, |
| 1758 | bool is_filled_new_array) { |
| 1759 | llvm::Function* runtime_func; |
| 1760 | |
| 1761 | bool skip_access_check = |
| 1762 | compiler_->CanAccessTypeWithoutChecks(method_idx_, dex_cache_, |
| 1763 | *dex_file_, type_idx); |
| 1764 | |
TDYa127 | a849cb6 | 2012-04-01 05:59:34 -0700 | [diff] [blame] | 1765 | llvm::Value* array_length_value; |
| 1766 | |
Logan Chien | a2cc6a3 | 2012-01-16 10:38:41 +0800 | [diff] [blame] | 1767 | if (is_filled_new_array) { |
| 1768 | runtime_func = skip_access_check ? |
| 1769 | irb_.GetRuntime(CheckAndAllocArray) : |
| 1770 | irb_.GetRuntime(CheckAndAllocArrayWithAccessCheck); |
TDYa127 | a849cb6 | 2012-04-01 05:59:34 -0700 | [diff] [blame] | 1771 | array_length_value = irb_.getInt32(length); |
Logan Chien | a2cc6a3 | 2012-01-16 10:38:41 +0800 | [diff] [blame] | 1772 | } else { |
| 1773 | runtime_func = skip_access_check ? |
| 1774 | irb_.GetRuntime(AllocArray) : |
| 1775 | irb_.GetRuntime(AllocArrayWithAccessCheck); |
TDYa127 | a849cb6 | 2012-04-01 05:59:34 -0700 | [diff] [blame] | 1776 | array_length_value = EmitLoadDalvikReg(length, kInt, kAccurate); |
Logan Chien | a2cc6a3 | 2012-01-16 10:38:41 +0800 | [diff] [blame] | 1777 | } |
| 1778 | |
| 1779 | llvm::Constant* type_index_value = irb_.getInt32(type_idx); |
| 1780 | |
| 1781 | llvm::Value* method_object_addr = EmitLoadMethodObjectAddr(); |
| 1782 | |
TDYa127 | da83d97 | 2012-04-18 00:21:49 -0700 | [diff] [blame] | 1783 | llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread)); |
| 1784 | |
TDYa127 | c8dc101 | 2012-04-19 07:03:33 -0700 | [diff] [blame] | 1785 | EmitUpdateDexPC(dex_pc); |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 1786 | |
Logan Chien | a2cc6a3 | 2012-01-16 10:38:41 +0800 | [diff] [blame] | 1787 | llvm::Value* object_addr = |
TDYa127 | da83d97 | 2012-04-18 00:21:49 -0700 | [diff] [blame] | 1788 | irb_.CreateCall4(runtime_func, type_index_value, method_object_addr, |
| 1789 | array_length_value, thread_object_addr); |
Logan Chien | a2cc6a3 | 2012-01-16 10:38:41 +0800 | [diff] [blame] | 1790 | |
| 1791 | EmitGuard_ExceptionLandingPad(dex_pc); |
| 1792 | |
| 1793 | return object_addr; |
| 1794 | } |
| 1795 | |
| 1796 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1797 | void MethodCompiler::EmitInsn_NewArray(uint32_t dex_pc, |
| 1798 | Instruction const* insn) { |
Logan Chien | a2cc6a3 | 2012-01-16 10:38:41 +0800 | [diff] [blame] | 1799 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1800 | DecodedInstruction dec_insn(insn); |
Logan Chien | a2cc6a3 | 2012-01-16 10:38:41 +0800 | [diff] [blame] | 1801 | |
| 1802 | llvm::Value* object_addr = |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1803 | EmitAllocNewArray(dex_pc, dec_insn.vB, dec_insn.vC, false); |
Logan Chien | a2cc6a3 | 2012-01-16 10:38:41 +0800 | [diff] [blame] | 1804 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1805 | EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, object_addr); |
Logan Chien | a2cc6a3 | 2012-01-16 10:38:41 +0800 | [diff] [blame] | 1806 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1807 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 1808 | } |
| 1809 | |
| 1810 | |
| 1811 | void MethodCompiler::EmitInsn_FilledNewArray(uint32_t dex_pc, |
| 1812 | Instruction const* insn, |
| 1813 | bool is_range) { |
Logan Chien | a85fb2f | 2012-01-16 12:52:56 +0800 | [diff] [blame] | 1814 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1815 | DecodedInstruction dec_insn(insn); |
Logan Chien | a85fb2f | 2012-01-16 12:52:56 +0800 | [diff] [blame] | 1816 | |
| 1817 | llvm::Value* object_addr = |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1818 | EmitAllocNewArray(dex_pc, dec_insn.vA, dec_insn.vB, true); |
Logan Chien | a85fb2f | 2012-01-16 12:52:56 +0800 | [diff] [blame] | 1819 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1820 | if (dec_insn.vA > 0) { |
Logan Chien | dd361c9 | 2012-04-10 23:40:37 +0800 | [diff] [blame] | 1821 | // Resolve the element type |
TDYa127 | 183cf26 | 2012-04-11 07:53:21 -0700 | [diff] [blame] | 1822 | Class* klass = dex_cache_->GetResolvedType(dec_insn.vB)->GetComponentType(); |
Logan Chien | dd361c9 | 2012-04-10 23:40:37 +0800 | [diff] [blame] | 1823 | // TODO: Avoid the usage of the dex_cache_. Try to figure out a better |
| 1824 | // way to distinguish [I and [L. |
Logan Chien | a85fb2f | 2012-01-16 12:52:56 +0800 | [diff] [blame] | 1825 | CHECK_NE(klass, static_cast<Class*>(NULL)); |
Logan Chien | a85fb2f | 2012-01-16 12:52:56 +0800 | [diff] [blame] | 1826 | |
Logan Chien | dd361c9 | 2012-04-10 23:40:37 +0800 | [diff] [blame] | 1827 | uint32_t alignment; |
| 1828 | llvm::Constant* elem_size; |
| 1829 | llvm::PointerType* field_type; |
Logan Chien | a85fb2f | 2012-01-16 12:52:56 +0800 | [diff] [blame] | 1830 | |
Logan Chien | dd361c9 | 2012-04-10 23:40:37 +0800 | [diff] [blame] | 1831 | // NOTE: Currently filled-new-array only supports 'L', '[', and 'I' |
| 1832 | // as the element, thus we are only checking 2 cases: primitive int and |
| 1833 | // non-primitive type. |
Logan Chien | a85fb2f | 2012-01-16 12:52:56 +0800 | [diff] [blame] | 1834 | if (klass->IsPrimitiveInt()) { |
Logan Chien | dd361c9 | 2012-04-10 23:40:37 +0800 | [diff] [blame] | 1835 | alignment = sizeof(int32_t); |
| 1836 | elem_size = irb_.getPtrEquivInt(sizeof(int32_t)); |
Logan Chien | a85fb2f | 2012-01-16 12:52:56 +0800 | [diff] [blame] | 1837 | field_type = irb_.getJIntTy()->getPointerTo(); |
| 1838 | } else { |
| 1839 | CHECK(!klass->IsPrimitive()); |
Logan Chien | dd361c9 | 2012-04-10 23:40:37 +0800 | [diff] [blame] | 1840 | alignment = irb_.getSizeOfPtrEquivInt(); |
| 1841 | elem_size = irb_.getSizeOfPtrEquivIntValue(); |
Logan Chien | a85fb2f | 2012-01-16 12:52:56 +0800 | [diff] [blame] | 1842 | field_type = irb_.getJObjectTy()->getPointerTo(); |
| 1843 | } |
| 1844 | |
Logan Chien | dd361c9 | 2012-04-10 23:40:37 +0800 | [diff] [blame] | 1845 | llvm::Value* data_field_offset = |
| 1846 | irb_.getPtrEquivInt(Array::DataOffset(alignment).Int32Value()); |
| 1847 | |
| 1848 | llvm::Value* data_field_addr = |
| 1849 | irb_.CreatePtrDisp(object_addr, data_field_offset, field_type); |
| 1850 | |
Logan Chien | a85fb2f | 2012-01-16 12:52:56 +0800 | [diff] [blame] | 1851 | // TODO: Tune this code. Currently we are generating one instruction for |
| 1852 | // one element which may be very space consuming. Maybe changing to use |
| 1853 | // memcpy may help; however, since we can't guarantee that the alloca of |
| 1854 | // dalvik register are continuous, we can't perform such optimization yet. |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1855 | for (uint32_t i = 0; i < dec_insn.vA; ++i) { |
Logan Chien | a85fb2f | 2012-01-16 12:52:56 +0800 | [diff] [blame] | 1856 | int reg_index; |
| 1857 | if (is_range) { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1858 | reg_index = dec_insn.vC + i; |
Logan Chien | a85fb2f | 2012-01-16 12:52:56 +0800 | [diff] [blame] | 1859 | } else { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1860 | reg_index = dec_insn.arg[i]; |
Logan Chien | a85fb2f | 2012-01-16 12:52:56 +0800 | [diff] [blame] | 1861 | } |
| 1862 | |
| 1863 | llvm::Value* reg_value; |
| 1864 | if (klass->IsPrimitiveInt()) { |
| 1865 | reg_value = EmitLoadDalvikReg(reg_index, kInt, kAccurate); |
| 1866 | } else { |
| 1867 | reg_value = EmitLoadDalvikReg(reg_index, kObject, kAccurate); |
| 1868 | } |
| 1869 | |
| 1870 | irb_.CreateStore(reg_value, data_field_addr); |
| 1871 | |
Logan Chien | dd361c9 | 2012-04-10 23:40:37 +0800 | [diff] [blame] | 1872 | data_field_addr = |
| 1873 | irb_.CreatePtrDisp(data_field_addr, elem_size, field_type); |
Logan Chien | a85fb2f | 2012-01-16 12:52:56 +0800 | [diff] [blame] | 1874 | } |
| 1875 | } |
| 1876 | |
| 1877 | EmitStoreDalvikRetValReg(kObject, kAccurate, object_addr); |
| 1878 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1879 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 1880 | } |
| 1881 | |
| 1882 | |
| 1883 | void MethodCompiler::EmitInsn_FillArrayData(uint32_t dex_pc, |
| 1884 | Instruction const* insn) { |
Logan Chien | e58b658 | 2012-01-16 17:13:13 +0800 | [diff] [blame] | 1885 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1886 | DecodedInstruction dec_insn(insn); |
Logan Chien | e58b658 | 2012-01-16 17:13:13 +0800 | [diff] [blame] | 1887 | |
| 1888 | // Read the payload |
Logan Chien | e58b658 | 2012-01-16 17:13:13 +0800 | [diff] [blame] | 1889 | int32_t payload_offset = static_cast<int32_t>(dex_pc) + |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1890 | static_cast<int32_t>(dec_insn.vB); |
Logan Chien | e58b658 | 2012-01-16 17:13:13 +0800 | [diff] [blame] | 1891 | |
Logan Chien | 19c350a | 2012-05-01 19:21:32 +0800 | [diff] [blame] | 1892 | const Instruction::ArrayDataPayload* payload = |
| 1893 | reinterpret_cast<const Instruction::ArrayDataPayload*>( |
| 1894 | code_item_->insns_ + payload_offset); |
Logan Chien | e58b658 | 2012-01-16 17:13:13 +0800 | [diff] [blame] | 1895 | |
Logan Chien | 86f5067 | 2012-04-24 13:08:45 +0800 | [diff] [blame] | 1896 | // Load array object |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1897 | llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate); |
Logan Chien | e58b658 | 2012-01-16 17:13:13 +0800 | [diff] [blame] | 1898 | |
Logan Chien | 86f5067 | 2012-04-24 13:08:45 +0800 | [diff] [blame] | 1899 | if (payload->element_count == 0) { |
| 1900 | // When the number of the elements in the payload is zero, we don't have |
| 1901 | // to copy any numbers. However, we should check whether the array object |
| 1902 | // address is equal to null or not. |
| 1903 | EmitGuard_NullPointerException(dex_pc, array_addr); |
| 1904 | } else { |
| 1905 | // To save the code size, we are going to call the runtime function to |
| 1906 | // copy the content from DexFile. |
Logan Chien | e58b658 | 2012-01-16 17:13:13 +0800 | [diff] [blame] | 1907 | |
Logan Chien | 86f5067 | 2012-04-24 13:08:45 +0800 | [diff] [blame] | 1908 | // NOTE: We will check for the NullPointerException in the runtime. |
Logan Chien | e58b658 | 2012-01-16 17:13:13 +0800 | [diff] [blame] | 1909 | |
Logan Chien | 86f5067 | 2012-04-24 13:08:45 +0800 | [diff] [blame] | 1910 | llvm::Function* runtime_func = irb_.GetRuntime(FillArrayData); |
Logan Chien | e58b658 | 2012-01-16 17:13:13 +0800 | [diff] [blame] | 1911 | |
Logan Chien | 86f5067 | 2012-04-24 13:08:45 +0800 | [diff] [blame] | 1912 | llvm::Value* method_object_addr = EmitLoadMethodObjectAddr(); |
Logan Chien | e58b658 | 2012-01-16 17:13:13 +0800 | [diff] [blame] | 1913 | |
Logan Chien | 86f5067 | 2012-04-24 13:08:45 +0800 | [diff] [blame] | 1914 | EmitUpdateDexPC(dex_pc); |
Logan Chien | e58b658 | 2012-01-16 17:13:13 +0800 | [diff] [blame] | 1915 | |
Logan Chien | 86f5067 | 2012-04-24 13:08:45 +0800 | [diff] [blame] | 1916 | irb_.CreateCall4(runtime_func, |
| 1917 | method_object_addr, irb_.getInt32(dex_pc), |
| 1918 | array_addr, irb_.getInt32(payload_offset)); |
Logan Chien | e58b658 | 2012-01-16 17:13:13 +0800 | [diff] [blame] | 1919 | |
Logan Chien | 86f5067 | 2012-04-24 13:08:45 +0800 | [diff] [blame] | 1920 | EmitGuard_ExceptionLandingPad(dex_pc); |
Logan Chien | e58b658 | 2012-01-16 17:13:13 +0800 | [diff] [blame] | 1921 | } |
| 1922 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1923 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 1924 | } |
| 1925 | |
| 1926 | |
| 1927 | void MethodCompiler::EmitInsn_UnconditionalBranch(uint32_t dex_pc, |
| 1928 | Instruction const* insn) { |
Logan Chien | a466c16 | 2011-12-27 17:55:46 +0800 | [diff] [blame] | 1929 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1930 | DecodedInstruction dec_insn(insn); |
Logan Chien | a466c16 | 2011-12-27 17:55:46 +0800 | [diff] [blame] | 1931 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1932 | int32_t branch_offset = dec_insn.vA; |
Logan Chien | a466c16 | 2011-12-27 17:55:46 +0800 | [diff] [blame] | 1933 | |
| 1934 | if (branch_offset <= 0) { |
| 1935 | // Garbage collection safe-point on backward branch |
| 1936 | EmitGuard_GarbageCollectionSuspend(dex_pc); |
| 1937 | } |
| 1938 | |
| 1939 | irb_.CreateBr(GetBasicBlock(dex_pc + branch_offset)); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1940 | } |
| 1941 | |
| 1942 | |
| 1943 | void MethodCompiler::EmitInsn_PackedSwitch(uint32_t dex_pc, |
| 1944 | Instruction const* insn) { |
Logan Chien | 7a89b6d | 2011-12-27 17:56:56 +0800 | [diff] [blame] | 1945 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1946 | DecodedInstruction dec_insn(insn); |
Logan Chien | 7a89b6d | 2011-12-27 17:56:56 +0800 | [diff] [blame] | 1947 | |
Logan Chien | 7a89b6d | 2011-12-27 17:56:56 +0800 | [diff] [blame] | 1948 | int32_t payload_offset = static_cast<int32_t>(dex_pc) + |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1949 | static_cast<int32_t>(dec_insn.vB); |
Logan Chien | 7a89b6d | 2011-12-27 17:56:56 +0800 | [diff] [blame] | 1950 | |
Logan Chien | 19c350a | 2012-05-01 19:21:32 +0800 | [diff] [blame] | 1951 | const Instruction::PackedSwitchPayload* payload = |
| 1952 | reinterpret_cast<const Instruction::PackedSwitchPayload*>( |
| 1953 | code_item_->insns_ + payload_offset); |
Logan Chien | 7a89b6d | 2011-12-27 17:56:56 +0800 | [diff] [blame] | 1954 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1955 | llvm::Value* value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate); |
Logan Chien | 7a89b6d | 2011-12-27 17:56:56 +0800 | [diff] [blame] | 1956 | |
| 1957 | llvm::SwitchInst* sw = |
Logan Chien | 19c350a | 2012-05-01 19:21:32 +0800 | [diff] [blame] | 1958 | irb_.CreateSwitch(value, GetNextBasicBlock(dex_pc), payload->case_count); |
Logan Chien | 7a89b6d | 2011-12-27 17:56:56 +0800 | [diff] [blame] | 1959 | |
Logan Chien | 19c350a | 2012-05-01 19:21:32 +0800 | [diff] [blame] | 1960 | for (uint16_t i = 0; i < payload->case_count; ++i) { |
| 1961 | sw->addCase(irb_.getInt32(payload->first_key + i), |
| 1962 | GetBasicBlock(dex_pc + payload->targets[i])); |
Logan Chien | 7a89b6d | 2011-12-27 17:56:56 +0800 | [diff] [blame] | 1963 | } |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1964 | } |
| 1965 | |
| 1966 | |
| 1967 | void MethodCompiler::EmitInsn_SparseSwitch(uint32_t dex_pc, |
| 1968 | Instruction const* insn) { |
Logan Chien | 7a89b6d | 2011-12-27 17:56:56 +0800 | [diff] [blame] | 1969 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1970 | DecodedInstruction dec_insn(insn); |
Logan Chien | 7a89b6d | 2011-12-27 17:56:56 +0800 | [diff] [blame] | 1971 | |
Logan Chien | 7a89b6d | 2011-12-27 17:56:56 +0800 | [diff] [blame] | 1972 | int32_t payload_offset = static_cast<int32_t>(dex_pc) + |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1973 | static_cast<int32_t>(dec_insn.vB); |
Logan Chien | 7a89b6d | 2011-12-27 17:56:56 +0800 | [diff] [blame] | 1974 | |
Logan Chien | 19c350a | 2012-05-01 19:21:32 +0800 | [diff] [blame] | 1975 | const Instruction::SparseSwitchPayload* payload = |
| 1976 | reinterpret_cast<const Instruction::SparseSwitchPayload*>( |
| 1977 | code_item_->insns_ + payload_offset); |
Logan Chien | 7a89b6d | 2011-12-27 17:56:56 +0800 | [diff] [blame] | 1978 | |
Logan Chien | 19c350a | 2012-05-01 19:21:32 +0800 | [diff] [blame] | 1979 | const int32_t* keys = payload->GetKeys(); |
| 1980 | const int32_t* targets = payload->GetTargets(); |
Logan Chien | 7a89b6d | 2011-12-27 17:56:56 +0800 | [diff] [blame] | 1981 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1982 | llvm::Value* value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate); |
Logan Chien | 7a89b6d | 2011-12-27 17:56:56 +0800 | [diff] [blame] | 1983 | |
| 1984 | llvm::SwitchInst* sw = |
Logan Chien | 19c350a | 2012-05-01 19:21:32 +0800 | [diff] [blame] | 1985 | irb_.CreateSwitch(value, GetNextBasicBlock(dex_pc), payload->case_count); |
Logan Chien | 7a89b6d | 2011-12-27 17:56:56 +0800 | [diff] [blame] | 1986 | |
Logan Chien | 19c350a | 2012-05-01 19:21:32 +0800 | [diff] [blame] | 1987 | for (size_t i = 0; i < payload->case_count; ++i) { |
Logan Chien | 7a89b6d | 2011-12-27 17:56:56 +0800 | [diff] [blame] | 1988 | sw->addCase(irb_.getInt32(keys[i]), GetBasicBlock(dex_pc + targets[i])); |
| 1989 | } |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1990 | } |
| 1991 | |
| 1992 | |
| 1993 | void MethodCompiler::EmitInsn_FPCompare(uint32_t dex_pc, |
| 1994 | Instruction const* insn, |
| 1995 | JType fp_jty, |
| 1996 | bool gt_bias) { |
Logan Chien | 2c37e8e | 2011-12-27 17:58:46 +0800 | [diff] [blame] | 1997 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1998 | DecodedInstruction dec_insn(insn); |
Logan Chien | 2c37e8e | 2011-12-27 17:58:46 +0800 | [diff] [blame] | 1999 | |
| 2000 | DCHECK(fp_jty == kFloat || fp_jty == kDouble) << "JType: " << fp_jty; |
| 2001 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2002 | llvm::Value* src1_value = EmitLoadDalvikReg(dec_insn.vB, fp_jty, kAccurate); |
| 2003 | llvm::Value* src2_value = EmitLoadDalvikReg(dec_insn.vC, fp_jty, kAccurate); |
Logan Chien | 2c37e8e | 2011-12-27 17:58:46 +0800 | [diff] [blame] | 2004 | |
| 2005 | llvm::Value* cmp_eq = irb_.CreateFCmpOEQ(src1_value, src2_value); |
| 2006 | llvm::Value* cmp_lt; |
| 2007 | |
| 2008 | if (gt_bias) { |
| 2009 | cmp_lt = irb_.CreateFCmpOLT(src1_value, src2_value); |
| 2010 | } else { |
| 2011 | cmp_lt = irb_.CreateFCmpULT(src1_value, src2_value); |
| 2012 | } |
| 2013 | |
| 2014 | llvm::Value* result = EmitCompareResultSelection(cmp_eq, cmp_lt); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2015 | EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result); |
Logan Chien | 2c37e8e | 2011-12-27 17:58:46 +0800 | [diff] [blame] | 2016 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 2017 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 2018 | } |
| 2019 | |
| 2020 | |
| 2021 | void MethodCompiler::EmitInsn_LongCompare(uint32_t dex_pc, |
| 2022 | Instruction const* insn) { |
Logan Chien | 2c37e8e | 2011-12-27 17:58:46 +0800 | [diff] [blame] | 2023 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2024 | DecodedInstruction dec_insn(insn); |
Logan Chien | 2c37e8e | 2011-12-27 17:58:46 +0800 | [diff] [blame] | 2025 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2026 | llvm::Value* src1_value = EmitLoadDalvikReg(dec_insn.vB, kLong, kAccurate); |
| 2027 | llvm::Value* src2_value = EmitLoadDalvikReg(dec_insn.vC, kLong, kAccurate); |
Logan Chien | 2c37e8e | 2011-12-27 17:58:46 +0800 | [diff] [blame] | 2028 | |
| 2029 | llvm::Value* cmp_eq = irb_.CreateICmpEQ(src1_value, src2_value); |
| 2030 | llvm::Value* cmp_lt = irb_.CreateICmpSLT(src1_value, src2_value); |
| 2031 | |
| 2032 | llvm::Value* result = EmitCompareResultSelection(cmp_eq, cmp_lt); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2033 | EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result); |
Logan Chien | 2c37e8e | 2011-12-27 17:58:46 +0800 | [diff] [blame] | 2034 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 2035 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 2036 | } |
| 2037 | |
| 2038 | |
Logan Chien | 2c37e8e | 2011-12-27 17:58:46 +0800 | [diff] [blame] | 2039 | llvm::Value* MethodCompiler::EmitCompareResultSelection(llvm::Value* cmp_eq, |
| 2040 | llvm::Value* cmp_lt) { |
| 2041 | |
| 2042 | llvm::Constant* zero = irb_.getJInt(0); |
| 2043 | llvm::Constant* pos1 = irb_.getJInt(1); |
| 2044 | llvm::Constant* neg1 = irb_.getJInt(-1); |
| 2045 | |
| 2046 | llvm::Value* result_lt = irb_.CreateSelect(cmp_lt, neg1, pos1); |
| 2047 | llvm::Value* result_eq = irb_.CreateSelect(cmp_eq, zero, result_lt); |
| 2048 | |
| 2049 | return result_eq; |
| 2050 | } |
| 2051 | |
| 2052 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 2053 | void MethodCompiler::EmitInsn_BinaryConditionalBranch(uint32_t dex_pc, |
| 2054 | Instruction const* insn, |
| 2055 | CondBranchKind cond) { |
Logan Chien | a78e3c8 | 2011-12-27 17:59:35 +0800 | [diff] [blame] | 2056 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2057 | DecodedInstruction dec_insn(insn); |
Logan Chien | a78e3c8 | 2011-12-27 17:59:35 +0800 | [diff] [blame] | 2058 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2059 | int8_t src1_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vA); |
| 2060 | int8_t src2_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vB); |
Logan Chien | a78e3c8 | 2011-12-27 17:59:35 +0800 | [diff] [blame] | 2061 | |
| 2062 | DCHECK_NE(kRegUnknown, src1_reg_cat); |
| 2063 | DCHECK_NE(kRegUnknown, src2_reg_cat); |
| 2064 | DCHECK_NE(kRegCat2, src1_reg_cat); |
| 2065 | DCHECK_NE(kRegCat2, src2_reg_cat); |
| 2066 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2067 | int32_t branch_offset = dec_insn.vC; |
Logan Chien | a78e3c8 | 2011-12-27 17:59:35 +0800 | [diff] [blame] | 2068 | |
| 2069 | if (branch_offset <= 0) { |
| 2070 | // Garbage collection safe-point on backward branch |
| 2071 | EmitGuard_GarbageCollectionSuspend(dex_pc); |
| 2072 | } |
| 2073 | |
Logan Chien | a78e3c8 | 2011-12-27 17:59:35 +0800 | [diff] [blame] | 2074 | llvm::Value* src1_value; |
| 2075 | llvm::Value* src2_value; |
| 2076 | |
TDYa127 | 8e9b449 | 2012-04-24 15:50:27 -0700 | [diff] [blame] | 2077 | if (src1_reg_cat == kRegZero && src2_reg_cat == kRegZero) { |
| 2078 | src1_value = irb_.getInt32(0); |
| 2079 | src2_value = irb_.getInt32(0); |
| 2080 | } else if (src1_reg_cat != kRegZero && src2_reg_cat != kRegZero) { |
Logan Chien | a78e3c8 | 2011-12-27 17:59:35 +0800 | [diff] [blame] | 2081 | CHECK_EQ(src1_reg_cat, src2_reg_cat); |
| 2082 | |
| 2083 | if (src1_reg_cat == kRegCat1nr) { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2084 | src1_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate); |
| 2085 | src2_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate); |
Logan Chien | a78e3c8 | 2011-12-27 17:59:35 +0800 | [diff] [blame] | 2086 | } else { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2087 | src1_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate); |
| 2088 | src2_value = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate); |
Logan Chien | a78e3c8 | 2011-12-27 17:59:35 +0800 | [diff] [blame] | 2089 | } |
| 2090 | } else { |
| 2091 | DCHECK(src1_reg_cat == kRegZero || |
| 2092 | src2_reg_cat == kRegZero); |
| 2093 | |
| 2094 | if (src1_reg_cat == kRegZero) { |
| 2095 | if (src2_reg_cat == kRegCat1nr) { |
| 2096 | src1_value = irb_.getJInt(0); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2097 | src2_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate); |
Logan Chien | a78e3c8 | 2011-12-27 17:59:35 +0800 | [diff] [blame] | 2098 | } else { |
| 2099 | src1_value = irb_.getJNull(); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2100 | src2_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate); |
Logan Chien | a78e3c8 | 2011-12-27 17:59:35 +0800 | [diff] [blame] | 2101 | } |
| 2102 | } else { // src2_reg_cat == kRegZero |
| 2103 | if (src2_reg_cat == kRegCat1nr) { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2104 | src1_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate); |
Logan Chien | a78e3c8 | 2011-12-27 17:59:35 +0800 | [diff] [blame] | 2105 | src2_value = irb_.getJInt(0); |
| 2106 | } else { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2107 | src1_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate); |
Logan Chien | a78e3c8 | 2011-12-27 17:59:35 +0800 | [diff] [blame] | 2108 | src2_value = irb_.getJNull(); |
| 2109 | } |
| 2110 | } |
| 2111 | } |
| 2112 | |
| 2113 | llvm::Value* cond_value = |
| 2114 | EmitConditionResult(src1_value, src2_value, cond); |
| 2115 | |
| 2116 | irb_.CreateCondBr(cond_value, |
| 2117 | GetBasicBlock(dex_pc + branch_offset), |
| 2118 | GetNextBasicBlock(dex_pc)); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 2119 | } |
| 2120 | |
| 2121 | |
| 2122 | void MethodCompiler::EmitInsn_UnaryConditionalBranch(uint32_t dex_pc, |
| 2123 | Instruction const* insn, |
| 2124 | CondBranchKind cond) { |
Logan Chien | a78e3c8 | 2011-12-27 17:59:35 +0800 | [diff] [blame] | 2125 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2126 | DecodedInstruction dec_insn(insn); |
Logan Chien | a78e3c8 | 2011-12-27 17:59:35 +0800 | [diff] [blame] | 2127 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2128 | int8_t src_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vA); |
Logan Chien | a78e3c8 | 2011-12-27 17:59:35 +0800 | [diff] [blame] | 2129 | |
| 2130 | DCHECK_NE(kRegUnknown, src_reg_cat); |
| 2131 | DCHECK_NE(kRegCat2, src_reg_cat); |
| 2132 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2133 | int32_t branch_offset = dec_insn.vB; |
Logan Chien | a78e3c8 | 2011-12-27 17:59:35 +0800 | [diff] [blame] | 2134 | |
| 2135 | if (branch_offset <= 0) { |
| 2136 | // Garbage collection safe-point on backward branch |
| 2137 | EmitGuard_GarbageCollectionSuspend(dex_pc); |
| 2138 | } |
| 2139 | |
Logan Chien | a78e3c8 | 2011-12-27 17:59:35 +0800 | [diff] [blame] | 2140 | llvm::Value* src1_value; |
| 2141 | llvm::Value* src2_value; |
| 2142 | |
TDYa127 | 8e9b449 | 2012-04-24 15:50:27 -0700 | [diff] [blame] | 2143 | if (src_reg_cat == kRegZero) { |
| 2144 | src1_value = irb_.getInt32(0); |
| 2145 | src2_value = irb_.getInt32(0); |
| 2146 | } else if (src_reg_cat == kRegCat1nr) { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2147 | src1_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate); |
Logan Chien | a78e3c8 | 2011-12-27 17:59:35 +0800 | [diff] [blame] | 2148 | src2_value = irb_.getInt32(0); |
| 2149 | } else { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2150 | src1_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate); |
Logan Chien | a78e3c8 | 2011-12-27 17:59:35 +0800 | [diff] [blame] | 2151 | src2_value = irb_.getJNull(); |
| 2152 | } |
| 2153 | |
| 2154 | llvm::Value* cond_value = |
| 2155 | EmitConditionResult(src1_value, src2_value, cond); |
| 2156 | |
| 2157 | irb_.CreateCondBr(cond_value, |
| 2158 | GetBasicBlock(dex_pc + branch_offset), |
| 2159 | GetNextBasicBlock(dex_pc)); |
| 2160 | } |
| 2161 | |
| 2162 | |
| 2163 | RegCategory MethodCompiler::GetInferredRegCategory(uint32_t dex_pc, |
| 2164 | uint16_t reg_idx) { |
Logan Chien | dd361c9 | 2012-04-10 23:40:37 +0800 | [diff] [blame] | 2165 | |
| 2166 | Compiler::MethodReference mref(dex_file_, method_idx_); |
| 2167 | |
| 2168 | InferredRegCategoryMap const* map = |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 2169 | verifier::MethodVerifier::GetInferredRegCategoryMap(mref); |
Logan Chien | dd361c9 | 2012-04-10 23:40:37 +0800 | [diff] [blame] | 2170 | |
Logan Chien | a78e3c8 | 2011-12-27 17:59:35 +0800 | [diff] [blame] | 2171 | CHECK_NE(map, static_cast<InferredRegCategoryMap*>(NULL)); |
| 2172 | |
| 2173 | return map->GetRegCategory(dex_pc, reg_idx); |
| 2174 | } |
| 2175 | |
| 2176 | |
| 2177 | llvm::Value* MethodCompiler::EmitConditionResult(llvm::Value* lhs, |
| 2178 | llvm::Value* rhs, |
| 2179 | CondBranchKind cond) { |
| 2180 | switch (cond) { |
| 2181 | case kCondBranch_EQ: |
| 2182 | return irb_.CreateICmpEQ(lhs, rhs); |
| 2183 | |
| 2184 | case kCondBranch_NE: |
| 2185 | return irb_.CreateICmpNE(lhs, rhs); |
| 2186 | |
| 2187 | case kCondBranch_LT: |
| 2188 | return irb_.CreateICmpSLT(lhs, rhs); |
| 2189 | |
| 2190 | case kCondBranch_GE: |
| 2191 | return irb_.CreateICmpSGE(lhs, rhs); |
| 2192 | |
| 2193 | case kCondBranch_GT: |
| 2194 | return irb_.CreateICmpSGT(lhs, rhs); |
| 2195 | |
| 2196 | case kCondBranch_LE: |
| 2197 | return irb_.CreateICmpSLE(lhs, rhs); |
| 2198 | |
| 2199 | default: // Unreachable |
| 2200 | LOG(FATAL) << "Unknown conditional branch kind: " << cond; |
| 2201 | return NULL; |
| 2202 | } |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 2203 | } |
| 2204 | |
TDYa127 | 83bb662 | 2012-04-17 02:20:34 -0700 | [diff] [blame] | 2205 | void MethodCompiler::EmitMarkGCCard(llvm::Value* value, llvm::Value* target_addr) { |
| 2206 | // Using runtime support, let the target can override by InlineAssembly. |
| 2207 | llvm::Function* runtime_func = irb_.GetRuntime(MarkGCCard); |
| 2208 | |
| 2209 | irb_.CreateCall2(runtime_func, value, target_addr); |
| 2210 | } |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 2211 | |
Logan Chien | e27fdbb | 2012-01-02 23:27:26 +0800 | [diff] [blame] | 2212 | void |
| 2213 | MethodCompiler::EmitGuard_ArrayIndexOutOfBoundsException(uint32_t dex_pc, |
| 2214 | llvm::Value* array, |
| 2215 | llvm::Value* index) { |
| 2216 | llvm::Value* array_len = EmitLoadArrayLength(array); |
| 2217 | |
| 2218 | llvm::Value* cmp = irb_.CreateICmpUGE(index, array_len); |
| 2219 | |
| 2220 | llvm::BasicBlock* block_exception = |
| 2221 | CreateBasicBlockWithDexPC(dex_pc, "overflow"); |
| 2222 | |
| 2223 | llvm::BasicBlock* block_continue = |
| 2224 | CreateBasicBlockWithDexPC(dex_pc, "cont"); |
| 2225 | |
| 2226 | irb_.CreateCondBr(cmp, block_exception, block_continue); |
| 2227 | |
| 2228 | irb_.SetInsertPoint(block_exception); |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 2229 | |
TDYa127 | c8dc101 | 2012-04-19 07:03:33 -0700 | [diff] [blame] | 2230 | EmitUpdateDexPC(dex_pc); |
Logan Chien | e27fdbb | 2012-01-02 23:27:26 +0800 | [diff] [blame] | 2231 | irb_.CreateCall2(irb_.GetRuntime(ThrowIndexOutOfBounds), index, array_len); |
| 2232 | EmitBranchExceptionLandingPad(dex_pc); |
| 2233 | |
| 2234 | irb_.SetInsertPoint(block_continue); |
| 2235 | } |
| 2236 | |
| 2237 | |
| 2238 | void MethodCompiler::EmitGuard_ArrayException(uint32_t dex_pc, |
| 2239 | llvm::Value* array, |
| 2240 | llvm::Value* index) { |
| 2241 | EmitGuard_NullPointerException(dex_pc, array); |
| 2242 | EmitGuard_ArrayIndexOutOfBoundsException(dex_pc, array, index); |
| 2243 | } |
| 2244 | |
| 2245 | |
| 2246 | // Emit Array GetElementPtr |
| 2247 | llvm::Value* MethodCompiler::EmitArrayGEP(llvm::Value* array_addr, |
| 2248 | llvm::Value* index_value, |
Ian Rogers | 04ec04e | 2012-02-28 16:15:33 -0800 | [diff] [blame] | 2249 | llvm::Type* elem_type, |
| 2250 | JType elem_jty) { |
| 2251 | |
| 2252 | int data_offset; |
| 2253 | if (elem_jty == kLong || elem_jty == kDouble || |
| 2254 | (elem_jty == kObject && sizeof(uint64_t) == sizeof(Object*))) { |
| 2255 | data_offset = Array::DataOffset(sizeof(int64_t)).Int32Value(); |
| 2256 | } else { |
| 2257 | data_offset = Array::DataOffset(sizeof(int32_t)).Int32Value(); |
| 2258 | } |
Logan Chien | e27fdbb | 2012-01-02 23:27:26 +0800 | [diff] [blame] | 2259 | |
| 2260 | llvm::Constant* data_offset_value = |
Ian Rogers | 04ec04e | 2012-02-28 16:15:33 -0800 | [diff] [blame] | 2261 | irb_.getPtrEquivInt(data_offset); |
Logan Chien | e27fdbb | 2012-01-02 23:27:26 +0800 | [diff] [blame] | 2262 | |
| 2263 | llvm::Value* array_data_addr = |
| 2264 | irb_.CreatePtrDisp(array_addr, data_offset_value, |
| 2265 | elem_type->getPointerTo()); |
| 2266 | |
| 2267 | return irb_.CreateGEP(array_data_addr, index_value); |
| 2268 | } |
| 2269 | |
| 2270 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 2271 | void MethodCompiler::EmitInsn_AGet(uint32_t dex_pc, |
| 2272 | Instruction const* insn, |
| 2273 | JType elem_jty) { |
Logan Chien | e27fdbb | 2012-01-02 23:27:26 +0800 | [diff] [blame] | 2274 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2275 | DecodedInstruction dec_insn(insn); |
Logan Chien | e27fdbb | 2012-01-02 23:27:26 +0800 | [diff] [blame] | 2276 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2277 | llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate); |
| 2278 | llvm::Value* index_value = EmitLoadDalvikReg(dec_insn.vC, kInt, kAccurate); |
Logan Chien | e27fdbb | 2012-01-02 23:27:26 +0800 | [diff] [blame] | 2279 | |
| 2280 | EmitGuard_ArrayException(dex_pc, array_addr, index_value); |
| 2281 | |
| 2282 | llvm::Type* elem_type = irb_.getJType(elem_jty, kArray); |
| 2283 | |
| 2284 | llvm::Value* array_elem_addr = |
Ian Rogers | 04ec04e | 2012-02-28 16:15:33 -0800 | [diff] [blame] | 2285 | EmitArrayGEP(array_addr, index_value, elem_type, elem_jty); |
Logan Chien | e27fdbb | 2012-01-02 23:27:26 +0800 | [diff] [blame] | 2286 | |
| 2287 | llvm::Value* array_elem_value = irb_.CreateLoad(array_elem_addr); |
| 2288 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2289 | EmitStoreDalvikReg(dec_insn.vA, elem_jty, kArray, array_elem_value); |
Logan Chien | e27fdbb | 2012-01-02 23:27:26 +0800 | [diff] [blame] | 2290 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 2291 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 2292 | } |
| 2293 | |
| 2294 | |
| 2295 | void MethodCompiler::EmitInsn_APut(uint32_t dex_pc, |
| 2296 | Instruction const* insn, |
| 2297 | JType elem_jty) { |
Logan Chien | 8dabb43 | 2012-01-02 23:29:32 +0800 | [diff] [blame] | 2298 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2299 | DecodedInstruction dec_insn(insn); |
Logan Chien | 8dabb43 | 2012-01-02 23:29:32 +0800 | [diff] [blame] | 2300 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2301 | llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate); |
| 2302 | llvm::Value* index_value = EmitLoadDalvikReg(dec_insn.vC, kInt, kAccurate); |
Logan Chien | 8dabb43 | 2012-01-02 23:29:32 +0800 | [diff] [blame] | 2303 | |
| 2304 | EmitGuard_ArrayException(dex_pc, array_addr, index_value); |
| 2305 | |
| 2306 | llvm::Type* elem_type = irb_.getJType(elem_jty, kArray); |
| 2307 | |
| 2308 | llvm::Value* array_elem_addr = |
Ian Rogers | 04ec04e | 2012-02-28 16:15:33 -0800 | [diff] [blame] | 2309 | EmitArrayGEP(array_addr, index_value, elem_type, elem_jty); |
Logan Chien | 8dabb43 | 2012-01-02 23:29:32 +0800 | [diff] [blame] | 2310 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2311 | llvm::Value* new_value = EmitLoadDalvikReg(dec_insn.vA, elem_jty, kArray); |
Logan Chien | 8dabb43 | 2012-01-02 23:29:32 +0800 | [diff] [blame] | 2312 | |
TDYa127 | 83bb662 | 2012-04-17 02:20:34 -0700 | [diff] [blame] | 2313 | if (elem_jty == kObject) { // If put an object, check the type, and mark GC card table. |
TDYa127 | 1b86d07 | 2012-04-05 17:38:56 -0700 | [diff] [blame] | 2314 | llvm::Function* runtime_func = irb_.GetRuntime(CheckPutArrayElement); |
| 2315 | |
| 2316 | irb_.CreateCall2(runtime_func, new_value, array_addr); |
| 2317 | |
| 2318 | EmitGuard_ExceptionLandingPad(dex_pc); |
TDYa127 | 83bb662 | 2012-04-17 02:20:34 -0700 | [diff] [blame] | 2319 | |
| 2320 | EmitMarkGCCard(new_value, array_addr); |
TDYa127 | 1b86d07 | 2012-04-05 17:38:56 -0700 | [diff] [blame] | 2321 | } |
| 2322 | |
Logan Chien | 8dabb43 | 2012-01-02 23:29:32 +0800 | [diff] [blame] | 2323 | irb_.CreateStore(new_value, array_elem_addr); |
| 2324 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 2325 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 2326 | } |
| 2327 | |
| 2328 | |
| 2329 | void MethodCompiler::EmitInsn_IGet(uint32_t dex_pc, |
| 2330 | Instruction const* insn, |
| 2331 | JType field_jty) { |
Logan Chien | 48f1d2a | 2012-01-02 22:49:53 +0800 | [diff] [blame] | 2332 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2333 | DecodedInstruction dec_insn(insn); |
Logan Chien | 48f1d2a | 2012-01-02 22:49:53 +0800 | [diff] [blame] | 2334 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2335 | uint32_t reg_idx = dec_insn.vB; |
| 2336 | uint32_t field_idx = dec_insn.vC; |
Logan Chien | 48f1d2a | 2012-01-02 22:49:53 +0800 | [diff] [blame] | 2337 | |
Logan Chien | 48f1d2a | 2012-01-02 22:49:53 +0800 | [diff] [blame] | 2338 | llvm::Value* object_addr = EmitLoadDalvikReg(reg_idx, kObject, kAccurate); |
| 2339 | |
| 2340 | EmitGuard_NullPointerException(dex_pc, object_addr); |
| 2341 | |
| 2342 | llvm::Value* field_value; |
| 2343 | |
Logan Chien | 933abf8 | 2012-04-11 12:24:31 +0800 | [diff] [blame] | 2344 | int field_offset; |
| 2345 | bool is_volatile; |
| 2346 | bool is_fast_path = compiler_->ComputeInstanceFieldInfo( |
| 2347 | field_idx, oat_compilation_unit_, field_offset, is_volatile, false); |
Logan Chien | 48f1d2a | 2012-01-02 22:49:53 +0800 | [diff] [blame] | 2348 | |
Logan Chien | 933abf8 | 2012-04-11 12:24:31 +0800 | [diff] [blame] | 2349 | if (!is_fast_path) { |
Logan Chien | 48f1d2a | 2012-01-02 22:49:53 +0800 | [diff] [blame] | 2350 | llvm::Function* runtime_func; |
| 2351 | |
| 2352 | if (field_jty == kObject) { |
Logan Chien | 3b2b2e7 | 2012-03-06 16:11:45 +0800 | [diff] [blame] | 2353 | runtime_func = irb_.GetRuntime(GetObjectInstance); |
Logan Chien | 48f1d2a | 2012-01-02 22:49:53 +0800 | [diff] [blame] | 2354 | } else if (field_jty == kLong || field_jty == kDouble) { |
Logan Chien | 3b2b2e7 | 2012-03-06 16:11:45 +0800 | [diff] [blame] | 2355 | runtime_func = irb_.GetRuntime(Get64Instance); |
Logan Chien | 48f1d2a | 2012-01-02 22:49:53 +0800 | [diff] [blame] | 2356 | } else { |
Logan Chien | 3b2b2e7 | 2012-03-06 16:11:45 +0800 | [diff] [blame] | 2357 | runtime_func = irb_.GetRuntime(Get32Instance); |
Logan Chien | 48f1d2a | 2012-01-02 22:49:53 +0800 | [diff] [blame] | 2358 | } |
| 2359 | |
| 2360 | llvm::ConstantInt* field_idx_value = irb_.getInt32(field_idx); |
| 2361 | |
| 2362 | llvm::Value* method_object_addr = EmitLoadMethodObjectAddr(); |
| 2363 | |
TDYa127 | c8dc101 | 2012-04-19 07:03:33 -0700 | [diff] [blame] | 2364 | EmitUpdateDexPC(dex_pc); |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 2365 | |
Logan Chien | 3b2b2e7 | 2012-03-06 16:11:45 +0800 | [diff] [blame] | 2366 | field_value = irb_.CreateCall3(runtime_func, field_idx_value, |
| 2367 | method_object_addr, object_addr); |
Logan Chien | 48f1d2a | 2012-01-02 22:49:53 +0800 | [diff] [blame] | 2368 | |
| 2369 | EmitGuard_ExceptionLandingPad(dex_pc); |
| 2370 | |
| 2371 | } else { |
Logan Chien | 933abf8 | 2012-04-11 12:24:31 +0800 | [diff] [blame] | 2372 | DCHECK_GE(field_offset, 0); |
| 2373 | |
Logan Chien | 48f1d2a | 2012-01-02 22:49:53 +0800 | [diff] [blame] | 2374 | llvm::PointerType* field_type = |
| 2375 | irb_.getJType(field_jty, kField)->getPointerTo(); |
| 2376 | |
Logan Chien | 933abf8 | 2012-04-11 12:24:31 +0800 | [diff] [blame] | 2377 | llvm::ConstantInt* field_offset_value = irb_.getPtrEquivInt(field_offset); |
Logan Chien | 48f1d2a | 2012-01-02 22:49:53 +0800 | [diff] [blame] | 2378 | |
| 2379 | llvm::Value* field_addr = |
Logan Chien | 933abf8 | 2012-04-11 12:24:31 +0800 | [diff] [blame] | 2380 | irb_.CreatePtrDisp(object_addr, field_offset_value, field_type); |
Logan Chien | 48f1d2a | 2012-01-02 22:49:53 +0800 | [diff] [blame] | 2381 | |
Logan Chien | 933abf8 | 2012-04-11 12:24:31 +0800 | [diff] [blame] | 2382 | // TODO: Check is_volatile. We need to generate atomic load instruction |
| 2383 | // when is_volatile is true. |
Logan Chien | 48f1d2a | 2012-01-02 22:49:53 +0800 | [diff] [blame] | 2384 | field_value = irb_.CreateLoad(field_addr); |
| 2385 | } |
| 2386 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2387 | EmitStoreDalvikReg(dec_insn.vA, field_jty, kField, field_value); |
Logan Chien | 48f1d2a | 2012-01-02 22:49:53 +0800 | [diff] [blame] | 2388 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 2389 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 2390 | } |
| 2391 | |
| 2392 | |
| 2393 | void MethodCompiler::EmitInsn_IPut(uint32_t dex_pc, |
| 2394 | Instruction const* insn, |
| 2395 | JType field_jty) { |
Logan Chien | dd6aa87 | 2012-01-03 16:06:32 +0800 | [diff] [blame] | 2396 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2397 | DecodedInstruction dec_insn(insn); |
Logan Chien | dd6aa87 | 2012-01-03 16:06:32 +0800 | [diff] [blame] | 2398 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2399 | uint32_t reg_idx = dec_insn.vB; |
| 2400 | uint32_t field_idx = dec_insn.vC; |
Logan Chien | dd6aa87 | 2012-01-03 16:06:32 +0800 | [diff] [blame] | 2401 | |
Logan Chien | dd6aa87 | 2012-01-03 16:06:32 +0800 | [diff] [blame] | 2402 | llvm::Value* object_addr = EmitLoadDalvikReg(reg_idx, kObject, kAccurate); |
| 2403 | |
| 2404 | EmitGuard_NullPointerException(dex_pc, object_addr); |
| 2405 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2406 | llvm::Value* new_value = EmitLoadDalvikReg(dec_insn.vA, field_jty, kField); |
Logan Chien | dd6aa87 | 2012-01-03 16:06:32 +0800 | [diff] [blame] | 2407 | |
Logan Chien | 933abf8 | 2012-04-11 12:24:31 +0800 | [diff] [blame] | 2408 | int field_offset; |
| 2409 | bool is_volatile; |
| 2410 | bool is_fast_path = compiler_->ComputeInstanceFieldInfo( |
| 2411 | field_idx, oat_compilation_unit_, field_offset, is_volatile, true); |
Logan Chien | dd6aa87 | 2012-01-03 16:06:32 +0800 | [diff] [blame] | 2412 | |
Logan Chien | 933abf8 | 2012-04-11 12:24:31 +0800 | [diff] [blame] | 2413 | if (!is_fast_path) { |
Logan Chien | dd6aa87 | 2012-01-03 16:06:32 +0800 | [diff] [blame] | 2414 | llvm::Function* runtime_func; |
| 2415 | |
| 2416 | if (field_jty == kObject) { |
| 2417 | runtime_func = irb_.GetRuntime(SetObjectInstance); |
| 2418 | } else if (field_jty == kLong || field_jty == kDouble) { |
| 2419 | runtime_func = irb_.GetRuntime(Set64Instance); |
| 2420 | } else { |
| 2421 | runtime_func = irb_.GetRuntime(Set32Instance); |
| 2422 | } |
| 2423 | |
| 2424 | llvm::Value* field_idx_value = irb_.getInt32(field_idx); |
| 2425 | |
| 2426 | llvm::Value* method_object_addr = EmitLoadMethodObjectAddr(); |
| 2427 | |
TDYa127 | c8dc101 | 2012-04-19 07:03:33 -0700 | [diff] [blame] | 2428 | EmitUpdateDexPC(dex_pc); |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 2429 | |
Logan Chien | 3b2b2e7 | 2012-03-06 16:11:45 +0800 | [diff] [blame] | 2430 | irb_.CreateCall4(runtime_func, field_idx_value, |
| 2431 | method_object_addr, object_addr, new_value); |
Logan Chien | dd6aa87 | 2012-01-03 16:06:32 +0800 | [diff] [blame] | 2432 | |
| 2433 | EmitGuard_ExceptionLandingPad(dex_pc); |
| 2434 | |
| 2435 | } else { |
Logan Chien | 933abf8 | 2012-04-11 12:24:31 +0800 | [diff] [blame] | 2436 | DCHECK_GE(field_offset, 0); |
| 2437 | |
Logan Chien | dd6aa87 | 2012-01-03 16:06:32 +0800 | [diff] [blame] | 2438 | llvm::PointerType* field_type = |
| 2439 | irb_.getJType(field_jty, kField)->getPointerTo(); |
| 2440 | |
Logan Chien | 933abf8 | 2012-04-11 12:24:31 +0800 | [diff] [blame] | 2441 | llvm::Value* field_offset_value = irb_.getPtrEquivInt(field_offset); |
Logan Chien | dd6aa87 | 2012-01-03 16:06:32 +0800 | [diff] [blame] | 2442 | |
| 2443 | llvm::Value* field_addr = |
Logan Chien | 933abf8 | 2012-04-11 12:24:31 +0800 | [diff] [blame] | 2444 | irb_.CreatePtrDisp(object_addr, field_offset_value, field_type); |
Logan Chien | dd6aa87 | 2012-01-03 16:06:32 +0800 | [diff] [blame] | 2445 | |
Logan Chien | 933abf8 | 2012-04-11 12:24:31 +0800 | [diff] [blame] | 2446 | // TODO: Check is_volatile. We need to generate atomic store instruction |
| 2447 | // when is_volatile is true. |
Logan Chien | dd6aa87 | 2012-01-03 16:06:32 +0800 | [diff] [blame] | 2448 | irb_.CreateStore(new_value, field_addr); |
TDYa127 | 83bb662 | 2012-04-17 02:20:34 -0700 | [diff] [blame] | 2449 | |
| 2450 | if (field_jty == kObject) { // If put an object, mark the GC card table. |
| 2451 | EmitMarkGCCard(new_value, object_addr); |
| 2452 | } |
Logan Chien | dd6aa87 | 2012-01-03 16:06:32 +0800 | [diff] [blame] | 2453 | } |
| 2454 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 2455 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 2456 | } |
| 2457 | |
| 2458 | |
Logan Chien | 438c4b6 | 2012-01-17 16:06:00 +0800 | [diff] [blame] | 2459 | llvm::Value* MethodCompiler::EmitLoadStaticStorage(uint32_t dex_pc, |
| 2460 | uint32_t type_idx) { |
| 2461 | llvm::BasicBlock* block_load_static = |
| 2462 | CreateBasicBlockWithDexPC(dex_pc, "load_static"); |
| 2463 | |
| 2464 | llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont"); |
| 2465 | |
| 2466 | // Load static storage from dex cache |
| 2467 | llvm::Value* storage_field_addr = |
| 2468 | EmitLoadDexCacheStaticStorageFieldAddr(type_idx); |
| 2469 | |
| 2470 | llvm::Value* storage_object_addr = irb_.CreateLoad(storage_field_addr); |
| 2471 | |
| 2472 | llvm::BasicBlock* block_original = irb_.GetInsertBlock(); |
| 2473 | |
| 2474 | // Test: Is the static storage of this class initialized? |
| 2475 | llvm::Value* equal_null = |
| 2476 | irb_.CreateICmpEQ(storage_object_addr, irb_.getJNull()); |
| 2477 | |
| 2478 | irb_.CreateCondBr(equal_null, block_load_static, block_cont); |
| 2479 | |
| 2480 | // Failback routine to load the class object |
| 2481 | irb_.SetInsertPoint(block_load_static); |
| 2482 | |
| 2483 | llvm::Function* runtime_func = |
| 2484 | irb_.GetRuntime(InitializeStaticStorage); |
| 2485 | |
| 2486 | llvm::Constant* type_idx_value = irb_.getInt32(type_idx); |
| 2487 | |
| 2488 | llvm::Value* method_object_addr = EmitLoadMethodObjectAddr(); |
| 2489 | |
TDYa127 | 706e9b6 | 2012-04-19 12:24:26 -0700 | [diff] [blame] | 2490 | llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread)); |
| 2491 | |
TDYa127 | c8dc101 | 2012-04-19 07:03:33 -0700 | [diff] [blame] | 2492 | EmitUpdateDexPC(dex_pc); |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 2493 | |
Logan Chien | 438c4b6 | 2012-01-17 16:06:00 +0800 | [diff] [blame] | 2494 | llvm::Value* loaded_storage_object_addr = |
TDYa127 | 706e9b6 | 2012-04-19 12:24:26 -0700 | [diff] [blame] | 2495 | irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr); |
Logan Chien | 438c4b6 | 2012-01-17 16:06:00 +0800 | [diff] [blame] | 2496 | |
| 2497 | EmitGuard_ExceptionLandingPad(dex_pc); |
| 2498 | |
| 2499 | llvm::BasicBlock* block_after_load_static = irb_.GetInsertBlock(); |
| 2500 | |
| 2501 | irb_.CreateBr(block_cont); |
| 2502 | |
| 2503 | // Now the class object must be loaded |
| 2504 | irb_.SetInsertPoint(block_cont); |
| 2505 | |
| 2506 | llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2); |
| 2507 | |
| 2508 | phi->addIncoming(storage_object_addr, block_original); |
| 2509 | phi->addIncoming(loaded_storage_object_addr, block_after_load_static); |
| 2510 | |
| 2511 | return phi; |
| 2512 | } |
| 2513 | |
| 2514 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 2515 | void MethodCompiler::EmitInsn_SGet(uint32_t dex_pc, |
| 2516 | Instruction const* insn, |
| 2517 | JType field_jty) { |
Logan Chien | 438c4b6 | 2012-01-17 16:06:00 +0800 | [diff] [blame] | 2518 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2519 | DecodedInstruction dec_insn(insn); |
Logan Chien | 438c4b6 | 2012-01-17 16:06:00 +0800 | [diff] [blame] | 2520 | |
Logan Chien | 933abf8 | 2012-04-11 12:24:31 +0800 | [diff] [blame] | 2521 | uint32_t field_idx = dec_insn.vB; |
Logan Chien | 438c4b6 | 2012-01-17 16:06:00 +0800 | [diff] [blame] | 2522 | |
Logan Chien | 933abf8 | 2012-04-11 12:24:31 +0800 | [diff] [blame] | 2523 | int field_offset; |
| 2524 | int ssb_index; |
| 2525 | bool is_referrers_class; |
| 2526 | bool is_volatile; |
| 2527 | |
| 2528 | bool is_fast_path = compiler_->ComputeStaticFieldInfo( |
| 2529 | field_idx, oat_compilation_unit_, field_offset, ssb_index, |
| 2530 | is_referrers_class, is_volatile, false); |
Logan Chien | 438c4b6 | 2012-01-17 16:06:00 +0800 | [diff] [blame] | 2531 | |
| 2532 | llvm::Value* static_field_value; |
| 2533 | |
Logan Chien | 933abf8 | 2012-04-11 12:24:31 +0800 | [diff] [blame] | 2534 | if (!is_fast_path) { |
Logan Chien | 438c4b6 | 2012-01-17 16:06:00 +0800 | [diff] [blame] | 2535 | llvm::Function* runtime_func; |
| 2536 | |
| 2537 | if (field_jty == kObject) { |
| 2538 | runtime_func = irb_.GetRuntime(GetObjectStatic); |
| 2539 | } else if (field_jty == kLong || field_jty == kDouble) { |
| 2540 | runtime_func = irb_.GetRuntime(Get64Static); |
| 2541 | } else { |
| 2542 | runtime_func = irb_.GetRuntime(Get32Static); |
| 2543 | } |
| 2544 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2545 | llvm::Constant* field_idx_value = irb_.getInt32(dec_insn.vB); |
Logan Chien | 438c4b6 | 2012-01-17 16:06:00 +0800 | [diff] [blame] | 2546 | |
| 2547 | llvm::Value* method_object_addr = EmitLoadMethodObjectAddr(); |
| 2548 | |
TDYa127 | c8dc101 | 2012-04-19 07:03:33 -0700 | [diff] [blame] | 2549 | EmitUpdateDexPC(dex_pc); |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 2550 | |
Logan Chien | 438c4b6 | 2012-01-17 16:06:00 +0800 | [diff] [blame] | 2551 | static_field_value = |
| 2552 | irb_.CreateCall2(runtime_func, field_idx_value, method_object_addr); |
| 2553 | |
| 2554 | EmitGuard_ExceptionLandingPad(dex_pc); |
| 2555 | |
| 2556 | } else { |
Logan Chien | 933abf8 | 2012-04-11 12:24:31 +0800 | [diff] [blame] | 2557 | DCHECK_GE(field_offset, 0); |
Logan Chien | 438c4b6 | 2012-01-17 16:06:00 +0800 | [diff] [blame] | 2558 | |
Logan Chien | 933abf8 | 2012-04-11 12:24:31 +0800 | [diff] [blame] | 2559 | llvm::Value* static_storage_addr = NULL; |
| 2560 | |
| 2561 | if (is_referrers_class) { |
| 2562 | // Fast path, static storage base is this method's class |
| 2563 | llvm::Value* method_object_addr = EmitLoadMethodObjectAddr(); |
| 2564 | |
| 2565 | llvm::Constant* declaring_class_offset_value = |
| 2566 | irb_.getPtrEquivInt(Method::DeclaringClassOffset().Int32Value()); |
| 2567 | |
| 2568 | llvm::Value* static_storage_field_addr = |
| 2569 | irb_.CreatePtrDisp(method_object_addr, declaring_class_offset_value, |
| 2570 | irb_.getJObjectTy()->getPointerTo()); |
| 2571 | |
| 2572 | static_storage_addr = irb_.CreateLoad(static_storage_field_addr); |
| 2573 | } else { |
| 2574 | // Medium path, static storage base in a different class which |
| 2575 | // requires checks that the other class is initialized |
| 2576 | DCHECK_GE(ssb_index, 0); |
| 2577 | static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index); |
| 2578 | } |
| 2579 | |
| 2580 | llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset); |
Logan Chien | 438c4b6 | 2012-01-17 16:06:00 +0800 | [diff] [blame] | 2581 | |
| 2582 | llvm::Value* static_field_addr = |
| 2583 | irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value, |
| 2584 | irb_.getJType(field_jty, kField)->getPointerTo()); |
| 2585 | |
Logan Chien | 933abf8 | 2012-04-11 12:24:31 +0800 | [diff] [blame] | 2586 | // TODO: Check is_volatile. We need to generate atomic load instruction |
| 2587 | // when is_volatile is true. |
Logan Chien | 438c4b6 | 2012-01-17 16:06:00 +0800 | [diff] [blame] | 2588 | static_field_value = irb_.CreateLoad(static_field_addr); |
| 2589 | } |
| 2590 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2591 | EmitStoreDalvikReg(dec_insn.vA, field_jty, kField, static_field_value); |
Logan Chien | 438c4b6 | 2012-01-17 16:06:00 +0800 | [diff] [blame] | 2592 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 2593 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 2594 | } |
| 2595 | |
| 2596 | |
| 2597 | void MethodCompiler::EmitInsn_SPut(uint32_t dex_pc, |
| 2598 | Instruction const* insn, |
| 2599 | JType field_jty) { |
Logan Chien | 14179c8 | 2012-01-17 17:06:34 +0800 | [diff] [blame] | 2600 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2601 | DecodedInstruction dec_insn(insn); |
Logan Chien | 14179c8 | 2012-01-17 17:06:34 +0800 | [diff] [blame] | 2602 | |
Logan Chien | 933abf8 | 2012-04-11 12:24:31 +0800 | [diff] [blame] | 2603 | uint32_t field_idx = dec_insn.vB; |
Logan Chien | 14179c8 | 2012-01-17 17:06:34 +0800 | [diff] [blame] | 2604 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2605 | llvm::Value* new_value = EmitLoadDalvikReg(dec_insn.vA, field_jty, kField); |
Logan Chien | 14179c8 | 2012-01-17 17:06:34 +0800 | [diff] [blame] | 2606 | |
Logan Chien | 933abf8 | 2012-04-11 12:24:31 +0800 | [diff] [blame] | 2607 | int field_offset; |
| 2608 | int ssb_index; |
| 2609 | bool is_referrers_class; |
| 2610 | bool is_volatile; |
| 2611 | |
| 2612 | bool is_fast_path = compiler_->ComputeStaticFieldInfo( |
| 2613 | field_idx, oat_compilation_unit_, field_offset, ssb_index, |
| 2614 | is_referrers_class, is_volatile, true); |
| 2615 | |
| 2616 | if (!is_fast_path) { |
Logan Chien | 14179c8 | 2012-01-17 17:06:34 +0800 | [diff] [blame] | 2617 | llvm::Function* runtime_func; |
| 2618 | |
| 2619 | if (field_jty == kObject) { |
| 2620 | runtime_func = irb_.GetRuntime(SetObjectStatic); |
| 2621 | } else if (field_jty == kLong || field_jty == kDouble) { |
| 2622 | runtime_func = irb_.GetRuntime(Set64Static); |
| 2623 | } else { |
| 2624 | runtime_func = irb_.GetRuntime(Set32Static); |
| 2625 | } |
| 2626 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2627 | llvm::Constant* field_idx_value = irb_.getInt32(dec_insn.vB); |
Logan Chien | 14179c8 | 2012-01-17 17:06:34 +0800 | [diff] [blame] | 2628 | |
| 2629 | llvm::Value* method_object_addr = EmitLoadMethodObjectAddr(); |
| 2630 | |
TDYa127 | c8dc101 | 2012-04-19 07:03:33 -0700 | [diff] [blame] | 2631 | EmitUpdateDexPC(dex_pc); |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 2632 | |
Logan Chien | 14179c8 | 2012-01-17 17:06:34 +0800 | [diff] [blame] | 2633 | irb_.CreateCall3(runtime_func, field_idx_value, |
| 2634 | method_object_addr, new_value); |
| 2635 | |
| 2636 | EmitGuard_ExceptionLandingPad(dex_pc); |
| 2637 | |
| 2638 | } else { |
Logan Chien | 933abf8 | 2012-04-11 12:24:31 +0800 | [diff] [blame] | 2639 | DCHECK_GE(field_offset, 0); |
Logan Chien | 14179c8 | 2012-01-17 17:06:34 +0800 | [diff] [blame] | 2640 | |
Logan Chien | 933abf8 | 2012-04-11 12:24:31 +0800 | [diff] [blame] | 2641 | llvm::Value* static_storage_addr = NULL; |
| 2642 | |
| 2643 | if (is_referrers_class) { |
| 2644 | // Fast path, static storage base is this method's class |
| 2645 | llvm::Value* method_object_addr = EmitLoadMethodObjectAddr(); |
| 2646 | |
| 2647 | llvm::Constant* declaring_class_offset_value = |
| 2648 | irb_.getPtrEquivInt(Method::DeclaringClassOffset().Int32Value()); |
| 2649 | |
| 2650 | llvm::Value* static_storage_field_addr = |
| 2651 | irb_.CreatePtrDisp(method_object_addr, declaring_class_offset_value, |
| 2652 | irb_.getJObjectTy()->getPointerTo()); |
| 2653 | |
| 2654 | static_storage_addr = irb_.CreateLoad(static_storage_field_addr); |
| 2655 | } else { |
| 2656 | // Medium path, static storage base in a different class which |
| 2657 | // requires checks that the other class is initialized |
| 2658 | DCHECK_GE(ssb_index, 0); |
| 2659 | static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index); |
| 2660 | } |
| 2661 | |
| 2662 | llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset); |
Logan Chien | 14179c8 | 2012-01-17 17:06:34 +0800 | [diff] [blame] | 2663 | |
| 2664 | llvm::Value* static_field_addr = |
| 2665 | irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value, |
| 2666 | irb_.getJType(field_jty, kField)->getPointerTo()); |
| 2667 | |
Logan Chien | 933abf8 | 2012-04-11 12:24:31 +0800 | [diff] [blame] | 2668 | // TODO: Check is_volatile. We need to generate atomic store instruction |
| 2669 | // when is_volatile is true. |
Logan Chien | 14179c8 | 2012-01-17 17:06:34 +0800 | [diff] [blame] | 2670 | irb_.CreateStore(new_value, static_field_addr); |
TDYa127 | 83bb662 | 2012-04-17 02:20:34 -0700 | [diff] [blame] | 2671 | |
| 2672 | if (field_jty == kObject) { // If put an object, mark the GC card table. |
| 2673 | EmitMarkGCCard(new_value, static_storage_addr); |
| 2674 | } |
Logan Chien | 14179c8 | 2012-01-17 17:06:34 +0800 | [diff] [blame] | 2675 | } |
| 2676 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 2677 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 2678 | } |
| 2679 | |
| 2680 | |
Logan Chien | 1a121b9 | 2012-02-15 22:23:42 +0800 | [diff] [blame] | 2681 | void MethodCompiler:: |
| 2682 | EmitLoadActualParameters(std::vector<llvm::Value*>& args, |
| 2683 | uint32_t callee_method_idx, |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2684 | DecodedInstruction const& dec_insn, |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 2685 | InvokeArgFmt arg_fmt, |
Logan Chien | 1a121b9 | 2012-02-15 22:23:42 +0800 | [diff] [blame] | 2686 | bool is_static) { |
| 2687 | |
| 2688 | // Get method signature |
| 2689 | DexFile::MethodId const& method_id = |
| 2690 | dex_file_->GetMethodId(callee_method_idx); |
| 2691 | |
Logan Chien | 8faf802 | 2012-02-24 12:25:29 +0800 | [diff] [blame] | 2692 | uint32_t shorty_size; |
Logan Chien | 1a121b9 | 2012-02-15 22:23:42 +0800 | [diff] [blame] | 2693 | char const* shorty = dex_file_->GetMethodShorty(method_id, &shorty_size); |
Logan Chien | 8faf802 | 2012-02-24 12:25:29 +0800 | [diff] [blame] | 2694 | CHECK_GE(shorty_size, 1u); |
Logan Chien | 1a121b9 | 2012-02-15 22:23:42 +0800 | [diff] [blame] | 2695 | |
| 2696 | // Load argument values according to the shorty (without "this") |
| 2697 | uint16_t reg_count = 0; |
| 2698 | |
| 2699 | if (!is_static) { |
| 2700 | ++reg_count; // skip the "this" pointer |
| 2701 | } |
| 2702 | |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 2703 | bool is_range = (arg_fmt == kArgRange); |
| 2704 | |
Logan Chien | 8faf802 | 2012-02-24 12:25:29 +0800 | [diff] [blame] | 2705 | for (uint32_t i = 1; i < shorty_size; ++i) { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2706 | uint32_t reg_idx = (is_range) ? (dec_insn.vC + reg_count) |
| 2707 | : (dec_insn.arg[reg_count]); |
Logan Chien | 1a121b9 | 2012-02-15 22:23:42 +0800 | [diff] [blame] | 2708 | |
| 2709 | args.push_back(EmitLoadDalvikReg(reg_idx, shorty[i], kAccurate)); |
| 2710 | |
| 2711 | ++reg_count; |
| 2712 | if (shorty[i] == 'J' || shorty[i] == 'D') { |
| 2713 | // Wide types, such as long and double, are using a pair of registers |
| 2714 | // to store the value, so we have to increase arg_reg again. |
| 2715 | ++reg_count; |
| 2716 | } |
| 2717 | } |
| 2718 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2719 | DCHECK_EQ(reg_count, dec_insn.vA) |
Logan Chien | 1a121b9 | 2012-02-15 22:23:42 +0800 | [diff] [blame] | 2720 | << "Actual argument mismatch for callee: " |
| 2721 | << PrettyMethod(callee_method_idx, *dex_file_); |
| 2722 | } |
| 2723 | |
TDYa127 | 0b686e5 | 2012-04-09 22:43:35 -0700 | [diff] [blame] | 2724 | llvm::Value* MethodCompiler::EmitFixStub(llvm::Value* callee_method_object_addr, |
| 2725 | uint32_t method_idx, |
| 2726 | bool is_static) { |
| 2727 | // TODO: Remove this after we solve the link and trampoline related problems. |
| 2728 | llvm::Value* code_addr = irb_.CreateCall(irb_.GetRuntime(FixStub), callee_method_object_addr); |
| 2729 | |
| 2730 | llvm::FunctionType* method_type = GetFunctionType(method_idx, is_static); |
| 2731 | |
| 2732 | return irb_.CreatePointerCast(code_addr, method_type->getPointerTo()); |
TDYa127 | 8532191 | 2012-04-01 15:24:56 -0700 | [diff] [blame] | 2733 | } |
Logan Chien | 1a121b9 | 2012-02-15 22:23:42 +0800 | [diff] [blame] | 2734 | |
Shih-wei Liao | 399ed3f | 2012-03-08 01:27:04 -0800 | [diff] [blame] | 2735 | |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 2736 | void MethodCompiler::EmitInsn_Invoke(uint32_t dex_pc, |
| 2737 | Instruction const* insn, |
| 2738 | InvokeType invoke_type, |
| 2739 | InvokeArgFmt arg_fmt) { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2740 | DecodedInstruction dec_insn(insn); |
Logan Chien | 46fbb41 | 2012-02-15 22:29:08 +0800 | [diff] [blame] | 2741 | |
Logan Chien | 61c65dc | 2012-02-29 03:22:30 +0800 | [diff] [blame] | 2742 | bool is_static = (invoke_type == kStatic); |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 2743 | uint32_t callee_method_idx = dec_insn.vB; |
Logan Chien | 61c65dc | 2012-02-29 03:22:30 +0800 | [diff] [blame] | 2744 | |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 2745 | // Compute invoke related information for compiler decision |
| 2746 | int vtable_idx = -1; |
Logan Chien | 92ad16d | 2012-03-18 05:48:55 +0800 | [diff] [blame] | 2747 | uintptr_t direct_code = 0; // Currently unused |
Logan Chien | fca6437 | 2012-04-23 14:57:01 +0800 | [diff] [blame] | 2748 | uintptr_t direct_method = 0; |
Logan Chien | 61c65dc | 2012-02-29 03:22:30 +0800 | [diff] [blame] | 2749 | bool is_fast_path = compiler_-> |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 2750 | ComputeInvokeInfo(callee_method_idx, oat_compilation_unit_, |
Logan Chien | 92ad16d | 2012-03-18 05:48:55 +0800 | [diff] [blame] | 2751 | invoke_type, vtable_idx, direct_code, direct_method); |
Logan Chien | 61c65dc | 2012-02-29 03:22:30 +0800 | [diff] [blame] | 2752 | |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 2753 | // Load *this* actual parameter |
Logan Chien | 1a121b9 | 2012-02-15 22:23:42 +0800 | [diff] [blame] | 2754 | llvm::Value* this_addr = NULL; |
| 2755 | |
| 2756 | if (!is_static) { |
| 2757 | // Test: Is *this* parameter equal to null? |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 2758 | this_addr = (arg_fmt == kArgReg) ? |
| 2759 | EmitLoadDalvikReg(dec_insn.arg[0], kObject, kAccurate): |
| 2760 | EmitLoadDalvikReg(dec_insn.vC + 0, kObject, kAccurate); |
| 2761 | |
Logan Chien | 1a121b9 | 2012-02-15 22:23:42 +0800 | [diff] [blame] | 2762 | EmitGuard_NullPointerException(dex_pc, this_addr); |
| 2763 | } |
| 2764 | |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 2765 | // Load the method object |
TDYa127 | 4e42a59 | 2012-04-10 20:13:54 -0700 | [diff] [blame] | 2766 | llvm::Value* callee_method_object_addr = NULL; |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 2767 | |
| 2768 | if (!is_fast_path) { |
TDYa127 | 4e42a59 | 2012-04-10 20:13:54 -0700 | [diff] [blame] | 2769 | callee_method_object_addr = |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 2770 | EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx, invoke_type, |
| 2771 | this_addr, dex_pc, is_fast_path); |
| 2772 | } else { |
| 2773 | switch (invoke_type) { |
| 2774 | case kStatic: |
| 2775 | case kDirect: |
Logan Chien | fca6437 | 2012-04-23 14:57:01 +0800 | [diff] [blame] | 2776 | if (direct_method != 0u && |
| 2777 | direct_method != static_cast<uintptr_t>(-1)) { |
| 2778 | callee_method_object_addr = |
TDYa127 | 17826bf | 2012-04-24 01:15:10 -0700 | [diff] [blame] | 2779 | irb_.CreateIntToPtr(irb_.getPtrEquivInt(direct_method), |
| 2780 | irb_.getJObjectTy()); |
Logan Chien | fca6437 | 2012-04-23 14:57:01 +0800 | [diff] [blame] | 2781 | } else { |
| 2782 | callee_method_object_addr = |
| 2783 | EmitLoadSDCalleeMethodObjectAddr(callee_method_idx); |
| 2784 | } |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 2785 | break; |
| 2786 | |
| 2787 | case kVirtual: |
| 2788 | DCHECK(vtable_idx != -1); |
TDYa127 | 4e42a59 | 2012-04-10 20:13:54 -0700 | [diff] [blame] | 2789 | callee_method_object_addr = |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 2790 | EmitLoadVirtualCalleeMethodObjectAddr(vtable_idx, this_addr); |
| 2791 | break; |
| 2792 | |
| 2793 | case kSuper: |
| 2794 | LOG(FATAL) << "invoke-super should be promoted to invoke-direct in " |
| 2795 | "the fast path."; |
| 2796 | break; |
| 2797 | |
| 2798 | case kInterface: |
TDYa127 | 4e42a59 | 2012-04-10 20:13:54 -0700 | [diff] [blame] | 2799 | callee_method_object_addr = |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 2800 | EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx, |
| 2801 | invoke_type, this_addr, |
| 2802 | dex_pc, is_fast_path); |
| 2803 | break; |
| 2804 | } |
| 2805 | } |
| 2806 | |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 2807 | llvm::Value* code_addr = |
TDYa127 | ce15472 | 2012-04-21 16:43:29 -0700 | [diff] [blame] | 2808 | irb_.LoadFromObjectOffset(callee_method_object_addr, |
| 2809 | Method::GetCodeOffset().Int32Value(), |
| 2810 | GetFunctionType(callee_method_idx, is_static)->getPointerTo()); |
TDYa127 | 8532191 | 2012-04-01 15:24:56 -0700 | [diff] [blame] | 2811 | |
Logan Chien | 1a121b9 | 2012-02-15 22:23:42 +0800 | [diff] [blame] | 2812 | // Load the actual parameter |
| 2813 | std::vector<llvm::Value*> args; |
| 2814 | |
| 2815 | args.push_back(callee_method_object_addr); // method object for callee |
| 2816 | |
| 2817 | if (!is_static) { |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 2818 | DCHECK(this_addr != NULL); |
Logan Chien | 1a121b9 | 2012-02-15 22:23:42 +0800 | [diff] [blame] | 2819 | args.push_back(this_addr); // "this" object for callee |
| 2820 | } |
| 2821 | |
| 2822 | EmitLoadActualParameters(args, callee_method_idx, dec_insn, |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 2823 | arg_fmt, is_static); |
Logan Chien | 1a121b9 | 2012-02-15 22:23:42 +0800 | [diff] [blame] | 2824 | |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 2825 | #if 0 |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 2826 | // Invoke callee |
TDYa127 | c8dc101 | 2012-04-19 07:03:33 -0700 | [diff] [blame] | 2827 | EmitUpdateDexPC(dex_pc); |
Logan Chien | 1a121b9 | 2012-02-15 22:23:42 +0800 | [diff] [blame] | 2828 | llvm::Value* retval = irb_.CreateCall(code_addr, args); |
| 2829 | EmitGuard_ExceptionLandingPad(dex_pc); |
| 2830 | |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 2831 | uint32_t callee_access_flags = is_static ? kAccStatic : 0; |
| 2832 | UniquePtr<OatCompilationUnit> callee_oat_compilation_unit( |
| 2833 | oat_compilation_unit_->GetCallee(callee_method_idx, callee_access_flags)); |
| 2834 | |
| 2835 | char ret_shorty = callee_oat_compilation_unit->GetShorty()[0]; |
Shih-wei Liao | 90d5099 | 2012-02-19 03:32:05 -0800 | [diff] [blame] | 2836 | if (ret_shorty != 'V') { |
Logan Chien | 1a121b9 | 2012-02-15 22:23:42 +0800 | [diff] [blame] | 2837 | EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval); |
| 2838 | } |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 2839 | #else |
| 2840 | uint32_t callee_access_flags = is_static ? kAccStatic : 0; |
| 2841 | UniquePtr<OatCompilationUnit> callee_oat_compilation_unit( |
| 2842 | oat_compilation_unit_->GetCallee(callee_method_idx, callee_access_flags)); |
| 2843 | |
| 2844 | char ret_shorty = callee_oat_compilation_unit->GetShorty()[0]; |
| 2845 | |
| 2846 | |
TDYa127 | c8dc101 | 2012-04-19 07:03:33 -0700 | [diff] [blame] | 2847 | EmitUpdateDexPC(dex_pc); |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 2848 | |
| 2849 | |
| 2850 | llvm::BasicBlock* block_normal = CreateBasicBlockWithDexPC(dex_pc, "normal"); |
TDYa127 | ce15472 | 2012-04-21 16:43:29 -0700 | [diff] [blame] | 2851 | llvm::BasicBlock* block_stub = CreateBasicBlockWithDexPC(dex_pc, "stub"); |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 2852 | llvm::BasicBlock* block_continue = CreateBasicBlockWithDexPC(dex_pc, "cont"); |
| 2853 | |
| 2854 | llvm::Type* accurate_ret_type = irb_.getJType(ret_shorty, kAccurate); |
| 2855 | llvm::Value* retval_addr = NULL; |
| 2856 | if (ret_shorty != 'V') { |
| 2857 | retval_addr = irb_.CreateAlloca(accurate_ret_type); |
| 2858 | } |
| 2859 | |
| 2860 | |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 2861 | llvm::Value* code_addr_int = irb_.CreatePtrToInt(code_addr, irb_.getPtrEquivIntTy()); |
TDYa127 | ce15472 | 2012-04-21 16:43:29 -0700 | [diff] [blame] | 2862 | llvm::Value* max_stub_int = irb_.getPtrEquivInt(special_stub::kMaxSpecialStub); |
| 2863 | llvm::Value* is_stub = irb_.CreateICmpULT(code_addr_int, max_stub_int); |
| 2864 | irb_.CreateCondBr(is_stub, block_stub, block_normal); |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 2865 | |
| 2866 | |
| 2867 | irb_.SetInsertPoint(block_normal); |
| 2868 | { |
| 2869 | // Invoke callee |
TDYa127 | ce15472 | 2012-04-21 16:43:29 -0700 | [diff] [blame] | 2870 | llvm::Value* retval = irb_.CreateCall(code_addr, args); |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 2871 | if (ret_shorty != 'V') { |
TDYa127 | ce15472 | 2012-04-21 16:43:29 -0700 | [diff] [blame] | 2872 | EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval); |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 2873 | } |
| 2874 | } |
| 2875 | irb_.CreateBr(block_continue); |
| 2876 | |
| 2877 | |
TDYa127 | ce15472 | 2012-04-21 16:43:29 -0700 | [diff] [blame] | 2878 | irb_.SetInsertPoint(block_stub); |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 2879 | { |
TDYa127 | ce15472 | 2012-04-21 16:43:29 -0700 | [diff] [blame] | 2880 | { // lazy link |
| 2881 | // TODO: Remove this after we solve the link problem. |
| 2882 | llvm::BasicBlock* block_proxy_stub = CreateBasicBlockWithDexPC(dex_pc, "proxy"); |
| 2883 | llvm::BasicBlock* block_link = CreateBasicBlockWithDexPC(dex_pc, "link"); |
| 2884 | |
| 2885 | irb_.CreateCondBr(irb_.CreateIsNull(code_addr), block_link, block_proxy_stub); |
| 2886 | |
| 2887 | |
| 2888 | irb_.SetInsertPoint(block_link); |
| 2889 | code_addr = EmitFixStub(callee_method_object_addr, callee_method_idx, is_static); |
| 2890 | |
| 2891 | EmitGuard_ExceptionLandingPad(dex_pc); |
| 2892 | |
| 2893 | llvm::Value* retval = irb_.CreateCall(code_addr, args); |
| 2894 | if (ret_shorty != 'V') { |
| 2895 | EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval); |
| 2896 | } |
| 2897 | irb_.CreateBr(block_continue); |
| 2898 | |
| 2899 | |
| 2900 | irb_.SetInsertPoint(block_proxy_stub); |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 2901 | } |
TDYa127 | ce15472 | 2012-04-21 16:43:29 -0700 | [diff] [blame] | 2902 | { // proxy stub |
| 2903 | llvm::Value* temp_space_addr; |
| 2904 | if (ret_shorty != 'V') { |
| 2905 | temp_space_addr = irb_.CreateAlloca(irb_.getJValueTy()); |
| 2906 | args.push_back(temp_space_addr); |
| 2907 | } |
| 2908 | // TODO: Remove this after we solve the proxy trampoline calling convention problem. |
| 2909 | irb_.CreateCall(irb_.GetRuntime(ProxyInvokeHandler), args); |
| 2910 | if (ret_shorty != 'V') { |
| 2911 | llvm::Value* result_addr = |
| 2912 | irb_.CreateBitCast(temp_space_addr, accurate_ret_type->getPointerTo()); |
| 2913 | llvm::Value* retval = irb_.CreateLoad(result_addr); |
| 2914 | EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval); |
| 2915 | } |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 2916 | } |
| 2917 | } |
| 2918 | irb_.CreateBr(block_continue); |
| 2919 | |
| 2920 | |
| 2921 | irb_.SetInsertPoint(block_continue); |
| 2922 | |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 2923 | EmitGuard_ExceptionLandingPad(dex_pc); |
| 2924 | #endif |
Logan Chien | 1a121b9 | 2012-02-15 22:23:42 +0800 | [diff] [blame] | 2925 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 2926 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 2927 | } |
| 2928 | |
| 2929 | |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 2930 | llvm::Value* MethodCompiler:: |
| 2931 | EmitLoadSDCalleeMethodObjectAddr(uint32_t callee_method_idx) { |
| 2932 | llvm::Value* callee_method_object_field_addr = |
| 2933 | EmitLoadDexCacheResolvedMethodFieldAddr(callee_method_idx); |
Logan Chien | 7caf37e | 2012-02-03 22:56:04 +0800 | [diff] [blame] | 2934 | |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 2935 | return irb_.CreateLoad(callee_method_object_field_addr); |
| 2936 | } |
Logan Chien | 7caf37e | 2012-02-03 22:56:04 +0800 | [diff] [blame] | 2937 | |
Logan Chien | 7caf37e | 2012-02-03 22:56:04 +0800 | [diff] [blame] | 2938 | |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 2939 | llvm::Value* MethodCompiler:: |
| 2940 | EmitLoadVirtualCalleeMethodObjectAddr(int vtable_idx, |
| 2941 | llvm::Value* this_addr) { |
| 2942 | // Load class object of *this* pointer |
| 2943 | llvm::Constant* class_field_offset_value = |
| 2944 | irb_.getPtrEquivInt(Object::ClassOffset().Int32Value()); |
Logan Chien | 7caf37e | 2012-02-03 22:56:04 +0800 | [diff] [blame] | 2945 | |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 2946 | llvm::Value* class_field_addr = |
| 2947 | irb_.CreatePtrDisp(this_addr, class_field_offset_value, |
| 2948 | irb_.getJObjectTy()->getPointerTo()); |
Logan Chien | 7caf37e | 2012-02-03 22:56:04 +0800 | [diff] [blame] | 2949 | |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 2950 | llvm::Value* class_object_addr = irb_.CreateLoad(class_field_addr); |
Logan Chien | 7caf37e | 2012-02-03 22:56:04 +0800 | [diff] [blame] | 2951 | |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 2952 | // Load vtable address |
| 2953 | llvm::Constant* vtable_offset_value = |
| 2954 | irb_.getPtrEquivInt(Class::VTableOffset().Int32Value()); |
| 2955 | |
| 2956 | llvm::Value* vtable_field_addr = |
| 2957 | irb_.CreatePtrDisp(class_object_addr, vtable_offset_value, |
| 2958 | irb_.getJObjectTy()->getPointerTo()); |
| 2959 | |
| 2960 | llvm::Value* vtable_addr = irb_.CreateLoad(vtable_field_addr); |
| 2961 | |
| 2962 | // Load callee method object |
| 2963 | llvm::Value* vtable_idx_value = |
| 2964 | irb_.getPtrEquivInt(static_cast<uint64_t>(vtable_idx)); |
| 2965 | |
| 2966 | llvm::Value* method_field_addr = |
| 2967 | EmitArrayGEP(vtable_addr, vtable_idx_value, irb_.getJObjectTy(), kObject); |
| 2968 | |
| 2969 | return irb_.CreateLoad(method_field_addr); |
| 2970 | } |
| 2971 | |
| 2972 | |
| 2973 | llvm::Value* MethodCompiler:: |
| 2974 | EmitCallRuntimeForCalleeMethodObjectAddr(uint32_t callee_method_idx, |
| 2975 | InvokeType invoke_type, |
| 2976 | llvm::Value* this_addr, |
| 2977 | uint32_t dex_pc, |
| 2978 | bool is_fast_path) { |
| 2979 | |
| 2980 | llvm::Function* runtime_func = NULL; |
| 2981 | |
| 2982 | switch (invoke_type) { |
| 2983 | case kStatic: |
| 2984 | runtime_func = irb_.GetRuntime(FindStaticMethodWithAccessCheck); |
| 2985 | break; |
| 2986 | |
| 2987 | case kDirect: |
| 2988 | runtime_func = irb_.GetRuntime(FindDirectMethodWithAccessCheck); |
| 2989 | break; |
| 2990 | |
| 2991 | case kVirtual: |
| 2992 | runtime_func = irb_.GetRuntime(FindVirtualMethodWithAccessCheck); |
| 2993 | break; |
| 2994 | |
| 2995 | case kSuper: |
| 2996 | runtime_func = irb_.GetRuntime(FindSuperMethodWithAccessCheck); |
| 2997 | break; |
| 2998 | |
| 2999 | case kInterface: |
| 3000 | if (is_fast_path) { |
| 3001 | runtime_func = irb_.GetRuntime(FindInterfaceMethod); |
| 3002 | } else { |
| 3003 | runtime_func = irb_.GetRuntime(FindInterfaceMethodWithAccessCheck); |
| 3004 | } |
| 3005 | break; |
| 3006 | } |
Logan Chien | 7caf37e | 2012-02-03 22:56:04 +0800 | [diff] [blame] | 3007 | |
| 3008 | llvm::Value* callee_method_idx_value = irb_.getInt32(callee_method_idx); |
| 3009 | |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 3010 | if (this_addr == NULL) { |
| 3011 | DCHECK_EQ(invoke_type, kStatic); |
| 3012 | this_addr = irb_.getJNull(); |
| 3013 | } |
| 3014 | |
| 3015 | llvm::Value* caller_method_object_addr = EmitLoadMethodObjectAddr(); |
| 3016 | |
TDYa127 | da83d97 | 2012-04-18 00:21:49 -0700 | [diff] [blame] | 3017 | llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread)); |
| 3018 | |
TDYa127 | c8dc101 | 2012-04-19 07:03:33 -0700 | [diff] [blame] | 3019 | EmitUpdateDexPC(dex_pc); |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 3020 | |
TDYa127 | 0b686e5 | 2012-04-09 22:43:35 -0700 | [diff] [blame] | 3021 | llvm::Value* callee_method_object_addr = |
TDYa127 | da83d97 | 2012-04-18 00:21:49 -0700 | [diff] [blame] | 3022 | irb_.CreateCall4(runtime_func, |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 3023 | callee_method_idx_value, |
| 3024 | this_addr, |
TDYa127 | da83d97 | 2012-04-18 00:21:49 -0700 | [diff] [blame] | 3025 | caller_method_object_addr, |
| 3026 | thread_object_addr); |
Logan Chien | 7caf37e | 2012-02-03 22:56:04 +0800 | [diff] [blame] | 3027 | |
| 3028 | EmitGuard_ExceptionLandingPad(dex_pc); |
| 3029 | |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 3030 | return callee_method_object_addr; |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 3031 | } |
| 3032 | |
| 3033 | |
| 3034 | void MethodCompiler::EmitInsn_Neg(uint32_t dex_pc, |
| 3035 | Instruction const* insn, |
| 3036 | JType op_jty) { |
Logan Chien | 1b5685f | 2011-12-27 18:01:14 +0800 | [diff] [blame] | 3037 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3038 | DecodedInstruction dec_insn(insn); |
Logan Chien | 1b5685f | 2011-12-27 18:01:14 +0800 | [diff] [blame] | 3039 | |
| 3040 | DCHECK(op_jty == kInt || op_jty == kLong) << op_jty; |
| 3041 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3042 | llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate); |
Logan Chien | 1b5685f | 2011-12-27 18:01:14 +0800 | [diff] [blame] | 3043 | llvm::Value* result_value = irb_.CreateNeg(src_value); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3044 | EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value); |
Logan Chien | 1b5685f | 2011-12-27 18:01:14 +0800 | [diff] [blame] | 3045 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 3046 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 3047 | } |
| 3048 | |
| 3049 | |
| 3050 | void MethodCompiler::EmitInsn_Not(uint32_t dex_pc, |
| 3051 | Instruction const* insn, |
| 3052 | JType op_jty) { |
Logan Chien | e53750d | 2011-12-27 18:02:27 +0800 | [diff] [blame] | 3053 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3054 | DecodedInstruction dec_insn(insn); |
Logan Chien | e53750d | 2011-12-27 18:02:27 +0800 | [diff] [blame] | 3055 | |
| 3056 | DCHECK(op_jty == kInt || op_jty == kLong) << op_jty; |
| 3057 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3058 | llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate); |
Logan Chien | e53750d | 2011-12-27 18:02:27 +0800 | [diff] [blame] | 3059 | llvm::Value* result_value = |
| 3060 | irb_.CreateXor(src_value, static_cast<uint64_t>(-1)); |
| 3061 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3062 | EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value); |
Logan Chien | e53750d | 2011-12-27 18:02:27 +0800 | [diff] [blame] | 3063 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 3064 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 3065 | } |
| 3066 | |
| 3067 | |
| 3068 | void MethodCompiler::EmitInsn_SExt(uint32_t dex_pc, |
| 3069 | Instruction const* insn) { |
Logan Chien | 61752ad | 2011-12-27 18:03:51 +0800 | [diff] [blame] | 3070 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3071 | DecodedInstruction dec_insn(insn); |
Logan Chien | 61752ad | 2011-12-27 18:03:51 +0800 | [diff] [blame] | 3072 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3073 | llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate); |
Logan Chien | 61752ad | 2011-12-27 18:03:51 +0800 | [diff] [blame] | 3074 | llvm::Value* result_value = irb_.CreateSExt(src_value, irb_.getJLongTy()); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3075 | EmitStoreDalvikReg(dec_insn.vA, kLong, kAccurate, result_value); |
Logan Chien | 61752ad | 2011-12-27 18:03:51 +0800 | [diff] [blame] | 3076 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 3077 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 3078 | } |
| 3079 | |
| 3080 | |
| 3081 | void MethodCompiler::EmitInsn_Trunc(uint32_t dex_pc, |
| 3082 | Instruction const* insn) { |
Logan Chien | 17a5766 | 2011-12-27 18:05:14 +0800 | [diff] [blame] | 3083 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3084 | DecodedInstruction dec_insn(insn); |
Logan Chien | 17a5766 | 2011-12-27 18:05:14 +0800 | [diff] [blame] | 3085 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3086 | llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kLong, kAccurate); |
Logan Chien | 17a5766 | 2011-12-27 18:05:14 +0800 | [diff] [blame] | 3087 | llvm::Value* result_value = irb_.CreateTrunc(src_value, irb_.getJIntTy()); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3088 | EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value); |
Logan Chien | 17a5766 | 2011-12-27 18:05:14 +0800 | [diff] [blame] | 3089 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 3090 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 3091 | } |
| 3092 | |
| 3093 | |
| 3094 | void MethodCompiler::EmitInsn_TruncAndSExt(uint32_t dex_pc, |
| 3095 | Instruction const* insn, |
| 3096 | unsigned N) { |
Logan Chien | b6744c5 | 2011-12-27 18:06:26 +0800 | [diff] [blame] | 3097 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3098 | DecodedInstruction dec_insn(insn); |
Logan Chien | b6744c5 | 2011-12-27 18:06:26 +0800 | [diff] [blame] | 3099 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3100 | llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate); |
Logan Chien | b6744c5 | 2011-12-27 18:06:26 +0800 | [diff] [blame] | 3101 | |
| 3102 | llvm::Value* trunc_value = |
| 3103 | irb_.CreateTrunc(src_value, llvm::Type::getIntNTy(*context_, N)); |
| 3104 | |
| 3105 | llvm::Value* result_value = irb_.CreateSExt(trunc_value, irb_.getJIntTy()); |
| 3106 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3107 | EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value); |
Logan Chien | b6744c5 | 2011-12-27 18:06:26 +0800 | [diff] [blame] | 3108 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 3109 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 3110 | } |
| 3111 | |
| 3112 | |
| 3113 | void MethodCompiler::EmitInsn_TruncAndZExt(uint32_t dex_pc, |
| 3114 | Instruction const* insn, |
| 3115 | unsigned N) { |
Logan Chien | b6744c5 | 2011-12-27 18:06:26 +0800 | [diff] [blame] | 3116 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3117 | DecodedInstruction dec_insn(insn); |
Logan Chien | b6744c5 | 2011-12-27 18:06:26 +0800 | [diff] [blame] | 3118 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3119 | llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate); |
Logan Chien | b6744c5 | 2011-12-27 18:06:26 +0800 | [diff] [blame] | 3120 | |
| 3121 | llvm::Value* trunc_value = |
| 3122 | irb_.CreateTrunc(src_value, llvm::Type::getIntNTy(*context_, N)); |
| 3123 | |
| 3124 | llvm::Value* result_value = irb_.CreateZExt(trunc_value, irb_.getJIntTy()); |
| 3125 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3126 | EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value); |
Logan Chien | b6744c5 | 2011-12-27 18:06:26 +0800 | [diff] [blame] | 3127 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 3128 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 3129 | } |
| 3130 | |
| 3131 | |
| 3132 | void MethodCompiler::EmitInsn_FNeg(uint32_t dex_pc, |
| 3133 | Instruction const* insn, |
| 3134 | JType op_jty) { |
Logan Chien | 7a48b09 | 2011-12-27 18:07:45 +0800 | [diff] [blame] | 3135 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3136 | DecodedInstruction dec_insn(insn); |
Logan Chien | 7a48b09 | 2011-12-27 18:07:45 +0800 | [diff] [blame] | 3137 | |
| 3138 | DCHECK(op_jty == kFloat || op_jty == kDouble) << op_jty; |
| 3139 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3140 | llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate); |
Logan Chien | 7a48b09 | 2011-12-27 18:07:45 +0800 | [diff] [blame] | 3141 | llvm::Value* result_value = irb_.CreateFNeg(src_value); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3142 | EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value); |
Logan Chien | 7a48b09 | 2011-12-27 18:07:45 +0800 | [diff] [blame] | 3143 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 3144 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 3145 | } |
| 3146 | |
| 3147 | |
| 3148 | void MethodCompiler::EmitInsn_IntToFP(uint32_t dex_pc, |
| 3149 | Instruction const* insn, |
| 3150 | JType src_jty, |
| 3151 | JType dest_jty) { |
Logan Chien | 62dd453 | 2011-12-27 18:09:00 +0800 | [diff] [blame] | 3152 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3153 | DecodedInstruction dec_insn(insn); |
Logan Chien | 62dd453 | 2011-12-27 18:09:00 +0800 | [diff] [blame] | 3154 | |
| 3155 | DCHECK(src_jty == kInt || src_jty == kLong) << src_jty; |
| 3156 | DCHECK(dest_jty == kFloat || dest_jty == kDouble) << dest_jty; |
| 3157 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3158 | llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, src_jty, kAccurate); |
Logan Chien | 62dd453 | 2011-12-27 18:09:00 +0800 | [diff] [blame] | 3159 | llvm::Type* dest_type = irb_.getJType(dest_jty, kAccurate); |
| 3160 | llvm::Value* dest_value = irb_.CreateSIToFP(src_value, dest_type); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3161 | EmitStoreDalvikReg(dec_insn.vA, dest_jty, kAccurate, dest_value); |
Logan Chien | 62dd453 | 2011-12-27 18:09:00 +0800 | [diff] [blame] | 3162 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 3163 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 3164 | } |
| 3165 | |
| 3166 | |
| 3167 | void MethodCompiler::EmitInsn_FPToInt(uint32_t dex_pc, |
| 3168 | Instruction const* insn, |
| 3169 | JType src_jty, |
TDYa127 | a474687 | 2012-04-11 23:48:55 -0700 | [diff] [blame] | 3170 | JType dest_jty, |
| 3171 | runtime_support::RuntimeId runtime_func_id) { |
Logan Chien | 12dc175 | 2011-12-27 18:10:15 +0800 | [diff] [blame] | 3172 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3173 | DecodedInstruction dec_insn(insn); |
Logan Chien | 12dc175 | 2011-12-27 18:10:15 +0800 | [diff] [blame] | 3174 | |
| 3175 | DCHECK(src_jty == kFloat || src_jty == kDouble) << src_jty; |
| 3176 | DCHECK(dest_jty == kInt || dest_jty == kLong) << dest_jty; |
| 3177 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3178 | llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, src_jty, kAccurate); |
TDYa127 | a474687 | 2012-04-11 23:48:55 -0700 | [diff] [blame] | 3179 | llvm::Value* dest_value = irb_.CreateCall(irb_.GetRuntime(runtime_func_id), src_value); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3180 | EmitStoreDalvikReg(dec_insn.vA, dest_jty, kAccurate, dest_value); |
Logan Chien | 12dc175 | 2011-12-27 18:10:15 +0800 | [diff] [blame] | 3181 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 3182 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 3183 | } |
| 3184 | |
| 3185 | |
| 3186 | void MethodCompiler::EmitInsn_FExt(uint32_t dex_pc, |
| 3187 | Instruction const* insn) { |
Logan Chien | c56ded9 | 2011-12-27 18:10:57 +0800 | [diff] [blame] | 3188 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3189 | DecodedInstruction dec_insn(insn); |
Logan Chien | c56ded9 | 2011-12-27 18:10:57 +0800 | [diff] [blame] | 3190 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3191 | llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kFloat, kAccurate); |
Logan Chien | c56ded9 | 2011-12-27 18:10:57 +0800 | [diff] [blame] | 3192 | llvm::Value* result_value = irb_.CreateFPExt(src_value, irb_.getJDoubleTy()); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3193 | EmitStoreDalvikReg(dec_insn.vA, kDouble, kAccurate, result_value); |
Logan Chien | c56ded9 | 2011-12-27 18:10:57 +0800 | [diff] [blame] | 3194 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 3195 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 3196 | } |
| 3197 | |
| 3198 | |
| 3199 | void MethodCompiler::EmitInsn_FTrunc(uint32_t dex_pc, |
| 3200 | Instruction const* insn) { |
Logan Chien | 927744f | 2011-12-27 18:11:52 +0800 | [diff] [blame] | 3201 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3202 | DecodedInstruction dec_insn(insn); |
Logan Chien | 927744f | 2011-12-27 18:11:52 +0800 | [diff] [blame] | 3203 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3204 | llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kDouble, kAccurate); |
Logan Chien | 927744f | 2011-12-27 18:11:52 +0800 | [diff] [blame] | 3205 | llvm::Value* result_value = irb_.CreateFPTrunc(src_value, irb_.getJFloatTy()); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3206 | EmitStoreDalvikReg(dec_insn.vA, kFloat, kAccurate, result_value); |
Logan Chien | 927744f | 2011-12-27 18:11:52 +0800 | [diff] [blame] | 3207 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 3208 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 3209 | } |
| 3210 | |
| 3211 | |
| 3212 | void MethodCompiler::EmitInsn_IntArithm(uint32_t dex_pc, |
| 3213 | Instruction const* insn, |
| 3214 | IntArithmKind arithm, |
| 3215 | JType op_jty, |
| 3216 | bool is_2addr) { |
Logan Chien | c3f7d96 | 2011-12-27 18:13:18 +0800 | [diff] [blame] | 3217 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3218 | DecodedInstruction dec_insn(insn); |
Logan Chien | c3f7d96 | 2011-12-27 18:13:18 +0800 | [diff] [blame] | 3219 | |
| 3220 | DCHECK(op_jty == kInt || op_jty == kLong) << op_jty; |
| 3221 | |
| 3222 | llvm::Value* src1_value; |
| 3223 | llvm::Value* src2_value; |
| 3224 | |
| 3225 | if (is_2addr) { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3226 | src1_value = EmitLoadDalvikReg(dec_insn.vA, op_jty, kAccurate); |
| 3227 | src2_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate); |
Logan Chien | c3f7d96 | 2011-12-27 18:13:18 +0800 | [diff] [blame] | 3228 | } else { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3229 | src1_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate); |
| 3230 | src2_value = EmitLoadDalvikReg(dec_insn.vC, op_jty, kAccurate); |
Logan Chien | c3f7d96 | 2011-12-27 18:13:18 +0800 | [diff] [blame] | 3231 | } |
| 3232 | |
| 3233 | llvm::Value* result_value = |
| 3234 | EmitIntArithmResultComputation(dex_pc, src1_value, src2_value, |
| 3235 | arithm, op_jty); |
| 3236 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3237 | EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value); |
Logan Chien | c3f7d96 | 2011-12-27 18:13:18 +0800 | [diff] [blame] | 3238 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 3239 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 3240 | } |
| 3241 | |
| 3242 | |
| 3243 | void MethodCompiler::EmitInsn_IntArithmImmediate(uint32_t dex_pc, |
| 3244 | Instruction const* insn, |
| 3245 | IntArithmKind arithm) { |
Logan Chien | c3f7d96 | 2011-12-27 18:13:18 +0800 | [diff] [blame] | 3246 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3247 | DecodedInstruction dec_insn(insn); |
Logan Chien | c3f7d96 | 2011-12-27 18:13:18 +0800 | [diff] [blame] | 3248 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3249 | llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate); |
Logan Chien | c3f7d96 | 2011-12-27 18:13:18 +0800 | [diff] [blame] | 3250 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3251 | llvm::Value* imm_value = irb_.getInt32(dec_insn.vC); |
Logan Chien | c3f7d96 | 2011-12-27 18:13:18 +0800 | [diff] [blame] | 3252 | |
| 3253 | llvm::Value* result_value = |
| 3254 | EmitIntArithmResultComputation(dex_pc, src_value, imm_value, arithm, kInt); |
| 3255 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3256 | EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value); |
Logan Chien | c3f7d96 | 2011-12-27 18:13:18 +0800 | [diff] [blame] | 3257 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 3258 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 3259 | } |
| 3260 | |
| 3261 | |
Logan Chien | c3f7d96 | 2011-12-27 18:13:18 +0800 | [diff] [blame] | 3262 | llvm::Value* |
| 3263 | MethodCompiler::EmitIntArithmResultComputation(uint32_t dex_pc, |
| 3264 | llvm::Value* lhs, |
| 3265 | llvm::Value* rhs, |
| 3266 | IntArithmKind arithm, |
| 3267 | JType op_jty) { |
| 3268 | DCHECK(op_jty == kInt || op_jty == kLong) << op_jty; |
| 3269 | |
| 3270 | switch (arithm) { |
| 3271 | case kIntArithm_Add: |
| 3272 | return irb_.CreateAdd(lhs, rhs); |
| 3273 | |
| 3274 | case kIntArithm_Sub: |
| 3275 | return irb_.CreateSub(lhs, rhs); |
| 3276 | |
| 3277 | case kIntArithm_Mul: |
| 3278 | return irb_.CreateMul(lhs, rhs); |
| 3279 | |
| 3280 | case kIntArithm_Div: |
Logan Chien | c3f7d96 | 2011-12-27 18:13:18 +0800 | [diff] [blame] | 3281 | case kIntArithm_Rem: |
TDYa127 | f8641ce | 2012-04-02 06:40:40 -0700 | [diff] [blame] | 3282 | return EmitIntDivRemResultComputation(dex_pc, lhs, rhs, arithm, op_jty); |
Logan Chien | c3f7d96 | 2011-12-27 18:13:18 +0800 | [diff] [blame] | 3283 | |
| 3284 | case kIntArithm_And: |
| 3285 | return irb_.CreateAnd(lhs, rhs); |
| 3286 | |
| 3287 | case kIntArithm_Or: |
| 3288 | return irb_.CreateOr(lhs, rhs); |
| 3289 | |
| 3290 | case kIntArithm_Xor: |
| 3291 | return irb_.CreateXor(lhs, rhs); |
| 3292 | |
Logan Chien | c3f7d96 | 2011-12-27 18:13:18 +0800 | [diff] [blame] | 3293 | default: |
| 3294 | LOG(FATAL) << "Unknown integer arithmetic kind: " << arithm; |
| 3295 | return NULL; |
| 3296 | } |
| 3297 | } |
| 3298 | |
| 3299 | |
TDYa127 | f8641ce | 2012-04-02 06:40:40 -0700 | [diff] [blame] | 3300 | llvm::Value* |
| 3301 | MethodCompiler::EmitIntDivRemResultComputation(uint32_t dex_pc, |
| 3302 | llvm::Value* dividend, |
| 3303 | llvm::Value* divisor, |
| 3304 | IntArithmKind arithm, |
| 3305 | JType op_jty) { |
| 3306 | // Throw exception if the divisor is 0. |
| 3307 | EmitGuard_DivZeroException(dex_pc, divisor, op_jty); |
| 3308 | |
| 3309 | // Check the special case: MININT / -1 = MININT |
| 3310 | // That case will cause overflow, which is undefined behavior in llvm. |
| 3311 | // So we check the divisor is -1 or not, if the divisor is -1, we do |
| 3312 | // the special path to avoid undefined behavior. |
| 3313 | llvm::Type* op_type = irb_.getJType(op_jty, kAccurate); |
| 3314 | llvm::Value* zero = irb_.getJZero(op_jty); |
| 3315 | llvm::Value* neg_one = llvm::ConstantInt::getSigned(op_type, -1); |
| 3316 | llvm::Value* result = irb_.CreateAlloca(op_type); |
| 3317 | |
| 3318 | llvm::BasicBlock* eq_neg_one = CreateBasicBlockWithDexPC(dex_pc, "eq_neg_one"); |
| 3319 | llvm::BasicBlock* ne_neg_one = CreateBasicBlockWithDexPC(dex_pc, "ne_neg_one"); |
| 3320 | llvm::BasicBlock* neg_one_cont = CreateBasicBlockWithDexPC(dex_pc, "neg_one_cont"); |
| 3321 | |
| 3322 | llvm::Value* is_equal_neg_one = EmitConditionResult(divisor, neg_one, kCondBranch_EQ); |
| 3323 | irb_.CreateCondBr(is_equal_neg_one, eq_neg_one, ne_neg_one); |
| 3324 | |
| 3325 | // If divisor == -1 |
| 3326 | irb_.SetInsertPoint(eq_neg_one); |
| 3327 | llvm::Value* eq_result; |
| 3328 | if (arithm == kIntArithm_Div) { |
| 3329 | // We can just change from "dividend div -1" to "neg dividend". |
| 3330 | // The sub don't care the sign/unsigned because of two's complement representation. |
| 3331 | // And the behavior is what we want: |
| 3332 | // -(2^n) (2^n)-1 |
| 3333 | // MININT < k <= MAXINT -> mul k -1 = -k |
| 3334 | // MININT == k -> mul k -1 = k |
| 3335 | // |
| 3336 | // LLVM use sub to represent 'neg' |
| 3337 | eq_result = irb_.CreateSub(zero, dividend); |
| 3338 | } else { |
| 3339 | // Everything modulo -1 will be 0. |
| 3340 | eq_result = zero; |
| 3341 | } |
| 3342 | irb_.CreateStore(eq_result, result); |
| 3343 | irb_.CreateBr(neg_one_cont); |
| 3344 | |
| 3345 | // If divisor != -1, just do the division. |
| 3346 | irb_.SetInsertPoint(ne_neg_one); |
| 3347 | llvm::Value* ne_result; |
| 3348 | if (arithm == kIntArithm_Div) { |
| 3349 | ne_result = irb_.CreateSDiv(dividend, divisor); |
| 3350 | } else { |
| 3351 | ne_result = irb_.CreateSRem(dividend, divisor); |
| 3352 | } |
| 3353 | irb_.CreateStore(ne_result, result); |
| 3354 | irb_.CreateBr(neg_one_cont); |
| 3355 | |
| 3356 | irb_.SetInsertPoint(neg_one_cont); |
| 3357 | return irb_.CreateLoad(result); |
| 3358 | } |
| 3359 | |
| 3360 | |
Logan Chien | 5539ad0 | 2012-04-02 14:36:55 +0800 | [diff] [blame] | 3361 | void MethodCompiler::EmitInsn_IntShiftArithm(uint32_t dex_pc, |
| 3362 | Instruction const* insn, |
| 3363 | IntShiftArithmKind arithm, |
| 3364 | JType op_jty, |
| 3365 | bool is_2addr) { |
| 3366 | |
| 3367 | DecodedInstruction dec_insn(insn); |
| 3368 | |
| 3369 | DCHECK(op_jty == kInt || op_jty == kLong) << op_jty; |
| 3370 | |
| 3371 | llvm::Value* src1_value; |
| 3372 | llvm::Value* src2_value; |
| 3373 | |
| 3374 | // NOTE: The 2nd operand of the shift arithmetic instruction is |
| 3375 | // 32-bit integer regardless of the 1st operand. |
| 3376 | if (is_2addr) { |
| 3377 | src1_value = EmitLoadDalvikReg(dec_insn.vA, op_jty, kAccurate); |
| 3378 | src2_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate); |
| 3379 | } else { |
| 3380 | src1_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate); |
| 3381 | src2_value = EmitLoadDalvikReg(dec_insn.vC, kInt, kAccurate); |
| 3382 | } |
| 3383 | |
| 3384 | llvm::Value* result_value = |
| 3385 | EmitIntShiftArithmResultComputation(dex_pc, src1_value, src2_value, |
| 3386 | arithm, op_jty); |
| 3387 | |
| 3388 | EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value); |
| 3389 | |
| 3390 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 3391 | } |
| 3392 | |
| 3393 | |
| 3394 | void MethodCompiler:: |
| 3395 | EmitInsn_IntShiftArithmImmediate(uint32_t dex_pc, |
| 3396 | Instruction const* insn, |
| 3397 | IntShiftArithmKind arithm) { |
| 3398 | |
| 3399 | DecodedInstruction dec_insn(insn); |
| 3400 | |
| 3401 | llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate); |
| 3402 | |
| 3403 | llvm::Value* imm_value = irb_.getInt32(dec_insn.vC); |
| 3404 | |
| 3405 | llvm::Value* result_value = |
| 3406 | EmitIntShiftArithmResultComputation(dex_pc, src_value, imm_value, |
| 3407 | arithm, kInt); |
| 3408 | |
| 3409 | EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value); |
| 3410 | |
| 3411 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 3412 | } |
| 3413 | |
| 3414 | |
| 3415 | llvm::Value* |
| 3416 | MethodCompiler::EmitIntShiftArithmResultComputation(uint32_t dex_pc, |
| 3417 | llvm::Value* lhs, |
| 3418 | llvm::Value* rhs, |
| 3419 | IntShiftArithmKind arithm, |
| 3420 | JType op_jty) { |
| 3421 | DCHECK(op_jty == kInt || op_jty == kLong) << op_jty; |
| 3422 | |
| 3423 | if (op_jty == kInt) { |
| 3424 | rhs = irb_.CreateAnd(rhs, 0x1f); |
| 3425 | } else { |
| 3426 | llvm::Value* masked_rhs = irb_.CreateAnd(rhs, 0x3f); |
| 3427 | rhs = irb_.CreateZExt(masked_rhs, irb_.getJLongTy()); |
| 3428 | } |
| 3429 | |
| 3430 | switch (arithm) { |
| 3431 | case kIntArithm_Shl: |
| 3432 | return irb_.CreateShl(lhs, rhs); |
| 3433 | |
| 3434 | case kIntArithm_Shr: |
| 3435 | return irb_.CreateAShr(lhs, rhs); |
| 3436 | |
| 3437 | case kIntArithm_UShr: |
| 3438 | return irb_.CreateLShr(lhs, rhs); |
| 3439 | |
| 3440 | default: |
| 3441 | LOG(FATAL) << "Unknown integer shift arithmetic kind: " << arithm; |
| 3442 | return NULL; |
| 3443 | } |
| 3444 | } |
| 3445 | |
| 3446 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 3447 | void MethodCompiler::EmitInsn_RSubImmediate(uint32_t dex_pc, |
| 3448 | Instruction const* insn) { |
Logan Chien | 65c62d4 | 2011-12-27 18:14:18 +0800 | [diff] [blame] | 3449 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3450 | DecodedInstruction dec_insn(insn); |
Logan Chien | 65c62d4 | 2011-12-27 18:14:18 +0800 | [diff] [blame] | 3451 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3452 | llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate); |
| 3453 | llvm::Value* imm_value = irb_.getInt32(dec_insn.vC); |
Logan Chien | 65c62d4 | 2011-12-27 18:14:18 +0800 | [diff] [blame] | 3454 | llvm::Value* result_value = irb_.CreateSub(imm_value, src_value); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3455 | EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value); |
Logan Chien | 65c62d4 | 2011-12-27 18:14:18 +0800 | [diff] [blame] | 3456 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 3457 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 3458 | } |
| 3459 | |
| 3460 | |
| 3461 | void MethodCompiler::EmitInsn_FPArithm(uint32_t dex_pc, |
| 3462 | Instruction const* insn, |
| 3463 | FPArithmKind arithm, |
| 3464 | JType op_jty, |
| 3465 | bool is_2addr) { |
Logan Chien | 76e1c79 | 2011-12-27 18:15:01 +0800 | [diff] [blame] | 3466 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3467 | DecodedInstruction dec_insn(insn); |
Logan Chien | 76e1c79 | 2011-12-27 18:15:01 +0800 | [diff] [blame] | 3468 | |
| 3469 | DCHECK(op_jty == kFloat || op_jty == kDouble) << op_jty; |
| 3470 | |
| 3471 | llvm::Value* src1_value; |
| 3472 | llvm::Value* src2_value; |
| 3473 | |
| 3474 | if (is_2addr) { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3475 | src1_value = EmitLoadDalvikReg(dec_insn.vA, op_jty, kAccurate); |
| 3476 | src2_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate); |
Logan Chien | 76e1c79 | 2011-12-27 18:15:01 +0800 | [diff] [blame] | 3477 | } else { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3478 | src1_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate); |
| 3479 | src2_value = EmitLoadDalvikReg(dec_insn.vC, op_jty, kAccurate); |
Logan Chien | 76e1c79 | 2011-12-27 18:15:01 +0800 | [diff] [blame] | 3480 | } |
| 3481 | |
| 3482 | llvm::Value* result_value = |
| 3483 | EmitFPArithmResultComputation(dex_pc, src1_value, src2_value, arithm); |
| 3484 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3485 | EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value); |
Logan Chien | 76e1c79 | 2011-12-27 18:15:01 +0800 | [diff] [blame] | 3486 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 3487 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 3488 | } |
| 3489 | |
| 3490 | |
Logan Chien | 76e1c79 | 2011-12-27 18:15:01 +0800 | [diff] [blame] | 3491 | llvm::Value* |
| 3492 | MethodCompiler::EmitFPArithmResultComputation(uint32_t dex_pc, |
| 3493 | llvm::Value *lhs, |
| 3494 | llvm::Value *rhs, |
| 3495 | FPArithmKind arithm) { |
| 3496 | switch (arithm) { |
| 3497 | case kFPArithm_Add: |
| 3498 | return irb_.CreateFAdd(lhs, rhs); |
| 3499 | |
| 3500 | case kFPArithm_Sub: |
| 3501 | return irb_.CreateFSub(lhs, rhs); |
| 3502 | |
| 3503 | case kFPArithm_Mul: |
| 3504 | return irb_.CreateFMul(lhs, rhs); |
| 3505 | |
| 3506 | case kFPArithm_Div: |
| 3507 | return irb_.CreateFDiv(lhs, rhs); |
| 3508 | |
| 3509 | case kFPArithm_Rem: |
| 3510 | return irb_.CreateFRem(lhs, rhs); |
| 3511 | |
| 3512 | default: |
| 3513 | LOG(FATAL) << "Unknown floating-point arithmetic kind: " << arithm; |
| 3514 | return NULL; |
| 3515 | } |
| 3516 | } |
| 3517 | |
| 3518 | |
Logan Chien | c3f7d96 | 2011-12-27 18:13:18 +0800 | [diff] [blame] | 3519 | void MethodCompiler::EmitGuard_DivZeroException(uint32_t dex_pc, |
| 3520 | llvm::Value* denominator, |
| 3521 | JType op_jty) { |
| 3522 | DCHECK(op_jty == kInt || op_jty == kLong) << op_jty; |
| 3523 | |
| 3524 | llvm::Constant* zero = irb_.getJZero(op_jty); |
| 3525 | |
| 3526 | llvm::Value* equal_zero = irb_.CreateICmpEQ(denominator, zero); |
| 3527 | |
| 3528 | llvm::BasicBlock* block_exception = CreateBasicBlockWithDexPC(dex_pc, "div0"); |
| 3529 | |
| 3530 | llvm::BasicBlock* block_continue = CreateBasicBlockWithDexPC(dex_pc, "cont"); |
| 3531 | |
| 3532 | irb_.CreateCondBr(equal_zero, block_exception, block_continue); |
| 3533 | |
| 3534 | irb_.SetInsertPoint(block_exception); |
TDYa127 | c8dc101 | 2012-04-19 07:03:33 -0700 | [diff] [blame] | 3535 | EmitUpdateDexPC(dex_pc); |
Logan Chien | c3f7d96 | 2011-12-27 18:13:18 +0800 | [diff] [blame] | 3536 | irb_.CreateCall(irb_.GetRuntime(ThrowDivZeroException)); |
| 3537 | EmitBranchExceptionLandingPad(dex_pc); |
| 3538 | |
| 3539 | irb_.SetInsertPoint(block_continue); |
| 3540 | } |
| 3541 | |
| 3542 | |
Logan Chien | 61bb614 | 2012-02-03 15:34:53 +0800 | [diff] [blame] | 3543 | void MethodCompiler::EmitGuard_NullPointerException(uint32_t dex_pc, |
| 3544 | llvm::Value* object) { |
| 3545 | llvm::Value* equal_null = irb_.CreateICmpEQ(object, irb_.getJNull()); |
| 3546 | |
| 3547 | llvm::BasicBlock* block_exception = |
| 3548 | CreateBasicBlockWithDexPC(dex_pc, "nullp"); |
| 3549 | |
| 3550 | llvm::BasicBlock* block_continue = |
| 3551 | CreateBasicBlockWithDexPC(dex_pc, "cont"); |
| 3552 | |
| 3553 | irb_.CreateCondBr(equal_null, block_exception, block_continue); |
| 3554 | |
| 3555 | irb_.SetInsertPoint(block_exception); |
TDYa127 | c8dc101 | 2012-04-19 07:03:33 -0700 | [diff] [blame] | 3556 | EmitUpdateDexPC(dex_pc); |
TDYa127 | 3f9137d | 2012-04-08 15:59:19 -0700 | [diff] [blame] | 3557 | irb_.CreateCall(irb_.GetRuntime(ThrowNullPointerException), irb_.getInt32(dex_pc)); |
Logan Chien | 61bb614 | 2012-02-03 15:34:53 +0800 | [diff] [blame] | 3558 | EmitBranchExceptionLandingPad(dex_pc); |
| 3559 | |
| 3560 | irb_.SetInsertPoint(block_continue); |
| 3561 | } |
| 3562 | |
| 3563 | |
Logan Chien | bb4d12a | 2012-02-17 14:10:01 +0800 | [diff] [blame] | 3564 | llvm::Value* MethodCompiler::EmitLoadDexCacheAddr(MemberOffset offset) { |
| 3565 | llvm::Value* method_object_addr = EmitLoadMethodObjectAddr(); |
| 3566 | |
| 3567 | llvm::Value* dex_cache_offset_value = |
| 3568 | irb_.getPtrEquivInt(offset.Int32Value()); |
| 3569 | |
| 3570 | llvm::Value* dex_cache_field_addr = |
| 3571 | irb_.CreatePtrDisp(method_object_addr, dex_cache_offset_value, |
| 3572 | irb_.getJObjectTy()->getPointerTo()); |
| 3573 | |
| 3574 | return irb_.CreateLoad(dex_cache_field_addr); |
| 3575 | } |
| 3576 | |
| 3577 | |
Logan Chien | bb4d12a | 2012-02-17 14:10:01 +0800 | [diff] [blame] | 3578 | llvm::Value* MethodCompiler:: |
| 3579 | EmitLoadDexCacheStaticStorageFieldAddr(uint32_t type_idx) { |
| 3580 | llvm::Value* static_storage_dex_cache_addr = |
| 3581 | EmitLoadDexCacheAddr(Method::DexCacheInitializedStaticStorageOffset()); |
| 3582 | |
| 3583 | llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx); |
| 3584 | |
| 3585 | return EmitArrayGEP(static_storage_dex_cache_addr, type_idx_value, |
Ian Rogers | 04ec04e | 2012-02-28 16:15:33 -0800 | [diff] [blame] | 3586 | irb_.getJObjectTy(), kObject); |
Logan Chien | bb4d12a | 2012-02-17 14:10:01 +0800 | [diff] [blame] | 3587 | } |
| 3588 | |
| 3589 | |
| 3590 | llvm::Value* MethodCompiler:: |
| 3591 | EmitLoadDexCacheResolvedTypeFieldAddr(uint32_t type_idx) { |
| 3592 | llvm::Value* resolved_type_dex_cache_addr = |
| 3593 | EmitLoadDexCacheAddr(Method::DexCacheResolvedTypesOffset()); |
| 3594 | |
| 3595 | llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx); |
| 3596 | |
| 3597 | return EmitArrayGEP(resolved_type_dex_cache_addr, type_idx_value, |
Ian Rogers | 04ec04e | 2012-02-28 16:15:33 -0800 | [diff] [blame] | 3598 | irb_.getJObjectTy(), kObject); |
Logan Chien | bb4d12a | 2012-02-17 14:10:01 +0800 | [diff] [blame] | 3599 | } |
| 3600 | |
| 3601 | |
| 3602 | llvm::Value* MethodCompiler:: |
Logan Chien | 61c65dc | 2012-02-29 03:22:30 +0800 | [diff] [blame] | 3603 | EmitLoadDexCacheResolvedMethodFieldAddr(uint32_t method_idx) { |
| 3604 | llvm::Value* resolved_method_dex_cache_addr = |
| 3605 | EmitLoadDexCacheAddr(Method::DexCacheResolvedMethodsOffset()); |
| 3606 | |
| 3607 | llvm::Value* method_idx_value = irb_.getPtrEquivInt(method_idx); |
| 3608 | |
| 3609 | return EmitArrayGEP(resolved_method_dex_cache_addr, method_idx_value, |
Ian Rogers | 04ec04e | 2012-02-28 16:15:33 -0800 | [diff] [blame] | 3610 | irb_.getJObjectTy(), kObject); |
Logan Chien | 61c65dc | 2012-02-29 03:22:30 +0800 | [diff] [blame] | 3611 | } |
| 3612 | |
| 3613 | |
| 3614 | llvm::Value* MethodCompiler:: |
Logan Chien | bb4d12a | 2012-02-17 14:10:01 +0800 | [diff] [blame] | 3615 | EmitLoadDexCacheStringFieldAddr(uint32_t string_idx) { |
| 3616 | llvm::Value* string_dex_cache_addr = |
| 3617 | EmitLoadDexCacheAddr(Method::DexCacheStringsOffset()); |
| 3618 | |
| 3619 | llvm::Value* string_idx_value = irb_.getPtrEquivInt(string_idx); |
| 3620 | |
| 3621 | return EmitArrayGEP(string_dex_cache_addr, string_idx_value, |
Ian Rogers | 04ec04e | 2012-02-28 16:15:33 -0800 | [diff] [blame] | 3622 | irb_.getJObjectTy(), kObject); |
Logan Chien | bb4d12a | 2012-02-17 14:10:01 +0800 | [diff] [blame] | 3623 | } |
| 3624 | |
| 3625 | |
Logan Chien | 8342616 | 2011-12-09 09:29:50 +0800 | [diff] [blame] | 3626 | CompiledMethod *MethodCompiler::Compile() { |
Logan Chien | 0b82710 | 2011-12-20 19:46:14 +0800 | [diff] [blame] | 3627 | // Code generation |
| 3628 | CreateFunction(); |
| 3629 | |
| 3630 | EmitPrologue(); |
| 3631 | EmitInstructions(); |
Logan Chien | c670a8d | 2011-12-20 21:25:56 +0800 | [diff] [blame] | 3632 | EmitPrologueLastBranch(); |
Logan Chien | 0b82710 | 2011-12-20 19:46:14 +0800 | [diff] [blame] | 3633 | |
Logan Chien | d6c239a | 2011-12-23 15:11:45 +0800 | [diff] [blame] | 3634 | // Verify the generated bitcode |
TDYa127 | 853cd09 | 2012-04-21 22:15:31 -0700 | [diff] [blame] | 3635 | VERIFY_LLVM_FUNCTION(*func_); |
Logan Chien | d6c239a | 2011-12-23 15:11:45 +0800 | [diff] [blame] | 3636 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 3637 | // Add the memory usage approximation of the compilation unit |
| 3638 | cunit_->AddMemUsageApproximation(code_item_->insns_size_in_code_units_ * 900); |
| 3639 | // NOTE: From statistic, the bitcode size is 4.5 times bigger than the |
| 3640 | // Dex file. Besides, we have to convert the code unit into bytes. |
| 3641 | // Thus, we got our magic number 9. |
| 3642 | |
Logan Chien | 110bcba | 2012-04-16 19:11:28 +0800 | [diff] [blame] | 3643 | CompiledMethod* compiled_method = |
| 3644 | new CompiledMethod(cunit_->GetInstructionSet(), |
| 3645 | cunit_->GetElfIndex(), |
| 3646 | elf_func_idx_); |
| 3647 | |
| 3648 | cunit_->RegisterCompiledMethod(func_, compiled_method); |
| 3649 | |
| 3650 | return compiled_method; |
Logan Chien | 0b82710 | 2011-12-20 19:46:14 +0800 | [diff] [blame] | 3651 | } |
| 3652 | |
| 3653 | |
| 3654 | llvm::Value* MethodCompiler::EmitLoadMethodObjectAddr() { |
| 3655 | return func_->arg_begin(); |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 3656 | } |
Logan Chien | 8342616 | 2011-12-09 09:29:50 +0800 | [diff] [blame] | 3657 | |
| 3658 | |
Logan Chien | 5bcc04e | 2012-01-30 14:15:12 +0800 | [diff] [blame] | 3659 | void MethodCompiler::EmitBranchExceptionLandingPad(uint32_t dex_pc) { |
| 3660 | if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) { |
| 3661 | irb_.CreateBr(lpad); |
| 3662 | } else { |
| 3663 | irb_.CreateBr(GetUnwindBasicBlock()); |
| 3664 | } |
| 3665 | } |
| 3666 | |
| 3667 | |
| 3668 | void MethodCompiler::EmitGuard_ExceptionLandingPad(uint32_t dex_pc) { |
| 3669 | llvm::Value* exception_pending = |
| 3670 | irb_.CreateCall(irb_.GetRuntime(IsExceptionPending)); |
| 3671 | |
| 3672 | llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont"); |
| 3673 | |
| 3674 | if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) { |
| 3675 | irb_.CreateCondBr(exception_pending, lpad, block_cont); |
| 3676 | } else { |
| 3677 | irb_.CreateCondBr(exception_pending, GetUnwindBasicBlock(), block_cont); |
| 3678 | } |
| 3679 | |
| 3680 | irb_.SetInsertPoint(block_cont); |
| 3681 | } |
| 3682 | |
| 3683 | |
Logan Chien | 924072f | 2012-01-30 15:07:24 +0800 | [diff] [blame] | 3684 | void MethodCompiler::EmitGuard_GarbageCollectionSuspend(uint32_t dex_pc) { |
| 3685 | llvm::Value* runtime_func = irb_.GetRuntime(TestSuspend); |
TDYa127 | 853cd09 | 2012-04-21 22:15:31 -0700 | [diff] [blame] | 3686 | |
| 3687 | llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread)); |
| 3688 | |
TDYa127 | c8dc101 | 2012-04-19 07:03:33 -0700 | [diff] [blame] | 3689 | EmitUpdateDexPC(dex_pc); |
TDYa127 | 853cd09 | 2012-04-21 22:15:31 -0700 | [diff] [blame] | 3690 | |
| 3691 | irb_.CreateCall(runtime_func, thread_object_addr); |
Logan Chien | 924072f | 2012-01-30 15:07:24 +0800 | [diff] [blame] | 3692 | |
| 3693 | EmitGuard_ExceptionLandingPad(dex_pc); |
| 3694 | } |
| 3695 | |
| 3696 | |
Logan Chien | d6c239a | 2011-12-23 15:11:45 +0800 | [diff] [blame] | 3697 | llvm::BasicBlock* MethodCompiler:: |
| 3698 | CreateBasicBlockWithDexPC(uint32_t dex_pc, char const* postfix) { |
| 3699 | std::string name; |
| 3700 | |
| 3701 | if (postfix) { |
| 3702 | StringAppendF(&name, "B%u.%s", dex_pc, postfix); |
| 3703 | } else { |
| 3704 | StringAppendF(&name, "B%u", dex_pc); |
| 3705 | } |
| 3706 | |
| 3707 | return llvm::BasicBlock::Create(*context_, name, func_); |
| 3708 | } |
| 3709 | |
| 3710 | |
| 3711 | llvm::BasicBlock* MethodCompiler::GetBasicBlock(uint32_t dex_pc) { |
| 3712 | DCHECK(dex_pc < code_item_->insns_size_in_code_units_); |
| 3713 | |
| 3714 | llvm::BasicBlock* basic_block = basic_blocks_[dex_pc]; |
| 3715 | |
| 3716 | if (!basic_block) { |
| 3717 | basic_block = CreateBasicBlockWithDexPC(dex_pc); |
| 3718 | basic_blocks_[dex_pc] = basic_block; |
| 3719 | } |
| 3720 | |
| 3721 | return basic_block; |
| 3722 | } |
| 3723 | |
| 3724 | |
| 3725 | llvm::BasicBlock* |
| 3726 | MethodCompiler::GetNextBasicBlock(uint32_t dex_pc) { |
| 3727 | Instruction const* insn = Instruction::At(code_item_->insns_ + dex_pc); |
| 3728 | return GetBasicBlock(dex_pc + insn->SizeInCodeUnits()); |
| 3729 | } |
| 3730 | |
| 3731 | |
Logan Chien | 5bcc04e | 2012-01-30 14:15:12 +0800 | [diff] [blame] | 3732 | int32_t MethodCompiler::GetTryItemOffset(uint32_t dex_pc) { |
| 3733 | // TODO: Since we are emitting the dex instructions in ascending order |
| 3734 | // w.r.t. address, we can cache the lastest try item offset so that we |
| 3735 | // don't have to do binary search for every query. |
| 3736 | |
| 3737 | int32_t min = 0; |
| 3738 | int32_t max = code_item_->tries_size_ - 1; |
| 3739 | |
| 3740 | while (min <= max) { |
| 3741 | int32_t mid = min + (max - min) / 2; |
| 3742 | |
| 3743 | DexFile::TryItem const* ti = DexFile::GetTryItems(*code_item_, mid); |
| 3744 | uint32_t start = ti->start_addr_; |
| 3745 | uint32_t end = start + ti->insn_count_; |
| 3746 | |
| 3747 | if (dex_pc < start) { |
| 3748 | max = mid - 1; |
| 3749 | } else if (dex_pc >= end) { |
| 3750 | min = mid + 1; |
| 3751 | } else { |
| 3752 | return mid; // found |
| 3753 | } |
| 3754 | } |
| 3755 | |
| 3756 | return -1; // not found |
| 3757 | } |
| 3758 | |
| 3759 | |
| 3760 | llvm::BasicBlock* MethodCompiler::GetLandingPadBasicBlock(uint32_t dex_pc) { |
| 3761 | // Find the try item for this address in this method |
| 3762 | int32_t ti_offset = GetTryItemOffset(dex_pc); |
| 3763 | |
| 3764 | if (ti_offset == -1) { |
| 3765 | return NULL; // No landing pad is available for this address. |
| 3766 | } |
| 3767 | |
| 3768 | // Check for the existing landing pad basic block |
| 3769 | DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset)); |
| 3770 | llvm::BasicBlock* block_lpad = basic_block_landing_pads_[ti_offset]; |
| 3771 | |
| 3772 | if (block_lpad) { |
| 3773 | // We have generated landing pad for this try item already. Return the |
| 3774 | // same basic block. |
| 3775 | return block_lpad; |
| 3776 | } |
| 3777 | |
| 3778 | // Get try item from code item |
| 3779 | DexFile::TryItem const* ti = DexFile::GetTryItems(*code_item_, ti_offset); |
| 3780 | |
| 3781 | // Create landing pad basic block |
| 3782 | block_lpad = llvm::BasicBlock::Create(*context_, |
| 3783 | StringPrintf("lpad%d", ti_offset), |
| 3784 | func_); |
| 3785 | |
| 3786 | // Change IRBuilder insert point |
| 3787 | llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP(); |
| 3788 | irb_.SetInsertPoint(block_lpad); |
| 3789 | |
| 3790 | // Find catch block with matching type |
| 3791 | llvm::Value* method_object_addr = EmitLoadMethodObjectAddr(); |
| 3792 | |
Logan Chien | 736df02 | 2012-04-27 16:25:57 +0800 | [diff] [blame] | 3793 | llvm::Value* ti_offset_value = irb_.getInt32(ti_offset); |
Logan Chien | 5bcc04e | 2012-01-30 14:15:12 +0800 | [diff] [blame] | 3794 | |
| 3795 | llvm::Value* catch_handler_index_value = |
| 3796 | irb_.CreateCall2(irb_.GetRuntime(FindCatchBlock), |
Logan Chien | 736df02 | 2012-04-27 16:25:57 +0800 | [diff] [blame] | 3797 | method_object_addr, ti_offset_value); |
Logan Chien | 5bcc04e | 2012-01-30 14:15:12 +0800 | [diff] [blame] | 3798 | |
| 3799 | // Switch instruction (Go to unwind basic block by default) |
| 3800 | llvm::SwitchInst* sw = |
| 3801 | irb_.CreateSwitch(catch_handler_index_value, GetUnwindBasicBlock()); |
| 3802 | |
| 3803 | // Cases with matched catch block |
| 3804 | CatchHandlerIterator iter(*code_item_, ti->start_addr_); |
| 3805 | |
| 3806 | for (uint32_t c = 0; iter.HasNext(); iter.Next(), ++c) { |
| 3807 | sw->addCase(irb_.getInt32(c), GetBasicBlock(iter.GetHandlerAddress())); |
| 3808 | } |
| 3809 | |
| 3810 | // Restore the orignal insert point for IRBuilder |
| 3811 | irb_.restoreIP(irb_ip_original); |
| 3812 | |
| 3813 | // Cache this landing pad |
| 3814 | DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset)); |
| 3815 | basic_block_landing_pads_[ti_offset] = block_lpad; |
| 3816 | |
| 3817 | return block_lpad; |
| 3818 | } |
| 3819 | |
| 3820 | |
| 3821 | llvm::BasicBlock* MethodCompiler::GetUnwindBasicBlock() { |
| 3822 | // Check the existing unwinding baisc block block |
| 3823 | if (basic_block_unwind_ != NULL) { |
| 3824 | return basic_block_unwind_; |
| 3825 | } |
| 3826 | |
| 3827 | // Create new basic block for unwinding |
| 3828 | basic_block_unwind_ = |
| 3829 | llvm::BasicBlock::Create(*context_, "exception_unwind", func_); |
| 3830 | |
| 3831 | // Change IRBuilder insert point |
| 3832 | llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP(); |
| 3833 | irb_.SetInsertPoint(basic_block_unwind_); |
| 3834 | |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 3835 | // Pop the shadow frame |
| 3836 | EmitPopShadowFrame(); |
| 3837 | |
Logan Chien | 5bcc04e | 2012-01-30 14:15:12 +0800 | [diff] [blame] | 3838 | // Emit the code to return default value (zero) for the given return type. |
Logan Chien | dd361c9 | 2012-04-10 23:40:37 +0800 | [diff] [blame] | 3839 | char ret_shorty = oat_compilation_unit_->GetShorty()[0]; |
Logan Chien | 5bcc04e | 2012-01-30 14:15:12 +0800 | [diff] [blame] | 3840 | if (ret_shorty == 'V') { |
| 3841 | irb_.CreateRetVoid(); |
| 3842 | } else { |
| 3843 | irb_.CreateRet(irb_.getJZero(ret_shorty)); |
| 3844 | } |
| 3845 | |
| 3846 | // Restore the orignal insert point for IRBuilder |
| 3847 | irb_.restoreIP(irb_ip_original); |
| 3848 | |
| 3849 | return basic_block_unwind_; |
| 3850 | } |
| 3851 | |
| 3852 | |
Logan Chien | c670a8d | 2011-12-20 21:25:56 +0800 | [diff] [blame] | 3853 | llvm::Value* MethodCompiler::AllocDalvikLocalVarReg(RegCategory cat, |
| 3854 | uint32_t reg_idx) { |
| 3855 | |
| 3856 | // Save current IR builder insert point |
| 3857 | llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP(); |
| 3858 | |
| 3859 | // Alloca |
| 3860 | llvm::Value* reg_addr = NULL; |
| 3861 | |
| 3862 | switch (cat) { |
| 3863 | case kRegCat1nr: |
| 3864 | irb_.SetInsertPoint(basic_block_reg_alloca_); |
| 3865 | reg_addr = irb_.CreateAlloca(irb_.getJIntTy(), 0, |
| 3866 | StringPrintf("r%u", reg_idx)); |
Logan Chien | c670a8d | 2011-12-20 21:25:56 +0800 | [diff] [blame] | 3867 | break; |
| 3868 | |
| 3869 | case kRegCat2: |
| 3870 | irb_.SetInsertPoint(basic_block_reg_alloca_); |
| 3871 | reg_addr = irb_.CreateAlloca(irb_.getJLongTy(), 0, |
| 3872 | StringPrintf("w%u", reg_idx)); |
Logan Chien | c670a8d | 2011-12-20 21:25:56 +0800 | [diff] [blame] | 3873 | break; |
| 3874 | |
| 3875 | case kRegObject: |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 3876 | { |
| 3877 | irb_.SetInsertPoint(basic_block_shadow_frame_alloca_); |
Logan Chien | c670a8d | 2011-12-20 21:25:56 +0800 | [diff] [blame] | 3878 | |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 3879 | llvm::Value* gep_index[] = { |
| 3880 | irb_.getInt32(0), // No pointer displacement |
| 3881 | irb_.getInt32(1), // SIRT |
| 3882 | irb_.getInt32(reg_idx) // Pointer field |
| 3883 | }; |
| 3884 | |
| 3885 | reg_addr = irb_.CreateGEP(shadow_frame_, gep_index, |
| 3886 | StringPrintf("p%u", reg_idx)); |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 3887 | } |
Logan Chien | c670a8d | 2011-12-20 21:25:56 +0800 | [diff] [blame] | 3888 | break; |
| 3889 | |
| 3890 | default: |
| 3891 | LOG(FATAL) << "Unknown register category for allocation: " << cat; |
| 3892 | } |
| 3893 | |
| 3894 | // Restore IRBuilder insert point |
| 3895 | irb_.restoreIP(irb_ip_original); |
| 3896 | |
| 3897 | DCHECK_NE(reg_addr, static_cast<llvm::Value*>(NULL)); |
| 3898 | return reg_addr; |
| 3899 | } |
| 3900 | |
| 3901 | |
| 3902 | llvm::Value* MethodCompiler::AllocDalvikRetValReg(RegCategory cat) { |
| 3903 | // Save current IR builder insert point |
| 3904 | llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP(); |
| 3905 | |
| 3906 | // Alloca |
| 3907 | llvm::Value* reg_addr = NULL; |
| 3908 | |
| 3909 | switch (cat) { |
| 3910 | case kRegCat1nr: |
| 3911 | irb_.SetInsertPoint(basic_block_reg_alloca_); |
| 3912 | reg_addr = irb_.CreateAlloca(irb_.getJIntTy(), 0, "r_res"); |
| 3913 | break; |
| 3914 | |
| 3915 | case kRegCat2: |
| 3916 | irb_.SetInsertPoint(basic_block_reg_alloca_); |
| 3917 | reg_addr = irb_.CreateAlloca(irb_.getJLongTy(), 0, "w_res"); |
| 3918 | break; |
| 3919 | |
| 3920 | case kRegObject: |
| 3921 | irb_.SetInsertPoint(basic_block_reg_alloca_); |
| 3922 | reg_addr = irb_.CreateAlloca(irb_.getJObjectTy(), 0, "p_res"); |
| 3923 | break; |
| 3924 | |
| 3925 | default: |
| 3926 | LOG(FATAL) << "Unknown register category for allocation: " << cat; |
| 3927 | } |
| 3928 | |
| 3929 | // Restore IRBuilder insert point |
| 3930 | irb_.restoreIP(irb_ip_original); |
| 3931 | |
| 3932 | DCHECK_NE(reg_addr, static_cast<llvm::Value*>(NULL)); |
| 3933 | return reg_addr; |
| 3934 | } |
| 3935 | |
| 3936 | |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 3937 | void MethodCompiler::EmitPopShadowFrame() { |
| 3938 | irb_.CreateCall(irb_.GetRuntime(PopShadowFrame)); |
| 3939 | } |
| 3940 | |
| 3941 | |
TDYa127 | c8dc101 | 2012-04-19 07:03:33 -0700 | [diff] [blame] | 3942 | void MethodCompiler::EmitUpdateDexPC(uint32_t dex_pc) { |
| 3943 | irb_.StoreToObjectOffset(shadow_frame_, |
| 3944 | ShadowFrame::DexPCOffset(), |
| 3945 | irb_.getInt32(dex_pc)); |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 3946 | } |
| 3947 | |
| 3948 | |
Logan Chien | 8342616 | 2011-12-09 09:29:50 +0800 | [diff] [blame] | 3949 | } // namespace compiler_llvm |
| 3950 | } // namespace art |