blob: 90285946baefa95a259bdb4bec281cb349f9b4ee [file] [log] [blame]
Shih-wei Liaod1fec812012-02-13 09:51:10 -08001/*
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 Chienfca7e872011-12-20 20:08:22 +080019#include "backend_types.h"
Logan Chien8b977d32012-02-21 19:14:55 +080020#include "compilation_unit.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080021#include "compiler.h"
Logan Chiena78e3c82011-12-27 17:59:35 +080022#include "inferred_reg_category_map.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080023#include "ir_builder.h"
24#include "logging.h"
Logan Chien4dd96f52012-02-29 01:26:58 +080025#include "oat_compilation_unit.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080026#include "object.h"
27#include "object_utils.h"
Logan Chien42e0e152012-01-13 15:42:36 +080028#include "runtime_support_func.h"
TDYa1275bb86012012-04-11 05:57:28 -070029#include "runtime_support_llvm.h"
Logan Chien1b0a1b72012-03-15 06:20:17 +080030#include "shadow_frame.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080031#include "stl_util.h"
Logan Chien0b827102011-12-20 19:46:14 +080032#include "stringprintf.h"
33#include "utils_llvm.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070034#include "verifier/method_verifier.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080035
36#include <iomanip>
37
Logan Chienc670a8d2011-12-20 21:25:56 +080038#include <llvm/BasicBlock.h>
Shih-wei Liaod1fec812012-02-13 09:51:10 -080039#include <llvm/Function.h>
Logan Chiena85fb2f2012-01-16 12:52:56 +080040#include <llvm/GlobalVariable.h>
41#include <llvm/Intrinsics.h>
Shih-wei Liaod1fec812012-02-13 09:51:10 -080042
Logan Chien83426162011-12-09 09:29:50 +080043namespace art {
44namespace compiler_llvm {
Shih-wei Liaod1fec812012-02-13 09:51:10 -080045
Logan Chien42e0e152012-01-13 15:42:36 +080046using namespace runtime_support;
47
Shih-wei Liaod1fec812012-02-13 09:51:10 -080048
Logan Chien8b977d32012-02-21 19:14:55 +080049MethodCompiler::MethodCompiler(CompilationUnit* cunit,
Logan Chien83426162011-12-09 09:29:50 +080050 Compiler* compiler,
Logan Chien4dd96f52012-02-29 01:26:58 +080051 OatCompilationUnit* oat_compilation_unit)
Logan Chien8b977d32012-02-21 19:14:55 +080052 : 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 Chien8b977d32012-02-21 19:14:55 +080059 method_idx_(oat_compilation_unit->method_idx_),
60 access_flags_(oat_compilation_unit->access_flags_),
61 module_(cunit->GetModule()),
62 context_(cunit->GetLLVMContext()),
TDYa1271d7e5102012-05-13 09:27:05 -070063 irb_(*cunit->GetIRBuilder()),
64 func_(NULL),
65 regs_(code_item_->registers_size_),
66 reg_to_shadow_frame_index_(code_item_->registers_size_, -1),
67 retval_reg_(NULL),
Ian Rogers776ac1f2012-04-13 23:36:36 -070068 basic_block_stack_overflow_(NULL),
Logan Chien8b977d32012-02-21 19:14:55 +080069 basic_block_reg_alloca_(NULL), basic_block_shadow_frame_alloca_(NULL),
Logan Chienef4a6562012-04-24 18:02:24 +080070 basic_block_reg_arg_init_(NULL),
Logan Chien8b977d32012-02-21 19:14:55 +080071 basic_blocks_(code_item_->insns_size_in_code_units_),
72 basic_block_landing_pads_(code_item_->tries_size_, NULL),
73 basic_block_unwind_(NULL), basic_block_unreachable_(NULL),
Logan Chien937105a2012-04-02 02:37:37 +080074 shadow_frame_(NULL), elf_func_idx_(cunit_->AcquireUniqueElfFuncIndex()) {
Shih-wei Liaod1fec812012-02-13 09:51:10 -080075}
76
77
78MethodCompiler::~MethodCompiler() {
Logan Chienc670a8d2011-12-20 21:25:56 +080079 STLDeleteElements(&regs_);
Shih-wei Liaod1fec812012-02-13 09:51:10 -080080}
81
82
Logan Chien0b827102011-12-20 19:46:14 +080083void MethodCompiler::CreateFunction() {
84 // LLVM function name
Logan Chien937105a2012-04-02 02:37:37 +080085 std::string func_name(ElfFuncName(elf_func_idx_));
Logan Chien0b827102011-12-20 19:46:14 +080086
87 // Get function type
88 llvm::FunctionType* func_type =
Logan Chiendd361c92012-04-10 23:40:37 +080089 GetFunctionType(method_idx_, oat_compilation_unit_->IsStatic());
Logan Chien0b827102011-12-20 19:46:14 +080090
91 // Create function
92 func_ = llvm::Function::Create(func_type, llvm::Function::ExternalLinkage,
93 func_name, module_);
94
TDYa12767ae8ff2012-05-02 19:08:02 -070095#if !defined(NDEBUG)
Logan Chien0b827102011-12-20 19:46:14 +080096 // Set argument name
97 llvm::Function::arg_iterator arg_iter(func_->arg_begin());
98 llvm::Function::arg_iterator arg_end(func_->arg_end());
99
100 DCHECK_NE(arg_iter, arg_end);
101 arg_iter->setName("method");
102 ++arg_iter;
103
Logan Chiendd361c92012-04-10 23:40:37 +0800104 if (!oat_compilation_unit_->IsStatic()) {
Logan Chien0b827102011-12-20 19:46:14 +0800105 DCHECK_NE(arg_iter, arg_end);
106 arg_iter->setName("this");
107 ++arg_iter;
108 }
109
110 for (unsigned i = 0; arg_iter != arg_end; ++i, ++arg_iter) {
111 arg_iter->setName(StringPrintf("a%u", i));
112 }
TDYa12767ae8ff2012-05-02 19:08:02 -0700113#endif
Logan Chien0b827102011-12-20 19:46:14 +0800114}
115
116
117llvm::FunctionType* MethodCompiler::GetFunctionType(uint32_t method_idx,
118 bool is_static) {
119 // Get method signature
120 DexFile::MethodId const& method_id = dex_file_->GetMethodId(method_idx);
121
Logan Chien8faf8022012-02-24 12:25:29 +0800122 uint32_t shorty_size;
Logan Chien0b827102011-12-20 19:46:14 +0800123 char const* shorty = dex_file_->GetMethodShorty(method_id, &shorty_size);
Logan Chien8faf8022012-02-24 12:25:29 +0800124 CHECK_GE(shorty_size, 1u);
Logan Chien0b827102011-12-20 19:46:14 +0800125
126 // Get return type
127 llvm::Type* ret_type = irb_.getJType(shorty[0], kAccurate);
128
129 // Get argument type
130 std::vector<llvm::Type*> args_type;
131
132 args_type.push_back(irb_.getJObjectTy()); // method object pointer
133
134 if (!is_static) {
135 args_type.push_back(irb_.getJType('L', kAccurate)); // "this" object pointer
136 }
137
Logan Chien8faf8022012-02-24 12:25:29 +0800138 for (uint32_t i = 1; i < shorty_size; ++i) {
Logan Chien0b827102011-12-20 19:46:14 +0800139 args_type.push_back(irb_.getJType(shorty[i], kAccurate));
140 }
141
142 return llvm::FunctionType::get(ret_type, args_type, false);
143}
144
145
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800146void MethodCompiler::EmitPrologue() {
Logan Chienc670a8d2011-12-20 21:25:56 +0800147 // Create basic blocks for prologue
TDYa12799489132012-04-29 01:27:58 -0700148#if !defined(NDEBUG)
149 // Add a BasicBlock named as PrettyMethod for debugging.
150 llvm::BasicBlock* entry =
151 llvm::BasicBlock::Create(*context_, PrettyMethod(method_idx_, *dex_file_), func_);
152#endif
Logan Chienc670a8d2011-12-20 21:25:56 +0800153 basic_block_reg_alloca_ =
154 llvm::BasicBlock::Create(*context_, "prologue.alloca", func_);
155
TDYa12797339c42012-04-29 01:26:35 -0700156 basic_block_stack_overflow_ =
157 llvm::BasicBlock::Create(*context_, "prologue.stack_overflow_check", func_);
158
Logan Chien8dfcbea2012-02-17 18:50:32 +0800159 basic_block_shadow_frame_alloca_ =
160 llvm::BasicBlock::Create(*context_, "prologue.shadowframe", func_);
161
Logan Chiend6ececa2011-12-27 16:20:15 +0800162 basic_block_reg_arg_init_ =
163 llvm::BasicBlock::Create(*context_, "prologue.arginit", func_);
164
TDYa12799489132012-04-29 01:27:58 -0700165#if !defined(NDEBUG)
166 irb_.SetInsertPoint(entry);
167 irb_.CreateBr(basic_block_reg_alloca_);
168#endif
169
Logan Chienc670a8d2011-12-20 21:25:56 +0800170 // Create register array
171 for (uint16_t r = 0; r < code_item_->registers_size_; ++r) {
TDYa1271d7e5102012-05-13 09:27:05 -0700172 regs_[r] = DalvikReg::CreateLocalVarReg(*this, r);
Logan Chienc670a8d2011-12-20 21:25:56 +0800173 }
174
175 retval_reg_.reset(DalvikReg::CreateRetValReg(*this));
Logan Chiend6ececa2011-12-27 16:20:15 +0800176
Logan Chien8dfcbea2012-02-17 18:50:32 +0800177 // Create Shadow Frame
TDYa127cc1b4c32012-05-15 07:31:37 -0700178 if (method_info_.need_shadow_frame) {
179 EmitPrologueAllocShadowFrame();
180 }
Logan Chien8dfcbea2012-02-17 18:50:32 +0800181
Logan Chiend6ececa2011-12-27 16:20:15 +0800182 // Store argument to dalvik register
183 irb_.SetInsertPoint(basic_block_reg_arg_init_);
184 EmitPrologueAssignArgRegister();
185
186 // Branch to start address
187 irb_.CreateBr(GetBasicBlock(0));
Logan Chienc670a8d2011-12-20 21:25:56 +0800188}
189
190
TDYa1274165a832012-04-03 17:47:16 -0700191void MethodCompiler::EmitStackOverflowCheck() {
192 irb_.SetInsertPoint(basic_block_stack_overflow_);
193
194 // Call llvm intrinsic function to get frame address.
195 llvm::Function* frameaddress =
196 llvm::Intrinsic::getDeclaration(module_, llvm::Intrinsic::frameaddress);
197
198 // The type of llvm::frameaddress is: i8* @llvm.frameaddress(i32)
199 llvm::Value* frame_address = irb_.CreateCall(frameaddress, irb_.getInt32(0));
200
201 // Cast i8* to int
202 frame_address = irb_.CreatePtrToInt(frame_address, irb_.getPtrEquivIntTy());
203
204 // Get thread.stack_end_
205 llvm::Value* thread_object_addr =
206 irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
207
TDYa127ee1f59b2012-04-25 00:56:40 -0700208 llvm::Value* stack_end =
209 irb_.LoadFromObjectOffset(thread_object_addr,
210 Thread::StackEndOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -0700211 irb_.getPtrEquivIntTy(),
212 kTBAARuntimeInfo);
TDYa1274165a832012-04-03 17:47:16 -0700213
214 // Check the frame address < thread.stack_end_ ?
215 llvm::Value* is_stack_overflow = irb_.CreateICmpULT(frame_address, stack_end);
216
217 llvm::BasicBlock* block_exception =
218 llvm::BasicBlock::Create(*context_, "stack_overflow", func_);
219
220 llvm::BasicBlock* block_continue =
221 llvm::BasicBlock::Create(*context_, "stack_overflow_cont", func_);
222
TDYa127ac7b5bb2012-05-11 13:17:49 -0700223 irb_.CreateCondBr(is_stack_overflow, block_exception, block_continue, kUnlikely);
TDYa1274165a832012-04-03 17:47:16 -0700224
225 // If stack overflow, throw exception.
226 irb_.SetInsertPoint(block_exception);
227 irb_.CreateCall(irb_.GetRuntime(ThrowStackOverflowException));
228
229 // Unwind.
Logan Chiendd361c92012-04-10 23:40:37 +0800230 char ret_shorty = oat_compilation_unit_->GetShorty()[0];
TDYa1274165a832012-04-03 17:47:16 -0700231 if (ret_shorty == 'V') {
232 irb_.CreateRetVoid();
233 } else {
234 irb_.CreateRet(irb_.getJZero(ret_shorty));
235 }
236
237 basic_block_stack_overflow_ = block_continue;
238}
239
240
Logan Chienc670a8d2011-12-20 21:25:56 +0800241void MethodCompiler::EmitPrologueLastBranch() {
242 irb_.SetInsertPoint(basic_block_reg_alloca_);
TDYa12797339c42012-04-29 01:26:35 -0700243 irb_.CreateBr(basic_block_stack_overflow_);
244
TDYa127cc1b4c32012-05-15 07:31:37 -0700245 // If a method will not call to other method, and the method is small, we can avoid stack overflow
246 // check.
247 if (method_info_.has_invoke ||
248 code_item_->registers_size_ > 32) { // Small leaf function is OK given
249 // the 8KB reserved at Stack End
250 EmitStackOverflowCheck();
251 }
TDYa12797339c42012-04-29 01:26:35 -0700252
253 irb_.SetInsertPoint(basic_block_stack_overflow_);
Logan Chien8dfcbea2012-02-17 18:50:32 +0800254 irb_.CreateBr(basic_block_shadow_frame_alloca_);
255
256 irb_.SetInsertPoint(basic_block_shadow_frame_alloca_);
Logan Chiend6ececa2011-12-27 16:20:15 +0800257 irb_.CreateBr(basic_block_reg_arg_init_);
258}
259
260
Logan Chien8dfcbea2012-02-17 18:50:32 +0800261void MethodCompiler::EmitPrologueAllocShadowFrame() {
262 irb_.SetInsertPoint(basic_block_shadow_frame_alloca_);
263
264 // Allocate the shadow frame now!
TDYa1271d7e5102012-05-13 09:27:05 -0700265 uint32_t sirt_size = 0;
TDYa127f1652862012-05-16 21:44:35 -0700266 if (method_info_.need_shadow_frame_entry) {
267 for (uint32_t i = 0, num_of_regs = code_item_->registers_size_; i < num_of_regs; ++i) {
268 if (IsRegCanBeObject(i)) {
269 reg_to_shadow_frame_index_[i] = sirt_size++;
270 }
TDYa1271d7e5102012-05-13 09:27:05 -0700271 }
272 }
Logan Chien8dfcbea2012-02-17 18:50:32 +0800273
274 llvm::StructType* shadow_frame_type = irb_.getShadowFrameTy(sirt_size);
275 shadow_frame_ = irb_.CreateAlloca(shadow_frame_type);
276
277 // Zero-initialization of the shadow frame
278 llvm::ConstantAggregateZero* zero_initializer =
279 llvm::ConstantAggregateZero::get(shadow_frame_type);
280
TDYa127d955bec2012-05-11 10:54:02 -0700281 irb_.CreateStore(zero_initializer, shadow_frame_, kTBAAShadowFrame);
Logan Chien8dfcbea2012-02-17 18:50:32 +0800282
TDYa127ee1f59b2012-04-25 00:56:40 -0700283 // Get method object
Logan Chien8dfcbea2012-02-17 18:50:32 +0800284 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
TDYa127ee1f59b2012-04-25 00:56:40 -0700285
286 // Store the method pointer
287 irb_.StoreToObjectOffset(shadow_frame_,
288 ShadowFrame::MethodOffset(),
TDYa127aba61122012-05-04 18:28:36 -0700289 method_object_addr,
TDYa127d955bec2012-05-11 10:54:02 -0700290 kTBAAShadowFrame);
Logan Chien8dfcbea2012-02-17 18:50:32 +0800291
292 // Store the number of the pointer slots
TDYa127ee1f59b2012-04-25 00:56:40 -0700293 irb_.StoreToObjectOffset(shadow_frame_,
294 ShadowFrame::NumberOfReferencesOffset(),
TDYa127aba61122012-05-04 18:28:36 -0700295 irb_.getJInt(sirt_size),
TDYa127d955bec2012-05-11 10:54:02 -0700296 kTBAAShadowFrame);
Logan Chien8dfcbea2012-02-17 18:50:32 +0800297
298 // Push the shadow frame
299 llvm::Value* shadow_frame_upcast =
300 irb_.CreateConstGEP2_32(shadow_frame_, 0, 0);
301
302 irb_.CreateCall(irb_.GetRuntime(PushShadowFrame), shadow_frame_upcast);
303}
304
305
Logan Chiend6ececa2011-12-27 16:20:15 +0800306void MethodCompiler::EmitPrologueAssignArgRegister() {
307 uint16_t arg_reg = code_item_->registers_size_ - code_item_->ins_size_;
308
309 llvm::Function::arg_iterator arg_iter(func_->arg_begin());
310 llvm::Function::arg_iterator arg_end(func_->arg_end());
311
Logan Chiendd361c92012-04-10 23:40:37 +0800312 uint32_t shorty_size = 0;
313 char const* shorty = oat_compilation_unit_->GetShorty(&shorty_size);
314 CHECK_GE(shorty_size, 1u);
Logan Chiend6ececa2011-12-27 16:20:15 +0800315
316 ++arg_iter; // skip method object
317
Logan Chiendd361c92012-04-10 23:40:37 +0800318 if (!oat_compilation_unit_->IsStatic()) {
Logan Chiend6ececa2011-12-27 16:20:15 +0800319 EmitStoreDalvikReg(arg_reg, kObject, kAccurate, arg_iter);
320 ++arg_iter;
321 ++arg_reg;
322 }
323
Logan Chiendd361c92012-04-10 23:40:37 +0800324 for (uint32_t i = 1; i < shorty_size; ++i, ++arg_iter) {
Logan Chiend6ececa2011-12-27 16:20:15 +0800325 EmitStoreDalvikReg(arg_reg, shorty[i], kAccurate, arg_iter);
326
327 ++arg_reg;
328 if (shorty[i] == 'J' || shorty[i] == 'D') {
329 // Wide types, such as long and double, are using a pair of registers
330 // to store the value, so we have to increase arg_reg again.
331 ++arg_reg;
332 }
333 }
334
335 DCHECK_EQ(arg_end, arg_iter);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800336}
337
338
Logan Chien83426162011-12-09 09:29:50 +0800339void MethodCompiler::EmitInstructions() {
Logan Chiend6c239a2011-12-23 15:11:45 +0800340 uint32_t dex_pc = 0;
341 while (dex_pc < code_item_->insns_size_in_code_units_) {
342 Instruction const* insn = Instruction::At(code_item_->insns_ + dex_pc);
343 EmitInstruction(dex_pc, insn);
344 dex_pc += insn->SizeInCodeUnits();
345 }
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800346}
347
348
Logan Chien83426162011-12-09 09:29:50 +0800349void MethodCompiler::EmitInstruction(uint32_t dex_pc,
350 Instruction const* insn) {
Logan Chiend6c239a2011-12-23 15:11:45 +0800351
352 // Set the IRBuilder insertion point
353 irb_.SetInsertPoint(GetBasicBlock(dex_pc));
354
Logan Chien70f94b42011-12-27 17:49:11 +0800355#define ARGS dex_pc, insn
356
357 // Dispatch the instruction
358 switch (insn->Opcode()) {
359 case Instruction::NOP:
360 EmitInsn_Nop(ARGS);
361 break;
362
363 case Instruction::MOVE:
364 case Instruction::MOVE_FROM16:
365 case Instruction::MOVE_16:
366 EmitInsn_Move(ARGS, kInt);
367 break;
368
369 case Instruction::MOVE_WIDE:
370 case Instruction::MOVE_WIDE_FROM16:
371 case Instruction::MOVE_WIDE_16:
372 EmitInsn_Move(ARGS, kLong);
373 break;
374
375 case Instruction::MOVE_OBJECT:
376 case Instruction::MOVE_OBJECT_FROM16:
377 case Instruction::MOVE_OBJECT_16:
378 EmitInsn_Move(ARGS, kObject);
379 break;
380
381 case Instruction::MOVE_RESULT:
382 EmitInsn_MoveResult(ARGS, kInt);
383 break;
384
385 case Instruction::MOVE_RESULT_WIDE:
386 EmitInsn_MoveResult(ARGS, kLong);
387 break;
388
389 case Instruction::MOVE_RESULT_OBJECT:
390 EmitInsn_MoveResult(ARGS, kObject);
391 break;
392
393 case Instruction::MOVE_EXCEPTION:
394 EmitInsn_MoveException(ARGS);
395 break;
396
397 case Instruction::RETURN_VOID:
398 EmitInsn_ReturnVoid(ARGS);
399 break;
400
401 case Instruction::RETURN:
402 case Instruction::RETURN_WIDE:
403 case Instruction::RETURN_OBJECT:
404 EmitInsn_Return(ARGS);
405 break;
406
407 case Instruction::CONST_4:
408 case Instruction::CONST_16:
409 case Instruction::CONST:
410 case Instruction::CONST_HIGH16:
411 EmitInsn_LoadConstant(ARGS, kInt);
412 break;
413
414 case Instruction::CONST_WIDE_16:
415 case Instruction::CONST_WIDE_32:
416 case Instruction::CONST_WIDE:
417 case Instruction::CONST_WIDE_HIGH16:
418 EmitInsn_LoadConstant(ARGS, kLong);
419 break;
420
421 case Instruction::CONST_STRING:
422 case Instruction::CONST_STRING_JUMBO:
423 EmitInsn_LoadConstantString(ARGS);
424 break;
425
426 case Instruction::CONST_CLASS:
427 EmitInsn_LoadConstantClass(ARGS);
428 break;
429
430 case Instruction::MONITOR_ENTER:
431 EmitInsn_MonitorEnter(ARGS);
432 break;
433
434 case Instruction::MONITOR_EXIT:
435 EmitInsn_MonitorExit(ARGS);
436 break;
437
438 case Instruction::CHECK_CAST:
439 EmitInsn_CheckCast(ARGS);
440 break;
441
442 case Instruction::INSTANCE_OF:
443 EmitInsn_InstanceOf(ARGS);
444 break;
445
446 case Instruction::ARRAY_LENGTH:
447 EmitInsn_ArrayLength(ARGS);
448 break;
449
450 case Instruction::NEW_INSTANCE:
451 EmitInsn_NewInstance(ARGS);
452 break;
453
454 case Instruction::NEW_ARRAY:
455 EmitInsn_NewArray(ARGS);
456 break;
457
458 case Instruction::FILLED_NEW_ARRAY:
459 EmitInsn_FilledNewArray(ARGS, false);
460 break;
461
462 case Instruction::FILLED_NEW_ARRAY_RANGE:
463 EmitInsn_FilledNewArray(ARGS, true);
464 break;
465
466 case Instruction::FILL_ARRAY_DATA:
467 EmitInsn_FillArrayData(ARGS);
468 break;
469
470 case Instruction::THROW:
471 EmitInsn_ThrowException(ARGS);
472 break;
473
474 case Instruction::GOTO:
475 case Instruction::GOTO_16:
476 case Instruction::GOTO_32:
477 EmitInsn_UnconditionalBranch(ARGS);
478 break;
479
480 case Instruction::PACKED_SWITCH:
481 EmitInsn_PackedSwitch(ARGS);
482 break;
483
484 case Instruction::SPARSE_SWITCH:
485 EmitInsn_SparseSwitch(ARGS);
486 break;
487
488 case Instruction::CMPL_FLOAT:
489 EmitInsn_FPCompare(ARGS, kFloat, false);
490 break;
491
492 case Instruction::CMPG_FLOAT:
493 EmitInsn_FPCompare(ARGS, kFloat, true);
494 break;
495
496 case Instruction::CMPL_DOUBLE:
497 EmitInsn_FPCompare(ARGS, kDouble, false);
498 break;
499
500 case Instruction::CMPG_DOUBLE:
501 EmitInsn_FPCompare(ARGS, kDouble, true);
502 break;
503
504 case Instruction::CMP_LONG:
505 EmitInsn_LongCompare(ARGS);
506 break;
507
508 case Instruction::IF_EQ:
509 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_EQ);
510 break;
511
512 case Instruction::IF_NE:
513 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_NE);
514 break;
515
516 case Instruction::IF_LT:
517 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_LT);
518 break;
519
520 case Instruction::IF_GE:
521 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_GE);
522 break;
523
524 case Instruction::IF_GT:
525 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_GT);
526 break;
527
528 case Instruction::IF_LE:
529 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_LE);
530 break;
531
532 case Instruction::IF_EQZ:
533 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_EQ);
534 break;
535
536 case Instruction::IF_NEZ:
537 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_NE);
538 break;
539
540 case Instruction::IF_LTZ:
541 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_LT);
542 break;
543
544 case Instruction::IF_GEZ:
545 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_GE);
546 break;
547
548 case Instruction::IF_GTZ:
549 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_GT);
550 break;
551
552 case Instruction::IF_LEZ:
553 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_LE);
554 break;
555
556 case Instruction::AGET:
557 EmitInsn_AGet(ARGS, kInt);
558 break;
559
560 case Instruction::AGET_WIDE:
561 EmitInsn_AGet(ARGS, kLong);
562 break;
563
564 case Instruction::AGET_OBJECT:
565 EmitInsn_AGet(ARGS, kObject);
566 break;
567
568 case Instruction::AGET_BOOLEAN:
569 EmitInsn_AGet(ARGS, kBoolean);
570 break;
571
572 case Instruction::AGET_BYTE:
573 EmitInsn_AGet(ARGS, kByte);
574 break;
575
576 case Instruction::AGET_CHAR:
577 EmitInsn_AGet(ARGS, kChar);
578 break;
579
580 case Instruction::AGET_SHORT:
581 EmitInsn_AGet(ARGS, kShort);
582 break;
583
584 case Instruction::APUT:
585 EmitInsn_APut(ARGS, kInt);
586 break;
587
588 case Instruction::APUT_WIDE:
589 EmitInsn_APut(ARGS, kLong);
590 break;
591
592 case Instruction::APUT_OBJECT:
593 EmitInsn_APut(ARGS, kObject);
594 break;
595
596 case Instruction::APUT_BOOLEAN:
597 EmitInsn_APut(ARGS, kBoolean);
598 break;
599
600 case Instruction::APUT_BYTE:
601 EmitInsn_APut(ARGS, kByte);
602 break;
603
604 case Instruction::APUT_CHAR:
605 EmitInsn_APut(ARGS, kChar);
606 break;
607
608 case Instruction::APUT_SHORT:
609 EmitInsn_APut(ARGS, kShort);
610 break;
611
612 case Instruction::IGET:
613 EmitInsn_IGet(ARGS, kInt);
614 break;
615
616 case Instruction::IGET_WIDE:
617 EmitInsn_IGet(ARGS, kLong);
618 break;
619
620 case Instruction::IGET_OBJECT:
621 EmitInsn_IGet(ARGS, kObject);
622 break;
623
624 case Instruction::IGET_BOOLEAN:
625 EmitInsn_IGet(ARGS, kBoolean);
626 break;
627
628 case Instruction::IGET_BYTE:
629 EmitInsn_IGet(ARGS, kByte);
630 break;
631
632 case Instruction::IGET_CHAR:
633 EmitInsn_IGet(ARGS, kChar);
634 break;
635
636 case Instruction::IGET_SHORT:
637 EmitInsn_IGet(ARGS, kShort);
638 break;
639
640 case Instruction::IPUT:
641 EmitInsn_IPut(ARGS, kInt);
642 break;
643
644 case Instruction::IPUT_WIDE:
645 EmitInsn_IPut(ARGS, kLong);
646 break;
647
648 case Instruction::IPUT_OBJECT:
649 EmitInsn_IPut(ARGS, kObject);
650 break;
651
652 case Instruction::IPUT_BOOLEAN:
653 EmitInsn_IPut(ARGS, kBoolean);
654 break;
655
656 case Instruction::IPUT_BYTE:
657 EmitInsn_IPut(ARGS, kByte);
658 break;
659
660 case Instruction::IPUT_CHAR:
661 EmitInsn_IPut(ARGS, kChar);
662 break;
663
664 case Instruction::IPUT_SHORT:
665 EmitInsn_IPut(ARGS, kShort);
666 break;
667
668 case Instruction::SGET:
669 EmitInsn_SGet(ARGS, kInt);
670 break;
671
672 case Instruction::SGET_WIDE:
673 EmitInsn_SGet(ARGS, kLong);
674 break;
675
676 case Instruction::SGET_OBJECT:
677 EmitInsn_SGet(ARGS, kObject);
678 break;
679
680 case Instruction::SGET_BOOLEAN:
681 EmitInsn_SGet(ARGS, kBoolean);
682 break;
683
684 case Instruction::SGET_BYTE:
685 EmitInsn_SGet(ARGS, kByte);
686 break;
687
688 case Instruction::SGET_CHAR:
689 EmitInsn_SGet(ARGS, kChar);
690 break;
691
692 case Instruction::SGET_SHORT:
693 EmitInsn_SGet(ARGS, kShort);
694 break;
695
696 case Instruction::SPUT:
697 EmitInsn_SPut(ARGS, kInt);
698 break;
699
700 case Instruction::SPUT_WIDE:
701 EmitInsn_SPut(ARGS, kLong);
702 break;
703
704 case Instruction::SPUT_OBJECT:
705 EmitInsn_SPut(ARGS, kObject);
706 break;
707
708 case Instruction::SPUT_BOOLEAN:
709 EmitInsn_SPut(ARGS, kBoolean);
710 break;
711
712 case Instruction::SPUT_BYTE:
713 EmitInsn_SPut(ARGS, kByte);
714 break;
715
716 case Instruction::SPUT_CHAR:
717 EmitInsn_SPut(ARGS, kChar);
718 break;
719
720 case Instruction::SPUT_SHORT:
721 EmitInsn_SPut(ARGS, kShort);
722 break;
723
724
725 case Instruction::INVOKE_VIRTUAL:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800726 EmitInsn_Invoke(ARGS, kVirtual, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800727 break;
728
729 case Instruction::INVOKE_SUPER:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800730 EmitInsn_Invoke(ARGS, kSuper, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800731 break;
732
733 case Instruction::INVOKE_DIRECT:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800734 EmitInsn_Invoke(ARGS, kDirect, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800735 break;
736
737 case Instruction::INVOKE_STATIC:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800738 EmitInsn_Invoke(ARGS, kStatic, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800739 break;
740
741 case Instruction::INVOKE_INTERFACE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800742 EmitInsn_Invoke(ARGS, kInterface, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800743 break;
744
745 case Instruction::INVOKE_VIRTUAL_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800746 EmitInsn_Invoke(ARGS, kVirtual, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800747 break;
748
749 case Instruction::INVOKE_SUPER_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800750 EmitInsn_Invoke(ARGS, kSuper, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800751 break;
752
753 case Instruction::INVOKE_DIRECT_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800754 EmitInsn_Invoke(ARGS, kDirect, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800755 break;
756
757 case Instruction::INVOKE_STATIC_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800758 EmitInsn_Invoke(ARGS, kStatic, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800759 break;
760
761 case Instruction::INVOKE_INTERFACE_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800762 EmitInsn_Invoke(ARGS, kInterface, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800763 break;
764
765 case Instruction::NEG_INT:
766 EmitInsn_Neg(ARGS, kInt);
767 break;
768
769 case Instruction::NOT_INT:
770 EmitInsn_Not(ARGS, kInt);
771 break;
772
773 case Instruction::NEG_LONG:
774 EmitInsn_Neg(ARGS, kLong);
775 break;
776
777 case Instruction::NOT_LONG:
778 EmitInsn_Not(ARGS, kLong);
779 break;
780
781 case Instruction::NEG_FLOAT:
782 EmitInsn_FNeg(ARGS, kFloat);
783 break;
784
785 case Instruction::NEG_DOUBLE:
786 EmitInsn_FNeg(ARGS, kDouble);
787 break;
788
789 case Instruction::INT_TO_LONG:
790 EmitInsn_SExt(ARGS);
791 break;
792
793 case Instruction::INT_TO_FLOAT:
794 EmitInsn_IntToFP(ARGS, kInt, kFloat);
795 break;
796
797 case Instruction::INT_TO_DOUBLE:
798 EmitInsn_IntToFP(ARGS, kInt, kDouble);
799 break;
800
801 case Instruction::LONG_TO_INT:
802 EmitInsn_Trunc(ARGS);
803 break;
804
805 case Instruction::LONG_TO_FLOAT:
806 EmitInsn_IntToFP(ARGS, kLong, kFloat);
807 break;
808
809 case Instruction::LONG_TO_DOUBLE:
810 EmitInsn_IntToFP(ARGS, kLong, kDouble);
811 break;
812
813 case Instruction::FLOAT_TO_INT:
jeffhao41005dd2012-05-09 17:58:52 -0700814 EmitInsn_FPToInt(ARGS, kFloat, kInt, art_f2i);
Logan Chien70f94b42011-12-27 17:49:11 +0800815 break;
816
817 case Instruction::FLOAT_TO_LONG:
jeffhao41005dd2012-05-09 17:58:52 -0700818 EmitInsn_FPToInt(ARGS, kFloat, kLong, art_f2l);
Logan Chien70f94b42011-12-27 17:49:11 +0800819 break;
820
821 case Instruction::FLOAT_TO_DOUBLE:
822 EmitInsn_FExt(ARGS);
823 break;
824
825 case Instruction::DOUBLE_TO_INT:
jeffhao41005dd2012-05-09 17:58:52 -0700826 EmitInsn_FPToInt(ARGS, kDouble, kInt, art_d2i);
Logan Chien70f94b42011-12-27 17:49:11 +0800827 break;
828
829 case Instruction::DOUBLE_TO_LONG:
jeffhao41005dd2012-05-09 17:58:52 -0700830 EmitInsn_FPToInt(ARGS, kDouble, kLong, art_d2l);
Logan Chien70f94b42011-12-27 17:49:11 +0800831 break;
832
833 case Instruction::DOUBLE_TO_FLOAT:
834 EmitInsn_FTrunc(ARGS);
835 break;
836
837 case Instruction::INT_TO_BYTE:
838 EmitInsn_TruncAndSExt(ARGS, 8);
839 break;
840
841 case Instruction::INT_TO_CHAR:
842 EmitInsn_TruncAndZExt(ARGS, 16);
843 break;
844
845 case Instruction::INT_TO_SHORT:
846 EmitInsn_TruncAndSExt(ARGS, 16);
847 break;
848
849 case Instruction::ADD_INT:
850 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kInt, false);
851 break;
852
853 case Instruction::SUB_INT:
854 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kInt, false);
855 break;
856
857 case Instruction::MUL_INT:
858 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kInt, false);
859 break;
860
861 case Instruction::DIV_INT:
862 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kInt, false);
863 break;
864
865 case Instruction::REM_INT:
866 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kInt, false);
867 break;
868
869 case Instruction::AND_INT:
870 EmitInsn_IntArithm(ARGS, kIntArithm_And, kInt, false);
871 break;
872
873 case Instruction::OR_INT:
874 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kInt, false);
875 break;
876
877 case Instruction::XOR_INT:
878 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kInt, false);
879 break;
880
881 case Instruction::SHL_INT:
Logan Chien5539ad02012-04-02 14:36:55 +0800882 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kInt, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800883 break;
884
885 case Instruction::SHR_INT:
Logan Chien5539ad02012-04-02 14:36:55 +0800886 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kInt, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800887 break;
888
889 case Instruction::USHR_INT:
Logan Chien5539ad02012-04-02 14:36:55 +0800890 EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kInt, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800891 break;
892
893 case Instruction::ADD_LONG:
894 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kLong, false);
895 break;
896
897 case Instruction::SUB_LONG:
898 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kLong, false);
899 break;
900
901 case Instruction::MUL_LONG:
902 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kLong, false);
903 break;
904
905 case Instruction::DIV_LONG:
906 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kLong, false);
907 break;
908
909 case Instruction::REM_LONG:
910 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kLong, false);
911 break;
912
913 case Instruction::AND_LONG:
914 EmitInsn_IntArithm(ARGS, kIntArithm_And, kLong, false);
915 break;
916
917 case Instruction::OR_LONG:
918 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kLong, false);
919 break;
920
921 case Instruction::XOR_LONG:
922 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kLong, false);
923 break;
924
925 case Instruction::SHL_LONG:
Logan Chien5539ad02012-04-02 14:36:55 +0800926 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kLong, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800927 break;
928
929 case Instruction::SHR_LONG:
Logan Chien5539ad02012-04-02 14:36:55 +0800930 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kLong, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800931 break;
932
933 case Instruction::USHR_LONG:
Logan Chien5539ad02012-04-02 14:36:55 +0800934 EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kLong, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800935 break;
936
937 case Instruction::ADD_FLOAT:
938 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kFloat, false);
939 break;
940
941 case Instruction::SUB_FLOAT:
942 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kFloat, false);
943 break;
944
945 case Instruction::MUL_FLOAT:
946 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kFloat, false);
947 break;
948
949 case Instruction::DIV_FLOAT:
950 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kFloat, false);
951 break;
952
953 case Instruction::REM_FLOAT:
954 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kFloat, false);
955 break;
956
957 case Instruction::ADD_DOUBLE:
958 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kDouble, false);
959 break;
960
961 case Instruction::SUB_DOUBLE:
962 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kDouble, false);
963 break;
964
965 case Instruction::MUL_DOUBLE:
966 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kDouble, false);
967 break;
968
969 case Instruction::DIV_DOUBLE:
970 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kDouble, false);
971 break;
972
973 case Instruction::REM_DOUBLE:
974 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kDouble, false);
975 break;
976
977 case Instruction::ADD_INT_2ADDR:
978 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kInt, true);
979 break;
980
981 case Instruction::SUB_INT_2ADDR:
982 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kInt, true);
983 break;
984
985 case Instruction::MUL_INT_2ADDR:
986 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kInt, true);
987 break;
988
989 case Instruction::DIV_INT_2ADDR:
990 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kInt, true);
991 break;
992
993 case Instruction::REM_INT_2ADDR:
994 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kInt, true);
995 break;
996
997 case Instruction::AND_INT_2ADDR:
998 EmitInsn_IntArithm(ARGS, kIntArithm_And, kInt, true);
999 break;
1000
1001 case Instruction::OR_INT_2ADDR:
1002 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kInt, true);
1003 break;
1004
1005 case Instruction::XOR_INT_2ADDR:
1006 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kInt, true);
1007 break;
1008
1009 case Instruction::SHL_INT_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001010 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kInt, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001011 break;
1012
1013 case Instruction::SHR_INT_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001014 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kInt, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001015 break;
1016
1017 case Instruction::USHR_INT_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001018 EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kInt, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001019 break;
1020
1021 case Instruction::ADD_LONG_2ADDR:
1022 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kLong, true);
1023 break;
1024
1025 case Instruction::SUB_LONG_2ADDR:
1026 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kLong, true);
1027 break;
1028
1029 case Instruction::MUL_LONG_2ADDR:
1030 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kLong, true);
1031 break;
1032
1033 case Instruction::DIV_LONG_2ADDR:
1034 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kLong, true);
1035 break;
1036
1037 case Instruction::REM_LONG_2ADDR:
1038 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kLong, true);
1039 break;
1040
1041 case Instruction::AND_LONG_2ADDR:
1042 EmitInsn_IntArithm(ARGS, kIntArithm_And, kLong, true);
1043 break;
1044
1045 case Instruction::OR_LONG_2ADDR:
1046 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kLong, true);
1047 break;
1048
1049 case Instruction::XOR_LONG_2ADDR:
1050 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kLong, true);
1051 break;
1052
1053 case Instruction::SHL_LONG_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001054 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kLong, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001055 break;
1056
1057 case Instruction::SHR_LONG_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001058 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kLong, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001059 break;
1060
1061 case Instruction::USHR_LONG_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001062 EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kLong, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001063 break;
1064
1065 case Instruction::ADD_FLOAT_2ADDR:
1066 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kFloat, true);
1067 break;
1068
1069 case Instruction::SUB_FLOAT_2ADDR:
1070 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kFloat, true);
1071 break;
1072
1073 case Instruction::MUL_FLOAT_2ADDR:
1074 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kFloat, true);
1075 break;
1076
1077 case Instruction::DIV_FLOAT_2ADDR:
1078 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kFloat, true);
1079 break;
1080
1081 case Instruction::REM_FLOAT_2ADDR:
1082 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kFloat, true);
1083 break;
1084
1085 case Instruction::ADD_DOUBLE_2ADDR:
1086 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kDouble, true);
1087 break;
1088
1089 case Instruction::SUB_DOUBLE_2ADDR:
1090 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kDouble, true);
1091 break;
1092
1093 case Instruction::MUL_DOUBLE_2ADDR:
1094 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kDouble, true);
1095 break;
1096
1097 case Instruction::DIV_DOUBLE_2ADDR:
1098 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kDouble, true);
1099 break;
1100
1101 case Instruction::REM_DOUBLE_2ADDR:
1102 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kDouble, true);
1103 break;
1104
1105 case Instruction::ADD_INT_LIT16:
1106 case Instruction::ADD_INT_LIT8:
1107 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Add);
1108 break;
1109
1110 case Instruction::RSUB_INT:
1111 case Instruction::RSUB_INT_LIT8:
1112 EmitInsn_RSubImmediate(ARGS);
1113 break;
1114
1115 case Instruction::MUL_INT_LIT16:
1116 case Instruction::MUL_INT_LIT8:
1117 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Mul);
1118 break;
1119
1120 case Instruction::DIV_INT_LIT16:
1121 case Instruction::DIV_INT_LIT8:
1122 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Div);
1123 break;
1124
1125 case Instruction::REM_INT_LIT16:
1126 case Instruction::REM_INT_LIT8:
1127 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Rem);
1128 break;
1129
1130 case Instruction::AND_INT_LIT16:
1131 case Instruction::AND_INT_LIT8:
1132 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_And);
1133 break;
1134
1135 case Instruction::OR_INT_LIT16:
1136 case Instruction::OR_INT_LIT8:
1137 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Or);
1138 break;
1139
1140 case Instruction::XOR_INT_LIT16:
1141 case Instruction::XOR_INT_LIT8:
1142 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Xor);
1143 break;
1144
1145 case Instruction::SHL_INT_LIT8:
Logan Chien5539ad02012-04-02 14:36:55 +08001146 EmitInsn_IntShiftArithmImmediate(ARGS, kIntArithm_Shl);
Logan Chien70f94b42011-12-27 17:49:11 +08001147 break;
1148
1149 case Instruction::SHR_INT_LIT8:
Logan Chien5539ad02012-04-02 14:36:55 +08001150 EmitInsn_IntShiftArithmImmediate(ARGS, kIntArithm_Shr);
Logan Chien70f94b42011-12-27 17:49:11 +08001151 break;
1152
1153 case Instruction::USHR_INT_LIT8:
Logan Chien5539ad02012-04-02 14:36:55 +08001154 EmitInsn_IntShiftArithmImmediate(ARGS, kIntArithm_UShr);
Logan Chien70f94b42011-12-27 17:49:11 +08001155 break;
1156
Logan Chien9e5f5c12012-04-10 13:51:45 +08001157 case Instruction::THROW_VERIFICATION_ERROR:
1158 EmitInsn_ThrowVerificationError(ARGS);
1159 break;
1160
Logan Chien70f94b42011-12-27 17:49:11 +08001161 case Instruction::UNUSED_3E:
1162 case Instruction::UNUSED_3F:
1163 case Instruction::UNUSED_40:
1164 case Instruction::UNUSED_41:
1165 case Instruction::UNUSED_42:
1166 case Instruction::UNUSED_43:
1167 case Instruction::UNUSED_73:
1168 case Instruction::UNUSED_79:
1169 case Instruction::UNUSED_7A:
1170 case Instruction::UNUSED_E3:
1171 case Instruction::UNUSED_E4:
1172 case Instruction::UNUSED_E5:
1173 case Instruction::UNUSED_E6:
1174 case Instruction::UNUSED_E7:
1175 case Instruction::UNUSED_E8:
1176 case Instruction::UNUSED_E9:
1177 case Instruction::UNUSED_EA:
1178 case Instruction::UNUSED_EB:
1179 case Instruction::UNUSED_EC:
Logan Chien70f94b42011-12-27 17:49:11 +08001180 case Instruction::UNUSED_EE:
1181 case Instruction::UNUSED_EF:
1182 case Instruction::UNUSED_F0:
1183 case Instruction::UNUSED_F1:
1184 case Instruction::UNUSED_F2:
1185 case Instruction::UNUSED_F3:
1186 case Instruction::UNUSED_F4:
1187 case Instruction::UNUSED_F5:
1188 case Instruction::UNUSED_F6:
1189 case Instruction::UNUSED_F7:
1190 case Instruction::UNUSED_F8:
1191 case Instruction::UNUSED_F9:
1192 case Instruction::UNUSED_FA:
1193 case Instruction::UNUSED_FB:
1194 case Instruction::UNUSED_FC:
1195 case Instruction::UNUSED_FD:
1196 case Instruction::UNUSED_FE:
1197 case Instruction::UNUSED_FF:
1198 LOG(FATAL) << "Dex file contains UNUSED bytecode: " << insn->Opcode();
1199 break;
1200 }
1201
1202#undef ARGS
1203}
1204
1205
1206void MethodCompiler::EmitInsn_Nop(uint32_t dex_pc,
1207 Instruction const* insn) {
Logan Chiene09a6b72011-12-27 17:50:21 +08001208
1209 uint16_t insn_signature = code_item_->insns_[dex_pc];
1210
1211 if (insn_signature == Instruction::kPackedSwitchSignature ||
1212 insn_signature == Instruction::kSparseSwitchSignature ||
1213 insn_signature == Instruction::kArrayDataSignature) {
1214 irb_.CreateUnreachable();
Elliott Hughesb25c3f62012-03-26 16:35:06 -07001215 } else {
Logan Chiene09a6b72011-12-27 17:50:21 +08001216 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1217 }
Shih-wei Liaod1fec812012-02-13 09:51:10 -08001218}
1219
1220
Logan Chien70f94b42011-12-27 17:49:11 +08001221void MethodCompiler::EmitInsn_Move(uint32_t dex_pc,
1222 Instruction const* insn,
1223 JType jty) {
Logan Chien48173132011-12-27 17:51:13 +08001224
Elliott Hughesadb8c672012-03-06 16:49:32 -08001225 DecodedInstruction dec_insn(insn);
Logan Chien48173132011-12-27 17:51:13 +08001226
Elliott Hughesadb8c672012-03-06 16:49:32 -08001227 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, jty, kReg);
1228 EmitStoreDalvikReg(dec_insn.vA, jty, kReg, src_value);
Logan Chien48173132011-12-27 17:51:13 +08001229
Logan Chien70f94b42011-12-27 17:49:11 +08001230 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1231}
1232
1233
1234void MethodCompiler::EmitInsn_MoveResult(uint32_t dex_pc,
1235 Instruction const* insn,
1236 JType jty) {
Logan Chien48173132011-12-27 17:51:13 +08001237
Elliott Hughesadb8c672012-03-06 16:49:32 -08001238 DecodedInstruction dec_insn(insn);
Logan Chien48173132011-12-27 17:51:13 +08001239
1240 llvm::Value* src_value = EmitLoadDalvikRetValReg(jty, kReg);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001241 EmitStoreDalvikReg(dec_insn.vA, jty, kReg, src_value);
Logan Chien48173132011-12-27 17:51:13 +08001242
Logan Chien70f94b42011-12-27 17:49:11 +08001243 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1244}
1245
1246
1247void MethodCompiler::EmitInsn_MoveException(uint32_t dex_pc,
1248 Instruction const* insn) {
Logan Chien3354cec2012-01-13 14:29:03 +08001249
Elliott Hughesadb8c672012-03-06 16:49:32 -08001250 DecodedInstruction dec_insn(insn);
Logan Chien3354cec2012-01-13 14:29:03 +08001251
TDYa127ee1f59b2012-04-25 00:56:40 -07001252 // Get thread
Logan Chien3354cec2012-01-13 14:29:03 +08001253 llvm::Value* thread_object_addr =
1254 irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1255
TDYa127ee1f59b2012-04-25 00:56:40 -07001256 // Get thread-local exception field address
1257 llvm::Value* exception_object_addr =
1258 irb_.LoadFromObjectOffset(thread_object_addr,
1259 Thread::ExceptionOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -07001260 irb_.getJObjectTy(),
TDYa1278ca10052012-05-05 19:57:06 -07001261 kTBAAJRuntime);
Logan Chien3354cec2012-01-13 14:29:03 +08001262
1263 // Set thread-local exception field address to NULL
TDYa127ee1f59b2012-04-25 00:56:40 -07001264 irb_.StoreToObjectOffset(thread_object_addr,
1265 Thread::ExceptionOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -07001266 irb_.getJNull(),
TDYa1278ca10052012-05-05 19:57:06 -07001267 kTBAAJRuntime);
Logan Chien3354cec2012-01-13 14:29:03 +08001268
1269 // Keep the exception object in the Dalvik register
Elliott Hughesadb8c672012-03-06 16:49:32 -08001270 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, exception_object_addr);
Logan Chien3354cec2012-01-13 14:29:03 +08001271
Logan Chien70f94b42011-12-27 17:49:11 +08001272 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1273}
1274
1275
1276void MethodCompiler::EmitInsn_ThrowException(uint32_t dex_pc,
1277 Instruction const* insn) {
Logan Chien6c6f12d2012-01-13 19:26:27 +08001278
Elliott Hughesadb8c672012-03-06 16:49:32 -08001279 DecodedInstruction dec_insn(insn);
Logan Chien6c6f12d2012-01-13 19:26:27 +08001280
1281 llvm::Value* exception_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001282 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chien6c6f12d2012-01-13 19:26:27 +08001283
TDYa127c8dc1012012-04-19 07:03:33 -07001284 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001285
Logan Chien6c6f12d2012-01-13 19:26:27 +08001286 irb_.CreateCall(irb_.GetRuntime(ThrowException), exception_addr);
1287
1288 EmitBranchExceptionLandingPad(dex_pc);
Logan Chien70f94b42011-12-27 17:49:11 +08001289}
1290
1291
Logan Chien9e5f5c12012-04-10 13:51:45 +08001292void MethodCompiler::EmitInsn_ThrowVerificationError(uint32_t dex_pc,
1293 Instruction const* insn) {
1294
1295 DecodedInstruction dec_insn(insn);
1296
TDYa127c8dc1012012-04-19 07:03:33 -07001297 EmitUpdateDexPC(dex_pc);
Logan Chien9e5f5c12012-04-10 13:51:45 +08001298
1299 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1300 llvm::Value* kind_value = irb_.getInt32(dec_insn.vA);
1301 llvm::Value* ref_value = irb_.getInt32(dec_insn.vB);
1302
1303 irb_.CreateCall3(irb_.GetRuntime(ThrowVerificationError),
1304 method_object_addr, kind_value, ref_value);
1305
1306 EmitBranchExceptionLandingPad(dex_pc);
1307}
1308
1309
Logan Chien70f94b42011-12-27 17:49:11 +08001310void MethodCompiler::EmitInsn_ReturnVoid(uint32_t dex_pc,
1311 Instruction const* insn) {
Logan Chien8898a272011-12-27 17:51:56 +08001312 // Garbage collection safe-point
1313 EmitGuard_GarbageCollectionSuspend(dex_pc);
1314
Logan Chien8dfcbea2012-02-17 18:50:32 +08001315 // Pop the shadow frame
1316 EmitPopShadowFrame();
1317
Logan Chien8898a272011-12-27 17:51:56 +08001318 // Return!
1319 irb_.CreateRetVoid();
Logan Chien70f94b42011-12-27 17:49:11 +08001320}
1321
1322
1323void MethodCompiler::EmitInsn_Return(uint32_t dex_pc,
1324 Instruction const* insn) {
Logan Chien8898a272011-12-27 17:51:56 +08001325
Elliott Hughesadb8c672012-03-06 16:49:32 -08001326 DecodedInstruction dec_insn(insn);
Logan Chien8898a272011-12-27 17:51:56 +08001327
1328 // Garbage collection safe-point
1329 EmitGuard_GarbageCollectionSuspend(dex_pc);
1330
Logan Chien8dfcbea2012-02-17 18:50:32 +08001331 // Pop the shadow frame
1332 EmitPopShadowFrame();
1333 // NOTE: It is important to keep this AFTER the GC safe-point. Otherwise,
1334 // the return value might be collected since the shadow stack is popped.
1335
Logan Chien8898a272011-12-27 17:51:56 +08001336 // Return!
Logan Chiendd361c92012-04-10 23:40:37 +08001337 char ret_shorty = oat_compilation_unit_->GetShorty()[0];
Elliott Hughesadb8c672012-03-06 16:49:32 -08001338 llvm::Value* retval = EmitLoadDalvikReg(dec_insn.vA, ret_shorty, kAccurate);
Logan Chien8898a272011-12-27 17:51:56 +08001339
1340 irb_.CreateRet(retval);
Logan Chien70f94b42011-12-27 17:49:11 +08001341}
1342
1343
1344void MethodCompiler::EmitInsn_LoadConstant(uint32_t dex_pc,
1345 Instruction const* insn,
1346 JType imm_jty) {
Shih-wei Liao798366e2012-02-16 09:25:33 -08001347
Elliott Hughesadb8c672012-03-06 16:49:32 -08001348 DecodedInstruction dec_insn(insn);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001349
1350 DCHECK(imm_jty == kInt || imm_jty == kLong) << imm_jty;
1351
1352 int64_t imm = 0;
1353
1354 switch (insn->Opcode()) {
1355 // 32-bit Immediate
1356 case Instruction::CONST_4:
1357 case Instruction::CONST_16:
1358 case Instruction::CONST:
1359 case Instruction::CONST_WIDE_16:
1360 case Instruction::CONST_WIDE_32:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001361 imm = static_cast<int64_t>(static_cast<int32_t>(dec_insn.vB));
Shih-wei Liao798366e2012-02-16 09:25:33 -08001362 break;
1363
1364 case Instruction::CONST_HIGH16:
1365 imm = static_cast<int64_t>(static_cast<int32_t>(
Elliott Hughesadb8c672012-03-06 16:49:32 -08001366 static_cast<uint32_t>(static_cast<uint16_t>(dec_insn.vB)) << 16));
Shih-wei Liao798366e2012-02-16 09:25:33 -08001367 break;
1368
1369 // 64-bit Immediate
1370 case Instruction::CONST_WIDE:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001371 imm = static_cast<int64_t>(dec_insn.vB_wide);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001372 break;
1373
1374 case Instruction::CONST_WIDE_HIGH16:
1375 imm = static_cast<int64_t>(
Elliott Hughesadb8c672012-03-06 16:49:32 -08001376 static_cast<uint64_t>(static_cast<uint16_t>(dec_insn.vB)) << 48);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001377 break;
1378
1379 // Unknown opcode for load constant (unreachable)
1380 default:
1381 LOG(FATAL) << "Unknown opcode for load constant: " << insn->Opcode();
1382 break;
1383 }
1384
1385 // Store the non-object register
1386 llvm::Type* imm_type = irb_.getJType(imm_jty, kAccurate);
1387 llvm::Constant* imm_value = llvm::ConstantInt::getSigned(imm_type, imm);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001388 EmitStoreDalvikReg(dec_insn.vA, imm_jty, kAccurate, imm_value);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001389
1390 // Store the object register if it is possible to be null.
1391 if (imm_jty == kInt && imm == 0) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001392 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, irb_.getJNull());
Shih-wei Liao798366e2012-02-16 09:25:33 -08001393 }
1394
Logan Chien70f94b42011-12-27 17:49:11 +08001395 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1396}
1397
1398
1399void MethodCompiler::EmitInsn_LoadConstantString(uint32_t dex_pc,
1400 Instruction const* insn) {
Logan Chienc3b4ba12012-01-16 19:52:53 +08001401
Elliott Hughesadb8c672012-03-06 16:49:32 -08001402 DecodedInstruction dec_insn(insn);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001403
Elliott Hughesadb8c672012-03-06 16:49:32 -08001404 uint32_t string_idx = dec_insn.vB;
Logan Chienc3b4ba12012-01-16 19:52:53 +08001405
1406 llvm::Value* string_field_addr = EmitLoadDexCacheStringFieldAddr(string_idx);
1407
TDYa1278ca10052012-05-05 19:57:06 -07001408 llvm::Value* string_addr = irb_.CreateLoad(string_field_addr, kTBAAJRuntime);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001409
1410 if (!compiler_->CanAssumeStringIsPresentInDexCache(dex_cache_, string_idx)) {
1411 llvm::BasicBlock* block_str_exist =
1412 CreateBasicBlockWithDexPC(dex_pc, "str_exist");
1413
1414 llvm::BasicBlock* block_str_resolve =
1415 CreateBasicBlockWithDexPC(dex_pc, "str_resolve");
1416
1417 // Test: Is the string resolved and in the dex cache?
1418 llvm::Value* equal_null = irb_.CreateICmpEQ(string_addr, irb_.getJNull());
1419
TDYa127ac7b5bb2012-05-11 13:17:49 -07001420 irb_.CreateCondBr(equal_null, block_str_resolve, block_str_exist, kUnlikely);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001421
1422 // String is resolved, go to next basic block.
1423 irb_.SetInsertPoint(block_str_exist);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001424 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, string_addr);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001425 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1426
1427 // String is not resolved yet, resolve it now.
1428 irb_.SetInsertPoint(block_str_resolve);
1429
1430 llvm::Function* runtime_func = irb_.GetRuntime(ResolveString);
1431
1432 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1433
1434 llvm::Value* string_idx_value = irb_.getInt32(string_idx);
1435
TDYa127c8dc1012012-04-19 07:03:33 -07001436 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001437
1438 string_addr = irb_.CreateCall2(runtime_func, method_object_addr,
1439 string_idx_value);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001440
1441 EmitGuard_ExceptionLandingPad(dex_pc);
1442 }
1443
1444 // Store the string object to the Dalvik register
Elliott Hughesadb8c672012-03-06 16:49:32 -08001445 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, string_addr);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001446
Logan Chien70f94b42011-12-27 17:49:11 +08001447 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1448}
1449
1450
Logan Chien27b30252012-01-14 03:43:35 +08001451llvm::Value* MethodCompiler::EmitLoadConstantClass(uint32_t dex_pc,
1452 uint32_t type_idx) {
1453 if (!compiler_->CanAccessTypeWithoutChecks(method_idx_, dex_cache_,
1454 *dex_file_, type_idx)) {
1455 llvm::Value* type_idx_value = irb_.getInt32(type_idx);
1456
1457 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1458
TDYa127706e9b62012-04-19 12:24:26 -07001459 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1460
Logan Chien27b30252012-01-14 03:43:35 +08001461 llvm::Function* runtime_func =
1462 irb_.GetRuntime(InitializeTypeAndVerifyAccess);
1463
TDYa127c8dc1012012-04-19 07:03:33 -07001464 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001465
Logan Chien27b30252012-01-14 03:43:35 +08001466 llvm::Value* type_object_addr =
TDYa127706e9b62012-04-19 12:24:26 -07001467 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
Logan Chien27b30252012-01-14 03:43:35 +08001468
1469 EmitGuard_ExceptionLandingPad(dex_pc);
1470
1471 return type_object_addr;
1472
1473 } else {
1474 // Try to load the class (type) object from the test cache.
1475 llvm::Value* type_field_addr =
1476 EmitLoadDexCacheResolvedTypeFieldAddr(type_idx);
1477
TDYa1278ca10052012-05-05 19:57:06 -07001478 llvm::Value* type_object_addr = irb_.CreateLoad(type_field_addr, kTBAAJRuntime);
Logan Chien27b30252012-01-14 03:43:35 +08001479
1480 if (compiler_->CanAssumeTypeIsPresentInDexCache(dex_cache_, type_idx)) {
1481 return type_object_addr;
1482 }
1483
1484 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
1485
1486 // Test whether class (type) object is in the dex cache or not
1487 llvm::Value* equal_null =
1488 irb_.CreateICmpEQ(type_object_addr, irb_.getJNull());
1489
1490 llvm::BasicBlock* block_cont =
1491 CreateBasicBlockWithDexPC(dex_pc, "cont");
1492
1493 llvm::BasicBlock* block_load_class =
1494 CreateBasicBlockWithDexPC(dex_pc, "load_class");
1495
TDYa127ac7b5bb2012-05-11 13:17:49 -07001496 irb_.CreateCondBr(equal_null, block_load_class, block_cont, kUnlikely);
Logan Chien27b30252012-01-14 03:43:35 +08001497
1498 // Failback routine to load the class object
1499 irb_.SetInsertPoint(block_load_class);
1500
1501 llvm::Function* runtime_func = irb_.GetRuntime(InitializeType);
1502
1503 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
1504
1505 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1506
TDYa127706e9b62012-04-19 12:24:26 -07001507 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1508
TDYa127c8dc1012012-04-19 07:03:33 -07001509 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001510
Logan Chien27b30252012-01-14 03:43:35 +08001511 llvm::Value* loaded_type_object_addr =
TDYa127706e9b62012-04-19 12:24:26 -07001512 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
Logan Chien27b30252012-01-14 03:43:35 +08001513
1514 EmitGuard_ExceptionLandingPad(dex_pc);
1515
1516 llvm::BasicBlock* block_after_load_class = irb_.GetInsertBlock();
1517
1518 irb_.CreateBr(block_cont);
1519
1520 // Now the class object must be loaded
1521 irb_.SetInsertPoint(block_cont);
1522
1523 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1524
1525 phi->addIncoming(type_object_addr, block_original);
1526 phi->addIncoming(loaded_type_object_addr, block_after_load_class);
1527
1528 return phi;
1529 }
1530}
1531
1532
Logan Chien70f94b42011-12-27 17:49:11 +08001533void MethodCompiler::EmitInsn_LoadConstantClass(uint32_t dex_pc,
1534 Instruction const* insn) {
Logan Chien27b30252012-01-14 03:43:35 +08001535
Elliott Hughesadb8c672012-03-06 16:49:32 -08001536 DecodedInstruction dec_insn(insn);
Logan Chien27b30252012-01-14 03:43:35 +08001537
Elliott Hughesadb8c672012-03-06 16:49:32 -08001538 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, dec_insn.vB);
1539 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, type_object_addr);
Logan Chien27b30252012-01-14 03:43:35 +08001540
Logan Chien70f94b42011-12-27 17:49:11 +08001541 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1542}
1543
1544
1545void MethodCompiler::EmitInsn_MonitorEnter(uint32_t dex_pc,
1546 Instruction const* insn) {
Logan Chien9e0dbe42012-01-13 12:11:37 +08001547
Elliott Hughesadb8c672012-03-06 16:49:32 -08001548 DecodedInstruction dec_insn(insn);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001549
1550 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001551 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001552
TDYa127cc1b4c32012-05-15 07:31:37 -07001553 if (!(method_info_.this_will_not_be_null && dec_insn.vA == method_info_.this_reg_idx)) {
1554 EmitGuard_NullPointerException(dex_pc, object_addr);
1555 }
Logan Chien9e0dbe42012-01-13 12:11:37 +08001556
TDYa127706e9b62012-04-19 12:24:26 -07001557 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1558
1559 irb_.CreateCall2(irb_.GetRuntime(LockObject), object_addr, thread_object_addr);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001560
Logan Chien70f94b42011-12-27 17:49:11 +08001561 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1562}
1563
1564
1565void MethodCompiler::EmitInsn_MonitorExit(uint32_t dex_pc,
1566 Instruction const* insn) {
Logan Chien9e0dbe42012-01-13 12:11:37 +08001567
Elliott Hughesadb8c672012-03-06 16:49:32 -08001568 DecodedInstruction dec_insn(insn);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001569
1570 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001571 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001572
TDYa127cc1b4c32012-05-15 07:31:37 -07001573 if (!(method_info_.this_will_not_be_null && dec_insn.vA == method_info_.this_reg_idx)) {
1574 EmitGuard_NullPointerException(dex_pc, object_addr);
1575 }
Logan Chien9e0dbe42012-01-13 12:11:37 +08001576
TDYa127c8dc1012012-04-19 07:03:33 -07001577 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001578
TDYa127706e9b62012-04-19 12:24:26 -07001579 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1580
1581 irb_.CreateCall2(irb_.GetRuntime(UnlockObject), object_addr, thread_object_addr);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001582 EmitGuard_ExceptionLandingPad(dex_pc);
1583
Logan Chien70f94b42011-12-27 17:49:11 +08001584 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1585}
1586
1587
1588void MethodCompiler::EmitInsn_CheckCast(uint32_t dex_pc,
1589 Instruction const* insn) {
Logan Chienfc880952012-01-15 23:53:10 +08001590
Elliott Hughesadb8c672012-03-06 16:49:32 -08001591 DecodedInstruction dec_insn(insn);
Logan Chienfc880952012-01-15 23:53:10 +08001592
1593 llvm::BasicBlock* block_test_class =
1594 CreateBasicBlockWithDexPC(dex_pc, "test_class");
1595
1596 llvm::BasicBlock* block_test_sub_class =
1597 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
1598
1599 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001600 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chienfc880952012-01-15 23:53:10 +08001601
1602 // Test: Is the reference equal to null? Act as no-op when it is null.
1603 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
1604
1605 irb_.CreateCondBr(equal_null,
1606 GetNextBasicBlock(dex_pc),
1607 block_test_class);
1608
1609 // Test: Is the object instantiated from the given class?
1610 irb_.SetInsertPoint(block_test_class);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001611 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, dec_insn.vB);
Logan Chienfc880952012-01-15 23:53:10 +08001612 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1613
1614 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
1615
1616 llvm::Value* object_type_field_addr =
1617 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
1618
1619 llvm::Value* object_type_object_addr =
TDYa127d3e24c22012-05-05 20:54:19 -07001620 irb_.CreateLoad(object_type_field_addr, kTBAAConstJObject);
Logan Chienfc880952012-01-15 23:53:10 +08001621
1622 llvm::Value* equal_class =
1623 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
1624
1625 irb_.CreateCondBr(equal_class,
1626 GetNextBasicBlock(dex_pc),
1627 block_test_sub_class);
1628
1629 // Test: Is the object instantiated from the subclass of the given class?
1630 irb_.SetInsertPoint(block_test_sub_class);
1631
TDYa127c8dc1012012-04-19 07:03:33 -07001632 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001633
Logan Chienfc880952012-01-15 23:53:10 +08001634 irb_.CreateCall2(irb_.GetRuntime(CheckCast),
1635 type_object_addr, object_type_object_addr);
1636
1637 EmitGuard_ExceptionLandingPad(dex_pc);
1638
Logan Chien70f94b42011-12-27 17:49:11 +08001639 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1640}
1641
1642
1643void MethodCompiler::EmitInsn_InstanceOf(uint32_t dex_pc,
1644 Instruction const* insn) {
Logan Chien68725e22012-01-15 22:25:34 +08001645
Elliott Hughesadb8c672012-03-06 16:49:32 -08001646 DecodedInstruction dec_insn(insn);
Logan Chien68725e22012-01-15 22:25:34 +08001647
1648 llvm::Constant* zero = irb_.getJInt(0);
1649 llvm::Constant* one = irb_.getJInt(1);
1650
1651 llvm::BasicBlock* block_nullp = CreateBasicBlockWithDexPC(dex_pc, "nullp");
1652
1653 llvm::BasicBlock* block_test_class =
1654 CreateBasicBlockWithDexPC(dex_pc, "test_class");
1655
1656 llvm::BasicBlock* block_class_equals =
1657 CreateBasicBlockWithDexPC(dex_pc, "class_eq");
1658
1659 llvm::BasicBlock* block_test_sub_class =
1660 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
1661
1662 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001663 EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
Logan Chien68725e22012-01-15 22:25:34 +08001664
1665 // Overview of the following code :
1666 // We check for null, if so, then false, otherwise check for class == . If so
1667 // then true, otherwise do callout slowpath.
1668 //
1669 // Test: Is the reference equal to null? Set 0 when it is null.
1670 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
1671
1672 irb_.CreateCondBr(equal_null, block_nullp, block_test_class);
1673
1674 irb_.SetInsertPoint(block_nullp);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001675 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, zero);
Logan Chien68725e22012-01-15 22:25:34 +08001676 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1677
1678 // Test: Is the object instantiated from the given class?
1679 irb_.SetInsertPoint(block_test_class);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001680 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, dec_insn.vC);
Logan Chien68725e22012-01-15 22:25:34 +08001681 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1682
1683 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
1684
1685 llvm::Value* object_type_field_addr =
1686 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
1687
1688 llvm::Value* object_type_object_addr =
TDYa127d3e24c22012-05-05 20:54:19 -07001689 irb_.CreateLoad(object_type_field_addr, kTBAAConstJObject);
Logan Chien68725e22012-01-15 22:25:34 +08001690
1691 llvm::Value* equal_class =
1692 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
1693
1694 irb_.CreateCondBr(equal_class, block_class_equals, block_test_sub_class);
1695
1696 irb_.SetInsertPoint(block_class_equals);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001697 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, one);
Logan Chien68725e22012-01-15 22:25:34 +08001698 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1699
1700 // Test: Is the object instantiated from the subclass of the given class?
1701 irb_.SetInsertPoint(block_test_sub_class);
1702
1703 llvm::Value* result =
1704 irb_.CreateCall2(irb_.GetRuntime(IsAssignable),
1705 type_object_addr, object_type_object_addr);
1706
Elliott Hughesadb8c672012-03-06 16:49:32 -08001707 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result);
Logan Chien68725e22012-01-15 22:25:34 +08001708
Logan Chien70f94b42011-12-27 17:49:11 +08001709 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1710}
1711
1712
Logan Chien61bb6142012-02-03 15:34:53 +08001713llvm::Value* MethodCompiler::EmitLoadArrayLength(llvm::Value* array) {
Logan Chien61bb6142012-02-03 15:34:53 +08001714 // Load array length
TDYa127ee1f59b2012-04-25 00:56:40 -07001715 return irb_.LoadFromObjectOffset(array,
1716 Array::LengthOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -07001717 irb_.getJIntTy(),
TDYa127d3e24c22012-05-05 20:54:19 -07001718 kTBAAConstJObject);
Logan Chien61bb6142012-02-03 15:34:53 +08001719}
1720
1721
Logan Chien70f94b42011-12-27 17:49:11 +08001722void MethodCompiler::EmitInsn_ArrayLength(uint32_t dex_pc,
1723 Instruction const* insn) {
Logan Chien61bb6142012-02-03 15:34:53 +08001724
Elliott Hughesadb8c672012-03-06 16:49:32 -08001725 DecodedInstruction dec_insn(insn);
Logan Chien61bb6142012-02-03 15:34:53 +08001726
1727 // Get the array object address
Elliott Hughesadb8c672012-03-06 16:49:32 -08001728 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
Logan Chien61bb6142012-02-03 15:34:53 +08001729 EmitGuard_NullPointerException(dex_pc, array_addr);
1730
1731 // Get the array length and store it to the register
1732 llvm::Value* array_len = EmitLoadArrayLength(array_addr);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001733 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, array_len);
Logan Chien61bb6142012-02-03 15:34:53 +08001734
Logan Chien70f94b42011-12-27 17:49:11 +08001735 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1736}
1737
1738
1739void MethodCompiler::EmitInsn_NewInstance(uint32_t dex_pc,
1740 Instruction const* insn) {
Logan Chien032bdad2012-01-16 09:59:23 +08001741
Elliott Hughesadb8c672012-03-06 16:49:32 -08001742 DecodedInstruction dec_insn(insn);
Logan Chien032bdad2012-01-16 09:59:23 +08001743
1744 llvm::Function* runtime_func;
Logan Chien1a032b12012-04-11 11:43:04 +08001745 if (compiler_->CanAccessInstantiableTypeWithoutChecks(
1746 method_idx_, dex_cache_, *dex_file_, dec_insn.vB)) {
Logan Chien032bdad2012-01-16 09:59:23 +08001747 runtime_func = irb_.GetRuntime(AllocObject);
1748 } else {
1749 runtime_func = irb_.GetRuntime(AllocObjectWithAccessCheck);
1750 }
1751
Elliott Hughesadb8c672012-03-06 16:49:32 -08001752 llvm::Constant* type_index_value = irb_.getInt32(dec_insn.vB);
Logan Chien032bdad2012-01-16 09:59:23 +08001753
1754 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1755
TDYa127da83d972012-04-18 00:21:49 -07001756 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1757
TDYa127c8dc1012012-04-19 07:03:33 -07001758 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001759
Logan Chien032bdad2012-01-16 09:59:23 +08001760 llvm::Value* object_addr =
TDYa127da83d972012-04-18 00:21:49 -07001761 irb_.CreateCall3(runtime_func, type_index_value, method_object_addr, thread_object_addr);
Logan Chien032bdad2012-01-16 09:59:23 +08001762
1763 EmitGuard_ExceptionLandingPad(dex_pc);
1764
Elliott Hughesadb8c672012-03-06 16:49:32 -08001765 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, object_addr);
Logan Chien032bdad2012-01-16 09:59:23 +08001766
Logan Chien70f94b42011-12-27 17:49:11 +08001767 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1768}
1769
1770
Logan Chiena2cc6a32012-01-16 10:38:41 +08001771llvm::Value* MethodCompiler::EmitAllocNewArray(uint32_t dex_pc,
1772 int32_t length,
1773 uint32_t type_idx,
1774 bool is_filled_new_array) {
1775 llvm::Function* runtime_func;
1776
1777 bool skip_access_check =
1778 compiler_->CanAccessTypeWithoutChecks(method_idx_, dex_cache_,
1779 *dex_file_, type_idx);
1780
TDYa127a849cb62012-04-01 05:59:34 -07001781 llvm::Value* array_length_value;
1782
Logan Chiena2cc6a32012-01-16 10:38:41 +08001783 if (is_filled_new_array) {
1784 runtime_func = skip_access_check ?
1785 irb_.GetRuntime(CheckAndAllocArray) :
1786 irb_.GetRuntime(CheckAndAllocArrayWithAccessCheck);
TDYa127a849cb62012-04-01 05:59:34 -07001787 array_length_value = irb_.getInt32(length);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001788 } else {
1789 runtime_func = skip_access_check ?
1790 irb_.GetRuntime(AllocArray) :
1791 irb_.GetRuntime(AllocArrayWithAccessCheck);
TDYa127a849cb62012-04-01 05:59:34 -07001792 array_length_value = EmitLoadDalvikReg(length, kInt, kAccurate);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001793 }
1794
1795 llvm::Constant* type_index_value = irb_.getInt32(type_idx);
1796
1797 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1798
TDYa127da83d972012-04-18 00:21:49 -07001799 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1800
TDYa127c8dc1012012-04-19 07:03:33 -07001801 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001802
Logan Chiena2cc6a32012-01-16 10:38:41 +08001803 llvm::Value* object_addr =
TDYa127da83d972012-04-18 00:21:49 -07001804 irb_.CreateCall4(runtime_func, type_index_value, method_object_addr,
1805 array_length_value, thread_object_addr);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001806
1807 EmitGuard_ExceptionLandingPad(dex_pc);
1808
1809 return object_addr;
1810}
1811
1812
Logan Chien70f94b42011-12-27 17:49:11 +08001813void MethodCompiler::EmitInsn_NewArray(uint32_t dex_pc,
1814 Instruction const* insn) {
Logan Chiena2cc6a32012-01-16 10:38:41 +08001815
Elliott Hughesadb8c672012-03-06 16:49:32 -08001816 DecodedInstruction dec_insn(insn);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001817
1818 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001819 EmitAllocNewArray(dex_pc, dec_insn.vB, dec_insn.vC, false);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001820
Elliott Hughesadb8c672012-03-06 16:49:32 -08001821 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, object_addr);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001822
Logan Chien70f94b42011-12-27 17:49:11 +08001823 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1824}
1825
1826
1827void MethodCompiler::EmitInsn_FilledNewArray(uint32_t dex_pc,
1828 Instruction const* insn,
1829 bool is_range) {
Logan Chiena85fb2f2012-01-16 12:52:56 +08001830
Elliott Hughesadb8c672012-03-06 16:49:32 -08001831 DecodedInstruction dec_insn(insn);
Logan Chiena85fb2f2012-01-16 12:52:56 +08001832
1833 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001834 EmitAllocNewArray(dex_pc, dec_insn.vA, dec_insn.vB, true);
Logan Chiena85fb2f2012-01-16 12:52:56 +08001835
Elliott Hughesadb8c672012-03-06 16:49:32 -08001836 if (dec_insn.vA > 0) {
Logan Chiendd361c92012-04-10 23:40:37 +08001837 // Resolve the element type
TDYa127183cf262012-04-11 07:53:21 -07001838 Class* klass = dex_cache_->GetResolvedType(dec_insn.vB)->GetComponentType();
Logan Chiendd361c92012-04-10 23:40:37 +08001839 // TODO: Avoid the usage of the dex_cache_. Try to figure out a better
1840 // way to distinguish [I and [L.
Logan Chiena85fb2f2012-01-16 12:52:56 +08001841 CHECK_NE(klass, static_cast<Class*>(NULL));
Logan Chiena85fb2f2012-01-16 12:52:56 +08001842
Logan Chiendd361c92012-04-10 23:40:37 +08001843 uint32_t alignment;
1844 llvm::Constant* elem_size;
1845 llvm::PointerType* field_type;
Logan Chiena85fb2f2012-01-16 12:52:56 +08001846
Logan Chiendd361c92012-04-10 23:40:37 +08001847 // NOTE: Currently filled-new-array only supports 'L', '[', and 'I'
1848 // as the element, thus we are only checking 2 cases: primitive int and
1849 // non-primitive type.
Logan Chiena85fb2f2012-01-16 12:52:56 +08001850 if (klass->IsPrimitiveInt()) {
Logan Chiendd361c92012-04-10 23:40:37 +08001851 alignment = sizeof(int32_t);
1852 elem_size = irb_.getPtrEquivInt(sizeof(int32_t));
Logan Chiena85fb2f2012-01-16 12:52:56 +08001853 field_type = irb_.getJIntTy()->getPointerTo();
1854 } else {
1855 CHECK(!klass->IsPrimitive());
Logan Chiendd361c92012-04-10 23:40:37 +08001856 alignment = irb_.getSizeOfPtrEquivInt();
1857 elem_size = irb_.getSizeOfPtrEquivIntValue();
Logan Chiena85fb2f2012-01-16 12:52:56 +08001858 field_type = irb_.getJObjectTy()->getPointerTo();
1859 }
1860
Logan Chiendd361c92012-04-10 23:40:37 +08001861 llvm::Value* data_field_offset =
1862 irb_.getPtrEquivInt(Array::DataOffset(alignment).Int32Value());
1863
1864 llvm::Value* data_field_addr =
1865 irb_.CreatePtrDisp(object_addr, data_field_offset, field_type);
1866
Logan Chiena85fb2f2012-01-16 12:52:56 +08001867 // TODO: Tune this code. Currently we are generating one instruction for
1868 // one element which may be very space consuming. Maybe changing to use
1869 // memcpy may help; however, since we can't guarantee that the alloca of
1870 // dalvik register are continuous, we can't perform such optimization yet.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001871 for (uint32_t i = 0; i < dec_insn.vA; ++i) {
Logan Chiena85fb2f2012-01-16 12:52:56 +08001872 int reg_index;
1873 if (is_range) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001874 reg_index = dec_insn.vC + i;
Logan Chiena85fb2f2012-01-16 12:52:56 +08001875 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001876 reg_index = dec_insn.arg[i];
Logan Chiena85fb2f2012-01-16 12:52:56 +08001877 }
1878
1879 llvm::Value* reg_value;
1880 if (klass->IsPrimitiveInt()) {
1881 reg_value = EmitLoadDalvikReg(reg_index, kInt, kAccurate);
1882 } else {
1883 reg_value = EmitLoadDalvikReg(reg_index, kObject, kAccurate);
1884 }
1885
TDYa127706e7db2012-05-06 00:05:33 -07001886 irb_.CreateStore(reg_value, data_field_addr, kTBAAHeapArray);
Logan Chiena85fb2f2012-01-16 12:52:56 +08001887
Logan Chiendd361c92012-04-10 23:40:37 +08001888 data_field_addr =
1889 irb_.CreatePtrDisp(data_field_addr, elem_size, field_type);
Logan Chiena85fb2f2012-01-16 12:52:56 +08001890 }
1891 }
1892
1893 EmitStoreDalvikRetValReg(kObject, kAccurate, object_addr);
1894
Logan Chien70f94b42011-12-27 17:49:11 +08001895 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1896}
1897
1898
1899void MethodCompiler::EmitInsn_FillArrayData(uint32_t dex_pc,
1900 Instruction const* insn) {
Logan Chiene58b6582012-01-16 17:13:13 +08001901
Elliott Hughesadb8c672012-03-06 16:49:32 -08001902 DecodedInstruction dec_insn(insn);
Logan Chiene58b6582012-01-16 17:13:13 +08001903
1904 // Read the payload
Logan Chiene58b6582012-01-16 17:13:13 +08001905 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
Elliott Hughesadb8c672012-03-06 16:49:32 -08001906 static_cast<int32_t>(dec_insn.vB);
Logan Chiene58b6582012-01-16 17:13:13 +08001907
Logan Chien19c350a2012-05-01 19:21:32 +08001908 const Instruction::ArrayDataPayload* payload =
1909 reinterpret_cast<const Instruction::ArrayDataPayload*>(
1910 code_item_->insns_ + payload_offset);
Logan Chiene58b6582012-01-16 17:13:13 +08001911
Logan Chien86f50672012-04-24 13:08:45 +08001912 // Load array object
Elliott Hughesadb8c672012-03-06 16:49:32 -08001913 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiene58b6582012-01-16 17:13:13 +08001914
Logan Chien86f50672012-04-24 13:08:45 +08001915 if (payload->element_count == 0) {
1916 // When the number of the elements in the payload is zero, we don't have
1917 // to copy any numbers. However, we should check whether the array object
1918 // address is equal to null or not.
1919 EmitGuard_NullPointerException(dex_pc, array_addr);
1920 } else {
1921 // To save the code size, we are going to call the runtime function to
1922 // copy the content from DexFile.
Logan Chiene58b6582012-01-16 17:13:13 +08001923
Logan Chien86f50672012-04-24 13:08:45 +08001924 // NOTE: We will check for the NullPointerException in the runtime.
Logan Chiene58b6582012-01-16 17:13:13 +08001925
Logan Chien86f50672012-04-24 13:08:45 +08001926 llvm::Function* runtime_func = irb_.GetRuntime(FillArrayData);
Logan Chiene58b6582012-01-16 17:13:13 +08001927
Logan Chien86f50672012-04-24 13:08:45 +08001928 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
Logan Chiene58b6582012-01-16 17:13:13 +08001929
Logan Chien86f50672012-04-24 13:08:45 +08001930 EmitUpdateDexPC(dex_pc);
Logan Chiene58b6582012-01-16 17:13:13 +08001931
Logan Chien86f50672012-04-24 13:08:45 +08001932 irb_.CreateCall4(runtime_func,
1933 method_object_addr, irb_.getInt32(dex_pc),
1934 array_addr, irb_.getInt32(payload_offset));
Logan Chiene58b6582012-01-16 17:13:13 +08001935
Logan Chien86f50672012-04-24 13:08:45 +08001936 EmitGuard_ExceptionLandingPad(dex_pc);
Logan Chiene58b6582012-01-16 17:13:13 +08001937 }
1938
Logan Chien70f94b42011-12-27 17:49:11 +08001939 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1940}
1941
1942
1943void MethodCompiler::EmitInsn_UnconditionalBranch(uint32_t dex_pc,
1944 Instruction const* insn) {
Logan Chiena466c162011-12-27 17:55:46 +08001945
Elliott Hughesadb8c672012-03-06 16:49:32 -08001946 DecodedInstruction dec_insn(insn);
Logan Chiena466c162011-12-27 17:55:46 +08001947
Elliott Hughesadb8c672012-03-06 16:49:32 -08001948 int32_t branch_offset = dec_insn.vA;
Logan Chiena466c162011-12-27 17:55:46 +08001949
Logan Chiena466c162011-12-27 17:55:46 +08001950 irb_.CreateBr(GetBasicBlock(dex_pc + branch_offset));
Logan Chien70f94b42011-12-27 17:49:11 +08001951}
1952
1953
1954void MethodCompiler::EmitInsn_PackedSwitch(uint32_t dex_pc,
1955 Instruction const* insn) {
Logan Chien7a89b6d2011-12-27 17:56:56 +08001956
Elliott Hughesadb8c672012-03-06 16:49:32 -08001957 DecodedInstruction dec_insn(insn);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001958
Logan Chien7a89b6d2011-12-27 17:56:56 +08001959 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
Elliott Hughesadb8c672012-03-06 16:49:32 -08001960 static_cast<int32_t>(dec_insn.vB);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001961
Logan Chien19c350a2012-05-01 19:21:32 +08001962 const Instruction::PackedSwitchPayload* payload =
1963 reinterpret_cast<const Instruction::PackedSwitchPayload*>(
1964 code_item_->insns_ + payload_offset);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001965
Elliott Hughesadb8c672012-03-06 16:49:32 -08001966 llvm::Value* value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001967
1968 llvm::SwitchInst* sw =
Logan Chien19c350a2012-05-01 19:21:32 +08001969 irb_.CreateSwitch(value, GetNextBasicBlock(dex_pc), payload->case_count);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001970
Logan Chien19c350a2012-05-01 19:21:32 +08001971 for (uint16_t i = 0; i < payload->case_count; ++i) {
1972 sw->addCase(irb_.getInt32(payload->first_key + i),
1973 GetBasicBlock(dex_pc + payload->targets[i]));
Logan Chien7a89b6d2011-12-27 17:56:56 +08001974 }
Logan Chien70f94b42011-12-27 17:49:11 +08001975}
1976
1977
1978void MethodCompiler::EmitInsn_SparseSwitch(uint32_t dex_pc,
1979 Instruction const* insn) {
Logan Chien7a89b6d2011-12-27 17:56:56 +08001980
Elliott Hughesadb8c672012-03-06 16:49:32 -08001981 DecodedInstruction dec_insn(insn);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001982
Logan Chien7a89b6d2011-12-27 17:56:56 +08001983 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
Elliott Hughesadb8c672012-03-06 16:49:32 -08001984 static_cast<int32_t>(dec_insn.vB);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001985
Logan Chien19c350a2012-05-01 19:21:32 +08001986 const Instruction::SparseSwitchPayload* payload =
1987 reinterpret_cast<const Instruction::SparseSwitchPayload*>(
1988 code_item_->insns_ + payload_offset);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001989
Logan Chien19c350a2012-05-01 19:21:32 +08001990 const int32_t* keys = payload->GetKeys();
1991 const int32_t* targets = payload->GetTargets();
Logan Chien7a89b6d2011-12-27 17:56:56 +08001992
Elliott Hughesadb8c672012-03-06 16:49:32 -08001993 llvm::Value* value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001994
1995 llvm::SwitchInst* sw =
Logan Chien19c350a2012-05-01 19:21:32 +08001996 irb_.CreateSwitch(value, GetNextBasicBlock(dex_pc), payload->case_count);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001997
Logan Chien19c350a2012-05-01 19:21:32 +08001998 for (size_t i = 0; i < payload->case_count; ++i) {
Logan Chien7a89b6d2011-12-27 17:56:56 +08001999 sw->addCase(irb_.getInt32(keys[i]), GetBasicBlock(dex_pc + targets[i]));
2000 }
Logan Chien70f94b42011-12-27 17:49:11 +08002001}
2002
2003
2004void MethodCompiler::EmitInsn_FPCompare(uint32_t dex_pc,
2005 Instruction const* insn,
2006 JType fp_jty,
2007 bool gt_bias) {
Logan Chien2c37e8e2011-12-27 17:58:46 +08002008
Elliott Hughesadb8c672012-03-06 16:49:32 -08002009 DecodedInstruction dec_insn(insn);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002010
2011 DCHECK(fp_jty == kFloat || fp_jty == kDouble) << "JType: " << fp_jty;
2012
Elliott Hughesadb8c672012-03-06 16:49:32 -08002013 llvm::Value* src1_value = EmitLoadDalvikReg(dec_insn.vB, fp_jty, kAccurate);
2014 llvm::Value* src2_value = EmitLoadDalvikReg(dec_insn.vC, fp_jty, kAccurate);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002015
2016 llvm::Value* cmp_eq = irb_.CreateFCmpOEQ(src1_value, src2_value);
2017 llvm::Value* cmp_lt;
2018
2019 if (gt_bias) {
2020 cmp_lt = irb_.CreateFCmpOLT(src1_value, src2_value);
2021 } else {
2022 cmp_lt = irb_.CreateFCmpULT(src1_value, src2_value);
2023 }
2024
2025 llvm::Value* result = EmitCompareResultSelection(cmp_eq, cmp_lt);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002026 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002027
Logan Chien70f94b42011-12-27 17:49:11 +08002028 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2029}
2030
2031
2032void MethodCompiler::EmitInsn_LongCompare(uint32_t dex_pc,
2033 Instruction const* insn) {
Logan Chien2c37e8e2011-12-27 17:58:46 +08002034
Elliott Hughesadb8c672012-03-06 16:49:32 -08002035 DecodedInstruction dec_insn(insn);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002036
Elliott Hughesadb8c672012-03-06 16:49:32 -08002037 llvm::Value* src1_value = EmitLoadDalvikReg(dec_insn.vB, kLong, kAccurate);
2038 llvm::Value* src2_value = EmitLoadDalvikReg(dec_insn.vC, kLong, kAccurate);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002039
2040 llvm::Value* cmp_eq = irb_.CreateICmpEQ(src1_value, src2_value);
2041 llvm::Value* cmp_lt = irb_.CreateICmpSLT(src1_value, src2_value);
2042
2043 llvm::Value* result = EmitCompareResultSelection(cmp_eq, cmp_lt);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002044 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002045
Logan Chien70f94b42011-12-27 17:49:11 +08002046 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2047}
2048
2049
Logan Chien2c37e8e2011-12-27 17:58:46 +08002050llvm::Value* MethodCompiler::EmitCompareResultSelection(llvm::Value* cmp_eq,
2051 llvm::Value* cmp_lt) {
2052
2053 llvm::Constant* zero = irb_.getJInt(0);
2054 llvm::Constant* pos1 = irb_.getJInt(1);
2055 llvm::Constant* neg1 = irb_.getJInt(-1);
2056
2057 llvm::Value* result_lt = irb_.CreateSelect(cmp_lt, neg1, pos1);
2058 llvm::Value* result_eq = irb_.CreateSelect(cmp_eq, zero, result_lt);
2059
2060 return result_eq;
2061}
2062
2063
Logan Chien70f94b42011-12-27 17:49:11 +08002064void MethodCompiler::EmitInsn_BinaryConditionalBranch(uint32_t dex_pc,
2065 Instruction const* insn,
2066 CondBranchKind cond) {
Logan Chiena78e3c82011-12-27 17:59:35 +08002067
Elliott Hughesadb8c672012-03-06 16:49:32 -08002068 DecodedInstruction dec_insn(insn);
Logan Chiena78e3c82011-12-27 17:59:35 +08002069
Elliott Hughesadb8c672012-03-06 16:49:32 -08002070 int8_t src1_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vA);
2071 int8_t src2_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vB);
Logan Chiena78e3c82011-12-27 17:59:35 +08002072
2073 DCHECK_NE(kRegUnknown, src1_reg_cat);
2074 DCHECK_NE(kRegUnknown, src2_reg_cat);
2075 DCHECK_NE(kRegCat2, src1_reg_cat);
2076 DCHECK_NE(kRegCat2, src2_reg_cat);
2077
Elliott Hughesadb8c672012-03-06 16:49:32 -08002078 int32_t branch_offset = dec_insn.vC;
Logan Chiena78e3c82011-12-27 17:59:35 +08002079
Logan Chiena78e3c82011-12-27 17:59:35 +08002080 llvm::Value* src1_value;
2081 llvm::Value* src2_value;
2082
TDYa1278e9b4492012-04-24 15:50:27 -07002083 if (src1_reg_cat == kRegZero && src2_reg_cat == kRegZero) {
2084 src1_value = irb_.getInt32(0);
2085 src2_value = irb_.getInt32(0);
2086 } else if (src1_reg_cat != kRegZero && src2_reg_cat != kRegZero) {
Logan Chiena78e3c82011-12-27 17:59:35 +08002087 CHECK_EQ(src1_reg_cat, src2_reg_cat);
2088
2089 if (src1_reg_cat == kRegCat1nr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002090 src1_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
2091 src2_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002092 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002093 src1_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
2094 src2_value = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002095 }
2096 } else {
2097 DCHECK(src1_reg_cat == kRegZero ||
2098 src2_reg_cat == kRegZero);
2099
2100 if (src1_reg_cat == kRegZero) {
2101 if (src2_reg_cat == kRegCat1nr) {
2102 src1_value = irb_.getJInt(0);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002103 src2_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002104 } else {
2105 src1_value = irb_.getJNull();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002106 src2_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002107 }
2108 } else { // src2_reg_cat == kRegZero
2109 if (src2_reg_cat == kRegCat1nr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002110 src1_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002111 src2_value = irb_.getJInt(0);
2112 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002113 src1_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002114 src2_value = irb_.getJNull();
2115 }
2116 }
2117 }
2118
2119 llvm::Value* cond_value =
2120 EmitConditionResult(src1_value, src2_value, cond);
2121
2122 irb_.CreateCondBr(cond_value,
2123 GetBasicBlock(dex_pc + branch_offset),
2124 GetNextBasicBlock(dex_pc));
Logan Chien70f94b42011-12-27 17:49:11 +08002125}
2126
2127
2128void MethodCompiler::EmitInsn_UnaryConditionalBranch(uint32_t dex_pc,
2129 Instruction const* insn,
2130 CondBranchKind cond) {
Logan Chiena78e3c82011-12-27 17:59:35 +08002131
Elliott Hughesadb8c672012-03-06 16:49:32 -08002132 DecodedInstruction dec_insn(insn);
Logan Chiena78e3c82011-12-27 17:59:35 +08002133
Elliott Hughesadb8c672012-03-06 16:49:32 -08002134 int8_t src_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vA);
Logan Chiena78e3c82011-12-27 17:59:35 +08002135
2136 DCHECK_NE(kRegUnknown, src_reg_cat);
2137 DCHECK_NE(kRegCat2, src_reg_cat);
2138
Elliott Hughesadb8c672012-03-06 16:49:32 -08002139 int32_t branch_offset = dec_insn.vB;
Logan Chiena78e3c82011-12-27 17:59:35 +08002140
Logan Chiena78e3c82011-12-27 17:59:35 +08002141 llvm::Value* src1_value;
2142 llvm::Value* src2_value;
2143
TDYa1278e9b4492012-04-24 15:50:27 -07002144 if (src_reg_cat == kRegZero) {
2145 src1_value = irb_.getInt32(0);
2146 src2_value = irb_.getInt32(0);
2147 } else if (src_reg_cat == kRegCat1nr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002148 src1_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002149 src2_value = irb_.getInt32(0);
2150 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002151 src1_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002152 src2_value = irb_.getJNull();
2153 }
2154
2155 llvm::Value* cond_value =
2156 EmitConditionResult(src1_value, src2_value, cond);
2157
2158 irb_.CreateCondBr(cond_value,
2159 GetBasicBlock(dex_pc + branch_offset),
2160 GetNextBasicBlock(dex_pc));
2161}
2162
TDYa1271d7e5102012-05-13 09:27:05 -07002163InferredRegCategoryMap const* MethodCompiler::GetInferredRegCategoryMap() {
Logan Chiendd361c92012-04-10 23:40:37 +08002164 Compiler::MethodReference mref(dex_file_, method_idx_);
2165
2166 InferredRegCategoryMap const* map =
Ian Rogers776ac1f2012-04-13 23:36:36 -07002167 verifier::MethodVerifier::GetInferredRegCategoryMap(mref);
Logan Chiendd361c92012-04-10 23:40:37 +08002168
Logan Chiena78e3c82011-12-27 17:59:35 +08002169 CHECK_NE(map, static_cast<InferredRegCategoryMap*>(NULL));
2170
TDYa1271d7e5102012-05-13 09:27:05 -07002171 return map;
2172}
2173
2174RegCategory MethodCompiler::GetInferredRegCategory(uint32_t dex_pc,
2175 uint16_t reg_idx) {
2176 InferredRegCategoryMap const* map = GetInferredRegCategoryMap();
2177
Logan Chiena78e3c82011-12-27 17:59:35 +08002178 return map->GetRegCategory(dex_pc, reg_idx);
2179}
2180
TDYa1271d7e5102012-05-13 09:27:05 -07002181bool MethodCompiler::IsRegCanBeObject(uint16_t reg_idx) {
2182 InferredRegCategoryMap const* map = GetInferredRegCategoryMap();
2183
2184 return map->IsRegCanBeObject(reg_idx);
2185}
2186
Logan Chiena78e3c82011-12-27 17:59:35 +08002187
2188llvm::Value* MethodCompiler::EmitConditionResult(llvm::Value* lhs,
2189 llvm::Value* rhs,
2190 CondBranchKind cond) {
2191 switch (cond) {
2192 case kCondBranch_EQ:
2193 return irb_.CreateICmpEQ(lhs, rhs);
2194
2195 case kCondBranch_NE:
2196 return irb_.CreateICmpNE(lhs, rhs);
2197
2198 case kCondBranch_LT:
2199 return irb_.CreateICmpSLT(lhs, rhs);
2200
2201 case kCondBranch_GE:
2202 return irb_.CreateICmpSGE(lhs, rhs);
2203
2204 case kCondBranch_GT:
2205 return irb_.CreateICmpSGT(lhs, rhs);
2206
2207 case kCondBranch_LE:
2208 return irb_.CreateICmpSLE(lhs, rhs);
2209
2210 default: // Unreachable
2211 LOG(FATAL) << "Unknown conditional branch kind: " << cond;
2212 return NULL;
2213 }
Logan Chien70f94b42011-12-27 17:49:11 +08002214}
2215
TDYa12783bb6622012-04-17 02:20:34 -07002216void MethodCompiler::EmitMarkGCCard(llvm::Value* value, llvm::Value* target_addr) {
2217 // Using runtime support, let the target can override by InlineAssembly.
2218 llvm::Function* runtime_func = irb_.GetRuntime(MarkGCCard);
2219
2220 irb_.CreateCall2(runtime_func, value, target_addr);
2221}
Logan Chien70f94b42011-12-27 17:49:11 +08002222
Logan Chiene27fdbb2012-01-02 23:27:26 +08002223void
2224MethodCompiler::EmitGuard_ArrayIndexOutOfBoundsException(uint32_t dex_pc,
2225 llvm::Value* array,
2226 llvm::Value* index) {
2227 llvm::Value* array_len = EmitLoadArrayLength(array);
2228
2229 llvm::Value* cmp = irb_.CreateICmpUGE(index, array_len);
2230
2231 llvm::BasicBlock* block_exception =
2232 CreateBasicBlockWithDexPC(dex_pc, "overflow");
2233
2234 llvm::BasicBlock* block_continue =
2235 CreateBasicBlockWithDexPC(dex_pc, "cont");
2236
TDYa127ac7b5bb2012-05-11 13:17:49 -07002237 irb_.CreateCondBr(cmp, block_exception, block_continue, kUnlikely);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002238
2239 irb_.SetInsertPoint(block_exception);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002240
TDYa127c8dc1012012-04-19 07:03:33 -07002241 EmitUpdateDexPC(dex_pc);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002242 irb_.CreateCall2(irb_.GetRuntime(ThrowIndexOutOfBounds), index, array_len);
2243 EmitBranchExceptionLandingPad(dex_pc);
2244
2245 irb_.SetInsertPoint(block_continue);
2246}
2247
2248
2249void MethodCompiler::EmitGuard_ArrayException(uint32_t dex_pc,
2250 llvm::Value* array,
2251 llvm::Value* index) {
2252 EmitGuard_NullPointerException(dex_pc, array);
2253 EmitGuard_ArrayIndexOutOfBoundsException(dex_pc, array, index);
2254}
2255
2256
2257// Emit Array GetElementPtr
2258llvm::Value* MethodCompiler::EmitArrayGEP(llvm::Value* array_addr,
2259 llvm::Value* index_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08002260 llvm::Type* elem_type,
2261 JType elem_jty) {
2262
2263 int data_offset;
2264 if (elem_jty == kLong || elem_jty == kDouble ||
2265 (elem_jty == kObject && sizeof(uint64_t) == sizeof(Object*))) {
2266 data_offset = Array::DataOffset(sizeof(int64_t)).Int32Value();
2267 } else {
2268 data_offset = Array::DataOffset(sizeof(int32_t)).Int32Value();
2269 }
Logan Chiene27fdbb2012-01-02 23:27:26 +08002270
2271 llvm::Constant* data_offset_value =
Ian Rogers04ec04e2012-02-28 16:15:33 -08002272 irb_.getPtrEquivInt(data_offset);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002273
2274 llvm::Value* array_data_addr =
2275 irb_.CreatePtrDisp(array_addr, data_offset_value,
2276 elem_type->getPointerTo());
2277
2278 return irb_.CreateGEP(array_data_addr, index_value);
2279}
2280
2281
Logan Chien70f94b42011-12-27 17:49:11 +08002282void MethodCompiler::EmitInsn_AGet(uint32_t dex_pc,
2283 Instruction const* insn,
2284 JType elem_jty) {
Logan Chiene27fdbb2012-01-02 23:27:26 +08002285
Elliott Hughesadb8c672012-03-06 16:49:32 -08002286 DecodedInstruction dec_insn(insn);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002287
Elliott Hughesadb8c672012-03-06 16:49:32 -08002288 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
2289 llvm::Value* index_value = EmitLoadDalvikReg(dec_insn.vC, kInt, kAccurate);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002290
2291 EmitGuard_ArrayException(dex_pc, array_addr, index_value);
2292
2293 llvm::Type* elem_type = irb_.getJType(elem_jty, kArray);
2294
2295 llvm::Value* array_elem_addr =
Ian Rogers04ec04e2012-02-28 16:15:33 -08002296 EmitArrayGEP(array_addr, index_value, elem_type, elem_jty);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002297
TDYa127706e7db2012-05-06 00:05:33 -07002298 llvm::Value* array_elem_value = irb_.CreateLoad(array_elem_addr, kTBAAHeapArray, elem_jty);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002299
Elliott Hughesadb8c672012-03-06 16:49:32 -08002300 EmitStoreDalvikReg(dec_insn.vA, elem_jty, kArray, array_elem_value);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002301
Logan Chien70f94b42011-12-27 17:49:11 +08002302 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2303}
2304
2305
2306void MethodCompiler::EmitInsn_APut(uint32_t dex_pc,
2307 Instruction const* insn,
2308 JType elem_jty) {
Logan Chien8dabb432012-01-02 23:29:32 +08002309
Elliott Hughesadb8c672012-03-06 16:49:32 -08002310 DecodedInstruction dec_insn(insn);
Logan Chien8dabb432012-01-02 23:29:32 +08002311
Elliott Hughesadb8c672012-03-06 16:49:32 -08002312 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
2313 llvm::Value* index_value = EmitLoadDalvikReg(dec_insn.vC, kInt, kAccurate);
Logan Chien8dabb432012-01-02 23:29:32 +08002314
2315 EmitGuard_ArrayException(dex_pc, array_addr, index_value);
2316
2317 llvm::Type* elem_type = irb_.getJType(elem_jty, kArray);
2318
2319 llvm::Value* array_elem_addr =
Ian Rogers04ec04e2012-02-28 16:15:33 -08002320 EmitArrayGEP(array_addr, index_value, elem_type, elem_jty);
Logan Chien8dabb432012-01-02 23:29:32 +08002321
Elliott Hughesadb8c672012-03-06 16:49:32 -08002322 llvm::Value* new_value = EmitLoadDalvikReg(dec_insn.vA, elem_jty, kArray);
Logan Chien8dabb432012-01-02 23:29:32 +08002323
TDYa12783bb6622012-04-17 02:20:34 -07002324 if (elem_jty == kObject) { // If put an object, check the type, and mark GC card table.
TDYa1271b86d072012-04-05 17:38:56 -07002325 llvm::Function* runtime_func = irb_.GetRuntime(CheckPutArrayElement);
2326
2327 irb_.CreateCall2(runtime_func, new_value, array_addr);
2328
2329 EmitGuard_ExceptionLandingPad(dex_pc);
TDYa12783bb6622012-04-17 02:20:34 -07002330
2331 EmitMarkGCCard(new_value, array_addr);
TDYa1271b86d072012-04-05 17:38:56 -07002332 }
2333
TDYa127706e7db2012-05-06 00:05:33 -07002334 irb_.CreateStore(new_value, array_elem_addr, kTBAAHeapArray, elem_jty);
Logan Chien8dabb432012-01-02 23:29:32 +08002335
Logan Chien70f94b42011-12-27 17:49:11 +08002336 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2337}
2338
2339
2340void MethodCompiler::EmitInsn_IGet(uint32_t dex_pc,
2341 Instruction const* insn,
2342 JType field_jty) {
Logan Chien48f1d2a2012-01-02 22:49:53 +08002343
Elliott Hughesadb8c672012-03-06 16:49:32 -08002344 DecodedInstruction dec_insn(insn);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002345
Elliott Hughesadb8c672012-03-06 16:49:32 -08002346 uint32_t reg_idx = dec_insn.vB;
2347 uint32_t field_idx = dec_insn.vC;
Logan Chien48f1d2a2012-01-02 22:49:53 +08002348
Logan Chien48f1d2a2012-01-02 22:49:53 +08002349 llvm::Value* object_addr = EmitLoadDalvikReg(reg_idx, kObject, kAccurate);
2350
TDYa127cc1b4c32012-05-15 07:31:37 -07002351 if (!(method_info_.this_will_not_be_null && reg_idx == method_info_.this_reg_idx)) {
2352 EmitGuard_NullPointerException(dex_pc, object_addr);
2353 }
Logan Chien48f1d2a2012-01-02 22:49:53 +08002354
2355 llvm::Value* field_value;
2356
Logan Chien933abf82012-04-11 12:24:31 +08002357 int field_offset;
2358 bool is_volatile;
2359 bool is_fast_path = compiler_->ComputeInstanceFieldInfo(
2360 field_idx, oat_compilation_unit_, field_offset, is_volatile, false);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002361
Logan Chien933abf82012-04-11 12:24:31 +08002362 if (!is_fast_path) {
Logan Chien48f1d2a2012-01-02 22:49:53 +08002363 llvm::Function* runtime_func;
2364
2365 if (field_jty == kObject) {
Logan Chien3b2b2e72012-03-06 16:11:45 +08002366 runtime_func = irb_.GetRuntime(GetObjectInstance);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002367 } else if (field_jty == kLong || field_jty == kDouble) {
Logan Chien3b2b2e72012-03-06 16:11:45 +08002368 runtime_func = irb_.GetRuntime(Get64Instance);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002369 } else {
Logan Chien3b2b2e72012-03-06 16:11:45 +08002370 runtime_func = irb_.GetRuntime(Get32Instance);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002371 }
2372
2373 llvm::ConstantInt* field_idx_value = irb_.getInt32(field_idx);
2374
2375 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2376
TDYa127c8dc1012012-04-19 07:03:33 -07002377 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002378
Logan Chien3b2b2e72012-03-06 16:11:45 +08002379 field_value = irb_.CreateCall3(runtime_func, field_idx_value,
2380 method_object_addr, object_addr);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002381
2382 EmitGuard_ExceptionLandingPad(dex_pc);
2383
2384 } else {
Logan Chien933abf82012-04-11 12:24:31 +08002385 DCHECK_GE(field_offset, 0);
2386
Logan Chien48f1d2a2012-01-02 22:49:53 +08002387 llvm::PointerType* field_type =
2388 irb_.getJType(field_jty, kField)->getPointerTo();
2389
Logan Chien933abf82012-04-11 12:24:31 +08002390 llvm::ConstantInt* field_offset_value = irb_.getPtrEquivInt(field_offset);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002391
2392 llvm::Value* field_addr =
Logan Chien933abf82012-04-11 12:24:31 +08002393 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002394
Logan Chien933abf82012-04-11 12:24:31 +08002395 // TODO: Check is_volatile. We need to generate atomic load instruction
2396 // when is_volatile is true.
TDYa127706e7db2012-05-06 00:05:33 -07002397 field_value = irb_.CreateLoad(field_addr, kTBAAHeapInstance, field_jty);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002398 }
2399
Elliott Hughesadb8c672012-03-06 16:49:32 -08002400 EmitStoreDalvikReg(dec_insn.vA, field_jty, kField, field_value);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002401
Logan Chien70f94b42011-12-27 17:49:11 +08002402 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2403}
2404
2405
2406void MethodCompiler::EmitInsn_IPut(uint32_t dex_pc,
2407 Instruction const* insn,
2408 JType field_jty) {
Logan Chiendd6aa872012-01-03 16:06:32 +08002409
Elliott Hughesadb8c672012-03-06 16:49:32 -08002410 DecodedInstruction dec_insn(insn);
Logan Chiendd6aa872012-01-03 16:06:32 +08002411
Elliott Hughesadb8c672012-03-06 16:49:32 -08002412 uint32_t reg_idx = dec_insn.vB;
2413 uint32_t field_idx = dec_insn.vC;
Logan Chiendd6aa872012-01-03 16:06:32 +08002414
Logan Chiendd6aa872012-01-03 16:06:32 +08002415 llvm::Value* object_addr = EmitLoadDalvikReg(reg_idx, kObject, kAccurate);
2416
TDYa127cc1b4c32012-05-15 07:31:37 -07002417 if (!(method_info_.this_will_not_be_null && reg_idx == method_info_.this_reg_idx)) {
2418 EmitGuard_NullPointerException(dex_pc, object_addr);
2419 }
Logan Chiendd6aa872012-01-03 16:06:32 +08002420
Elliott Hughesadb8c672012-03-06 16:49:32 -08002421 llvm::Value* new_value = EmitLoadDalvikReg(dec_insn.vA, field_jty, kField);
Logan Chiendd6aa872012-01-03 16:06:32 +08002422
Logan Chien933abf82012-04-11 12:24:31 +08002423 int field_offset;
2424 bool is_volatile;
2425 bool is_fast_path = compiler_->ComputeInstanceFieldInfo(
2426 field_idx, oat_compilation_unit_, field_offset, is_volatile, true);
Logan Chiendd6aa872012-01-03 16:06:32 +08002427
Logan Chien933abf82012-04-11 12:24:31 +08002428 if (!is_fast_path) {
Logan Chiendd6aa872012-01-03 16:06:32 +08002429 llvm::Function* runtime_func;
2430
2431 if (field_jty == kObject) {
2432 runtime_func = irb_.GetRuntime(SetObjectInstance);
2433 } else if (field_jty == kLong || field_jty == kDouble) {
2434 runtime_func = irb_.GetRuntime(Set64Instance);
2435 } else {
2436 runtime_func = irb_.GetRuntime(Set32Instance);
2437 }
2438
2439 llvm::Value* field_idx_value = irb_.getInt32(field_idx);
2440
2441 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2442
TDYa127c8dc1012012-04-19 07:03:33 -07002443 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002444
Logan Chien3b2b2e72012-03-06 16:11:45 +08002445 irb_.CreateCall4(runtime_func, field_idx_value,
2446 method_object_addr, object_addr, new_value);
Logan Chiendd6aa872012-01-03 16:06:32 +08002447
2448 EmitGuard_ExceptionLandingPad(dex_pc);
2449
2450 } else {
Logan Chien933abf82012-04-11 12:24:31 +08002451 DCHECK_GE(field_offset, 0);
2452
Logan Chiendd6aa872012-01-03 16:06:32 +08002453 llvm::PointerType* field_type =
2454 irb_.getJType(field_jty, kField)->getPointerTo();
2455
Logan Chien933abf82012-04-11 12:24:31 +08002456 llvm::Value* field_offset_value = irb_.getPtrEquivInt(field_offset);
Logan Chiendd6aa872012-01-03 16:06:32 +08002457
2458 llvm::Value* field_addr =
Logan Chien933abf82012-04-11 12:24:31 +08002459 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
Logan Chiendd6aa872012-01-03 16:06:32 +08002460
Logan Chien933abf82012-04-11 12:24:31 +08002461 // TODO: Check is_volatile. We need to generate atomic store instruction
2462 // when is_volatile is true.
TDYa127706e7db2012-05-06 00:05:33 -07002463 irb_.CreateStore(new_value, field_addr, kTBAAHeapInstance, field_jty);
TDYa12783bb6622012-04-17 02:20:34 -07002464
2465 if (field_jty == kObject) { // If put an object, mark the GC card table.
2466 EmitMarkGCCard(new_value, object_addr);
2467 }
Logan Chiendd6aa872012-01-03 16:06:32 +08002468 }
2469
Logan Chien70f94b42011-12-27 17:49:11 +08002470 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2471}
2472
2473
Logan Chien438c4b62012-01-17 16:06:00 +08002474llvm::Value* MethodCompiler::EmitLoadStaticStorage(uint32_t dex_pc,
2475 uint32_t type_idx) {
2476 llvm::BasicBlock* block_load_static =
2477 CreateBasicBlockWithDexPC(dex_pc, "load_static");
2478
2479 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
2480
2481 // Load static storage from dex cache
2482 llvm::Value* storage_field_addr =
2483 EmitLoadDexCacheStaticStorageFieldAddr(type_idx);
2484
TDYa1278ca10052012-05-05 19:57:06 -07002485 llvm::Value* storage_object_addr = irb_.CreateLoad(storage_field_addr, kTBAAJRuntime);
Logan Chien438c4b62012-01-17 16:06:00 +08002486
2487 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
2488
2489 // Test: Is the static storage of this class initialized?
2490 llvm::Value* equal_null =
2491 irb_.CreateICmpEQ(storage_object_addr, irb_.getJNull());
2492
TDYa127ac7b5bb2012-05-11 13:17:49 -07002493 irb_.CreateCondBr(equal_null, block_load_static, block_cont, kUnlikely);
Logan Chien438c4b62012-01-17 16:06:00 +08002494
2495 // Failback routine to load the class object
2496 irb_.SetInsertPoint(block_load_static);
2497
2498 llvm::Function* runtime_func =
2499 irb_.GetRuntime(InitializeStaticStorage);
2500
2501 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
2502
2503 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2504
TDYa127706e9b62012-04-19 12:24:26 -07002505 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
2506
TDYa127c8dc1012012-04-19 07:03:33 -07002507 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002508
Logan Chien438c4b62012-01-17 16:06:00 +08002509 llvm::Value* loaded_storage_object_addr =
TDYa127706e9b62012-04-19 12:24:26 -07002510 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
Logan Chien438c4b62012-01-17 16:06:00 +08002511
2512 EmitGuard_ExceptionLandingPad(dex_pc);
2513
2514 llvm::BasicBlock* block_after_load_static = irb_.GetInsertBlock();
2515
2516 irb_.CreateBr(block_cont);
2517
2518 // Now the class object must be loaded
2519 irb_.SetInsertPoint(block_cont);
2520
2521 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
2522
2523 phi->addIncoming(storage_object_addr, block_original);
2524 phi->addIncoming(loaded_storage_object_addr, block_after_load_static);
2525
2526 return phi;
2527}
2528
2529
Logan Chien70f94b42011-12-27 17:49:11 +08002530void MethodCompiler::EmitInsn_SGet(uint32_t dex_pc,
2531 Instruction const* insn,
2532 JType field_jty) {
Logan Chien438c4b62012-01-17 16:06:00 +08002533
Elliott Hughesadb8c672012-03-06 16:49:32 -08002534 DecodedInstruction dec_insn(insn);
Logan Chien438c4b62012-01-17 16:06:00 +08002535
Logan Chien933abf82012-04-11 12:24:31 +08002536 uint32_t field_idx = dec_insn.vB;
Logan Chien438c4b62012-01-17 16:06:00 +08002537
Logan Chien933abf82012-04-11 12:24:31 +08002538 int field_offset;
2539 int ssb_index;
2540 bool is_referrers_class;
2541 bool is_volatile;
2542
2543 bool is_fast_path = compiler_->ComputeStaticFieldInfo(
2544 field_idx, oat_compilation_unit_, field_offset, ssb_index,
2545 is_referrers_class, is_volatile, false);
Logan Chien438c4b62012-01-17 16:06:00 +08002546
2547 llvm::Value* static_field_value;
2548
Logan Chien933abf82012-04-11 12:24:31 +08002549 if (!is_fast_path) {
Logan Chien438c4b62012-01-17 16:06:00 +08002550 llvm::Function* runtime_func;
2551
2552 if (field_jty == kObject) {
2553 runtime_func = irb_.GetRuntime(GetObjectStatic);
2554 } else if (field_jty == kLong || field_jty == kDouble) {
2555 runtime_func = irb_.GetRuntime(Get64Static);
2556 } else {
2557 runtime_func = irb_.GetRuntime(Get32Static);
2558 }
2559
Elliott Hughesadb8c672012-03-06 16:49:32 -08002560 llvm::Constant* field_idx_value = irb_.getInt32(dec_insn.vB);
Logan Chien438c4b62012-01-17 16:06:00 +08002561
2562 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2563
TDYa127c8dc1012012-04-19 07:03:33 -07002564 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002565
Logan Chien438c4b62012-01-17 16:06:00 +08002566 static_field_value =
2567 irb_.CreateCall2(runtime_func, field_idx_value, method_object_addr);
2568
2569 EmitGuard_ExceptionLandingPad(dex_pc);
2570
2571 } else {
Logan Chien933abf82012-04-11 12:24:31 +08002572 DCHECK_GE(field_offset, 0);
Logan Chien438c4b62012-01-17 16:06:00 +08002573
Logan Chien933abf82012-04-11 12:24:31 +08002574 llvm::Value* static_storage_addr = NULL;
2575
2576 if (is_referrers_class) {
2577 // Fast path, static storage base is this method's class
2578 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2579
TDYa127ee1f59b2012-04-25 00:56:40 -07002580 static_storage_addr =
2581 irb_.LoadFromObjectOffset(method_object_addr,
2582 Method::DeclaringClassOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -07002583 irb_.getJObjectTy(),
TDYa127d3e24c22012-05-05 20:54:19 -07002584 kTBAAConstJObject);
Logan Chien933abf82012-04-11 12:24:31 +08002585 } else {
2586 // Medium path, static storage base in a different class which
2587 // requires checks that the other class is initialized
2588 DCHECK_GE(ssb_index, 0);
2589 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
2590 }
2591
2592 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
Logan Chien438c4b62012-01-17 16:06:00 +08002593
2594 llvm::Value* static_field_addr =
2595 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
2596 irb_.getJType(field_jty, kField)->getPointerTo());
2597
Logan Chien933abf82012-04-11 12:24:31 +08002598 // TODO: Check is_volatile. We need to generate atomic load instruction
2599 // when is_volatile is true.
TDYa127706e7db2012-05-06 00:05:33 -07002600 static_field_value = irb_.CreateLoad(static_field_addr, kTBAAHeapStatic, field_jty);
Logan Chien438c4b62012-01-17 16:06:00 +08002601 }
2602
Elliott Hughesadb8c672012-03-06 16:49:32 -08002603 EmitStoreDalvikReg(dec_insn.vA, field_jty, kField, static_field_value);
Logan Chien438c4b62012-01-17 16:06:00 +08002604
Logan Chien70f94b42011-12-27 17:49:11 +08002605 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2606}
2607
2608
2609void MethodCompiler::EmitInsn_SPut(uint32_t dex_pc,
2610 Instruction const* insn,
2611 JType field_jty) {
Logan Chien14179c82012-01-17 17:06:34 +08002612
Elliott Hughesadb8c672012-03-06 16:49:32 -08002613 DecodedInstruction dec_insn(insn);
Logan Chien14179c82012-01-17 17:06:34 +08002614
Logan Chien933abf82012-04-11 12:24:31 +08002615 uint32_t field_idx = dec_insn.vB;
Logan Chien14179c82012-01-17 17:06:34 +08002616
Elliott Hughesadb8c672012-03-06 16:49:32 -08002617 llvm::Value* new_value = EmitLoadDalvikReg(dec_insn.vA, field_jty, kField);
Logan Chien14179c82012-01-17 17:06:34 +08002618
Logan Chien933abf82012-04-11 12:24:31 +08002619 int field_offset;
2620 int ssb_index;
2621 bool is_referrers_class;
2622 bool is_volatile;
2623
2624 bool is_fast_path = compiler_->ComputeStaticFieldInfo(
2625 field_idx, oat_compilation_unit_, field_offset, ssb_index,
2626 is_referrers_class, is_volatile, true);
2627
2628 if (!is_fast_path) {
Logan Chien14179c82012-01-17 17:06:34 +08002629 llvm::Function* runtime_func;
2630
2631 if (field_jty == kObject) {
2632 runtime_func = irb_.GetRuntime(SetObjectStatic);
2633 } else if (field_jty == kLong || field_jty == kDouble) {
2634 runtime_func = irb_.GetRuntime(Set64Static);
2635 } else {
2636 runtime_func = irb_.GetRuntime(Set32Static);
2637 }
2638
Elliott Hughesadb8c672012-03-06 16:49:32 -08002639 llvm::Constant* field_idx_value = irb_.getInt32(dec_insn.vB);
Logan Chien14179c82012-01-17 17:06:34 +08002640
2641 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2642
TDYa127c8dc1012012-04-19 07:03:33 -07002643 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002644
Logan Chien14179c82012-01-17 17:06:34 +08002645 irb_.CreateCall3(runtime_func, field_idx_value,
2646 method_object_addr, new_value);
2647
2648 EmitGuard_ExceptionLandingPad(dex_pc);
2649
2650 } else {
Logan Chien933abf82012-04-11 12:24:31 +08002651 DCHECK_GE(field_offset, 0);
Logan Chien14179c82012-01-17 17:06:34 +08002652
Logan Chien933abf82012-04-11 12:24:31 +08002653 llvm::Value* static_storage_addr = NULL;
2654
2655 if (is_referrers_class) {
2656 // Fast path, static storage base is this method's class
2657 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2658
TDYa127ee1f59b2012-04-25 00:56:40 -07002659 static_storage_addr =
2660 irb_.LoadFromObjectOffset(method_object_addr,
2661 Method::DeclaringClassOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -07002662 irb_.getJObjectTy(),
TDYa127d3e24c22012-05-05 20:54:19 -07002663 kTBAAConstJObject);
Logan Chien933abf82012-04-11 12:24:31 +08002664 } else {
2665 // Medium path, static storage base in a different class which
2666 // requires checks that the other class is initialized
2667 DCHECK_GE(ssb_index, 0);
2668 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
2669 }
2670
2671 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
Logan Chien14179c82012-01-17 17:06:34 +08002672
2673 llvm::Value* static_field_addr =
2674 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
2675 irb_.getJType(field_jty, kField)->getPointerTo());
2676
Logan Chien933abf82012-04-11 12:24:31 +08002677 // TODO: Check is_volatile. We need to generate atomic store instruction
2678 // when is_volatile is true.
TDYa127706e7db2012-05-06 00:05:33 -07002679 irb_.CreateStore(new_value, static_field_addr, kTBAAHeapStatic, field_jty);
TDYa12783bb6622012-04-17 02:20:34 -07002680
2681 if (field_jty == kObject) { // If put an object, mark the GC card table.
2682 EmitMarkGCCard(new_value, static_storage_addr);
2683 }
Logan Chien14179c82012-01-17 17:06:34 +08002684 }
2685
Logan Chien70f94b42011-12-27 17:49:11 +08002686 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2687}
2688
2689
Logan Chien1a121b92012-02-15 22:23:42 +08002690void MethodCompiler::
2691EmitLoadActualParameters(std::vector<llvm::Value*>& args,
2692 uint32_t callee_method_idx,
Elliott Hughesadb8c672012-03-06 16:49:32 -08002693 DecodedInstruction const& dec_insn,
Logan Chien7e7fabc2012-04-10 18:59:11 +08002694 InvokeArgFmt arg_fmt,
Logan Chien1a121b92012-02-15 22:23:42 +08002695 bool is_static) {
2696
2697 // Get method signature
2698 DexFile::MethodId const& method_id =
2699 dex_file_->GetMethodId(callee_method_idx);
2700
Logan Chien8faf8022012-02-24 12:25:29 +08002701 uint32_t shorty_size;
Logan Chien1a121b92012-02-15 22:23:42 +08002702 char const* shorty = dex_file_->GetMethodShorty(method_id, &shorty_size);
Logan Chien8faf8022012-02-24 12:25:29 +08002703 CHECK_GE(shorty_size, 1u);
Logan Chien1a121b92012-02-15 22:23:42 +08002704
2705 // Load argument values according to the shorty (without "this")
2706 uint16_t reg_count = 0;
2707
2708 if (!is_static) {
2709 ++reg_count; // skip the "this" pointer
2710 }
2711
Logan Chien7e7fabc2012-04-10 18:59:11 +08002712 bool is_range = (arg_fmt == kArgRange);
2713
Logan Chien8faf8022012-02-24 12:25:29 +08002714 for (uint32_t i = 1; i < shorty_size; ++i) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002715 uint32_t reg_idx = (is_range) ? (dec_insn.vC + reg_count)
2716 : (dec_insn.arg[reg_count]);
Logan Chien1a121b92012-02-15 22:23:42 +08002717
2718 args.push_back(EmitLoadDalvikReg(reg_idx, shorty[i], kAccurate));
2719
2720 ++reg_count;
2721 if (shorty[i] == 'J' || shorty[i] == 'D') {
2722 // Wide types, such as long and double, are using a pair of registers
2723 // to store the value, so we have to increase arg_reg again.
2724 ++reg_count;
2725 }
2726 }
2727
Elliott Hughesadb8c672012-03-06 16:49:32 -08002728 DCHECK_EQ(reg_count, dec_insn.vA)
Logan Chien1a121b92012-02-15 22:23:42 +08002729 << "Actual argument mismatch for callee: "
2730 << PrettyMethod(callee_method_idx, *dex_file_);
2731}
2732
TDYa1270b686e52012-04-09 22:43:35 -07002733llvm::Value* MethodCompiler::EmitFixStub(llvm::Value* callee_method_object_addr,
2734 uint32_t method_idx,
2735 bool is_static) {
2736 // TODO: Remove this after we solve the link and trampoline related problems.
2737 llvm::Value* code_addr = irb_.CreateCall(irb_.GetRuntime(FixStub), callee_method_object_addr);
2738
2739 llvm::FunctionType* method_type = GetFunctionType(method_idx, is_static);
2740
2741 return irb_.CreatePointerCast(code_addr, method_type->getPointerTo());
TDYa12785321912012-04-01 15:24:56 -07002742}
Logan Chien1a121b92012-02-15 22:23:42 +08002743
Shih-wei Liao399ed3f2012-03-08 01:27:04 -08002744
Logan Chien7e7fabc2012-04-10 18:59:11 +08002745void MethodCompiler::EmitInsn_Invoke(uint32_t dex_pc,
2746 Instruction const* insn,
2747 InvokeType invoke_type,
2748 InvokeArgFmt arg_fmt) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002749 DecodedInstruction dec_insn(insn);
Logan Chien46fbb412012-02-15 22:29:08 +08002750
Logan Chien61c65dc2012-02-29 03:22:30 +08002751 bool is_static = (invoke_type == kStatic);
Logan Chien7e7fabc2012-04-10 18:59:11 +08002752 uint32_t callee_method_idx = dec_insn.vB;
Logan Chien61c65dc2012-02-29 03:22:30 +08002753
Logan Chien7e7fabc2012-04-10 18:59:11 +08002754 // Compute invoke related information for compiler decision
2755 int vtable_idx = -1;
Logan Chien92ad16d2012-03-18 05:48:55 +08002756 uintptr_t direct_code = 0; // Currently unused
Logan Chienfca64372012-04-23 14:57:01 +08002757 uintptr_t direct_method = 0;
Logan Chien61c65dc2012-02-29 03:22:30 +08002758 bool is_fast_path = compiler_->
Logan Chien7e7fabc2012-04-10 18:59:11 +08002759 ComputeInvokeInfo(callee_method_idx, oat_compilation_unit_,
Logan Chien92ad16d2012-03-18 05:48:55 +08002760 invoke_type, vtable_idx, direct_code, direct_method);
Logan Chien61c65dc2012-02-29 03:22:30 +08002761
Logan Chien7e7fabc2012-04-10 18:59:11 +08002762 // Load *this* actual parameter
Logan Chien1a121b92012-02-15 22:23:42 +08002763 llvm::Value* this_addr = NULL;
2764
2765 if (!is_static) {
2766 // Test: Is *this* parameter equal to null?
TDYa127cc1b4c32012-05-15 07:31:37 -07002767 uint32_t reg_idx = (arg_fmt == kArgReg) ? dec_insn.arg[0] : (dec_insn.vC + 0);
2768 this_addr = EmitLoadDalvikReg(reg_idx, kObject, kAccurate);
Logan Chien7e7fabc2012-04-10 18:59:11 +08002769
TDYa127cc1b4c32012-05-15 07:31:37 -07002770 if (!(method_info_.this_will_not_be_null && reg_idx == method_info_.this_reg_idx)) {
2771 EmitGuard_NullPointerException(dex_pc, this_addr);
2772 }
Logan Chien1a121b92012-02-15 22:23:42 +08002773 }
2774
Logan Chien7e7fabc2012-04-10 18:59:11 +08002775 // Load the method object
TDYa1274e42a592012-04-10 20:13:54 -07002776 llvm::Value* callee_method_object_addr = NULL;
Logan Chien7e7fabc2012-04-10 18:59:11 +08002777
2778 if (!is_fast_path) {
TDYa1274e42a592012-04-10 20:13:54 -07002779 callee_method_object_addr =
Logan Chien7e7fabc2012-04-10 18:59:11 +08002780 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx, invoke_type,
2781 this_addr, dex_pc, is_fast_path);
2782 } else {
2783 switch (invoke_type) {
2784 case kStatic:
2785 case kDirect:
Logan Chienfca64372012-04-23 14:57:01 +08002786 if (direct_method != 0u &&
2787 direct_method != static_cast<uintptr_t>(-1)) {
2788 callee_method_object_addr =
TDYa12717826bf2012-04-24 01:15:10 -07002789 irb_.CreateIntToPtr(irb_.getPtrEquivInt(direct_method),
2790 irb_.getJObjectTy());
Logan Chienfca64372012-04-23 14:57:01 +08002791 } else {
2792 callee_method_object_addr =
2793 EmitLoadSDCalleeMethodObjectAddr(callee_method_idx);
2794 }
Logan Chien7e7fabc2012-04-10 18:59:11 +08002795 break;
2796
2797 case kVirtual:
2798 DCHECK(vtable_idx != -1);
TDYa1274e42a592012-04-10 20:13:54 -07002799 callee_method_object_addr =
Logan Chien7e7fabc2012-04-10 18:59:11 +08002800 EmitLoadVirtualCalleeMethodObjectAddr(vtable_idx, this_addr);
2801 break;
2802
2803 case kSuper:
2804 LOG(FATAL) << "invoke-super should be promoted to invoke-direct in "
2805 "the fast path.";
2806 break;
2807
2808 case kInterface:
TDYa1274e42a592012-04-10 20:13:54 -07002809 callee_method_object_addr =
Logan Chien7e7fabc2012-04-10 18:59:11 +08002810 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx,
2811 invoke_type, this_addr,
2812 dex_pc, is_fast_path);
2813 break;
2814 }
2815 }
2816
Logan Chien7e7fabc2012-04-10 18:59:11 +08002817 llvm::Value* code_addr =
TDYa127ee1f59b2012-04-25 00:56:40 -07002818 irb_.LoadFromObjectOffset(callee_method_object_addr,
2819 Method::GetCodeOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -07002820 GetFunctionType(callee_method_idx, is_static)->getPointerTo(),
TDYa1278ca10052012-05-05 19:57:06 -07002821 kTBAAJRuntime);
TDYa12785321912012-04-01 15:24:56 -07002822
Logan Chien1a121b92012-02-15 22:23:42 +08002823 // Load the actual parameter
2824 std::vector<llvm::Value*> args;
2825
2826 args.push_back(callee_method_object_addr); // method object for callee
2827
2828 if (!is_static) {
Logan Chien7e7fabc2012-04-10 18:59:11 +08002829 DCHECK(this_addr != NULL);
Logan Chien1a121b92012-02-15 22:23:42 +08002830 args.push_back(this_addr); // "this" object for callee
2831 }
2832
2833 EmitLoadActualParameters(args, callee_method_idx, dec_insn,
Logan Chien7e7fabc2012-04-10 18:59:11 +08002834 arg_fmt, is_static);
Logan Chien1a121b92012-02-15 22:23:42 +08002835
TDYa1275bb86012012-04-11 05:57:28 -07002836#if 0
Logan Chien8dfcbea2012-02-17 18:50:32 +08002837 // Invoke callee
TDYa127c8dc1012012-04-19 07:03:33 -07002838 EmitUpdateDexPC(dex_pc);
Logan Chien1a121b92012-02-15 22:23:42 +08002839 llvm::Value* retval = irb_.CreateCall(code_addr, args);
2840 EmitGuard_ExceptionLandingPad(dex_pc);
2841
Logan Chien7e7fabc2012-04-10 18:59:11 +08002842 uint32_t callee_access_flags = is_static ? kAccStatic : 0;
2843 UniquePtr<OatCompilationUnit> callee_oat_compilation_unit(
2844 oat_compilation_unit_->GetCallee(callee_method_idx, callee_access_flags));
2845
2846 char ret_shorty = callee_oat_compilation_unit->GetShorty()[0];
Shih-wei Liao90d50992012-02-19 03:32:05 -08002847 if (ret_shorty != 'V') {
Logan Chien1a121b92012-02-15 22:23:42 +08002848 EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval);
2849 }
TDYa1275bb86012012-04-11 05:57:28 -07002850#else
2851 uint32_t callee_access_flags = is_static ? kAccStatic : 0;
2852 UniquePtr<OatCompilationUnit> callee_oat_compilation_unit(
2853 oat_compilation_unit_->GetCallee(callee_method_idx, callee_access_flags));
2854
2855 char ret_shorty = callee_oat_compilation_unit->GetShorty()[0];
2856
2857
TDYa127c8dc1012012-04-19 07:03:33 -07002858 EmitUpdateDexPC(dex_pc);
TDYa1275bb86012012-04-11 05:57:28 -07002859
2860
2861 llvm::BasicBlock* block_normal = CreateBasicBlockWithDexPC(dex_pc, "normal");
TDYa127ce154722012-04-21 16:43:29 -07002862 llvm::BasicBlock* block_stub = CreateBasicBlockWithDexPC(dex_pc, "stub");
TDYa1275bb86012012-04-11 05:57:28 -07002863 llvm::BasicBlock* block_continue = CreateBasicBlockWithDexPC(dex_pc, "cont");
2864
TDYa1275bb86012012-04-11 05:57:28 -07002865 llvm::Value* code_addr_int = irb_.CreatePtrToInt(code_addr, irb_.getPtrEquivIntTy());
TDYa127ce154722012-04-21 16:43:29 -07002866 llvm::Value* max_stub_int = irb_.getPtrEquivInt(special_stub::kMaxSpecialStub);
2867 llvm::Value* is_stub = irb_.CreateICmpULT(code_addr_int, max_stub_int);
TDYa127ac7b5bb2012-05-11 13:17:49 -07002868 irb_.CreateCondBr(is_stub, block_stub, block_normal, kUnlikely);
TDYa1275bb86012012-04-11 05:57:28 -07002869
2870
2871 irb_.SetInsertPoint(block_normal);
2872 {
2873 // Invoke callee
TDYa127ce154722012-04-21 16:43:29 -07002874 llvm::Value* retval = irb_.CreateCall(code_addr, args);
TDYa1275bb86012012-04-11 05:57:28 -07002875 if (ret_shorty != 'V') {
TDYa127ce154722012-04-21 16:43:29 -07002876 EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval);
TDYa1275bb86012012-04-11 05:57:28 -07002877 }
2878 }
2879 irb_.CreateBr(block_continue);
2880
2881
TDYa127ce154722012-04-21 16:43:29 -07002882 irb_.SetInsertPoint(block_stub);
TDYa1275bb86012012-04-11 05:57:28 -07002883 {
TDYa127ce154722012-04-21 16:43:29 -07002884 { // lazy link
2885 // TODO: Remove this after we solve the link problem.
2886 llvm::BasicBlock* block_proxy_stub = CreateBasicBlockWithDexPC(dex_pc, "proxy");
2887 llvm::BasicBlock* block_link = CreateBasicBlockWithDexPC(dex_pc, "link");
2888
TDYa127ac7b5bb2012-05-11 13:17:49 -07002889 irb_.CreateCondBr(irb_.CreateIsNull(code_addr), block_link, block_proxy_stub, kUnlikely);
TDYa127ce154722012-04-21 16:43:29 -07002890
2891
2892 irb_.SetInsertPoint(block_link);
2893 code_addr = EmitFixStub(callee_method_object_addr, callee_method_idx, is_static);
2894
2895 EmitGuard_ExceptionLandingPad(dex_pc);
2896
2897 llvm::Value* retval = irb_.CreateCall(code_addr, args);
2898 if (ret_shorty != 'V') {
2899 EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval);
2900 }
2901 irb_.CreateBr(block_continue);
2902
2903
2904 irb_.SetInsertPoint(block_proxy_stub);
TDYa1275bb86012012-04-11 05:57:28 -07002905 }
TDYa127ce154722012-04-21 16:43:29 -07002906 { // proxy stub
2907 llvm::Value* temp_space_addr;
2908 if (ret_shorty != 'V') {
2909 temp_space_addr = irb_.CreateAlloca(irb_.getJValueTy());
2910 args.push_back(temp_space_addr);
2911 }
2912 // TODO: Remove this after we solve the proxy trampoline calling convention problem.
2913 irb_.CreateCall(irb_.GetRuntime(ProxyInvokeHandler), args);
2914 if (ret_shorty != 'V') {
Shih-wei Liaoe6a7adc2012-05-09 02:50:08 -07002915 llvm::Type* accurate_ret_type = irb_.getJType(ret_shorty, kAccurate);
TDYa127ce154722012-04-21 16:43:29 -07002916 llvm::Value* result_addr =
2917 irb_.CreateBitCast(temp_space_addr, accurate_ret_type->getPointerTo());
TDYa127aba61122012-05-04 18:28:36 -07002918 llvm::Value* retval = irb_.CreateLoad(result_addr, kTBAAStackTemp);
TDYa127ce154722012-04-21 16:43:29 -07002919 EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval);
2920 }
TDYa1275bb86012012-04-11 05:57:28 -07002921 }
2922 }
2923 irb_.CreateBr(block_continue);
2924
2925
2926 irb_.SetInsertPoint(block_continue);
2927
TDYa1275bb86012012-04-11 05:57:28 -07002928 EmitGuard_ExceptionLandingPad(dex_pc);
2929#endif
Logan Chien1a121b92012-02-15 22:23:42 +08002930
Logan Chien70f94b42011-12-27 17:49:11 +08002931 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2932}
2933
2934
Logan Chien7e7fabc2012-04-10 18:59:11 +08002935llvm::Value* MethodCompiler::
2936EmitLoadSDCalleeMethodObjectAddr(uint32_t callee_method_idx) {
2937 llvm::Value* callee_method_object_field_addr =
2938 EmitLoadDexCacheResolvedMethodFieldAddr(callee_method_idx);
Logan Chien7caf37e2012-02-03 22:56:04 +08002939
TDYa1278ca10052012-05-05 19:57:06 -07002940 return irb_.CreateLoad(callee_method_object_field_addr, kTBAAJRuntime);
Logan Chien7e7fabc2012-04-10 18:59:11 +08002941}
Logan Chien7caf37e2012-02-03 22:56:04 +08002942
Logan Chien7caf37e2012-02-03 22:56:04 +08002943
Logan Chien7e7fabc2012-04-10 18:59:11 +08002944llvm::Value* MethodCompiler::
2945EmitLoadVirtualCalleeMethodObjectAddr(int vtable_idx,
2946 llvm::Value* this_addr) {
2947 // Load class object of *this* pointer
TDYa127ee1f59b2012-04-25 00:56:40 -07002948 llvm::Value* class_object_addr =
2949 irb_.LoadFromObjectOffset(this_addr,
2950 Object::ClassOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -07002951 irb_.getJObjectTy(),
TDYa127d3e24c22012-05-05 20:54:19 -07002952 kTBAAConstJObject);
Logan Chien7caf37e2012-02-03 22:56:04 +08002953
Logan Chien7e7fabc2012-04-10 18:59:11 +08002954 // Load vtable address
TDYa127ee1f59b2012-04-25 00:56:40 -07002955 llvm::Value* vtable_addr =
2956 irb_.LoadFromObjectOffset(class_object_addr,
2957 Class::VTableOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -07002958 irb_.getJObjectTy(),
TDYa127d3e24c22012-05-05 20:54:19 -07002959 kTBAAConstJObject);
Logan Chien7e7fabc2012-04-10 18:59:11 +08002960
2961 // Load callee method object
2962 llvm::Value* vtable_idx_value =
2963 irb_.getPtrEquivInt(static_cast<uint64_t>(vtable_idx));
2964
2965 llvm::Value* method_field_addr =
2966 EmitArrayGEP(vtable_addr, vtable_idx_value, irb_.getJObjectTy(), kObject);
2967
TDYa127d3e24c22012-05-05 20:54:19 -07002968 return irb_.CreateLoad(method_field_addr, kTBAAConstJObject);
Logan Chien7e7fabc2012-04-10 18:59:11 +08002969}
2970
2971
2972llvm::Value* MethodCompiler::
2973EmitCallRuntimeForCalleeMethodObjectAddr(uint32_t callee_method_idx,
2974 InvokeType invoke_type,
2975 llvm::Value* this_addr,
2976 uint32_t dex_pc,
2977 bool is_fast_path) {
2978
2979 llvm::Function* runtime_func = NULL;
2980
2981 switch (invoke_type) {
2982 case kStatic:
2983 runtime_func = irb_.GetRuntime(FindStaticMethodWithAccessCheck);
2984 break;
2985
2986 case kDirect:
2987 runtime_func = irb_.GetRuntime(FindDirectMethodWithAccessCheck);
2988 break;
2989
2990 case kVirtual:
2991 runtime_func = irb_.GetRuntime(FindVirtualMethodWithAccessCheck);
2992 break;
2993
2994 case kSuper:
2995 runtime_func = irb_.GetRuntime(FindSuperMethodWithAccessCheck);
2996 break;
2997
2998 case kInterface:
2999 if (is_fast_path) {
3000 runtime_func = irb_.GetRuntime(FindInterfaceMethod);
3001 } else {
3002 runtime_func = irb_.GetRuntime(FindInterfaceMethodWithAccessCheck);
3003 }
3004 break;
3005 }
Logan Chien7caf37e2012-02-03 22:56:04 +08003006
3007 llvm::Value* callee_method_idx_value = irb_.getInt32(callee_method_idx);
3008
Logan Chien7e7fabc2012-04-10 18:59:11 +08003009 if (this_addr == NULL) {
3010 DCHECK_EQ(invoke_type, kStatic);
3011 this_addr = irb_.getJNull();
3012 }
3013
3014 llvm::Value* caller_method_object_addr = EmitLoadMethodObjectAddr();
3015
TDYa127da83d972012-04-18 00:21:49 -07003016 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
3017
TDYa127c8dc1012012-04-19 07:03:33 -07003018 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08003019
TDYa1270b686e52012-04-09 22:43:35 -07003020 llvm::Value* callee_method_object_addr =
TDYa127da83d972012-04-18 00:21:49 -07003021 irb_.CreateCall4(runtime_func,
Logan Chien7e7fabc2012-04-10 18:59:11 +08003022 callee_method_idx_value,
3023 this_addr,
TDYa127da83d972012-04-18 00:21:49 -07003024 caller_method_object_addr,
3025 thread_object_addr);
Logan Chien7caf37e2012-02-03 22:56:04 +08003026
3027 EmitGuard_ExceptionLandingPad(dex_pc);
3028
Logan Chien7e7fabc2012-04-10 18:59:11 +08003029 return callee_method_object_addr;
Logan Chien70f94b42011-12-27 17:49:11 +08003030}
3031
3032
3033void MethodCompiler::EmitInsn_Neg(uint32_t dex_pc,
3034 Instruction const* insn,
3035 JType op_jty) {
Logan Chien1b5685f2011-12-27 18:01:14 +08003036
Elliott Hughesadb8c672012-03-06 16:49:32 -08003037 DecodedInstruction dec_insn(insn);
Logan Chien1b5685f2011-12-27 18:01:14 +08003038
3039 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3040
Elliott Hughesadb8c672012-03-06 16:49:32 -08003041 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chien1b5685f2011-12-27 18:01:14 +08003042 llvm::Value* result_value = irb_.CreateNeg(src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003043 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chien1b5685f2011-12-27 18:01:14 +08003044
Logan Chien70f94b42011-12-27 17:49:11 +08003045 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3046}
3047
3048
3049void MethodCompiler::EmitInsn_Not(uint32_t dex_pc,
3050 Instruction const* insn,
3051 JType op_jty) {
Logan Chiene53750d2011-12-27 18:02:27 +08003052
Elliott Hughesadb8c672012-03-06 16:49:32 -08003053 DecodedInstruction dec_insn(insn);
Logan Chiene53750d2011-12-27 18:02:27 +08003054
3055 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3056
Elliott Hughesadb8c672012-03-06 16:49:32 -08003057 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chiene53750d2011-12-27 18:02:27 +08003058 llvm::Value* result_value =
3059 irb_.CreateXor(src_value, static_cast<uint64_t>(-1));
3060
Elliott Hughesadb8c672012-03-06 16:49:32 -08003061 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chiene53750d2011-12-27 18:02:27 +08003062
Logan Chien70f94b42011-12-27 17:49:11 +08003063 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3064}
3065
3066
3067void MethodCompiler::EmitInsn_SExt(uint32_t dex_pc,
3068 Instruction const* insn) {
Logan Chien61752ad2011-12-27 18:03:51 +08003069
Elliott Hughesadb8c672012-03-06 16:49:32 -08003070 DecodedInstruction dec_insn(insn);
Logan Chien61752ad2011-12-27 18:03:51 +08003071
Elliott Hughesadb8c672012-03-06 16:49:32 -08003072 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chien61752ad2011-12-27 18:03:51 +08003073 llvm::Value* result_value = irb_.CreateSExt(src_value, irb_.getJLongTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003074 EmitStoreDalvikReg(dec_insn.vA, kLong, kAccurate, result_value);
Logan Chien61752ad2011-12-27 18:03:51 +08003075
Logan Chien70f94b42011-12-27 17:49:11 +08003076 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3077}
3078
3079
3080void MethodCompiler::EmitInsn_Trunc(uint32_t dex_pc,
3081 Instruction const* insn) {
Logan Chien17a57662011-12-27 18:05:14 +08003082
Elliott Hughesadb8c672012-03-06 16:49:32 -08003083 DecodedInstruction dec_insn(insn);
Logan Chien17a57662011-12-27 18:05:14 +08003084
Elliott Hughesadb8c672012-03-06 16:49:32 -08003085 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kLong, kAccurate);
Logan Chien17a57662011-12-27 18:05:14 +08003086 llvm::Value* result_value = irb_.CreateTrunc(src_value, irb_.getJIntTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003087 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chien17a57662011-12-27 18:05:14 +08003088
Logan Chien70f94b42011-12-27 17:49:11 +08003089 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3090}
3091
3092
3093void MethodCompiler::EmitInsn_TruncAndSExt(uint32_t dex_pc,
3094 Instruction const* insn,
3095 unsigned N) {
Logan Chienb6744c52011-12-27 18:06:26 +08003096
Elliott Hughesadb8c672012-03-06 16:49:32 -08003097 DecodedInstruction dec_insn(insn);
Logan Chienb6744c52011-12-27 18:06:26 +08003098
Elliott Hughesadb8c672012-03-06 16:49:32 -08003099 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chienb6744c52011-12-27 18:06:26 +08003100
3101 llvm::Value* trunc_value =
3102 irb_.CreateTrunc(src_value, llvm::Type::getIntNTy(*context_, N));
3103
3104 llvm::Value* result_value = irb_.CreateSExt(trunc_value, irb_.getJIntTy());
3105
Elliott Hughesadb8c672012-03-06 16:49:32 -08003106 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chienb6744c52011-12-27 18:06:26 +08003107
Logan Chien70f94b42011-12-27 17:49:11 +08003108 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3109}
3110
3111
3112void MethodCompiler::EmitInsn_TruncAndZExt(uint32_t dex_pc,
3113 Instruction const* insn,
3114 unsigned N) {
Logan Chienb6744c52011-12-27 18:06:26 +08003115
Elliott Hughesadb8c672012-03-06 16:49:32 -08003116 DecodedInstruction dec_insn(insn);
Logan Chienb6744c52011-12-27 18:06:26 +08003117
Elliott Hughesadb8c672012-03-06 16:49:32 -08003118 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chienb6744c52011-12-27 18:06:26 +08003119
3120 llvm::Value* trunc_value =
3121 irb_.CreateTrunc(src_value, llvm::Type::getIntNTy(*context_, N));
3122
3123 llvm::Value* result_value = irb_.CreateZExt(trunc_value, irb_.getJIntTy());
3124
Elliott Hughesadb8c672012-03-06 16:49:32 -08003125 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chienb6744c52011-12-27 18:06:26 +08003126
Logan Chien70f94b42011-12-27 17:49:11 +08003127 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3128}
3129
3130
3131void MethodCompiler::EmitInsn_FNeg(uint32_t dex_pc,
3132 Instruction const* insn,
3133 JType op_jty) {
Logan Chien7a48b092011-12-27 18:07:45 +08003134
Elliott Hughesadb8c672012-03-06 16:49:32 -08003135 DecodedInstruction dec_insn(insn);
Logan Chien7a48b092011-12-27 18:07:45 +08003136
3137 DCHECK(op_jty == kFloat || op_jty == kDouble) << op_jty;
3138
Elliott Hughesadb8c672012-03-06 16:49:32 -08003139 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chien7a48b092011-12-27 18:07:45 +08003140 llvm::Value* result_value = irb_.CreateFNeg(src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003141 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chien7a48b092011-12-27 18:07:45 +08003142
Logan Chien70f94b42011-12-27 17:49:11 +08003143 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3144}
3145
3146
3147void MethodCompiler::EmitInsn_IntToFP(uint32_t dex_pc,
3148 Instruction const* insn,
3149 JType src_jty,
3150 JType dest_jty) {
Logan Chien62dd4532011-12-27 18:09:00 +08003151
Elliott Hughesadb8c672012-03-06 16:49:32 -08003152 DecodedInstruction dec_insn(insn);
Logan Chien62dd4532011-12-27 18:09:00 +08003153
3154 DCHECK(src_jty == kInt || src_jty == kLong) << src_jty;
3155 DCHECK(dest_jty == kFloat || dest_jty == kDouble) << dest_jty;
3156
Elliott Hughesadb8c672012-03-06 16:49:32 -08003157 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, src_jty, kAccurate);
Logan Chien62dd4532011-12-27 18:09:00 +08003158 llvm::Type* dest_type = irb_.getJType(dest_jty, kAccurate);
3159 llvm::Value* dest_value = irb_.CreateSIToFP(src_value, dest_type);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003160 EmitStoreDalvikReg(dec_insn.vA, dest_jty, kAccurate, dest_value);
Logan Chien62dd4532011-12-27 18:09:00 +08003161
Logan Chien70f94b42011-12-27 17:49:11 +08003162 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3163}
3164
3165
3166void MethodCompiler::EmitInsn_FPToInt(uint32_t dex_pc,
3167 Instruction const* insn,
3168 JType src_jty,
TDYa127a4746872012-04-11 23:48:55 -07003169 JType dest_jty,
3170 runtime_support::RuntimeId runtime_func_id) {
Logan Chien12dc1752011-12-27 18:10:15 +08003171
Elliott Hughesadb8c672012-03-06 16:49:32 -08003172 DecodedInstruction dec_insn(insn);
Logan Chien12dc1752011-12-27 18:10:15 +08003173
3174 DCHECK(src_jty == kFloat || src_jty == kDouble) << src_jty;
3175 DCHECK(dest_jty == kInt || dest_jty == kLong) << dest_jty;
3176
Elliott Hughesadb8c672012-03-06 16:49:32 -08003177 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, src_jty, kAccurate);
TDYa127a4746872012-04-11 23:48:55 -07003178 llvm::Value* dest_value = irb_.CreateCall(irb_.GetRuntime(runtime_func_id), src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003179 EmitStoreDalvikReg(dec_insn.vA, dest_jty, kAccurate, dest_value);
Logan Chien12dc1752011-12-27 18:10:15 +08003180
Logan Chien70f94b42011-12-27 17:49:11 +08003181 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3182}
3183
3184
3185void MethodCompiler::EmitInsn_FExt(uint32_t dex_pc,
3186 Instruction const* insn) {
Logan Chienc56ded92011-12-27 18:10:57 +08003187
Elliott Hughesadb8c672012-03-06 16:49:32 -08003188 DecodedInstruction dec_insn(insn);
Logan Chienc56ded92011-12-27 18:10:57 +08003189
Elliott Hughesadb8c672012-03-06 16:49:32 -08003190 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kFloat, kAccurate);
Logan Chienc56ded92011-12-27 18:10:57 +08003191 llvm::Value* result_value = irb_.CreateFPExt(src_value, irb_.getJDoubleTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003192 EmitStoreDalvikReg(dec_insn.vA, kDouble, kAccurate, result_value);
Logan Chienc56ded92011-12-27 18:10:57 +08003193
Logan Chien70f94b42011-12-27 17:49:11 +08003194 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3195}
3196
3197
3198void MethodCompiler::EmitInsn_FTrunc(uint32_t dex_pc,
3199 Instruction const* insn) {
Logan Chien927744f2011-12-27 18:11:52 +08003200
Elliott Hughesadb8c672012-03-06 16:49:32 -08003201 DecodedInstruction dec_insn(insn);
Logan Chien927744f2011-12-27 18:11:52 +08003202
Elliott Hughesadb8c672012-03-06 16:49:32 -08003203 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kDouble, kAccurate);
Logan Chien927744f2011-12-27 18:11:52 +08003204 llvm::Value* result_value = irb_.CreateFPTrunc(src_value, irb_.getJFloatTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003205 EmitStoreDalvikReg(dec_insn.vA, kFloat, kAccurate, result_value);
Logan Chien927744f2011-12-27 18:11:52 +08003206
Logan Chien70f94b42011-12-27 17:49:11 +08003207 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3208}
3209
3210
3211void MethodCompiler::EmitInsn_IntArithm(uint32_t dex_pc,
3212 Instruction const* insn,
3213 IntArithmKind arithm,
3214 JType op_jty,
3215 bool is_2addr) {
Logan Chienc3f7d962011-12-27 18:13:18 +08003216
Elliott Hughesadb8c672012-03-06 16:49:32 -08003217 DecodedInstruction dec_insn(insn);
Logan Chienc3f7d962011-12-27 18:13:18 +08003218
3219 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3220
3221 llvm::Value* src1_value;
3222 llvm::Value* src2_value;
3223
3224 if (is_2addr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003225 src1_value = EmitLoadDalvikReg(dec_insn.vA, op_jty, kAccurate);
3226 src2_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chienc3f7d962011-12-27 18:13:18 +08003227 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003228 src1_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
3229 src2_value = EmitLoadDalvikReg(dec_insn.vC, op_jty, kAccurate);
Logan Chienc3f7d962011-12-27 18:13:18 +08003230 }
3231
3232 llvm::Value* result_value =
3233 EmitIntArithmResultComputation(dex_pc, src1_value, src2_value,
3234 arithm, op_jty);
3235
Elliott Hughesadb8c672012-03-06 16:49:32 -08003236 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chienc3f7d962011-12-27 18:13:18 +08003237
Logan Chien70f94b42011-12-27 17:49:11 +08003238 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3239}
3240
3241
3242void MethodCompiler::EmitInsn_IntArithmImmediate(uint32_t dex_pc,
3243 Instruction const* insn,
3244 IntArithmKind arithm) {
Logan Chienc3f7d962011-12-27 18:13:18 +08003245
Elliott Hughesadb8c672012-03-06 16:49:32 -08003246 DecodedInstruction dec_insn(insn);
Logan Chienc3f7d962011-12-27 18:13:18 +08003247
Elliott Hughesadb8c672012-03-06 16:49:32 -08003248 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chienc3f7d962011-12-27 18:13:18 +08003249
Elliott Hughesadb8c672012-03-06 16:49:32 -08003250 llvm::Value* imm_value = irb_.getInt32(dec_insn.vC);
Logan Chienc3f7d962011-12-27 18:13:18 +08003251
3252 llvm::Value* result_value =
3253 EmitIntArithmResultComputation(dex_pc, src_value, imm_value, arithm, kInt);
3254
Elliott Hughesadb8c672012-03-06 16:49:32 -08003255 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chienc3f7d962011-12-27 18:13:18 +08003256
Logan Chien70f94b42011-12-27 17:49:11 +08003257 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3258}
3259
3260
Logan Chienc3f7d962011-12-27 18:13:18 +08003261llvm::Value*
3262MethodCompiler::EmitIntArithmResultComputation(uint32_t dex_pc,
3263 llvm::Value* lhs,
3264 llvm::Value* rhs,
3265 IntArithmKind arithm,
3266 JType op_jty) {
3267 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3268
3269 switch (arithm) {
3270 case kIntArithm_Add:
3271 return irb_.CreateAdd(lhs, rhs);
3272
3273 case kIntArithm_Sub:
3274 return irb_.CreateSub(lhs, rhs);
3275
3276 case kIntArithm_Mul:
3277 return irb_.CreateMul(lhs, rhs);
3278
3279 case kIntArithm_Div:
Logan Chienc3f7d962011-12-27 18:13:18 +08003280 case kIntArithm_Rem:
TDYa127f8641ce2012-04-02 06:40:40 -07003281 return EmitIntDivRemResultComputation(dex_pc, lhs, rhs, arithm, op_jty);
Logan Chienc3f7d962011-12-27 18:13:18 +08003282
3283 case kIntArithm_And:
3284 return irb_.CreateAnd(lhs, rhs);
3285
3286 case kIntArithm_Or:
3287 return irb_.CreateOr(lhs, rhs);
3288
3289 case kIntArithm_Xor:
3290 return irb_.CreateXor(lhs, rhs);
3291
Logan Chienc3f7d962011-12-27 18:13:18 +08003292 default:
3293 LOG(FATAL) << "Unknown integer arithmetic kind: " << arithm;
3294 return NULL;
3295 }
3296}
3297
3298
TDYa127f8641ce2012-04-02 06:40:40 -07003299llvm::Value*
3300MethodCompiler::EmitIntDivRemResultComputation(uint32_t dex_pc,
3301 llvm::Value* dividend,
3302 llvm::Value* divisor,
3303 IntArithmKind arithm,
3304 JType op_jty) {
3305 // Throw exception if the divisor is 0.
3306 EmitGuard_DivZeroException(dex_pc, divisor, op_jty);
3307
3308 // Check the special case: MININT / -1 = MININT
3309 // That case will cause overflow, which is undefined behavior in llvm.
3310 // So we check the divisor is -1 or not, if the divisor is -1, we do
3311 // the special path to avoid undefined behavior.
3312 llvm::Type* op_type = irb_.getJType(op_jty, kAccurate);
3313 llvm::Value* zero = irb_.getJZero(op_jty);
3314 llvm::Value* neg_one = llvm::ConstantInt::getSigned(op_type, -1);
3315 llvm::Value* result = irb_.CreateAlloca(op_type);
3316
3317 llvm::BasicBlock* eq_neg_one = CreateBasicBlockWithDexPC(dex_pc, "eq_neg_one");
3318 llvm::BasicBlock* ne_neg_one = CreateBasicBlockWithDexPC(dex_pc, "ne_neg_one");
3319 llvm::BasicBlock* neg_one_cont = CreateBasicBlockWithDexPC(dex_pc, "neg_one_cont");
3320
3321 llvm::Value* is_equal_neg_one = EmitConditionResult(divisor, neg_one, kCondBranch_EQ);
TDYa127ac7b5bb2012-05-11 13:17:49 -07003322 irb_.CreateCondBr(is_equal_neg_one, eq_neg_one, ne_neg_one, kUnlikely);
TDYa127f8641ce2012-04-02 06:40:40 -07003323
3324 // If divisor == -1
3325 irb_.SetInsertPoint(eq_neg_one);
3326 llvm::Value* eq_result;
3327 if (arithm == kIntArithm_Div) {
3328 // We can just change from "dividend div -1" to "neg dividend".
3329 // The sub don't care the sign/unsigned because of two's complement representation.
3330 // And the behavior is what we want:
3331 // -(2^n) (2^n)-1
3332 // MININT < k <= MAXINT -> mul k -1 = -k
3333 // MININT == k -> mul k -1 = k
3334 //
3335 // LLVM use sub to represent 'neg'
3336 eq_result = irb_.CreateSub(zero, dividend);
3337 } else {
3338 // Everything modulo -1 will be 0.
3339 eq_result = zero;
3340 }
TDYa127aba61122012-05-04 18:28:36 -07003341 irb_.CreateStore(eq_result, result, kTBAAStackTemp);
TDYa127f8641ce2012-04-02 06:40:40 -07003342 irb_.CreateBr(neg_one_cont);
3343
3344 // If divisor != -1, just do the division.
3345 irb_.SetInsertPoint(ne_neg_one);
3346 llvm::Value* ne_result;
3347 if (arithm == kIntArithm_Div) {
3348 ne_result = irb_.CreateSDiv(dividend, divisor);
3349 } else {
3350 ne_result = irb_.CreateSRem(dividend, divisor);
3351 }
TDYa127aba61122012-05-04 18:28:36 -07003352 irb_.CreateStore(ne_result, result, kTBAAStackTemp);
TDYa127f8641ce2012-04-02 06:40:40 -07003353 irb_.CreateBr(neg_one_cont);
3354
3355 irb_.SetInsertPoint(neg_one_cont);
TDYa127aba61122012-05-04 18:28:36 -07003356 return irb_.CreateLoad(result, kTBAAStackTemp);
TDYa127f8641ce2012-04-02 06:40:40 -07003357}
3358
3359
Logan Chien5539ad02012-04-02 14:36:55 +08003360void MethodCompiler::EmitInsn_IntShiftArithm(uint32_t dex_pc,
3361 Instruction const* insn,
3362 IntShiftArithmKind arithm,
3363 JType op_jty,
3364 bool is_2addr) {
3365
3366 DecodedInstruction dec_insn(insn);
3367
3368 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3369
3370 llvm::Value* src1_value;
3371 llvm::Value* src2_value;
3372
3373 // NOTE: The 2nd operand of the shift arithmetic instruction is
3374 // 32-bit integer regardless of the 1st operand.
3375 if (is_2addr) {
3376 src1_value = EmitLoadDalvikReg(dec_insn.vA, op_jty, kAccurate);
3377 src2_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
3378 } else {
3379 src1_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
3380 src2_value = EmitLoadDalvikReg(dec_insn.vC, kInt, kAccurate);
3381 }
3382
3383 llvm::Value* result_value =
3384 EmitIntShiftArithmResultComputation(dex_pc, src1_value, src2_value,
3385 arithm, op_jty);
3386
3387 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
3388
3389 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3390}
3391
3392
3393void MethodCompiler::
3394EmitInsn_IntShiftArithmImmediate(uint32_t dex_pc,
3395 Instruction const* insn,
3396 IntShiftArithmKind arithm) {
3397
3398 DecodedInstruction dec_insn(insn);
3399
3400 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
3401
3402 llvm::Value* imm_value = irb_.getInt32(dec_insn.vC);
3403
3404 llvm::Value* result_value =
3405 EmitIntShiftArithmResultComputation(dex_pc, src_value, imm_value,
3406 arithm, kInt);
3407
3408 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
3409
3410 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3411}
3412
3413
3414llvm::Value*
3415MethodCompiler::EmitIntShiftArithmResultComputation(uint32_t dex_pc,
3416 llvm::Value* lhs,
3417 llvm::Value* rhs,
3418 IntShiftArithmKind arithm,
3419 JType op_jty) {
3420 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3421
3422 if (op_jty == kInt) {
3423 rhs = irb_.CreateAnd(rhs, 0x1f);
3424 } else {
3425 llvm::Value* masked_rhs = irb_.CreateAnd(rhs, 0x3f);
3426 rhs = irb_.CreateZExt(masked_rhs, irb_.getJLongTy());
3427 }
3428
3429 switch (arithm) {
3430 case kIntArithm_Shl:
3431 return irb_.CreateShl(lhs, rhs);
3432
3433 case kIntArithm_Shr:
3434 return irb_.CreateAShr(lhs, rhs);
3435
3436 case kIntArithm_UShr:
3437 return irb_.CreateLShr(lhs, rhs);
3438
3439 default:
3440 LOG(FATAL) << "Unknown integer shift arithmetic kind: " << arithm;
3441 return NULL;
3442 }
3443}
3444
3445
Logan Chien70f94b42011-12-27 17:49:11 +08003446void MethodCompiler::EmitInsn_RSubImmediate(uint32_t dex_pc,
3447 Instruction const* insn) {
Logan Chien65c62d42011-12-27 18:14:18 +08003448
Elliott Hughesadb8c672012-03-06 16:49:32 -08003449 DecodedInstruction dec_insn(insn);
Logan Chien65c62d42011-12-27 18:14:18 +08003450
Elliott Hughesadb8c672012-03-06 16:49:32 -08003451 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
3452 llvm::Value* imm_value = irb_.getInt32(dec_insn.vC);
Logan Chien65c62d42011-12-27 18:14:18 +08003453 llvm::Value* result_value = irb_.CreateSub(imm_value, src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003454 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chien65c62d42011-12-27 18:14:18 +08003455
Logan Chien70f94b42011-12-27 17:49:11 +08003456 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3457}
3458
3459
3460void MethodCompiler::EmitInsn_FPArithm(uint32_t dex_pc,
3461 Instruction const* insn,
3462 FPArithmKind arithm,
3463 JType op_jty,
3464 bool is_2addr) {
Logan Chien76e1c792011-12-27 18:15:01 +08003465
Elliott Hughesadb8c672012-03-06 16:49:32 -08003466 DecodedInstruction dec_insn(insn);
Logan Chien76e1c792011-12-27 18:15:01 +08003467
3468 DCHECK(op_jty == kFloat || op_jty == kDouble) << op_jty;
3469
3470 llvm::Value* src1_value;
3471 llvm::Value* src2_value;
3472
3473 if (is_2addr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003474 src1_value = EmitLoadDalvikReg(dec_insn.vA, op_jty, kAccurate);
3475 src2_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chien76e1c792011-12-27 18:15:01 +08003476 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003477 src1_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
3478 src2_value = EmitLoadDalvikReg(dec_insn.vC, op_jty, kAccurate);
Logan Chien76e1c792011-12-27 18:15:01 +08003479 }
3480
3481 llvm::Value* result_value =
3482 EmitFPArithmResultComputation(dex_pc, src1_value, src2_value, arithm);
3483
Elliott Hughesadb8c672012-03-06 16:49:32 -08003484 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chien76e1c792011-12-27 18:15:01 +08003485
Logan Chien70f94b42011-12-27 17:49:11 +08003486 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3487}
3488
3489
Logan Chien76e1c792011-12-27 18:15:01 +08003490llvm::Value*
3491MethodCompiler::EmitFPArithmResultComputation(uint32_t dex_pc,
3492 llvm::Value *lhs,
3493 llvm::Value *rhs,
3494 FPArithmKind arithm) {
3495 switch (arithm) {
3496 case kFPArithm_Add:
3497 return irb_.CreateFAdd(lhs, rhs);
3498
3499 case kFPArithm_Sub:
3500 return irb_.CreateFSub(lhs, rhs);
3501
3502 case kFPArithm_Mul:
3503 return irb_.CreateFMul(lhs, rhs);
3504
3505 case kFPArithm_Div:
3506 return irb_.CreateFDiv(lhs, rhs);
3507
3508 case kFPArithm_Rem:
3509 return irb_.CreateFRem(lhs, rhs);
3510
3511 default:
3512 LOG(FATAL) << "Unknown floating-point arithmetic kind: " << arithm;
3513 return NULL;
3514 }
3515}
3516
3517
Logan Chienc3f7d962011-12-27 18:13:18 +08003518void MethodCompiler::EmitGuard_DivZeroException(uint32_t dex_pc,
3519 llvm::Value* denominator,
3520 JType op_jty) {
3521 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3522
3523 llvm::Constant* zero = irb_.getJZero(op_jty);
3524
3525 llvm::Value* equal_zero = irb_.CreateICmpEQ(denominator, zero);
3526
3527 llvm::BasicBlock* block_exception = CreateBasicBlockWithDexPC(dex_pc, "div0");
3528
3529 llvm::BasicBlock* block_continue = CreateBasicBlockWithDexPC(dex_pc, "cont");
3530
TDYa127ac7b5bb2012-05-11 13:17:49 -07003531 irb_.CreateCondBr(equal_zero, block_exception, block_continue, kUnlikely);
Logan Chienc3f7d962011-12-27 18:13:18 +08003532
3533 irb_.SetInsertPoint(block_exception);
TDYa127c8dc1012012-04-19 07:03:33 -07003534 EmitUpdateDexPC(dex_pc);
Logan Chienc3f7d962011-12-27 18:13:18 +08003535 irb_.CreateCall(irb_.GetRuntime(ThrowDivZeroException));
3536 EmitBranchExceptionLandingPad(dex_pc);
3537
3538 irb_.SetInsertPoint(block_continue);
3539}
3540
3541
Logan Chien61bb6142012-02-03 15:34:53 +08003542void MethodCompiler::EmitGuard_NullPointerException(uint32_t dex_pc,
3543 llvm::Value* object) {
3544 llvm::Value* equal_null = irb_.CreateICmpEQ(object, irb_.getJNull());
3545
3546 llvm::BasicBlock* block_exception =
3547 CreateBasicBlockWithDexPC(dex_pc, "nullp");
3548
3549 llvm::BasicBlock* block_continue =
3550 CreateBasicBlockWithDexPC(dex_pc, "cont");
3551
TDYa127ac7b5bb2012-05-11 13:17:49 -07003552 irb_.CreateCondBr(equal_null, block_exception, block_continue, kUnlikely);
Logan Chien61bb6142012-02-03 15:34:53 +08003553
3554 irb_.SetInsertPoint(block_exception);
TDYa127c8dc1012012-04-19 07:03:33 -07003555 EmitUpdateDexPC(dex_pc);
TDYa1273f9137d2012-04-08 15:59:19 -07003556 irb_.CreateCall(irb_.GetRuntime(ThrowNullPointerException), irb_.getInt32(dex_pc));
Logan Chien61bb6142012-02-03 15:34:53 +08003557 EmitBranchExceptionLandingPad(dex_pc);
3558
3559 irb_.SetInsertPoint(block_continue);
3560}
3561
3562
Logan Chienbb4d12a2012-02-17 14:10:01 +08003563llvm::Value* MethodCompiler::EmitLoadDexCacheAddr(MemberOffset offset) {
3564 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
3565
TDYa127ee1f59b2012-04-25 00:56:40 -07003566 return irb_.LoadFromObjectOffset(method_object_addr,
3567 offset.Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -07003568 irb_.getJObjectTy(),
TDYa127d3e24c22012-05-05 20:54:19 -07003569 kTBAAConstJObject);
Logan Chienbb4d12a2012-02-17 14:10:01 +08003570}
3571
3572
Logan Chienbb4d12a2012-02-17 14:10:01 +08003573llvm::Value* MethodCompiler::
3574EmitLoadDexCacheStaticStorageFieldAddr(uint32_t type_idx) {
3575 llvm::Value* static_storage_dex_cache_addr =
3576 EmitLoadDexCacheAddr(Method::DexCacheInitializedStaticStorageOffset());
3577
3578 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
3579
3580 return EmitArrayGEP(static_storage_dex_cache_addr, type_idx_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08003581 irb_.getJObjectTy(), kObject);
Logan Chienbb4d12a2012-02-17 14:10:01 +08003582}
3583
3584
3585llvm::Value* MethodCompiler::
3586EmitLoadDexCacheResolvedTypeFieldAddr(uint32_t type_idx) {
3587 llvm::Value* resolved_type_dex_cache_addr =
3588 EmitLoadDexCacheAddr(Method::DexCacheResolvedTypesOffset());
3589
3590 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
3591
3592 return EmitArrayGEP(resolved_type_dex_cache_addr, type_idx_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08003593 irb_.getJObjectTy(), kObject);
Logan Chienbb4d12a2012-02-17 14:10:01 +08003594}
3595
3596
3597llvm::Value* MethodCompiler::
Logan Chien61c65dc2012-02-29 03:22:30 +08003598EmitLoadDexCacheResolvedMethodFieldAddr(uint32_t method_idx) {
3599 llvm::Value* resolved_method_dex_cache_addr =
3600 EmitLoadDexCacheAddr(Method::DexCacheResolvedMethodsOffset());
3601
3602 llvm::Value* method_idx_value = irb_.getPtrEquivInt(method_idx);
3603
3604 return EmitArrayGEP(resolved_method_dex_cache_addr, method_idx_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08003605 irb_.getJObjectTy(), kObject);
Logan Chien61c65dc2012-02-29 03:22:30 +08003606}
3607
3608
3609llvm::Value* MethodCompiler::
Logan Chienbb4d12a2012-02-17 14:10:01 +08003610EmitLoadDexCacheStringFieldAddr(uint32_t string_idx) {
3611 llvm::Value* string_dex_cache_addr =
3612 EmitLoadDexCacheAddr(Method::DexCacheStringsOffset());
3613
3614 llvm::Value* string_idx_value = irb_.getPtrEquivInt(string_idx);
3615
3616 return EmitArrayGEP(string_dex_cache_addr, string_idx_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08003617 irb_.getJObjectTy(), kObject);
Logan Chienbb4d12a2012-02-17 14:10:01 +08003618}
3619
3620
Logan Chien83426162011-12-09 09:29:50 +08003621CompiledMethod *MethodCompiler::Compile() {
TDYa127cc1b4c32012-05-15 07:31:37 -07003622 // TODO: Use high-level IR to do this
3623 // Compute method info
3624 ComputeMethodInfo();
3625
Logan Chien0b827102011-12-20 19:46:14 +08003626 // Code generation
3627 CreateFunction();
3628
3629 EmitPrologue();
3630 EmitInstructions();
Logan Chienc670a8d2011-12-20 21:25:56 +08003631 EmitPrologueLastBranch();
Logan Chien0b827102011-12-20 19:46:14 +08003632
Logan Chiend6c239a2011-12-23 15:11:45 +08003633 // Verify the generated bitcode
TDYa127853cd092012-04-21 22:15:31 -07003634 VERIFY_LLVM_FUNCTION(*func_);
Logan Chiend6c239a2011-12-23 15:11:45 +08003635
Logan Chien8b977d32012-02-21 19:14:55 +08003636 // Add the memory usage approximation of the compilation unit
3637 cunit_->AddMemUsageApproximation(code_item_->insns_size_in_code_units_ * 900);
TDYa127cc1b4c32012-05-15 07:31:37 -07003638 // NOTE: From statistics, the bitcode size is 4.5 times bigger than the
Logan Chien8b977d32012-02-21 19:14:55 +08003639 // Dex file. Besides, we have to convert the code unit into bytes.
3640 // Thus, we got our magic number 9.
3641
Logan Chien110bcba2012-04-16 19:11:28 +08003642 CompiledMethod* compiled_method =
3643 new CompiledMethod(cunit_->GetInstructionSet(),
3644 cunit_->GetElfIndex(),
3645 elf_func_idx_);
3646
3647 cunit_->RegisterCompiledMethod(func_, compiled_method);
3648
3649 return compiled_method;
Logan Chien0b827102011-12-20 19:46:14 +08003650}
3651
3652
3653llvm::Value* MethodCompiler::EmitLoadMethodObjectAddr() {
3654 return func_->arg_begin();
Shih-wei Liaod1fec812012-02-13 09:51:10 -08003655}
Logan Chien83426162011-12-09 09:29:50 +08003656
3657
Logan Chien5bcc04e2012-01-30 14:15:12 +08003658void MethodCompiler::EmitBranchExceptionLandingPad(uint32_t dex_pc) {
3659 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
3660 irb_.CreateBr(lpad);
3661 } else {
3662 irb_.CreateBr(GetUnwindBasicBlock());
3663 }
3664}
3665
3666
3667void MethodCompiler::EmitGuard_ExceptionLandingPad(uint32_t dex_pc) {
3668 llvm::Value* exception_pending =
3669 irb_.CreateCall(irb_.GetRuntime(IsExceptionPending));
3670
3671 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
3672
3673 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
TDYa127ac7b5bb2012-05-11 13:17:49 -07003674 irb_.CreateCondBr(exception_pending, lpad, block_cont, kUnlikely);
Logan Chien5bcc04e2012-01-30 14:15:12 +08003675 } else {
TDYa127ac7b5bb2012-05-11 13:17:49 -07003676 irb_.CreateCondBr(exception_pending, GetUnwindBasicBlock(), block_cont, kUnlikely);
Logan Chien5bcc04e2012-01-30 14:15:12 +08003677 }
3678
3679 irb_.SetInsertPoint(block_cont);
3680}
3681
3682
Logan Chien924072f2012-01-30 15:07:24 +08003683void MethodCompiler::EmitGuard_GarbageCollectionSuspend(uint32_t dex_pc) {
TDYa127cc1b4c32012-05-15 07:31:37 -07003684 if (!method_info_.need_shadow_frame_entry) {
3685 return;
3686 }
3687
Logan Chien924072f2012-01-30 15:07:24 +08003688 llvm::Value* runtime_func = irb_.GetRuntime(TestSuspend);
TDYa127853cd092012-04-21 22:15:31 -07003689
3690 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
3691
TDYa127853cd092012-04-21 22:15:31 -07003692 irb_.CreateCall(runtime_func, thread_object_addr);
Logan Chien924072f2012-01-30 15:07:24 +08003693}
3694
3695
Logan Chiend6c239a2011-12-23 15:11:45 +08003696llvm::BasicBlock* MethodCompiler::
3697CreateBasicBlockWithDexPC(uint32_t dex_pc, char const* postfix) {
3698 std::string name;
3699
TDYa12767ae8ff2012-05-02 19:08:02 -07003700#if !defined(NDEBUG)
Logan Chiend6c239a2011-12-23 15:11:45 +08003701 if (postfix) {
TDYa12799489132012-04-29 01:27:58 -07003702 StringAppendF(&name, "B%04x.%s", dex_pc, postfix);
Logan Chiend6c239a2011-12-23 15:11:45 +08003703 } else {
TDYa12799489132012-04-29 01:27:58 -07003704 StringAppendF(&name, "B%04x", dex_pc);
Logan Chiend6c239a2011-12-23 15:11:45 +08003705 }
TDYa12767ae8ff2012-05-02 19:08:02 -07003706#endif
Logan Chiend6c239a2011-12-23 15:11:45 +08003707
3708 return llvm::BasicBlock::Create(*context_, name, func_);
3709}
3710
3711
3712llvm::BasicBlock* MethodCompiler::GetBasicBlock(uint32_t dex_pc) {
3713 DCHECK(dex_pc < code_item_->insns_size_in_code_units_);
3714
3715 llvm::BasicBlock* basic_block = basic_blocks_[dex_pc];
3716
3717 if (!basic_block) {
3718 basic_block = CreateBasicBlockWithDexPC(dex_pc);
3719 basic_blocks_[dex_pc] = basic_block;
3720 }
3721
3722 return basic_block;
3723}
3724
3725
3726llvm::BasicBlock*
3727MethodCompiler::GetNextBasicBlock(uint32_t dex_pc) {
3728 Instruction const* insn = Instruction::At(code_item_->insns_ + dex_pc);
3729 return GetBasicBlock(dex_pc + insn->SizeInCodeUnits());
3730}
3731
3732
Logan Chien5bcc04e2012-01-30 14:15:12 +08003733int32_t MethodCompiler::GetTryItemOffset(uint32_t dex_pc) {
3734 // TODO: Since we are emitting the dex instructions in ascending order
3735 // w.r.t. address, we can cache the lastest try item offset so that we
3736 // don't have to do binary search for every query.
3737
3738 int32_t min = 0;
3739 int32_t max = code_item_->tries_size_ - 1;
3740
3741 while (min <= max) {
3742 int32_t mid = min + (max - min) / 2;
3743
3744 DexFile::TryItem const* ti = DexFile::GetTryItems(*code_item_, mid);
3745 uint32_t start = ti->start_addr_;
3746 uint32_t end = start + ti->insn_count_;
3747
3748 if (dex_pc < start) {
3749 max = mid - 1;
3750 } else if (dex_pc >= end) {
3751 min = mid + 1;
3752 } else {
3753 return mid; // found
3754 }
3755 }
3756
3757 return -1; // not found
3758}
3759
3760
3761llvm::BasicBlock* MethodCompiler::GetLandingPadBasicBlock(uint32_t dex_pc) {
3762 // Find the try item for this address in this method
3763 int32_t ti_offset = GetTryItemOffset(dex_pc);
3764
3765 if (ti_offset == -1) {
3766 return NULL; // No landing pad is available for this address.
3767 }
3768
3769 // Check for the existing landing pad basic block
3770 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
3771 llvm::BasicBlock* block_lpad = basic_block_landing_pads_[ti_offset];
3772
3773 if (block_lpad) {
3774 // We have generated landing pad for this try item already. Return the
3775 // same basic block.
3776 return block_lpad;
3777 }
3778
3779 // Get try item from code item
3780 DexFile::TryItem const* ti = DexFile::GetTryItems(*code_item_, ti_offset);
3781
TDYa12767ae8ff2012-05-02 19:08:02 -07003782 std::string lpadname;
3783
3784#if !defined(NDEBUG)
3785 StringAppendF(&lpadname, "lpad%d_%04x_to_%04x", ti_offset, ti->start_addr_, ti->handler_off_);
3786#endif
3787
Logan Chien5bcc04e2012-01-30 14:15:12 +08003788 // Create landing pad basic block
TDYa12767ae8ff2012-05-02 19:08:02 -07003789 block_lpad = llvm::BasicBlock::Create(*context_, lpadname, func_);
Logan Chien5bcc04e2012-01-30 14:15:12 +08003790
3791 // Change IRBuilder insert point
3792 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3793 irb_.SetInsertPoint(block_lpad);
3794
3795 // Find catch block with matching type
3796 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
3797
Logan Chien736df022012-04-27 16:25:57 +08003798 llvm::Value* ti_offset_value = irb_.getInt32(ti_offset);
Logan Chien5bcc04e2012-01-30 14:15:12 +08003799
3800 llvm::Value* catch_handler_index_value =
3801 irb_.CreateCall2(irb_.GetRuntime(FindCatchBlock),
Logan Chien736df022012-04-27 16:25:57 +08003802 method_object_addr, ti_offset_value);
Logan Chien5bcc04e2012-01-30 14:15:12 +08003803
3804 // Switch instruction (Go to unwind basic block by default)
3805 llvm::SwitchInst* sw =
3806 irb_.CreateSwitch(catch_handler_index_value, GetUnwindBasicBlock());
3807
3808 // Cases with matched catch block
3809 CatchHandlerIterator iter(*code_item_, ti->start_addr_);
3810
3811 for (uint32_t c = 0; iter.HasNext(); iter.Next(), ++c) {
3812 sw->addCase(irb_.getInt32(c), GetBasicBlock(iter.GetHandlerAddress()));
3813 }
3814
3815 // Restore the orignal insert point for IRBuilder
3816 irb_.restoreIP(irb_ip_original);
3817
3818 // Cache this landing pad
3819 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
3820 basic_block_landing_pads_[ti_offset] = block_lpad;
3821
3822 return block_lpad;
3823}
3824
3825
3826llvm::BasicBlock* MethodCompiler::GetUnwindBasicBlock() {
3827 // Check the existing unwinding baisc block block
3828 if (basic_block_unwind_ != NULL) {
3829 return basic_block_unwind_;
3830 }
3831
3832 // Create new basic block for unwinding
3833 basic_block_unwind_ =
3834 llvm::BasicBlock::Create(*context_, "exception_unwind", func_);
3835
3836 // Change IRBuilder insert point
3837 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3838 irb_.SetInsertPoint(basic_block_unwind_);
3839
Logan Chien8dfcbea2012-02-17 18:50:32 +08003840 // Pop the shadow frame
3841 EmitPopShadowFrame();
3842
Logan Chien5bcc04e2012-01-30 14:15:12 +08003843 // Emit the code to return default value (zero) for the given return type.
Logan Chiendd361c92012-04-10 23:40:37 +08003844 char ret_shorty = oat_compilation_unit_->GetShorty()[0];
Logan Chien5bcc04e2012-01-30 14:15:12 +08003845 if (ret_shorty == 'V') {
3846 irb_.CreateRetVoid();
3847 } else {
3848 irb_.CreateRet(irb_.getJZero(ret_shorty));
3849 }
3850
3851 // Restore the orignal insert point for IRBuilder
3852 irb_.restoreIP(irb_ip_original);
3853
3854 return basic_block_unwind_;
3855}
3856
3857
Logan Chienc670a8d2011-12-20 21:25:56 +08003858llvm::Value* MethodCompiler::AllocDalvikLocalVarReg(RegCategory cat,
3859 uint32_t reg_idx) {
TDYa12767ae8ff2012-05-02 19:08:02 -07003860 // Get reg_type and reg_name from DalvikReg
3861 llvm::Type* reg_type = DalvikReg::GetRegCategoryEquivSizeTy(irb_, cat);
3862 std::string reg_name;
3863
3864#if !defined(NDEBUG)
3865 StringAppendF(&reg_name, "%c%u", DalvikReg::GetRegCategoryNamePrefix(cat), reg_idx);
3866#endif
Logan Chienc670a8d2011-12-20 21:25:56 +08003867
3868 // Save current IR builder insert point
3869 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
TDYa12767ae8ff2012-05-02 19:08:02 -07003870 irb_.SetInsertPoint(basic_block_reg_alloca_);
Logan Chienc670a8d2011-12-20 21:25:56 +08003871
3872 // Alloca
TDYa12767ae8ff2012-05-02 19:08:02 -07003873 llvm::Value* reg_addr = irb_.CreateAlloca(reg_type, 0, reg_name);
Logan Chienc670a8d2011-12-20 21:25:56 +08003874
3875 // Restore IRBuilder insert point
3876 irb_.restoreIP(irb_ip_original);
3877
3878 DCHECK_NE(reg_addr, static_cast<llvm::Value*>(NULL));
3879 return reg_addr;
3880}
3881
3882
TDYa1277f5b9be2012-04-29 01:31:49 -07003883llvm::Value* MethodCompiler::AllocShadowFrameEntry(uint32_t reg_idx) {
TDYa1271d7e5102012-05-13 09:27:05 -07003884 if (reg_to_shadow_frame_index_[reg_idx] == -1) {
3885 // This register dosen't need ShadowFrame entry
3886 return NULL;
3887 }
3888
TDYa127cc1b4c32012-05-15 07:31:37 -07003889 if (!method_info_.need_shadow_frame_entry) {
3890 return NULL;
3891 }
3892
TDYa12767ae8ff2012-05-02 19:08:02 -07003893 std::string reg_name;
3894
3895#if !defined(NDEBUG)
3896 StringAppendF(&reg_name, "o%u", reg_idx);
3897#endif
3898
TDYa1277f5b9be2012-04-29 01:31:49 -07003899 // Save current IR builder insert point
3900 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3901
3902 irb_.SetInsertPoint(basic_block_shadow_frame_alloca_);
3903
3904 llvm::Value* gep_index[] = {
3905 irb_.getInt32(0), // No pointer displacement
3906 irb_.getInt32(1), // SIRT
TDYa1271d7e5102012-05-13 09:27:05 -07003907 irb_.getInt32(reg_to_shadow_frame_index_[reg_idx]) // Pointer field
TDYa1277f5b9be2012-04-29 01:31:49 -07003908 };
3909
TDYa12767ae8ff2012-05-02 19:08:02 -07003910 llvm::Value* reg_addr = irb_.CreateGEP(shadow_frame_, gep_index, reg_name);
TDYa1277f5b9be2012-04-29 01:31:49 -07003911
3912 // Restore IRBuilder insert point
3913 irb_.restoreIP(irb_ip_original);
3914
3915 DCHECK_NE(reg_addr, static_cast<llvm::Value*>(NULL));
3916 return reg_addr;
3917}
3918
3919
Logan Chienc670a8d2011-12-20 21:25:56 +08003920llvm::Value* MethodCompiler::AllocDalvikRetValReg(RegCategory cat) {
TDYa12767ae8ff2012-05-02 19:08:02 -07003921 // Get reg_type and reg_name from DalvikReg
3922 llvm::Type* reg_type = DalvikReg::GetRegCategoryEquivSizeTy(irb_, cat);
3923 std::string reg_name;
3924
3925#if !defined(NDEBUG)
3926 StringAppendF(&reg_name, "%c_res", DalvikReg::GetRegCategoryNamePrefix(cat));
3927#endif
3928
Logan Chienc670a8d2011-12-20 21:25:56 +08003929 // Save current IR builder insert point
3930 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
TDYa12767ae8ff2012-05-02 19:08:02 -07003931 irb_.SetInsertPoint(basic_block_reg_alloca_);
Logan Chienc670a8d2011-12-20 21:25:56 +08003932
3933 // Alloca
TDYa12767ae8ff2012-05-02 19:08:02 -07003934 llvm::Value* reg_addr = irb_.CreateAlloca(reg_type, 0, reg_name);
Logan Chienc670a8d2011-12-20 21:25:56 +08003935
3936 // Restore IRBuilder insert point
3937 irb_.restoreIP(irb_ip_original);
3938
3939 DCHECK_NE(reg_addr, static_cast<llvm::Value*>(NULL));
3940 return reg_addr;
3941}
3942
3943
Logan Chien8dfcbea2012-02-17 18:50:32 +08003944void MethodCompiler::EmitPopShadowFrame() {
TDYa127cc1b4c32012-05-15 07:31:37 -07003945 if (!method_info_.need_shadow_frame) {
3946 return;
3947 }
Logan Chien8dfcbea2012-02-17 18:50:32 +08003948 irb_.CreateCall(irb_.GetRuntime(PopShadowFrame));
3949}
3950
3951
TDYa127c8dc1012012-04-19 07:03:33 -07003952void MethodCompiler::EmitUpdateDexPC(uint32_t dex_pc) {
TDYa127cc1b4c32012-05-15 07:31:37 -07003953 if (!method_info_.need_shadow_frame) {
3954 return;
3955 }
TDYa127c8dc1012012-04-19 07:03:33 -07003956 irb_.StoreToObjectOffset(shadow_frame_,
3957 ShadowFrame::DexPCOffset(),
TDYa127aba61122012-05-04 18:28:36 -07003958 irb_.getInt32(dex_pc),
TDYa127d955bec2012-05-11 10:54:02 -07003959 kTBAAShadowFrame);
Logan Chien8dfcbea2012-02-17 18:50:32 +08003960}
3961
3962
TDYa127cc1b4c32012-05-15 07:31:37 -07003963// TODO: Use high-level IR to do this
3964void MethodCompiler::ComputeMethodInfo() {
3965 // If this method is static, we set the "this" register index to -1. So we don't worry about this
3966 // method is static or not in the following comparison.
3967 int64_t this_reg_idx = (oat_compilation_unit_->IsStatic()) ?
3968 (-1) :
3969 (code_item_->registers_size_ - code_item_->ins_size_);
3970 bool has_invoke = false;
3971 bool may_have_loop = false;
3972 bool modify_this = false;
3973 bool may_throw_exception = false;
3974
3975 Instruction const* insn;
3976 for (uint32_t dex_pc = 0;
3977 dex_pc < code_item_->insns_size_in_code_units_;
3978 dex_pc += insn->SizeInCodeUnits()) {
3979 insn = Instruction::At(code_item_->insns_ + dex_pc);
3980 DecodedInstruction dec_insn(insn);
3981
3982 switch (insn->Opcode()) {
3983 case Instruction::NOP:
3984 break;
3985
3986 case Instruction::MOVE:
3987 case Instruction::MOVE_FROM16:
3988 case Instruction::MOVE_16:
3989 case Instruction::MOVE_WIDE:
3990 case Instruction::MOVE_WIDE_FROM16:
3991 case Instruction::MOVE_WIDE_16:
3992 case Instruction::MOVE_OBJECT:
3993 case Instruction::MOVE_OBJECT_FROM16:
3994 case Instruction::MOVE_OBJECT_16:
3995 case Instruction::MOVE_RESULT:
3996 case Instruction::MOVE_RESULT_WIDE:
3997 case Instruction::MOVE_RESULT_OBJECT:
3998 case Instruction::MOVE_EXCEPTION:
3999 if (dec_insn.vA == this_reg_idx) {
4000 modify_this = true;
4001 }
4002 break;
4003
4004 case Instruction::RETURN_VOID:
4005 case Instruction::RETURN:
4006 case Instruction::RETURN_WIDE:
4007 case Instruction::RETURN_OBJECT:
4008 break;
4009
4010 case Instruction::CONST_4:
4011 case Instruction::CONST_16:
4012 case Instruction::CONST:
4013 case Instruction::CONST_HIGH16:
4014 case Instruction::CONST_WIDE_16:
4015 case Instruction::CONST_WIDE_32:
4016 case Instruction::CONST_WIDE:
4017 case Instruction::CONST_WIDE_HIGH16:
4018 if (dec_insn.vA == this_reg_idx) {
4019 modify_this = true;
4020 }
4021 break;
4022
4023 case Instruction::CONST_STRING:
4024 case Instruction::CONST_STRING_JUMBO:
4025 // TODO: Will the ResolveString throw exception?
4026 if (!compiler_->CanAssumeStringIsPresentInDexCache(dex_cache_, dec_insn.vB)) {
4027 may_throw_exception = true;
4028 }
4029 if (dec_insn.vA == this_reg_idx) {
4030 modify_this = true;
4031 }
4032 break;
4033
4034 case Instruction::CONST_CLASS:
4035 may_throw_exception = true;
4036 if (dec_insn.vA == this_reg_idx) {
4037 modify_this = true;
4038 }
4039 break;
4040
4041 case Instruction::MONITOR_ENTER:
4042 case Instruction::MONITOR_EXIT:
4043 case Instruction::CHECK_CAST:
4044 may_throw_exception = true;
4045 break;
4046
4047 case Instruction::INSTANCE_OF:
4048 may_throw_exception = true;
4049 if (dec_insn.vA == this_reg_idx) {
4050 modify_this = true;
4051 }
4052 break;
4053
4054 case Instruction::ARRAY_LENGTH:
4055 if (dec_insn.vA == this_reg_idx) {
4056 modify_this = true;
4057 }
4058 break;
4059
4060 case Instruction::NEW_INSTANCE:
4061 case Instruction::NEW_ARRAY:
4062 may_throw_exception = true;
4063 if (dec_insn.vA == this_reg_idx) {
4064 modify_this = true;
4065 }
4066 break;
4067
4068 case Instruction::FILLED_NEW_ARRAY:
4069 case Instruction::FILLED_NEW_ARRAY_RANGE:
4070 case Instruction::FILL_ARRAY_DATA:
4071 case Instruction::THROW:
4072 may_throw_exception = true;
4073 break;
4074
4075 case Instruction::GOTO:
4076 case Instruction::GOTO_16:
4077 case Instruction::GOTO_32:
4078 {
4079 int32_t branch_offset = dec_insn.vA;
4080 if (branch_offset <= 0) {
4081 Instruction const* target = Instruction::At(code_item_->insns_ + dex_pc + branch_offset);
4082 // According to the statistics, there are a few methods have "fake" back edge, which means
4083 // the backedge is not for a loop.
4084 // The most "fake" edges in the small methods are just branches to a return instruction. So
4085 // we can do an simple check to avoid false loop detection. After we have a high-level IR
4086 // before IRBuilder, we should remove this trick.
4087 switch (target->Opcode()) {
4088 default:
4089 may_have_loop = true;
4090 break;
4091 case Instruction::RETURN_VOID:
4092 case Instruction::RETURN:
4093 case Instruction::RETURN_WIDE:
4094 case Instruction::RETURN_OBJECT:
4095 break;
4096 }
4097 }
4098 }
4099 break;
4100
4101 case Instruction::PACKED_SWITCH:
4102 case Instruction::SPARSE_SWITCH:
4103 break;
4104
4105 case Instruction::CMPL_FLOAT:
4106 case Instruction::CMPG_FLOAT:
4107 case Instruction::CMPL_DOUBLE:
4108 case Instruction::CMPG_DOUBLE:
4109 case Instruction::CMP_LONG:
4110 if (dec_insn.vA == this_reg_idx) {
4111 modify_this = true;
4112 }
4113 break;
4114
4115 case Instruction::IF_EQ:
4116 case Instruction::IF_NE:
4117 case Instruction::IF_LT:
4118 case Instruction::IF_GE:
4119 case Instruction::IF_GT:
4120 case Instruction::IF_LE:
4121 {
4122 int32_t branch_offset = dec_insn.vC;
4123 if (branch_offset <= 0) {
4124 Instruction const* target = Instruction::At(code_item_->insns_ + dex_pc + branch_offset);
4125 switch (target->Opcode()) {
4126 default:
4127 may_have_loop = true;
4128 break;
4129 case Instruction::RETURN_VOID:
4130 case Instruction::RETURN:
4131 case Instruction::RETURN_WIDE:
4132 case Instruction::RETURN_OBJECT:
4133 break;
4134 }
4135 }
4136 }
4137 break;
4138
4139 case Instruction::IF_EQZ:
4140 case Instruction::IF_NEZ:
4141 case Instruction::IF_LTZ:
4142 case Instruction::IF_GEZ:
4143 case Instruction::IF_GTZ:
4144 case Instruction::IF_LEZ:
4145 {
4146 int32_t branch_offset = dec_insn.vB;
4147 if (branch_offset <= 0) {
4148 Instruction const* target = Instruction::At(code_item_->insns_ + dex_pc + branch_offset);
4149 switch (target->Opcode()) {
4150 default:
4151 may_have_loop = true;
4152 break;
4153 case Instruction::RETURN_VOID:
4154 case Instruction::RETURN:
4155 case Instruction::RETURN_WIDE:
4156 case Instruction::RETURN_OBJECT:
4157 break;
4158 }
4159 }
4160 }
4161 break;
4162
4163 case Instruction::AGET:
4164 case Instruction::AGET_WIDE:
4165 case Instruction::AGET_OBJECT:
4166 case Instruction::AGET_BOOLEAN:
4167 case Instruction::AGET_BYTE:
4168 case Instruction::AGET_CHAR:
4169 case Instruction::AGET_SHORT:
4170 may_throw_exception = true;
4171 if (dec_insn.vA == this_reg_idx) {
4172 modify_this = true;
4173 }
4174 break;
4175
4176 case Instruction::APUT:
4177 case Instruction::APUT_WIDE:
4178 case Instruction::APUT_OBJECT:
4179 case Instruction::APUT_BOOLEAN:
4180 case Instruction::APUT_BYTE:
4181 case Instruction::APUT_CHAR:
4182 case Instruction::APUT_SHORT:
4183 may_throw_exception = true;
4184 break;
4185
4186 case Instruction::IGET:
4187 case Instruction::IGET_WIDE:
4188 case Instruction::IGET_OBJECT:
4189 case Instruction::IGET_BOOLEAN:
4190 case Instruction::IGET_BYTE:
4191 case Instruction::IGET_CHAR:
4192 case Instruction::IGET_SHORT:
4193 {
4194 if (dec_insn.vA == this_reg_idx) {
4195 modify_this = true;
4196 }
4197 uint32_t reg_idx = dec_insn.vB;
4198 uint32_t field_idx = dec_insn.vC;
4199 int field_offset;
4200 bool is_volatile;
4201 bool is_fast_path = compiler_->ComputeInstanceFieldInfo(
4202 field_idx, oat_compilation_unit_, field_offset, is_volatile, false);
4203 if (!is_fast_path) {
4204 may_throw_exception = true;
4205 } else if (reg_idx != this_reg_idx) {
4206 // NullPointerException
4207 may_throw_exception = true;
4208 }
4209 }
4210 break;
4211
4212 case Instruction::IPUT:
4213 case Instruction::IPUT_WIDE:
4214 case Instruction::IPUT_OBJECT:
4215 case Instruction::IPUT_BOOLEAN:
4216 case Instruction::IPUT_BYTE:
4217 case Instruction::IPUT_CHAR:
4218 case Instruction::IPUT_SHORT:
4219 {
4220 uint32_t reg_idx = dec_insn.vB;
4221 uint32_t field_idx = dec_insn.vC;
4222 int field_offset;
4223 bool is_volatile;
4224 bool is_fast_path = compiler_->ComputeInstanceFieldInfo(
4225 field_idx, oat_compilation_unit_, field_offset, is_volatile, true);
4226 if (!is_fast_path) {
4227 may_throw_exception = true;
4228 } else if (reg_idx != this_reg_idx) {
4229 // NullPointerException
4230 may_throw_exception = true;
4231 }
4232 }
4233 break;
4234
4235 case Instruction::SGET:
4236 case Instruction::SGET_WIDE:
4237 case Instruction::SGET_OBJECT:
4238 case Instruction::SGET_BOOLEAN:
4239 case Instruction::SGET_BYTE:
4240 case Instruction::SGET_CHAR:
4241 case Instruction::SGET_SHORT:
4242 {
4243 if (dec_insn.vA == this_reg_idx) {
4244 modify_this = true;
4245 }
4246 uint32_t field_idx = dec_insn.vB;
4247
4248 int field_offset;
4249 int ssb_index;
4250 bool is_referrers_class;
4251 bool is_volatile;
4252
4253 bool is_fast_path = compiler_->ComputeStaticFieldInfo(
4254 field_idx, oat_compilation_unit_, field_offset, ssb_index,
4255 is_referrers_class, is_volatile, false);
4256 if (!is_fast_path || !is_referrers_class) {
4257 may_throw_exception = true;
4258 }
4259 }
4260 break;
4261
4262 case Instruction::SPUT:
4263 case Instruction::SPUT_WIDE:
4264 case Instruction::SPUT_OBJECT:
4265 case Instruction::SPUT_BOOLEAN:
4266 case Instruction::SPUT_BYTE:
4267 case Instruction::SPUT_CHAR:
4268 case Instruction::SPUT_SHORT:
4269 {
4270 uint32_t field_idx = dec_insn.vB;
4271
4272 int field_offset;
4273 int ssb_index;
4274 bool is_referrers_class;
4275 bool is_volatile;
4276
4277 bool is_fast_path = compiler_->ComputeStaticFieldInfo(
4278 field_idx, oat_compilation_unit_, field_offset, ssb_index,
4279 is_referrers_class, is_volatile, true);
4280 if (!is_fast_path || !is_referrers_class) {
4281 may_throw_exception = true;
4282 }
4283 }
4284 break;
4285
4286
4287 case Instruction::INVOKE_VIRTUAL:
4288 case Instruction::INVOKE_SUPER:
4289 case Instruction::INVOKE_DIRECT:
4290 case Instruction::INVOKE_STATIC:
4291 case Instruction::INVOKE_INTERFACE:
4292 case Instruction::INVOKE_VIRTUAL_RANGE:
4293 case Instruction::INVOKE_SUPER_RANGE:
4294 case Instruction::INVOKE_DIRECT_RANGE:
4295 case Instruction::INVOKE_STATIC_RANGE:
4296 case Instruction::INVOKE_INTERFACE_RANGE:
4297 has_invoke = true;
4298 may_throw_exception = true;
4299 break;
4300
4301 case Instruction::NEG_INT:
4302 case Instruction::NOT_INT:
4303 case Instruction::NEG_LONG:
4304 case Instruction::NOT_LONG:
4305 case Instruction::NEG_FLOAT:
4306 case Instruction::NEG_DOUBLE:
4307 case Instruction::INT_TO_LONG:
4308 case Instruction::INT_TO_FLOAT:
4309 case Instruction::INT_TO_DOUBLE:
4310 case Instruction::LONG_TO_INT:
4311 case Instruction::LONG_TO_FLOAT:
4312 case Instruction::LONG_TO_DOUBLE:
4313 case Instruction::FLOAT_TO_INT:
4314 case Instruction::FLOAT_TO_LONG:
4315 case Instruction::FLOAT_TO_DOUBLE:
4316 case Instruction::DOUBLE_TO_INT:
4317 case Instruction::DOUBLE_TO_LONG:
4318 case Instruction::DOUBLE_TO_FLOAT:
4319 case Instruction::INT_TO_BYTE:
4320 case Instruction::INT_TO_CHAR:
4321 case Instruction::INT_TO_SHORT:
4322 case Instruction::ADD_INT:
4323 case Instruction::SUB_INT:
4324 case Instruction::MUL_INT:
4325 case Instruction::AND_INT:
4326 case Instruction::OR_INT:
4327 case Instruction::XOR_INT:
4328 case Instruction::SHL_INT:
4329 case Instruction::SHR_INT:
4330 case Instruction::USHR_INT:
4331 case Instruction::ADD_LONG:
4332 case Instruction::SUB_LONG:
4333 case Instruction::MUL_LONG:
4334 case Instruction::AND_LONG:
4335 case Instruction::OR_LONG:
4336 case Instruction::XOR_LONG:
4337 case Instruction::SHL_LONG:
4338 case Instruction::SHR_LONG:
4339 case Instruction::USHR_LONG:
4340 case Instruction::ADD_INT_2ADDR:
4341 case Instruction::SUB_INT_2ADDR:
4342 case Instruction::MUL_INT_2ADDR:
4343 case Instruction::AND_INT_2ADDR:
4344 case Instruction::OR_INT_2ADDR:
4345 case Instruction::XOR_INT_2ADDR:
4346 case Instruction::SHL_INT_2ADDR:
4347 case Instruction::SHR_INT_2ADDR:
4348 case Instruction::USHR_INT_2ADDR:
4349 case Instruction::ADD_LONG_2ADDR:
4350 case Instruction::SUB_LONG_2ADDR:
4351 case Instruction::MUL_LONG_2ADDR:
4352 case Instruction::AND_LONG_2ADDR:
4353 case Instruction::OR_LONG_2ADDR:
4354 case Instruction::XOR_LONG_2ADDR:
4355 case Instruction::SHL_LONG_2ADDR:
4356 case Instruction::SHR_LONG_2ADDR:
4357 case Instruction::USHR_LONG_2ADDR:
4358 if (dec_insn.vA == this_reg_idx) {
4359 modify_this = true;
4360 }
4361 break;
4362
4363 case Instruction::DIV_INT:
4364 case Instruction::REM_INT:
4365 case Instruction::DIV_LONG:
4366 case Instruction::REM_LONG:
4367 case Instruction::DIV_INT_2ADDR:
4368 case Instruction::REM_INT_2ADDR:
4369 case Instruction::DIV_LONG_2ADDR:
4370 case Instruction::REM_LONG_2ADDR:
4371 may_throw_exception = true;
4372 if (dec_insn.vA == this_reg_idx) {
4373 modify_this = true;
4374 }
4375 break;
4376
4377 case Instruction::ADD_FLOAT:
4378 case Instruction::SUB_FLOAT:
4379 case Instruction::MUL_FLOAT:
4380 case Instruction::DIV_FLOAT:
4381 case Instruction::REM_FLOAT:
4382 case Instruction::ADD_DOUBLE:
4383 case Instruction::SUB_DOUBLE:
4384 case Instruction::MUL_DOUBLE:
4385 case Instruction::DIV_DOUBLE:
4386 case Instruction::REM_DOUBLE:
4387 case Instruction::ADD_FLOAT_2ADDR:
4388 case Instruction::SUB_FLOAT_2ADDR:
4389 case Instruction::MUL_FLOAT_2ADDR:
4390 case Instruction::DIV_FLOAT_2ADDR:
4391 case Instruction::REM_FLOAT_2ADDR:
4392 case Instruction::ADD_DOUBLE_2ADDR:
4393 case Instruction::SUB_DOUBLE_2ADDR:
4394 case Instruction::MUL_DOUBLE_2ADDR:
4395 case Instruction::DIV_DOUBLE_2ADDR:
4396 case Instruction::REM_DOUBLE_2ADDR:
4397 if (dec_insn.vA == this_reg_idx) {
4398 modify_this = true;
4399 }
4400 break;
4401
4402 case Instruction::ADD_INT_LIT16:
4403 case Instruction::ADD_INT_LIT8:
4404 case Instruction::RSUB_INT:
4405 case Instruction::RSUB_INT_LIT8:
4406 case Instruction::MUL_INT_LIT16:
4407 case Instruction::MUL_INT_LIT8:
4408 case Instruction::AND_INT_LIT16:
4409 case Instruction::AND_INT_LIT8:
4410 case Instruction::OR_INT_LIT16:
4411 case Instruction::OR_INT_LIT8:
4412 case Instruction::XOR_INT_LIT16:
4413 case Instruction::XOR_INT_LIT8:
4414 case Instruction::SHL_INT_LIT8:
4415 case Instruction::SHR_INT_LIT8:
4416 case Instruction::USHR_INT_LIT8:
4417 if (dec_insn.vA == this_reg_idx) {
4418 modify_this = true;
4419 }
4420 break;
4421 case Instruction::DIV_INT_LIT16:
4422 case Instruction::DIV_INT_LIT8:
4423 case Instruction::REM_INT_LIT16:
4424 case Instruction::REM_INT_LIT8:
4425 if (dec_insn.vC == 0) {
4426 may_throw_exception = true;
4427 }
4428 if (dec_insn.vA == this_reg_idx) {
4429 modify_this = true;
4430 }
4431 break;
4432
4433 case Instruction::THROW_VERIFICATION_ERROR:
4434 may_throw_exception = true;
4435 break;
4436
4437 case Instruction::UNUSED_3E:
4438 case Instruction::UNUSED_3F:
4439 case Instruction::UNUSED_40:
4440 case Instruction::UNUSED_41:
4441 case Instruction::UNUSED_42:
4442 case Instruction::UNUSED_43:
4443 case Instruction::UNUSED_73:
4444 case Instruction::UNUSED_79:
4445 case Instruction::UNUSED_7A:
4446 case Instruction::UNUSED_E3:
4447 case Instruction::UNUSED_E4:
4448 case Instruction::UNUSED_E5:
4449 case Instruction::UNUSED_E6:
4450 case Instruction::UNUSED_E7:
4451 case Instruction::UNUSED_E8:
4452 case Instruction::UNUSED_E9:
4453 case Instruction::UNUSED_EA:
4454 case Instruction::UNUSED_EB:
4455 case Instruction::UNUSED_EC:
4456 case Instruction::UNUSED_EE:
4457 case Instruction::UNUSED_EF:
4458 case Instruction::UNUSED_F0:
4459 case Instruction::UNUSED_F1:
4460 case Instruction::UNUSED_F2:
4461 case Instruction::UNUSED_F3:
4462 case Instruction::UNUSED_F4:
4463 case Instruction::UNUSED_F5:
4464 case Instruction::UNUSED_F6:
4465 case Instruction::UNUSED_F7:
4466 case Instruction::UNUSED_F8:
4467 case Instruction::UNUSED_F9:
4468 case Instruction::UNUSED_FA:
4469 case Instruction::UNUSED_FB:
4470 case Instruction::UNUSED_FC:
4471 case Instruction::UNUSED_FD:
4472 case Instruction::UNUSED_FE:
4473 case Instruction::UNUSED_FF:
4474 LOG(FATAL) << "Dex file contains UNUSED bytecode: " << insn->Opcode();
4475 break;
4476 }
4477 }
4478
4479 method_info_.this_reg_idx = this_reg_idx;
4480 // According to the statistics, there are few methods that modify the "this" pointer. So this is a
4481 // simple way to avoid data flow analysis. After we have a high-level IR before IRBuilder, we
4482 // should remove this trick.
4483 method_info_.this_will_not_be_null = !modify_this;
4484 method_info_.has_invoke = has_invoke;
4485 // If this method has loop or invoke instruction, it may suspend. Thus we need a shadow frame entry
4486 // for GC.
4487 method_info_.need_shadow_frame_entry = has_invoke || may_have_loop;
4488 // If this method may throw an exception, we need a shadow frame for stack trace (dexpc).
4489 method_info_.need_shadow_frame = method_info_.need_shadow_frame_entry || may_throw_exception;
4490}
4491
4492
4493
Logan Chien83426162011-12-09 09:29:50 +08004494} // namespace compiler_llvm
4495} // namespace art