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