blob: 4070791061f176d83573f0b1594163a5c5b953f2 [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;
266 for (uint32_t i = 0, num_of_regs = code_item_->registers_size_; i < num_of_regs; ++i) {
267 if (IsRegCanBeObject(i)) {
268 reg_to_shadow_frame_index_[i] = sirt_size++;
269 }
270 }
Logan Chien8dfcbea2012-02-17 18:50:32 +0800271
272 llvm::StructType* shadow_frame_type = irb_.getShadowFrameTy(sirt_size);
273 shadow_frame_ = irb_.CreateAlloca(shadow_frame_type);
274
275 // Zero-initialization of the shadow frame
276 llvm::ConstantAggregateZero* zero_initializer =
277 llvm::ConstantAggregateZero::get(shadow_frame_type);
278
TDYa127d955bec2012-05-11 10:54:02 -0700279 irb_.CreateStore(zero_initializer, shadow_frame_, kTBAAShadowFrame);
Logan Chien8dfcbea2012-02-17 18:50:32 +0800280
TDYa127ee1f59b2012-04-25 00:56:40 -0700281 // Get method object
Logan Chien8dfcbea2012-02-17 18:50:32 +0800282 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
TDYa127ee1f59b2012-04-25 00:56:40 -0700283
284 // Store the method pointer
285 irb_.StoreToObjectOffset(shadow_frame_,
286 ShadowFrame::MethodOffset(),
TDYa127aba61122012-05-04 18:28:36 -0700287 method_object_addr,
TDYa127d955bec2012-05-11 10:54:02 -0700288 kTBAAShadowFrame);
Logan Chien8dfcbea2012-02-17 18:50:32 +0800289
290 // Store the number of the pointer slots
TDYa127ee1f59b2012-04-25 00:56:40 -0700291 irb_.StoreToObjectOffset(shadow_frame_,
292 ShadowFrame::NumberOfReferencesOffset(),
TDYa127aba61122012-05-04 18:28:36 -0700293 irb_.getJInt(sirt_size),
TDYa127d955bec2012-05-11 10:54:02 -0700294 kTBAAShadowFrame);
Logan Chien8dfcbea2012-02-17 18:50:32 +0800295
296 // Push the shadow frame
297 llvm::Value* shadow_frame_upcast =
298 irb_.CreateConstGEP2_32(shadow_frame_, 0, 0);
299
300 irb_.CreateCall(irb_.GetRuntime(PushShadowFrame), shadow_frame_upcast);
301}
302
303
Logan Chiend6ececa2011-12-27 16:20:15 +0800304void MethodCompiler::EmitPrologueAssignArgRegister() {
305 uint16_t arg_reg = code_item_->registers_size_ - code_item_->ins_size_;
306
307 llvm::Function::arg_iterator arg_iter(func_->arg_begin());
308 llvm::Function::arg_iterator arg_end(func_->arg_end());
309
Logan Chiendd361c92012-04-10 23:40:37 +0800310 uint32_t shorty_size = 0;
311 char const* shorty = oat_compilation_unit_->GetShorty(&shorty_size);
312 CHECK_GE(shorty_size, 1u);
Logan Chiend6ececa2011-12-27 16:20:15 +0800313
314 ++arg_iter; // skip method object
315
Logan Chiendd361c92012-04-10 23:40:37 +0800316 if (!oat_compilation_unit_->IsStatic()) {
Logan Chiend6ececa2011-12-27 16:20:15 +0800317 EmitStoreDalvikReg(arg_reg, kObject, kAccurate, arg_iter);
318 ++arg_iter;
319 ++arg_reg;
320 }
321
Logan Chiendd361c92012-04-10 23:40:37 +0800322 for (uint32_t i = 1; i < shorty_size; ++i, ++arg_iter) {
Logan Chiend6ececa2011-12-27 16:20:15 +0800323 EmitStoreDalvikReg(arg_reg, shorty[i], kAccurate, arg_iter);
324
325 ++arg_reg;
326 if (shorty[i] == 'J' || shorty[i] == 'D') {
327 // Wide types, such as long and double, are using a pair of registers
328 // to store the value, so we have to increase arg_reg again.
329 ++arg_reg;
330 }
331 }
332
333 DCHECK_EQ(arg_end, arg_iter);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800334}
335
336
Logan Chien83426162011-12-09 09:29:50 +0800337void MethodCompiler::EmitInstructions() {
Logan Chiend6c239a2011-12-23 15:11:45 +0800338 uint32_t dex_pc = 0;
339 while (dex_pc < code_item_->insns_size_in_code_units_) {
340 Instruction const* insn = Instruction::At(code_item_->insns_ + dex_pc);
341 EmitInstruction(dex_pc, insn);
342 dex_pc += insn->SizeInCodeUnits();
343 }
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800344}
345
346
Logan Chien83426162011-12-09 09:29:50 +0800347void MethodCompiler::EmitInstruction(uint32_t dex_pc,
348 Instruction const* insn) {
Logan Chiend6c239a2011-12-23 15:11:45 +0800349
350 // Set the IRBuilder insertion point
351 irb_.SetInsertPoint(GetBasicBlock(dex_pc));
352
Logan Chien70f94b42011-12-27 17:49:11 +0800353#define ARGS dex_pc, insn
354
355 // Dispatch the instruction
356 switch (insn->Opcode()) {
357 case Instruction::NOP:
358 EmitInsn_Nop(ARGS);
359 break;
360
361 case Instruction::MOVE:
362 case Instruction::MOVE_FROM16:
363 case Instruction::MOVE_16:
364 EmitInsn_Move(ARGS, kInt);
365 break;
366
367 case Instruction::MOVE_WIDE:
368 case Instruction::MOVE_WIDE_FROM16:
369 case Instruction::MOVE_WIDE_16:
370 EmitInsn_Move(ARGS, kLong);
371 break;
372
373 case Instruction::MOVE_OBJECT:
374 case Instruction::MOVE_OBJECT_FROM16:
375 case Instruction::MOVE_OBJECT_16:
376 EmitInsn_Move(ARGS, kObject);
377 break;
378
379 case Instruction::MOVE_RESULT:
380 EmitInsn_MoveResult(ARGS, kInt);
381 break;
382
383 case Instruction::MOVE_RESULT_WIDE:
384 EmitInsn_MoveResult(ARGS, kLong);
385 break;
386
387 case Instruction::MOVE_RESULT_OBJECT:
388 EmitInsn_MoveResult(ARGS, kObject);
389 break;
390
391 case Instruction::MOVE_EXCEPTION:
392 EmitInsn_MoveException(ARGS);
393 break;
394
395 case Instruction::RETURN_VOID:
396 EmitInsn_ReturnVoid(ARGS);
397 break;
398
399 case Instruction::RETURN:
400 case Instruction::RETURN_WIDE:
401 case Instruction::RETURN_OBJECT:
402 EmitInsn_Return(ARGS);
403 break;
404
405 case Instruction::CONST_4:
406 case Instruction::CONST_16:
407 case Instruction::CONST:
408 case Instruction::CONST_HIGH16:
409 EmitInsn_LoadConstant(ARGS, kInt);
410 break;
411
412 case Instruction::CONST_WIDE_16:
413 case Instruction::CONST_WIDE_32:
414 case Instruction::CONST_WIDE:
415 case Instruction::CONST_WIDE_HIGH16:
416 EmitInsn_LoadConstant(ARGS, kLong);
417 break;
418
419 case Instruction::CONST_STRING:
420 case Instruction::CONST_STRING_JUMBO:
421 EmitInsn_LoadConstantString(ARGS);
422 break;
423
424 case Instruction::CONST_CLASS:
425 EmitInsn_LoadConstantClass(ARGS);
426 break;
427
428 case Instruction::MONITOR_ENTER:
429 EmitInsn_MonitorEnter(ARGS);
430 break;
431
432 case Instruction::MONITOR_EXIT:
433 EmitInsn_MonitorExit(ARGS);
434 break;
435
436 case Instruction::CHECK_CAST:
437 EmitInsn_CheckCast(ARGS);
438 break;
439
440 case Instruction::INSTANCE_OF:
441 EmitInsn_InstanceOf(ARGS);
442 break;
443
444 case Instruction::ARRAY_LENGTH:
445 EmitInsn_ArrayLength(ARGS);
446 break;
447
448 case Instruction::NEW_INSTANCE:
449 EmitInsn_NewInstance(ARGS);
450 break;
451
452 case Instruction::NEW_ARRAY:
453 EmitInsn_NewArray(ARGS);
454 break;
455
456 case Instruction::FILLED_NEW_ARRAY:
457 EmitInsn_FilledNewArray(ARGS, false);
458 break;
459
460 case Instruction::FILLED_NEW_ARRAY_RANGE:
461 EmitInsn_FilledNewArray(ARGS, true);
462 break;
463
464 case Instruction::FILL_ARRAY_DATA:
465 EmitInsn_FillArrayData(ARGS);
466 break;
467
468 case Instruction::THROW:
469 EmitInsn_ThrowException(ARGS);
470 break;
471
472 case Instruction::GOTO:
473 case Instruction::GOTO_16:
474 case Instruction::GOTO_32:
475 EmitInsn_UnconditionalBranch(ARGS);
476 break;
477
478 case Instruction::PACKED_SWITCH:
479 EmitInsn_PackedSwitch(ARGS);
480 break;
481
482 case Instruction::SPARSE_SWITCH:
483 EmitInsn_SparseSwitch(ARGS);
484 break;
485
486 case Instruction::CMPL_FLOAT:
487 EmitInsn_FPCompare(ARGS, kFloat, false);
488 break;
489
490 case Instruction::CMPG_FLOAT:
491 EmitInsn_FPCompare(ARGS, kFloat, true);
492 break;
493
494 case Instruction::CMPL_DOUBLE:
495 EmitInsn_FPCompare(ARGS, kDouble, false);
496 break;
497
498 case Instruction::CMPG_DOUBLE:
499 EmitInsn_FPCompare(ARGS, kDouble, true);
500 break;
501
502 case Instruction::CMP_LONG:
503 EmitInsn_LongCompare(ARGS);
504 break;
505
506 case Instruction::IF_EQ:
507 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_EQ);
508 break;
509
510 case Instruction::IF_NE:
511 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_NE);
512 break;
513
514 case Instruction::IF_LT:
515 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_LT);
516 break;
517
518 case Instruction::IF_GE:
519 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_GE);
520 break;
521
522 case Instruction::IF_GT:
523 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_GT);
524 break;
525
526 case Instruction::IF_LE:
527 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_LE);
528 break;
529
530 case Instruction::IF_EQZ:
531 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_EQ);
532 break;
533
534 case Instruction::IF_NEZ:
535 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_NE);
536 break;
537
538 case Instruction::IF_LTZ:
539 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_LT);
540 break;
541
542 case Instruction::IF_GEZ:
543 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_GE);
544 break;
545
546 case Instruction::IF_GTZ:
547 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_GT);
548 break;
549
550 case Instruction::IF_LEZ:
551 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_LE);
552 break;
553
554 case Instruction::AGET:
555 EmitInsn_AGet(ARGS, kInt);
556 break;
557
558 case Instruction::AGET_WIDE:
559 EmitInsn_AGet(ARGS, kLong);
560 break;
561
562 case Instruction::AGET_OBJECT:
563 EmitInsn_AGet(ARGS, kObject);
564 break;
565
566 case Instruction::AGET_BOOLEAN:
567 EmitInsn_AGet(ARGS, kBoolean);
568 break;
569
570 case Instruction::AGET_BYTE:
571 EmitInsn_AGet(ARGS, kByte);
572 break;
573
574 case Instruction::AGET_CHAR:
575 EmitInsn_AGet(ARGS, kChar);
576 break;
577
578 case Instruction::AGET_SHORT:
579 EmitInsn_AGet(ARGS, kShort);
580 break;
581
582 case Instruction::APUT:
583 EmitInsn_APut(ARGS, kInt);
584 break;
585
586 case Instruction::APUT_WIDE:
587 EmitInsn_APut(ARGS, kLong);
588 break;
589
590 case Instruction::APUT_OBJECT:
591 EmitInsn_APut(ARGS, kObject);
592 break;
593
594 case Instruction::APUT_BOOLEAN:
595 EmitInsn_APut(ARGS, kBoolean);
596 break;
597
598 case Instruction::APUT_BYTE:
599 EmitInsn_APut(ARGS, kByte);
600 break;
601
602 case Instruction::APUT_CHAR:
603 EmitInsn_APut(ARGS, kChar);
604 break;
605
606 case Instruction::APUT_SHORT:
607 EmitInsn_APut(ARGS, kShort);
608 break;
609
610 case Instruction::IGET:
611 EmitInsn_IGet(ARGS, kInt);
612 break;
613
614 case Instruction::IGET_WIDE:
615 EmitInsn_IGet(ARGS, kLong);
616 break;
617
618 case Instruction::IGET_OBJECT:
619 EmitInsn_IGet(ARGS, kObject);
620 break;
621
622 case Instruction::IGET_BOOLEAN:
623 EmitInsn_IGet(ARGS, kBoolean);
624 break;
625
626 case Instruction::IGET_BYTE:
627 EmitInsn_IGet(ARGS, kByte);
628 break;
629
630 case Instruction::IGET_CHAR:
631 EmitInsn_IGet(ARGS, kChar);
632 break;
633
634 case Instruction::IGET_SHORT:
635 EmitInsn_IGet(ARGS, kShort);
636 break;
637
638 case Instruction::IPUT:
639 EmitInsn_IPut(ARGS, kInt);
640 break;
641
642 case Instruction::IPUT_WIDE:
643 EmitInsn_IPut(ARGS, kLong);
644 break;
645
646 case Instruction::IPUT_OBJECT:
647 EmitInsn_IPut(ARGS, kObject);
648 break;
649
650 case Instruction::IPUT_BOOLEAN:
651 EmitInsn_IPut(ARGS, kBoolean);
652 break;
653
654 case Instruction::IPUT_BYTE:
655 EmitInsn_IPut(ARGS, kByte);
656 break;
657
658 case Instruction::IPUT_CHAR:
659 EmitInsn_IPut(ARGS, kChar);
660 break;
661
662 case Instruction::IPUT_SHORT:
663 EmitInsn_IPut(ARGS, kShort);
664 break;
665
666 case Instruction::SGET:
667 EmitInsn_SGet(ARGS, kInt);
668 break;
669
670 case Instruction::SGET_WIDE:
671 EmitInsn_SGet(ARGS, kLong);
672 break;
673
674 case Instruction::SGET_OBJECT:
675 EmitInsn_SGet(ARGS, kObject);
676 break;
677
678 case Instruction::SGET_BOOLEAN:
679 EmitInsn_SGet(ARGS, kBoolean);
680 break;
681
682 case Instruction::SGET_BYTE:
683 EmitInsn_SGet(ARGS, kByte);
684 break;
685
686 case Instruction::SGET_CHAR:
687 EmitInsn_SGet(ARGS, kChar);
688 break;
689
690 case Instruction::SGET_SHORT:
691 EmitInsn_SGet(ARGS, kShort);
692 break;
693
694 case Instruction::SPUT:
695 EmitInsn_SPut(ARGS, kInt);
696 break;
697
698 case Instruction::SPUT_WIDE:
699 EmitInsn_SPut(ARGS, kLong);
700 break;
701
702 case Instruction::SPUT_OBJECT:
703 EmitInsn_SPut(ARGS, kObject);
704 break;
705
706 case Instruction::SPUT_BOOLEAN:
707 EmitInsn_SPut(ARGS, kBoolean);
708 break;
709
710 case Instruction::SPUT_BYTE:
711 EmitInsn_SPut(ARGS, kByte);
712 break;
713
714 case Instruction::SPUT_CHAR:
715 EmitInsn_SPut(ARGS, kChar);
716 break;
717
718 case Instruction::SPUT_SHORT:
719 EmitInsn_SPut(ARGS, kShort);
720 break;
721
722
723 case Instruction::INVOKE_VIRTUAL:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800724 EmitInsn_Invoke(ARGS, kVirtual, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800725 break;
726
727 case Instruction::INVOKE_SUPER:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800728 EmitInsn_Invoke(ARGS, kSuper, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800729 break;
730
731 case Instruction::INVOKE_DIRECT:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800732 EmitInsn_Invoke(ARGS, kDirect, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800733 break;
734
735 case Instruction::INVOKE_STATIC:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800736 EmitInsn_Invoke(ARGS, kStatic, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800737 break;
738
739 case Instruction::INVOKE_INTERFACE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800740 EmitInsn_Invoke(ARGS, kInterface, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800741 break;
742
743 case Instruction::INVOKE_VIRTUAL_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800744 EmitInsn_Invoke(ARGS, kVirtual, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800745 break;
746
747 case Instruction::INVOKE_SUPER_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800748 EmitInsn_Invoke(ARGS, kSuper, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800749 break;
750
751 case Instruction::INVOKE_DIRECT_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800752 EmitInsn_Invoke(ARGS, kDirect, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800753 break;
754
755 case Instruction::INVOKE_STATIC_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800756 EmitInsn_Invoke(ARGS, kStatic, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800757 break;
758
759 case Instruction::INVOKE_INTERFACE_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800760 EmitInsn_Invoke(ARGS, kInterface, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800761 break;
762
763 case Instruction::NEG_INT:
764 EmitInsn_Neg(ARGS, kInt);
765 break;
766
767 case Instruction::NOT_INT:
768 EmitInsn_Not(ARGS, kInt);
769 break;
770
771 case Instruction::NEG_LONG:
772 EmitInsn_Neg(ARGS, kLong);
773 break;
774
775 case Instruction::NOT_LONG:
776 EmitInsn_Not(ARGS, kLong);
777 break;
778
779 case Instruction::NEG_FLOAT:
780 EmitInsn_FNeg(ARGS, kFloat);
781 break;
782
783 case Instruction::NEG_DOUBLE:
784 EmitInsn_FNeg(ARGS, kDouble);
785 break;
786
787 case Instruction::INT_TO_LONG:
788 EmitInsn_SExt(ARGS);
789 break;
790
791 case Instruction::INT_TO_FLOAT:
792 EmitInsn_IntToFP(ARGS, kInt, kFloat);
793 break;
794
795 case Instruction::INT_TO_DOUBLE:
796 EmitInsn_IntToFP(ARGS, kInt, kDouble);
797 break;
798
799 case Instruction::LONG_TO_INT:
800 EmitInsn_Trunc(ARGS);
801 break;
802
803 case Instruction::LONG_TO_FLOAT:
804 EmitInsn_IntToFP(ARGS, kLong, kFloat);
805 break;
806
807 case Instruction::LONG_TO_DOUBLE:
808 EmitInsn_IntToFP(ARGS, kLong, kDouble);
809 break;
810
811 case Instruction::FLOAT_TO_INT:
jeffhao41005dd2012-05-09 17:58:52 -0700812 EmitInsn_FPToInt(ARGS, kFloat, kInt, art_f2i);
Logan Chien70f94b42011-12-27 17:49:11 +0800813 break;
814
815 case Instruction::FLOAT_TO_LONG:
jeffhao41005dd2012-05-09 17:58:52 -0700816 EmitInsn_FPToInt(ARGS, kFloat, kLong, art_f2l);
Logan Chien70f94b42011-12-27 17:49:11 +0800817 break;
818
819 case Instruction::FLOAT_TO_DOUBLE:
820 EmitInsn_FExt(ARGS);
821 break;
822
823 case Instruction::DOUBLE_TO_INT:
jeffhao41005dd2012-05-09 17:58:52 -0700824 EmitInsn_FPToInt(ARGS, kDouble, kInt, art_d2i);
Logan Chien70f94b42011-12-27 17:49:11 +0800825 break;
826
827 case Instruction::DOUBLE_TO_LONG:
jeffhao41005dd2012-05-09 17:58:52 -0700828 EmitInsn_FPToInt(ARGS, kDouble, kLong, art_d2l);
Logan Chien70f94b42011-12-27 17:49:11 +0800829 break;
830
831 case Instruction::DOUBLE_TO_FLOAT:
832 EmitInsn_FTrunc(ARGS);
833 break;
834
835 case Instruction::INT_TO_BYTE:
836 EmitInsn_TruncAndSExt(ARGS, 8);
837 break;
838
839 case Instruction::INT_TO_CHAR:
840 EmitInsn_TruncAndZExt(ARGS, 16);
841 break;
842
843 case Instruction::INT_TO_SHORT:
844 EmitInsn_TruncAndSExt(ARGS, 16);
845 break;
846
847 case Instruction::ADD_INT:
848 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kInt, false);
849 break;
850
851 case Instruction::SUB_INT:
852 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kInt, false);
853 break;
854
855 case Instruction::MUL_INT:
856 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kInt, false);
857 break;
858
859 case Instruction::DIV_INT:
860 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kInt, false);
861 break;
862
863 case Instruction::REM_INT:
864 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kInt, false);
865 break;
866
867 case Instruction::AND_INT:
868 EmitInsn_IntArithm(ARGS, kIntArithm_And, kInt, false);
869 break;
870
871 case Instruction::OR_INT:
872 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kInt, false);
873 break;
874
875 case Instruction::XOR_INT:
876 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kInt, false);
877 break;
878
879 case Instruction::SHL_INT:
Logan Chien5539ad02012-04-02 14:36:55 +0800880 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kInt, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800881 break;
882
883 case Instruction::SHR_INT:
Logan Chien5539ad02012-04-02 14:36:55 +0800884 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kInt, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800885 break;
886
887 case Instruction::USHR_INT:
Logan Chien5539ad02012-04-02 14:36:55 +0800888 EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kInt, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800889 break;
890
891 case Instruction::ADD_LONG:
892 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kLong, false);
893 break;
894
895 case Instruction::SUB_LONG:
896 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kLong, false);
897 break;
898
899 case Instruction::MUL_LONG:
900 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kLong, false);
901 break;
902
903 case Instruction::DIV_LONG:
904 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kLong, false);
905 break;
906
907 case Instruction::REM_LONG:
908 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kLong, false);
909 break;
910
911 case Instruction::AND_LONG:
912 EmitInsn_IntArithm(ARGS, kIntArithm_And, kLong, false);
913 break;
914
915 case Instruction::OR_LONG:
916 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kLong, false);
917 break;
918
919 case Instruction::XOR_LONG:
920 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kLong, false);
921 break;
922
923 case Instruction::SHL_LONG:
Logan Chien5539ad02012-04-02 14:36:55 +0800924 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kLong, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800925 break;
926
927 case Instruction::SHR_LONG:
Logan Chien5539ad02012-04-02 14:36:55 +0800928 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kLong, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800929 break;
930
931 case Instruction::USHR_LONG:
Logan Chien5539ad02012-04-02 14:36:55 +0800932 EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kLong, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800933 break;
934
935 case Instruction::ADD_FLOAT:
936 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kFloat, false);
937 break;
938
939 case Instruction::SUB_FLOAT:
940 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kFloat, false);
941 break;
942
943 case Instruction::MUL_FLOAT:
944 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kFloat, false);
945 break;
946
947 case Instruction::DIV_FLOAT:
948 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kFloat, false);
949 break;
950
951 case Instruction::REM_FLOAT:
952 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kFloat, false);
953 break;
954
955 case Instruction::ADD_DOUBLE:
956 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kDouble, false);
957 break;
958
959 case Instruction::SUB_DOUBLE:
960 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kDouble, false);
961 break;
962
963 case Instruction::MUL_DOUBLE:
964 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kDouble, false);
965 break;
966
967 case Instruction::DIV_DOUBLE:
968 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kDouble, false);
969 break;
970
971 case Instruction::REM_DOUBLE:
972 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kDouble, false);
973 break;
974
975 case Instruction::ADD_INT_2ADDR:
976 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kInt, true);
977 break;
978
979 case Instruction::SUB_INT_2ADDR:
980 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kInt, true);
981 break;
982
983 case Instruction::MUL_INT_2ADDR:
984 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kInt, true);
985 break;
986
987 case Instruction::DIV_INT_2ADDR:
988 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kInt, true);
989 break;
990
991 case Instruction::REM_INT_2ADDR:
992 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kInt, true);
993 break;
994
995 case Instruction::AND_INT_2ADDR:
996 EmitInsn_IntArithm(ARGS, kIntArithm_And, kInt, true);
997 break;
998
999 case Instruction::OR_INT_2ADDR:
1000 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kInt, true);
1001 break;
1002
1003 case Instruction::XOR_INT_2ADDR:
1004 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kInt, true);
1005 break;
1006
1007 case Instruction::SHL_INT_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001008 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kInt, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001009 break;
1010
1011 case Instruction::SHR_INT_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001012 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kInt, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001013 break;
1014
1015 case Instruction::USHR_INT_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001016 EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kInt, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001017 break;
1018
1019 case Instruction::ADD_LONG_2ADDR:
1020 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kLong, true);
1021 break;
1022
1023 case Instruction::SUB_LONG_2ADDR:
1024 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kLong, true);
1025 break;
1026
1027 case Instruction::MUL_LONG_2ADDR:
1028 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kLong, true);
1029 break;
1030
1031 case Instruction::DIV_LONG_2ADDR:
1032 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kLong, true);
1033 break;
1034
1035 case Instruction::REM_LONG_2ADDR:
1036 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kLong, true);
1037 break;
1038
1039 case Instruction::AND_LONG_2ADDR:
1040 EmitInsn_IntArithm(ARGS, kIntArithm_And, kLong, true);
1041 break;
1042
1043 case Instruction::OR_LONG_2ADDR:
1044 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kLong, true);
1045 break;
1046
1047 case Instruction::XOR_LONG_2ADDR:
1048 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kLong, true);
1049 break;
1050
1051 case Instruction::SHL_LONG_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001052 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kLong, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001053 break;
1054
1055 case Instruction::SHR_LONG_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001056 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kLong, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001057 break;
1058
1059 case Instruction::USHR_LONG_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001060 EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kLong, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001061 break;
1062
1063 case Instruction::ADD_FLOAT_2ADDR:
1064 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kFloat, true);
1065 break;
1066
1067 case Instruction::SUB_FLOAT_2ADDR:
1068 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kFloat, true);
1069 break;
1070
1071 case Instruction::MUL_FLOAT_2ADDR:
1072 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kFloat, true);
1073 break;
1074
1075 case Instruction::DIV_FLOAT_2ADDR:
1076 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kFloat, true);
1077 break;
1078
1079 case Instruction::REM_FLOAT_2ADDR:
1080 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kFloat, true);
1081 break;
1082
1083 case Instruction::ADD_DOUBLE_2ADDR:
1084 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kDouble, true);
1085 break;
1086
1087 case Instruction::SUB_DOUBLE_2ADDR:
1088 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kDouble, true);
1089 break;
1090
1091 case Instruction::MUL_DOUBLE_2ADDR:
1092 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kDouble, true);
1093 break;
1094
1095 case Instruction::DIV_DOUBLE_2ADDR:
1096 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kDouble, true);
1097 break;
1098
1099 case Instruction::REM_DOUBLE_2ADDR:
1100 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kDouble, true);
1101 break;
1102
1103 case Instruction::ADD_INT_LIT16:
1104 case Instruction::ADD_INT_LIT8:
1105 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Add);
1106 break;
1107
1108 case Instruction::RSUB_INT:
1109 case Instruction::RSUB_INT_LIT8:
1110 EmitInsn_RSubImmediate(ARGS);
1111 break;
1112
1113 case Instruction::MUL_INT_LIT16:
1114 case Instruction::MUL_INT_LIT8:
1115 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Mul);
1116 break;
1117
1118 case Instruction::DIV_INT_LIT16:
1119 case Instruction::DIV_INT_LIT8:
1120 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Div);
1121 break;
1122
1123 case Instruction::REM_INT_LIT16:
1124 case Instruction::REM_INT_LIT8:
1125 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Rem);
1126 break;
1127
1128 case Instruction::AND_INT_LIT16:
1129 case Instruction::AND_INT_LIT8:
1130 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_And);
1131 break;
1132
1133 case Instruction::OR_INT_LIT16:
1134 case Instruction::OR_INT_LIT8:
1135 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Or);
1136 break;
1137
1138 case Instruction::XOR_INT_LIT16:
1139 case Instruction::XOR_INT_LIT8:
1140 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Xor);
1141 break;
1142
1143 case Instruction::SHL_INT_LIT8:
Logan Chien5539ad02012-04-02 14:36:55 +08001144 EmitInsn_IntShiftArithmImmediate(ARGS, kIntArithm_Shl);
Logan Chien70f94b42011-12-27 17:49:11 +08001145 break;
1146
1147 case Instruction::SHR_INT_LIT8:
Logan Chien5539ad02012-04-02 14:36:55 +08001148 EmitInsn_IntShiftArithmImmediate(ARGS, kIntArithm_Shr);
Logan Chien70f94b42011-12-27 17:49:11 +08001149 break;
1150
1151 case Instruction::USHR_INT_LIT8:
Logan Chien5539ad02012-04-02 14:36:55 +08001152 EmitInsn_IntShiftArithmImmediate(ARGS, kIntArithm_UShr);
Logan Chien70f94b42011-12-27 17:49:11 +08001153 break;
1154
Logan Chien9e5f5c12012-04-10 13:51:45 +08001155 case Instruction::THROW_VERIFICATION_ERROR:
1156 EmitInsn_ThrowVerificationError(ARGS);
1157 break;
1158
Logan Chien70f94b42011-12-27 17:49:11 +08001159 case Instruction::UNUSED_3E:
1160 case Instruction::UNUSED_3F:
1161 case Instruction::UNUSED_40:
1162 case Instruction::UNUSED_41:
1163 case Instruction::UNUSED_42:
1164 case Instruction::UNUSED_43:
1165 case Instruction::UNUSED_73:
1166 case Instruction::UNUSED_79:
1167 case Instruction::UNUSED_7A:
1168 case Instruction::UNUSED_E3:
1169 case Instruction::UNUSED_E4:
1170 case Instruction::UNUSED_E5:
1171 case Instruction::UNUSED_E6:
1172 case Instruction::UNUSED_E7:
1173 case Instruction::UNUSED_E8:
1174 case Instruction::UNUSED_E9:
1175 case Instruction::UNUSED_EA:
1176 case Instruction::UNUSED_EB:
1177 case Instruction::UNUSED_EC:
Logan Chien70f94b42011-12-27 17:49:11 +08001178 case Instruction::UNUSED_EE:
1179 case Instruction::UNUSED_EF:
1180 case Instruction::UNUSED_F0:
1181 case Instruction::UNUSED_F1:
1182 case Instruction::UNUSED_F2:
1183 case Instruction::UNUSED_F3:
1184 case Instruction::UNUSED_F4:
1185 case Instruction::UNUSED_F5:
1186 case Instruction::UNUSED_F6:
1187 case Instruction::UNUSED_F7:
1188 case Instruction::UNUSED_F8:
1189 case Instruction::UNUSED_F9:
1190 case Instruction::UNUSED_FA:
1191 case Instruction::UNUSED_FB:
1192 case Instruction::UNUSED_FC:
1193 case Instruction::UNUSED_FD:
1194 case Instruction::UNUSED_FE:
1195 case Instruction::UNUSED_FF:
1196 LOG(FATAL) << "Dex file contains UNUSED bytecode: " << insn->Opcode();
1197 break;
1198 }
1199
1200#undef ARGS
1201}
1202
1203
1204void MethodCompiler::EmitInsn_Nop(uint32_t dex_pc,
1205 Instruction const* insn) {
Logan Chiene09a6b72011-12-27 17:50:21 +08001206
1207 uint16_t insn_signature = code_item_->insns_[dex_pc];
1208
1209 if (insn_signature == Instruction::kPackedSwitchSignature ||
1210 insn_signature == Instruction::kSparseSwitchSignature ||
1211 insn_signature == Instruction::kArrayDataSignature) {
1212 irb_.CreateUnreachable();
Elliott Hughesb25c3f62012-03-26 16:35:06 -07001213 } else {
Logan Chiene09a6b72011-12-27 17:50:21 +08001214 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1215 }
Shih-wei Liaod1fec812012-02-13 09:51:10 -08001216}
1217
1218
Logan Chien70f94b42011-12-27 17:49:11 +08001219void MethodCompiler::EmitInsn_Move(uint32_t dex_pc,
1220 Instruction const* insn,
1221 JType jty) {
Logan Chien48173132011-12-27 17:51:13 +08001222
Elliott Hughesadb8c672012-03-06 16:49:32 -08001223 DecodedInstruction dec_insn(insn);
Logan Chien48173132011-12-27 17:51:13 +08001224
Elliott Hughesadb8c672012-03-06 16:49:32 -08001225 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, jty, kReg);
1226 EmitStoreDalvikReg(dec_insn.vA, jty, kReg, src_value);
Logan Chien48173132011-12-27 17:51:13 +08001227
Logan Chien70f94b42011-12-27 17:49:11 +08001228 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1229}
1230
1231
1232void MethodCompiler::EmitInsn_MoveResult(uint32_t dex_pc,
1233 Instruction const* insn,
1234 JType jty) {
Logan Chien48173132011-12-27 17:51:13 +08001235
Elliott Hughesadb8c672012-03-06 16:49:32 -08001236 DecodedInstruction dec_insn(insn);
Logan Chien48173132011-12-27 17:51:13 +08001237
1238 llvm::Value* src_value = EmitLoadDalvikRetValReg(jty, kReg);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001239 EmitStoreDalvikReg(dec_insn.vA, jty, kReg, src_value);
Logan Chien48173132011-12-27 17:51:13 +08001240
Logan Chien70f94b42011-12-27 17:49:11 +08001241 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1242}
1243
1244
1245void MethodCompiler::EmitInsn_MoveException(uint32_t dex_pc,
1246 Instruction const* insn) {
Logan Chien3354cec2012-01-13 14:29:03 +08001247
Elliott Hughesadb8c672012-03-06 16:49:32 -08001248 DecodedInstruction dec_insn(insn);
Logan Chien3354cec2012-01-13 14:29:03 +08001249
TDYa127ee1f59b2012-04-25 00:56:40 -07001250 // Get thread
Logan Chien3354cec2012-01-13 14:29:03 +08001251 llvm::Value* thread_object_addr =
1252 irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1253
TDYa127ee1f59b2012-04-25 00:56:40 -07001254 // Get thread-local exception field address
1255 llvm::Value* exception_object_addr =
1256 irb_.LoadFromObjectOffset(thread_object_addr,
1257 Thread::ExceptionOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -07001258 irb_.getJObjectTy(),
TDYa1278ca10052012-05-05 19:57:06 -07001259 kTBAAJRuntime);
Logan Chien3354cec2012-01-13 14:29:03 +08001260
1261 // Set thread-local exception field address to NULL
TDYa127ee1f59b2012-04-25 00:56:40 -07001262 irb_.StoreToObjectOffset(thread_object_addr,
1263 Thread::ExceptionOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -07001264 irb_.getJNull(),
TDYa1278ca10052012-05-05 19:57:06 -07001265 kTBAAJRuntime);
Logan Chien3354cec2012-01-13 14:29:03 +08001266
1267 // Keep the exception object in the Dalvik register
Elliott Hughesadb8c672012-03-06 16:49:32 -08001268 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, exception_object_addr);
Logan Chien3354cec2012-01-13 14:29:03 +08001269
Logan Chien70f94b42011-12-27 17:49:11 +08001270 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1271}
1272
1273
1274void MethodCompiler::EmitInsn_ThrowException(uint32_t dex_pc,
1275 Instruction const* insn) {
Logan Chien6c6f12d2012-01-13 19:26:27 +08001276
Elliott Hughesadb8c672012-03-06 16:49:32 -08001277 DecodedInstruction dec_insn(insn);
Logan Chien6c6f12d2012-01-13 19:26:27 +08001278
1279 llvm::Value* exception_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001280 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chien6c6f12d2012-01-13 19:26:27 +08001281
TDYa127c8dc1012012-04-19 07:03:33 -07001282 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001283
Logan Chien6c6f12d2012-01-13 19:26:27 +08001284 irb_.CreateCall(irb_.GetRuntime(ThrowException), exception_addr);
1285
1286 EmitBranchExceptionLandingPad(dex_pc);
Logan Chien70f94b42011-12-27 17:49:11 +08001287}
1288
1289
Logan Chien9e5f5c12012-04-10 13:51:45 +08001290void MethodCompiler::EmitInsn_ThrowVerificationError(uint32_t dex_pc,
1291 Instruction const* insn) {
1292
1293 DecodedInstruction dec_insn(insn);
1294
TDYa127c8dc1012012-04-19 07:03:33 -07001295 EmitUpdateDexPC(dex_pc);
Logan Chien9e5f5c12012-04-10 13:51:45 +08001296
1297 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1298 llvm::Value* kind_value = irb_.getInt32(dec_insn.vA);
1299 llvm::Value* ref_value = irb_.getInt32(dec_insn.vB);
1300
1301 irb_.CreateCall3(irb_.GetRuntime(ThrowVerificationError),
1302 method_object_addr, kind_value, ref_value);
1303
1304 EmitBranchExceptionLandingPad(dex_pc);
1305}
1306
1307
Logan Chien70f94b42011-12-27 17:49:11 +08001308void MethodCompiler::EmitInsn_ReturnVoid(uint32_t dex_pc,
1309 Instruction const* insn) {
Logan Chien8898a272011-12-27 17:51:56 +08001310 // Garbage collection safe-point
1311 EmitGuard_GarbageCollectionSuspend(dex_pc);
1312
Logan Chien8dfcbea2012-02-17 18:50:32 +08001313 // Pop the shadow frame
1314 EmitPopShadowFrame();
1315
Logan Chien8898a272011-12-27 17:51:56 +08001316 // Return!
1317 irb_.CreateRetVoid();
Logan Chien70f94b42011-12-27 17:49:11 +08001318}
1319
1320
1321void MethodCompiler::EmitInsn_Return(uint32_t dex_pc,
1322 Instruction const* insn) {
Logan Chien8898a272011-12-27 17:51:56 +08001323
Elliott Hughesadb8c672012-03-06 16:49:32 -08001324 DecodedInstruction dec_insn(insn);
Logan Chien8898a272011-12-27 17:51:56 +08001325
1326 // Garbage collection safe-point
1327 EmitGuard_GarbageCollectionSuspend(dex_pc);
1328
Logan Chien8dfcbea2012-02-17 18:50:32 +08001329 // Pop the shadow frame
1330 EmitPopShadowFrame();
1331 // NOTE: It is important to keep this AFTER the GC safe-point. Otherwise,
1332 // the return value might be collected since the shadow stack is popped.
1333
Logan Chien8898a272011-12-27 17:51:56 +08001334 // Return!
Logan Chiendd361c92012-04-10 23:40:37 +08001335 char ret_shorty = oat_compilation_unit_->GetShorty()[0];
Elliott Hughesadb8c672012-03-06 16:49:32 -08001336 llvm::Value* retval = EmitLoadDalvikReg(dec_insn.vA, ret_shorty, kAccurate);
Logan Chien8898a272011-12-27 17:51:56 +08001337
1338 irb_.CreateRet(retval);
Logan Chien70f94b42011-12-27 17:49:11 +08001339}
1340
1341
1342void MethodCompiler::EmitInsn_LoadConstant(uint32_t dex_pc,
1343 Instruction const* insn,
1344 JType imm_jty) {
Shih-wei Liao798366e2012-02-16 09:25:33 -08001345
Elliott Hughesadb8c672012-03-06 16:49:32 -08001346 DecodedInstruction dec_insn(insn);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001347
1348 DCHECK(imm_jty == kInt || imm_jty == kLong) << imm_jty;
1349
1350 int64_t imm = 0;
1351
1352 switch (insn->Opcode()) {
1353 // 32-bit Immediate
1354 case Instruction::CONST_4:
1355 case Instruction::CONST_16:
1356 case Instruction::CONST:
1357 case Instruction::CONST_WIDE_16:
1358 case Instruction::CONST_WIDE_32:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001359 imm = static_cast<int64_t>(static_cast<int32_t>(dec_insn.vB));
Shih-wei Liao798366e2012-02-16 09:25:33 -08001360 break;
1361
1362 case Instruction::CONST_HIGH16:
1363 imm = static_cast<int64_t>(static_cast<int32_t>(
Elliott Hughesadb8c672012-03-06 16:49:32 -08001364 static_cast<uint32_t>(static_cast<uint16_t>(dec_insn.vB)) << 16));
Shih-wei Liao798366e2012-02-16 09:25:33 -08001365 break;
1366
1367 // 64-bit Immediate
1368 case Instruction::CONST_WIDE:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001369 imm = static_cast<int64_t>(dec_insn.vB_wide);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001370 break;
1371
1372 case Instruction::CONST_WIDE_HIGH16:
1373 imm = static_cast<int64_t>(
Elliott Hughesadb8c672012-03-06 16:49:32 -08001374 static_cast<uint64_t>(static_cast<uint16_t>(dec_insn.vB)) << 48);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001375 break;
1376
1377 // Unknown opcode for load constant (unreachable)
1378 default:
1379 LOG(FATAL) << "Unknown opcode for load constant: " << insn->Opcode();
1380 break;
1381 }
1382
1383 // Store the non-object register
1384 llvm::Type* imm_type = irb_.getJType(imm_jty, kAccurate);
1385 llvm::Constant* imm_value = llvm::ConstantInt::getSigned(imm_type, imm);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001386 EmitStoreDalvikReg(dec_insn.vA, imm_jty, kAccurate, imm_value);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001387
1388 // Store the object register if it is possible to be null.
1389 if (imm_jty == kInt && imm == 0) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001390 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, irb_.getJNull());
Shih-wei Liao798366e2012-02-16 09:25:33 -08001391 }
1392
Logan Chien70f94b42011-12-27 17:49:11 +08001393 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1394}
1395
1396
1397void MethodCompiler::EmitInsn_LoadConstantString(uint32_t dex_pc,
1398 Instruction const* insn) {
Logan Chienc3b4ba12012-01-16 19:52:53 +08001399
Elliott Hughesadb8c672012-03-06 16:49:32 -08001400 DecodedInstruction dec_insn(insn);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001401
Elliott Hughesadb8c672012-03-06 16:49:32 -08001402 uint32_t string_idx = dec_insn.vB;
Logan Chienc3b4ba12012-01-16 19:52:53 +08001403
1404 llvm::Value* string_field_addr = EmitLoadDexCacheStringFieldAddr(string_idx);
1405
TDYa1278ca10052012-05-05 19:57:06 -07001406 llvm::Value* string_addr = irb_.CreateLoad(string_field_addr, kTBAAJRuntime);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001407
1408 if (!compiler_->CanAssumeStringIsPresentInDexCache(dex_cache_, string_idx)) {
1409 llvm::BasicBlock* block_str_exist =
1410 CreateBasicBlockWithDexPC(dex_pc, "str_exist");
1411
1412 llvm::BasicBlock* block_str_resolve =
1413 CreateBasicBlockWithDexPC(dex_pc, "str_resolve");
1414
1415 // Test: Is the string resolved and in the dex cache?
1416 llvm::Value* equal_null = irb_.CreateICmpEQ(string_addr, irb_.getJNull());
1417
TDYa127ac7b5bb2012-05-11 13:17:49 -07001418 irb_.CreateCondBr(equal_null, block_str_resolve, block_str_exist, kUnlikely);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001419
1420 // String is resolved, go to next basic block.
1421 irb_.SetInsertPoint(block_str_exist);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001422 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, string_addr);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001423 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1424
1425 // String is not resolved yet, resolve it now.
1426 irb_.SetInsertPoint(block_str_resolve);
1427
1428 llvm::Function* runtime_func = irb_.GetRuntime(ResolveString);
1429
1430 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1431
1432 llvm::Value* string_idx_value = irb_.getInt32(string_idx);
1433
TDYa127c8dc1012012-04-19 07:03:33 -07001434 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001435
1436 string_addr = irb_.CreateCall2(runtime_func, method_object_addr,
1437 string_idx_value);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001438
1439 EmitGuard_ExceptionLandingPad(dex_pc);
1440 }
1441
1442 // Store the string object to the Dalvik register
Elliott Hughesadb8c672012-03-06 16:49:32 -08001443 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, string_addr);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001444
Logan Chien70f94b42011-12-27 17:49:11 +08001445 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1446}
1447
1448
Logan Chien27b30252012-01-14 03:43:35 +08001449llvm::Value* MethodCompiler::EmitLoadConstantClass(uint32_t dex_pc,
1450 uint32_t type_idx) {
1451 if (!compiler_->CanAccessTypeWithoutChecks(method_idx_, dex_cache_,
1452 *dex_file_, type_idx)) {
1453 llvm::Value* type_idx_value = irb_.getInt32(type_idx);
1454
1455 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1456
TDYa127706e9b62012-04-19 12:24:26 -07001457 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1458
Logan Chien27b30252012-01-14 03:43:35 +08001459 llvm::Function* runtime_func =
1460 irb_.GetRuntime(InitializeTypeAndVerifyAccess);
1461
TDYa127c8dc1012012-04-19 07:03:33 -07001462 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001463
Logan Chien27b30252012-01-14 03:43:35 +08001464 llvm::Value* type_object_addr =
TDYa127706e9b62012-04-19 12:24:26 -07001465 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
Logan Chien27b30252012-01-14 03:43:35 +08001466
1467 EmitGuard_ExceptionLandingPad(dex_pc);
1468
1469 return type_object_addr;
1470
1471 } else {
1472 // Try to load the class (type) object from the test cache.
1473 llvm::Value* type_field_addr =
1474 EmitLoadDexCacheResolvedTypeFieldAddr(type_idx);
1475
TDYa1278ca10052012-05-05 19:57:06 -07001476 llvm::Value* type_object_addr = irb_.CreateLoad(type_field_addr, kTBAAJRuntime);
Logan Chien27b30252012-01-14 03:43:35 +08001477
1478 if (compiler_->CanAssumeTypeIsPresentInDexCache(dex_cache_, type_idx)) {
1479 return type_object_addr;
1480 }
1481
1482 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
1483
1484 // Test whether class (type) object is in the dex cache or not
1485 llvm::Value* equal_null =
1486 irb_.CreateICmpEQ(type_object_addr, irb_.getJNull());
1487
1488 llvm::BasicBlock* block_cont =
1489 CreateBasicBlockWithDexPC(dex_pc, "cont");
1490
1491 llvm::BasicBlock* block_load_class =
1492 CreateBasicBlockWithDexPC(dex_pc, "load_class");
1493
TDYa127ac7b5bb2012-05-11 13:17:49 -07001494 irb_.CreateCondBr(equal_null, block_load_class, block_cont, kUnlikely);
Logan Chien27b30252012-01-14 03:43:35 +08001495
1496 // Failback routine to load the class object
1497 irb_.SetInsertPoint(block_load_class);
1498
1499 llvm::Function* runtime_func = irb_.GetRuntime(InitializeType);
1500
1501 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
1502
1503 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1504
TDYa127706e9b62012-04-19 12:24:26 -07001505 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1506
TDYa127c8dc1012012-04-19 07:03:33 -07001507 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001508
Logan Chien27b30252012-01-14 03:43:35 +08001509 llvm::Value* loaded_type_object_addr =
TDYa127706e9b62012-04-19 12:24:26 -07001510 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
Logan Chien27b30252012-01-14 03:43:35 +08001511
1512 EmitGuard_ExceptionLandingPad(dex_pc);
1513
1514 llvm::BasicBlock* block_after_load_class = irb_.GetInsertBlock();
1515
1516 irb_.CreateBr(block_cont);
1517
1518 // Now the class object must be loaded
1519 irb_.SetInsertPoint(block_cont);
1520
1521 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1522
1523 phi->addIncoming(type_object_addr, block_original);
1524 phi->addIncoming(loaded_type_object_addr, block_after_load_class);
1525
1526 return phi;
1527 }
1528}
1529
1530
Logan Chien70f94b42011-12-27 17:49:11 +08001531void MethodCompiler::EmitInsn_LoadConstantClass(uint32_t dex_pc,
1532 Instruction const* insn) {
Logan Chien27b30252012-01-14 03:43:35 +08001533
Elliott Hughesadb8c672012-03-06 16:49:32 -08001534 DecodedInstruction dec_insn(insn);
Logan Chien27b30252012-01-14 03:43:35 +08001535
Elliott Hughesadb8c672012-03-06 16:49:32 -08001536 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, dec_insn.vB);
1537 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, type_object_addr);
Logan Chien27b30252012-01-14 03:43:35 +08001538
Logan Chien70f94b42011-12-27 17:49:11 +08001539 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1540}
1541
1542
1543void MethodCompiler::EmitInsn_MonitorEnter(uint32_t dex_pc,
1544 Instruction const* insn) {
Logan Chien9e0dbe42012-01-13 12:11:37 +08001545
Elliott Hughesadb8c672012-03-06 16:49:32 -08001546 DecodedInstruction dec_insn(insn);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001547
1548 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001549 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001550
TDYa127cc1b4c32012-05-15 07:31:37 -07001551 if (!(method_info_.this_will_not_be_null && dec_insn.vA == method_info_.this_reg_idx)) {
1552 EmitGuard_NullPointerException(dex_pc, object_addr);
1553 }
Logan Chien9e0dbe42012-01-13 12:11:37 +08001554
TDYa127706e9b62012-04-19 12:24:26 -07001555 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1556
1557 irb_.CreateCall2(irb_.GetRuntime(LockObject), object_addr, thread_object_addr);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001558
Logan Chien70f94b42011-12-27 17:49:11 +08001559 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1560}
1561
1562
1563void MethodCompiler::EmitInsn_MonitorExit(uint32_t dex_pc,
1564 Instruction const* insn) {
Logan Chien9e0dbe42012-01-13 12:11:37 +08001565
Elliott Hughesadb8c672012-03-06 16:49:32 -08001566 DecodedInstruction dec_insn(insn);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001567
1568 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001569 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001570
TDYa127cc1b4c32012-05-15 07:31:37 -07001571 if (!(method_info_.this_will_not_be_null && dec_insn.vA == method_info_.this_reg_idx)) {
1572 EmitGuard_NullPointerException(dex_pc, object_addr);
1573 }
Logan Chien9e0dbe42012-01-13 12:11:37 +08001574
TDYa127c8dc1012012-04-19 07:03:33 -07001575 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001576
TDYa127706e9b62012-04-19 12:24:26 -07001577 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1578
1579 irb_.CreateCall2(irb_.GetRuntime(UnlockObject), object_addr, thread_object_addr);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001580 EmitGuard_ExceptionLandingPad(dex_pc);
1581
Logan Chien70f94b42011-12-27 17:49:11 +08001582 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1583}
1584
1585
1586void MethodCompiler::EmitInsn_CheckCast(uint32_t dex_pc,
1587 Instruction const* insn) {
Logan Chienfc880952012-01-15 23:53:10 +08001588
Elliott Hughesadb8c672012-03-06 16:49:32 -08001589 DecodedInstruction dec_insn(insn);
Logan Chienfc880952012-01-15 23:53:10 +08001590
1591 llvm::BasicBlock* block_test_class =
1592 CreateBasicBlockWithDexPC(dex_pc, "test_class");
1593
1594 llvm::BasicBlock* block_test_sub_class =
1595 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
1596
1597 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001598 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chienfc880952012-01-15 23:53:10 +08001599
1600 // Test: Is the reference equal to null? Act as no-op when it is null.
1601 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
1602
1603 irb_.CreateCondBr(equal_null,
1604 GetNextBasicBlock(dex_pc),
1605 block_test_class);
1606
1607 // Test: Is the object instantiated from the given class?
1608 irb_.SetInsertPoint(block_test_class);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001609 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, dec_insn.vB);
Logan Chienfc880952012-01-15 23:53:10 +08001610 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1611
1612 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
1613
1614 llvm::Value* object_type_field_addr =
1615 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
1616
1617 llvm::Value* object_type_object_addr =
TDYa127d3e24c22012-05-05 20:54:19 -07001618 irb_.CreateLoad(object_type_field_addr, kTBAAConstJObject);
Logan Chienfc880952012-01-15 23:53:10 +08001619
1620 llvm::Value* equal_class =
1621 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
1622
1623 irb_.CreateCondBr(equal_class,
1624 GetNextBasicBlock(dex_pc),
1625 block_test_sub_class);
1626
1627 // Test: Is the object instantiated from the subclass of the given class?
1628 irb_.SetInsertPoint(block_test_sub_class);
1629
TDYa127c8dc1012012-04-19 07:03:33 -07001630 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001631
Logan Chienfc880952012-01-15 23:53:10 +08001632 irb_.CreateCall2(irb_.GetRuntime(CheckCast),
1633 type_object_addr, object_type_object_addr);
1634
1635 EmitGuard_ExceptionLandingPad(dex_pc);
1636
Logan Chien70f94b42011-12-27 17:49:11 +08001637 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1638}
1639
1640
1641void MethodCompiler::EmitInsn_InstanceOf(uint32_t dex_pc,
1642 Instruction const* insn) {
Logan Chien68725e22012-01-15 22:25:34 +08001643
Elliott Hughesadb8c672012-03-06 16:49:32 -08001644 DecodedInstruction dec_insn(insn);
Logan Chien68725e22012-01-15 22:25:34 +08001645
1646 llvm::Constant* zero = irb_.getJInt(0);
1647 llvm::Constant* one = irb_.getJInt(1);
1648
1649 llvm::BasicBlock* block_nullp = CreateBasicBlockWithDexPC(dex_pc, "nullp");
1650
1651 llvm::BasicBlock* block_test_class =
1652 CreateBasicBlockWithDexPC(dex_pc, "test_class");
1653
1654 llvm::BasicBlock* block_class_equals =
1655 CreateBasicBlockWithDexPC(dex_pc, "class_eq");
1656
1657 llvm::BasicBlock* block_test_sub_class =
1658 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
1659
1660 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001661 EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
Logan Chien68725e22012-01-15 22:25:34 +08001662
1663 // Overview of the following code :
1664 // We check for null, if so, then false, otherwise check for class == . If so
1665 // then true, otherwise do callout slowpath.
1666 //
1667 // Test: Is the reference equal to null? Set 0 when it is null.
1668 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
1669
1670 irb_.CreateCondBr(equal_null, block_nullp, block_test_class);
1671
1672 irb_.SetInsertPoint(block_nullp);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001673 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, zero);
Logan Chien68725e22012-01-15 22:25:34 +08001674 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1675
1676 // Test: Is the object instantiated from the given class?
1677 irb_.SetInsertPoint(block_test_class);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001678 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, dec_insn.vC);
Logan Chien68725e22012-01-15 22:25:34 +08001679 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1680
1681 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
1682
1683 llvm::Value* object_type_field_addr =
1684 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
1685
1686 llvm::Value* object_type_object_addr =
TDYa127d3e24c22012-05-05 20:54:19 -07001687 irb_.CreateLoad(object_type_field_addr, kTBAAConstJObject);
Logan Chien68725e22012-01-15 22:25:34 +08001688
1689 llvm::Value* equal_class =
1690 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
1691
1692 irb_.CreateCondBr(equal_class, block_class_equals, block_test_sub_class);
1693
1694 irb_.SetInsertPoint(block_class_equals);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001695 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, one);
Logan Chien68725e22012-01-15 22:25:34 +08001696 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1697
1698 // Test: Is the object instantiated from the subclass of the given class?
1699 irb_.SetInsertPoint(block_test_sub_class);
1700
1701 llvm::Value* result =
1702 irb_.CreateCall2(irb_.GetRuntime(IsAssignable),
1703 type_object_addr, object_type_object_addr);
1704
Elliott Hughesadb8c672012-03-06 16:49:32 -08001705 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result);
Logan Chien68725e22012-01-15 22:25:34 +08001706
Logan Chien70f94b42011-12-27 17:49:11 +08001707 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1708}
1709
1710
Logan Chien61bb6142012-02-03 15:34:53 +08001711llvm::Value* MethodCompiler::EmitLoadArrayLength(llvm::Value* array) {
Logan Chien61bb6142012-02-03 15:34:53 +08001712 // Load array length
TDYa127ee1f59b2012-04-25 00:56:40 -07001713 return irb_.LoadFromObjectOffset(array,
1714 Array::LengthOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -07001715 irb_.getJIntTy(),
TDYa127d3e24c22012-05-05 20:54:19 -07001716 kTBAAConstJObject);
Logan Chien61bb6142012-02-03 15:34:53 +08001717}
1718
1719
Logan Chien70f94b42011-12-27 17:49:11 +08001720void MethodCompiler::EmitInsn_ArrayLength(uint32_t dex_pc,
1721 Instruction const* insn) {
Logan Chien61bb6142012-02-03 15:34:53 +08001722
Elliott Hughesadb8c672012-03-06 16:49:32 -08001723 DecodedInstruction dec_insn(insn);
Logan Chien61bb6142012-02-03 15:34:53 +08001724
1725 // Get the array object address
Elliott Hughesadb8c672012-03-06 16:49:32 -08001726 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
Logan Chien61bb6142012-02-03 15:34:53 +08001727 EmitGuard_NullPointerException(dex_pc, array_addr);
1728
1729 // Get the array length and store it to the register
1730 llvm::Value* array_len = EmitLoadArrayLength(array_addr);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001731 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, array_len);
Logan Chien61bb6142012-02-03 15:34:53 +08001732
Logan Chien70f94b42011-12-27 17:49:11 +08001733 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1734}
1735
1736
1737void MethodCompiler::EmitInsn_NewInstance(uint32_t dex_pc,
1738 Instruction const* insn) {
Logan Chien032bdad2012-01-16 09:59:23 +08001739
Elliott Hughesadb8c672012-03-06 16:49:32 -08001740 DecodedInstruction dec_insn(insn);
Logan Chien032bdad2012-01-16 09:59:23 +08001741
1742 llvm::Function* runtime_func;
Logan Chien1a032b12012-04-11 11:43:04 +08001743 if (compiler_->CanAccessInstantiableTypeWithoutChecks(
1744 method_idx_, dex_cache_, *dex_file_, dec_insn.vB)) {
Logan Chien032bdad2012-01-16 09:59:23 +08001745 runtime_func = irb_.GetRuntime(AllocObject);
1746 } else {
1747 runtime_func = irb_.GetRuntime(AllocObjectWithAccessCheck);
1748 }
1749
Elliott Hughesadb8c672012-03-06 16:49:32 -08001750 llvm::Constant* type_index_value = irb_.getInt32(dec_insn.vB);
Logan Chien032bdad2012-01-16 09:59:23 +08001751
1752 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1753
TDYa127da83d972012-04-18 00:21:49 -07001754 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1755
TDYa127c8dc1012012-04-19 07:03:33 -07001756 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001757
Logan Chien032bdad2012-01-16 09:59:23 +08001758 llvm::Value* object_addr =
TDYa127da83d972012-04-18 00:21:49 -07001759 irb_.CreateCall3(runtime_func, type_index_value, method_object_addr, thread_object_addr);
Logan Chien032bdad2012-01-16 09:59:23 +08001760
1761 EmitGuard_ExceptionLandingPad(dex_pc);
1762
Elliott Hughesadb8c672012-03-06 16:49:32 -08001763 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, object_addr);
Logan Chien032bdad2012-01-16 09:59:23 +08001764
Logan Chien70f94b42011-12-27 17:49:11 +08001765 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1766}
1767
1768
Logan Chiena2cc6a32012-01-16 10:38:41 +08001769llvm::Value* MethodCompiler::EmitAllocNewArray(uint32_t dex_pc,
1770 int32_t length,
1771 uint32_t type_idx,
1772 bool is_filled_new_array) {
1773 llvm::Function* runtime_func;
1774
1775 bool skip_access_check =
1776 compiler_->CanAccessTypeWithoutChecks(method_idx_, dex_cache_,
1777 *dex_file_, type_idx);
1778
TDYa127a849cb62012-04-01 05:59:34 -07001779 llvm::Value* array_length_value;
1780
Logan Chiena2cc6a32012-01-16 10:38:41 +08001781 if (is_filled_new_array) {
1782 runtime_func = skip_access_check ?
1783 irb_.GetRuntime(CheckAndAllocArray) :
1784 irb_.GetRuntime(CheckAndAllocArrayWithAccessCheck);
TDYa127a849cb62012-04-01 05:59:34 -07001785 array_length_value = irb_.getInt32(length);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001786 } else {
1787 runtime_func = skip_access_check ?
1788 irb_.GetRuntime(AllocArray) :
1789 irb_.GetRuntime(AllocArrayWithAccessCheck);
TDYa127a849cb62012-04-01 05:59:34 -07001790 array_length_value = EmitLoadDalvikReg(length, kInt, kAccurate);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001791 }
1792
1793 llvm::Constant* type_index_value = irb_.getInt32(type_idx);
1794
1795 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1796
TDYa127da83d972012-04-18 00:21:49 -07001797 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1798
TDYa127c8dc1012012-04-19 07:03:33 -07001799 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001800
Logan Chiena2cc6a32012-01-16 10:38:41 +08001801 llvm::Value* object_addr =
TDYa127da83d972012-04-18 00:21:49 -07001802 irb_.CreateCall4(runtime_func, type_index_value, method_object_addr,
1803 array_length_value, thread_object_addr);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001804
1805 EmitGuard_ExceptionLandingPad(dex_pc);
1806
1807 return object_addr;
1808}
1809
1810
Logan Chien70f94b42011-12-27 17:49:11 +08001811void MethodCompiler::EmitInsn_NewArray(uint32_t dex_pc,
1812 Instruction const* insn) {
Logan Chiena2cc6a32012-01-16 10:38:41 +08001813
Elliott Hughesadb8c672012-03-06 16:49:32 -08001814 DecodedInstruction dec_insn(insn);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001815
1816 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001817 EmitAllocNewArray(dex_pc, dec_insn.vB, dec_insn.vC, false);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001818
Elliott Hughesadb8c672012-03-06 16:49:32 -08001819 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, object_addr);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001820
Logan Chien70f94b42011-12-27 17:49:11 +08001821 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1822}
1823
1824
1825void MethodCompiler::EmitInsn_FilledNewArray(uint32_t dex_pc,
1826 Instruction const* insn,
1827 bool is_range) {
Logan Chiena85fb2f2012-01-16 12:52:56 +08001828
Elliott Hughesadb8c672012-03-06 16:49:32 -08001829 DecodedInstruction dec_insn(insn);
Logan Chiena85fb2f2012-01-16 12:52:56 +08001830
1831 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001832 EmitAllocNewArray(dex_pc, dec_insn.vA, dec_insn.vB, true);
Logan Chiena85fb2f2012-01-16 12:52:56 +08001833
Elliott Hughesadb8c672012-03-06 16:49:32 -08001834 if (dec_insn.vA > 0) {
Logan Chiendd361c92012-04-10 23:40:37 +08001835 // Resolve the element type
TDYa127183cf262012-04-11 07:53:21 -07001836 Class* klass = dex_cache_->GetResolvedType(dec_insn.vB)->GetComponentType();
Logan Chiendd361c92012-04-10 23:40:37 +08001837 // TODO: Avoid the usage of the dex_cache_. Try to figure out a better
1838 // way to distinguish [I and [L.
Logan Chiena85fb2f2012-01-16 12:52:56 +08001839 CHECK_NE(klass, static_cast<Class*>(NULL));
Logan Chiena85fb2f2012-01-16 12:52:56 +08001840
Logan Chiendd361c92012-04-10 23:40:37 +08001841 uint32_t alignment;
1842 llvm::Constant* elem_size;
1843 llvm::PointerType* field_type;
Logan Chiena85fb2f2012-01-16 12:52:56 +08001844
Logan Chiendd361c92012-04-10 23:40:37 +08001845 // NOTE: Currently filled-new-array only supports 'L', '[', and 'I'
1846 // as the element, thus we are only checking 2 cases: primitive int and
1847 // non-primitive type.
Logan Chiena85fb2f2012-01-16 12:52:56 +08001848 if (klass->IsPrimitiveInt()) {
Logan Chiendd361c92012-04-10 23:40:37 +08001849 alignment = sizeof(int32_t);
1850 elem_size = irb_.getPtrEquivInt(sizeof(int32_t));
Logan Chiena85fb2f2012-01-16 12:52:56 +08001851 field_type = irb_.getJIntTy()->getPointerTo();
1852 } else {
1853 CHECK(!klass->IsPrimitive());
Logan Chiendd361c92012-04-10 23:40:37 +08001854 alignment = irb_.getSizeOfPtrEquivInt();
1855 elem_size = irb_.getSizeOfPtrEquivIntValue();
Logan Chiena85fb2f2012-01-16 12:52:56 +08001856 field_type = irb_.getJObjectTy()->getPointerTo();
1857 }
1858
Logan Chiendd361c92012-04-10 23:40:37 +08001859 llvm::Value* data_field_offset =
1860 irb_.getPtrEquivInt(Array::DataOffset(alignment).Int32Value());
1861
1862 llvm::Value* data_field_addr =
1863 irb_.CreatePtrDisp(object_addr, data_field_offset, field_type);
1864
Logan Chiena85fb2f2012-01-16 12:52:56 +08001865 // TODO: Tune this code. Currently we are generating one instruction for
1866 // one element which may be very space consuming. Maybe changing to use
1867 // memcpy may help; however, since we can't guarantee that the alloca of
1868 // dalvik register are continuous, we can't perform such optimization yet.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001869 for (uint32_t i = 0; i < dec_insn.vA; ++i) {
Logan Chiena85fb2f2012-01-16 12:52:56 +08001870 int reg_index;
1871 if (is_range) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001872 reg_index = dec_insn.vC + i;
Logan Chiena85fb2f2012-01-16 12:52:56 +08001873 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001874 reg_index = dec_insn.arg[i];
Logan Chiena85fb2f2012-01-16 12:52:56 +08001875 }
1876
1877 llvm::Value* reg_value;
1878 if (klass->IsPrimitiveInt()) {
1879 reg_value = EmitLoadDalvikReg(reg_index, kInt, kAccurate);
1880 } else {
1881 reg_value = EmitLoadDalvikReg(reg_index, kObject, kAccurate);
1882 }
1883
TDYa127706e7db2012-05-06 00:05:33 -07001884 irb_.CreateStore(reg_value, data_field_addr, kTBAAHeapArray);
Logan Chiena85fb2f2012-01-16 12:52:56 +08001885
Logan Chiendd361c92012-04-10 23:40:37 +08001886 data_field_addr =
1887 irb_.CreatePtrDisp(data_field_addr, elem_size, field_type);
Logan Chiena85fb2f2012-01-16 12:52:56 +08001888 }
1889 }
1890
1891 EmitStoreDalvikRetValReg(kObject, kAccurate, object_addr);
1892
Logan Chien70f94b42011-12-27 17:49:11 +08001893 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1894}
1895
1896
1897void MethodCompiler::EmitInsn_FillArrayData(uint32_t dex_pc,
1898 Instruction const* insn) {
Logan Chiene58b6582012-01-16 17:13:13 +08001899
Elliott Hughesadb8c672012-03-06 16:49:32 -08001900 DecodedInstruction dec_insn(insn);
Logan Chiene58b6582012-01-16 17:13:13 +08001901
1902 // Read the payload
Logan Chiene58b6582012-01-16 17:13:13 +08001903 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
Elliott Hughesadb8c672012-03-06 16:49:32 -08001904 static_cast<int32_t>(dec_insn.vB);
Logan Chiene58b6582012-01-16 17:13:13 +08001905
Logan Chien19c350a2012-05-01 19:21:32 +08001906 const Instruction::ArrayDataPayload* payload =
1907 reinterpret_cast<const Instruction::ArrayDataPayload*>(
1908 code_item_->insns_ + payload_offset);
Logan Chiene58b6582012-01-16 17:13:13 +08001909
Logan Chien86f50672012-04-24 13:08:45 +08001910 // Load array object
Elliott Hughesadb8c672012-03-06 16:49:32 -08001911 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiene58b6582012-01-16 17:13:13 +08001912
Logan Chien86f50672012-04-24 13:08:45 +08001913 if (payload->element_count == 0) {
1914 // When the number of the elements in the payload is zero, we don't have
1915 // to copy any numbers. However, we should check whether the array object
1916 // address is equal to null or not.
1917 EmitGuard_NullPointerException(dex_pc, array_addr);
1918 } else {
1919 // To save the code size, we are going to call the runtime function to
1920 // copy the content from DexFile.
Logan Chiene58b6582012-01-16 17:13:13 +08001921
Logan Chien86f50672012-04-24 13:08:45 +08001922 // NOTE: We will check for the NullPointerException in the runtime.
Logan Chiene58b6582012-01-16 17:13:13 +08001923
Logan Chien86f50672012-04-24 13:08:45 +08001924 llvm::Function* runtime_func = irb_.GetRuntime(FillArrayData);
Logan Chiene58b6582012-01-16 17:13:13 +08001925
Logan Chien86f50672012-04-24 13:08:45 +08001926 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
Logan Chiene58b6582012-01-16 17:13:13 +08001927
Logan Chien86f50672012-04-24 13:08:45 +08001928 EmitUpdateDexPC(dex_pc);
Logan Chiene58b6582012-01-16 17:13:13 +08001929
Logan Chien86f50672012-04-24 13:08:45 +08001930 irb_.CreateCall4(runtime_func,
1931 method_object_addr, irb_.getInt32(dex_pc),
1932 array_addr, irb_.getInt32(payload_offset));
Logan Chiene58b6582012-01-16 17:13:13 +08001933
Logan Chien86f50672012-04-24 13:08:45 +08001934 EmitGuard_ExceptionLandingPad(dex_pc);
Logan Chiene58b6582012-01-16 17:13:13 +08001935 }
1936
Logan Chien70f94b42011-12-27 17:49:11 +08001937 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1938}
1939
1940
1941void MethodCompiler::EmitInsn_UnconditionalBranch(uint32_t dex_pc,
1942 Instruction const* insn) {
Logan Chiena466c162011-12-27 17:55:46 +08001943
Elliott Hughesadb8c672012-03-06 16:49:32 -08001944 DecodedInstruction dec_insn(insn);
Logan Chiena466c162011-12-27 17:55:46 +08001945
Elliott Hughesadb8c672012-03-06 16:49:32 -08001946 int32_t branch_offset = dec_insn.vA;
Logan Chiena466c162011-12-27 17:55:46 +08001947
Logan Chiena466c162011-12-27 17:55:46 +08001948 irb_.CreateBr(GetBasicBlock(dex_pc + branch_offset));
Logan Chien70f94b42011-12-27 17:49:11 +08001949}
1950
1951
1952void MethodCompiler::EmitInsn_PackedSwitch(uint32_t dex_pc,
1953 Instruction const* insn) {
Logan Chien7a89b6d2011-12-27 17:56:56 +08001954
Elliott Hughesadb8c672012-03-06 16:49:32 -08001955 DecodedInstruction dec_insn(insn);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001956
Logan Chien7a89b6d2011-12-27 17:56:56 +08001957 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
Elliott Hughesadb8c672012-03-06 16:49:32 -08001958 static_cast<int32_t>(dec_insn.vB);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001959
Logan Chien19c350a2012-05-01 19:21:32 +08001960 const Instruction::PackedSwitchPayload* payload =
1961 reinterpret_cast<const Instruction::PackedSwitchPayload*>(
1962 code_item_->insns_ + payload_offset);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001963
Elliott Hughesadb8c672012-03-06 16:49:32 -08001964 llvm::Value* value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001965
1966 llvm::SwitchInst* sw =
Logan Chien19c350a2012-05-01 19:21:32 +08001967 irb_.CreateSwitch(value, GetNextBasicBlock(dex_pc), payload->case_count);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001968
Logan Chien19c350a2012-05-01 19:21:32 +08001969 for (uint16_t i = 0; i < payload->case_count; ++i) {
1970 sw->addCase(irb_.getInt32(payload->first_key + i),
1971 GetBasicBlock(dex_pc + payload->targets[i]));
Logan Chien7a89b6d2011-12-27 17:56:56 +08001972 }
Logan Chien70f94b42011-12-27 17:49:11 +08001973}
1974
1975
1976void MethodCompiler::EmitInsn_SparseSwitch(uint32_t dex_pc,
1977 Instruction const* insn) {
Logan Chien7a89b6d2011-12-27 17:56:56 +08001978
Elliott Hughesadb8c672012-03-06 16:49:32 -08001979 DecodedInstruction dec_insn(insn);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001980
Logan Chien7a89b6d2011-12-27 17:56:56 +08001981 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
Elliott Hughesadb8c672012-03-06 16:49:32 -08001982 static_cast<int32_t>(dec_insn.vB);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001983
Logan Chien19c350a2012-05-01 19:21:32 +08001984 const Instruction::SparseSwitchPayload* payload =
1985 reinterpret_cast<const Instruction::SparseSwitchPayload*>(
1986 code_item_->insns_ + payload_offset);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001987
Logan Chien19c350a2012-05-01 19:21:32 +08001988 const int32_t* keys = payload->GetKeys();
1989 const int32_t* targets = payload->GetTargets();
Logan Chien7a89b6d2011-12-27 17:56:56 +08001990
Elliott Hughesadb8c672012-03-06 16:49:32 -08001991 llvm::Value* value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001992
1993 llvm::SwitchInst* sw =
Logan Chien19c350a2012-05-01 19:21:32 +08001994 irb_.CreateSwitch(value, GetNextBasicBlock(dex_pc), payload->case_count);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001995
Logan Chien19c350a2012-05-01 19:21:32 +08001996 for (size_t i = 0; i < payload->case_count; ++i) {
Logan Chien7a89b6d2011-12-27 17:56:56 +08001997 sw->addCase(irb_.getInt32(keys[i]), GetBasicBlock(dex_pc + targets[i]));
1998 }
Logan Chien70f94b42011-12-27 17:49:11 +08001999}
2000
2001
2002void MethodCompiler::EmitInsn_FPCompare(uint32_t dex_pc,
2003 Instruction const* insn,
2004 JType fp_jty,
2005 bool gt_bias) {
Logan Chien2c37e8e2011-12-27 17:58:46 +08002006
Elliott Hughesadb8c672012-03-06 16:49:32 -08002007 DecodedInstruction dec_insn(insn);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002008
2009 DCHECK(fp_jty == kFloat || fp_jty == kDouble) << "JType: " << fp_jty;
2010
Elliott Hughesadb8c672012-03-06 16:49:32 -08002011 llvm::Value* src1_value = EmitLoadDalvikReg(dec_insn.vB, fp_jty, kAccurate);
2012 llvm::Value* src2_value = EmitLoadDalvikReg(dec_insn.vC, fp_jty, kAccurate);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002013
2014 llvm::Value* cmp_eq = irb_.CreateFCmpOEQ(src1_value, src2_value);
2015 llvm::Value* cmp_lt;
2016
2017 if (gt_bias) {
2018 cmp_lt = irb_.CreateFCmpOLT(src1_value, src2_value);
2019 } else {
2020 cmp_lt = irb_.CreateFCmpULT(src1_value, src2_value);
2021 }
2022
2023 llvm::Value* result = EmitCompareResultSelection(cmp_eq, cmp_lt);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002024 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002025
Logan Chien70f94b42011-12-27 17:49:11 +08002026 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2027}
2028
2029
2030void MethodCompiler::EmitInsn_LongCompare(uint32_t dex_pc,
2031 Instruction const* insn) {
Logan Chien2c37e8e2011-12-27 17:58:46 +08002032
Elliott Hughesadb8c672012-03-06 16:49:32 -08002033 DecodedInstruction dec_insn(insn);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002034
Elliott Hughesadb8c672012-03-06 16:49:32 -08002035 llvm::Value* src1_value = EmitLoadDalvikReg(dec_insn.vB, kLong, kAccurate);
2036 llvm::Value* src2_value = EmitLoadDalvikReg(dec_insn.vC, kLong, kAccurate);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002037
2038 llvm::Value* cmp_eq = irb_.CreateICmpEQ(src1_value, src2_value);
2039 llvm::Value* cmp_lt = irb_.CreateICmpSLT(src1_value, src2_value);
2040
2041 llvm::Value* result = EmitCompareResultSelection(cmp_eq, cmp_lt);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002042 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002043
Logan Chien70f94b42011-12-27 17:49:11 +08002044 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2045}
2046
2047
Logan Chien2c37e8e2011-12-27 17:58:46 +08002048llvm::Value* MethodCompiler::EmitCompareResultSelection(llvm::Value* cmp_eq,
2049 llvm::Value* cmp_lt) {
2050
2051 llvm::Constant* zero = irb_.getJInt(0);
2052 llvm::Constant* pos1 = irb_.getJInt(1);
2053 llvm::Constant* neg1 = irb_.getJInt(-1);
2054
2055 llvm::Value* result_lt = irb_.CreateSelect(cmp_lt, neg1, pos1);
2056 llvm::Value* result_eq = irb_.CreateSelect(cmp_eq, zero, result_lt);
2057
2058 return result_eq;
2059}
2060
2061
Logan Chien70f94b42011-12-27 17:49:11 +08002062void MethodCompiler::EmitInsn_BinaryConditionalBranch(uint32_t dex_pc,
2063 Instruction const* insn,
2064 CondBranchKind cond) {
Logan Chiena78e3c82011-12-27 17:59:35 +08002065
Elliott Hughesadb8c672012-03-06 16:49:32 -08002066 DecodedInstruction dec_insn(insn);
Logan Chiena78e3c82011-12-27 17:59:35 +08002067
Elliott Hughesadb8c672012-03-06 16:49:32 -08002068 int8_t src1_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vA);
2069 int8_t src2_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vB);
Logan Chiena78e3c82011-12-27 17:59:35 +08002070
2071 DCHECK_NE(kRegUnknown, src1_reg_cat);
2072 DCHECK_NE(kRegUnknown, src2_reg_cat);
2073 DCHECK_NE(kRegCat2, src1_reg_cat);
2074 DCHECK_NE(kRegCat2, src2_reg_cat);
2075
Elliott Hughesadb8c672012-03-06 16:49:32 -08002076 int32_t branch_offset = dec_insn.vC;
Logan Chiena78e3c82011-12-27 17:59:35 +08002077
Logan Chiena78e3c82011-12-27 17:59:35 +08002078 llvm::Value* src1_value;
2079 llvm::Value* src2_value;
2080
TDYa1278e9b4492012-04-24 15:50:27 -07002081 if (src1_reg_cat == kRegZero && src2_reg_cat == kRegZero) {
2082 src1_value = irb_.getInt32(0);
2083 src2_value = irb_.getInt32(0);
2084 } else if (src1_reg_cat != kRegZero && src2_reg_cat != kRegZero) {
Logan Chiena78e3c82011-12-27 17:59:35 +08002085 CHECK_EQ(src1_reg_cat, src2_reg_cat);
2086
2087 if (src1_reg_cat == kRegCat1nr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002088 src1_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
2089 src2_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002090 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002091 src1_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
2092 src2_value = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002093 }
2094 } else {
2095 DCHECK(src1_reg_cat == kRegZero ||
2096 src2_reg_cat == kRegZero);
2097
2098 if (src1_reg_cat == kRegZero) {
2099 if (src2_reg_cat == kRegCat1nr) {
2100 src1_value = irb_.getJInt(0);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002101 src2_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002102 } else {
2103 src1_value = irb_.getJNull();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002104 src2_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002105 }
2106 } else { // src2_reg_cat == kRegZero
2107 if (src2_reg_cat == kRegCat1nr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002108 src1_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002109 src2_value = irb_.getJInt(0);
2110 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002111 src1_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002112 src2_value = irb_.getJNull();
2113 }
2114 }
2115 }
2116
2117 llvm::Value* cond_value =
2118 EmitConditionResult(src1_value, src2_value, cond);
2119
2120 irb_.CreateCondBr(cond_value,
2121 GetBasicBlock(dex_pc + branch_offset),
2122 GetNextBasicBlock(dex_pc));
Logan Chien70f94b42011-12-27 17:49:11 +08002123}
2124
2125
2126void MethodCompiler::EmitInsn_UnaryConditionalBranch(uint32_t dex_pc,
2127 Instruction const* insn,
2128 CondBranchKind cond) {
Logan Chiena78e3c82011-12-27 17:59:35 +08002129
Elliott Hughesadb8c672012-03-06 16:49:32 -08002130 DecodedInstruction dec_insn(insn);
Logan Chiena78e3c82011-12-27 17:59:35 +08002131
Elliott Hughesadb8c672012-03-06 16:49:32 -08002132 int8_t src_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vA);
Logan Chiena78e3c82011-12-27 17:59:35 +08002133
2134 DCHECK_NE(kRegUnknown, src_reg_cat);
2135 DCHECK_NE(kRegCat2, src_reg_cat);
2136
Elliott Hughesadb8c672012-03-06 16:49:32 -08002137 int32_t branch_offset = dec_insn.vB;
Logan Chiena78e3c82011-12-27 17:59:35 +08002138
Logan Chiena78e3c82011-12-27 17:59:35 +08002139 llvm::Value* src1_value;
2140 llvm::Value* src2_value;
2141
TDYa1278e9b4492012-04-24 15:50:27 -07002142 if (src_reg_cat == kRegZero) {
2143 src1_value = irb_.getInt32(0);
2144 src2_value = irb_.getInt32(0);
2145 } else if (src_reg_cat == kRegCat1nr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002146 src1_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002147 src2_value = irb_.getInt32(0);
2148 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002149 src1_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002150 src2_value = irb_.getJNull();
2151 }
2152
2153 llvm::Value* cond_value =
2154 EmitConditionResult(src1_value, src2_value, cond);
2155
2156 irb_.CreateCondBr(cond_value,
2157 GetBasicBlock(dex_pc + branch_offset),
2158 GetNextBasicBlock(dex_pc));
2159}
2160
TDYa1271d7e5102012-05-13 09:27:05 -07002161InferredRegCategoryMap const* MethodCompiler::GetInferredRegCategoryMap() {
Logan Chiendd361c92012-04-10 23:40:37 +08002162 Compiler::MethodReference mref(dex_file_, method_idx_);
2163
2164 InferredRegCategoryMap const* map =
Ian Rogers776ac1f2012-04-13 23:36:36 -07002165 verifier::MethodVerifier::GetInferredRegCategoryMap(mref);
Logan Chiendd361c92012-04-10 23:40:37 +08002166
Logan Chiena78e3c82011-12-27 17:59:35 +08002167 CHECK_NE(map, static_cast<InferredRegCategoryMap*>(NULL));
2168
TDYa1271d7e5102012-05-13 09:27:05 -07002169 return map;
2170}
2171
2172RegCategory MethodCompiler::GetInferredRegCategory(uint32_t dex_pc,
2173 uint16_t reg_idx) {
2174 InferredRegCategoryMap const* map = GetInferredRegCategoryMap();
2175
Logan Chiena78e3c82011-12-27 17:59:35 +08002176 return map->GetRegCategory(dex_pc, reg_idx);
2177}
2178
TDYa1271d7e5102012-05-13 09:27:05 -07002179bool MethodCompiler::IsRegCanBeObject(uint16_t reg_idx) {
2180 InferredRegCategoryMap const* map = GetInferredRegCategoryMap();
2181
2182 return map->IsRegCanBeObject(reg_idx);
2183}
2184
Logan Chiena78e3c82011-12-27 17:59:35 +08002185
2186llvm::Value* MethodCompiler::EmitConditionResult(llvm::Value* lhs,
2187 llvm::Value* rhs,
2188 CondBranchKind cond) {
2189 switch (cond) {
2190 case kCondBranch_EQ:
2191 return irb_.CreateICmpEQ(lhs, rhs);
2192
2193 case kCondBranch_NE:
2194 return irb_.CreateICmpNE(lhs, rhs);
2195
2196 case kCondBranch_LT:
2197 return irb_.CreateICmpSLT(lhs, rhs);
2198
2199 case kCondBranch_GE:
2200 return irb_.CreateICmpSGE(lhs, rhs);
2201
2202 case kCondBranch_GT:
2203 return irb_.CreateICmpSGT(lhs, rhs);
2204
2205 case kCondBranch_LE:
2206 return irb_.CreateICmpSLE(lhs, rhs);
2207
2208 default: // Unreachable
2209 LOG(FATAL) << "Unknown conditional branch kind: " << cond;
2210 return NULL;
2211 }
Logan Chien70f94b42011-12-27 17:49:11 +08002212}
2213
TDYa12783bb6622012-04-17 02:20:34 -07002214void MethodCompiler::EmitMarkGCCard(llvm::Value* value, llvm::Value* target_addr) {
2215 // Using runtime support, let the target can override by InlineAssembly.
2216 llvm::Function* runtime_func = irb_.GetRuntime(MarkGCCard);
2217
2218 irb_.CreateCall2(runtime_func, value, target_addr);
2219}
Logan Chien70f94b42011-12-27 17:49:11 +08002220
Logan Chiene27fdbb2012-01-02 23:27:26 +08002221void
2222MethodCompiler::EmitGuard_ArrayIndexOutOfBoundsException(uint32_t dex_pc,
2223 llvm::Value* array,
2224 llvm::Value* index) {
2225 llvm::Value* array_len = EmitLoadArrayLength(array);
2226
2227 llvm::Value* cmp = irb_.CreateICmpUGE(index, array_len);
2228
2229 llvm::BasicBlock* block_exception =
2230 CreateBasicBlockWithDexPC(dex_pc, "overflow");
2231
2232 llvm::BasicBlock* block_continue =
2233 CreateBasicBlockWithDexPC(dex_pc, "cont");
2234
TDYa127ac7b5bb2012-05-11 13:17:49 -07002235 irb_.CreateCondBr(cmp, block_exception, block_continue, kUnlikely);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002236
2237 irb_.SetInsertPoint(block_exception);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002238
TDYa127c8dc1012012-04-19 07:03:33 -07002239 EmitUpdateDexPC(dex_pc);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002240 irb_.CreateCall2(irb_.GetRuntime(ThrowIndexOutOfBounds), index, array_len);
2241 EmitBranchExceptionLandingPad(dex_pc);
2242
2243 irb_.SetInsertPoint(block_continue);
2244}
2245
2246
2247void MethodCompiler::EmitGuard_ArrayException(uint32_t dex_pc,
2248 llvm::Value* array,
2249 llvm::Value* index) {
2250 EmitGuard_NullPointerException(dex_pc, array);
2251 EmitGuard_ArrayIndexOutOfBoundsException(dex_pc, array, index);
2252}
2253
2254
2255// Emit Array GetElementPtr
2256llvm::Value* MethodCompiler::EmitArrayGEP(llvm::Value* array_addr,
2257 llvm::Value* index_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08002258 llvm::Type* elem_type,
2259 JType elem_jty) {
2260
2261 int data_offset;
2262 if (elem_jty == kLong || elem_jty == kDouble ||
2263 (elem_jty == kObject && sizeof(uint64_t) == sizeof(Object*))) {
2264 data_offset = Array::DataOffset(sizeof(int64_t)).Int32Value();
2265 } else {
2266 data_offset = Array::DataOffset(sizeof(int32_t)).Int32Value();
2267 }
Logan Chiene27fdbb2012-01-02 23:27:26 +08002268
2269 llvm::Constant* data_offset_value =
Ian Rogers04ec04e2012-02-28 16:15:33 -08002270 irb_.getPtrEquivInt(data_offset);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002271
2272 llvm::Value* array_data_addr =
2273 irb_.CreatePtrDisp(array_addr, data_offset_value,
2274 elem_type->getPointerTo());
2275
2276 return irb_.CreateGEP(array_data_addr, index_value);
2277}
2278
2279
Logan Chien70f94b42011-12-27 17:49:11 +08002280void MethodCompiler::EmitInsn_AGet(uint32_t dex_pc,
2281 Instruction const* insn,
2282 JType elem_jty) {
Logan Chiene27fdbb2012-01-02 23:27:26 +08002283
Elliott Hughesadb8c672012-03-06 16:49:32 -08002284 DecodedInstruction dec_insn(insn);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002285
Elliott Hughesadb8c672012-03-06 16:49:32 -08002286 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
2287 llvm::Value* index_value = EmitLoadDalvikReg(dec_insn.vC, kInt, kAccurate);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002288
2289 EmitGuard_ArrayException(dex_pc, array_addr, index_value);
2290
2291 llvm::Type* elem_type = irb_.getJType(elem_jty, kArray);
2292
2293 llvm::Value* array_elem_addr =
Ian Rogers04ec04e2012-02-28 16:15:33 -08002294 EmitArrayGEP(array_addr, index_value, elem_type, elem_jty);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002295
TDYa127706e7db2012-05-06 00:05:33 -07002296 llvm::Value* array_elem_value = irb_.CreateLoad(array_elem_addr, kTBAAHeapArray, elem_jty);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002297
Elliott Hughesadb8c672012-03-06 16:49:32 -08002298 EmitStoreDalvikReg(dec_insn.vA, elem_jty, kArray, array_elem_value);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002299
Logan Chien70f94b42011-12-27 17:49:11 +08002300 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2301}
2302
2303
2304void MethodCompiler::EmitInsn_APut(uint32_t dex_pc,
2305 Instruction const* insn,
2306 JType elem_jty) {
Logan Chien8dabb432012-01-02 23:29:32 +08002307
Elliott Hughesadb8c672012-03-06 16:49:32 -08002308 DecodedInstruction dec_insn(insn);
Logan Chien8dabb432012-01-02 23:29:32 +08002309
Elliott Hughesadb8c672012-03-06 16:49:32 -08002310 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
2311 llvm::Value* index_value = EmitLoadDalvikReg(dec_insn.vC, kInt, kAccurate);
Logan Chien8dabb432012-01-02 23:29:32 +08002312
2313 EmitGuard_ArrayException(dex_pc, array_addr, index_value);
2314
2315 llvm::Type* elem_type = irb_.getJType(elem_jty, kArray);
2316
2317 llvm::Value* array_elem_addr =
Ian Rogers04ec04e2012-02-28 16:15:33 -08002318 EmitArrayGEP(array_addr, index_value, elem_type, elem_jty);
Logan Chien8dabb432012-01-02 23:29:32 +08002319
Elliott Hughesadb8c672012-03-06 16:49:32 -08002320 llvm::Value* new_value = EmitLoadDalvikReg(dec_insn.vA, elem_jty, kArray);
Logan Chien8dabb432012-01-02 23:29:32 +08002321
TDYa12783bb6622012-04-17 02:20:34 -07002322 if (elem_jty == kObject) { // If put an object, check the type, and mark GC card table.
TDYa1271b86d072012-04-05 17:38:56 -07002323 llvm::Function* runtime_func = irb_.GetRuntime(CheckPutArrayElement);
2324
2325 irb_.CreateCall2(runtime_func, new_value, array_addr);
2326
2327 EmitGuard_ExceptionLandingPad(dex_pc);
TDYa12783bb6622012-04-17 02:20:34 -07002328
2329 EmitMarkGCCard(new_value, array_addr);
TDYa1271b86d072012-04-05 17:38:56 -07002330 }
2331
TDYa127706e7db2012-05-06 00:05:33 -07002332 irb_.CreateStore(new_value, array_elem_addr, kTBAAHeapArray, elem_jty);
Logan Chien8dabb432012-01-02 23:29:32 +08002333
Logan Chien70f94b42011-12-27 17:49:11 +08002334 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2335}
2336
2337
2338void MethodCompiler::EmitInsn_IGet(uint32_t dex_pc,
2339 Instruction const* insn,
2340 JType field_jty) {
Logan Chien48f1d2a2012-01-02 22:49:53 +08002341
Elliott Hughesadb8c672012-03-06 16:49:32 -08002342 DecodedInstruction dec_insn(insn);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002343
Elliott Hughesadb8c672012-03-06 16:49:32 -08002344 uint32_t reg_idx = dec_insn.vB;
2345 uint32_t field_idx = dec_insn.vC;
Logan Chien48f1d2a2012-01-02 22:49:53 +08002346
Logan Chien48f1d2a2012-01-02 22:49:53 +08002347 llvm::Value* object_addr = EmitLoadDalvikReg(reg_idx, kObject, kAccurate);
2348
TDYa127cc1b4c32012-05-15 07:31:37 -07002349 if (!(method_info_.this_will_not_be_null && reg_idx == method_info_.this_reg_idx)) {
2350 EmitGuard_NullPointerException(dex_pc, object_addr);
2351 }
Logan Chien48f1d2a2012-01-02 22:49:53 +08002352
2353 llvm::Value* field_value;
2354
Logan Chien933abf82012-04-11 12:24:31 +08002355 int field_offset;
2356 bool is_volatile;
2357 bool is_fast_path = compiler_->ComputeInstanceFieldInfo(
2358 field_idx, oat_compilation_unit_, field_offset, is_volatile, false);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002359
Logan Chien933abf82012-04-11 12:24:31 +08002360 if (!is_fast_path) {
Logan Chien48f1d2a2012-01-02 22:49:53 +08002361 llvm::Function* runtime_func;
2362
2363 if (field_jty == kObject) {
Logan Chien3b2b2e72012-03-06 16:11:45 +08002364 runtime_func = irb_.GetRuntime(GetObjectInstance);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002365 } else if (field_jty == kLong || field_jty == kDouble) {
Logan Chien3b2b2e72012-03-06 16:11:45 +08002366 runtime_func = irb_.GetRuntime(Get64Instance);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002367 } else {
Logan Chien3b2b2e72012-03-06 16:11:45 +08002368 runtime_func = irb_.GetRuntime(Get32Instance);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002369 }
2370
2371 llvm::ConstantInt* field_idx_value = irb_.getInt32(field_idx);
2372
2373 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2374
TDYa127c8dc1012012-04-19 07:03:33 -07002375 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002376
Logan Chien3b2b2e72012-03-06 16:11:45 +08002377 field_value = irb_.CreateCall3(runtime_func, field_idx_value,
2378 method_object_addr, object_addr);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002379
2380 EmitGuard_ExceptionLandingPad(dex_pc);
2381
2382 } else {
Logan Chien933abf82012-04-11 12:24:31 +08002383 DCHECK_GE(field_offset, 0);
2384
Logan Chien48f1d2a2012-01-02 22:49:53 +08002385 llvm::PointerType* field_type =
2386 irb_.getJType(field_jty, kField)->getPointerTo();
2387
Logan Chien933abf82012-04-11 12:24:31 +08002388 llvm::ConstantInt* field_offset_value = irb_.getPtrEquivInt(field_offset);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002389
2390 llvm::Value* field_addr =
Logan Chien933abf82012-04-11 12:24:31 +08002391 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002392
Logan Chien933abf82012-04-11 12:24:31 +08002393 // TODO: Check is_volatile. We need to generate atomic load instruction
2394 // when is_volatile is true.
TDYa127706e7db2012-05-06 00:05:33 -07002395 field_value = irb_.CreateLoad(field_addr, kTBAAHeapInstance, field_jty);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002396 }
2397
Elliott Hughesadb8c672012-03-06 16:49:32 -08002398 EmitStoreDalvikReg(dec_insn.vA, field_jty, kField, field_value);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002399
Logan Chien70f94b42011-12-27 17:49:11 +08002400 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2401}
2402
2403
2404void MethodCompiler::EmitInsn_IPut(uint32_t dex_pc,
2405 Instruction const* insn,
2406 JType field_jty) {
Logan Chiendd6aa872012-01-03 16:06:32 +08002407
Elliott Hughesadb8c672012-03-06 16:49:32 -08002408 DecodedInstruction dec_insn(insn);
Logan Chiendd6aa872012-01-03 16:06:32 +08002409
Elliott Hughesadb8c672012-03-06 16:49:32 -08002410 uint32_t reg_idx = dec_insn.vB;
2411 uint32_t field_idx = dec_insn.vC;
Logan Chiendd6aa872012-01-03 16:06:32 +08002412
Logan Chiendd6aa872012-01-03 16:06:32 +08002413 llvm::Value* object_addr = EmitLoadDalvikReg(reg_idx, kObject, kAccurate);
2414
TDYa127cc1b4c32012-05-15 07:31:37 -07002415 if (!(method_info_.this_will_not_be_null && reg_idx == method_info_.this_reg_idx)) {
2416 EmitGuard_NullPointerException(dex_pc, object_addr);
2417 }
Logan Chiendd6aa872012-01-03 16:06:32 +08002418
Elliott Hughesadb8c672012-03-06 16:49:32 -08002419 llvm::Value* new_value = EmitLoadDalvikReg(dec_insn.vA, field_jty, kField);
Logan Chiendd6aa872012-01-03 16:06:32 +08002420
Logan Chien933abf82012-04-11 12:24:31 +08002421 int field_offset;
2422 bool is_volatile;
2423 bool is_fast_path = compiler_->ComputeInstanceFieldInfo(
2424 field_idx, oat_compilation_unit_, field_offset, is_volatile, true);
Logan Chiendd6aa872012-01-03 16:06:32 +08002425
Logan Chien933abf82012-04-11 12:24:31 +08002426 if (!is_fast_path) {
Logan Chiendd6aa872012-01-03 16:06:32 +08002427 llvm::Function* runtime_func;
2428
2429 if (field_jty == kObject) {
2430 runtime_func = irb_.GetRuntime(SetObjectInstance);
2431 } else if (field_jty == kLong || field_jty == kDouble) {
2432 runtime_func = irb_.GetRuntime(Set64Instance);
2433 } else {
2434 runtime_func = irb_.GetRuntime(Set32Instance);
2435 }
2436
2437 llvm::Value* field_idx_value = irb_.getInt32(field_idx);
2438
2439 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2440
TDYa127c8dc1012012-04-19 07:03:33 -07002441 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002442
Logan Chien3b2b2e72012-03-06 16:11:45 +08002443 irb_.CreateCall4(runtime_func, field_idx_value,
2444 method_object_addr, object_addr, new_value);
Logan Chiendd6aa872012-01-03 16:06:32 +08002445
2446 EmitGuard_ExceptionLandingPad(dex_pc);
2447
2448 } else {
Logan Chien933abf82012-04-11 12:24:31 +08002449 DCHECK_GE(field_offset, 0);
2450
Logan Chiendd6aa872012-01-03 16:06:32 +08002451 llvm::PointerType* field_type =
2452 irb_.getJType(field_jty, kField)->getPointerTo();
2453
Logan Chien933abf82012-04-11 12:24:31 +08002454 llvm::Value* field_offset_value = irb_.getPtrEquivInt(field_offset);
Logan Chiendd6aa872012-01-03 16:06:32 +08002455
2456 llvm::Value* field_addr =
Logan Chien933abf82012-04-11 12:24:31 +08002457 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
Logan Chiendd6aa872012-01-03 16:06:32 +08002458
Logan Chien933abf82012-04-11 12:24:31 +08002459 // TODO: Check is_volatile. We need to generate atomic store instruction
2460 // when is_volatile is true.
TDYa127706e7db2012-05-06 00:05:33 -07002461 irb_.CreateStore(new_value, field_addr, kTBAAHeapInstance, field_jty);
TDYa12783bb6622012-04-17 02:20:34 -07002462
2463 if (field_jty == kObject) { // If put an object, mark the GC card table.
2464 EmitMarkGCCard(new_value, object_addr);
2465 }
Logan Chiendd6aa872012-01-03 16:06:32 +08002466 }
2467
Logan Chien70f94b42011-12-27 17:49:11 +08002468 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2469}
2470
2471
Logan Chien438c4b62012-01-17 16:06:00 +08002472llvm::Value* MethodCompiler::EmitLoadStaticStorage(uint32_t dex_pc,
2473 uint32_t type_idx) {
2474 llvm::BasicBlock* block_load_static =
2475 CreateBasicBlockWithDexPC(dex_pc, "load_static");
2476
2477 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
2478
2479 // Load static storage from dex cache
2480 llvm::Value* storage_field_addr =
2481 EmitLoadDexCacheStaticStorageFieldAddr(type_idx);
2482
TDYa1278ca10052012-05-05 19:57:06 -07002483 llvm::Value* storage_object_addr = irb_.CreateLoad(storage_field_addr, kTBAAJRuntime);
Logan Chien438c4b62012-01-17 16:06:00 +08002484
2485 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
2486
2487 // Test: Is the static storage of this class initialized?
2488 llvm::Value* equal_null =
2489 irb_.CreateICmpEQ(storage_object_addr, irb_.getJNull());
2490
TDYa127ac7b5bb2012-05-11 13:17:49 -07002491 irb_.CreateCondBr(equal_null, block_load_static, block_cont, kUnlikely);
Logan Chien438c4b62012-01-17 16:06:00 +08002492
2493 // Failback routine to load the class object
2494 irb_.SetInsertPoint(block_load_static);
2495
2496 llvm::Function* runtime_func =
2497 irb_.GetRuntime(InitializeStaticStorage);
2498
2499 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
2500
2501 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2502
TDYa127706e9b62012-04-19 12:24:26 -07002503 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
2504
TDYa127c8dc1012012-04-19 07:03:33 -07002505 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002506
Logan Chien438c4b62012-01-17 16:06:00 +08002507 llvm::Value* loaded_storage_object_addr =
TDYa127706e9b62012-04-19 12:24:26 -07002508 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
Logan Chien438c4b62012-01-17 16:06:00 +08002509
2510 EmitGuard_ExceptionLandingPad(dex_pc);
2511
2512 llvm::BasicBlock* block_after_load_static = irb_.GetInsertBlock();
2513
2514 irb_.CreateBr(block_cont);
2515
2516 // Now the class object must be loaded
2517 irb_.SetInsertPoint(block_cont);
2518
2519 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
2520
2521 phi->addIncoming(storage_object_addr, block_original);
2522 phi->addIncoming(loaded_storage_object_addr, block_after_load_static);
2523
2524 return phi;
2525}
2526
2527
Logan Chien70f94b42011-12-27 17:49:11 +08002528void MethodCompiler::EmitInsn_SGet(uint32_t dex_pc,
2529 Instruction const* insn,
2530 JType field_jty) {
Logan Chien438c4b62012-01-17 16:06:00 +08002531
Elliott Hughesadb8c672012-03-06 16:49:32 -08002532 DecodedInstruction dec_insn(insn);
Logan Chien438c4b62012-01-17 16:06:00 +08002533
Logan Chien933abf82012-04-11 12:24:31 +08002534 uint32_t field_idx = dec_insn.vB;
Logan Chien438c4b62012-01-17 16:06:00 +08002535
Logan Chien933abf82012-04-11 12:24:31 +08002536 int field_offset;
2537 int ssb_index;
2538 bool is_referrers_class;
2539 bool is_volatile;
2540
2541 bool is_fast_path = compiler_->ComputeStaticFieldInfo(
2542 field_idx, oat_compilation_unit_, field_offset, ssb_index,
2543 is_referrers_class, is_volatile, false);
Logan Chien438c4b62012-01-17 16:06:00 +08002544
2545 llvm::Value* static_field_value;
2546
Logan Chien933abf82012-04-11 12:24:31 +08002547 if (!is_fast_path) {
Logan Chien438c4b62012-01-17 16:06:00 +08002548 llvm::Function* runtime_func;
2549
2550 if (field_jty == kObject) {
2551 runtime_func = irb_.GetRuntime(GetObjectStatic);
2552 } else if (field_jty == kLong || field_jty == kDouble) {
2553 runtime_func = irb_.GetRuntime(Get64Static);
2554 } else {
2555 runtime_func = irb_.GetRuntime(Get32Static);
2556 }
2557
Elliott Hughesadb8c672012-03-06 16:49:32 -08002558 llvm::Constant* field_idx_value = irb_.getInt32(dec_insn.vB);
Logan Chien438c4b62012-01-17 16:06:00 +08002559
2560 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2561
TDYa127c8dc1012012-04-19 07:03:33 -07002562 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002563
Logan Chien438c4b62012-01-17 16:06:00 +08002564 static_field_value =
2565 irb_.CreateCall2(runtime_func, field_idx_value, method_object_addr);
2566
2567 EmitGuard_ExceptionLandingPad(dex_pc);
2568
2569 } else {
Logan Chien933abf82012-04-11 12:24:31 +08002570 DCHECK_GE(field_offset, 0);
Logan Chien438c4b62012-01-17 16:06:00 +08002571
Logan Chien933abf82012-04-11 12:24:31 +08002572 llvm::Value* static_storage_addr = NULL;
2573
2574 if (is_referrers_class) {
2575 // Fast path, static storage base is this method's class
2576 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2577
TDYa127ee1f59b2012-04-25 00:56:40 -07002578 static_storage_addr =
2579 irb_.LoadFromObjectOffset(method_object_addr,
2580 Method::DeclaringClassOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -07002581 irb_.getJObjectTy(),
TDYa127d3e24c22012-05-05 20:54:19 -07002582 kTBAAConstJObject);
Logan Chien933abf82012-04-11 12:24:31 +08002583 } else {
2584 // Medium path, static storage base in a different class which
2585 // requires checks that the other class is initialized
2586 DCHECK_GE(ssb_index, 0);
2587 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
2588 }
2589
2590 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
Logan Chien438c4b62012-01-17 16:06:00 +08002591
2592 llvm::Value* static_field_addr =
2593 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
2594 irb_.getJType(field_jty, kField)->getPointerTo());
2595
Logan Chien933abf82012-04-11 12:24:31 +08002596 // TODO: Check is_volatile. We need to generate atomic load instruction
2597 // when is_volatile is true.
TDYa127706e7db2012-05-06 00:05:33 -07002598 static_field_value = irb_.CreateLoad(static_field_addr, kTBAAHeapStatic, field_jty);
Logan Chien438c4b62012-01-17 16:06:00 +08002599 }
2600
Elliott Hughesadb8c672012-03-06 16:49:32 -08002601 EmitStoreDalvikReg(dec_insn.vA, field_jty, kField, static_field_value);
Logan Chien438c4b62012-01-17 16:06:00 +08002602
Logan Chien70f94b42011-12-27 17:49:11 +08002603 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2604}
2605
2606
2607void MethodCompiler::EmitInsn_SPut(uint32_t dex_pc,
2608 Instruction const* insn,
2609 JType field_jty) {
Logan Chien14179c82012-01-17 17:06:34 +08002610
Elliott Hughesadb8c672012-03-06 16:49:32 -08002611 DecodedInstruction dec_insn(insn);
Logan Chien14179c82012-01-17 17:06:34 +08002612
Logan Chien933abf82012-04-11 12:24:31 +08002613 uint32_t field_idx = dec_insn.vB;
Logan Chien14179c82012-01-17 17:06:34 +08002614
Elliott Hughesadb8c672012-03-06 16:49:32 -08002615 llvm::Value* new_value = EmitLoadDalvikReg(dec_insn.vA, field_jty, kField);
Logan Chien14179c82012-01-17 17:06:34 +08002616
Logan Chien933abf82012-04-11 12:24:31 +08002617 int field_offset;
2618 int ssb_index;
2619 bool is_referrers_class;
2620 bool is_volatile;
2621
2622 bool is_fast_path = compiler_->ComputeStaticFieldInfo(
2623 field_idx, oat_compilation_unit_, field_offset, ssb_index,
2624 is_referrers_class, is_volatile, true);
2625
2626 if (!is_fast_path) {
Logan Chien14179c82012-01-17 17:06:34 +08002627 llvm::Function* runtime_func;
2628
2629 if (field_jty == kObject) {
2630 runtime_func = irb_.GetRuntime(SetObjectStatic);
2631 } else if (field_jty == kLong || field_jty == kDouble) {
2632 runtime_func = irb_.GetRuntime(Set64Static);
2633 } else {
2634 runtime_func = irb_.GetRuntime(Set32Static);
2635 }
2636
Elliott Hughesadb8c672012-03-06 16:49:32 -08002637 llvm::Constant* field_idx_value = irb_.getInt32(dec_insn.vB);
Logan Chien14179c82012-01-17 17:06:34 +08002638
2639 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2640
TDYa127c8dc1012012-04-19 07:03:33 -07002641 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002642
Logan Chien14179c82012-01-17 17:06:34 +08002643 irb_.CreateCall3(runtime_func, field_idx_value,
2644 method_object_addr, new_value);
2645
2646 EmitGuard_ExceptionLandingPad(dex_pc);
2647
2648 } else {
Logan Chien933abf82012-04-11 12:24:31 +08002649 DCHECK_GE(field_offset, 0);
Logan Chien14179c82012-01-17 17:06:34 +08002650
Logan Chien933abf82012-04-11 12:24:31 +08002651 llvm::Value* static_storage_addr = NULL;
2652
2653 if (is_referrers_class) {
2654 // Fast path, static storage base is this method's class
2655 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2656
TDYa127ee1f59b2012-04-25 00:56:40 -07002657 static_storage_addr =
2658 irb_.LoadFromObjectOffset(method_object_addr,
2659 Method::DeclaringClassOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -07002660 irb_.getJObjectTy(),
TDYa127d3e24c22012-05-05 20:54:19 -07002661 kTBAAConstJObject);
Logan Chien933abf82012-04-11 12:24:31 +08002662 } else {
2663 // Medium path, static storage base in a different class which
2664 // requires checks that the other class is initialized
2665 DCHECK_GE(ssb_index, 0);
2666 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
2667 }
2668
2669 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
Logan Chien14179c82012-01-17 17:06:34 +08002670
2671 llvm::Value* static_field_addr =
2672 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
2673 irb_.getJType(field_jty, kField)->getPointerTo());
2674
Logan Chien933abf82012-04-11 12:24:31 +08002675 // TODO: Check is_volatile. We need to generate atomic store instruction
2676 // when is_volatile is true.
TDYa127706e7db2012-05-06 00:05:33 -07002677 irb_.CreateStore(new_value, static_field_addr, kTBAAHeapStatic, field_jty);
TDYa12783bb6622012-04-17 02:20:34 -07002678
2679 if (field_jty == kObject) { // If put an object, mark the GC card table.
2680 EmitMarkGCCard(new_value, static_storage_addr);
2681 }
Logan Chien14179c82012-01-17 17:06:34 +08002682 }
2683
Logan Chien70f94b42011-12-27 17:49:11 +08002684 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2685}
2686
2687
Logan Chien1a121b92012-02-15 22:23:42 +08002688void MethodCompiler::
2689EmitLoadActualParameters(std::vector<llvm::Value*>& args,
2690 uint32_t callee_method_idx,
Elliott Hughesadb8c672012-03-06 16:49:32 -08002691 DecodedInstruction const& dec_insn,
Logan Chien7e7fabc2012-04-10 18:59:11 +08002692 InvokeArgFmt arg_fmt,
Logan Chien1a121b92012-02-15 22:23:42 +08002693 bool is_static) {
2694
2695 // Get method signature
2696 DexFile::MethodId const& method_id =
2697 dex_file_->GetMethodId(callee_method_idx);
2698
Logan Chien8faf8022012-02-24 12:25:29 +08002699 uint32_t shorty_size;
Logan Chien1a121b92012-02-15 22:23:42 +08002700 char const* shorty = dex_file_->GetMethodShorty(method_id, &shorty_size);
Logan Chien8faf8022012-02-24 12:25:29 +08002701 CHECK_GE(shorty_size, 1u);
Logan Chien1a121b92012-02-15 22:23:42 +08002702
2703 // Load argument values according to the shorty (without "this")
2704 uint16_t reg_count = 0;
2705
2706 if (!is_static) {
2707 ++reg_count; // skip the "this" pointer
2708 }
2709
Logan Chien7e7fabc2012-04-10 18:59:11 +08002710 bool is_range = (arg_fmt == kArgRange);
2711
Logan Chien8faf8022012-02-24 12:25:29 +08002712 for (uint32_t i = 1; i < shorty_size; ++i) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002713 uint32_t reg_idx = (is_range) ? (dec_insn.vC + reg_count)
2714 : (dec_insn.arg[reg_count]);
Logan Chien1a121b92012-02-15 22:23:42 +08002715
2716 args.push_back(EmitLoadDalvikReg(reg_idx, shorty[i], kAccurate));
2717
2718 ++reg_count;
2719 if (shorty[i] == 'J' || shorty[i] == 'D') {
2720 // Wide types, such as long and double, are using a pair of registers
2721 // to store the value, so we have to increase arg_reg again.
2722 ++reg_count;
2723 }
2724 }
2725
Elliott Hughesadb8c672012-03-06 16:49:32 -08002726 DCHECK_EQ(reg_count, dec_insn.vA)
Logan Chien1a121b92012-02-15 22:23:42 +08002727 << "Actual argument mismatch for callee: "
2728 << PrettyMethod(callee_method_idx, *dex_file_);
2729}
2730
TDYa1270b686e52012-04-09 22:43:35 -07002731llvm::Value* MethodCompiler::EmitFixStub(llvm::Value* callee_method_object_addr,
2732 uint32_t method_idx,
2733 bool is_static) {
2734 // TODO: Remove this after we solve the link and trampoline related problems.
2735 llvm::Value* code_addr = irb_.CreateCall(irb_.GetRuntime(FixStub), callee_method_object_addr);
2736
2737 llvm::FunctionType* method_type = GetFunctionType(method_idx, is_static);
2738
2739 return irb_.CreatePointerCast(code_addr, method_type->getPointerTo());
TDYa12785321912012-04-01 15:24:56 -07002740}
Logan Chien1a121b92012-02-15 22:23:42 +08002741
Shih-wei Liao399ed3f2012-03-08 01:27:04 -08002742
Logan Chien7e7fabc2012-04-10 18:59:11 +08002743void MethodCompiler::EmitInsn_Invoke(uint32_t dex_pc,
2744 Instruction const* insn,
2745 InvokeType invoke_type,
2746 InvokeArgFmt arg_fmt) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002747 DecodedInstruction dec_insn(insn);
Logan Chien46fbb412012-02-15 22:29:08 +08002748
Logan Chien61c65dc2012-02-29 03:22:30 +08002749 bool is_static = (invoke_type == kStatic);
Logan Chien7e7fabc2012-04-10 18:59:11 +08002750 uint32_t callee_method_idx = dec_insn.vB;
Logan Chien61c65dc2012-02-29 03:22:30 +08002751
Logan Chien7e7fabc2012-04-10 18:59:11 +08002752 // Compute invoke related information for compiler decision
2753 int vtable_idx = -1;
Logan Chien92ad16d2012-03-18 05:48:55 +08002754 uintptr_t direct_code = 0; // Currently unused
Logan Chienfca64372012-04-23 14:57:01 +08002755 uintptr_t direct_method = 0;
Logan Chien61c65dc2012-02-29 03:22:30 +08002756 bool is_fast_path = compiler_->
Logan Chien7e7fabc2012-04-10 18:59:11 +08002757 ComputeInvokeInfo(callee_method_idx, oat_compilation_unit_,
Logan Chien92ad16d2012-03-18 05:48:55 +08002758 invoke_type, vtable_idx, direct_code, direct_method);
Logan Chien61c65dc2012-02-29 03:22:30 +08002759
Logan Chien7e7fabc2012-04-10 18:59:11 +08002760 // Load *this* actual parameter
Logan Chien1a121b92012-02-15 22:23:42 +08002761 llvm::Value* this_addr = NULL;
2762
2763 if (!is_static) {
2764 // Test: Is *this* parameter equal to null?
TDYa127cc1b4c32012-05-15 07:31:37 -07002765 uint32_t reg_idx = (arg_fmt == kArgReg) ? dec_insn.arg[0] : (dec_insn.vC + 0);
2766 this_addr = EmitLoadDalvikReg(reg_idx, kObject, kAccurate);
Logan Chien7e7fabc2012-04-10 18:59:11 +08002767
TDYa127cc1b4c32012-05-15 07:31:37 -07002768 if (!(method_info_.this_will_not_be_null && reg_idx == method_info_.this_reg_idx)) {
2769 EmitGuard_NullPointerException(dex_pc, this_addr);
2770 }
Logan Chien1a121b92012-02-15 22:23:42 +08002771 }
2772
Logan Chien7e7fabc2012-04-10 18:59:11 +08002773 // Load the method object
TDYa1274e42a592012-04-10 20:13:54 -07002774 llvm::Value* callee_method_object_addr = NULL;
Logan Chien7e7fabc2012-04-10 18:59:11 +08002775
2776 if (!is_fast_path) {
TDYa1274e42a592012-04-10 20:13:54 -07002777 callee_method_object_addr =
Logan Chien7e7fabc2012-04-10 18:59:11 +08002778 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx, invoke_type,
2779 this_addr, dex_pc, is_fast_path);
2780 } else {
2781 switch (invoke_type) {
2782 case kStatic:
2783 case kDirect:
Logan Chienfca64372012-04-23 14:57:01 +08002784 if (direct_method != 0u &&
2785 direct_method != static_cast<uintptr_t>(-1)) {
2786 callee_method_object_addr =
TDYa12717826bf2012-04-24 01:15:10 -07002787 irb_.CreateIntToPtr(irb_.getPtrEquivInt(direct_method),
2788 irb_.getJObjectTy());
Logan Chienfca64372012-04-23 14:57:01 +08002789 } else {
2790 callee_method_object_addr =
2791 EmitLoadSDCalleeMethodObjectAddr(callee_method_idx);
2792 }
Logan Chien7e7fabc2012-04-10 18:59:11 +08002793 break;
2794
2795 case kVirtual:
2796 DCHECK(vtable_idx != -1);
TDYa1274e42a592012-04-10 20:13:54 -07002797 callee_method_object_addr =
Logan Chien7e7fabc2012-04-10 18:59:11 +08002798 EmitLoadVirtualCalleeMethodObjectAddr(vtable_idx, this_addr);
2799 break;
2800
2801 case kSuper:
2802 LOG(FATAL) << "invoke-super should be promoted to invoke-direct in "
2803 "the fast path.";
2804 break;
2805
2806 case kInterface:
TDYa1274e42a592012-04-10 20:13:54 -07002807 callee_method_object_addr =
Logan Chien7e7fabc2012-04-10 18:59:11 +08002808 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx,
2809 invoke_type, this_addr,
2810 dex_pc, is_fast_path);
2811 break;
2812 }
2813 }
2814
Logan Chien7e7fabc2012-04-10 18:59:11 +08002815 llvm::Value* code_addr =
TDYa127ee1f59b2012-04-25 00:56:40 -07002816 irb_.LoadFromObjectOffset(callee_method_object_addr,
2817 Method::GetCodeOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -07002818 GetFunctionType(callee_method_idx, is_static)->getPointerTo(),
TDYa1278ca10052012-05-05 19:57:06 -07002819 kTBAAJRuntime);
TDYa12785321912012-04-01 15:24:56 -07002820
Logan Chien1a121b92012-02-15 22:23:42 +08002821 // Load the actual parameter
2822 std::vector<llvm::Value*> args;
2823
2824 args.push_back(callee_method_object_addr); // method object for callee
2825
2826 if (!is_static) {
Logan Chien7e7fabc2012-04-10 18:59:11 +08002827 DCHECK(this_addr != NULL);
Logan Chien1a121b92012-02-15 22:23:42 +08002828 args.push_back(this_addr); // "this" object for callee
2829 }
2830
2831 EmitLoadActualParameters(args, callee_method_idx, dec_insn,
Logan Chien7e7fabc2012-04-10 18:59:11 +08002832 arg_fmt, is_static);
Logan Chien1a121b92012-02-15 22:23:42 +08002833
TDYa1275bb86012012-04-11 05:57:28 -07002834#if 0
Logan Chien8dfcbea2012-02-17 18:50:32 +08002835 // Invoke callee
TDYa127c8dc1012012-04-19 07:03:33 -07002836 EmitUpdateDexPC(dex_pc);
Logan Chien1a121b92012-02-15 22:23:42 +08002837 llvm::Value* retval = irb_.CreateCall(code_addr, args);
2838 EmitGuard_ExceptionLandingPad(dex_pc);
2839
Logan Chien7e7fabc2012-04-10 18:59:11 +08002840 uint32_t callee_access_flags = is_static ? kAccStatic : 0;
2841 UniquePtr<OatCompilationUnit> callee_oat_compilation_unit(
2842 oat_compilation_unit_->GetCallee(callee_method_idx, callee_access_flags));
2843
2844 char ret_shorty = callee_oat_compilation_unit->GetShorty()[0];
Shih-wei Liao90d50992012-02-19 03:32:05 -08002845 if (ret_shorty != 'V') {
Logan Chien1a121b92012-02-15 22:23:42 +08002846 EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval);
2847 }
TDYa1275bb86012012-04-11 05:57:28 -07002848#else
2849 uint32_t callee_access_flags = is_static ? kAccStatic : 0;
2850 UniquePtr<OatCompilationUnit> callee_oat_compilation_unit(
2851 oat_compilation_unit_->GetCallee(callee_method_idx, callee_access_flags));
2852
2853 char ret_shorty = callee_oat_compilation_unit->GetShorty()[0];
2854
2855
TDYa127c8dc1012012-04-19 07:03:33 -07002856 EmitUpdateDexPC(dex_pc);
TDYa1275bb86012012-04-11 05:57:28 -07002857
2858
2859 llvm::BasicBlock* block_normal = CreateBasicBlockWithDexPC(dex_pc, "normal");
TDYa127ce154722012-04-21 16:43:29 -07002860 llvm::BasicBlock* block_stub = CreateBasicBlockWithDexPC(dex_pc, "stub");
TDYa1275bb86012012-04-11 05:57:28 -07002861 llvm::BasicBlock* block_continue = CreateBasicBlockWithDexPC(dex_pc, "cont");
2862
TDYa1275bb86012012-04-11 05:57:28 -07002863 llvm::Value* code_addr_int = irb_.CreatePtrToInt(code_addr, irb_.getPtrEquivIntTy());
TDYa127ce154722012-04-21 16:43:29 -07002864 llvm::Value* max_stub_int = irb_.getPtrEquivInt(special_stub::kMaxSpecialStub);
2865 llvm::Value* is_stub = irb_.CreateICmpULT(code_addr_int, max_stub_int);
TDYa127ac7b5bb2012-05-11 13:17:49 -07002866 irb_.CreateCondBr(is_stub, block_stub, block_normal, kUnlikely);
TDYa1275bb86012012-04-11 05:57:28 -07002867
2868
2869 irb_.SetInsertPoint(block_normal);
2870 {
2871 // Invoke callee
TDYa127ce154722012-04-21 16:43:29 -07002872 llvm::Value* retval = irb_.CreateCall(code_addr, args);
TDYa1275bb86012012-04-11 05:57:28 -07002873 if (ret_shorty != 'V') {
TDYa127ce154722012-04-21 16:43:29 -07002874 EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval);
TDYa1275bb86012012-04-11 05:57:28 -07002875 }
2876 }
2877 irb_.CreateBr(block_continue);
2878
2879
TDYa127ce154722012-04-21 16:43:29 -07002880 irb_.SetInsertPoint(block_stub);
TDYa1275bb86012012-04-11 05:57:28 -07002881 {
TDYa127ce154722012-04-21 16:43:29 -07002882 { // lazy link
2883 // TODO: Remove this after we solve the link problem.
2884 llvm::BasicBlock* block_proxy_stub = CreateBasicBlockWithDexPC(dex_pc, "proxy");
2885 llvm::BasicBlock* block_link = CreateBasicBlockWithDexPC(dex_pc, "link");
2886
TDYa127ac7b5bb2012-05-11 13:17:49 -07002887 irb_.CreateCondBr(irb_.CreateIsNull(code_addr), block_link, block_proxy_stub, kUnlikely);
TDYa127ce154722012-04-21 16:43:29 -07002888
2889
2890 irb_.SetInsertPoint(block_link);
2891 code_addr = EmitFixStub(callee_method_object_addr, callee_method_idx, is_static);
2892
2893 EmitGuard_ExceptionLandingPad(dex_pc);
2894
2895 llvm::Value* retval = irb_.CreateCall(code_addr, args);
2896 if (ret_shorty != 'V') {
2897 EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval);
2898 }
2899 irb_.CreateBr(block_continue);
2900
2901
2902 irb_.SetInsertPoint(block_proxy_stub);
TDYa1275bb86012012-04-11 05:57:28 -07002903 }
TDYa127ce154722012-04-21 16:43:29 -07002904 { // proxy stub
2905 llvm::Value* temp_space_addr;
2906 if (ret_shorty != 'V') {
2907 temp_space_addr = irb_.CreateAlloca(irb_.getJValueTy());
2908 args.push_back(temp_space_addr);
2909 }
2910 // TODO: Remove this after we solve the proxy trampoline calling convention problem.
2911 irb_.CreateCall(irb_.GetRuntime(ProxyInvokeHandler), args);
2912 if (ret_shorty != 'V') {
Shih-wei Liaoe6a7adc2012-05-09 02:50:08 -07002913 llvm::Type* accurate_ret_type = irb_.getJType(ret_shorty, kAccurate);
TDYa127ce154722012-04-21 16:43:29 -07002914 llvm::Value* result_addr =
2915 irb_.CreateBitCast(temp_space_addr, accurate_ret_type->getPointerTo());
TDYa127aba61122012-05-04 18:28:36 -07002916 llvm::Value* retval = irb_.CreateLoad(result_addr, kTBAAStackTemp);
TDYa127ce154722012-04-21 16:43:29 -07002917 EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval);
2918 }
TDYa1275bb86012012-04-11 05:57:28 -07002919 }
2920 }
2921 irb_.CreateBr(block_continue);
2922
2923
2924 irb_.SetInsertPoint(block_continue);
2925
TDYa1275bb86012012-04-11 05:57:28 -07002926 EmitGuard_ExceptionLandingPad(dex_pc);
2927#endif
Logan Chien1a121b92012-02-15 22:23:42 +08002928
Logan Chien70f94b42011-12-27 17:49:11 +08002929 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2930}
2931
2932
Logan Chien7e7fabc2012-04-10 18:59:11 +08002933llvm::Value* MethodCompiler::
2934EmitLoadSDCalleeMethodObjectAddr(uint32_t callee_method_idx) {
2935 llvm::Value* callee_method_object_field_addr =
2936 EmitLoadDexCacheResolvedMethodFieldAddr(callee_method_idx);
Logan Chien7caf37e2012-02-03 22:56:04 +08002937
TDYa1278ca10052012-05-05 19:57:06 -07002938 return irb_.CreateLoad(callee_method_object_field_addr, kTBAAJRuntime);
Logan Chien7e7fabc2012-04-10 18:59:11 +08002939}
Logan Chien7caf37e2012-02-03 22:56:04 +08002940
Logan Chien7caf37e2012-02-03 22:56:04 +08002941
Logan Chien7e7fabc2012-04-10 18:59:11 +08002942llvm::Value* MethodCompiler::
2943EmitLoadVirtualCalleeMethodObjectAddr(int vtable_idx,
2944 llvm::Value* this_addr) {
2945 // Load class object of *this* pointer
TDYa127ee1f59b2012-04-25 00:56:40 -07002946 llvm::Value* class_object_addr =
2947 irb_.LoadFromObjectOffset(this_addr,
2948 Object::ClassOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -07002949 irb_.getJObjectTy(),
TDYa127d3e24c22012-05-05 20:54:19 -07002950 kTBAAConstJObject);
Logan Chien7caf37e2012-02-03 22:56:04 +08002951
Logan Chien7e7fabc2012-04-10 18:59:11 +08002952 // Load vtable address
TDYa127ee1f59b2012-04-25 00:56:40 -07002953 llvm::Value* vtable_addr =
2954 irb_.LoadFromObjectOffset(class_object_addr,
2955 Class::VTableOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -07002956 irb_.getJObjectTy(),
TDYa127d3e24c22012-05-05 20:54:19 -07002957 kTBAAConstJObject);
Logan Chien7e7fabc2012-04-10 18:59:11 +08002958
2959 // Load callee method object
2960 llvm::Value* vtable_idx_value =
2961 irb_.getPtrEquivInt(static_cast<uint64_t>(vtable_idx));
2962
2963 llvm::Value* method_field_addr =
2964 EmitArrayGEP(vtable_addr, vtable_idx_value, irb_.getJObjectTy(), kObject);
2965
TDYa127d3e24c22012-05-05 20:54:19 -07002966 return irb_.CreateLoad(method_field_addr, kTBAAConstJObject);
Logan Chien7e7fabc2012-04-10 18:59:11 +08002967}
2968
2969
2970llvm::Value* MethodCompiler::
2971EmitCallRuntimeForCalleeMethodObjectAddr(uint32_t callee_method_idx,
2972 InvokeType invoke_type,
2973 llvm::Value* this_addr,
2974 uint32_t dex_pc,
2975 bool is_fast_path) {
2976
2977 llvm::Function* runtime_func = NULL;
2978
2979 switch (invoke_type) {
2980 case kStatic:
2981 runtime_func = irb_.GetRuntime(FindStaticMethodWithAccessCheck);
2982 break;
2983
2984 case kDirect:
2985 runtime_func = irb_.GetRuntime(FindDirectMethodWithAccessCheck);
2986 break;
2987
2988 case kVirtual:
2989 runtime_func = irb_.GetRuntime(FindVirtualMethodWithAccessCheck);
2990 break;
2991
2992 case kSuper:
2993 runtime_func = irb_.GetRuntime(FindSuperMethodWithAccessCheck);
2994 break;
2995
2996 case kInterface:
2997 if (is_fast_path) {
2998 runtime_func = irb_.GetRuntime(FindInterfaceMethod);
2999 } else {
3000 runtime_func = irb_.GetRuntime(FindInterfaceMethodWithAccessCheck);
3001 }
3002 break;
3003 }
Logan Chien7caf37e2012-02-03 22:56:04 +08003004
3005 llvm::Value* callee_method_idx_value = irb_.getInt32(callee_method_idx);
3006
Logan Chien7e7fabc2012-04-10 18:59:11 +08003007 if (this_addr == NULL) {
3008 DCHECK_EQ(invoke_type, kStatic);
3009 this_addr = irb_.getJNull();
3010 }
3011
3012 llvm::Value* caller_method_object_addr = EmitLoadMethodObjectAddr();
3013
TDYa127da83d972012-04-18 00:21:49 -07003014 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
3015
TDYa127c8dc1012012-04-19 07:03:33 -07003016 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08003017
TDYa1270b686e52012-04-09 22:43:35 -07003018 llvm::Value* callee_method_object_addr =
TDYa127da83d972012-04-18 00:21:49 -07003019 irb_.CreateCall4(runtime_func,
Logan Chien7e7fabc2012-04-10 18:59:11 +08003020 callee_method_idx_value,
3021 this_addr,
TDYa127da83d972012-04-18 00:21:49 -07003022 caller_method_object_addr,
3023 thread_object_addr);
Logan Chien7caf37e2012-02-03 22:56:04 +08003024
3025 EmitGuard_ExceptionLandingPad(dex_pc);
3026
Logan Chien7e7fabc2012-04-10 18:59:11 +08003027 return callee_method_object_addr;
Logan Chien70f94b42011-12-27 17:49:11 +08003028}
3029
3030
3031void MethodCompiler::EmitInsn_Neg(uint32_t dex_pc,
3032 Instruction const* insn,
3033 JType op_jty) {
Logan Chien1b5685f2011-12-27 18:01:14 +08003034
Elliott Hughesadb8c672012-03-06 16:49:32 -08003035 DecodedInstruction dec_insn(insn);
Logan Chien1b5685f2011-12-27 18:01:14 +08003036
3037 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3038
Elliott Hughesadb8c672012-03-06 16:49:32 -08003039 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chien1b5685f2011-12-27 18:01:14 +08003040 llvm::Value* result_value = irb_.CreateNeg(src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003041 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chien1b5685f2011-12-27 18:01:14 +08003042
Logan Chien70f94b42011-12-27 17:49:11 +08003043 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3044}
3045
3046
3047void MethodCompiler::EmitInsn_Not(uint32_t dex_pc,
3048 Instruction const* insn,
3049 JType op_jty) {
Logan Chiene53750d2011-12-27 18:02:27 +08003050
Elliott Hughesadb8c672012-03-06 16:49:32 -08003051 DecodedInstruction dec_insn(insn);
Logan Chiene53750d2011-12-27 18:02:27 +08003052
3053 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3054
Elliott Hughesadb8c672012-03-06 16:49:32 -08003055 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chiene53750d2011-12-27 18:02:27 +08003056 llvm::Value* result_value =
3057 irb_.CreateXor(src_value, static_cast<uint64_t>(-1));
3058
Elliott Hughesadb8c672012-03-06 16:49:32 -08003059 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chiene53750d2011-12-27 18:02:27 +08003060
Logan Chien70f94b42011-12-27 17:49:11 +08003061 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3062}
3063
3064
3065void MethodCompiler::EmitInsn_SExt(uint32_t dex_pc,
3066 Instruction const* insn) {
Logan Chien61752ad2011-12-27 18:03:51 +08003067
Elliott Hughesadb8c672012-03-06 16:49:32 -08003068 DecodedInstruction dec_insn(insn);
Logan Chien61752ad2011-12-27 18:03:51 +08003069
Elliott Hughesadb8c672012-03-06 16:49:32 -08003070 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chien61752ad2011-12-27 18:03:51 +08003071 llvm::Value* result_value = irb_.CreateSExt(src_value, irb_.getJLongTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003072 EmitStoreDalvikReg(dec_insn.vA, kLong, kAccurate, result_value);
Logan Chien61752ad2011-12-27 18:03:51 +08003073
Logan Chien70f94b42011-12-27 17:49:11 +08003074 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3075}
3076
3077
3078void MethodCompiler::EmitInsn_Trunc(uint32_t dex_pc,
3079 Instruction const* insn) {
Logan Chien17a57662011-12-27 18:05:14 +08003080
Elliott Hughesadb8c672012-03-06 16:49:32 -08003081 DecodedInstruction dec_insn(insn);
Logan Chien17a57662011-12-27 18:05:14 +08003082
Elliott Hughesadb8c672012-03-06 16:49:32 -08003083 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kLong, kAccurate);
Logan Chien17a57662011-12-27 18:05:14 +08003084 llvm::Value* result_value = irb_.CreateTrunc(src_value, irb_.getJIntTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003085 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chien17a57662011-12-27 18:05:14 +08003086
Logan Chien70f94b42011-12-27 17:49:11 +08003087 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3088}
3089
3090
3091void MethodCompiler::EmitInsn_TruncAndSExt(uint32_t dex_pc,
3092 Instruction const* insn,
3093 unsigned N) {
Logan Chienb6744c52011-12-27 18:06:26 +08003094
Elliott Hughesadb8c672012-03-06 16:49:32 -08003095 DecodedInstruction dec_insn(insn);
Logan Chienb6744c52011-12-27 18:06:26 +08003096
Elliott Hughesadb8c672012-03-06 16:49:32 -08003097 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chienb6744c52011-12-27 18:06:26 +08003098
3099 llvm::Value* trunc_value =
3100 irb_.CreateTrunc(src_value, llvm::Type::getIntNTy(*context_, N));
3101
3102 llvm::Value* result_value = irb_.CreateSExt(trunc_value, irb_.getJIntTy());
3103
Elliott Hughesadb8c672012-03-06 16:49:32 -08003104 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chienb6744c52011-12-27 18:06:26 +08003105
Logan Chien70f94b42011-12-27 17:49:11 +08003106 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3107}
3108
3109
3110void MethodCompiler::EmitInsn_TruncAndZExt(uint32_t dex_pc,
3111 Instruction const* insn,
3112 unsigned N) {
Logan Chienb6744c52011-12-27 18:06:26 +08003113
Elliott Hughesadb8c672012-03-06 16:49:32 -08003114 DecodedInstruction dec_insn(insn);
Logan Chienb6744c52011-12-27 18:06:26 +08003115
Elliott Hughesadb8c672012-03-06 16:49:32 -08003116 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chienb6744c52011-12-27 18:06:26 +08003117
3118 llvm::Value* trunc_value =
3119 irb_.CreateTrunc(src_value, llvm::Type::getIntNTy(*context_, N));
3120
3121 llvm::Value* result_value = irb_.CreateZExt(trunc_value, irb_.getJIntTy());
3122
Elliott Hughesadb8c672012-03-06 16:49:32 -08003123 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chienb6744c52011-12-27 18:06:26 +08003124
Logan Chien70f94b42011-12-27 17:49:11 +08003125 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3126}
3127
3128
3129void MethodCompiler::EmitInsn_FNeg(uint32_t dex_pc,
3130 Instruction const* insn,
3131 JType op_jty) {
Logan Chien7a48b092011-12-27 18:07:45 +08003132
Elliott Hughesadb8c672012-03-06 16:49:32 -08003133 DecodedInstruction dec_insn(insn);
Logan Chien7a48b092011-12-27 18:07:45 +08003134
3135 DCHECK(op_jty == kFloat || op_jty == kDouble) << op_jty;
3136
Elliott Hughesadb8c672012-03-06 16:49:32 -08003137 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chien7a48b092011-12-27 18:07:45 +08003138 llvm::Value* result_value = irb_.CreateFNeg(src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003139 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chien7a48b092011-12-27 18:07:45 +08003140
Logan Chien70f94b42011-12-27 17:49:11 +08003141 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3142}
3143
3144
3145void MethodCompiler::EmitInsn_IntToFP(uint32_t dex_pc,
3146 Instruction const* insn,
3147 JType src_jty,
3148 JType dest_jty) {
Logan Chien62dd4532011-12-27 18:09:00 +08003149
Elliott Hughesadb8c672012-03-06 16:49:32 -08003150 DecodedInstruction dec_insn(insn);
Logan Chien62dd4532011-12-27 18:09:00 +08003151
3152 DCHECK(src_jty == kInt || src_jty == kLong) << src_jty;
3153 DCHECK(dest_jty == kFloat || dest_jty == kDouble) << dest_jty;
3154
Elliott Hughesadb8c672012-03-06 16:49:32 -08003155 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, src_jty, kAccurate);
Logan Chien62dd4532011-12-27 18:09:00 +08003156 llvm::Type* dest_type = irb_.getJType(dest_jty, kAccurate);
3157 llvm::Value* dest_value = irb_.CreateSIToFP(src_value, dest_type);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003158 EmitStoreDalvikReg(dec_insn.vA, dest_jty, kAccurate, dest_value);
Logan Chien62dd4532011-12-27 18:09:00 +08003159
Logan Chien70f94b42011-12-27 17:49:11 +08003160 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3161}
3162
3163
3164void MethodCompiler::EmitInsn_FPToInt(uint32_t dex_pc,
3165 Instruction const* insn,
3166 JType src_jty,
TDYa127a4746872012-04-11 23:48:55 -07003167 JType dest_jty,
3168 runtime_support::RuntimeId runtime_func_id) {
Logan Chien12dc1752011-12-27 18:10:15 +08003169
Elliott Hughesadb8c672012-03-06 16:49:32 -08003170 DecodedInstruction dec_insn(insn);
Logan Chien12dc1752011-12-27 18:10:15 +08003171
3172 DCHECK(src_jty == kFloat || src_jty == kDouble) << src_jty;
3173 DCHECK(dest_jty == kInt || dest_jty == kLong) << dest_jty;
3174
Elliott Hughesadb8c672012-03-06 16:49:32 -08003175 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, src_jty, kAccurate);
TDYa127a4746872012-04-11 23:48:55 -07003176 llvm::Value* dest_value = irb_.CreateCall(irb_.GetRuntime(runtime_func_id), src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003177 EmitStoreDalvikReg(dec_insn.vA, dest_jty, kAccurate, dest_value);
Logan Chien12dc1752011-12-27 18:10:15 +08003178
Logan Chien70f94b42011-12-27 17:49:11 +08003179 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3180}
3181
3182
3183void MethodCompiler::EmitInsn_FExt(uint32_t dex_pc,
3184 Instruction const* insn) {
Logan Chienc56ded92011-12-27 18:10:57 +08003185
Elliott Hughesadb8c672012-03-06 16:49:32 -08003186 DecodedInstruction dec_insn(insn);
Logan Chienc56ded92011-12-27 18:10:57 +08003187
Elliott Hughesadb8c672012-03-06 16:49:32 -08003188 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kFloat, kAccurate);
Logan Chienc56ded92011-12-27 18:10:57 +08003189 llvm::Value* result_value = irb_.CreateFPExt(src_value, irb_.getJDoubleTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003190 EmitStoreDalvikReg(dec_insn.vA, kDouble, kAccurate, result_value);
Logan Chienc56ded92011-12-27 18:10:57 +08003191
Logan Chien70f94b42011-12-27 17:49:11 +08003192 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3193}
3194
3195
3196void MethodCompiler::EmitInsn_FTrunc(uint32_t dex_pc,
3197 Instruction const* insn) {
Logan Chien927744f2011-12-27 18:11:52 +08003198
Elliott Hughesadb8c672012-03-06 16:49:32 -08003199 DecodedInstruction dec_insn(insn);
Logan Chien927744f2011-12-27 18:11:52 +08003200
Elliott Hughesadb8c672012-03-06 16:49:32 -08003201 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kDouble, kAccurate);
Logan Chien927744f2011-12-27 18:11:52 +08003202 llvm::Value* result_value = irb_.CreateFPTrunc(src_value, irb_.getJFloatTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003203 EmitStoreDalvikReg(dec_insn.vA, kFloat, kAccurate, result_value);
Logan Chien927744f2011-12-27 18:11:52 +08003204
Logan Chien70f94b42011-12-27 17:49:11 +08003205 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3206}
3207
3208
3209void MethodCompiler::EmitInsn_IntArithm(uint32_t dex_pc,
3210 Instruction const* insn,
3211 IntArithmKind arithm,
3212 JType op_jty,
3213 bool is_2addr) {
Logan Chienc3f7d962011-12-27 18:13:18 +08003214
Elliott Hughesadb8c672012-03-06 16:49:32 -08003215 DecodedInstruction dec_insn(insn);
Logan Chienc3f7d962011-12-27 18:13:18 +08003216
3217 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3218
3219 llvm::Value* src1_value;
3220 llvm::Value* src2_value;
3221
3222 if (is_2addr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003223 src1_value = EmitLoadDalvikReg(dec_insn.vA, op_jty, kAccurate);
3224 src2_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chienc3f7d962011-12-27 18:13:18 +08003225 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003226 src1_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
3227 src2_value = EmitLoadDalvikReg(dec_insn.vC, op_jty, kAccurate);
Logan Chienc3f7d962011-12-27 18:13:18 +08003228 }
3229
3230 llvm::Value* result_value =
3231 EmitIntArithmResultComputation(dex_pc, src1_value, src2_value,
3232 arithm, op_jty);
3233
Elliott Hughesadb8c672012-03-06 16:49:32 -08003234 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chienc3f7d962011-12-27 18:13:18 +08003235
Logan Chien70f94b42011-12-27 17:49:11 +08003236 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3237}
3238
3239
3240void MethodCompiler::EmitInsn_IntArithmImmediate(uint32_t dex_pc,
3241 Instruction const* insn,
3242 IntArithmKind arithm) {
Logan Chienc3f7d962011-12-27 18:13:18 +08003243
Elliott Hughesadb8c672012-03-06 16:49:32 -08003244 DecodedInstruction dec_insn(insn);
Logan Chienc3f7d962011-12-27 18:13:18 +08003245
Elliott Hughesadb8c672012-03-06 16:49:32 -08003246 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chienc3f7d962011-12-27 18:13:18 +08003247
Elliott Hughesadb8c672012-03-06 16:49:32 -08003248 llvm::Value* imm_value = irb_.getInt32(dec_insn.vC);
Logan Chienc3f7d962011-12-27 18:13:18 +08003249
3250 llvm::Value* result_value =
3251 EmitIntArithmResultComputation(dex_pc, src_value, imm_value, arithm, kInt);
3252
Elliott Hughesadb8c672012-03-06 16:49:32 -08003253 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chienc3f7d962011-12-27 18:13:18 +08003254
Logan Chien70f94b42011-12-27 17:49:11 +08003255 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3256}
3257
3258
Logan Chienc3f7d962011-12-27 18:13:18 +08003259llvm::Value*
3260MethodCompiler::EmitIntArithmResultComputation(uint32_t dex_pc,
3261 llvm::Value* lhs,
3262 llvm::Value* rhs,
3263 IntArithmKind arithm,
3264 JType op_jty) {
3265 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3266
3267 switch (arithm) {
3268 case kIntArithm_Add:
3269 return irb_.CreateAdd(lhs, rhs);
3270
3271 case kIntArithm_Sub:
3272 return irb_.CreateSub(lhs, rhs);
3273
3274 case kIntArithm_Mul:
3275 return irb_.CreateMul(lhs, rhs);
3276
3277 case kIntArithm_Div:
Logan Chienc3f7d962011-12-27 18:13:18 +08003278 case kIntArithm_Rem:
TDYa127f8641ce2012-04-02 06:40:40 -07003279 return EmitIntDivRemResultComputation(dex_pc, lhs, rhs, arithm, op_jty);
Logan Chienc3f7d962011-12-27 18:13:18 +08003280
3281 case kIntArithm_And:
3282 return irb_.CreateAnd(lhs, rhs);
3283
3284 case kIntArithm_Or:
3285 return irb_.CreateOr(lhs, rhs);
3286
3287 case kIntArithm_Xor:
3288 return irb_.CreateXor(lhs, rhs);
3289
Logan Chienc3f7d962011-12-27 18:13:18 +08003290 default:
3291 LOG(FATAL) << "Unknown integer arithmetic kind: " << arithm;
3292 return NULL;
3293 }
3294}
3295
3296
TDYa127f8641ce2012-04-02 06:40:40 -07003297llvm::Value*
3298MethodCompiler::EmitIntDivRemResultComputation(uint32_t dex_pc,
3299 llvm::Value* dividend,
3300 llvm::Value* divisor,
3301 IntArithmKind arithm,
3302 JType op_jty) {
3303 // Throw exception if the divisor is 0.
3304 EmitGuard_DivZeroException(dex_pc, divisor, op_jty);
3305
3306 // Check the special case: MININT / -1 = MININT
3307 // That case will cause overflow, which is undefined behavior in llvm.
3308 // So we check the divisor is -1 or not, if the divisor is -1, we do
3309 // the special path to avoid undefined behavior.
3310 llvm::Type* op_type = irb_.getJType(op_jty, kAccurate);
3311 llvm::Value* zero = irb_.getJZero(op_jty);
3312 llvm::Value* neg_one = llvm::ConstantInt::getSigned(op_type, -1);
3313 llvm::Value* result = irb_.CreateAlloca(op_type);
3314
3315 llvm::BasicBlock* eq_neg_one = CreateBasicBlockWithDexPC(dex_pc, "eq_neg_one");
3316 llvm::BasicBlock* ne_neg_one = CreateBasicBlockWithDexPC(dex_pc, "ne_neg_one");
3317 llvm::BasicBlock* neg_one_cont = CreateBasicBlockWithDexPC(dex_pc, "neg_one_cont");
3318
3319 llvm::Value* is_equal_neg_one = EmitConditionResult(divisor, neg_one, kCondBranch_EQ);
TDYa127ac7b5bb2012-05-11 13:17:49 -07003320 irb_.CreateCondBr(is_equal_neg_one, eq_neg_one, ne_neg_one, kUnlikely);
TDYa127f8641ce2012-04-02 06:40:40 -07003321
3322 // If divisor == -1
3323 irb_.SetInsertPoint(eq_neg_one);
3324 llvm::Value* eq_result;
3325 if (arithm == kIntArithm_Div) {
3326 // We can just change from "dividend div -1" to "neg dividend".
3327 // The sub don't care the sign/unsigned because of two's complement representation.
3328 // And the behavior is what we want:
3329 // -(2^n) (2^n)-1
3330 // MININT < k <= MAXINT -> mul k -1 = -k
3331 // MININT == k -> mul k -1 = k
3332 //
3333 // LLVM use sub to represent 'neg'
3334 eq_result = irb_.CreateSub(zero, dividend);
3335 } else {
3336 // Everything modulo -1 will be 0.
3337 eq_result = zero;
3338 }
TDYa127aba61122012-05-04 18:28:36 -07003339 irb_.CreateStore(eq_result, result, kTBAAStackTemp);
TDYa127f8641ce2012-04-02 06:40:40 -07003340 irb_.CreateBr(neg_one_cont);
3341
3342 // If divisor != -1, just do the division.
3343 irb_.SetInsertPoint(ne_neg_one);
3344 llvm::Value* ne_result;
3345 if (arithm == kIntArithm_Div) {
3346 ne_result = irb_.CreateSDiv(dividend, divisor);
3347 } else {
3348 ne_result = irb_.CreateSRem(dividend, divisor);
3349 }
TDYa127aba61122012-05-04 18:28:36 -07003350 irb_.CreateStore(ne_result, result, kTBAAStackTemp);
TDYa127f8641ce2012-04-02 06:40:40 -07003351 irb_.CreateBr(neg_one_cont);
3352
3353 irb_.SetInsertPoint(neg_one_cont);
TDYa127aba61122012-05-04 18:28:36 -07003354 return irb_.CreateLoad(result, kTBAAStackTemp);
TDYa127f8641ce2012-04-02 06:40:40 -07003355}
3356
3357
Logan Chien5539ad02012-04-02 14:36:55 +08003358void MethodCompiler::EmitInsn_IntShiftArithm(uint32_t dex_pc,
3359 Instruction const* insn,
3360 IntShiftArithmKind arithm,
3361 JType op_jty,
3362 bool is_2addr) {
3363
3364 DecodedInstruction dec_insn(insn);
3365
3366 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3367
3368 llvm::Value* src1_value;
3369 llvm::Value* src2_value;
3370
3371 // NOTE: The 2nd operand of the shift arithmetic instruction is
3372 // 32-bit integer regardless of the 1st operand.
3373 if (is_2addr) {
3374 src1_value = EmitLoadDalvikReg(dec_insn.vA, op_jty, kAccurate);
3375 src2_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
3376 } else {
3377 src1_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
3378 src2_value = EmitLoadDalvikReg(dec_insn.vC, kInt, kAccurate);
3379 }
3380
3381 llvm::Value* result_value =
3382 EmitIntShiftArithmResultComputation(dex_pc, src1_value, src2_value,
3383 arithm, op_jty);
3384
3385 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
3386
3387 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3388}
3389
3390
3391void MethodCompiler::
3392EmitInsn_IntShiftArithmImmediate(uint32_t dex_pc,
3393 Instruction const* insn,
3394 IntShiftArithmKind arithm) {
3395
3396 DecodedInstruction dec_insn(insn);
3397
3398 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
3399
3400 llvm::Value* imm_value = irb_.getInt32(dec_insn.vC);
3401
3402 llvm::Value* result_value =
3403 EmitIntShiftArithmResultComputation(dex_pc, src_value, imm_value,
3404 arithm, kInt);
3405
3406 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
3407
3408 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3409}
3410
3411
3412llvm::Value*
3413MethodCompiler::EmitIntShiftArithmResultComputation(uint32_t dex_pc,
3414 llvm::Value* lhs,
3415 llvm::Value* rhs,
3416 IntShiftArithmKind arithm,
3417 JType op_jty) {
3418 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3419
3420 if (op_jty == kInt) {
3421 rhs = irb_.CreateAnd(rhs, 0x1f);
3422 } else {
3423 llvm::Value* masked_rhs = irb_.CreateAnd(rhs, 0x3f);
3424 rhs = irb_.CreateZExt(masked_rhs, irb_.getJLongTy());
3425 }
3426
3427 switch (arithm) {
3428 case kIntArithm_Shl:
3429 return irb_.CreateShl(lhs, rhs);
3430
3431 case kIntArithm_Shr:
3432 return irb_.CreateAShr(lhs, rhs);
3433
3434 case kIntArithm_UShr:
3435 return irb_.CreateLShr(lhs, rhs);
3436
3437 default:
3438 LOG(FATAL) << "Unknown integer shift arithmetic kind: " << arithm;
3439 return NULL;
3440 }
3441}
3442
3443
Logan Chien70f94b42011-12-27 17:49:11 +08003444void MethodCompiler::EmitInsn_RSubImmediate(uint32_t dex_pc,
3445 Instruction const* insn) {
Logan Chien65c62d42011-12-27 18:14:18 +08003446
Elliott Hughesadb8c672012-03-06 16:49:32 -08003447 DecodedInstruction dec_insn(insn);
Logan Chien65c62d42011-12-27 18:14:18 +08003448
Elliott Hughesadb8c672012-03-06 16:49:32 -08003449 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
3450 llvm::Value* imm_value = irb_.getInt32(dec_insn.vC);
Logan Chien65c62d42011-12-27 18:14:18 +08003451 llvm::Value* result_value = irb_.CreateSub(imm_value, src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003452 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chien65c62d42011-12-27 18:14:18 +08003453
Logan Chien70f94b42011-12-27 17:49:11 +08003454 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3455}
3456
3457
3458void MethodCompiler::EmitInsn_FPArithm(uint32_t dex_pc,
3459 Instruction const* insn,
3460 FPArithmKind arithm,
3461 JType op_jty,
3462 bool is_2addr) {
Logan Chien76e1c792011-12-27 18:15:01 +08003463
Elliott Hughesadb8c672012-03-06 16:49:32 -08003464 DecodedInstruction dec_insn(insn);
Logan Chien76e1c792011-12-27 18:15:01 +08003465
3466 DCHECK(op_jty == kFloat || op_jty == kDouble) << op_jty;
3467
3468 llvm::Value* src1_value;
3469 llvm::Value* src2_value;
3470
3471 if (is_2addr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003472 src1_value = EmitLoadDalvikReg(dec_insn.vA, op_jty, kAccurate);
3473 src2_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chien76e1c792011-12-27 18:15:01 +08003474 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003475 src1_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
3476 src2_value = EmitLoadDalvikReg(dec_insn.vC, op_jty, kAccurate);
Logan Chien76e1c792011-12-27 18:15:01 +08003477 }
3478
3479 llvm::Value* result_value =
3480 EmitFPArithmResultComputation(dex_pc, src1_value, src2_value, arithm);
3481
Elliott Hughesadb8c672012-03-06 16:49:32 -08003482 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chien76e1c792011-12-27 18:15:01 +08003483
Logan Chien70f94b42011-12-27 17:49:11 +08003484 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3485}
3486
3487
Logan Chien76e1c792011-12-27 18:15:01 +08003488llvm::Value*
3489MethodCompiler::EmitFPArithmResultComputation(uint32_t dex_pc,
3490 llvm::Value *lhs,
3491 llvm::Value *rhs,
3492 FPArithmKind arithm) {
3493 switch (arithm) {
3494 case kFPArithm_Add:
3495 return irb_.CreateFAdd(lhs, rhs);
3496
3497 case kFPArithm_Sub:
3498 return irb_.CreateFSub(lhs, rhs);
3499
3500 case kFPArithm_Mul:
3501 return irb_.CreateFMul(lhs, rhs);
3502
3503 case kFPArithm_Div:
3504 return irb_.CreateFDiv(lhs, rhs);
3505
3506 case kFPArithm_Rem:
3507 return irb_.CreateFRem(lhs, rhs);
3508
3509 default:
3510 LOG(FATAL) << "Unknown floating-point arithmetic kind: " << arithm;
3511 return NULL;
3512 }
3513}
3514
3515
Logan Chienc3f7d962011-12-27 18:13:18 +08003516void MethodCompiler::EmitGuard_DivZeroException(uint32_t dex_pc,
3517 llvm::Value* denominator,
3518 JType op_jty) {
3519 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3520
3521 llvm::Constant* zero = irb_.getJZero(op_jty);
3522
3523 llvm::Value* equal_zero = irb_.CreateICmpEQ(denominator, zero);
3524
3525 llvm::BasicBlock* block_exception = CreateBasicBlockWithDexPC(dex_pc, "div0");
3526
3527 llvm::BasicBlock* block_continue = CreateBasicBlockWithDexPC(dex_pc, "cont");
3528
TDYa127ac7b5bb2012-05-11 13:17:49 -07003529 irb_.CreateCondBr(equal_zero, block_exception, block_continue, kUnlikely);
Logan Chienc3f7d962011-12-27 18:13:18 +08003530
3531 irb_.SetInsertPoint(block_exception);
TDYa127c8dc1012012-04-19 07:03:33 -07003532 EmitUpdateDexPC(dex_pc);
Logan Chienc3f7d962011-12-27 18:13:18 +08003533 irb_.CreateCall(irb_.GetRuntime(ThrowDivZeroException));
3534 EmitBranchExceptionLandingPad(dex_pc);
3535
3536 irb_.SetInsertPoint(block_continue);
3537}
3538
3539
Logan Chien61bb6142012-02-03 15:34:53 +08003540void MethodCompiler::EmitGuard_NullPointerException(uint32_t dex_pc,
3541 llvm::Value* object) {
3542 llvm::Value* equal_null = irb_.CreateICmpEQ(object, irb_.getJNull());
3543
3544 llvm::BasicBlock* block_exception =
3545 CreateBasicBlockWithDexPC(dex_pc, "nullp");
3546
3547 llvm::BasicBlock* block_continue =
3548 CreateBasicBlockWithDexPC(dex_pc, "cont");
3549
TDYa127ac7b5bb2012-05-11 13:17:49 -07003550 irb_.CreateCondBr(equal_null, block_exception, block_continue, kUnlikely);
Logan Chien61bb6142012-02-03 15:34:53 +08003551
3552 irb_.SetInsertPoint(block_exception);
TDYa127c8dc1012012-04-19 07:03:33 -07003553 EmitUpdateDexPC(dex_pc);
TDYa1273f9137d2012-04-08 15:59:19 -07003554 irb_.CreateCall(irb_.GetRuntime(ThrowNullPointerException), irb_.getInt32(dex_pc));
Logan Chien61bb6142012-02-03 15:34:53 +08003555 EmitBranchExceptionLandingPad(dex_pc);
3556
3557 irb_.SetInsertPoint(block_continue);
3558}
3559
3560
Logan Chienbb4d12a2012-02-17 14:10:01 +08003561llvm::Value* MethodCompiler::EmitLoadDexCacheAddr(MemberOffset offset) {
3562 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
3563
TDYa127ee1f59b2012-04-25 00:56:40 -07003564 return irb_.LoadFromObjectOffset(method_object_addr,
3565 offset.Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -07003566 irb_.getJObjectTy(),
TDYa127d3e24c22012-05-05 20:54:19 -07003567 kTBAAConstJObject);
Logan Chienbb4d12a2012-02-17 14:10:01 +08003568}
3569
3570
Logan Chienbb4d12a2012-02-17 14:10:01 +08003571llvm::Value* MethodCompiler::
3572EmitLoadDexCacheStaticStorageFieldAddr(uint32_t type_idx) {
3573 llvm::Value* static_storage_dex_cache_addr =
3574 EmitLoadDexCacheAddr(Method::DexCacheInitializedStaticStorageOffset());
3575
3576 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
3577
3578 return EmitArrayGEP(static_storage_dex_cache_addr, type_idx_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08003579 irb_.getJObjectTy(), kObject);
Logan Chienbb4d12a2012-02-17 14:10:01 +08003580}
3581
3582
3583llvm::Value* MethodCompiler::
3584EmitLoadDexCacheResolvedTypeFieldAddr(uint32_t type_idx) {
3585 llvm::Value* resolved_type_dex_cache_addr =
3586 EmitLoadDexCacheAddr(Method::DexCacheResolvedTypesOffset());
3587
3588 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
3589
3590 return EmitArrayGEP(resolved_type_dex_cache_addr, type_idx_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08003591 irb_.getJObjectTy(), kObject);
Logan Chienbb4d12a2012-02-17 14:10:01 +08003592}
3593
3594
3595llvm::Value* MethodCompiler::
Logan Chien61c65dc2012-02-29 03:22:30 +08003596EmitLoadDexCacheResolvedMethodFieldAddr(uint32_t method_idx) {
3597 llvm::Value* resolved_method_dex_cache_addr =
3598 EmitLoadDexCacheAddr(Method::DexCacheResolvedMethodsOffset());
3599
3600 llvm::Value* method_idx_value = irb_.getPtrEquivInt(method_idx);
3601
3602 return EmitArrayGEP(resolved_method_dex_cache_addr, method_idx_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08003603 irb_.getJObjectTy(), kObject);
Logan Chien61c65dc2012-02-29 03:22:30 +08003604}
3605
3606
3607llvm::Value* MethodCompiler::
Logan Chienbb4d12a2012-02-17 14:10:01 +08003608EmitLoadDexCacheStringFieldAddr(uint32_t string_idx) {
3609 llvm::Value* string_dex_cache_addr =
3610 EmitLoadDexCacheAddr(Method::DexCacheStringsOffset());
3611
3612 llvm::Value* string_idx_value = irb_.getPtrEquivInt(string_idx);
3613
3614 return EmitArrayGEP(string_dex_cache_addr, string_idx_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08003615 irb_.getJObjectTy(), kObject);
Logan Chienbb4d12a2012-02-17 14:10:01 +08003616}
3617
3618
Logan Chien83426162011-12-09 09:29:50 +08003619CompiledMethod *MethodCompiler::Compile() {
TDYa127cc1b4c32012-05-15 07:31:37 -07003620 // TODO: Use high-level IR to do this
3621 // Compute method info
3622 ComputeMethodInfo();
3623
Logan Chien0b827102011-12-20 19:46:14 +08003624 // Code generation
3625 CreateFunction();
3626
3627 EmitPrologue();
3628 EmitInstructions();
Logan Chienc670a8d2011-12-20 21:25:56 +08003629 EmitPrologueLastBranch();
Logan Chien0b827102011-12-20 19:46:14 +08003630
Logan Chiend6c239a2011-12-23 15:11:45 +08003631 // Verify the generated bitcode
TDYa127853cd092012-04-21 22:15:31 -07003632 VERIFY_LLVM_FUNCTION(*func_);
Logan Chiend6c239a2011-12-23 15:11:45 +08003633
Logan Chien8b977d32012-02-21 19:14:55 +08003634 // Add the memory usage approximation of the compilation unit
3635 cunit_->AddMemUsageApproximation(code_item_->insns_size_in_code_units_ * 900);
TDYa127cc1b4c32012-05-15 07:31:37 -07003636 // NOTE: From statistics, the bitcode size is 4.5 times bigger than the
Logan Chien8b977d32012-02-21 19:14:55 +08003637 // Dex file. Besides, we have to convert the code unit into bytes.
3638 // Thus, we got our magic number 9.
3639
Logan Chien110bcba2012-04-16 19:11:28 +08003640 CompiledMethod* compiled_method =
3641 new CompiledMethod(cunit_->GetInstructionSet(),
3642 cunit_->GetElfIndex(),
3643 elf_func_idx_);
3644
3645 cunit_->RegisterCompiledMethod(func_, compiled_method);
3646
3647 return compiled_method;
Logan Chien0b827102011-12-20 19:46:14 +08003648}
3649
3650
3651llvm::Value* MethodCompiler::EmitLoadMethodObjectAddr() {
3652 return func_->arg_begin();
Shih-wei Liaod1fec812012-02-13 09:51:10 -08003653}
Logan Chien83426162011-12-09 09:29:50 +08003654
3655
Logan Chien5bcc04e2012-01-30 14:15:12 +08003656void MethodCompiler::EmitBranchExceptionLandingPad(uint32_t dex_pc) {
3657 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
3658 irb_.CreateBr(lpad);
3659 } else {
3660 irb_.CreateBr(GetUnwindBasicBlock());
3661 }
3662}
3663
3664
3665void MethodCompiler::EmitGuard_ExceptionLandingPad(uint32_t dex_pc) {
3666 llvm::Value* exception_pending =
3667 irb_.CreateCall(irb_.GetRuntime(IsExceptionPending));
3668
3669 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
3670
3671 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
TDYa127ac7b5bb2012-05-11 13:17:49 -07003672 irb_.CreateCondBr(exception_pending, lpad, block_cont, kUnlikely);
Logan Chien5bcc04e2012-01-30 14:15:12 +08003673 } else {
TDYa127ac7b5bb2012-05-11 13:17:49 -07003674 irb_.CreateCondBr(exception_pending, GetUnwindBasicBlock(), block_cont, kUnlikely);
Logan Chien5bcc04e2012-01-30 14:15:12 +08003675 }
3676
3677 irb_.SetInsertPoint(block_cont);
3678}
3679
3680
Logan Chien924072f2012-01-30 15:07:24 +08003681void MethodCompiler::EmitGuard_GarbageCollectionSuspend(uint32_t dex_pc) {
TDYa127cc1b4c32012-05-15 07:31:37 -07003682 if (!method_info_.need_shadow_frame_entry) {
3683 return;
3684 }
3685
Logan Chien924072f2012-01-30 15:07:24 +08003686 llvm::Value* runtime_func = irb_.GetRuntime(TestSuspend);
TDYa127853cd092012-04-21 22:15:31 -07003687
3688 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
3689
TDYa127853cd092012-04-21 22:15:31 -07003690 irb_.CreateCall(runtime_func, thread_object_addr);
Logan Chien924072f2012-01-30 15:07:24 +08003691}
3692
3693
Logan Chiend6c239a2011-12-23 15:11:45 +08003694llvm::BasicBlock* MethodCompiler::
3695CreateBasicBlockWithDexPC(uint32_t dex_pc, char const* postfix) {
3696 std::string name;
3697
TDYa12767ae8ff2012-05-02 19:08:02 -07003698#if !defined(NDEBUG)
Logan Chiend6c239a2011-12-23 15:11:45 +08003699 if (postfix) {
TDYa12799489132012-04-29 01:27:58 -07003700 StringAppendF(&name, "B%04x.%s", dex_pc, postfix);
Logan Chiend6c239a2011-12-23 15:11:45 +08003701 } else {
TDYa12799489132012-04-29 01:27:58 -07003702 StringAppendF(&name, "B%04x", dex_pc);
Logan Chiend6c239a2011-12-23 15:11:45 +08003703 }
TDYa12767ae8ff2012-05-02 19:08:02 -07003704#endif
Logan Chiend6c239a2011-12-23 15:11:45 +08003705
3706 return llvm::BasicBlock::Create(*context_, name, func_);
3707}
3708
3709
3710llvm::BasicBlock* MethodCompiler::GetBasicBlock(uint32_t dex_pc) {
3711 DCHECK(dex_pc < code_item_->insns_size_in_code_units_);
3712
3713 llvm::BasicBlock* basic_block = basic_blocks_[dex_pc];
3714
3715 if (!basic_block) {
3716 basic_block = CreateBasicBlockWithDexPC(dex_pc);
3717 basic_blocks_[dex_pc] = basic_block;
3718 }
3719
3720 return basic_block;
3721}
3722
3723
3724llvm::BasicBlock*
3725MethodCompiler::GetNextBasicBlock(uint32_t dex_pc) {
3726 Instruction const* insn = Instruction::At(code_item_->insns_ + dex_pc);
3727 return GetBasicBlock(dex_pc + insn->SizeInCodeUnits());
3728}
3729
3730
Logan Chien5bcc04e2012-01-30 14:15:12 +08003731int32_t MethodCompiler::GetTryItemOffset(uint32_t dex_pc) {
3732 // TODO: Since we are emitting the dex instructions in ascending order
3733 // w.r.t. address, we can cache the lastest try item offset so that we
3734 // don't have to do binary search for every query.
3735
3736 int32_t min = 0;
3737 int32_t max = code_item_->tries_size_ - 1;
3738
3739 while (min <= max) {
3740 int32_t mid = min + (max - min) / 2;
3741
3742 DexFile::TryItem const* ti = DexFile::GetTryItems(*code_item_, mid);
3743 uint32_t start = ti->start_addr_;
3744 uint32_t end = start + ti->insn_count_;
3745
3746 if (dex_pc < start) {
3747 max = mid - 1;
3748 } else if (dex_pc >= end) {
3749 min = mid + 1;
3750 } else {
3751 return mid; // found
3752 }
3753 }
3754
3755 return -1; // not found
3756}
3757
3758
3759llvm::BasicBlock* MethodCompiler::GetLandingPadBasicBlock(uint32_t dex_pc) {
3760 // Find the try item for this address in this method
3761 int32_t ti_offset = GetTryItemOffset(dex_pc);
3762
3763 if (ti_offset == -1) {
3764 return NULL; // No landing pad is available for this address.
3765 }
3766
3767 // Check for the existing landing pad basic block
3768 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
3769 llvm::BasicBlock* block_lpad = basic_block_landing_pads_[ti_offset];
3770
3771 if (block_lpad) {
3772 // We have generated landing pad for this try item already. Return the
3773 // same basic block.
3774 return block_lpad;
3775 }
3776
3777 // Get try item from code item
3778 DexFile::TryItem const* ti = DexFile::GetTryItems(*code_item_, ti_offset);
3779
TDYa12767ae8ff2012-05-02 19:08:02 -07003780 std::string lpadname;
3781
3782#if !defined(NDEBUG)
3783 StringAppendF(&lpadname, "lpad%d_%04x_to_%04x", ti_offset, ti->start_addr_, ti->handler_off_);
3784#endif
3785
Logan Chien5bcc04e2012-01-30 14:15:12 +08003786 // Create landing pad basic block
TDYa12767ae8ff2012-05-02 19:08:02 -07003787 block_lpad = llvm::BasicBlock::Create(*context_, lpadname, func_);
Logan Chien5bcc04e2012-01-30 14:15:12 +08003788
3789 // Change IRBuilder insert point
3790 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3791 irb_.SetInsertPoint(block_lpad);
3792
3793 // Find catch block with matching type
3794 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
3795
Logan Chien736df022012-04-27 16:25:57 +08003796 llvm::Value* ti_offset_value = irb_.getInt32(ti_offset);
Logan Chien5bcc04e2012-01-30 14:15:12 +08003797
3798 llvm::Value* catch_handler_index_value =
3799 irb_.CreateCall2(irb_.GetRuntime(FindCatchBlock),
Logan Chien736df022012-04-27 16:25:57 +08003800 method_object_addr, ti_offset_value);
Logan Chien5bcc04e2012-01-30 14:15:12 +08003801
3802 // Switch instruction (Go to unwind basic block by default)
3803 llvm::SwitchInst* sw =
3804 irb_.CreateSwitch(catch_handler_index_value, GetUnwindBasicBlock());
3805
3806 // Cases with matched catch block
3807 CatchHandlerIterator iter(*code_item_, ti->start_addr_);
3808
3809 for (uint32_t c = 0; iter.HasNext(); iter.Next(), ++c) {
3810 sw->addCase(irb_.getInt32(c), GetBasicBlock(iter.GetHandlerAddress()));
3811 }
3812
3813 // Restore the orignal insert point for IRBuilder
3814 irb_.restoreIP(irb_ip_original);
3815
3816 // Cache this landing pad
3817 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
3818 basic_block_landing_pads_[ti_offset] = block_lpad;
3819
3820 return block_lpad;
3821}
3822
3823
3824llvm::BasicBlock* MethodCompiler::GetUnwindBasicBlock() {
3825 // Check the existing unwinding baisc block block
3826 if (basic_block_unwind_ != NULL) {
3827 return basic_block_unwind_;
3828 }
3829
3830 // Create new basic block for unwinding
3831 basic_block_unwind_ =
3832 llvm::BasicBlock::Create(*context_, "exception_unwind", func_);
3833
3834 // Change IRBuilder insert point
3835 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3836 irb_.SetInsertPoint(basic_block_unwind_);
3837
Logan Chien8dfcbea2012-02-17 18:50:32 +08003838 // Pop the shadow frame
3839 EmitPopShadowFrame();
3840
Logan Chien5bcc04e2012-01-30 14:15:12 +08003841 // Emit the code to return default value (zero) for the given return type.
Logan Chiendd361c92012-04-10 23:40:37 +08003842 char ret_shorty = oat_compilation_unit_->GetShorty()[0];
Logan Chien5bcc04e2012-01-30 14:15:12 +08003843 if (ret_shorty == 'V') {
3844 irb_.CreateRetVoid();
3845 } else {
3846 irb_.CreateRet(irb_.getJZero(ret_shorty));
3847 }
3848
3849 // Restore the orignal insert point for IRBuilder
3850 irb_.restoreIP(irb_ip_original);
3851
3852 return basic_block_unwind_;
3853}
3854
3855
Logan Chienc670a8d2011-12-20 21:25:56 +08003856llvm::Value* MethodCompiler::AllocDalvikLocalVarReg(RegCategory cat,
3857 uint32_t reg_idx) {
TDYa12767ae8ff2012-05-02 19:08:02 -07003858 // Get reg_type and reg_name from DalvikReg
3859 llvm::Type* reg_type = DalvikReg::GetRegCategoryEquivSizeTy(irb_, cat);
3860 std::string reg_name;
3861
3862#if !defined(NDEBUG)
3863 StringAppendF(&reg_name, "%c%u", DalvikReg::GetRegCategoryNamePrefix(cat), reg_idx);
3864#endif
Logan Chienc670a8d2011-12-20 21:25:56 +08003865
3866 // Save current IR builder insert point
3867 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
TDYa12767ae8ff2012-05-02 19:08:02 -07003868 irb_.SetInsertPoint(basic_block_reg_alloca_);
Logan Chienc670a8d2011-12-20 21:25:56 +08003869
3870 // Alloca
TDYa12767ae8ff2012-05-02 19:08:02 -07003871 llvm::Value* reg_addr = irb_.CreateAlloca(reg_type, 0, reg_name);
Logan Chienc670a8d2011-12-20 21:25:56 +08003872
3873 // Restore IRBuilder insert point
3874 irb_.restoreIP(irb_ip_original);
3875
3876 DCHECK_NE(reg_addr, static_cast<llvm::Value*>(NULL));
3877 return reg_addr;
3878}
3879
3880
TDYa1277f5b9be2012-04-29 01:31:49 -07003881llvm::Value* MethodCompiler::AllocShadowFrameEntry(uint32_t reg_idx) {
TDYa1271d7e5102012-05-13 09:27:05 -07003882 if (reg_to_shadow_frame_index_[reg_idx] == -1) {
3883 // This register dosen't need ShadowFrame entry
3884 return NULL;
3885 }
3886
TDYa127cc1b4c32012-05-15 07:31:37 -07003887 if (!method_info_.need_shadow_frame_entry) {
3888 return NULL;
3889 }
3890
TDYa12767ae8ff2012-05-02 19:08:02 -07003891 std::string reg_name;
3892
3893#if !defined(NDEBUG)
3894 StringAppendF(&reg_name, "o%u", reg_idx);
3895#endif
3896
TDYa1277f5b9be2012-04-29 01:31:49 -07003897 // Save current IR builder insert point
3898 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3899
3900 irb_.SetInsertPoint(basic_block_shadow_frame_alloca_);
3901
3902 llvm::Value* gep_index[] = {
3903 irb_.getInt32(0), // No pointer displacement
3904 irb_.getInt32(1), // SIRT
TDYa1271d7e5102012-05-13 09:27:05 -07003905 irb_.getInt32(reg_to_shadow_frame_index_[reg_idx]) // Pointer field
TDYa1277f5b9be2012-04-29 01:31:49 -07003906 };
3907
TDYa12767ae8ff2012-05-02 19:08:02 -07003908 llvm::Value* reg_addr = irb_.CreateGEP(shadow_frame_, gep_index, reg_name);
TDYa1277f5b9be2012-04-29 01:31:49 -07003909
3910 // Restore IRBuilder insert point
3911 irb_.restoreIP(irb_ip_original);
3912
3913 DCHECK_NE(reg_addr, static_cast<llvm::Value*>(NULL));
3914 return reg_addr;
3915}
3916
3917
Logan Chienc670a8d2011-12-20 21:25:56 +08003918llvm::Value* MethodCompiler::AllocDalvikRetValReg(RegCategory cat) {
TDYa12767ae8ff2012-05-02 19:08:02 -07003919 // Get reg_type and reg_name from DalvikReg
3920 llvm::Type* reg_type = DalvikReg::GetRegCategoryEquivSizeTy(irb_, cat);
3921 std::string reg_name;
3922
3923#if !defined(NDEBUG)
3924 StringAppendF(&reg_name, "%c_res", DalvikReg::GetRegCategoryNamePrefix(cat));
3925#endif
3926
Logan Chienc670a8d2011-12-20 21:25:56 +08003927 // Save current IR builder insert point
3928 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
TDYa12767ae8ff2012-05-02 19:08:02 -07003929 irb_.SetInsertPoint(basic_block_reg_alloca_);
Logan Chienc670a8d2011-12-20 21:25:56 +08003930
3931 // Alloca
TDYa12767ae8ff2012-05-02 19:08:02 -07003932 llvm::Value* reg_addr = irb_.CreateAlloca(reg_type, 0, reg_name);
Logan Chienc670a8d2011-12-20 21:25:56 +08003933
3934 // Restore IRBuilder insert point
3935 irb_.restoreIP(irb_ip_original);
3936
3937 DCHECK_NE(reg_addr, static_cast<llvm::Value*>(NULL));
3938 return reg_addr;
3939}
3940
3941
Logan Chien8dfcbea2012-02-17 18:50:32 +08003942void MethodCompiler::EmitPopShadowFrame() {
TDYa127cc1b4c32012-05-15 07:31:37 -07003943 if (!method_info_.need_shadow_frame) {
3944 return;
3945 }
Logan Chien8dfcbea2012-02-17 18:50:32 +08003946 irb_.CreateCall(irb_.GetRuntime(PopShadowFrame));
3947}
3948
3949
TDYa127c8dc1012012-04-19 07:03:33 -07003950void MethodCompiler::EmitUpdateDexPC(uint32_t dex_pc) {
TDYa127cc1b4c32012-05-15 07:31:37 -07003951 if (!method_info_.need_shadow_frame) {
3952 return;
3953 }
TDYa127c8dc1012012-04-19 07:03:33 -07003954 irb_.StoreToObjectOffset(shadow_frame_,
3955 ShadowFrame::DexPCOffset(),
TDYa127aba61122012-05-04 18:28:36 -07003956 irb_.getInt32(dex_pc),
TDYa127d955bec2012-05-11 10:54:02 -07003957 kTBAAShadowFrame);
Logan Chien8dfcbea2012-02-17 18:50:32 +08003958}
3959
3960
TDYa127cc1b4c32012-05-15 07:31:37 -07003961// TODO: Use high-level IR to do this
3962void MethodCompiler::ComputeMethodInfo() {
3963 // If this method is static, we set the "this" register index to -1. So we don't worry about this
3964 // method is static or not in the following comparison.
3965 int64_t this_reg_idx = (oat_compilation_unit_->IsStatic()) ?
3966 (-1) :
3967 (code_item_->registers_size_ - code_item_->ins_size_);
3968 bool has_invoke = false;
3969 bool may_have_loop = false;
3970 bool modify_this = false;
3971 bool may_throw_exception = false;
3972
3973 Instruction const* insn;
3974 for (uint32_t dex_pc = 0;
3975 dex_pc < code_item_->insns_size_in_code_units_;
3976 dex_pc += insn->SizeInCodeUnits()) {
3977 insn = Instruction::At(code_item_->insns_ + dex_pc);
3978 DecodedInstruction dec_insn(insn);
3979
3980 switch (insn->Opcode()) {
3981 case Instruction::NOP:
3982 break;
3983
3984 case Instruction::MOVE:
3985 case Instruction::MOVE_FROM16:
3986 case Instruction::MOVE_16:
3987 case Instruction::MOVE_WIDE:
3988 case Instruction::MOVE_WIDE_FROM16:
3989 case Instruction::MOVE_WIDE_16:
3990 case Instruction::MOVE_OBJECT:
3991 case Instruction::MOVE_OBJECT_FROM16:
3992 case Instruction::MOVE_OBJECT_16:
3993 case Instruction::MOVE_RESULT:
3994 case Instruction::MOVE_RESULT_WIDE:
3995 case Instruction::MOVE_RESULT_OBJECT:
3996 case Instruction::MOVE_EXCEPTION:
3997 if (dec_insn.vA == this_reg_idx) {
3998 modify_this = true;
3999 }
4000 break;
4001
4002 case Instruction::RETURN_VOID:
4003 case Instruction::RETURN:
4004 case Instruction::RETURN_WIDE:
4005 case Instruction::RETURN_OBJECT:
4006 break;
4007
4008 case Instruction::CONST_4:
4009 case Instruction::CONST_16:
4010 case Instruction::CONST:
4011 case Instruction::CONST_HIGH16:
4012 case Instruction::CONST_WIDE_16:
4013 case Instruction::CONST_WIDE_32:
4014 case Instruction::CONST_WIDE:
4015 case Instruction::CONST_WIDE_HIGH16:
4016 if (dec_insn.vA == this_reg_idx) {
4017 modify_this = true;
4018 }
4019 break;
4020
4021 case Instruction::CONST_STRING:
4022 case Instruction::CONST_STRING_JUMBO:
4023 // TODO: Will the ResolveString throw exception?
4024 if (!compiler_->CanAssumeStringIsPresentInDexCache(dex_cache_, dec_insn.vB)) {
4025 may_throw_exception = true;
4026 }
4027 if (dec_insn.vA == this_reg_idx) {
4028 modify_this = true;
4029 }
4030 break;
4031
4032 case Instruction::CONST_CLASS:
4033 may_throw_exception = true;
4034 if (dec_insn.vA == this_reg_idx) {
4035 modify_this = true;
4036 }
4037 break;
4038
4039 case Instruction::MONITOR_ENTER:
4040 case Instruction::MONITOR_EXIT:
4041 case Instruction::CHECK_CAST:
4042 may_throw_exception = true;
4043 break;
4044
4045 case Instruction::INSTANCE_OF:
4046 may_throw_exception = true;
4047 if (dec_insn.vA == this_reg_idx) {
4048 modify_this = true;
4049 }
4050 break;
4051
4052 case Instruction::ARRAY_LENGTH:
4053 if (dec_insn.vA == this_reg_idx) {
4054 modify_this = true;
4055 }
4056 break;
4057
4058 case Instruction::NEW_INSTANCE:
4059 case Instruction::NEW_ARRAY:
4060 may_throw_exception = true;
4061 if (dec_insn.vA == this_reg_idx) {
4062 modify_this = true;
4063 }
4064 break;
4065
4066 case Instruction::FILLED_NEW_ARRAY:
4067 case Instruction::FILLED_NEW_ARRAY_RANGE:
4068 case Instruction::FILL_ARRAY_DATA:
4069 case Instruction::THROW:
4070 may_throw_exception = true;
4071 break;
4072
4073 case Instruction::GOTO:
4074 case Instruction::GOTO_16:
4075 case Instruction::GOTO_32:
4076 {
4077 int32_t branch_offset = dec_insn.vA;
4078 if (branch_offset <= 0) {
4079 Instruction const* target = Instruction::At(code_item_->insns_ + dex_pc + branch_offset);
4080 // According to the statistics, there are a few methods have "fake" back edge, which means
4081 // the backedge is not for a loop.
4082 // The most "fake" edges in the small methods are just branches to a return instruction. So
4083 // we can do an simple check to avoid false loop detection. After we have a high-level IR
4084 // before IRBuilder, we should remove this trick.
4085 switch (target->Opcode()) {
4086 default:
4087 may_have_loop = true;
4088 break;
4089 case Instruction::RETURN_VOID:
4090 case Instruction::RETURN:
4091 case Instruction::RETURN_WIDE:
4092 case Instruction::RETURN_OBJECT:
4093 break;
4094 }
4095 }
4096 }
4097 break;
4098
4099 case Instruction::PACKED_SWITCH:
4100 case Instruction::SPARSE_SWITCH:
4101 break;
4102
4103 case Instruction::CMPL_FLOAT:
4104 case Instruction::CMPG_FLOAT:
4105 case Instruction::CMPL_DOUBLE:
4106 case Instruction::CMPG_DOUBLE:
4107 case Instruction::CMP_LONG:
4108 if (dec_insn.vA == this_reg_idx) {
4109 modify_this = true;
4110 }
4111 break;
4112
4113 case Instruction::IF_EQ:
4114 case Instruction::IF_NE:
4115 case Instruction::IF_LT:
4116 case Instruction::IF_GE:
4117 case Instruction::IF_GT:
4118 case Instruction::IF_LE:
4119 {
4120 int32_t branch_offset = dec_insn.vC;
4121 if (branch_offset <= 0) {
4122 Instruction const* target = Instruction::At(code_item_->insns_ + dex_pc + branch_offset);
4123 switch (target->Opcode()) {
4124 default:
4125 may_have_loop = true;
4126 break;
4127 case Instruction::RETURN_VOID:
4128 case Instruction::RETURN:
4129 case Instruction::RETURN_WIDE:
4130 case Instruction::RETURN_OBJECT:
4131 break;
4132 }
4133 }
4134 }
4135 break;
4136
4137 case Instruction::IF_EQZ:
4138 case Instruction::IF_NEZ:
4139 case Instruction::IF_LTZ:
4140 case Instruction::IF_GEZ:
4141 case Instruction::IF_GTZ:
4142 case Instruction::IF_LEZ:
4143 {
4144 int32_t branch_offset = dec_insn.vB;
4145 if (branch_offset <= 0) {
4146 Instruction const* target = Instruction::At(code_item_->insns_ + dex_pc + branch_offset);
4147 switch (target->Opcode()) {
4148 default:
4149 may_have_loop = true;
4150 break;
4151 case Instruction::RETURN_VOID:
4152 case Instruction::RETURN:
4153 case Instruction::RETURN_WIDE:
4154 case Instruction::RETURN_OBJECT:
4155 break;
4156 }
4157 }
4158 }
4159 break;
4160
4161 case Instruction::AGET:
4162 case Instruction::AGET_WIDE:
4163 case Instruction::AGET_OBJECT:
4164 case Instruction::AGET_BOOLEAN:
4165 case Instruction::AGET_BYTE:
4166 case Instruction::AGET_CHAR:
4167 case Instruction::AGET_SHORT:
4168 may_throw_exception = true;
4169 if (dec_insn.vA == this_reg_idx) {
4170 modify_this = true;
4171 }
4172 break;
4173
4174 case Instruction::APUT:
4175 case Instruction::APUT_WIDE:
4176 case Instruction::APUT_OBJECT:
4177 case Instruction::APUT_BOOLEAN:
4178 case Instruction::APUT_BYTE:
4179 case Instruction::APUT_CHAR:
4180 case Instruction::APUT_SHORT:
4181 may_throw_exception = true;
4182 break;
4183
4184 case Instruction::IGET:
4185 case Instruction::IGET_WIDE:
4186 case Instruction::IGET_OBJECT:
4187 case Instruction::IGET_BOOLEAN:
4188 case Instruction::IGET_BYTE:
4189 case Instruction::IGET_CHAR:
4190 case Instruction::IGET_SHORT:
4191 {
4192 if (dec_insn.vA == this_reg_idx) {
4193 modify_this = true;
4194 }
4195 uint32_t reg_idx = dec_insn.vB;
4196 uint32_t field_idx = dec_insn.vC;
4197 int field_offset;
4198 bool is_volatile;
4199 bool is_fast_path = compiler_->ComputeInstanceFieldInfo(
4200 field_idx, oat_compilation_unit_, field_offset, is_volatile, false);
4201 if (!is_fast_path) {
4202 may_throw_exception = true;
4203 } else if (reg_idx != this_reg_idx) {
4204 // NullPointerException
4205 may_throw_exception = true;
4206 }
4207 }
4208 break;
4209
4210 case Instruction::IPUT:
4211 case Instruction::IPUT_WIDE:
4212 case Instruction::IPUT_OBJECT:
4213 case Instruction::IPUT_BOOLEAN:
4214 case Instruction::IPUT_BYTE:
4215 case Instruction::IPUT_CHAR:
4216 case Instruction::IPUT_SHORT:
4217 {
4218 uint32_t reg_idx = dec_insn.vB;
4219 uint32_t field_idx = dec_insn.vC;
4220 int field_offset;
4221 bool is_volatile;
4222 bool is_fast_path = compiler_->ComputeInstanceFieldInfo(
4223 field_idx, oat_compilation_unit_, field_offset, is_volatile, true);
4224 if (!is_fast_path) {
4225 may_throw_exception = true;
4226 } else if (reg_idx != this_reg_idx) {
4227 // NullPointerException
4228 may_throw_exception = true;
4229 }
4230 }
4231 break;
4232
4233 case Instruction::SGET:
4234 case Instruction::SGET_WIDE:
4235 case Instruction::SGET_OBJECT:
4236 case Instruction::SGET_BOOLEAN:
4237 case Instruction::SGET_BYTE:
4238 case Instruction::SGET_CHAR:
4239 case Instruction::SGET_SHORT:
4240 {
4241 if (dec_insn.vA == this_reg_idx) {
4242 modify_this = true;
4243 }
4244 uint32_t field_idx = dec_insn.vB;
4245
4246 int field_offset;
4247 int ssb_index;
4248 bool is_referrers_class;
4249 bool is_volatile;
4250
4251 bool is_fast_path = compiler_->ComputeStaticFieldInfo(
4252 field_idx, oat_compilation_unit_, field_offset, ssb_index,
4253 is_referrers_class, is_volatile, false);
4254 if (!is_fast_path || !is_referrers_class) {
4255 may_throw_exception = true;
4256 }
4257 }
4258 break;
4259
4260 case Instruction::SPUT:
4261 case Instruction::SPUT_WIDE:
4262 case Instruction::SPUT_OBJECT:
4263 case Instruction::SPUT_BOOLEAN:
4264 case Instruction::SPUT_BYTE:
4265 case Instruction::SPUT_CHAR:
4266 case Instruction::SPUT_SHORT:
4267 {
4268 uint32_t field_idx = dec_insn.vB;
4269
4270 int field_offset;
4271 int ssb_index;
4272 bool is_referrers_class;
4273 bool is_volatile;
4274
4275 bool is_fast_path = compiler_->ComputeStaticFieldInfo(
4276 field_idx, oat_compilation_unit_, field_offset, ssb_index,
4277 is_referrers_class, is_volatile, true);
4278 if (!is_fast_path || !is_referrers_class) {
4279 may_throw_exception = true;
4280 }
4281 }
4282 break;
4283
4284
4285 case Instruction::INVOKE_VIRTUAL:
4286 case Instruction::INVOKE_SUPER:
4287 case Instruction::INVOKE_DIRECT:
4288 case Instruction::INVOKE_STATIC:
4289 case Instruction::INVOKE_INTERFACE:
4290 case Instruction::INVOKE_VIRTUAL_RANGE:
4291 case Instruction::INVOKE_SUPER_RANGE:
4292 case Instruction::INVOKE_DIRECT_RANGE:
4293 case Instruction::INVOKE_STATIC_RANGE:
4294 case Instruction::INVOKE_INTERFACE_RANGE:
4295 has_invoke = true;
4296 may_throw_exception = true;
4297 break;
4298
4299 case Instruction::NEG_INT:
4300 case Instruction::NOT_INT:
4301 case Instruction::NEG_LONG:
4302 case Instruction::NOT_LONG:
4303 case Instruction::NEG_FLOAT:
4304 case Instruction::NEG_DOUBLE:
4305 case Instruction::INT_TO_LONG:
4306 case Instruction::INT_TO_FLOAT:
4307 case Instruction::INT_TO_DOUBLE:
4308 case Instruction::LONG_TO_INT:
4309 case Instruction::LONG_TO_FLOAT:
4310 case Instruction::LONG_TO_DOUBLE:
4311 case Instruction::FLOAT_TO_INT:
4312 case Instruction::FLOAT_TO_LONG:
4313 case Instruction::FLOAT_TO_DOUBLE:
4314 case Instruction::DOUBLE_TO_INT:
4315 case Instruction::DOUBLE_TO_LONG:
4316 case Instruction::DOUBLE_TO_FLOAT:
4317 case Instruction::INT_TO_BYTE:
4318 case Instruction::INT_TO_CHAR:
4319 case Instruction::INT_TO_SHORT:
4320 case Instruction::ADD_INT:
4321 case Instruction::SUB_INT:
4322 case Instruction::MUL_INT:
4323 case Instruction::AND_INT:
4324 case Instruction::OR_INT:
4325 case Instruction::XOR_INT:
4326 case Instruction::SHL_INT:
4327 case Instruction::SHR_INT:
4328 case Instruction::USHR_INT:
4329 case Instruction::ADD_LONG:
4330 case Instruction::SUB_LONG:
4331 case Instruction::MUL_LONG:
4332 case Instruction::AND_LONG:
4333 case Instruction::OR_LONG:
4334 case Instruction::XOR_LONG:
4335 case Instruction::SHL_LONG:
4336 case Instruction::SHR_LONG:
4337 case Instruction::USHR_LONG:
4338 case Instruction::ADD_INT_2ADDR:
4339 case Instruction::SUB_INT_2ADDR:
4340 case Instruction::MUL_INT_2ADDR:
4341 case Instruction::AND_INT_2ADDR:
4342 case Instruction::OR_INT_2ADDR:
4343 case Instruction::XOR_INT_2ADDR:
4344 case Instruction::SHL_INT_2ADDR:
4345 case Instruction::SHR_INT_2ADDR:
4346 case Instruction::USHR_INT_2ADDR:
4347 case Instruction::ADD_LONG_2ADDR:
4348 case Instruction::SUB_LONG_2ADDR:
4349 case Instruction::MUL_LONG_2ADDR:
4350 case Instruction::AND_LONG_2ADDR:
4351 case Instruction::OR_LONG_2ADDR:
4352 case Instruction::XOR_LONG_2ADDR:
4353 case Instruction::SHL_LONG_2ADDR:
4354 case Instruction::SHR_LONG_2ADDR:
4355 case Instruction::USHR_LONG_2ADDR:
4356 if (dec_insn.vA == this_reg_idx) {
4357 modify_this = true;
4358 }
4359 break;
4360
4361 case Instruction::DIV_INT:
4362 case Instruction::REM_INT:
4363 case Instruction::DIV_LONG:
4364 case Instruction::REM_LONG:
4365 case Instruction::DIV_INT_2ADDR:
4366 case Instruction::REM_INT_2ADDR:
4367 case Instruction::DIV_LONG_2ADDR:
4368 case Instruction::REM_LONG_2ADDR:
4369 may_throw_exception = true;
4370 if (dec_insn.vA == this_reg_idx) {
4371 modify_this = true;
4372 }
4373 break;
4374
4375 case Instruction::ADD_FLOAT:
4376 case Instruction::SUB_FLOAT:
4377 case Instruction::MUL_FLOAT:
4378 case Instruction::DIV_FLOAT:
4379 case Instruction::REM_FLOAT:
4380 case Instruction::ADD_DOUBLE:
4381 case Instruction::SUB_DOUBLE:
4382 case Instruction::MUL_DOUBLE:
4383 case Instruction::DIV_DOUBLE:
4384 case Instruction::REM_DOUBLE:
4385 case Instruction::ADD_FLOAT_2ADDR:
4386 case Instruction::SUB_FLOAT_2ADDR:
4387 case Instruction::MUL_FLOAT_2ADDR:
4388 case Instruction::DIV_FLOAT_2ADDR:
4389 case Instruction::REM_FLOAT_2ADDR:
4390 case Instruction::ADD_DOUBLE_2ADDR:
4391 case Instruction::SUB_DOUBLE_2ADDR:
4392 case Instruction::MUL_DOUBLE_2ADDR:
4393 case Instruction::DIV_DOUBLE_2ADDR:
4394 case Instruction::REM_DOUBLE_2ADDR:
4395 if (dec_insn.vA == this_reg_idx) {
4396 modify_this = true;
4397 }
4398 break;
4399
4400 case Instruction::ADD_INT_LIT16:
4401 case Instruction::ADD_INT_LIT8:
4402 case Instruction::RSUB_INT:
4403 case Instruction::RSUB_INT_LIT8:
4404 case Instruction::MUL_INT_LIT16:
4405 case Instruction::MUL_INT_LIT8:
4406 case Instruction::AND_INT_LIT16:
4407 case Instruction::AND_INT_LIT8:
4408 case Instruction::OR_INT_LIT16:
4409 case Instruction::OR_INT_LIT8:
4410 case Instruction::XOR_INT_LIT16:
4411 case Instruction::XOR_INT_LIT8:
4412 case Instruction::SHL_INT_LIT8:
4413 case Instruction::SHR_INT_LIT8:
4414 case Instruction::USHR_INT_LIT8:
4415 if (dec_insn.vA == this_reg_idx) {
4416 modify_this = true;
4417 }
4418 break;
4419 case Instruction::DIV_INT_LIT16:
4420 case Instruction::DIV_INT_LIT8:
4421 case Instruction::REM_INT_LIT16:
4422 case Instruction::REM_INT_LIT8:
4423 if (dec_insn.vC == 0) {
4424 may_throw_exception = true;
4425 }
4426 if (dec_insn.vA == this_reg_idx) {
4427 modify_this = true;
4428 }
4429 break;
4430
4431 case Instruction::THROW_VERIFICATION_ERROR:
4432 may_throw_exception = true;
4433 break;
4434
4435 case Instruction::UNUSED_3E:
4436 case Instruction::UNUSED_3F:
4437 case Instruction::UNUSED_40:
4438 case Instruction::UNUSED_41:
4439 case Instruction::UNUSED_42:
4440 case Instruction::UNUSED_43:
4441 case Instruction::UNUSED_73:
4442 case Instruction::UNUSED_79:
4443 case Instruction::UNUSED_7A:
4444 case Instruction::UNUSED_E3:
4445 case Instruction::UNUSED_E4:
4446 case Instruction::UNUSED_E5:
4447 case Instruction::UNUSED_E6:
4448 case Instruction::UNUSED_E7:
4449 case Instruction::UNUSED_E8:
4450 case Instruction::UNUSED_E9:
4451 case Instruction::UNUSED_EA:
4452 case Instruction::UNUSED_EB:
4453 case Instruction::UNUSED_EC:
4454 case Instruction::UNUSED_EE:
4455 case Instruction::UNUSED_EF:
4456 case Instruction::UNUSED_F0:
4457 case Instruction::UNUSED_F1:
4458 case Instruction::UNUSED_F2:
4459 case Instruction::UNUSED_F3:
4460 case Instruction::UNUSED_F4:
4461 case Instruction::UNUSED_F5:
4462 case Instruction::UNUSED_F6:
4463 case Instruction::UNUSED_F7:
4464 case Instruction::UNUSED_F8:
4465 case Instruction::UNUSED_F9:
4466 case Instruction::UNUSED_FA:
4467 case Instruction::UNUSED_FB:
4468 case Instruction::UNUSED_FC:
4469 case Instruction::UNUSED_FD:
4470 case Instruction::UNUSED_FE:
4471 case Instruction::UNUSED_FF:
4472 LOG(FATAL) << "Dex file contains UNUSED bytecode: " << insn->Opcode();
4473 break;
4474 }
4475 }
4476
4477 method_info_.this_reg_idx = this_reg_idx;
4478 // According to the statistics, there are few methods that modify the "this" pointer. So this is a
4479 // simple way to avoid data flow analysis. After we have a high-level IR before IRBuilder, we
4480 // should remove this trick.
4481 method_info_.this_will_not_be_null = !modify_this;
4482 method_info_.has_invoke = has_invoke;
4483 // If this method has loop or invoke instruction, it may suspend. Thus we need a shadow frame entry
4484 // for GC.
4485 method_info_.need_shadow_frame_entry = has_invoke || may_have_loop;
4486 // If this method may throw an exception, we need a shadow frame for stack trace (dexpc).
4487 method_info_.need_shadow_frame = method_info_.need_shadow_frame_entry || may_throw_exception;
4488}
4489
4490
4491
Logan Chien83426162011-12-09 09:29:50 +08004492} // namespace compiler_llvm
4493} // namespace art