blob: 3e8a6f801e3d93531b84c5352bf3b486f157c766 [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()),
63 irb_(*cunit->GetIRBuilder()), func_(NULL), retval_reg_(NULL),
Ian Rogers776ac1f2012-04-13 23:36:36 -070064 basic_block_stack_overflow_(NULL),
Logan Chien8b977d32012-02-21 19:14:55 +080065 basic_block_reg_alloca_(NULL), basic_block_shadow_frame_alloca_(NULL),
Logan Chienef4a6562012-04-24 18:02:24 +080066 basic_block_reg_arg_init_(NULL),
Logan Chien8b977d32012-02-21 19:14:55 +080067 basic_blocks_(code_item_->insns_size_in_code_units_),
68 basic_block_landing_pads_(code_item_->tries_size_, NULL),
69 basic_block_unwind_(NULL), basic_block_unreachable_(NULL),
Logan Chien937105a2012-04-02 02:37:37 +080070 shadow_frame_(NULL), elf_func_idx_(cunit_->AcquireUniqueElfFuncIndex()) {
Shih-wei Liaod1fec812012-02-13 09:51:10 -080071}
72
73
74MethodCompiler::~MethodCompiler() {
Logan Chienc670a8d2011-12-20 21:25:56 +080075 STLDeleteElements(&regs_);
Shih-wei Liaod1fec812012-02-13 09:51:10 -080076}
77
78
Logan Chien0b827102011-12-20 19:46:14 +080079void MethodCompiler::CreateFunction() {
80 // LLVM function name
Logan Chien937105a2012-04-02 02:37:37 +080081 std::string func_name(ElfFuncName(elf_func_idx_));
Logan Chien0b827102011-12-20 19:46:14 +080082
83 // Get function type
84 llvm::FunctionType* func_type =
Logan Chiendd361c92012-04-10 23:40:37 +080085 GetFunctionType(method_idx_, oat_compilation_unit_->IsStatic());
Logan Chien0b827102011-12-20 19:46:14 +080086
87 // Create function
88 func_ = llvm::Function::Create(func_type, llvm::Function::ExternalLinkage,
89 func_name, module_);
90
91 // Set argument name
92 llvm::Function::arg_iterator arg_iter(func_->arg_begin());
93 llvm::Function::arg_iterator arg_end(func_->arg_end());
94
95 DCHECK_NE(arg_iter, arg_end);
96 arg_iter->setName("method");
97 ++arg_iter;
98
Logan Chiendd361c92012-04-10 23:40:37 +080099 if (!oat_compilation_unit_->IsStatic()) {
Logan Chien0b827102011-12-20 19:46:14 +0800100 DCHECK_NE(arg_iter, arg_end);
101 arg_iter->setName("this");
102 ++arg_iter;
103 }
104
105 for (unsigned i = 0; arg_iter != arg_end; ++i, ++arg_iter) {
106 arg_iter->setName(StringPrintf("a%u", i));
107 }
108}
109
110
111llvm::FunctionType* MethodCompiler::GetFunctionType(uint32_t method_idx,
112 bool is_static) {
113 // Get method signature
114 DexFile::MethodId const& method_id = dex_file_->GetMethodId(method_idx);
115
Logan Chien8faf8022012-02-24 12:25:29 +0800116 uint32_t shorty_size;
Logan Chien0b827102011-12-20 19:46:14 +0800117 char const* shorty = dex_file_->GetMethodShorty(method_id, &shorty_size);
Logan Chien8faf8022012-02-24 12:25:29 +0800118 CHECK_GE(shorty_size, 1u);
Logan Chien0b827102011-12-20 19:46:14 +0800119
120 // Get return type
121 llvm::Type* ret_type = irb_.getJType(shorty[0], kAccurate);
122
123 // Get argument type
124 std::vector<llvm::Type*> args_type;
125
126 args_type.push_back(irb_.getJObjectTy()); // method object pointer
127
128 if (!is_static) {
129 args_type.push_back(irb_.getJType('L', kAccurate)); // "this" object pointer
130 }
131
Logan Chien8faf8022012-02-24 12:25:29 +0800132 for (uint32_t i = 1; i < shorty_size; ++i) {
Logan Chien0b827102011-12-20 19:46:14 +0800133 args_type.push_back(irb_.getJType(shorty[i], kAccurate));
134 }
135
136 return llvm::FunctionType::get(ret_type, args_type, false);
137}
138
139
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800140void MethodCompiler::EmitPrologue() {
Logan Chienc670a8d2011-12-20 21:25:56 +0800141 // Create basic blocks for prologue
TDYa1274165a832012-04-03 17:47:16 -0700142 basic_block_stack_overflow_ =
143 llvm::BasicBlock::Create(*context_, "prologue.stack_overflow_check", func_);
144
Logan Chienc670a8d2011-12-20 21:25:56 +0800145 basic_block_reg_alloca_ =
146 llvm::BasicBlock::Create(*context_, "prologue.alloca", func_);
147
Logan Chien8dfcbea2012-02-17 18:50:32 +0800148 basic_block_shadow_frame_alloca_ =
149 llvm::BasicBlock::Create(*context_, "prologue.shadowframe", func_);
150
Logan Chiend6ececa2011-12-27 16:20:15 +0800151 basic_block_reg_arg_init_ =
152 llvm::BasicBlock::Create(*context_, "prologue.arginit", func_);
153
TDYa1274165a832012-04-03 17:47:16 -0700154 // Before alloca, check stack overflow.
155 EmitStackOverflowCheck();
156
Logan Chienc670a8d2011-12-20 21:25:56 +0800157 // Create register array
158 for (uint16_t r = 0; r < code_item_->registers_size_; ++r) {
159 regs_.push_back(DalvikReg::CreateLocalVarReg(*this, r));
160 }
161
162 retval_reg_.reset(DalvikReg::CreateRetValReg(*this));
Logan Chiend6ececa2011-12-27 16:20:15 +0800163
Logan Chien8dfcbea2012-02-17 18:50:32 +0800164 // Create Shadow Frame
165 EmitPrologueAllocShadowFrame();
166
Logan Chiend6ececa2011-12-27 16:20:15 +0800167 // Store argument to dalvik register
168 irb_.SetInsertPoint(basic_block_reg_arg_init_);
169 EmitPrologueAssignArgRegister();
170
171 // Branch to start address
172 irb_.CreateBr(GetBasicBlock(0));
Logan Chienc670a8d2011-12-20 21:25:56 +0800173}
174
175
TDYa1274165a832012-04-03 17:47:16 -0700176void MethodCompiler::EmitStackOverflowCheck() {
177 irb_.SetInsertPoint(basic_block_stack_overflow_);
178
179 // Call llvm intrinsic function to get frame address.
180 llvm::Function* frameaddress =
181 llvm::Intrinsic::getDeclaration(module_, llvm::Intrinsic::frameaddress);
182
183 // The type of llvm::frameaddress is: i8* @llvm.frameaddress(i32)
184 llvm::Value* frame_address = irb_.CreateCall(frameaddress, irb_.getInt32(0));
185
186 // Cast i8* to int
187 frame_address = irb_.CreatePtrToInt(frame_address, irb_.getPtrEquivIntTy());
188
189 // Get thread.stack_end_
190 llvm::Value* thread_object_addr =
191 irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
192
TDYa127ee1f59b2012-04-25 00:56:40 -0700193 llvm::Value* stack_end =
194 irb_.LoadFromObjectOffset(thread_object_addr,
195 Thread::StackEndOffset().Int32Value(),
196 irb_.getPtrEquivIntTy());
TDYa1274165a832012-04-03 17:47:16 -0700197
198 // Check the frame address < thread.stack_end_ ?
199 llvm::Value* is_stack_overflow = irb_.CreateICmpULT(frame_address, stack_end);
200
201 llvm::BasicBlock* block_exception =
202 llvm::BasicBlock::Create(*context_, "stack_overflow", func_);
203
204 llvm::BasicBlock* block_continue =
205 llvm::BasicBlock::Create(*context_, "stack_overflow_cont", func_);
206
207 irb_.CreateCondBr(is_stack_overflow, block_exception, block_continue);
208
209 // If stack overflow, throw exception.
210 irb_.SetInsertPoint(block_exception);
211 irb_.CreateCall(irb_.GetRuntime(ThrowStackOverflowException));
212
213 // Unwind.
Logan Chiendd361c92012-04-10 23:40:37 +0800214 char ret_shorty = oat_compilation_unit_->GetShorty()[0];
TDYa1274165a832012-04-03 17:47:16 -0700215 if (ret_shorty == 'V') {
216 irb_.CreateRetVoid();
217 } else {
218 irb_.CreateRet(irb_.getJZero(ret_shorty));
219 }
220
221 basic_block_stack_overflow_ = block_continue;
222}
223
224
Logan Chienc670a8d2011-12-20 21:25:56 +0800225void MethodCompiler::EmitPrologueLastBranch() {
TDYa1274165a832012-04-03 17:47:16 -0700226 irb_.SetInsertPoint(basic_block_stack_overflow_);
227 irb_.CreateBr(basic_block_reg_alloca_);
228
Logan Chienc670a8d2011-12-20 21:25:56 +0800229 irb_.SetInsertPoint(basic_block_reg_alloca_);
Logan Chien8dfcbea2012-02-17 18:50:32 +0800230 irb_.CreateBr(basic_block_shadow_frame_alloca_);
231
232 irb_.SetInsertPoint(basic_block_shadow_frame_alloca_);
Logan Chiend6ececa2011-12-27 16:20:15 +0800233 irb_.CreateBr(basic_block_reg_arg_init_);
234}
235
236
Logan Chien8dfcbea2012-02-17 18:50:32 +0800237void MethodCompiler::EmitPrologueAllocShadowFrame() {
238 irb_.SetInsertPoint(basic_block_shadow_frame_alloca_);
239
240 // Allocate the shadow frame now!
241 uint32_t sirt_size = code_item_->registers_size_;
242 // TODO: registers_size_ is a bad approximation. Compute a
243 // tighter approximation at Dex verifier while performing data-flow
244 // analysis.
245
246 llvm::StructType* shadow_frame_type = irb_.getShadowFrameTy(sirt_size);
247 shadow_frame_ = irb_.CreateAlloca(shadow_frame_type);
248
249 // Zero-initialization of the shadow frame
250 llvm::ConstantAggregateZero* zero_initializer =
251 llvm::ConstantAggregateZero::get(shadow_frame_type);
252
253 irb_.CreateStore(zero_initializer, shadow_frame_);
254
TDYa127ee1f59b2012-04-25 00:56:40 -0700255 // Get method object
Logan Chien8dfcbea2012-02-17 18:50:32 +0800256 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
TDYa127ee1f59b2012-04-25 00:56:40 -0700257
258 // Store the method pointer
259 irb_.StoreToObjectOffset(shadow_frame_,
260 ShadowFrame::MethodOffset(),
261 method_object_addr);
Logan Chien8dfcbea2012-02-17 18:50:32 +0800262
263 // Store the number of the pointer slots
TDYa127ee1f59b2012-04-25 00:56:40 -0700264 irb_.StoreToObjectOffset(shadow_frame_,
265 ShadowFrame::NumberOfReferencesOffset(),
266 irb_.getJInt(sirt_size));
Logan Chien8dfcbea2012-02-17 18:50:32 +0800267
268 // Push the shadow frame
269 llvm::Value* shadow_frame_upcast =
270 irb_.CreateConstGEP2_32(shadow_frame_, 0, 0);
271
272 irb_.CreateCall(irb_.GetRuntime(PushShadowFrame), shadow_frame_upcast);
273}
274
275
Logan Chiend6ececa2011-12-27 16:20:15 +0800276void MethodCompiler::EmitPrologueAssignArgRegister() {
277 uint16_t arg_reg = code_item_->registers_size_ - code_item_->ins_size_;
278
279 llvm::Function::arg_iterator arg_iter(func_->arg_begin());
280 llvm::Function::arg_iterator arg_end(func_->arg_end());
281
Logan Chiendd361c92012-04-10 23:40:37 +0800282 uint32_t shorty_size = 0;
283 char const* shorty = oat_compilation_unit_->GetShorty(&shorty_size);
284 CHECK_GE(shorty_size, 1u);
Logan Chiend6ececa2011-12-27 16:20:15 +0800285
286 ++arg_iter; // skip method object
287
Logan Chiendd361c92012-04-10 23:40:37 +0800288 if (!oat_compilation_unit_->IsStatic()) {
Logan Chiend6ececa2011-12-27 16:20:15 +0800289 EmitStoreDalvikReg(arg_reg, kObject, kAccurate, arg_iter);
290 ++arg_iter;
291 ++arg_reg;
292 }
293
Logan Chiendd361c92012-04-10 23:40:37 +0800294 for (uint32_t i = 1; i < shorty_size; ++i, ++arg_iter) {
Logan Chiend6ececa2011-12-27 16:20:15 +0800295 EmitStoreDalvikReg(arg_reg, shorty[i], kAccurate, arg_iter);
296
297 ++arg_reg;
298 if (shorty[i] == 'J' || shorty[i] == 'D') {
299 // Wide types, such as long and double, are using a pair of registers
300 // to store the value, so we have to increase arg_reg again.
301 ++arg_reg;
302 }
303 }
304
305 DCHECK_EQ(arg_end, arg_iter);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800306}
307
308
Logan Chien83426162011-12-09 09:29:50 +0800309void MethodCompiler::EmitInstructions() {
Logan Chiend6c239a2011-12-23 15:11:45 +0800310 uint32_t dex_pc = 0;
311 while (dex_pc < code_item_->insns_size_in_code_units_) {
312 Instruction const* insn = Instruction::At(code_item_->insns_ + dex_pc);
313 EmitInstruction(dex_pc, insn);
314 dex_pc += insn->SizeInCodeUnits();
315 }
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800316}
317
318
Logan Chien83426162011-12-09 09:29:50 +0800319void MethodCompiler::EmitInstruction(uint32_t dex_pc,
320 Instruction const* insn) {
Logan Chiend6c239a2011-12-23 15:11:45 +0800321
322 // Set the IRBuilder insertion point
323 irb_.SetInsertPoint(GetBasicBlock(dex_pc));
324
Logan Chien70f94b42011-12-27 17:49:11 +0800325#define ARGS dex_pc, insn
326
327 // Dispatch the instruction
328 switch (insn->Opcode()) {
329 case Instruction::NOP:
330 EmitInsn_Nop(ARGS);
331 break;
332
333 case Instruction::MOVE:
334 case Instruction::MOVE_FROM16:
335 case Instruction::MOVE_16:
336 EmitInsn_Move(ARGS, kInt);
337 break;
338
339 case Instruction::MOVE_WIDE:
340 case Instruction::MOVE_WIDE_FROM16:
341 case Instruction::MOVE_WIDE_16:
342 EmitInsn_Move(ARGS, kLong);
343 break;
344
345 case Instruction::MOVE_OBJECT:
346 case Instruction::MOVE_OBJECT_FROM16:
347 case Instruction::MOVE_OBJECT_16:
348 EmitInsn_Move(ARGS, kObject);
349 break;
350
351 case Instruction::MOVE_RESULT:
352 EmitInsn_MoveResult(ARGS, kInt);
353 break;
354
355 case Instruction::MOVE_RESULT_WIDE:
356 EmitInsn_MoveResult(ARGS, kLong);
357 break;
358
359 case Instruction::MOVE_RESULT_OBJECT:
360 EmitInsn_MoveResult(ARGS, kObject);
361 break;
362
363 case Instruction::MOVE_EXCEPTION:
364 EmitInsn_MoveException(ARGS);
365 break;
366
367 case Instruction::RETURN_VOID:
368 EmitInsn_ReturnVoid(ARGS);
369 break;
370
371 case Instruction::RETURN:
372 case Instruction::RETURN_WIDE:
373 case Instruction::RETURN_OBJECT:
374 EmitInsn_Return(ARGS);
375 break;
376
377 case Instruction::CONST_4:
378 case Instruction::CONST_16:
379 case Instruction::CONST:
380 case Instruction::CONST_HIGH16:
381 EmitInsn_LoadConstant(ARGS, kInt);
382 break;
383
384 case Instruction::CONST_WIDE_16:
385 case Instruction::CONST_WIDE_32:
386 case Instruction::CONST_WIDE:
387 case Instruction::CONST_WIDE_HIGH16:
388 EmitInsn_LoadConstant(ARGS, kLong);
389 break;
390
391 case Instruction::CONST_STRING:
392 case Instruction::CONST_STRING_JUMBO:
393 EmitInsn_LoadConstantString(ARGS);
394 break;
395
396 case Instruction::CONST_CLASS:
397 EmitInsn_LoadConstantClass(ARGS);
398 break;
399
400 case Instruction::MONITOR_ENTER:
401 EmitInsn_MonitorEnter(ARGS);
402 break;
403
404 case Instruction::MONITOR_EXIT:
405 EmitInsn_MonitorExit(ARGS);
406 break;
407
408 case Instruction::CHECK_CAST:
409 EmitInsn_CheckCast(ARGS);
410 break;
411
412 case Instruction::INSTANCE_OF:
413 EmitInsn_InstanceOf(ARGS);
414 break;
415
416 case Instruction::ARRAY_LENGTH:
417 EmitInsn_ArrayLength(ARGS);
418 break;
419
420 case Instruction::NEW_INSTANCE:
421 EmitInsn_NewInstance(ARGS);
422 break;
423
424 case Instruction::NEW_ARRAY:
425 EmitInsn_NewArray(ARGS);
426 break;
427
428 case Instruction::FILLED_NEW_ARRAY:
429 EmitInsn_FilledNewArray(ARGS, false);
430 break;
431
432 case Instruction::FILLED_NEW_ARRAY_RANGE:
433 EmitInsn_FilledNewArray(ARGS, true);
434 break;
435
436 case Instruction::FILL_ARRAY_DATA:
437 EmitInsn_FillArrayData(ARGS);
438 break;
439
440 case Instruction::THROW:
441 EmitInsn_ThrowException(ARGS);
442 break;
443
444 case Instruction::GOTO:
445 case Instruction::GOTO_16:
446 case Instruction::GOTO_32:
447 EmitInsn_UnconditionalBranch(ARGS);
448 break;
449
450 case Instruction::PACKED_SWITCH:
451 EmitInsn_PackedSwitch(ARGS);
452 break;
453
454 case Instruction::SPARSE_SWITCH:
455 EmitInsn_SparseSwitch(ARGS);
456 break;
457
458 case Instruction::CMPL_FLOAT:
459 EmitInsn_FPCompare(ARGS, kFloat, false);
460 break;
461
462 case Instruction::CMPG_FLOAT:
463 EmitInsn_FPCompare(ARGS, kFloat, true);
464 break;
465
466 case Instruction::CMPL_DOUBLE:
467 EmitInsn_FPCompare(ARGS, kDouble, false);
468 break;
469
470 case Instruction::CMPG_DOUBLE:
471 EmitInsn_FPCompare(ARGS, kDouble, true);
472 break;
473
474 case Instruction::CMP_LONG:
475 EmitInsn_LongCompare(ARGS);
476 break;
477
478 case Instruction::IF_EQ:
479 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_EQ);
480 break;
481
482 case Instruction::IF_NE:
483 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_NE);
484 break;
485
486 case Instruction::IF_LT:
487 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_LT);
488 break;
489
490 case Instruction::IF_GE:
491 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_GE);
492 break;
493
494 case Instruction::IF_GT:
495 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_GT);
496 break;
497
498 case Instruction::IF_LE:
499 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_LE);
500 break;
501
502 case Instruction::IF_EQZ:
503 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_EQ);
504 break;
505
506 case Instruction::IF_NEZ:
507 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_NE);
508 break;
509
510 case Instruction::IF_LTZ:
511 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_LT);
512 break;
513
514 case Instruction::IF_GEZ:
515 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_GE);
516 break;
517
518 case Instruction::IF_GTZ:
519 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_GT);
520 break;
521
522 case Instruction::IF_LEZ:
523 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_LE);
524 break;
525
526 case Instruction::AGET:
527 EmitInsn_AGet(ARGS, kInt);
528 break;
529
530 case Instruction::AGET_WIDE:
531 EmitInsn_AGet(ARGS, kLong);
532 break;
533
534 case Instruction::AGET_OBJECT:
535 EmitInsn_AGet(ARGS, kObject);
536 break;
537
538 case Instruction::AGET_BOOLEAN:
539 EmitInsn_AGet(ARGS, kBoolean);
540 break;
541
542 case Instruction::AGET_BYTE:
543 EmitInsn_AGet(ARGS, kByte);
544 break;
545
546 case Instruction::AGET_CHAR:
547 EmitInsn_AGet(ARGS, kChar);
548 break;
549
550 case Instruction::AGET_SHORT:
551 EmitInsn_AGet(ARGS, kShort);
552 break;
553
554 case Instruction::APUT:
555 EmitInsn_APut(ARGS, kInt);
556 break;
557
558 case Instruction::APUT_WIDE:
559 EmitInsn_APut(ARGS, kLong);
560 break;
561
562 case Instruction::APUT_OBJECT:
563 EmitInsn_APut(ARGS, kObject);
564 break;
565
566 case Instruction::APUT_BOOLEAN:
567 EmitInsn_APut(ARGS, kBoolean);
568 break;
569
570 case Instruction::APUT_BYTE:
571 EmitInsn_APut(ARGS, kByte);
572 break;
573
574 case Instruction::APUT_CHAR:
575 EmitInsn_APut(ARGS, kChar);
576 break;
577
578 case Instruction::APUT_SHORT:
579 EmitInsn_APut(ARGS, kShort);
580 break;
581
582 case Instruction::IGET:
583 EmitInsn_IGet(ARGS, kInt);
584 break;
585
586 case Instruction::IGET_WIDE:
587 EmitInsn_IGet(ARGS, kLong);
588 break;
589
590 case Instruction::IGET_OBJECT:
591 EmitInsn_IGet(ARGS, kObject);
592 break;
593
594 case Instruction::IGET_BOOLEAN:
595 EmitInsn_IGet(ARGS, kBoolean);
596 break;
597
598 case Instruction::IGET_BYTE:
599 EmitInsn_IGet(ARGS, kByte);
600 break;
601
602 case Instruction::IGET_CHAR:
603 EmitInsn_IGet(ARGS, kChar);
604 break;
605
606 case Instruction::IGET_SHORT:
607 EmitInsn_IGet(ARGS, kShort);
608 break;
609
610 case Instruction::IPUT:
611 EmitInsn_IPut(ARGS, kInt);
612 break;
613
614 case Instruction::IPUT_WIDE:
615 EmitInsn_IPut(ARGS, kLong);
616 break;
617
618 case Instruction::IPUT_OBJECT:
619 EmitInsn_IPut(ARGS, kObject);
620 break;
621
622 case Instruction::IPUT_BOOLEAN:
623 EmitInsn_IPut(ARGS, kBoolean);
624 break;
625
626 case Instruction::IPUT_BYTE:
627 EmitInsn_IPut(ARGS, kByte);
628 break;
629
630 case Instruction::IPUT_CHAR:
631 EmitInsn_IPut(ARGS, kChar);
632 break;
633
634 case Instruction::IPUT_SHORT:
635 EmitInsn_IPut(ARGS, kShort);
636 break;
637
638 case Instruction::SGET:
639 EmitInsn_SGet(ARGS, kInt);
640 break;
641
642 case Instruction::SGET_WIDE:
643 EmitInsn_SGet(ARGS, kLong);
644 break;
645
646 case Instruction::SGET_OBJECT:
647 EmitInsn_SGet(ARGS, kObject);
648 break;
649
650 case Instruction::SGET_BOOLEAN:
651 EmitInsn_SGet(ARGS, kBoolean);
652 break;
653
654 case Instruction::SGET_BYTE:
655 EmitInsn_SGet(ARGS, kByte);
656 break;
657
658 case Instruction::SGET_CHAR:
659 EmitInsn_SGet(ARGS, kChar);
660 break;
661
662 case Instruction::SGET_SHORT:
663 EmitInsn_SGet(ARGS, kShort);
664 break;
665
666 case Instruction::SPUT:
667 EmitInsn_SPut(ARGS, kInt);
668 break;
669
670 case Instruction::SPUT_WIDE:
671 EmitInsn_SPut(ARGS, kLong);
672 break;
673
674 case Instruction::SPUT_OBJECT:
675 EmitInsn_SPut(ARGS, kObject);
676 break;
677
678 case Instruction::SPUT_BOOLEAN:
679 EmitInsn_SPut(ARGS, kBoolean);
680 break;
681
682 case Instruction::SPUT_BYTE:
683 EmitInsn_SPut(ARGS, kByte);
684 break;
685
686 case Instruction::SPUT_CHAR:
687 EmitInsn_SPut(ARGS, kChar);
688 break;
689
690 case Instruction::SPUT_SHORT:
691 EmitInsn_SPut(ARGS, kShort);
692 break;
693
694
695 case Instruction::INVOKE_VIRTUAL:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800696 EmitInsn_Invoke(ARGS, kVirtual, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800697 break;
698
699 case Instruction::INVOKE_SUPER:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800700 EmitInsn_Invoke(ARGS, kSuper, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800701 break;
702
703 case Instruction::INVOKE_DIRECT:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800704 EmitInsn_Invoke(ARGS, kDirect, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800705 break;
706
707 case Instruction::INVOKE_STATIC:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800708 EmitInsn_Invoke(ARGS, kStatic, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800709 break;
710
711 case Instruction::INVOKE_INTERFACE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800712 EmitInsn_Invoke(ARGS, kInterface, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800713 break;
714
715 case Instruction::INVOKE_VIRTUAL_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800716 EmitInsn_Invoke(ARGS, kVirtual, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800717 break;
718
719 case Instruction::INVOKE_SUPER_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800720 EmitInsn_Invoke(ARGS, kSuper, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800721 break;
722
723 case Instruction::INVOKE_DIRECT_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800724 EmitInsn_Invoke(ARGS, kDirect, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800725 break;
726
727 case Instruction::INVOKE_STATIC_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800728 EmitInsn_Invoke(ARGS, kStatic, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800729 break;
730
731 case Instruction::INVOKE_INTERFACE_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800732 EmitInsn_Invoke(ARGS, kInterface, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800733 break;
734
735 case Instruction::NEG_INT:
736 EmitInsn_Neg(ARGS, kInt);
737 break;
738
739 case Instruction::NOT_INT:
740 EmitInsn_Not(ARGS, kInt);
741 break;
742
743 case Instruction::NEG_LONG:
744 EmitInsn_Neg(ARGS, kLong);
745 break;
746
747 case Instruction::NOT_LONG:
748 EmitInsn_Not(ARGS, kLong);
749 break;
750
751 case Instruction::NEG_FLOAT:
752 EmitInsn_FNeg(ARGS, kFloat);
753 break;
754
755 case Instruction::NEG_DOUBLE:
756 EmitInsn_FNeg(ARGS, kDouble);
757 break;
758
759 case Instruction::INT_TO_LONG:
760 EmitInsn_SExt(ARGS);
761 break;
762
763 case Instruction::INT_TO_FLOAT:
764 EmitInsn_IntToFP(ARGS, kInt, kFloat);
765 break;
766
767 case Instruction::INT_TO_DOUBLE:
768 EmitInsn_IntToFP(ARGS, kInt, kDouble);
769 break;
770
771 case Instruction::LONG_TO_INT:
772 EmitInsn_Trunc(ARGS);
773 break;
774
775 case Instruction::LONG_TO_FLOAT:
776 EmitInsn_IntToFP(ARGS, kLong, kFloat);
777 break;
778
779 case Instruction::LONG_TO_DOUBLE:
780 EmitInsn_IntToFP(ARGS, kLong, kDouble);
781 break;
782
783 case Instruction::FLOAT_TO_INT:
TDYa127a4746872012-04-11 23:48:55 -0700784 EmitInsn_FPToInt(ARGS, kFloat, kInt, F2I);
Logan Chien70f94b42011-12-27 17:49:11 +0800785 break;
786
787 case Instruction::FLOAT_TO_LONG:
TDYa127a4746872012-04-11 23:48:55 -0700788 EmitInsn_FPToInt(ARGS, kFloat, kLong, F2L);
Logan Chien70f94b42011-12-27 17:49:11 +0800789 break;
790
791 case Instruction::FLOAT_TO_DOUBLE:
792 EmitInsn_FExt(ARGS);
793 break;
794
795 case Instruction::DOUBLE_TO_INT:
TDYa127a4746872012-04-11 23:48:55 -0700796 EmitInsn_FPToInt(ARGS, kDouble, kInt, D2I);
Logan Chien70f94b42011-12-27 17:49:11 +0800797 break;
798
799 case Instruction::DOUBLE_TO_LONG:
TDYa127a4746872012-04-11 23:48:55 -0700800 EmitInsn_FPToInt(ARGS, kDouble, kLong, D2L);
Logan Chien70f94b42011-12-27 17:49:11 +0800801 break;
802
803 case Instruction::DOUBLE_TO_FLOAT:
804 EmitInsn_FTrunc(ARGS);
805 break;
806
807 case Instruction::INT_TO_BYTE:
808 EmitInsn_TruncAndSExt(ARGS, 8);
809 break;
810
811 case Instruction::INT_TO_CHAR:
812 EmitInsn_TruncAndZExt(ARGS, 16);
813 break;
814
815 case Instruction::INT_TO_SHORT:
816 EmitInsn_TruncAndSExt(ARGS, 16);
817 break;
818
819 case Instruction::ADD_INT:
820 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kInt, false);
821 break;
822
823 case Instruction::SUB_INT:
824 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kInt, false);
825 break;
826
827 case Instruction::MUL_INT:
828 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kInt, false);
829 break;
830
831 case Instruction::DIV_INT:
832 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kInt, false);
833 break;
834
835 case Instruction::REM_INT:
836 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kInt, false);
837 break;
838
839 case Instruction::AND_INT:
840 EmitInsn_IntArithm(ARGS, kIntArithm_And, kInt, false);
841 break;
842
843 case Instruction::OR_INT:
844 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kInt, false);
845 break;
846
847 case Instruction::XOR_INT:
848 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kInt, false);
849 break;
850
851 case Instruction::SHL_INT:
Logan Chien5539ad02012-04-02 14:36:55 +0800852 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kInt, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800853 break;
854
855 case Instruction::SHR_INT:
Logan Chien5539ad02012-04-02 14:36:55 +0800856 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kInt, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800857 break;
858
859 case Instruction::USHR_INT:
Logan Chien5539ad02012-04-02 14:36:55 +0800860 EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kInt, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800861 break;
862
863 case Instruction::ADD_LONG:
864 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kLong, false);
865 break;
866
867 case Instruction::SUB_LONG:
868 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kLong, false);
869 break;
870
871 case Instruction::MUL_LONG:
872 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kLong, false);
873 break;
874
875 case Instruction::DIV_LONG:
876 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kLong, false);
877 break;
878
879 case Instruction::REM_LONG:
880 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kLong, false);
881 break;
882
883 case Instruction::AND_LONG:
884 EmitInsn_IntArithm(ARGS, kIntArithm_And, kLong, false);
885 break;
886
887 case Instruction::OR_LONG:
888 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kLong, false);
889 break;
890
891 case Instruction::XOR_LONG:
892 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kLong, false);
893 break;
894
895 case Instruction::SHL_LONG:
Logan Chien5539ad02012-04-02 14:36:55 +0800896 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kLong, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800897 break;
898
899 case Instruction::SHR_LONG:
Logan Chien5539ad02012-04-02 14:36:55 +0800900 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kLong, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800901 break;
902
903 case Instruction::USHR_LONG:
Logan Chien5539ad02012-04-02 14:36:55 +0800904 EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kLong, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800905 break;
906
907 case Instruction::ADD_FLOAT:
908 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kFloat, false);
909 break;
910
911 case Instruction::SUB_FLOAT:
912 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kFloat, false);
913 break;
914
915 case Instruction::MUL_FLOAT:
916 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kFloat, false);
917 break;
918
919 case Instruction::DIV_FLOAT:
920 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kFloat, false);
921 break;
922
923 case Instruction::REM_FLOAT:
924 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kFloat, false);
925 break;
926
927 case Instruction::ADD_DOUBLE:
928 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kDouble, false);
929 break;
930
931 case Instruction::SUB_DOUBLE:
932 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kDouble, false);
933 break;
934
935 case Instruction::MUL_DOUBLE:
936 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kDouble, false);
937 break;
938
939 case Instruction::DIV_DOUBLE:
940 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kDouble, false);
941 break;
942
943 case Instruction::REM_DOUBLE:
944 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kDouble, false);
945 break;
946
947 case Instruction::ADD_INT_2ADDR:
948 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kInt, true);
949 break;
950
951 case Instruction::SUB_INT_2ADDR:
952 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kInt, true);
953 break;
954
955 case Instruction::MUL_INT_2ADDR:
956 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kInt, true);
957 break;
958
959 case Instruction::DIV_INT_2ADDR:
960 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kInt, true);
961 break;
962
963 case Instruction::REM_INT_2ADDR:
964 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kInt, true);
965 break;
966
967 case Instruction::AND_INT_2ADDR:
968 EmitInsn_IntArithm(ARGS, kIntArithm_And, kInt, true);
969 break;
970
971 case Instruction::OR_INT_2ADDR:
972 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kInt, true);
973 break;
974
975 case Instruction::XOR_INT_2ADDR:
976 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kInt, true);
977 break;
978
979 case Instruction::SHL_INT_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +0800980 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kInt, true);
Logan Chien70f94b42011-12-27 17:49:11 +0800981 break;
982
983 case Instruction::SHR_INT_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +0800984 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kInt, true);
Logan Chien70f94b42011-12-27 17:49:11 +0800985 break;
986
987 case Instruction::USHR_INT_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +0800988 EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kInt, true);
Logan Chien70f94b42011-12-27 17:49:11 +0800989 break;
990
991 case Instruction::ADD_LONG_2ADDR:
992 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kLong, true);
993 break;
994
995 case Instruction::SUB_LONG_2ADDR:
996 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kLong, true);
997 break;
998
999 case Instruction::MUL_LONG_2ADDR:
1000 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kLong, true);
1001 break;
1002
1003 case Instruction::DIV_LONG_2ADDR:
1004 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kLong, true);
1005 break;
1006
1007 case Instruction::REM_LONG_2ADDR:
1008 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kLong, true);
1009 break;
1010
1011 case Instruction::AND_LONG_2ADDR:
1012 EmitInsn_IntArithm(ARGS, kIntArithm_And, kLong, true);
1013 break;
1014
1015 case Instruction::OR_LONG_2ADDR:
1016 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kLong, true);
1017 break;
1018
1019 case Instruction::XOR_LONG_2ADDR:
1020 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kLong, true);
1021 break;
1022
1023 case Instruction::SHL_LONG_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001024 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kLong, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001025 break;
1026
1027 case Instruction::SHR_LONG_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001028 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kLong, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001029 break;
1030
1031 case Instruction::USHR_LONG_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001032 EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kLong, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001033 break;
1034
1035 case Instruction::ADD_FLOAT_2ADDR:
1036 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kFloat, true);
1037 break;
1038
1039 case Instruction::SUB_FLOAT_2ADDR:
1040 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kFloat, true);
1041 break;
1042
1043 case Instruction::MUL_FLOAT_2ADDR:
1044 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kFloat, true);
1045 break;
1046
1047 case Instruction::DIV_FLOAT_2ADDR:
1048 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kFloat, true);
1049 break;
1050
1051 case Instruction::REM_FLOAT_2ADDR:
1052 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kFloat, true);
1053 break;
1054
1055 case Instruction::ADD_DOUBLE_2ADDR:
1056 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kDouble, true);
1057 break;
1058
1059 case Instruction::SUB_DOUBLE_2ADDR:
1060 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kDouble, true);
1061 break;
1062
1063 case Instruction::MUL_DOUBLE_2ADDR:
1064 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kDouble, true);
1065 break;
1066
1067 case Instruction::DIV_DOUBLE_2ADDR:
1068 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kDouble, true);
1069 break;
1070
1071 case Instruction::REM_DOUBLE_2ADDR:
1072 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kDouble, true);
1073 break;
1074
1075 case Instruction::ADD_INT_LIT16:
1076 case Instruction::ADD_INT_LIT8:
1077 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Add);
1078 break;
1079
1080 case Instruction::RSUB_INT:
1081 case Instruction::RSUB_INT_LIT8:
1082 EmitInsn_RSubImmediate(ARGS);
1083 break;
1084
1085 case Instruction::MUL_INT_LIT16:
1086 case Instruction::MUL_INT_LIT8:
1087 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Mul);
1088 break;
1089
1090 case Instruction::DIV_INT_LIT16:
1091 case Instruction::DIV_INT_LIT8:
1092 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Div);
1093 break;
1094
1095 case Instruction::REM_INT_LIT16:
1096 case Instruction::REM_INT_LIT8:
1097 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Rem);
1098 break;
1099
1100 case Instruction::AND_INT_LIT16:
1101 case Instruction::AND_INT_LIT8:
1102 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_And);
1103 break;
1104
1105 case Instruction::OR_INT_LIT16:
1106 case Instruction::OR_INT_LIT8:
1107 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Or);
1108 break;
1109
1110 case Instruction::XOR_INT_LIT16:
1111 case Instruction::XOR_INT_LIT8:
1112 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Xor);
1113 break;
1114
1115 case Instruction::SHL_INT_LIT8:
Logan Chien5539ad02012-04-02 14:36:55 +08001116 EmitInsn_IntShiftArithmImmediate(ARGS, kIntArithm_Shl);
Logan Chien70f94b42011-12-27 17:49:11 +08001117 break;
1118
1119 case Instruction::SHR_INT_LIT8:
Logan Chien5539ad02012-04-02 14:36:55 +08001120 EmitInsn_IntShiftArithmImmediate(ARGS, kIntArithm_Shr);
Logan Chien70f94b42011-12-27 17:49:11 +08001121 break;
1122
1123 case Instruction::USHR_INT_LIT8:
Logan Chien5539ad02012-04-02 14:36:55 +08001124 EmitInsn_IntShiftArithmImmediate(ARGS, kIntArithm_UShr);
Logan Chien70f94b42011-12-27 17:49:11 +08001125 break;
1126
Logan Chien9e5f5c12012-04-10 13:51:45 +08001127 case Instruction::THROW_VERIFICATION_ERROR:
1128 EmitInsn_ThrowVerificationError(ARGS);
1129 break;
1130
Logan Chien70f94b42011-12-27 17:49:11 +08001131 case Instruction::UNUSED_3E:
1132 case Instruction::UNUSED_3F:
1133 case Instruction::UNUSED_40:
1134 case Instruction::UNUSED_41:
1135 case Instruction::UNUSED_42:
1136 case Instruction::UNUSED_43:
1137 case Instruction::UNUSED_73:
1138 case Instruction::UNUSED_79:
1139 case Instruction::UNUSED_7A:
1140 case Instruction::UNUSED_E3:
1141 case Instruction::UNUSED_E4:
1142 case Instruction::UNUSED_E5:
1143 case Instruction::UNUSED_E6:
1144 case Instruction::UNUSED_E7:
1145 case Instruction::UNUSED_E8:
1146 case Instruction::UNUSED_E9:
1147 case Instruction::UNUSED_EA:
1148 case Instruction::UNUSED_EB:
1149 case Instruction::UNUSED_EC:
Logan Chien70f94b42011-12-27 17:49:11 +08001150 case Instruction::UNUSED_EE:
1151 case Instruction::UNUSED_EF:
1152 case Instruction::UNUSED_F0:
1153 case Instruction::UNUSED_F1:
1154 case Instruction::UNUSED_F2:
1155 case Instruction::UNUSED_F3:
1156 case Instruction::UNUSED_F4:
1157 case Instruction::UNUSED_F5:
1158 case Instruction::UNUSED_F6:
1159 case Instruction::UNUSED_F7:
1160 case Instruction::UNUSED_F8:
1161 case Instruction::UNUSED_F9:
1162 case Instruction::UNUSED_FA:
1163 case Instruction::UNUSED_FB:
1164 case Instruction::UNUSED_FC:
1165 case Instruction::UNUSED_FD:
1166 case Instruction::UNUSED_FE:
1167 case Instruction::UNUSED_FF:
1168 LOG(FATAL) << "Dex file contains UNUSED bytecode: " << insn->Opcode();
1169 break;
1170 }
1171
1172#undef ARGS
1173}
1174
1175
1176void MethodCompiler::EmitInsn_Nop(uint32_t dex_pc,
1177 Instruction const* insn) {
Logan Chiene09a6b72011-12-27 17:50:21 +08001178
1179 uint16_t insn_signature = code_item_->insns_[dex_pc];
1180
1181 if (insn_signature == Instruction::kPackedSwitchSignature ||
1182 insn_signature == Instruction::kSparseSwitchSignature ||
1183 insn_signature == Instruction::kArrayDataSignature) {
1184 irb_.CreateUnreachable();
Elliott Hughesb25c3f62012-03-26 16:35:06 -07001185 } else {
Logan Chiene09a6b72011-12-27 17:50:21 +08001186 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1187 }
Shih-wei Liaod1fec812012-02-13 09:51:10 -08001188}
1189
1190
Logan Chien70f94b42011-12-27 17:49:11 +08001191void MethodCompiler::EmitInsn_Move(uint32_t dex_pc,
1192 Instruction const* insn,
1193 JType jty) {
Logan Chien48173132011-12-27 17:51:13 +08001194
Elliott Hughesadb8c672012-03-06 16:49:32 -08001195 DecodedInstruction dec_insn(insn);
Logan Chien48173132011-12-27 17:51:13 +08001196
Elliott Hughesadb8c672012-03-06 16:49:32 -08001197 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, jty, kReg);
1198 EmitStoreDalvikReg(dec_insn.vA, jty, kReg, src_value);
Logan Chien48173132011-12-27 17:51:13 +08001199
Logan Chien70f94b42011-12-27 17:49:11 +08001200 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1201}
1202
1203
1204void MethodCompiler::EmitInsn_MoveResult(uint32_t dex_pc,
1205 Instruction const* insn,
1206 JType jty) {
Logan Chien48173132011-12-27 17:51:13 +08001207
Elliott Hughesadb8c672012-03-06 16:49:32 -08001208 DecodedInstruction dec_insn(insn);
Logan Chien48173132011-12-27 17:51:13 +08001209
1210 llvm::Value* src_value = EmitLoadDalvikRetValReg(jty, kReg);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001211 EmitStoreDalvikReg(dec_insn.vA, jty, kReg, src_value);
Logan Chien48173132011-12-27 17:51:13 +08001212
Logan Chien70f94b42011-12-27 17:49:11 +08001213 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1214}
1215
1216
1217void MethodCompiler::EmitInsn_MoveException(uint32_t dex_pc,
1218 Instruction const* insn) {
Logan Chien3354cec2012-01-13 14:29:03 +08001219
Elliott Hughesadb8c672012-03-06 16:49:32 -08001220 DecodedInstruction dec_insn(insn);
Logan Chien3354cec2012-01-13 14:29:03 +08001221
TDYa127ee1f59b2012-04-25 00:56:40 -07001222 // Get thread
Logan Chien3354cec2012-01-13 14:29:03 +08001223 llvm::Value* thread_object_addr =
1224 irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1225
TDYa127ee1f59b2012-04-25 00:56:40 -07001226 // Get thread-local exception field address
1227 llvm::Value* exception_object_addr =
1228 irb_.LoadFromObjectOffset(thread_object_addr,
1229 Thread::ExceptionOffset().Int32Value(),
1230 irb_.getJObjectTy());
Logan Chien3354cec2012-01-13 14:29:03 +08001231
1232 // Set thread-local exception field address to NULL
TDYa127ee1f59b2012-04-25 00:56:40 -07001233 irb_.StoreToObjectOffset(thread_object_addr,
1234 Thread::ExceptionOffset().Int32Value(),
1235 irb_.getJNull());
Logan Chien3354cec2012-01-13 14:29:03 +08001236
1237 // Keep the exception object in the Dalvik register
Elliott Hughesadb8c672012-03-06 16:49:32 -08001238 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, exception_object_addr);
Logan Chien3354cec2012-01-13 14:29:03 +08001239
Logan Chien70f94b42011-12-27 17:49:11 +08001240 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1241}
1242
1243
1244void MethodCompiler::EmitInsn_ThrowException(uint32_t dex_pc,
1245 Instruction const* insn) {
Logan Chien6c6f12d2012-01-13 19:26:27 +08001246
Elliott Hughesadb8c672012-03-06 16:49:32 -08001247 DecodedInstruction dec_insn(insn);
Logan Chien6c6f12d2012-01-13 19:26:27 +08001248
1249 llvm::Value* exception_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001250 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chien6c6f12d2012-01-13 19:26:27 +08001251
TDYa127c8dc1012012-04-19 07:03:33 -07001252 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001253
Logan Chien6c6f12d2012-01-13 19:26:27 +08001254 irb_.CreateCall(irb_.GetRuntime(ThrowException), exception_addr);
1255
1256 EmitBranchExceptionLandingPad(dex_pc);
Logan Chien70f94b42011-12-27 17:49:11 +08001257}
1258
1259
Logan Chien9e5f5c12012-04-10 13:51:45 +08001260void MethodCompiler::EmitInsn_ThrowVerificationError(uint32_t dex_pc,
1261 Instruction const* insn) {
1262
1263 DecodedInstruction dec_insn(insn);
1264
TDYa127c8dc1012012-04-19 07:03:33 -07001265 EmitUpdateDexPC(dex_pc);
Logan Chien9e5f5c12012-04-10 13:51:45 +08001266
1267 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1268 llvm::Value* kind_value = irb_.getInt32(dec_insn.vA);
1269 llvm::Value* ref_value = irb_.getInt32(dec_insn.vB);
1270
1271 irb_.CreateCall3(irb_.GetRuntime(ThrowVerificationError),
1272 method_object_addr, kind_value, ref_value);
1273
1274 EmitBranchExceptionLandingPad(dex_pc);
1275}
1276
1277
Logan Chien70f94b42011-12-27 17:49:11 +08001278void MethodCompiler::EmitInsn_ReturnVoid(uint32_t dex_pc,
1279 Instruction const* insn) {
Logan Chien8898a272011-12-27 17:51:56 +08001280 // Garbage collection safe-point
1281 EmitGuard_GarbageCollectionSuspend(dex_pc);
1282
Logan Chien8dfcbea2012-02-17 18:50:32 +08001283 // Pop the shadow frame
1284 EmitPopShadowFrame();
1285
Logan Chien8898a272011-12-27 17:51:56 +08001286 // Return!
1287 irb_.CreateRetVoid();
Logan Chien70f94b42011-12-27 17:49:11 +08001288}
1289
1290
1291void MethodCompiler::EmitInsn_Return(uint32_t dex_pc,
1292 Instruction const* insn) {
Logan Chien8898a272011-12-27 17:51:56 +08001293
Elliott Hughesadb8c672012-03-06 16:49:32 -08001294 DecodedInstruction dec_insn(insn);
Logan Chien8898a272011-12-27 17:51:56 +08001295
1296 // Garbage collection safe-point
1297 EmitGuard_GarbageCollectionSuspend(dex_pc);
1298
Logan Chien8dfcbea2012-02-17 18:50:32 +08001299 // Pop the shadow frame
1300 EmitPopShadowFrame();
1301 // NOTE: It is important to keep this AFTER the GC safe-point. Otherwise,
1302 // the return value might be collected since the shadow stack is popped.
1303
Logan Chien8898a272011-12-27 17:51:56 +08001304 // Return!
Logan Chiendd361c92012-04-10 23:40:37 +08001305 char ret_shorty = oat_compilation_unit_->GetShorty()[0];
Elliott Hughesadb8c672012-03-06 16:49:32 -08001306 llvm::Value* retval = EmitLoadDalvikReg(dec_insn.vA, ret_shorty, kAccurate);
Logan Chien8898a272011-12-27 17:51:56 +08001307
1308 irb_.CreateRet(retval);
Logan Chien70f94b42011-12-27 17:49:11 +08001309}
1310
1311
1312void MethodCompiler::EmitInsn_LoadConstant(uint32_t dex_pc,
1313 Instruction const* insn,
1314 JType imm_jty) {
Shih-wei Liao798366e2012-02-16 09:25:33 -08001315
Elliott Hughesadb8c672012-03-06 16:49:32 -08001316 DecodedInstruction dec_insn(insn);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001317
1318 DCHECK(imm_jty == kInt || imm_jty == kLong) << imm_jty;
1319
1320 int64_t imm = 0;
1321
1322 switch (insn->Opcode()) {
1323 // 32-bit Immediate
1324 case Instruction::CONST_4:
1325 case Instruction::CONST_16:
1326 case Instruction::CONST:
1327 case Instruction::CONST_WIDE_16:
1328 case Instruction::CONST_WIDE_32:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001329 imm = static_cast<int64_t>(static_cast<int32_t>(dec_insn.vB));
Shih-wei Liao798366e2012-02-16 09:25:33 -08001330 break;
1331
1332 case Instruction::CONST_HIGH16:
1333 imm = static_cast<int64_t>(static_cast<int32_t>(
Elliott Hughesadb8c672012-03-06 16:49:32 -08001334 static_cast<uint32_t>(static_cast<uint16_t>(dec_insn.vB)) << 16));
Shih-wei Liao798366e2012-02-16 09:25:33 -08001335 break;
1336
1337 // 64-bit Immediate
1338 case Instruction::CONST_WIDE:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001339 imm = static_cast<int64_t>(dec_insn.vB_wide);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001340 break;
1341
1342 case Instruction::CONST_WIDE_HIGH16:
1343 imm = static_cast<int64_t>(
Elliott Hughesadb8c672012-03-06 16:49:32 -08001344 static_cast<uint64_t>(static_cast<uint16_t>(dec_insn.vB)) << 48);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001345 break;
1346
1347 // Unknown opcode for load constant (unreachable)
1348 default:
1349 LOG(FATAL) << "Unknown opcode for load constant: " << insn->Opcode();
1350 break;
1351 }
1352
1353 // Store the non-object register
1354 llvm::Type* imm_type = irb_.getJType(imm_jty, kAccurate);
1355 llvm::Constant* imm_value = llvm::ConstantInt::getSigned(imm_type, imm);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001356 EmitStoreDalvikReg(dec_insn.vA, imm_jty, kAccurate, imm_value);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001357
1358 // Store the object register if it is possible to be null.
1359 if (imm_jty == kInt && imm == 0) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001360 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, irb_.getJNull());
Shih-wei Liao798366e2012-02-16 09:25:33 -08001361 }
1362
Logan Chien70f94b42011-12-27 17:49:11 +08001363 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1364}
1365
1366
1367void MethodCompiler::EmitInsn_LoadConstantString(uint32_t dex_pc,
1368 Instruction const* insn) {
Logan Chienc3b4ba12012-01-16 19:52:53 +08001369
Elliott Hughesadb8c672012-03-06 16:49:32 -08001370 DecodedInstruction dec_insn(insn);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001371
Elliott Hughesadb8c672012-03-06 16:49:32 -08001372 uint32_t string_idx = dec_insn.vB;
Logan Chienc3b4ba12012-01-16 19:52:53 +08001373
1374 llvm::Value* string_field_addr = EmitLoadDexCacheStringFieldAddr(string_idx);
1375
1376 llvm::Value* string_addr = irb_.CreateLoad(string_field_addr);
1377
1378 if (!compiler_->CanAssumeStringIsPresentInDexCache(dex_cache_, string_idx)) {
1379 llvm::BasicBlock* block_str_exist =
1380 CreateBasicBlockWithDexPC(dex_pc, "str_exist");
1381
1382 llvm::BasicBlock* block_str_resolve =
1383 CreateBasicBlockWithDexPC(dex_pc, "str_resolve");
1384
1385 // Test: Is the string resolved and in the dex cache?
1386 llvm::Value* equal_null = irb_.CreateICmpEQ(string_addr, irb_.getJNull());
1387
TDYa127a849cb62012-04-01 05:59:34 -07001388 irb_.CreateCondBr(equal_null, block_str_resolve, block_str_exist);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001389
1390 // String is resolved, go to next basic block.
1391 irb_.SetInsertPoint(block_str_exist);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001392 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, string_addr);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001393 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1394
1395 // String is not resolved yet, resolve it now.
1396 irb_.SetInsertPoint(block_str_resolve);
1397
1398 llvm::Function* runtime_func = irb_.GetRuntime(ResolveString);
1399
1400 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1401
1402 llvm::Value* string_idx_value = irb_.getInt32(string_idx);
1403
TDYa127c8dc1012012-04-19 07:03:33 -07001404 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001405
1406 string_addr = irb_.CreateCall2(runtime_func, method_object_addr,
1407 string_idx_value);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001408
1409 EmitGuard_ExceptionLandingPad(dex_pc);
1410 }
1411
1412 // Store the string object to the Dalvik register
Elliott Hughesadb8c672012-03-06 16:49:32 -08001413 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, string_addr);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001414
Logan Chien70f94b42011-12-27 17:49:11 +08001415 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1416}
1417
1418
Logan Chien27b30252012-01-14 03:43:35 +08001419llvm::Value* MethodCompiler::EmitLoadConstantClass(uint32_t dex_pc,
1420 uint32_t type_idx) {
1421 if (!compiler_->CanAccessTypeWithoutChecks(method_idx_, dex_cache_,
1422 *dex_file_, type_idx)) {
1423 llvm::Value* type_idx_value = irb_.getInt32(type_idx);
1424
1425 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1426
TDYa127706e9b62012-04-19 12:24:26 -07001427 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1428
Logan Chien27b30252012-01-14 03:43:35 +08001429 llvm::Function* runtime_func =
1430 irb_.GetRuntime(InitializeTypeAndVerifyAccess);
1431
TDYa127c8dc1012012-04-19 07:03:33 -07001432 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001433
Logan Chien27b30252012-01-14 03:43:35 +08001434 llvm::Value* type_object_addr =
TDYa127706e9b62012-04-19 12:24:26 -07001435 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
Logan Chien27b30252012-01-14 03:43:35 +08001436
1437 EmitGuard_ExceptionLandingPad(dex_pc);
1438
1439 return type_object_addr;
1440
1441 } else {
1442 // Try to load the class (type) object from the test cache.
1443 llvm::Value* type_field_addr =
1444 EmitLoadDexCacheResolvedTypeFieldAddr(type_idx);
1445
1446 llvm::Value* type_object_addr = irb_.CreateLoad(type_field_addr);
1447
1448 if (compiler_->CanAssumeTypeIsPresentInDexCache(dex_cache_, type_idx)) {
1449 return type_object_addr;
1450 }
1451
1452 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
1453
1454 // Test whether class (type) object is in the dex cache or not
1455 llvm::Value* equal_null =
1456 irb_.CreateICmpEQ(type_object_addr, irb_.getJNull());
1457
1458 llvm::BasicBlock* block_cont =
1459 CreateBasicBlockWithDexPC(dex_pc, "cont");
1460
1461 llvm::BasicBlock* block_load_class =
1462 CreateBasicBlockWithDexPC(dex_pc, "load_class");
1463
1464 irb_.CreateCondBr(equal_null, block_load_class, block_cont);
1465
1466 // Failback routine to load the class object
1467 irb_.SetInsertPoint(block_load_class);
1468
1469 llvm::Function* runtime_func = irb_.GetRuntime(InitializeType);
1470
1471 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
1472
1473 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1474
TDYa127706e9b62012-04-19 12:24:26 -07001475 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1476
TDYa127c8dc1012012-04-19 07:03:33 -07001477 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001478
Logan Chien27b30252012-01-14 03:43:35 +08001479 llvm::Value* loaded_type_object_addr =
TDYa127706e9b62012-04-19 12:24:26 -07001480 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
Logan Chien27b30252012-01-14 03:43:35 +08001481
1482 EmitGuard_ExceptionLandingPad(dex_pc);
1483
1484 llvm::BasicBlock* block_after_load_class = irb_.GetInsertBlock();
1485
1486 irb_.CreateBr(block_cont);
1487
1488 // Now the class object must be loaded
1489 irb_.SetInsertPoint(block_cont);
1490
1491 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1492
1493 phi->addIncoming(type_object_addr, block_original);
1494 phi->addIncoming(loaded_type_object_addr, block_after_load_class);
1495
1496 return phi;
1497 }
1498}
1499
1500
Logan Chien70f94b42011-12-27 17:49:11 +08001501void MethodCompiler::EmitInsn_LoadConstantClass(uint32_t dex_pc,
1502 Instruction const* insn) {
Logan Chien27b30252012-01-14 03:43:35 +08001503
Elliott Hughesadb8c672012-03-06 16:49:32 -08001504 DecodedInstruction dec_insn(insn);
Logan Chien27b30252012-01-14 03:43:35 +08001505
Elliott Hughesadb8c672012-03-06 16:49:32 -08001506 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, dec_insn.vB);
1507 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, type_object_addr);
Logan Chien27b30252012-01-14 03:43:35 +08001508
Logan Chien70f94b42011-12-27 17:49:11 +08001509 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1510}
1511
1512
1513void MethodCompiler::EmitInsn_MonitorEnter(uint32_t dex_pc,
1514 Instruction const* insn) {
Logan Chien9e0dbe42012-01-13 12:11:37 +08001515
Elliott Hughesadb8c672012-03-06 16:49:32 -08001516 DecodedInstruction dec_insn(insn);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001517
1518 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001519 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001520
1521 // TODO: Slow path always. May not need NullPointerException check.
1522 EmitGuard_NullPointerException(dex_pc, object_addr);
1523
TDYa127c8dc1012012-04-19 07:03:33 -07001524 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001525
TDYa127706e9b62012-04-19 12:24:26 -07001526 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1527
1528 irb_.CreateCall2(irb_.GetRuntime(LockObject), object_addr, thread_object_addr);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001529 EmitGuard_ExceptionLandingPad(dex_pc);
1530
Logan Chien70f94b42011-12-27 17:49:11 +08001531 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1532}
1533
1534
1535void MethodCompiler::EmitInsn_MonitorExit(uint32_t dex_pc,
1536 Instruction const* insn) {
Logan Chien9e0dbe42012-01-13 12:11:37 +08001537
Elliott Hughesadb8c672012-03-06 16:49:32 -08001538 DecodedInstruction dec_insn(insn);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001539
1540 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001541 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001542
1543 EmitGuard_NullPointerException(dex_pc, object_addr);
1544
TDYa127c8dc1012012-04-19 07:03:33 -07001545 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001546
TDYa127706e9b62012-04-19 12:24:26 -07001547 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1548
1549 irb_.CreateCall2(irb_.GetRuntime(UnlockObject), object_addr, thread_object_addr);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001550 EmitGuard_ExceptionLandingPad(dex_pc);
1551
Logan Chien70f94b42011-12-27 17:49:11 +08001552 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1553}
1554
1555
1556void MethodCompiler::EmitInsn_CheckCast(uint32_t dex_pc,
1557 Instruction const* insn) {
Logan Chienfc880952012-01-15 23:53:10 +08001558
Elliott Hughesadb8c672012-03-06 16:49:32 -08001559 DecodedInstruction dec_insn(insn);
Logan Chienfc880952012-01-15 23:53:10 +08001560
1561 llvm::BasicBlock* block_test_class =
1562 CreateBasicBlockWithDexPC(dex_pc, "test_class");
1563
1564 llvm::BasicBlock* block_test_sub_class =
1565 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
1566
1567 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001568 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chienfc880952012-01-15 23:53:10 +08001569
1570 // Test: Is the reference equal to null? Act as no-op when it is null.
1571 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
1572
1573 irb_.CreateCondBr(equal_null,
1574 GetNextBasicBlock(dex_pc),
1575 block_test_class);
1576
1577 // Test: Is the object instantiated from the given class?
1578 irb_.SetInsertPoint(block_test_class);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001579 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, dec_insn.vB);
Logan Chienfc880952012-01-15 23:53:10 +08001580 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1581
1582 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
1583
1584 llvm::Value* object_type_field_addr =
1585 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
1586
1587 llvm::Value* object_type_object_addr =
1588 irb_.CreateLoad(object_type_field_addr);
1589
1590 llvm::Value* equal_class =
1591 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
1592
1593 irb_.CreateCondBr(equal_class,
1594 GetNextBasicBlock(dex_pc),
1595 block_test_sub_class);
1596
1597 // Test: Is the object instantiated from the subclass of the given class?
1598 irb_.SetInsertPoint(block_test_sub_class);
1599
TDYa127c8dc1012012-04-19 07:03:33 -07001600 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001601
Logan Chienfc880952012-01-15 23:53:10 +08001602 irb_.CreateCall2(irb_.GetRuntime(CheckCast),
1603 type_object_addr, object_type_object_addr);
1604
1605 EmitGuard_ExceptionLandingPad(dex_pc);
1606
Logan Chien70f94b42011-12-27 17:49:11 +08001607 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1608}
1609
1610
1611void MethodCompiler::EmitInsn_InstanceOf(uint32_t dex_pc,
1612 Instruction const* insn) {
Logan Chien68725e22012-01-15 22:25:34 +08001613
Elliott Hughesadb8c672012-03-06 16:49:32 -08001614 DecodedInstruction dec_insn(insn);
Logan Chien68725e22012-01-15 22:25:34 +08001615
1616 llvm::Constant* zero = irb_.getJInt(0);
1617 llvm::Constant* one = irb_.getJInt(1);
1618
1619 llvm::BasicBlock* block_nullp = CreateBasicBlockWithDexPC(dex_pc, "nullp");
1620
1621 llvm::BasicBlock* block_test_class =
1622 CreateBasicBlockWithDexPC(dex_pc, "test_class");
1623
1624 llvm::BasicBlock* block_class_equals =
1625 CreateBasicBlockWithDexPC(dex_pc, "class_eq");
1626
1627 llvm::BasicBlock* block_test_sub_class =
1628 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
1629
1630 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001631 EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
Logan Chien68725e22012-01-15 22:25:34 +08001632
1633 // Overview of the following code :
1634 // We check for null, if so, then false, otherwise check for class == . If so
1635 // then true, otherwise do callout slowpath.
1636 //
1637 // Test: Is the reference equal to null? Set 0 when it is null.
1638 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
1639
1640 irb_.CreateCondBr(equal_null, block_nullp, block_test_class);
1641
1642 irb_.SetInsertPoint(block_nullp);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001643 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, zero);
Logan Chien68725e22012-01-15 22:25:34 +08001644 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1645
1646 // Test: Is the object instantiated from the given class?
1647 irb_.SetInsertPoint(block_test_class);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001648 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, dec_insn.vC);
Logan Chien68725e22012-01-15 22:25:34 +08001649 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1650
1651 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
1652
1653 llvm::Value* object_type_field_addr =
1654 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
1655
1656 llvm::Value* object_type_object_addr =
1657 irb_.CreateLoad(object_type_field_addr);
1658
1659 llvm::Value* equal_class =
1660 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
1661
1662 irb_.CreateCondBr(equal_class, block_class_equals, block_test_sub_class);
1663
1664 irb_.SetInsertPoint(block_class_equals);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001665 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, one);
Logan Chien68725e22012-01-15 22:25:34 +08001666 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1667
1668 // Test: Is the object instantiated from the subclass of the given class?
1669 irb_.SetInsertPoint(block_test_sub_class);
1670
1671 llvm::Value* result =
1672 irb_.CreateCall2(irb_.GetRuntime(IsAssignable),
1673 type_object_addr, object_type_object_addr);
1674
Elliott Hughesadb8c672012-03-06 16:49:32 -08001675 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result);
Logan Chien68725e22012-01-15 22:25:34 +08001676
Logan Chien70f94b42011-12-27 17:49:11 +08001677 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1678}
1679
1680
Logan Chien61bb6142012-02-03 15:34:53 +08001681llvm::Value* MethodCompiler::EmitLoadArrayLength(llvm::Value* array) {
Logan Chien61bb6142012-02-03 15:34:53 +08001682 // Load array length
TDYa127ee1f59b2012-04-25 00:56:40 -07001683 return irb_.LoadFromObjectOffset(array,
1684 Array::LengthOffset().Int32Value(),
1685 irb_.getJIntTy());
Logan Chien61bb6142012-02-03 15:34:53 +08001686}
1687
1688
Logan Chien70f94b42011-12-27 17:49:11 +08001689void MethodCompiler::EmitInsn_ArrayLength(uint32_t dex_pc,
1690 Instruction const* insn) {
Logan Chien61bb6142012-02-03 15:34:53 +08001691
Elliott Hughesadb8c672012-03-06 16:49:32 -08001692 DecodedInstruction dec_insn(insn);
Logan Chien61bb6142012-02-03 15:34:53 +08001693
1694 // Get the array object address
Elliott Hughesadb8c672012-03-06 16:49:32 -08001695 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
Logan Chien61bb6142012-02-03 15:34:53 +08001696 EmitGuard_NullPointerException(dex_pc, array_addr);
1697
1698 // Get the array length and store it to the register
1699 llvm::Value* array_len = EmitLoadArrayLength(array_addr);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001700 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, array_len);
Logan Chien61bb6142012-02-03 15:34:53 +08001701
Logan Chien70f94b42011-12-27 17:49:11 +08001702 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1703}
1704
1705
1706void MethodCompiler::EmitInsn_NewInstance(uint32_t dex_pc,
1707 Instruction const* insn) {
Logan Chien032bdad2012-01-16 09:59:23 +08001708
Elliott Hughesadb8c672012-03-06 16:49:32 -08001709 DecodedInstruction dec_insn(insn);
Logan Chien032bdad2012-01-16 09:59:23 +08001710
1711 llvm::Function* runtime_func;
Logan Chien1a032b12012-04-11 11:43:04 +08001712 if (compiler_->CanAccessInstantiableTypeWithoutChecks(
1713 method_idx_, dex_cache_, *dex_file_, dec_insn.vB)) {
Logan Chien032bdad2012-01-16 09:59:23 +08001714 runtime_func = irb_.GetRuntime(AllocObject);
1715 } else {
1716 runtime_func = irb_.GetRuntime(AllocObjectWithAccessCheck);
1717 }
1718
Elliott Hughesadb8c672012-03-06 16:49:32 -08001719 llvm::Constant* type_index_value = irb_.getInt32(dec_insn.vB);
Logan Chien032bdad2012-01-16 09:59:23 +08001720
1721 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1722
TDYa127da83d972012-04-18 00:21:49 -07001723 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1724
TDYa127c8dc1012012-04-19 07:03:33 -07001725 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001726
Logan Chien032bdad2012-01-16 09:59:23 +08001727 llvm::Value* object_addr =
TDYa127da83d972012-04-18 00:21:49 -07001728 irb_.CreateCall3(runtime_func, type_index_value, method_object_addr, thread_object_addr);
Logan Chien032bdad2012-01-16 09:59:23 +08001729
1730 EmitGuard_ExceptionLandingPad(dex_pc);
1731
Elliott Hughesadb8c672012-03-06 16:49:32 -08001732 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, object_addr);
Logan Chien032bdad2012-01-16 09:59:23 +08001733
Logan Chien70f94b42011-12-27 17:49:11 +08001734 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1735}
1736
1737
Logan Chiena2cc6a32012-01-16 10:38:41 +08001738llvm::Value* MethodCompiler::EmitAllocNewArray(uint32_t dex_pc,
1739 int32_t length,
1740 uint32_t type_idx,
1741 bool is_filled_new_array) {
1742 llvm::Function* runtime_func;
1743
1744 bool skip_access_check =
1745 compiler_->CanAccessTypeWithoutChecks(method_idx_, dex_cache_,
1746 *dex_file_, type_idx);
1747
TDYa127a849cb62012-04-01 05:59:34 -07001748 llvm::Value* array_length_value;
1749
Logan Chiena2cc6a32012-01-16 10:38:41 +08001750 if (is_filled_new_array) {
1751 runtime_func = skip_access_check ?
1752 irb_.GetRuntime(CheckAndAllocArray) :
1753 irb_.GetRuntime(CheckAndAllocArrayWithAccessCheck);
TDYa127a849cb62012-04-01 05:59:34 -07001754 array_length_value = irb_.getInt32(length);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001755 } else {
1756 runtime_func = skip_access_check ?
1757 irb_.GetRuntime(AllocArray) :
1758 irb_.GetRuntime(AllocArrayWithAccessCheck);
TDYa127a849cb62012-04-01 05:59:34 -07001759 array_length_value = EmitLoadDalvikReg(length, kInt, kAccurate);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001760 }
1761
1762 llvm::Constant* type_index_value = irb_.getInt32(type_idx);
1763
1764 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1765
TDYa127da83d972012-04-18 00:21:49 -07001766 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1767
TDYa127c8dc1012012-04-19 07:03:33 -07001768 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001769
Logan Chiena2cc6a32012-01-16 10:38:41 +08001770 llvm::Value* object_addr =
TDYa127da83d972012-04-18 00:21:49 -07001771 irb_.CreateCall4(runtime_func, type_index_value, method_object_addr,
1772 array_length_value, thread_object_addr);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001773
1774 EmitGuard_ExceptionLandingPad(dex_pc);
1775
1776 return object_addr;
1777}
1778
1779
Logan Chien70f94b42011-12-27 17:49:11 +08001780void MethodCompiler::EmitInsn_NewArray(uint32_t dex_pc,
1781 Instruction const* insn) {
Logan Chiena2cc6a32012-01-16 10:38:41 +08001782
Elliott Hughesadb8c672012-03-06 16:49:32 -08001783 DecodedInstruction dec_insn(insn);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001784
1785 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001786 EmitAllocNewArray(dex_pc, dec_insn.vB, dec_insn.vC, false);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001787
Elliott Hughesadb8c672012-03-06 16:49:32 -08001788 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, object_addr);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001789
Logan Chien70f94b42011-12-27 17:49:11 +08001790 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1791}
1792
1793
1794void MethodCompiler::EmitInsn_FilledNewArray(uint32_t dex_pc,
1795 Instruction const* insn,
1796 bool is_range) {
Logan Chiena85fb2f2012-01-16 12:52:56 +08001797
Elliott Hughesadb8c672012-03-06 16:49:32 -08001798 DecodedInstruction dec_insn(insn);
Logan Chiena85fb2f2012-01-16 12:52:56 +08001799
1800 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001801 EmitAllocNewArray(dex_pc, dec_insn.vA, dec_insn.vB, true);
Logan Chiena85fb2f2012-01-16 12:52:56 +08001802
Elliott Hughesadb8c672012-03-06 16:49:32 -08001803 if (dec_insn.vA > 0) {
Logan Chiendd361c92012-04-10 23:40:37 +08001804 // Resolve the element type
TDYa127183cf262012-04-11 07:53:21 -07001805 Class* klass = dex_cache_->GetResolvedType(dec_insn.vB)->GetComponentType();
Logan Chiendd361c92012-04-10 23:40:37 +08001806 // TODO: Avoid the usage of the dex_cache_. Try to figure out a better
1807 // way to distinguish [I and [L.
Logan Chiena85fb2f2012-01-16 12:52:56 +08001808 CHECK_NE(klass, static_cast<Class*>(NULL));
Logan Chiena85fb2f2012-01-16 12:52:56 +08001809
Logan Chiendd361c92012-04-10 23:40:37 +08001810 uint32_t alignment;
1811 llvm::Constant* elem_size;
1812 llvm::PointerType* field_type;
Logan Chiena85fb2f2012-01-16 12:52:56 +08001813
Logan Chiendd361c92012-04-10 23:40:37 +08001814 // NOTE: Currently filled-new-array only supports 'L', '[', and 'I'
1815 // as the element, thus we are only checking 2 cases: primitive int and
1816 // non-primitive type.
Logan Chiena85fb2f2012-01-16 12:52:56 +08001817 if (klass->IsPrimitiveInt()) {
Logan Chiendd361c92012-04-10 23:40:37 +08001818 alignment = sizeof(int32_t);
1819 elem_size = irb_.getPtrEquivInt(sizeof(int32_t));
Logan Chiena85fb2f2012-01-16 12:52:56 +08001820 field_type = irb_.getJIntTy()->getPointerTo();
1821 } else {
1822 CHECK(!klass->IsPrimitive());
Logan Chiendd361c92012-04-10 23:40:37 +08001823 alignment = irb_.getSizeOfPtrEquivInt();
1824 elem_size = irb_.getSizeOfPtrEquivIntValue();
Logan Chiena85fb2f2012-01-16 12:52:56 +08001825 field_type = irb_.getJObjectTy()->getPointerTo();
1826 }
1827
Logan Chiendd361c92012-04-10 23:40:37 +08001828 llvm::Value* data_field_offset =
1829 irb_.getPtrEquivInt(Array::DataOffset(alignment).Int32Value());
1830
1831 llvm::Value* data_field_addr =
1832 irb_.CreatePtrDisp(object_addr, data_field_offset, field_type);
1833
Logan Chiena85fb2f2012-01-16 12:52:56 +08001834 // TODO: Tune this code. Currently we are generating one instruction for
1835 // one element which may be very space consuming. Maybe changing to use
1836 // memcpy may help; however, since we can't guarantee that the alloca of
1837 // dalvik register are continuous, we can't perform such optimization yet.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001838 for (uint32_t i = 0; i < dec_insn.vA; ++i) {
Logan Chiena85fb2f2012-01-16 12:52:56 +08001839 int reg_index;
1840 if (is_range) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001841 reg_index = dec_insn.vC + i;
Logan Chiena85fb2f2012-01-16 12:52:56 +08001842 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001843 reg_index = dec_insn.arg[i];
Logan Chiena85fb2f2012-01-16 12:52:56 +08001844 }
1845
1846 llvm::Value* reg_value;
1847 if (klass->IsPrimitiveInt()) {
1848 reg_value = EmitLoadDalvikReg(reg_index, kInt, kAccurate);
1849 } else {
1850 reg_value = EmitLoadDalvikReg(reg_index, kObject, kAccurate);
1851 }
1852
1853 irb_.CreateStore(reg_value, data_field_addr);
1854
Logan Chiendd361c92012-04-10 23:40:37 +08001855 data_field_addr =
1856 irb_.CreatePtrDisp(data_field_addr, elem_size, field_type);
Logan Chiena85fb2f2012-01-16 12:52:56 +08001857 }
1858 }
1859
1860 EmitStoreDalvikRetValReg(kObject, kAccurate, object_addr);
1861
Logan Chien70f94b42011-12-27 17:49:11 +08001862 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1863}
1864
1865
1866void MethodCompiler::EmitInsn_FillArrayData(uint32_t dex_pc,
1867 Instruction const* insn) {
Logan Chiene58b6582012-01-16 17:13:13 +08001868
Elliott Hughesadb8c672012-03-06 16:49:32 -08001869 DecodedInstruction dec_insn(insn);
Logan Chiene58b6582012-01-16 17:13:13 +08001870
1871 // Read the payload
Logan Chiene58b6582012-01-16 17:13:13 +08001872 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
Elliott Hughesadb8c672012-03-06 16:49:32 -08001873 static_cast<int32_t>(dec_insn.vB);
Logan Chiene58b6582012-01-16 17:13:13 +08001874
Logan Chien19c350a2012-05-01 19:21:32 +08001875 const Instruction::ArrayDataPayload* payload =
1876 reinterpret_cast<const Instruction::ArrayDataPayload*>(
1877 code_item_->insns_ + payload_offset);
Logan Chiene58b6582012-01-16 17:13:13 +08001878
Logan Chien86f50672012-04-24 13:08:45 +08001879 // Load array object
Elliott Hughesadb8c672012-03-06 16:49:32 -08001880 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiene58b6582012-01-16 17:13:13 +08001881
Logan Chien86f50672012-04-24 13:08:45 +08001882 if (payload->element_count == 0) {
1883 // When the number of the elements in the payload is zero, we don't have
1884 // to copy any numbers. However, we should check whether the array object
1885 // address is equal to null or not.
1886 EmitGuard_NullPointerException(dex_pc, array_addr);
1887 } else {
1888 // To save the code size, we are going to call the runtime function to
1889 // copy the content from DexFile.
Logan Chiene58b6582012-01-16 17:13:13 +08001890
Logan Chien86f50672012-04-24 13:08:45 +08001891 // NOTE: We will check for the NullPointerException in the runtime.
Logan Chiene58b6582012-01-16 17:13:13 +08001892
Logan Chien86f50672012-04-24 13:08:45 +08001893 llvm::Function* runtime_func = irb_.GetRuntime(FillArrayData);
Logan Chiene58b6582012-01-16 17:13:13 +08001894
Logan Chien86f50672012-04-24 13:08:45 +08001895 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
Logan Chiene58b6582012-01-16 17:13:13 +08001896
Logan Chien86f50672012-04-24 13:08:45 +08001897 EmitUpdateDexPC(dex_pc);
Logan Chiene58b6582012-01-16 17:13:13 +08001898
Logan Chien86f50672012-04-24 13:08:45 +08001899 irb_.CreateCall4(runtime_func,
1900 method_object_addr, irb_.getInt32(dex_pc),
1901 array_addr, irb_.getInt32(payload_offset));
Logan Chiene58b6582012-01-16 17:13:13 +08001902
Logan Chien86f50672012-04-24 13:08:45 +08001903 EmitGuard_ExceptionLandingPad(dex_pc);
Logan Chiene58b6582012-01-16 17:13:13 +08001904 }
1905
Logan Chien70f94b42011-12-27 17:49:11 +08001906 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1907}
1908
1909
1910void MethodCompiler::EmitInsn_UnconditionalBranch(uint32_t dex_pc,
1911 Instruction const* insn) {
Logan Chiena466c162011-12-27 17:55:46 +08001912
Elliott Hughesadb8c672012-03-06 16:49:32 -08001913 DecodedInstruction dec_insn(insn);
Logan Chiena466c162011-12-27 17:55:46 +08001914
Elliott Hughesadb8c672012-03-06 16:49:32 -08001915 int32_t branch_offset = dec_insn.vA;
Logan Chiena466c162011-12-27 17:55:46 +08001916
1917 if (branch_offset <= 0) {
1918 // Garbage collection safe-point on backward branch
1919 EmitGuard_GarbageCollectionSuspend(dex_pc);
1920 }
1921
1922 irb_.CreateBr(GetBasicBlock(dex_pc + branch_offset));
Logan Chien70f94b42011-12-27 17:49:11 +08001923}
1924
1925
1926void MethodCompiler::EmitInsn_PackedSwitch(uint32_t dex_pc,
1927 Instruction const* insn) {
Logan Chien7a89b6d2011-12-27 17:56:56 +08001928
Elliott Hughesadb8c672012-03-06 16:49:32 -08001929 DecodedInstruction dec_insn(insn);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001930
Logan Chien7a89b6d2011-12-27 17:56:56 +08001931 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
Elliott Hughesadb8c672012-03-06 16:49:32 -08001932 static_cast<int32_t>(dec_insn.vB);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001933
Logan Chien19c350a2012-05-01 19:21:32 +08001934 const Instruction::PackedSwitchPayload* payload =
1935 reinterpret_cast<const Instruction::PackedSwitchPayload*>(
1936 code_item_->insns_ + payload_offset);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001937
Elliott Hughesadb8c672012-03-06 16:49:32 -08001938 llvm::Value* value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001939
1940 llvm::SwitchInst* sw =
Logan Chien19c350a2012-05-01 19:21:32 +08001941 irb_.CreateSwitch(value, GetNextBasicBlock(dex_pc), payload->case_count);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001942
Logan Chien19c350a2012-05-01 19:21:32 +08001943 for (uint16_t i = 0; i < payload->case_count; ++i) {
1944 sw->addCase(irb_.getInt32(payload->first_key + i),
1945 GetBasicBlock(dex_pc + payload->targets[i]));
Logan Chien7a89b6d2011-12-27 17:56:56 +08001946 }
Logan Chien70f94b42011-12-27 17:49:11 +08001947}
1948
1949
1950void MethodCompiler::EmitInsn_SparseSwitch(uint32_t dex_pc,
1951 Instruction const* insn) {
Logan Chien7a89b6d2011-12-27 17:56:56 +08001952
Elliott Hughesadb8c672012-03-06 16:49:32 -08001953 DecodedInstruction dec_insn(insn);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001954
Logan Chien7a89b6d2011-12-27 17:56:56 +08001955 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
Elliott Hughesadb8c672012-03-06 16:49:32 -08001956 static_cast<int32_t>(dec_insn.vB);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001957
Logan Chien19c350a2012-05-01 19:21:32 +08001958 const Instruction::SparseSwitchPayload* payload =
1959 reinterpret_cast<const Instruction::SparseSwitchPayload*>(
1960 code_item_->insns_ + payload_offset);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001961
Logan Chien19c350a2012-05-01 19:21:32 +08001962 const int32_t* keys = payload->GetKeys();
1963 const int32_t* targets = payload->GetTargets();
Logan Chien7a89b6d2011-12-27 17:56:56 +08001964
Elliott Hughesadb8c672012-03-06 16:49:32 -08001965 llvm::Value* value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001966
1967 llvm::SwitchInst* sw =
Logan Chien19c350a2012-05-01 19:21:32 +08001968 irb_.CreateSwitch(value, GetNextBasicBlock(dex_pc), payload->case_count);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001969
Logan Chien19c350a2012-05-01 19:21:32 +08001970 for (size_t i = 0; i < payload->case_count; ++i) {
Logan Chien7a89b6d2011-12-27 17:56:56 +08001971 sw->addCase(irb_.getInt32(keys[i]), GetBasicBlock(dex_pc + targets[i]));
1972 }
Logan Chien70f94b42011-12-27 17:49:11 +08001973}
1974
1975
1976void MethodCompiler::EmitInsn_FPCompare(uint32_t dex_pc,
1977 Instruction const* insn,
1978 JType fp_jty,
1979 bool gt_bias) {
Logan Chien2c37e8e2011-12-27 17:58:46 +08001980
Elliott Hughesadb8c672012-03-06 16:49:32 -08001981 DecodedInstruction dec_insn(insn);
Logan Chien2c37e8e2011-12-27 17:58:46 +08001982
1983 DCHECK(fp_jty == kFloat || fp_jty == kDouble) << "JType: " << fp_jty;
1984
Elliott Hughesadb8c672012-03-06 16:49:32 -08001985 llvm::Value* src1_value = EmitLoadDalvikReg(dec_insn.vB, fp_jty, kAccurate);
1986 llvm::Value* src2_value = EmitLoadDalvikReg(dec_insn.vC, fp_jty, kAccurate);
Logan Chien2c37e8e2011-12-27 17:58:46 +08001987
1988 llvm::Value* cmp_eq = irb_.CreateFCmpOEQ(src1_value, src2_value);
1989 llvm::Value* cmp_lt;
1990
1991 if (gt_bias) {
1992 cmp_lt = irb_.CreateFCmpOLT(src1_value, src2_value);
1993 } else {
1994 cmp_lt = irb_.CreateFCmpULT(src1_value, src2_value);
1995 }
1996
1997 llvm::Value* result = EmitCompareResultSelection(cmp_eq, cmp_lt);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001998 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result);
Logan Chien2c37e8e2011-12-27 17:58:46 +08001999
Logan Chien70f94b42011-12-27 17:49:11 +08002000 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2001}
2002
2003
2004void MethodCompiler::EmitInsn_LongCompare(uint32_t dex_pc,
2005 Instruction const* insn) {
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
Elliott Hughesadb8c672012-03-06 16:49:32 -08002009 llvm::Value* src1_value = EmitLoadDalvikReg(dec_insn.vB, kLong, kAccurate);
2010 llvm::Value* src2_value = EmitLoadDalvikReg(dec_insn.vC, kLong, kAccurate);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002011
2012 llvm::Value* cmp_eq = irb_.CreateICmpEQ(src1_value, src2_value);
2013 llvm::Value* cmp_lt = irb_.CreateICmpSLT(src1_value, src2_value);
2014
2015 llvm::Value* result = EmitCompareResultSelection(cmp_eq, cmp_lt);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002016 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002017
Logan Chien70f94b42011-12-27 17:49:11 +08002018 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2019}
2020
2021
Logan Chien2c37e8e2011-12-27 17:58:46 +08002022llvm::Value* MethodCompiler::EmitCompareResultSelection(llvm::Value* cmp_eq,
2023 llvm::Value* cmp_lt) {
2024
2025 llvm::Constant* zero = irb_.getJInt(0);
2026 llvm::Constant* pos1 = irb_.getJInt(1);
2027 llvm::Constant* neg1 = irb_.getJInt(-1);
2028
2029 llvm::Value* result_lt = irb_.CreateSelect(cmp_lt, neg1, pos1);
2030 llvm::Value* result_eq = irb_.CreateSelect(cmp_eq, zero, result_lt);
2031
2032 return result_eq;
2033}
2034
2035
Logan Chien70f94b42011-12-27 17:49:11 +08002036void MethodCompiler::EmitInsn_BinaryConditionalBranch(uint32_t dex_pc,
2037 Instruction const* insn,
2038 CondBranchKind cond) {
Logan Chiena78e3c82011-12-27 17:59:35 +08002039
Elliott Hughesadb8c672012-03-06 16:49:32 -08002040 DecodedInstruction dec_insn(insn);
Logan Chiena78e3c82011-12-27 17:59:35 +08002041
Elliott Hughesadb8c672012-03-06 16:49:32 -08002042 int8_t src1_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vA);
2043 int8_t src2_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vB);
Logan Chiena78e3c82011-12-27 17:59:35 +08002044
2045 DCHECK_NE(kRegUnknown, src1_reg_cat);
2046 DCHECK_NE(kRegUnknown, src2_reg_cat);
2047 DCHECK_NE(kRegCat2, src1_reg_cat);
2048 DCHECK_NE(kRegCat2, src2_reg_cat);
2049
Elliott Hughesadb8c672012-03-06 16:49:32 -08002050 int32_t branch_offset = dec_insn.vC;
Logan Chiena78e3c82011-12-27 17:59:35 +08002051
2052 if (branch_offset <= 0) {
2053 // Garbage collection safe-point on backward branch
2054 EmitGuard_GarbageCollectionSuspend(dex_pc);
2055 }
2056
Logan Chiena78e3c82011-12-27 17:59:35 +08002057 llvm::Value* src1_value;
2058 llvm::Value* src2_value;
2059
TDYa1278e9b4492012-04-24 15:50:27 -07002060 if (src1_reg_cat == kRegZero && src2_reg_cat == kRegZero) {
2061 src1_value = irb_.getInt32(0);
2062 src2_value = irb_.getInt32(0);
2063 } else if (src1_reg_cat != kRegZero && src2_reg_cat != kRegZero) {
Logan Chiena78e3c82011-12-27 17:59:35 +08002064 CHECK_EQ(src1_reg_cat, src2_reg_cat);
2065
2066 if (src1_reg_cat == kRegCat1nr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002067 src1_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
2068 src2_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002069 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002070 src1_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
2071 src2_value = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002072 }
2073 } else {
2074 DCHECK(src1_reg_cat == kRegZero ||
2075 src2_reg_cat == kRegZero);
2076
2077 if (src1_reg_cat == kRegZero) {
2078 if (src2_reg_cat == kRegCat1nr) {
2079 src1_value = irb_.getJInt(0);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002080 src2_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002081 } else {
2082 src1_value = irb_.getJNull();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002083 src2_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002084 }
2085 } else { // src2_reg_cat == kRegZero
2086 if (src2_reg_cat == kRegCat1nr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002087 src1_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002088 src2_value = irb_.getJInt(0);
2089 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002090 src1_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002091 src2_value = irb_.getJNull();
2092 }
2093 }
2094 }
2095
2096 llvm::Value* cond_value =
2097 EmitConditionResult(src1_value, src2_value, cond);
2098
2099 irb_.CreateCondBr(cond_value,
2100 GetBasicBlock(dex_pc + branch_offset),
2101 GetNextBasicBlock(dex_pc));
Logan Chien70f94b42011-12-27 17:49:11 +08002102}
2103
2104
2105void MethodCompiler::EmitInsn_UnaryConditionalBranch(uint32_t dex_pc,
2106 Instruction const* insn,
2107 CondBranchKind cond) {
Logan Chiena78e3c82011-12-27 17:59:35 +08002108
Elliott Hughesadb8c672012-03-06 16:49:32 -08002109 DecodedInstruction dec_insn(insn);
Logan Chiena78e3c82011-12-27 17:59:35 +08002110
Elliott Hughesadb8c672012-03-06 16:49:32 -08002111 int8_t src_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vA);
Logan Chiena78e3c82011-12-27 17:59:35 +08002112
2113 DCHECK_NE(kRegUnknown, src_reg_cat);
2114 DCHECK_NE(kRegCat2, src_reg_cat);
2115
Elliott Hughesadb8c672012-03-06 16:49:32 -08002116 int32_t branch_offset = dec_insn.vB;
Logan Chiena78e3c82011-12-27 17:59:35 +08002117
2118 if (branch_offset <= 0) {
2119 // Garbage collection safe-point on backward branch
2120 EmitGuard_GarbageCollectionSuspend(dex_pc);
2121 }
2122
Logan Chiena78e3c82011-12-27 17:59:35 +08002123 llvm::Value* src1_value;
2124 llvm::Value* src2_value;
2125
TDYa1278e9b4492012-04-24 15:50:27 -07002126 if (src_reg_cat == kRegZero) {
2127 src1_value = irb_.getInt32(0);
2128 src2_value = irb_.getInt32(0);
2129 } else if (src_reg_cat == kRegCat1nr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002130 src1_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002131 src2_value = irb_.getInt32(0);
2132 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002133 src1_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002134 src2_value = irb_.getJNull();
2135 }
2136
2137 llvm::Value* cond_value =
2138 EmitConditionResult(src1_value, src2_value, cond);
2139
2140 irb_.CreateCondBr(cond_value,
2141 GetBasicBlock(dex_pc + branch_offset),
2142 GetNextBasicBlock(dex_pc));
2143}
2144
2145
2146RegCategory MethodCompiler::GetInferredRegCategory(uint32_t dex_pc,
2147 uint16_t reg_idx) {
Logan Chiendd361c92012-04-10 23:40:37 +08002148
2149 Compiler::MethodReference mref(dex_file_, method_idx_);
2150
2151 InferredRegCategoryMap const* map =
Ian Rogers776ac1f2012-04-13 23:36:36 -07002152 verifier::MethodVerifier::GetInferredRegCategoryMap(mref);
Logan Chiendd361c92012-04-10 23:40:37 +08002153
Logan Chiena78e3c82011-12-27 17:59:35 +08002154 CHECK_NE(map, static_cast<InferredRegCategoryMap*>(NULL));
2155
2156 return map->GetRegCategory(dex_pc, reg_idx);
2157}
2158
2159
2160llvm::Value* MethodCompiler::EmitConditionResult(llvm::Value* lhs,
2161 llvm::Value* rhs,
2162 CondBranchKind cond) {
2163 switch (cond) {
2164 case kCondBranch_EQ:
2165 return irb_.CreateICmpEQ(lhs, rhs);
2166
2167 case kCondBranch_NE:
2168 return irb_.CreateICmpNE(lhs, rhs);
2169
2170 case kCondBranch_LT:
2171 return irb_.CreateICmpSLT(lhs, rhs);
2172
2173 case kCondBranch_GE:
2174 return irb_.CreateICmpSGE(lhs, rhs);
2175
2176 case kCondBranch_GT:
2177 return irb_.CreateICmpSGT(lhs, rhs);
2178
2179 case kCondBranch_LE:
2180 return irb_.CreateICmpSLE(lhs, rhs);
2181
2182 default: // Unreachable
2183 LOG(FATAL) << "Unknown conditional branch kind: " << cond;
2184 return NULL;
2185 }
Logan Chien70f94b42011-12-27 17:49:11 +08002186}
2187
TDYa12783bb6622012-04-17 02:20:34 -07002188void MethodCompiler::EmitMarkGCCard(llvm::Value* value, llvm::Value* target_addr) {
2189 // Using runtime support, let the target can override by InlineAssembly.
2190 llvm::Function* runtime_func = irb_.GetRuntime(MarkGCCard);
2191
2192 irb_.CreateCall2(runtime_func, value, target_addr);
2193}
Logan Chien70f94b42011-12-27 17:49:11 +08002194
Logan Chiene27fdbb2012-01-02 23:27:26 +08002195void
2196MethodCompiler::EmitGuard_ArrayIndexOutOfBoundsException(uint32_t dex_pc,
2197 llvm::Value* array,
2198 llvm::Value* index) {
2199 llvm::Value* array_len = EmitLoadArrayLength(array);
2200
2201 llvm::Value* cmp = irb_.CreateICmpUGE(index, array_len);
2202
2203 llvm::BasicBlock* block_exception =
2204 CreateBasicBlockWithDexPC(dex_pc, "overflow");
2205
2206 llvm::BasicBlock* block_continue =
2207 CreateBasicBlockWithDexPC(dex_pc, "cont");
2208
2209 irb_.CreateCondBr(cmp, block_exception, block_continue);
2210
2211 irb_.SetInsertPoint(block_exception);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002212
TDYa127c8dc1012012-04-19 07:03:33 -07002213 EmitUpdateDexPC(dex_pc);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002214 irb_.CreateCall2(irb_.GetRuntime(ThrowIndexOutOfBounds), index, array_len);
2215 EmitBranchExceptionLandingPad(dex_pc);
2216
2217 irb_.SetInsertPoint(block_continue);
2218}
2219
2220
2221void MethodCompiler::EmitGuard_ArrayException(uint32_t dex_pc,
2222 llvm::Value* array,
2223 llvm::Value* index) {
2224 EmitGuard_NullPointerException(dex_pc, array);
2225 EmitGuard_ArrayIndexOutOfBoundsException(dex_pc, array, index);
2226}
2227
2228
2229// Emit Array GetElementPtr
2230llvm::Value* MethodCompiler::EmitArrayGEP(llvm::Value* array_addr,
2231 llvm::Value* index_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08002232 llvm::Type* elem_type,
2233 JType elem_jty) {
2234
2235 int data_offset;
2236 if (elem_jty == kLong || elem_jty == kDouble ||
2237 (elem_jty == kObject && sizeof(uint64_t) == sizeof(Object*))) {
2238 data_offset = Array::DataOffset(sizeof(int64_t)).Int32Value();
2239 } else {
2240 data_offset = Array::DataOffset(sizeof(int32_t)).Int32Value();
2241 }
Logan Chiene27fdbb2012-01-02 23:27:26 +08002242
2243 llvm::Constant* data_offset_value =
Ian Rogers04ec04e2012-02-28 16:15:33 -08002244 irb_.getPtrEquivInt(data_offset);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002245
2246 llvm::Value* array_data_addr =
2247 irb_.CreatePtrDisp(array_addr, data_offset_value,
2248 elem_type->getPointerTo());
2249
2250 return irb_.CreateGEP(array_data_addr, index_value);
2251}
2252
2253
Logan Chien70f94b42011-12-27 17:49:11 +08002254void MethodCompiler::EmitInsn_AGet(uint32_t dex_pc,
2255 Instruction const* insn,
2256 JType elem_jty) {
Logan Chiene27fdbb2012-01-02 23:27:26 +08002257
Elliott Hughesadb8c672012-03-06 16:49:32 -08002258 DecodedInstruction dec_insn(insn);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002259
Elliott Hughesadb8c672012-03-06 16:49:32 -08002260 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
2261 llvm::Value* index_value = EmitLoadDalvikReg(dec_insn.vC, kInt, kAccurate);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002262
2263 EmitGuard_ArrayException(dex_pc, array_addr, index_value);
2264
2265 llvm::Type* elem_type = irb_.getJType(elem_jty, kArray);
2266
2267 llvm::Value* array_elem_addr =
Ian Rogers04ec04e2012-02-28 16:15:33 -08002268 EmitArrayGEP(array_addr, index_value, elem_type, elem_jty);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002269
2270 llvm::Value* array_elem_value = irb_.CreateLoad(array_elem_addr);
2271
Elliott Hughesadb8c672012-03-06 16:49:32 -08002272 EmitStoreDalvikReg(dec_insn.vA, elem_jty, kArray, array_elem_value);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002273
Logan Chien70f94b42011-12-27 17:49:11 +08002274 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2275}
2276
2277
2278void MethodCompiler::EmitInsn_APut(uint32_t dex_pc,
2279 Instruction const* insn,
2280 JType elem_jty) {
Logan Chien8dabb432012-01-02 23:29:32 +08002281
Elliott Hughesadb8c672012-03-06 16:49:32 -08002282 DecodedInstruction dec_insn(insn);
Logan Chien8dabb432012-01-02 23:29:32 +08002283
Elliott Hughesadb8c672012-03-06 16:49:32 -08002284 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
2285 llvm::Value* index_value = EmitLoadDalvikReg(dec_insn.vC, kInt, kAccurate);
Logan Chien8dabb432012-01-02 23:29:32 +08002286
2287 EmitGuard_ArrayException(dex_pc, array_addr, index_value);
2288
2289 llvm::Type* elem_type = irb_.getJType(elem_jty, kArray);
2290
2291 llvm::Value* array_elem_addr =
Ian Rogers04ec04e2012-02-28 16:15:33 -08002292 EmitArrayGEP(array_addr, index_value, elem_type, elem_jty);
Logan Chien8dabb432012-01-02 23:29:32 +08002293
Elliott Hughesadb8c672012-03-06 16:49:32 -08002294 llvm::Value* new_value = EmitLoadDalvikReg(dec_insn.vA, elem_jty, kArray);
Logan Chien8dabb432012-01-02 23:29:32 +08002295
TDYa12783bb6622012-04-17 02:20:34 -07002296 if (elem_jty == kObject) { // If put an object, check the type, and mark GC card table.
TDYa1271b86d072012-04-05 17:38:56 -07002297 llvm::Function* runtime_func = irb_.GetRuntime(CheckPutArrayElement);
2298
2299 irb_.CreateCall2(runtime_func, new_value, array_addr);
2300
2301 EmitGuard_ExceptionLandingPad(dex_pc);
TDYa12783bb6622012-04-17 02:20:34 -07002302
2303 EmitMarkGCCard(new_value, array_addr);
TDYa1271b86d072012-04-05 17:38:56 -07002304 }
2305
Logan Chien8dabb432012-01-02 23:29:32 +08002306 irb_.CreateStore(new_value, array_elem_addr);
2307
Logan Chien70f94b42011-12-27 17:49:11 +08002308 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2309}
2310
2311
2312void MethodCompiler::EmitInsn_IGet(uint32_t dex_pc,
2313 Instruction const* insn,
2314 JType field_jty) {
Logan Chien48f1d2a2012-01-02 22:49:53 +08002315
Elliott Hughesadb8c672012-03-06 16:49:32 -08002316 DecodedInstruction dec_insn(insn);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002317
Elliott Hughesadb8c672012-03-06 16:49:32 -08002318 uint32_t reg_idx = dec_insn.vB;
2319 uint32_t field_idx = dec_insn.vC;
Logan Chien48f1d2a2012-01-02 22:49:53 +08002320
Logan Chien48f1d2a2012-01-02 22:49:53 +08002321 llvm::Value* object_addr = EmitLoadDalvikReg(reg_idx, kObject, kAccurate);
2322
2323 EmitGuard_NullPointerException(dex_pc, object_addr);
2324
2325 llvm::Value* field_value;
2326
Logan Chien933abf82012-04-11 12:24:31 +08002327 int field_offset;
2328 bool is_volatile;
2329 bool is_fast_path = compiler_->ComputeInstanceFieldInfo(
2330 field_idx, oat_compilation_unit_, field_offset, is_volatile, false);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002331
Logan Chien933abf82012-04-11 12:24:31 +08002332 if (!is_fast_path) {
Logan Chien48f1d2a2012-01-02 22:49:53 +08002333 llvm::Function* runtime_func;
2334
2335 if (field_jty == kObject) {
Logan Chien3b2b2e72012-03-06 16:11:45 +08002336 runtime_func = irb_.GetRuntime(GetObjectInstance);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002337 } else if (field_jty == kLong || field_jty == kDouble) {
Logan Chien3b2b2e72012-03-06 16:11:45 +08002338 runtime_func = irb_.GetRuntime(Get64Instance);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002339 } else {
Logan Chien3b2b2e72012-03-06 16:11:45 +08002340 runtime_func = irb_.GetRuntime(Get32Instance);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002341 }
2342
2343 llvm::ConstantInt* field_idx_value = irb_.getInt32(field_idx);
2344
2345 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2346
TDYa127c8dc1012012-04-19 07:03:33 -07002347 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002348
Logan Chien3b2b2e72012-03-06 16:11:45 +08002349 field_value = irb_.CreateCall3(runtime_func, field_idx_value,
2350 method_object_addr, object_addr);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002351
2352 EmitGuard_ExceptionLandingPad(dex_pc);
2353
2354 } else {
Logan Chien933abf82012-04-11 12:24:31 +08002355 DCHECK_GE(field_offset, 0);
2356
Logan Chien48f1d2a2012-01-02 22:49:53 +08002357 llvm::PointerType* field_type =
2358 irb_.getJType(field_jty, kField)->getPointerTo();
2359
Logan Chien933abf82012-04-11 12:24:31 +08002360 llvm::ConstantInt* field_offset_value = irb_.getPtrEquivInt(field_offset);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002361
2362 llvm::Value* field_addr =
Logan Chien933abf82012-04-11 12:24:31 +08002363 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002364
Logan Chien933abf82012-04-11 12:24:31 +08002365 // TODO: Check is_volatile. We need to generate atomic load instruction
2366 // when is_volatile is true.
Logan Chien48f1d2a2012-01-02 22:49:53 +08002367 field_value = irb_.CreateLoad(field_addr);
2368 }
2369
Elliott Hughesadb8c672012-03-06 16:49:32 -08002370 EmitStoreDalvikReg(dec_insn.vA, field_jty, kField, field_value);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002371
Logan Chien70f94b42011-12-27 17:49:11 +08002372 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2373}
2374
2375
2376void MethodCompiler::EmitInsn_IPut(uint32_t dex_pc,
2377 Instruction const* insn,
2378 JType field_jty) {
Logan Chiendd6aa872012-01-03 16:06:32 +08002379
Elliott Hughesadb8c672012-03-06 16:49:32 -08002380 DecodedInstruction dec_insn(insn);
Logan Chiendd6aa872012-01-03 16:06:32 +08002381
Elliott Hughesadb8c672012-03-06 16:49:32 -08002382 uint32_t reg_idx = dec_insn.vB;
2383 uint32_t field_idx = dec_insn.vC;
Logan Chiendd6aa872012-01-03 16:06:32 +08002384
Logan Chiendd6aa872012-01-03 16:06:32 +08002385 llvm::Value* object_addr = EmitLoadDalvikReg(reg_idx, kObject, kAccurate);
2386
2387 EmitGuard_NullPointerException(dex_pc, object_addr);
2388
Elliott Hughesadb8c672012-03-06 16:49:32 -08002389 llvm::Value* new_value = EmitLoadDalvikReg(dec_insn.vA, field_jty, kField);
Logan Chiendd6aa872012-01-03 16:06:32 +08002390
Logan Chien933abf82012-04-11 12:24:31 +08002391 int field_offset;
2392 bool is_volatile;
2393 bool is_fast_path = compiler_->ComputeInstanceFieldInfo(
2394 field_idx, oat_compilation_unit_, field_offset, is_volatile, true);
Logan Chiendd6aa872012-01-03 16:06:32 +08002395
Logan Chien933abf82012-04-11 12:24:31 +08002396 if (!is_fast_path) {
Logan Chiendd6aa872012-01-03 16:06:32 +08002397 llvm::Function* runtime_func;
2398
2399 if (field_jty == kObject) {
2400 runtime_func = irb_.GetRuntime(SetObjectInstance);
2401 } else if (field_jty == kLong || field_jty == kDouble) {
2402 runtime_func = irb_.GetRuntime(Set64Instance);
2403 } else {
2404 runtime_func = irb_.GetRuntime(Set32Instance);
2405 }
2406
2407 llvm::Value* field_idx_value = irb_.getInt32(field_idx);
2408
2409 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2410
TDYa127c8dc1012012-04-19 07:03:33 -07002411 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002412
Logan Chien3b2b2e72012-03-06 16:11:45 +08002413 irb_.CreateCall4(runtime_func, field_idx_value,
2414 method_object_addr, object_addr, new_value);
Logan Chiendd6aa872012-01-03 16:06:32 +08002415
2416 EmitGuard_ExceptionLandingPad(dex_pc);
2417
2418 } else {
Logan Chien933abf82012-04-11 12:24:31 +08002419 DCHECK_GE(field_offset, 0);
2420
Logan Chiendd6aa872012-01-03 16:06:32 +08002421 llvm::PointerType* field_type =
2422 irb_.getJType(field_jty, kField)->getPointerTo();
2423
Logan Chien933abf82012-04-11 12:24:31 +08002424 llvm::Value* field_offset_value = irb_.getPtrEquivInt(field_offset);
Logan Chiendd6aa872012-01-03 16:06:32 +08002425
2426 llvm::Value* field_addr =
Logan Chien933abf82012-04-11 12:24:31 +08002427 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
Logan Chiendd6aa872012-01-03 16:06:32 +08002428
Logan Chien933abf82012-04-11 12:24:31 +08002429 // TODO: Check is_volatile. We need to generate atomic store instruction
2430 // when is_volatile is true.
Logan Chiendd6aa872012-01-03 16:06:32 +08002431 irb_.CreateStore(new_value, field_addr);
TDYa12783bb6622012-04-17 02:20:34 -07002432
2433 if (field_jty == kObject) { // If put an object, mark the GC card table.
2434 EmitMarkGCCard(new_value, object_addr);
2435 }
Logan Chiendd6aa872012-01-03 16:06:32 +08002436 }
2437
Logan Chien70f94b42011-12-27 17:49:11 +08002438 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2439}
2440
2441
Logan Chien438c4b62012-01-17 16:06:00 +08002442llvm::Value* MethodCompiler::EmitLoadStaticStorage(uint32_t dex_pc,
2443 uint32_t type_idx) {
2444 llvm::BasicBlock* block_load_static =
2445 CreateBasicBlockWithDexPC(dex_pc, "load_static");
2446
2447 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
2448
2449 // Load static storage from dex cache
2450 llvm::Value* storage_field_addr =
2451 EmitLoadDexCacheStaticStorageFieldAddr(type_idx);
2452
2453 llvm::Value* storage_object_addr = irb_.CreateLoad(storage_field_addr);
2454
2455 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
2456
2457 // Test: Is the static storage of this class initialized?
2458 llvm::Value* equal_null =
2459 irb_.CreateICmpEQ(storage_object_addr, irb_.getJNull());
2460
2461 irb_.CreateCondBr(equal_null, block_load_static, block_cont);
2462
2463 // Failback routine to load the class object
2464 irb_.SetInsertPoint(block_load_static);
2465
2466 llvm::Function* runtime_func =
2467 irb_.GetRuntime(InitializeStaticStorage);
2468
2469 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
2470
2471 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2472
TDYa127706e9b62012-04-19 12:24:26 -07002473 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
2474
TDYa127c8dc1012012-04-19 07:03:33 -07002475 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002476
Logan Chien438c4b62012-01-17 16:06:00 +08002477 llvm::Value* loaded_storage_object_addr =
TDYa127706e9b62012-04-19 12:24:26 -07002478 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
Logan Chien438c4b62012-01-17 16:06:00 +08002479
2480 EmitGuard_ExceptionLandingPad(dex_pc);
2481
2482 llvm::BasicBlock* block_after_load_static = irb_.GetInsertBlock();
2483
2484 irb_.CreateBr(block_cont);
2485
2486 // Now the class object must be loaded
2487 irb_.SetInsertPoint(block_cont);
2488
2489 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
2490
2491 phi->addIncoming(storage_object_addr, block_original);
2492 phi->addIncoming(loaded_storage_object_addr, block_after_load_static);
2493
2494 return phi;
2495}
2496
2497
Logan Chien70f94b42011-12-27 17:49:11 +08002498void MethodCompiler::EmitInsn_SGet(uint32_t dex_pc,
2499 Instruction const* insn,
2500 JType field_jty) {
Logan Chien438c4b62012-01-17 16:06:00 +08002501
Elliott Hughesadb8c672012-03-06 16:49:32 -08002502 DecodedInstruction dec_insn(insn);
Logan Chien438c4b62012-01-17 16:06:00 +08002503
Logan Chien933abf82012-04-11 12:24:31 +08002504 uint32_t field_idx = dec_insn.vB;
Logan Chien438c4b62012-01-17 16:06:00 +08002505
Logan Chien933abf82012-04-11 12:24:31 +08002506 int field_offset;
2507 int ssb_index;
2508 bool is_referrers_class;
2509 bool is_volatile;
2510
2511 bool is_fast_path = compiler_->ComputeStaticFieldInfo(
2512 field_idx, oat_compilation_unit_, field_offset, ssb_index,
2513 is_referrers_class, is_volatile, false);
Logan Chien438c4b62012-01-17 16:06:00 +08002514
2515 llvm::Value* static_field_value;
2516
Logan Chien933abf82012-04-11 12:24:31 +08002517 if (!is_fast_path) {
Logan Chien438c4b62012-01-17 16:06:00 +08002518 llvm::Function* runtime_func;
2519
2520 if (field_jty == kObject) {
2521 runtime_func = irb_.GetRuntime(GetObjectStatic);
2522 } else if (field_jty == kLong || field_jty == kDouble) {
2523 runtime_func = irb_.GetRuntime(Get64Static);
2524 } else {
2525 runtime_func = irb_.GetRuntime(Get32Static);
2526 }
2527
Elliott Hughesadb8c672012-03-06 16:49:32 -08002528 llvm::Constant* field_idx_value = irb_.getInt32(dec_insn.vB);
Logan Chien438c4b62012-01-17 16:06:00 +08002529
2530 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2531
TDYa127c8dc1012012-04-19 07:03:33 -07002532 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002533
Logan Chien438c4b62012-01-17 16:06:00 +08002534 static_field_value =
2535 irb_.CreateCall2(runtime_func, field_idx_value, method_object_addr);
2536
2537 EmitGuard_ExceptionLandingPad(dex_pc);
2538
2539 } else {
Logan Chien933abf82012-04-11 12:24:31 +08002540 DCHECK_GE(field_offset, 0);
Logan Chien438c4b62012-01-17 16:06:00 +08002541
Logan Chien933abf82012-04-11 12:24:31 +08002542 llvm::Value* static_storage_addr = NULL;
2543
2544 if (is_referrers_class) {
2545 // Fast path, static storage base is this method's class
2546 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2547
TDYa127ee1f59b2012-04-25 00:56:40 -07002548 static_storage_addr =
2549 irb_.LoadFromObjectOffset(method_object_addr,
2550 Method::DeclaringClassOffset().Int32Value(),
2551 irb_.getJObjectTy());
Logan Chien933abf82012-04-11 12:24:31 +08002552 } else {
2553 // Medium path, static storage base in a different class which
2554 // requires checks that the other class is initialized
2555 DCHECK_GE(ssb_index, 0);
2556 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
2557 }
2558
2559 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
Logan Chien438c4b62012-01-17 16:06:00 +08002560
2561 llvm::Value* static_field_addr =
2562 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
2563 irb_.getJType(field_jty, kField)->getPointerTo());
2564
Logan Chien933abf82012-04-11 12:24:31 +08002565 // TODO: Check is_volatile. We need to generate atomic load instruction
2566 // when is_volatile is true.
Logan Chien438c4b62012-01-17 16:06:00 +08002567 static_field_value = irb_.CreateLoad(static_field_addr);
2568 }
2569
Elliott Hughesadb8c672012-03-06 16:49:32 -08002570 EmitStoreDalvikReg(dec_insn.vA, field_jty, kField, static_field_value);
Logan Chien438c4b62012-01-17 16:06:00 +08002571
Logan Chien70f94b42011-12-27 17:49:11 +08002572 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2573}
2574
2575
2576void MethodCompiler::EmitInsn_SPut(uint32_t dex_pc,
2577 Instruction const* insn,
2578 JType field_jty) {
Logan Chien14179c82012-01-17 17:06:34 +08002579
Elliott Hughesadb8c672012-03-06 16:49:32 -08002580 DecodedInstruction dec_insn(insn);
Logan Chien14179c82012-01-17 17:06:34 +08002581
Logan Chien933abf82012-04-11 12:24:31 +08002582 uint32_t field_idx = dec_insn.vB;
Logan Chien14179c82012-01-17 17:06:34 +08002583
Elliott Hughesadb8c672012-03-06 16:49:32 -08002584 llvm::Value* new_value = EmitLoadDalvikReg(dec_insn.vA, field_jty, kField);
Logan Chien14179c82012-01-17 17:06:34 +08002585
Logan Chien933abf82012-04-11 12:24:31 +08002586 int field_offset;
2587 int ssb_index;
2588 bool is_referrers_class;
2589 bool is_volatile;
2590
2591 bool is_fast_path = compiler_->ComputeStaticFieldInfo(
2592 field_idx, oat_compilation_unit_, field_offset, ssb_index,
2593 is_referrers_class, is_volatile, true);
2594
2595 if (!is_fast_path) {
Logan Chien14179c82012-01-17 17:06:34 +08002596 llvm::Function* runtime_func;
2597
2598 if (field_jty == kObject) {
2599 runtime_func = irb_.GetRuntime(SetObjectStatic);
2600 } else if (field_jty == kLong || field_jty == kDouble) {
2601 runtime_func = irb_.GetRuntime(Set64Static);
2602 } else {
2603 runtime_func = irb_.GetRuntime(Set32Static);
2604 }
2605
Elliott Hughesadb8c672012-03-06 16:49:32 -08002606 llvm::Constant* field_idx_value = irb_.getInt32(dec_insn.vB);
Logan Chien14179c82012-01-17 17:06:34 +08002607
2608 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2609
TDYa127c8dc1012012-04-19 07:03:33 -07002610 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002611
Logan Chien14179c82012-01-17 17:06:34 +08002612 irb_.CreateCall3(runtime_func, field_idx_value,
2613 method_object_addr, new_value);
2614
2615 EmitGuard_ExceptionLandingPad(dex_pc);
2616
2617 } else {
Logan Chien933abf82012-04-11 12:24:31 +08002618 DCHECK_GE(field_offset, 0);
Logan Chien14179c82012-01-17 17:06:34 +08002619
Logan Chien933abf82012-04-11 12:24:31 +08002620 llvm::Value* static_storage_addr = NULL;
2621
2622 if (is_referrers_class) {
2623 // Fast path, static storage base is this method's class
2624 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2625
TDYa127ee1f59b2012-04-25 00:56:40 -07002626 static_storage_addr =
2627 irb_.LoadFromObjectOffset(method_object_addr,
2628 Method::DeclaringClassOffset().Int32Value(),
2629 irb_.getJObjectTy());
Logan Chien933abf82012-04-11 12:24:31 +08002630 } else {
2631 // Medium path, static storage base in a different class which
2632 // requires checks that the other class is initialized
2633 DCHECK_GE(ssb_index, 0);
2634 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
2635 }
2636
2637 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
Logan Chien14179c82012-01-17 17:06:34 +08002638
2639 llvm::Value* static_field_addr =
2640 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
2641 irb_.getJType(field_jty, kField)->getPointerTo());
2642
Logan Chien933abf82012-04-11 12:24:31 +08002643 // TODO: Check is_volatile. We need to generate atomic store instruction
2644 // when is_volatile is true.
Logan Chien14179c82012-01-17 17:06:34 +08002645 irb_.CreateStore(new_value, static_field_addr);
TDYa12783bb6622012-04-17 02:20:34 -07002646
2647 if (field_jty == kObject) { // If put an object, mark the GC card table.
2648 EmitMarkGCCard(new_value, static_storage_addr);
2649 }
Logan Chien14179c82012-01-17 17:06:34 +08002650 }
2651
Logan Chien70f94b42011-12-27 17:49:11 +08002652 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2653}
2654
2655
Logan Chien1a121b92012-02-15 22:23:42 +08002656void MethodCompiler::
2657EmitLoadActualParameters(std::vector<llvm::Value*>& args,
2658 uint32_t callee_method_idx,
Elliott Hughesadb8c672012-03-06 16:49:32 -08002659 DecodedInstruction const& dec_insn,
Logan Chien7e7fabc2012-04-10 18:59:11 +08002660 InvokeArgFmt arg_fmt,
Logan Chien1a121b92012-02-15 22:23:42 +08002661 bool is_static) {
2662
2663 // Get method signature
2664 DexFile::MethodId const& method_id =
2665 dex_file_->GetMethodId(callee_method_idx);
2666
Logan Chien8faf8022012-02-24 12:25:29 +08002667 uint32_t shorty_size;
Logan Chien1a121b92012-02-15 22:23:42 +08002668 char const* shorty = dex_file_->GetMethodShorty(method_id, &shorty_size);
Logan Chien8faf8022012-02-24 12:25:29 +08002669 CHECK_GE(shorty_size, 1u);
Logan Chien1a121b92012-02-15 22:23:42 +08002670
2671 // Load argument values according to the shorty (without "this")
2672 uint16_t reg_count = 0;
2673
2674 if (!is_static) {
2675 ++reg_count; // skip the "this" pointer
2676 }
2677
Logan Chien7e7fabc2012-04-10 18:59:11 +08002678 bool is_range = (arg_fmt == kArgRange);
2679
Logan Chien8faf8022012-02-24 12:25:29 +08002680 for (uint32_t i = 1; i < shorty_size; ++i) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002681 uint32_t reg_idx = (is_range) ? (dec_insn.vC + reg_count)
2682 : (dec_insn.arg[reg_count]);
Logan Chien1a121b92012-02-15 22:23:42 +08002683
2684 args.push_back(EmitLoadDalvikReg(reg_idx, shorty[i], kAccurate));
2685
2686 ++reg_count;
2687 if (shorty[i] == 'J' || shorty[i] == 'D') {
2688 // Wide types, such as long and double, are using a pair of registers
2689 // to store the value, so we have to increase arg_reg again.
2690 ++reg_count;
2691 }
2692 }
2693
Elliott Hughesadb8c672012-03-06 16:49:32 -08002694 DCHECK_EQ(reg_count, dec_insn.vA)
Logan Chien1a121b92012-02-15 22:23:42 +08002695 << "Actual argument mismatch for callee: "
2696 << PrettyMethod(callee_method_idx, *dex_file_);
2697}
2698
TDYa1270b686e52012-04-09 22:43:35 -07002699llvm::Value* MethodCompiler::EmitFixStub(llvm::Value* callee_method_object_addr,
2700 uint32_t method_idx,
2701 bool is_static) {
2702 // TODO: Remove this after we solve the link and trampoline related problems.
2703 llvm::Value* code_addr = irb_.CreateCall(irb_.GetRuntime(FixStub), callee_method_object_addr);
2704
2705 llvm::FunctionType* method_type = GetFunctionType(method_idx, is_static);
2706
2707 return irb_.CreatePointerCast(code_addr, method_type->getPointerTo());
TDYa12785321912012-04-01 15:24:56 -07002708}
Logan Chien1a121b92012-02-15 22:23:42 +08002709
Shih-wei Liao399ed3f2012-03-08 01:27:04 -08002710
Logan Chien7e7fabc2012-04-10 18:59:11 +08002711void MethodCompiler::EmitInsn_Invoke(uint32_t dex_pc,
2712 Instruction const* insn,
2713 InvokeType invoke_type,
2714 InvokeArgFmt arg_fmt) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002715 DecodedInstruction dec_insn(insn);
Logan Chien46fbb412012-02-15 22:29:08 +08002716
Logan Chien61c65dc2012-02-29 03:22:30 +08002717 bool is_static = (invoke_type == kStatic);
Logan Chien7e7fabc2012-04-10 18:59:11 +08002718 uint32_t callee_method_idx = dec_insn.vB;
Logan Chien61c65dc2012-02-29 03:22:30 +08002719
Logan Chien7e7fabc2012-04-10 18:59:11 +08002720 // Compute invoke related information for compiler decision
2721 int vtable_idx = -1;
Logan Chien92ad16d2012-03-18 05:48:55 +08002722 uintptr_t direct_code = 0; // Currently unused
Logan Chienfca64372012-04-23 14:57:01 +08002723 uintptr_t direct_method = 0;
Logan Chien61c65dc2012-02-29 03:22:30 +08002724 bool is_fast_path = compiler_->
Logan Chien7e7fabc2012-04-10 18:59:11 +08002725 ComputeInvokeInfo(callee_method_idx, oat_compilation_unit_,
Logan Chien92ad16d2012-03-18 05:48:55 +08002726 invoke_type, vtable_idx, direct_code, direct_method);
Logan Chien61c65dc2012-02-29 03:22:30 +08002727
Logan Chien7e7fabc2012-04-10 18:59:11 +08002728 // Load *this* actual parameter
Logan Chien1a121b92012-02-15 22:23:42 +08002729 llvm::Value* this_addr = NULL;
2730
2731 if (!is_static) {
2732 // Test: Is *this* parameter equal to null?
Logan Chien7e7fabc2012-04-10 18:59:11 +08002733 this_addr = (arg_fmt == kArgReg) ?
2734 EmitLoadDalvikReg(dec_insn.arg[0], kObject, kAccurate):
2735 EmitLoadDalvikReg(dec_insn.vC + 0, kObject, kAccurate);
2736
Logan Chien1a121b92012-02-15 22:23:42 +08002737 EmitGuard_NullPointerException(dex_pc, this_addr);
2738 }
2739
Logan Chien7e7fabc2012-04-10 18:59:11 +08002740 // Load the method object
TDYa1274e42a592012-04-10 20:13:54 -07002741 llvm::Value* callee_method_object_addr = NULL;
Logan Chien7e7fabc2012-04-10 18:59:11 +08002742
2743 if (!is_fast_path) {
TDYa1274e42a592012-04-10 20:13:54 -07002744 callee_method_object_addr =
Logan Chien7e7fabc2012-04-10 18:59:11 +08002745 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx, invoke_type,
2746 this_addr, dex_pc, is_fast_path);
2747 } else {
2748 switch (invoke_type) {
2749 case kStatic:
2750 case kDirect:
Logan Chienfca64372012-04-23 14:57:01 +08002751 if (direct_method != 0u &&
2752 direct_method != static_cast<uintptr_t>(-1)) {
2753 callee_method_object_addr =
TDYa12717826bf2012-04-24 01:15:10 -07002754 irb_.CreateIntToPtr(irb_.getPtrEquivInt(direct_method),
2755 irb_.getJObjectTy());
Logan Chienfca64372012-04-23 14:57:01 +08002756 } else {
2757 callee_method_object_addr =
2758 EmitLoadSDCalleeMethodObjectAddr(callee_method_idx);
2759 }
Logan Chien7e7fabc2012-04-10 18:59:11 +08002760 break;
2761
2762 case kVirtual:
2763 DCHECK(vtable_idx != -1);
TDYa1274e42a592012-04-10 20:13:54 -07002764 callee_method_object_addr =
Logan Chien7e7fabc2012-04-10 18:59:11 +08002765 EmitLoadVirtualCalleeMethodObjectAddr(vtable_idx, this_addr);
2766 break;
2767
2768 case kSuper:
2769 LOG(FATAL) << "invoke-super should be promoted to invoke-direct in "
2770 "the fast path.";
2771 break;
2772
2773 case kInterface:
TDYa1274e42a592012-04-10 20:13:54 -07002774 callee_method_object_addr =
Logan Chien7e7fabc2012-04-10 18:59:11 +08002775 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx,
2776 invoke_type, this_addr,
2777 dex_pc, is_fast_path);
2778 break;
2779 }
2780 }
2781
Logan Chien7e7fabc2012-04-10 18:59:11 +08002782 llvm::Value* code_addr =
TDYa127ee1f59b2012-04-25 00:56:40 -07002783 irb_.LoadFromObjectOffset(callee_method_object_addr,
2784 Method::GetCodeOffset().Int32Value(),
2785 GetFunctionType(callee_method_idx, is_static)->getPointerTo());
TDYa12785321912012-04-01 15:24:56 -07002786
Logan Chien1a121b92012-02-15 22:23:42 +08002787 // Load the actual parameter
2788 std::vector<llvm::Value*> args;
2789
2790 args.push_back(callee_method_object_addr); // method object for callee
2791
2792 if (!is_static) {
Logan Chien7e7fabc2012-04-10 18:59:11 +08002793 DCHECK(this_addr != NULL);
Logan Chien1a121b92012-02-15 22:23:42 +08002794 args.push_back(this_addr); // "this" object for callee
2795 }
2796
2797 EmitLoadActualParameters(args, callee_method_idx, dec_insn,
Logan Chien7e7fabc2012-04-10 18:59:11 +08002798 arg_fmt, is_static);
Logan Chien1a121b92012-02-15 22:23:42 +08002799
TDYa1275bb86012012-04-11 05:57:28 -07002800#if 0
Logan Chien8dfcbea2012-02-17 18:50:32 +08002801 // Invoke callee
TDYa127c8dc1012012-04-19 07:03:33 -07002802 EmitUpdateDexPC(dex_pc);
Logan Chien1a121b92012-02-15 22:23:42 +08002803 llvm::Value* retval = irb_.CreateCall(code_addr, args);
2804 EmitGuard_ExceptionLandingPad(dex_pc);
2805
Logan Chien7e7fabc2012-04-10 18:59:11 +08002806 uint32_t callee_access_flags = is_static ? kAccStatic : 0;
2807 UniquePtr<OatCompilationUnit> callee_oat_compilation_unit(
2808 oat_compilation_unit_->GetCallee(callee_method_idx, callee_access_flags));
2809
2810 char ret_shorty = callee_oat_compilation_unit->GetShorty()[0];
Shih-wei Liao90d50992012-02-19 03:32:05 -08002811 if (ret_shorty != 'V') {
Logan Chien1a121b92012-02-15 22:23:42 +08002812 EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval);
2813 }
TDYa1275bb86012012-04-11 05:57:28 -07002814#else
2815 uint32_t callee_access_flags = is_static ? kAccStatic : 0;
2816 UniquePtr<OatCompilationUnit> callee_oat_compilation_unit(
2817 oat_compilation_unit_->GetCallee(callee_method_idx, callee_access_flags));
2818
2819 char ret_shorty = callee_oat_compilation_unit->GetShorty()[0];
2820
2821
TDYa127c8dc1012012-04-19 07:03:33 -07002822 EmitUpdateDexPC(dex_pc);
TDYa1275bb86012012-04-11 05:57:28 -07002823
2824
2825 llvm::BasicBlock* block_normal = CreateBasicBlockWithDexPC(dex_pc, "normal");
TDYa127ce154722012-04-21 16:43:29 -07002826 llvm::BasicBlock* block_stub = CreateBasicBlockWithDexPC(dex_pc, "stub");
TDYa1275bb86012012-04-11 05:57:28 -07002827 llvm::BasicBlock* block_continue = CreateBasicBlockWithDexPC(dex_pc, "cont");
2828
2829 llvm::Type* accurate_ret_type = irb_.getJType(ret_shorty, kAccurate);
2830 llvm::Value* retval_addr = NULL;
2831 if (ret_shorty != 'V') {
2832 retval_addr = irb_.CreateAlloca(accurate_ret_type);
2833 }
2834
2835
TDYa1275bb86012012-04-11 05:57:28 -07002836 llvm::Value* code_addr_int = irb_.CreatePtrToInt(code_addr, irb_.getPtrEquivIntTy());
TDYa127ce154722012-04-21 16:43:29 -07002837 llvm::Value* max_stub_int = irb_.getPtrEquivInt(special_stub::kMaxSpecialStub);
2838 llvm::Value* is_stub = irb_.CreateICmpULT(code_addr_int, max_stub_int);
2839 irb_.CreateCondBr(is_stub, block_stub, block_normal);
TDYa1275bb86012012-04-11 05:57:28 -07002840
2841
2842 irb_.SetInsertPoint(block_normal);
2843 {
2844 // Invoke callee
TDYa127ce154722012-04-21 16:43:29 -07002845 llvm::Value* retval = irb_.CreateCall(code_addr, args);
TDYa1275bb86012012-04-11 05:57:28 -07002846 if (ret_shorty != 'V') {
TDYa127ce154722012-04-21 16:43:29 -07002847 EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval);
TDYa1275bb86012012-04-11 05:57:28 -07002848 }
2849 }
2850 irb_.CreateBr(block_continue);
2851
2852
TDYa127ce154722012-04-21 16:43:29 -07002853 irb_.SetInsertPoint(block_stub);
TDYa1275bb86012012-04-11 05:57:28 -07002854 {
TDYa127ce154722012-04-21 16:43:29 -07002855 { // lazy link
2856 // TODO: Remove this after we solve the link problem.
2857 llvm::BasicBlock* block_proxy_stub = CreateBasicBlockWithDexPC(dex_pc, "proxy");
2858 llvm::BasicBlock* block_link = CreateBasicBlockWithDexPC(dex_pc, "link");
2859
2860 irb_.CreateCondBr(irb_.CreateIsNull(code_addr), block_link, block_proxy_stub);
2861
2862
2863 irb_.SetInsertPoint(block_link);
2864 code_addr = EmitFixStub(callee_method_object_addr, callee_method_idx, is_static);
2865
2866 EmitGuard_ExceptionLandingPad(dex_pc);
2867
2868 llvm::Value* retval = irb_.CreateCall(code_addr, args);
2869 if (ret_shorty != 'V') {
2870 EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval);
2871 }
2872 irb_.CreateBr(block_continue);
2873
2874
2875 irb_.SetInsertPoint(block_proxy_stub);
TDYa1275bb86012012-04-11 05:57:28 -07002876 }
TDYa127ce154722012-04-21 16:43:29 -07002877 { // proxy stub
2878 llvm::Value* temp_space_addr;
2879 if (ret_shorty != 'V') {
2880 temp_space_addr = irb_.CreateAlloca(irb_.getJValueTy());
2881 args.push_back(temp_space_addr);
2882 }
2883 // TODO: Remove this after we solve the proxy trampoline calling convention problem.
2884 irb_.CreateCall(irb_.GetRuntime(ProxyInvokeHandler), args);
2885 if (ret_shorty != 'V') {
2886 llvm::Value* result_addr =
2887 irb_.CreateBitCast(temp_space_addr, accurate_ret_type->getPointerTo());
2888 llvm::Value* retval = irb_.CreateLoad(result_addr);
2889 EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval);
2890 }
TDYa1275bb86012012-04-11 05:57:28 -07002891 }
2892 }
2893 irb_.CreateBr(block_continue);
2894
2895
2896 irb_.SetInsertPoint(block_continue);
2897
TDYa1275bb86012012-04-11 05:57:28 -07002898 EmitGuard_ExceptionLandingPad(dex_pc);
2899#endif
Logan Chien1a121b92012-02-15 22:23:42 +08002900
Logan Chien70f94b42011-12-27 17:49:11 +08002901 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2902}
2903
2904
Logan Chien7e7fabc2012-04-10 18:59:11 +08002905llvm::Value* MethodCompiler::
2906EmitLoadSDCalleeMethodObjectAddr(uint32_t callee_method_idx) {
2907 llvm::Value* callee_method_object_field_addr =
2908 EmitLoadDexCacheResolvedMethodFieldAddr(callee_method_idx);
Logan Chien7caf37e2012-02-03 22:56:04 +08002909
Logan Chien7e7fabc2012-04-10 18:59:11 +08002910 return irb_.CreateLoad(callee_method_object_field_addr);
2911}
Logan Chien7caf37e2012-02-03 22:56:04 +08002912
Logan Chien7caf37e2012-02-03 22:56:04 +08002913
Logan Chien7e7fabc2012-04-10 18:59:11 +08002914llvm::Value* MethodCompiler::
2915EmitLoadVirtualCalleeMethodObjectAddr(int vtable_idx,
2916 llvm::Value* this_addr) {
2917 // Load class object of *this* pointer
TDYa127ee1f59b2012-04-25 00:56:40 -07002918 llvm::Value* class_object_addr =
2919 irb_.LoadFromObjectOffset(this_addr,
2920 Object::ClassOffset().Int32Value(),
2921 irb_.getJObjectTy());
Logan Chien7caf37e2012-02-03 22:56:04 +08002922
Logan Chien7e7fabc2012-04-10 18:59:11 +08002923 // Load vtable address
TDYa127ee1f59b2012-04-25 00:56:40 -07002924 llvm::Value* vtable_addr =
2925 irb_.LoadFromObjectOffset(class_object_addr,
2926 Class::VTableOffset().Int32Value(),
2927 irb_.getJObjectTy());
Logan Chien7e7fabc2012-04-10 18:59:11 +08002928
2929 // Load callee method object
2930 llvm::Value* vtable_idx_value =
2931 irb_.getPtrEquivInt(static_cast<uint64_t>(vtable_idx));
2932
2933 llvm::Value* method_field_addr =
2934 EmitArrayGEP(vtable_addr, vtable_idx_value, irb_.getJObjectTy(), kObject);
2935
2936 return irb_.CreateLoad(method_field_addr);
2937}
2938
2939
2940llvm::Value* MethodCompiler::
2941EmitCallRuntimeForCalleeMethodObjectAddr(uint32_t callee_method_idx,
2942 InvokeType invoke_type,
2943 llvm::Value* this_addr,
2944 uint32_t dex_pc,
2945 bool is_fast_path) {
2946
2947 llvm::Function* runtime_func = NULL;
2948
2949 switch (invoke_type) {
2950 case kStatic:
2951 runtime_func = irb_.GetRuntime(FindStaticMethodWithAccessCheck);
2952 break;
2953
2954 case kDirect:
2955 runtime_func = irb_.GetRuntime(FindDirectMethodWithAccessCheck);
2956 break;
2957
2958 case kVirtual:
2959 runtime_func = irb_.GetRuntime(FindVirtualMethodWithAccessCheck);
2960 break;
2961
2962 case kSuper:
2963 runtime_func = irb_.GetRuntime(FindSuperMethodWithAccessCheck);
2964 break;
2965
2966 case kInterface:
2967 if (is_fast_path) {
2968 runtime_func = irb_.GetRuntime(FindInterfaceMethod);
2969 } else {
2970 runtime_func = irb_.GetRuntime(FindInterfaceMethodWithAccessCheck);
2971 }
2972 break;
2973 }
Logan Chien7caf37e2012-02-03 22:56:04 +08002974
2975 llvm::Value* callee_method_idx_value = irb_.getInt32(callee_method_idx);
2976
Logan Chien7e7fabc2012-04-10 18:59:11 +08002977 if (this_addr == NULL) {
2978 DCHECK_EQ(invoke_type, kStatic);
2979 this_addr = irb_.getJNull();
2980 }
2981
2982 llvm::Value* caller_method_object_addr = EmitLoadMethodObjectAddr();
2983
TDYa127da83d972012-04-18 00:21:49 -07002984 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
2985
TDYa127c8dc1012012-04-19 07:03:33 -07002986 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002987
TDYa1270b686e52012-04-09 22:43:35 -07002988 llvm::Value* callee_method_object_addr =
TDYa127da83d972012-04-18 00:21:49 -07002989 irb_.CreateCall4(runtime_func,
Logan Chien7e7fabc2012-04-10 18:59:11 +08002990 callee_method_idx_value,
2991 this_addr,
TDYa127da83d972012-04-18 00:21:49 -07002992 caller_method_object_addr,
2993 thread_object_addr);
Logan Chien7caf37e2012-02-03 22:56:04 +08002994
2995 EmitGuard_ExceptionLandingPad(dex_pc);
2996
Logan Chien7e7fabc2012-04-10 18:59:11 +08002997 return callee_method_object_addr;
Logan Chien70f94b42011-12-27 17:49:11 +08002998}
2999
3000
3001void MethodCompiler::EmitInsn_Neg(uint32_t dex_pc,
3002 Instruction const* insn,
3003 JType op_jty) {
Logan Chien1b5685f2011-12-27 18:01:14 +08003004
Elliott Hughesadb8c672012-03-06 16:49:32 -08003005 DecodedInstruction dec_insn(insn);
Logan Chien1b5685f2011-12-27 18:01:14 +08003006
3007 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3008
Elliott Hughesadb8c672012-03-06 16:49:32 -08003009 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chien1b5685f2011-12-27 18:01:14 +08003010 llvm::Value* result_value = irb_.CreateNeg(src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003011 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chien1b5685f2011-12-27 18:01:14 +08003012
Logan Chien70f94b42011-12-27 17:49:11 +08003013 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3014}
3015
3016
3017void MethodCompiler::EmitInsn_Not(uint32_t dex_pc,
3018 Instruction const* insn,
3019 JType op_jty) {
Logan Chiene53750d2011-12-27 18:02:27 +08003020
Elliott Hughesadb8c672012-03-06 16:49:32 -08003021 DecodedInstruction dec_insn(insn);
Logan Chiene53750d2011-12-27 18:02:27 +08003022
3023 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3024
Elliott Hughesadb8c672012-03-06 16:49:32 -08003025 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chiene53750d2011-12-27 18:02:27 +08003026 llvm::Value* result_value =
3027 irb_.CreateXor(src_value, static_cast<uint64_t>(-1));
3028
Elliott Hughesadb8c672012-03-06 16:49:32 -08003029 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chiene53750d2011-12-27 18:02:27 +08003030
Logan Chien70f94b42011-12-27 17:49:11 +08003031 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3032}
3033
3034
3035void MethodCompiler::EmitInsn_SExt(uint32_t dex_pc,
3036 Instruction const* insn) {
Logan Chien61752ad2011-12-27 18:03:51 +08003037
Elliott Hughesadb8c672012-03-06 16:49:32 -08003038 DecodedInstruction dec_insn(insn);
Logan Chien61752ad2011-12-27 18:03:51 +08003039
Elliott Hughesadb8c672012-03-06 16:49:32 -08003040 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chien61752ad2011-12-27 18:03:51 +08003041 llvm::Value* result_value = irb_.CreateSExt(src_value, irb_.getJLongTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003042 EmitStoreDalvikReg(dec_insn.vA, kLong, kAccurate, result_value);
Logan Chien61752ad2011-12-27 18:03:51 +08003043
Logan Chien70f94b42011-12-27 17:49:11 +08003044 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3045}
3046
3047
3048void MethodCompiler::EmitInsn_Trunc(uint32_t dex_pc,
3049 Instruction const* insn) {
Logan Chien17a57662011-12-27 18:05:14 +08003050
Elliott Hughesadb8c672012-03-06 16:49:32 -08003051 DecodedInstruction dec_insn(insn);
Logan Chien17a57662011-12-27 18:05:14 +08003052
Elliott Hughesadb8c672012-03-06 16:49:32 -08003053 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kLong, kAccurate);
Logan Chien17a57662011-12-27 18:05:14 +08003054 llvm::Value* result_value = irb_.CreateTrunc(src_value, irb_.getJIntTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003055 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chien17a57662011-12-27 18:05:14 +08003056
Logan Chien70f94b42011-12-27 17:49:11 +08003057 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3058}
3059
3060
3061void MethodCompiler::EmitInsn_TruncAndSExt(uint32_t dex_pc,
3062 Instruction const* insn,
3063 unsigned N) {
Logan Chienb6744c52011-12-27 18:06:26 +08003064
Elliott Hughesadb8c672012-03-06 16:49:32 -08003065 DecodedInstruction dec_insn(insn);
Logan Chienb6744c52011-12-27 18:06:26 +08003066
Elliott Hughesadb8c672012-03-06 16:49:32 -08003067 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chienb6744c52011-12-27 18:06:26 +08003068
3069 llvm::Value* trunc_value =
3070 irb_.CreateTrunc(src_value, llvm::Type::getIntNTy(*context_, N));
3071
3072 llvm::Value* result_value = irb_.CreateSExt(trunc_value, irb_.getJIntTy());
3073
Elliott Hughesadb8c672012-03-06 16:49:32 -08003074 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chienb6744c52011-12-27 18:06:26 +08003075
Logan Chien70f94b42011-12-27 17:49:11 +08003076 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3077}
3078
3079
3080void MethodCompiler::EmitInsn_TruncAndZExt(uint32_t dex_pc,
3081 Instruction const* insn,
3082 unsigned N) {
Logan Chienb6744c52011-12-27 18:06:26 +08003083
Elliott Hughesadb8c672012-03-06 16:49:32 -08003084 DecodedInstruction dec_insn(insn);
Logan Chienb6744c52011-12-27 18:06:26 +08003085
Elliott Hughesadb8c672012-03-06 16:49:32 -08003086 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chienb6744c52011-12-27 18:06:26 +08003087
3088 llvm::Value* trunc_value =
3089 irb_.CreateTrunc(src_value, llvm::Type::getIntNTy(*context_, N));
3090
3091 llvm::Value* result_value = irb_.CreateZExt(trunc_value, irb_.getJIntTy());
3092
Elliott Hughesadb8c672012-03-06 16:49:32 -08003093 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chienb6744c52011-12-27 18:06:26 +08003094
Logan Chien70f94b42011-12-27 17:49:11 +08003095 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3096}
3097
3098
3099void MethodCompiler::EmitInsn_FNeg(uint32_t dex_pc,
3100 Instruction const* insn,
3101 JType op_jty) {
Logan Chien7a48b092011-12-27 18:07:45 +08003102
Elliott Hughesadb8c672012-03-06 16:49:32 -08003103 DecodedInstruction dec_insn(insn);
Logan Chien7a48b092011-12-27 18:07:45 +08003104
3105 DCHECK(op_jty == kFloat || op_jty == kDouble) << op_jty;
3106
Elliott Hughesadb8c672012-03-06 16:49:32 -08003107 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chien7a48b092011-12-27 18:07:45 +08003108 llvm::Value* result_value = irb_.CreateFNeg(src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003109 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chien7a48b092011-12-27 18:07:45 +08003110
Logan Chien70f94b42011-12-27 17:49:11 +08003111 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3112}
3113
3114
3115void MethodCompiler::EmitInsn_IntToFP(uint32_t dex_pc,
3116 Instruction const* insn,
3117 JType src_jty,
3118 JType dest_jty) {
Logan Chien62dd4532011-12-27 18:09:00 +08003119
Elliott Hughesadb8c672012-03-06 16:49:32 -08003120 DecodedInstruction dec_insn(insn);
Logan Chien62dd4532011-12-27 18:09:00 +08003121
3122 DCHECK(src_jty == kInt || src_jty == kLong) << src_jty;
3123 DCHECK(dest_jty == kFloat || dest_jty == kDouble) << dest_jty;
3124
Elliott Hughesadb8c672012-03-06 16:49:32 -08003125 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, src_jty, kAccurate);
Logan Chien62dd4532011-12-27 18:09:00 +08003126 llvm::Type* dest_type = irb_.getJType(dest_jty, kAccurate);
3127 llvm::Value* dest_value = irb_.CreateSIToFP(src_value, dest_type);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003128 EmitStoreDalvikReg(dec_insn.vA, dest_jty, kAccurate, dest_value);
Logan Chien62dd4532011-12-27 18:09:00 +08003129
Logan Chien70f94b42011-12-27 17:49:11 +08003130 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3131}
3132
3133
3134void MethodCompiler::EmitInsn_FPToInt(uint32_t dex_pc,
3135 Instruction const* insn,
3136 JType src_jty,
TDYa127a4746872012-04-11 23:48:55 -07003137 JType dest_jty,
3138 runtime_support::RuntimeId runtime_func_id) {
Logan Chien12dc1752011-12-27 18:10:15 +08003139
Elliott Hughesadb8c672012-03-06 16:49:32 -08003140 DecodedInstruction dec_insn(insn);
Logan Chien12dc1752011-12-27 18:10:15 +08003141
3142 DCHECK(src_jty == kFloat || src_jty == kDouble) << src_jty;
3143 DCHECK(dest_jty == kInt || dest_jty == kLong) << dest_jty;
3144
Elliott Hughesadb8c672012-03-06 16:49:32 -08003145 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, src_jty, kAccurate);
TDYa127a4746872012-04-11 23:48:55 -07003146 llvm::Value* dest_value = irb_.CreateCall(irb_.GetRuntime(runtime_func_id), src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003147 EmitStoreDalvikReg(dec_insn.vA, dest_jty, kAccurate, dest_value);
Logan Chien12dc1752011-12-27 18:10:15 +08003148
Logan Chien70f94b42011-12-27 17:49:11 +08003149 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3150}
3151
3152
3153void MethodCompiler::EmitInsn_FExt(uint32_t dex_pc,
3154 Instruction const* insn) {
Logan Chienc56ded92011-12-27 18:10:57 +08003155
Elliott Hughesadb8c672012-03-06 16:49:32 -08003156 DecodedInstruction dec_insn(insn);
Logan Chienc56ded92011-12-27 18:10:57 +08003157
Elliott Hughesadb8c672012-03-06 16:49:32 -08003158 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kFloat, kAccurate);
Logan Chienc56ded92011-12-27 18:10:57 +08003159 llvm::Value* result_value = irb_.CreateFPExt(src_value, irb_.getJDoubleTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003160 EmitStoreDalvikReg(dec_insn.vA, kDouble, kAccurate, result_value);
Logan Chienc56ded92011-12-27 18:10:57 +08003161
Logan Chien70f94b42011-12-27 17:49:11 +08003162 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3163}
3164
3165
3166void MethodCompiler::EmitInsn_FTrunc(uint32_t dex_pc,
3167 Instruction const* insn) {
Logan Chien927744f2011-12-27 18:11:52 +08003168
Elliott Hughesadb8c672012-03-06 16:49:32 -08003169 DecodedInstruction dec_insn(insn);
Logan Chien927744f2011-12-27 18:11:52 +08003170
Elliott Hughesadb8c672012-03-06 16:49:32 -08003171 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kDouble, kAccurate);
Logan Chien927744f2011-12-27 18:11:52 +08003172 llvm::Value* result_value = irb_.CreateFPTrunc(src_value, irb_.getJFloatTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003173 EmitStoreDalvikReg(dec_insn.vA, kFloat, kAccurate, result_value);
Logan Chien927744f2011-12-27 18:11:52 +08003174
Logan Chien70f94b42011-12-27 17:49:11 +08003175 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3176}
3177
3178
3179void MethodCompiler::EmitInsn_IntArithm(uint32_t dex_pc,
3180 Instruction const* insn,
3181 IntArithmKind arithm,
3182 JType op_jty,
3183 bool is_2addr) {
Logan Chienc3f7d962011-12-27 18:13:18 +08003184
Elliott Hughesadb8c672012-03-06 16:49:32 -08003185 DecodedInstruction dec_insn(insn);
Logan Chienc3f7d962011-12-27 18:13:18 +08003186
3187 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3188
3189 llvm::Value* src1_value;
3190 llvm::Value* src2_value;
3191
3192 if (is_2addr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003193 src1_value = EmitLoadDalvikReg(dec_insn.vA, op_jty, kAccurate);
3194 src2_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chienc3f7d962011-12-27 18:13:18 +08003195 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003196 src1_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
3197 src2_value = EmitLoadDalvikReg(dec_insn.vC, op_jty, kAccurate);
Logan Chienc3f7d962011-12-27 18:13:18 +08003198 }
3199
3200 llvm::Value* result_value =
3201 EmitIntArithmResultComputation(dex_pc, src1_value, src2_value,
3202 arithm, op_jty);
3203
Elliott Hughesadb8c672012-03-06 16:49:32 -08003204 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chienc3f7d962011-12-27 18:13:18 +08003205
Logan Chien70f94b42011-12-27 17:49:11 +08003206 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3207}
3208
3209
3210void MethodCompiler::EmitInsn_IntArithmImmediate(uint32_t dex_pc,
3211 Instruction const* insn,
3212 IntArithmKind arithm) {
Logan Chienc3f7d962011-12-27 18:13:18 +08003213
Elliott Hughesadb8c672012-03-06 16:49:32 -08003214 DecodedInstruction dec_insn(insn);
Logan Chienc3f7d962011-12-27 18:13:18 +08003215
Elliott Hughesadb8c672012-03-06 16:49:32 -08003216 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chienc3f7d962011-12-27 18:13:18 +08003217
Elliott Hughesadb8c672012-03-06 16:49:32 -08003218 llvm::Value* imm_value = irb_.getInt32(dec_insn.vC);
Logan Chienc3f7d962011-12-27 18:13:18 +08003219
3220 llvm::Value* result_value =
3221 EmitIntArithmResultComputation(dex_pc, src_value, imm_value, arithm, kInt);
3222
Elliott Hughesadb8c672012-03-06 16:49:32 -08003223 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chienc3f7d962011-12-27 18:13:18 +08003224
Logan Chien70f94b42011-12-27 17:49:11 +08003225 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3226}
3227
3228
Logan Chienc3f7d962011-12-27 18:13:18 +08003229llvm::Value*
3230MethodCompiler::EmitIntArithmResultComputation(uint32_t dex_pc,
3231 llvm::Value* lhs,
3232 llvm::Value* rhs,
3233 IntArithmKind arithm,
3234 JType op_jty) {
3235 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3236
3237 switch (arithm) {
3238 case kIntArithm_Add:
3239 return irb_.CreateAdd(lhs, rhs);
3240
3241 case kIntArithm_Sub:
3242 return irb_.CreateSub(lhs, rhs);
3243
3244 case kIntArithm_Mul:
3245 return irb_.CreateMul(lhs, rhs);
3246
3247 case kIntArithm_Div:
Logan Chienc3f7d962011-12-27 18:13:18 +08003248 case kIntArithm_Rem:
TDYa127f8641ce2012-04-02 06:40:40 -07003249 return EmitIntDivRemResultComputation(dex_pc, lhs, rhs, arithm, op_jty);
Logan Chienc3f7d962011-12-27 18:13:18 +08003250
3251 case kIntArithm_And:
3252 return irb_.CreateAnd(lhs, rhs);
3253
3254 case kIntArithm_Or:
3255 return irb_.CreateOr(lhs, rhs);
3256
3257 case kIntArithm_Xor:
3258 return irb_.CreateXor(lhs, rhs);
3259
Logan Chienc3f7d962011-12-27 18:13:18 +08003260 default:
3261 LOG(FATAL) << "Unknown integer arithmetic kind: " << arithm;
3262 return NULL;
3263 }
3264}
3265
3266
TDYa127f8641ce2012-04-02 06:40:40 -07003267llvm::Value*
3268MethodCompiler::EmitIntDivRemResultComputation(uint32_t dex_pc,
3269 llvm::Value* dividend,
3270 llvm::Value* divisor,
3271 IntArithmKind arithm,
3272 JType op_jty) {
3273 // Throw exception if the divisor is 0.
3274 EmitGuard_DivZeroException(dex_pc, divisor, op_jty);
3275
3276 // Check the special case: MININT / -1 = MININT
3277 // That case will cause overflow, which is undefined behavior in llvm.
3278 // So we check the divisor is -1 or not, if the divisor is -1, we do
3279 // the special path to avoid undefined behavior.
3280 llvm::Type* op_type = irb_.getJType(op_jty, kAccurate);
3281 llvm::Value* zero = irb_.getJZero(op_jty);
3282 llvm::Value* neg_one = llvm::ConstantInt::getSigned(op_type, -1);
3283 llvm::Value* result = irb_.CreateAlloca(op_type);
3284
3285 llvm::BasicBlock* eq_neg_one = CreateBasicBlockWithDexPC(dex_pc, "eq_neg_one");
3286 llvm::BasicBlock* ne_neg_one = CreateBasicBlockWithDexPC(dex_pc, "ne_neg_one");
3287 llvm::BasicBlock* neg_one_cont = CreateBasicBlockWithDexPC(dex_pc, "neg_one_cont");
3288
3289 llvm::Value* is_equal_neg_one = EmitConditionResult(divisor, neg_one, kCondBranch_EQ);
3290 irb_.CreateCondBr(is_equal_neg_one, eq_neg_one, ne_neg_one);
3291
3292 // If divisor == -1
3293 irb_.SetInsertPoint(eq_neg_one);
3294 llvm::Value* eq_result;
3295 if (arithm == kIntArithm_Div) {
3296 // We can just change from "dividend div -1" to "neg dividend".
3297 // The sub don't care the sign/unsigned because of two's complement representation.
3298 // And the behavior is what we want:
3299 // -(2^n) (2^n)-1
3300 // MININT < k <= MAXINT -> mul k -1 = -k
3301 // MININT == k -> mul k -1 = k
3302 //
3303 // LLVM use sub to represent 'neg'
3304 eq_result = irb_.CreateSub(zero, dividend);
3305 } else {
3306 // Everything modulo -1 will be 0.
3307 eq_result = zero;
3308 }
3309 irb_.CreateStore(eq_result, result);
3310 irb_.CreateBr(neg_one_cont);
3311
3312 // If divisor != -1, just do the division.
3313 irb_.SetInsertPoint(ne_neg_one);
3314 llvm::Value* ne_result;
3315 if (arithm == kIntArithm_Div) {
3316 ne_result = irb_.CreateSDiv(dividend, divisor);
3317 } else {
3318 ne_result = irb_.CreateSRem(dividend, divisor);
3319 }
3320 irb_.CreateStore(ne_result, result);
3321 irb_.CreateBr(neg_one_cont);
3322
3323 irb_.SetInsertPoint(neg_one_cont);
3324 return irb_.CreateLoad(result);
3325}
3326
3327
Logan Chien5539ad02012-04-02 14:36:55 +08003328void MethodCompiler::EmitInsn_IntShiftArithm(uint32_t dex_pc,
3329 Instruction const* insn,
3330 IntShiftArithmKind arithm,
3331 JType op_jty,
3332 bool is_2addr) {
3333
3334 DecodedInstruction dec_insn(insn);
3335
3336 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3337
3338 llvm::Value* src1_value;
3339 llvm::Value* src2_value;
3340
3341 // NOTE: The 2nd operand of the shift arithmetic instruction is
3342 // 32-bit integer regardless of the 1st operand.
3343 if (is_2addr) {
3344 src1_value = EmitLoadDalvikReg(dec_insn.vA, op_jty, kAccurate);
3345 src2_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
3346 } else {
3347 src1_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
3348 src2_value = EmitLoadDalvikReg(dec_insn.vC, kInt, kAccurate);
3349 }
3350
3351 llvm::Value* result_value =
3352 EmitIntShiftArithmResultComputation(dex_pc, src1_value, src2_value,
3353 arithm, op_jty);
3354
3355 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
3356
3357 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3358}
3359
3360
3361void MethodCompiler::
3362EmitInsn_IntShiftArithmImmediate(uint32_t dex_pc,
3363 Instruction const* insn,
3364 IntShiftArithmKind arithm) {
3365
3366 DecodedInstruction dec_insn(insn);
3367
3368 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
3369
3370 llvm::Value* imm_value = irb_.getInt32(dec_insn.vC);
3371
3372 llvm::Value* result_value =
3373 EmitIntShiftArithmResultComputation(dex_pc, src_value, imm_value,
3374 arithm, kInt);
3375
3376 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
3377
3378 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3379}
3380
3381
3382llvm::Value*
3383MethodCompiler::EmitIntShiftArithmResultComputation(uint32_t dex_pc,
3384 llvm::Value* lhs,
3385 llvm::Value* rhs,
3386 IntShiftArithmKind arithm,
3387 JType op_jty) {
3388 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3389
3390 if (op_jty == kInt) {
3391 rhs = irb_.CreateAnd(rhs, 0x1f);
3392 } else {
3393 llvm::Value* masked_rhs = irb_.CreateAnd(rhs, 0x3f);
3394 rhs = irb_.CreateZExt(masked_rhs, irb_.getJLongTy());
3395 }
3396
3397 switch (arithm) {
3398 case kIntArithm_Shl:
3399 return irb_.CreateShl(lhs, rhs);
3400
3401 case kIntArithm_Shr:
3402 return irb_.CreateAShr(lhs, rhs);
3403
3404 case kIntArithm_UShr:
3405 return irb_.CreateLShr(lhs, rhs);
3406
3407 default:
3408 LOG(FATAL) << "Unknown integer shift arithmetic kind: " << arithm;
3409 return NULL;
3410 }
3411}
3412
3413
Logan Chien70f94b42011-12-27 17:49:11 +08003414void MethodCompiler::EmitInsn_RSubImmediate(uint32_t dex_pc,
3415 Instruction const* insn) {
Logan Chien65c62d42011-12-27 18:14:18 +08003416
Elliott Hughesadb8c672012-03-06 16:49:32 -08003417 DecodedInstruction dec_insn(insn);
Logan Chien65c62d42011-12-27 18:14:18 +08003418
Elliott Hughesadb8c672012-03-06 16:49:32 -08003419 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
3420 llvm::Value* imm_value = irb_.getInt32(dec_insn.vC);
Logan Chien65c62d42011-12-27 18:14:18 +08003421 llvm::Value* result_value = irb_.CreateSub(imm_value, src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003422 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chien65c62d42011-12-27 18:14:18 +08003423
Logan Chien70f94b42011-12-27 17:49:11 +08003424 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3425}
3426
3427
3428void MethodCompiler::EmitInsn_FPArithm(uint32_t dex_pc,
3429 Instruction const* insn,
3430 FPArithmKind arithm,
3431 JType op_jty,
3432 bool is_2addr) {
Logan Chien76e1c792011-12-27 18:15:01 +08003433
Elliott Hughesadb8c672012-03-06 16:49:32 -08003434 DecodedInstruction dec_insn(insn);
Logan Chien76e1c792011-12-27 18:15:01 +08003435
3436 DCHECK(op_jty == kFloat || op_jty == kDouble) << op_jty;
3437
3438 llvm::Value* src1_value;
3439 llvm::Value* src2_value;
3440
3441 if (is_2addr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003442 src1_value = EmitLoadDalvikReg(dec_insn.vA, op_jty, kAccurate);
3443 src2_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chien76e1c792011-12-27 18:15:01 +08003444 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003445 src1_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
3446 src2_value = EmitLoadDalvikReg(dec_insn.vC, op_jty, kAccurate);
Logan Chien76e1c792011-12-27 18:15:01 +08003447 }
3448
3449 llvm::Value* result_value =
3450 EmitFPArithmResultComputation(dex_pc, src1_value, src2_value, arithm);
3451
Elliott Hughesadb8c672012-03-06 16:49:32 -08003452 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chien76e1c792011-12-27 18:15:01 +08003453
Logan Chien70f94b42011-12-27 17:49:11 +08003454 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3455}
3456
3457
Logan Chien76e1c792011-12-27 18:15:01 +08003458llvm::Value*
3459MethodCompiler::EmitFPArithmResultComputation(uint32_t dex_pc,
3460 llvm::Value *lhs,
3461 llvm::Value *rhs,
3462 FPArithmKind arithm) {
3463 switch (arithm) {
3464 case kFPArithm_Add:
3465 return irb_.CreateFAdd(lhs, rhs);
3466
3467 case kFPArithm_Sub:
3468 return irb_.CreateFSub(lhs, rhs);
3469
3470 case kFPArithm_Mul:
3471 return irb_.CreateFMul(lhs, rhs);
3472
3473 case kFPArithm_Div:
3474 return irb_.CreateFDiv(lhs, rhs);
3475
3476 case kFPArithm_Rem:
3477 return irb_.CreateFRem(lhs, rhs);
3478
3479 default:
3480 LOG(FATAL) << "Unknown floating-point arithmetic kind: " << arithm;
3481 return NULL;
3482 }
3483}
3484
3485
Logan Chienc3f7d962011-12-27 18:13:18 +08003486void MethodCompiler::EmitGuard_DivZeroException(uint32_t dex_pc,
3487 llvm::Value* denominator,
3488 JType op_jty) {
3489 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3490
3491 llvm::Constant* zero = irb_.getJZero(op_jty);
3492
3493 llvm::Value* equal_zero = irb_.CreateICmpEQ(denominator, zero);
3494
3495 llvm::BasicBlock* block_exception = CreateBasicBlockWithDexPC(dex_pc, "div0");
3496
3497 llvm::BasicBlock* block_continue = CreateBasicBlockWithDexPC(dex_pc, "cont");
3498
3499 irb_.CreateCondBr(equal_zero, block_exception, block_continue);
3500
3501 irb_.SetInsertPoint(block_exception);
TDYa127c8dc1012012-04-19 07:03:33 -07003502 EmitUpdateDexPC(dex_pc);
Logan Chienc3f7d962011-12-27 18:13:18 +08003503 irb_.CreateCall(irb_.GetRuntime(ThrowDivZeroException));
3504 EmitBranchExceptionLandingPad(dex_pc);
3505
3506 irb_.SetInsertPoint(block_continue);
3507}
3508
3509
Logan Chien61bb6142012-02-03 15:34:53 +08003510void MethodCompiler::EmitGuard_NullPointerException(uint32_t dex_pc,
3511 llvm::Value* object) {
3512 llvm::Value* equal_null = irb_.CreateICmpEQ(object, irb_.getJNull());
3513
3514 llvm::BasicBlock* block_exception =
3515 CreateBasicBlockWithDexPC(dex_pc, "nullp");
3516
3517 llvm::BasicBlock* block_continue =
3518 CreateBasicBlockWithDexPC(dex_pc, "cont");
3519
3520 irb_.CreateCondBr(equal_null, block_exception, block_continue);
3521
3522 irb_.SetInsertPoint(block_exception);
TDYa127c8dc1012012-04-19 07:03:33 -07003523 EmitUpdateDexPC(dex_pc);
TDYa1273f9137d2012-04-08 15:59:19 -07003524 irb_.CreateCall(irb_.GetRuntime(ThrowNullPointerException), irb_.getInt32(dex_pc));
Logan Chien61bb6142012-02-03 15:34:53 +08003525 EmitBranchExceptionLandingPad(dex_pc);
3526
3527 irb_.SetInsertPoint(block_continue);
3528}
3529
3530
Logan Chienbb4d12a2012-02-17 14:10:01 +08003531llvm::Value* MethodCompiler::EmitLoadDexCacheAddr(MemberOffset offset) {
3532 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
3533
TDYa127ee1f59b2012-04-25 00:56:40 -07003534 return irb_.LoadFromObjectOffset(method_object_addr,
3535 offset.Int32Value(),
3536 irb_.getJObjectTy());
Logan Chienbb4d12a2012-02-17 14:10:01 +08003537}
3538
3539
Logan Chienbb4d12a2012-02-17 14:10:01 +08003540llvm::Value* MethodCompiler::
3541EmitLoadDexCacheStaticStorageFieldAddr(uint32_t type_idx) {
3542 llvm::Value* static_storage_dex_cache_addr =
3543 EmitLoadDexCacheAddr(Method::DexCacheInitializedStaticStorageOffset());
3544
3545 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
3546
3547 return EmitArrayGEP(static_storage_dex_cache_addr, type_idx_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08003548 irb_.getJObjectTy(), kObject);
Logan Chienbb4d12a2012-02-17 14:10:01 +08003549}
3550
3551
3552llvm::Value* MethodCompiler::
3553EmitLoadDexCacheResolvedTypeFieldAddr(uint32_t type_idx) {
3554 llvm::Value* resolved_type_dex_cache_addr =
3555 EmitLoadDexCacheAddr(Method::DexCacheResolvedTypesOffset());
3556
3557 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
3558
3559 return EmitArrayGEP(resolved_type_dex_cache_addr, type_idx_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08003560 irb_.getJObjectTy(), kObject);
Logan Chienbb4d12a2012-02-17 14:10:01 +08003561}
3562
3563
3564llvm::Value* MethodCompiler::
Logan Chien61c65dc2012-02-29 03:22:30 +08003565EmitLoadDexCacheResolvedMethodFieldAddr(uint32_t method_idx) {
3566 llvm::Value* resolved_method_dex_cache_addr =
3567 EmitLoadDexCacheAddr(Method::DexCacheResolvedMethodsOffset());
3568
3569 llvm::Value* method_idx_value = irb_.getPtrEquivInt(method_idx);
3570
3571 return EmitArrayGEP(resolved_method_dex_cache_addr, method_idx_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08003572 irb_.getJObjectTy(), kObject);
Logan Chien61c65dc2012-02-29 03:22:30 +08003573}
3574
3575
3576llvm::Value* MethodCompiler::
Logan Chienbb4d12a2012-02-17 14:10:01 +08003577EmitLoadDexCacheStringFieldAddr(uint32_t string_idx) {
3578 llvm::Value* string_dex_cache_addr =
3579 EmitLoadDexCacheAddr(Method::DexCacheStringsOffset());
3580
3581 llvm::Value* string_idx_value = irb_.getPtrEquivInt(string_idx);
3582
3583 return EmitArrayGEP(string_dex_cache_addr, string_idx_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08003584 irb_.getJObjectTy(), kObject);
Logan Chienbb4d12a2012-02-17 14:10:01 +08003585}
3586
3587
Logan Chien83426162011-12-09 09:29:50 +08003588CompiledMethod *MethodCompiler::Compile() {
Logan Chien0b827102011-12-20 19:46:14 +08003589 // Code generation
3590 CreateFunction();
3591
3592 EmitPrologue();
3593 EmitInstructions();
Logan Chienc670a8d2011-12-20 21:25:56 +08003594 EmitPrologueLastBranch();
Logan Chien0b827102011-12-20 19:46:14 +08003595
Logan Chiend6c239a2011-12-23 15:11:45 +08003596 // Verify the generated bitcode
TDYa127853cd092012-04-21 22:15:31 -07003597 VERIFY_LLVM_FUNCTION(*func_);
Logan Chiend6c239a2011-12-23 15:11:45 +08003598
Logan Chien8b977d32012-02-21 19:14:55 +08003599 // Add the memory usage approximation of the compilation unit
3600 cunit_->AddMemUsageApproximation(code_item_->insns_size_in_code_units_ * 900);
3601 // NOTE: From statistic, the bitcode size is 4.5 times bigger than the
3602 // Dex file. Besides, we have to convert the code unit into bytes.
3603 // Thus, we got our magic number 9.
3604
Logan Chien110bcba2012-04-16 19:11:28 +08003605 CompiledMethod* compiled_method =
3606 new CompiledMethod(cunit_->GetInstructionSet(),
3607 cunit_->GetElfIndex(),
3608 elf_func_idx_);
3609
3610 cunit_->RegisterCompiledMethod(func_, compiled_method);
3611
3612 return compiled_method;
Logan Chien0b827102011-12-20 19:46:14 +08003613}
3614
3615
3616llvm::Value* MethodCompiler::EmitLoadMethodObjectAddr() {
3617 return func_->arg_begin();
Shih-wei Liaod1fec812012-02-13 09:51:10 -08003618}
Logan Chien83426162011-12-09 09:29:50 +08003619
3620
Logan Chien5bcc04e2012-01-30 14:15:12 +08003621void MethodCompiler::EmitBranchExceptionLandingPad(uint32_t dex_pc) {
3622 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
3623 irb_.CreateBr(lpad);
3624 } else {
3625 irb_.CreateBr(GetUnwindBasicBlock());
3626 }
3627}
3628
3629
3630void MethodCompiler::EmitGuard_ExceptionLandingPad(uint32_t dex_pc) {
3631 llvm::Value* exception_pending =
3632 irb_.CreateCall(irb_.GetRuntime(IsExceptionPending));
3633
3634 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
3635
3636 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
3637 irb_.CreateCondBr(exception_pending, lpad, block_cont);
3638 } else {
3639 irb_.CreateCondBr(exception_pending, GetUnwindBasicBlock(), block_cont);
3640 }
3641
3642 irb_.SetInsertPoint(block_cont);
3643}
3644
3645
Logan Chien924072f2012-01-30 15:07:24 +08003646void MethodCompiler::EmitGuard_GarbageCollectionSuspend(uint32_t dex_pc) {
3647 llvm::Value* runtime_func = irb_.GetRuntime(TestSuspend);
TDYa127853cd092012-04-21 22:15:31 -07003648
3649 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
3650
TDYa127c8dc1012012-04-19 07:03:33 -07003651 EmitUpdateDexPC(dex_pc);
TDYa127853cd092012-04-21 22:15:31 -07003652
3653 irb_.CreateCall(runtime_func, thread_object_addr);
Logan Chien924072f2012-01-30 15:07:24 +08003654
3655 EmitGuard_ExceptionLandingPad(dex_pc);
3656}
3657
3658
Logan Chiend6c239a2011-12-23 15:11:45 +08003659llvm::BasicBlock* MethodCompiler::
3660CreateBasicBlockWithDexPC(uint32_t dex_pc, char const* postfix) {
3661 std::string name;
3662
3663 if (postfix) {
3664 StringAppendF(&name, "B%u.%s", dex_pc, postfix);
3665 } else {
3666 StringAppendF(&name, "B%u", dex_pc);
3667 }
3668
3669 return llvm::BasicBlock::Create(*context_, name, func_);
3670}
3671
3672
3673llvm::BasicBlock* MethodCompiler::GetBasicBlock(uint32_t dex_pc) {
3674 DCHECK(dex_pc < code_item_->insns_size_in_code_units_);
3675
3676 llvm::BasicBlock* basic_block = basic_blocks_[dex_pc];
3677
3678 if (!basic_block) {
3679 basic_block = CreateBasicBlockWithDexPC(dex_pc);
3680 basic_blocks_[dex_pc] = basic_block;
3681 }
3682
3683 return basic_block;
3684}
3685
3686
3687llvm::BasicBlock*
3688MethodCompiler::GetNextBasicBlock(uint32_t dex_pc) {
3689 Instruction const* insn = Instruction::At(code_item_->insns_ + dex_pc);
3690 return GetBasicBlock(dex_pc + insn->SizeInCodeUnits());
3691}
3692
3693
Logan Chien5bcc04e2012-01-30 14:15:12 +08003694int32_t MethodCompiler::GetTryItemOffset(uint32_t dex_pc) {
3695 // TODO: Since we are emitting the dex instructions in ascending order
3696 // w.r.t. address, we can cache the lastest try item offset so that we
3697 // don't have to do binary search for every query.
3698
3699 int32_t min = 0;
3700 int32_t max = code_item_->tries_size_ - 1;
3701
3702 while (min <= max) {
3703 int32_t mid = min + (max - min) / 2;
3704
3705 DexFile::TryItem const* ti = DexFile::GetTryItems(*code_item_, mid);
3706 uint32_t start = ti->start_addr_;
3707 uint32_t end = start + ti->insn_count_;
3708
3709 if (dex_pc < start) {
3710 max = mid - 1;
3711 } else if (dex_pc >= end) {
3712 min = mid + 1;
3713 } else {
3714 return mid; // found
3715 }
3716 }
3717
3718 return -1; // not found
3719}
3720
3721
3722llvm::BasicBlock* MethodCompiler::GetLandingPadBasicBlock(uint32_t dex_pc) {
3723 // Find the try item for this address in this method
3724 int32_t ti_offset = GetTryItemOffset(dex_pc);
3725
3726 if (ti_offset == -1) {
3727 return NULL; // No landing pad is available for this address.
3728 }
3729
3730 // Check for the existing landing pad basic block
3731 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
3732 llvm::BasicBlock* block_lpad = basic_block_landing_pads_[ti_offset];
3733
3734 if (block_lpad) {
3735 // We have generated landing pad for this try item already. Return the
3736 // same basic block.
3737 return block_lpad;
3738 }
3739
3740 // Get try item from code item
3741 DexFile::TryItem const* ti = DexFile::GetTryItems(*code_item_, ti_offset);
3742
3743 // Create landing pad basic block
3744 block_lpad = llvm::BasicBlock::Create(*context_,
3745 StringPrintf("lpad%d", ti_offset),
3746 func_);
3747
3748 // Change IRBuilder insert point
3749 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3750 irb_.SetInsertPoint(block_lpad);
3751
3752 // Find catch block with matching type
3753 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
3754
Logan Chien736df022012-04-27 16:25:57 +08003755 llvm::Value* ti_offset_value = irb_.getInt32(ti_offset);
Logan Chien5bcc04e2012-01-30 14:15:12 +08003756
3757 llvm::Value* catch_handler_index_value =
3758 irb_.CreateCall2(irb_.GetRuntime(FindCatchBlock),
Logan Chien736df022012-04-27 16:25:57 +08003759 method_object_addr, ti_offset_value);
Logan Chien5bcc04e2012-01-30 14:15:12 +08003760
3761 // Switch instruction (Go to unwind basic block by default)
3762 llvm::SwitchInst* sw =
3763 irb_.CreateSwitch(catch_handler_index_value, GetUnwindBasicBlock());
3764
3765 // Cases with matched catch block
3766 CatchHandlerIterator iter(*code_item_, ti->start_addr_);
3767
3768 for (uint32_t c = 0; iter.HasNext(); iter.Next(), ++c) {
3769 sw->addCase(irb_.getInt32(c), GetBasicBlock(iter.GetHandlerAddress()));
3770 }
3771
3772 // Restore the orignal insert point for IRBuilder
3773 irb_.restoreIP(irb_ip_original);
3774
3775 // Cache this landing pad
3776 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
3777 basic_block_landing_pads_[ti_offset] = block_lpad;
3778
3779 return block_lpad;
3780}
3781
3782
3783llvm::BasicBlock* MethodCompiler::GetUnwindBasicBlock() {
3784 // Check the existing unwinding baisc block block
3785 if (basic_block_unwind_ != NULL) {
3786 return basic_block_unwind_;
3787 }
3788
3789 // Create new basic block for unwinding
3790 basic_block_unwind_ =
3791 llvm::BasicBlock::Create(*context_, "exception_unwind", func_);
3792
3793 // Change IRBuilder insert point
3794 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3795 irb_.SetInsertPoint(basic_block_unwind_);
3796
Logan Chien8dfcbea2012-02-17 18:50:32 +08003797 // Pop the shadow frame
3798 EmitPopShadowFrame();
3799
Logan Chien5bcc04e2012-01-30 14:15:12 +08003800 // Emit the code to return default value (zero) for the given return type.
Logan Chiendd361c92012-04-10 23:40:37 +08003801 char ret_shorty = oat_compilation_unit_->GetShorty()[0];
Logan Chien5bcc04e2012-01-30 14:15:12 +08003802 if (ret_shorty == 'V') {
3803 irb_.CreateRetVoid();
3804 } else {
3805 irb_.CreateRet(irb_.getJZero(ret_shorty));
3806 }
3807
3808 // Restore the orignal insert point for IRBuilder
3809 irb_.restoreIP(irb_ip_original);
3810
3811 return basic_block_unwind_;
3812}
3813
3814
Logan Chienc670a8d2011-12-20 21:25:56 +08003815llvm::Value* MethodCompiler::AllocDalvikLocalVarReg(RegCategory cat,
3816 uint32_t reg_idx) {
3817
3818 // Save current IR builder insert point
3819 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3820
3821 // Alloca
3822 llvm::Value* reg_addr = NULL;
3823
3824 switch (cat) {
3825 case kRegCat1nr:
3826 irb_.SetInsertPoint(basic_block_reg_alloca_);
3827 reg_addr = irb_.CreateAlloca(irb_.getJIntTy(), 0,
3828 StringPrintf("r%u", reg_idx));
Logan Chienc670a8d2011-12-20 21:25:56 +08003829 break;
3830
3831 case kRegCat2:
3832 irb_.SetInsertPoint(basic_block_reg_alloca_);
3833 reg_addr = irb_.CreateAlloca(irb_.getJLongTy(), 0,
3834 StringPrintf("w%u", reg_idx));
Logan Chienc670a8d2011-12-20 21:25:56 +08003835 break;
3836
3837 case kRegObject:
Logan Chien8dfcbea2012-02-17 18:50:32 +08003838 {
3839 irb_.SetInsertPoint(basic_block_shadow_frame_alloca_);
Logan Chienc670a8d2011-12-20 21:25:56 +08003840
Logan Chien8dfcbea2012-02-17 18:50:32 +08003841 llvm::Value* gep_index[] = {
3842 irb_.getInt32(0), // No pointer displacement
3843 irb_.getInt32(1), // SIRT
3844 irb_.getInt32(reg_idx) // Pointer field
3845 };
3846
3847 reg_addr = irb_.CreateGEP(shadow_frame_, gep_index,
3848 StringPrintf("p%u", reg_idx));
Logan Chien8dfcbea2012-02-17 18:50:32 +08003849 }
Logan Chienc670a8d2011-12-20 21:25:56 +08003850 break;
3851
3852 default:
3853 LOG(FATAL) << "Unknown register category for allocation: " << cat;
3854 }
3855
3856 // Restore IRBuilder insert point
3857 irb_.restoreIP(irb_ip_original);
3858
3859 DCHECK_NE(reg_addr, static_cast<llvm::Value*>(NULL));
3860 return reg_addr;
3861}
3862
3863
3864llvm::Value* MethodCompiler::AllocDalvikRetValReg(RegCategory cat) {
3865 // Save current IR builder insert point
3866 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3867
3868 // Alloca
3869 llvm::Value* reg_addr = NULL;
3870
3871 switch (cat) {
3872 case kRegCat1nr:
3873 irb_.SetInsertPoint(basic_block_reg_alloca_);
3874 reg_addr = irb_.CreateAlloca(irb_.getJIntTy(), 0, "r_res");
3875 break;
3876
3877 case kRegCat2:
3878 irb_.SetInsertPoint(basic_block_reg_alloca_);
3879 reg_addr = irb_.CreateAlloca(irb_.getJLongTy(), 0, "w_res");
3880 break;
3881
3882 case kRegObject:
3883 irb_.SetInsertPoint(basic_block_reg_alloca_);
3884 reg_addr = irb_.CreateAlloca(irb_.getJObjectTy(), 0, "p_res");
3885 break;
3886
3887 default:
3888 LOG(FATAL) << "Unknown register category for allocation: " << cat;
3889 }
3890
3891 // Restore IRBuilder insert point
3892 irb_.restoreIP(irb_ip_original);
3893
3894 DCHECK_NE(reg_addr, static_cast<llvm::Value*>(NULL));
3895 return reg_addr;
3896}
3897
3898
Logan Chien8dfcbea2012-02-17 18:50:32 +08003899void MethodCompiler::EmitPopShadowFrame() {
3900 irb_.CreateCall(irb_.GetRuntime(PopShadowFrame));
3901}
3902
3903
TDYa127c8dc1012012-04-19 07:03:33 -07003904void MethodCompiler::EmitUpdateDexPC(uint32_t dex_pc) {
3905 irb_.StoreToObjectOffset(shadow_frame_,
3906 ShadowFrame::DexPCOffset(),
3907 irb_.getInt32(dex_pc));
Logan Chien8dfcbea2012-02-17 18:50:32 +08003908}
3909
3910
Logan Chien83426162011-12-09 09:29:50 +08003911} // namespace compiler_llvm
3912} // namespace art