blob: 153d99a93b88ac242e15be3b220f5d8fa1d7dd43 [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
38#include <llvm/Analysis/Verifier.h>
Logan Chienc670a8d2011-12-20 21:25:56 +080039#include <llvm/BasicBlock.h>
Shih-wei Liaod1fec812012-02-13 09:51:10 -080040#include <llvm/Function.h>
Logan Chiena85fb2f2012-01-16 12:52:56 +080041#include <llvm/GlobalVariable.h>
42#include <llvm/Intrinsics.h>
Shih-wei Liaod1fec812012-02-13 09:51:10 -080043
Logan Chien83426162011-12-09 09:29:50 +080044namespace art {
45namespace compiler_llvm {
Shih-wei Liaod1fec812012-02-13 09:51:10 -080046
Logan Chien42e0e152012-01-13 15:42:36 +080047using namespace runtime_support;
48
Shih-wei Liaod1fec812012-02-13 09:51:10 -080049
Logan Chien8b977d32012-02-21 19:14:55 +080050MethodCompiler::MethodCompiler(CompilationUnit* cunit,
Logan Chien83426162011-12-09 09:29:50 +080051 Compiler* compiler,
Logan Chien4dd96f52012-02-29 01:26:58 +080052 OatCompilationUnit* oat_compilation_unit)
Logan Chien8b977d32012-02-21 19:14:55 +080053 : cunit_(cunit), compiler_(compiler),
54 class_linker_(oat_compilation_unit->class_linker_),
55 class_loader_(oat_compilation_unit->class_loader_),
56 dex_file_(oat_compilation_unit->dex_file_),
57 dex_cache_(oat_compilation_unit->dex_cache_),
58 code_item_(oat_compilation_unit->code_item_),
59 oat_compilation_unit_(oat_compilation_unit),
Logan Chien8b977d32012-02-21 19:14:55 +080060 method_idx_(oat_compilation_unit->method_idx_),
61 access_flags_(oat_compilation_unit->access_flags_),
62 module_(cunit->GetModule()),
63 context_(cunit->GetLLVMContext()),
64 irb_(*cunit->GetIRBuilder()), func_(NULL), retval_reg_(NULL),
Ian Rogers776ac1f2012-04-13 23:36:36 -070065 basic_block_stack_overflow_(NULL),
Logan Chien8b977d32012-02-21 19:14:55 +080066 basic_block_reg_alloca_(NULL), basic_block_shadow_frame_alloca_(NULL),
67 basic_block_reg_zero_init_(NULL), basic_block_reg_arg_init_(NULL),
68 basic_blocks_(code_item_->insns_size_in_code_units_),
69 basic_block_landing_pads_(code_item_->tries_size_, NULL),
70 basic_block_unwind_(NULL), basic_block_unreachable_(NULL),
Logan Chien937105a2012-04-02 02:37:37 +080071 shadow_frame_(NULL), elf_func_idx_(cunit_->AcquireUniqueElfFuncIndex()) {
Shih-wei Liaod1fec812012-02-13 09:51:10 -080072}
73
74
75MethodCompiler::~MethodCompiler() {
Logan Chienc670a8d2011-12-20 21:25:56 +080076 STLDeleteElements(&regs_);
Shih-wei Liaod1fec812012-02-13 09:51:10 -080077}
78
79
Logan Chien0b827102011-12-20 19:46:14 +080080void MethodCompiler::CreateFunction() {
81 // LLVM function name
Logan Chien937105a2012-04-02 02:37:37 +080082 std::string func_name(ElfFuncName(elf_func_idx_));
Logan Chien0b827102011-12-20 19:46:14 +080083
84 // Get function type
85 llvm::FunctionType* func_type =
Logan Chiendd361c92012-04-10 23:40:37 +080086 GetFunctionType(method_idx_, oat_compilation_unit_->IsStatic());
Logan Chien0b827102011-12-20 19:46:14 +080087
88 // Create function
89 func_ = llvm::Function::Create(func_type, llvm::Function::ExternalLinkage,
90 func_name, module_);
91
92 // Set argument name
93 llvm::Function::arg_iterator arg_iter(func_->arg_begin());
94 llvm::Function::arg_iterator arg_end(func_->arg_end());
95
96 DCHECK_NE(arg_iter, arg_end);
97 arg_iter->setName("method");
98 ++arg_iter;
99
Logan Chiendd361c92012-04-10 23:40:37 +0800100 if (!oat_compilation_unit_->IsStatic()) {
Logan Chien0b827102011-12-20 19:46:14 +0800101 DCHECK_NE(arg_iter, arg_end);
102 arg_iter->setName("this");
103 ++arg_iter;
104 }
105
106 for (unsigned i = 0; arg_iter != arg_end; ++i, ++arg_iter) {
107 arg_iter->setName(StringPrintf("a%u", i));
108 }
109}
110
111
112llvm::FunctionType* MethodCompiler::GetFunctionType(uint32_t method_idx,
113 bool is_static) {
114 // Get method signature
115 DexFile::MethodId const& method_id = dex_file_->GetMethodId(method_idx);
116
Logan Chien8faf8022012-02-24 12:25:29 +0800117 uint32_t shorty_size;
Logan Chien0b827102011-12-20 19:46:14 +0800118 char const* shorty = dex_file_->GetMethodShorty(method_id, &shorty_size);
Logan Chien8faf8022012-02-24 12:25:29 +0800119 CHECK_GE(shorty_size, 1u);
Logan Chien0b827102011-12-20 19:46:14 +0800120
121 // Get return type
122 llvm::Type* ret_type = irb_.getJType(shorty[0], kAccurate);
123
124 // Get argument type
125 std::vector<llvm::Type*> args_type;
126
127 args_type.push_back(irb_.getJObjectTy()); // method object pointer
128
129 if (!is_static) {
130 args_type.push_back(irb_.getJType('L', kAccurate)); // "this" object pointer
131 }
132
Logan Chien8faf8022012-02-24 12:25:29 +0800133 for (uint32_t i = 1; i < shorty_size; ++i) {
Logan Chien0b827102011-12-20 19:46:14 +0800134 args_type.push_back(irb_.getJType(shorty[i], kAccurate));
135 }
136
137 return llvm::FunctionType::get(ret_type, args_type, false);
138}
139
140
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800141void MethodCompiler::EmitPrologue() {
Logan Chienc670a8d2011-12-20 21:25:56 +0800142 // Create basic blocks for prologue
TDYa1274165a832012-04-03 17:47:16 -0700143 basic_block_stack_overflow_ =
144 llvm::BasicBlock::Create(*context_, "prologue.stack_overflow_check", func_);
145
Logan Chienc670a8d2011-12-20 21:25:56 +0800146 basic_block_reg_alloca_ =
147 llvm::BasicBlock::Create(*context_, "prologue.alloca", func_);
148
Logan Chien8dfcbea2012-02-17 18:50:32 +0800149 basic_block_shadow_frame_alloca_ =
150 llvm::BasicBlock::Create(*context_, "prologue.shadowframe", func_);
151
Logan Chienc670a8d2011-12-20 21:25:56 +0800152 basic_block_reg_zero_init_ =
153 llvm::BasicBlock::Create(*context_, "prologue.zeroinit", func_);
154
Logan Chiend6ececa2011-12-27 16:20:15 +0800155 basic_block_reg_arg_init_ =
156 llvm::BasicBlock::Create(*context_, "prologue.arginit", func_);
157
TDYa1274165a832012-04-03 17:47:16 -0700158 // Before alloca, check stack overflow.
159 EmitStackOverflowCheck();
160
Logan Chienc670a8d2011-12-20 21:25:56 +0800161 // Create register array
162 for (uint16_t r = 0; r < code_item_->registers_size_; ++r) {
163 regs_.push_back(DalvikReg::CreateLocalVarReg(*this, r));
164 }
165
166 retval_reg_.reset(DalvikReg::CreateRetValReg(*this));
Logan Chiend6ececa2011-12-27 16:20:15 +0800167
Logan Chien8dfcbea2012-02-17 18:50:32 +0800168 // Create Shadow Frame
169 EmitPrologueAllocShadowFrame();
170
Logan Chiend6ececa2011-12-27 16:20:15 +0800171 // Store argument to dalvik register
172 irb_.SetInsertPoint(basic_block_reg_arg_init_);
173 EmitPrologueAssignArgRegister();
174
175 // Branch to start address
176 irb_.CreateBr(GetBasicBlock(0));
Logan Chienc670a8d2011-12-20 21:25:56 +0800177}
178
179
TDYa1274165a832012-04-03 17:47:16 -0700180void MethodCompiler::EmitStackOverflowCheck() {
181 irb_.SetInsertPoint(basic_block_stack_overflow_);
182
183 // Call llvm intrinsic function to get frame address.
184 llvm::Function* frameaddress =
185 llvm::Intrinsic::getDeclaration(module_, llvm::Intrinsic::frameaddress);
186
187 // The type of llvm::frameaddress is: i8* @llvm.frameaddress(i32)
188 llvm::Value* frame_address = irb_.CreateCall(frameaddress, irb_.getInt32(0));
189
190 // Cast i8* to int
191 frame_address = irb_.CreatePtrToInt(frame_address, irb_.getPtrEquivIntTy());
192
193 // Get thread.stack_end_
194 llvm::Value* thread_object_addr =
195 irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
196
197 llvm::Value* stack_end_addr =
198 irb_.CreatePtrDisp(thread_object_addr,
199 irb_.getPtrEquivInt(Thread::StackEndOffset().Int32Value()),
200 irb_.getPtrEquivIntTy()->getPointerTo());
201
202 llvm::Value* stack_end = irb_.CreateLoad(stack_end_addr);
203
204 // Check the frame address < thread.stack_end_ ?
205 llvm::Value* is_stack_overflow = irb_.CreateICmpULT(frame_address, stack_end);
206
207 llvm::BasicBlock* block_exception =
208 llvm::BasicBlock::Create(*context_, "stack_overflow", func_);
209
210 llvm::BasicBlock* block_continue =
211 llvm::BasicBlock::Create(*context_, "stack_overflow_cont", func_);
212
213 irb_.CreateCondBr(is_stack_overflow, block_exception, block_continue);
214
215 // If stack overflow, throw exception.
216 irb_.SetInsertPoint(block_exception);
217 irb_.CreateCall(irb_.GetRuntime(ThrowStackOverflowException));
218
219 // Unwind.
Logan Chiendd361c92012-04-10 23:40:37 +0800220 char ret_shorty = oat_compilation_unit_->GetShorty()[0];
TDYa1274165a832012-04-03 17:47:16 -0700221 if (ret_shorty == 'V') {
222 irb_.CreateRetVoid();
223 } else {
224 irb_.CreateRet(irb_.getJZero(ret_shorty));
225 }
226
227 basic_block_stack_overflow_ = block_continue;
228}
229
230
Logan Chienc670a8d2011-12-20 21:25:56 +0800231void MethodCompiler::EmitPrologueLastBranch() {
TDYa1274165a832012-04-03 17:47:16 -0700232 irb_.SetInsertPoint(basic_block_stack_overflow_);
233 irb_.CreateBr(basic_block_reg_alloca_);
234
Logan Chienc670a8d2011-12-20 21:25:56 +0800235 irb_.SetInsertPoint(basic_block_reg_alloca_);
Logan Chien8dfcbea2012-02-17 18:50:32 +0800236 irb_.CreateBr(basic_block_shadow_frame_alloca_);
237
238 irb_.SetInsertPoint(basic_block_shadow_frame_alloca_);
Logan Chienc670a8d2011-12-20 21:25:56 +0800239 irb_.CreateBr(basic_block_reg_zero_init_);
240
241 irb_.SetInsertPoint(basic_block_reg_zero_init_);
Logan Chiend6ececa2011-12-27 16:20:15 +0800242 irb_.CreateBr(basic_block_reg_arg_init_);
243}
244
245
Logan Chien8dfcbea2012-02-17 18:50:32 +0800246void MethodCompiler::EmitPrologueAllocShadowFrame() {
247 irb_.SetInsertPoint(basic_block_shadow_frame_alloca_);
248
249 // Allocate the shadow frame now!
250 uint32_t sirt_size = code_item_->registers_size_;
251 // TODO: registers_size_ is a bad approximation. Compute a
252 // tighter approximation at Dex verifier while performing data-flow
253 // analysis.
254
255 llvm::StructType* shadow_frame_type = irb_.getShadowFrameTy(sirt_size);
256 shadow_frame_ = irb_.CreateAlloca(shadow_frame_type);
257
258 // Zero-initialization of the shadow frame
259 llvm::ConstantAggregateZero* zero_initializer =
260 llvm::ConstantAggregateZero::get(shadow_frame_type);
261
262 irb_.CreateStore(zero_initializer, shadow_frame_);
263
Logan Chien8dfcbea2012-02-17 18:50:32 +0800264 // Store the method pointer
Logan Chien1b0a1b72012-03-15 06:20:17 +0800265 llvm::Value* method_field_addr =
266 irb_.CreatePtrDisp(shadow_frame_,
267 irb_.getPtrEquivInt(ShadowFrame::MethodOffset()),
268 irb_.getJObjectTy()->getPointerTo());
269
Logan Chien8dfcbea2012-02-17 18:50:32 +0800270 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
271 irb_.CreateStore(method_object_addr, method_field_addr);
272
273 // Store the number of the pointer slots
Logan Chien1b0a1b72012-03-15 06:20:17 +0800274 llvm::ConstantInt* num_of_refs_offset =
275 irb_.getPtrEquivInt(ShadowFrame::NumberOfReferencesOffset());
276
277 llvm::Value* num_of_refs_field_addr =
278 irb_.CreatePtrDisp(shadow_frame_, num_of_refs_offset,
279 irb_.getJIntTy()->getPointerTo());
280
281 llvm::ConstantInt* num_of_refs_value = irb_.getJInt(sirt_size);
282 irb_.CreateStore(num_of_refs_value, num_of_refs_field_addr);
Logan Chien8dfcbea2012-02-17 18:50:32 +0800283
284 // Push the shadow frame
285 llvm::Value* shadow_frame_upcast =
286 irb_.CreateConstGEP2_32(shadow_frame_, 0, 0);
287
288 irb_.CreateCall(irb_.GetRuntime(PushShadowFrame), shadow_frame_upcast);
289}
290
291
Logan Chiend6ececa2011-12-27 16:20:15 +0800292void MethodCompiler::EmitPrologueAssignArgRegister() {
293 uint16_t arg_reg = code_item_->registers_size_ - code_item_->ins_size_;
294
295 llvm::Function::arg_iterator arg_iter(func_->arg_begin());
296 llvm::Function::arg_iterator arg_end(func_->arg_end());
297
Logan Chiendd361c92012-04-10 23:40:37 +0800298 uint32_t shorty_size = 0;
299 char const* shorty = oat_compilation_unit_->GetShorty(&shorty_size);
300 CHECK_GE(shorty_size, 1u);
Logan Chiend6ececa2011-12-27 16:20:15 +0800301
302 ++arg_iter; // skip method object
303
Logan Chiendd361c92012-04-10 23:40:37 +0800304 if (!oat_compilation_unit_->IsStatic()) {
Logan Chiend6ececa2011-12-27 16:20:15 +0800305 EmitStoreDalvikReg(arg_reg, kObject, kAccurate, arg_iter);
306 ++arg_iter;
307 ++arg_reg;
308 }
309
Logan Chiendd361c92012-04-10 23:40:37 +0800310 for (uint32_t i = 1; i < shorty_size; ++i, ++arg_iter) {
Logan Chiend6ececa2011-12-27 16:20:15 +0800311 EmitStoreDalvikReg(arg_reg, shorty[i], kAccurate, arg_iter);
312
313 ++arg_reg;
314 if (shorty[i] == 'J' || shorty[i] == 'D') {
315 // Wide types, such as long and double, are using a pair of registers
316 // to store the value, so we have to increase arg_reg again.
317 ++arg_reg;
318 }
319 }
320
321 DCHECK_EQ(arg_end, arg_iter);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800322}
323
324
Logan Chien83426162011-12-09 09:29:50 +0800325void MethodCompiler::EmitInstructions() {
Logan Chiend6c239a2011-12-23 15:11:45 +0800326 uint32_t dex_pc = 0;
327 while (dex_pc < code_item_->insns_size_in_code_units_) {
328 Instruction const* insn = Instruction::At(code_item_->insns_ + dex_pc);
329 EmitInstruction(dex_pc, insn);
330 dex_pc += insn->SizeInCodeUnits();
331 }
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800332}
333
334
Logan Chien83426162011-12-09 09:29:50 +0800335void MethodCompiler::EmitInstruction(uint32_t dex_pc,
336 Instruction const* insn) {
Logan Chiend6c239a2011-12-23 15:11:45 +0800337
338 // Set the IRBuilder insertion point
339 irb_.SetInsertPoint(GetBasicBlock(dex_pc));
340
Logan Chien70f94b42011-12-27 17:49:11 +0800341#define ARGS dex_pc, insn
342
343 // Dispatch the instruction
344 switch (insn->Opcode()) {
345 case Instruction::NOP:
346 EmitInsn_Nop(ARGS);
347 break;
348
349 case Instruction::MOVE:
350 case Instruction::MOVE_FROM16:
351 case Instruction::MOVE_16:
352 EmitInsn_Move(ARGS, kInt);
353 break;
354
355 case Instruction::MOVE_WIDE:
356 case Instruction::MOVE_WIDE_FROM16:
357 case Instruction::MOVE_WIDE_16:
358 EmitInsn_Move(ARGS, kLong);
359 break;
360
361 case Instruction::MOVE_OBJECT:
362 case Instruction::MOVE_OBJECT_FROM16:
363 case Instruction::MOVE_OBJECT_16:
364 EmitInsn_Move(ARGS, kObject);
365 break;
366
367 case Instruction::MOVE_RESULT:
368 EmitInsn_MoveResult(ARGS, kInt);
369 break;
370
371 case Instruction::MOVE_RESULT_WIDE:
372 EmitInsn_MoveResult(ARGS, kLong);
373 break;
374
375 case Instruction::MOVE_RESULT_OBJECT:
376 EmitInsn_MoveResult(ARGS, kObject);
377 break;
378
379 case Instruction::MOVE_EXCEPTION:
380 EmitInsn_MoveException(ARGS);
381 break;
382
383 case Instruction::RETURN_VOID:
384 EmitInsn_ReturnVoid(ARGS);
385 break;
386
387 case Instruction::RETURN:
388 case Instruction::RETURN_WIDE:
389 case Instruction::RETURN_OBJECT:
390 EmitInsn_Return(ARGS);
391 break;
392
393 case Instruction::CONST_4:
394 case Instruction::CONST_16:
395 case Instruction::CONST:
396 case Instruction::CONST_HIGH16:
397 EmitInsn_LoadConstant(ARGS, kInt);
398 break;
399
400 case Instruction::CONST_WIDE_16:
401 case Instruction::CONST_WIDE_32:
402 case Instruction::CONST_WIDE:
403 case Instruction::CONST_WIDE_HIGH16:
404 EmitInsn_LoadConstant(ARGS, kLong);
405 break;
406
407 case Instruction::CONST_STRING:
408 case Instruction::CONST_STRING_JUMBO:
409 EmitInsn_LoadConstantString(ARGS);
410 break;
411
412 case Instruction::CONST_CLASS:
413 EmitInsn_LoadConstantClass(ARGS);
414 break;
415
416 case Instruction::MONITOR_ENTER:
417 EmitInsn_MonitorEnter(ARGS);
418 break;
419
420 case Instruction::MONITOR_EXIT:
421 EmitInsn_MonitorExit(ARGS);
422 break;
423
424 case Instruction::CHECK_CAST:
425 EmitInsn_CheckCast(ARGS);
426 break;
427
428 case Instruction::INSTANCE_OF:
429 EmitInsn_InstanceOf(ARGS);
430 break;
431
432 case Instruction::ARRAY_LENGTH:
433 EmitInsn_ArrayLength(ARGS);
434 break;
435
436 case Instruction::NEW_INSTANCE:
437 EmitInsn_NewInstance(ARGS);
438 break;
439
440 case Instruction::NEW_ARRAY:
441 EmitInsn_NewArray(ARGS);
442 break;
443
444 case Instruction::FILLED_NEW_ARRAY:
445 EmitInsn_FilledNewArray(ARGS, false);
446 break;
447
448 case Instruction::FILLED_NEW_ARRAY_RANGE:
449 EmitInsn_FilledNewArray(ARGS, true);
450 break;
451
452 case Instruction::FILL_ARRAY_DATA:
453 EmitInsn_FillArrayData(ARGS);
454 break;
455
456 case Instruction::THROW:
457 EmitInsn_ThrowException(ARGS);
458 break;
459
460 case Instruction::GOTO:
461 case Instruction::GOTO_16:
462 case Instruction::GOTO_32:
463 EmitInsn_UnconditionalBranch(ARGS);
464 break;
465
466 case Instruction::PACKED_SWITCH:
467 EmitInsn_PackedSwitch(ARGS);
468 break;
469
470 case Instruction::SPARSE_SWITCH:
471 EmitInsn_SparseSwitch(ARGS);
472 break;
473
474 case Instruction::CMPL_FLOAT:
475 EmitInsn_FPCompare(ARGS, kFloat, false);
476 break;
477
478 case Instruction::CMPG_FLOAT:
479 EmitInsn_FPCompare(ARGS, kFloat, true);
480 break;
481
482 case Instruction::CMPL_DOUBLE:
483 EmitInsn_FPCompare(ARGS, kDouble, false);
484 break;
485
486 case Instruction::CMPG_DOUBLE:
487 EmitInsn_FPCompare(ARGS, kDouble, true);
488 break;
489
490 case Instruction::CMP_LONG:
491 EmitInsn_LongCompare(ARGS);
492 break;
493
494 case Instruction::IF_EQ:
495 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_EQ);
496 break;
497
498 case Instruction::IF_NE:
499 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_NE);
500 break;
501
502 case Instruction::IF_LT:
503 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_LT);
504 break;
505
506 case Instruction::IF_GE:
507 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_GE);
508 break;
509
510 case Instruction::IF_GT:
511 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_GT);
512 break;
513
514 case Instruction::IF_LE:
515 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_LE);
516 break;
517
518 case Instruction::IF_EQZ:
519 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_EQ);
520 break;
521
522 case Instruction::IF_NEZ:
523 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_NE);
524 break;
525
526 case Instruction::IF_LTZ:
527 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_LT);
528 break;
529
530 case Instruction::IF_GEZ:
531 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_GE);
532 break;
533
534 case Instruction::IF_GTZ:
535 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_GT);
536 break;
537
538 case Instruction::IF_LEZ:
539 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_LE);
540 break;
541
542 case Instruction::AGET:
543 EmitInsn_AGet(ARGS, kInt);
544 break;
545
546 case Instruction::AGET_WIDE:
547 EmitInsn_AGet(ARGS, kLong);
548 break;
549
550 case Instruction::AGET_OBJECT:
551 EmitInsn_AGet(ARGS, kObject);
552 break;
553
554 case Instruction::AGET_BOOLEAN:
555 EmitInsn_AGet(ARGS, kBoolean);
556 break;
557
558 case Instruction::AGET_BYTE:
559 EmitInsn_AGet(ARGS, kByte);
560 break;
561
562 case Instruction::AGET_CHAR:
563 EmitInsn_AGet(ARGS, kChar);
564 break;
565
566 case Instruction::AGET_SHORT:
567 EmitInsn_AGet(ARGS, kShort);
568 break;
569
570 case Instruction::APUT:
571 EmitInsn_APut(ARGS, kInt);
572 break;
573
574 case Instruction::APUT_WIDE:
575 EmitInsn_APut(ARGS, kLong);
576 break;
577
578 case Instruction::APUT_OBJECT:
579 EmitInsn_APut(ARGS, kObject);
580 break;
581
582 case Instruction::APUT_BOOLEAN:
583 EmitInsn_APut(ARGS, kBoolean);
584 break;
585
586 case Instruction::APUT_BYTE:
587 EmitInsn_APut(ARGS, kByte);
588 break;
589
590 case Instruction::APUT_CHAR:
591 EmitInsn_APut(ARGS, kChar);
592 break;
593
594 case Instruction::APUT_SHORT:
595 EmitInsn_APut(ARGS, kShort);
596 break;
597
598 case Instruction::IGET:
599 EmitInsn_IGet(ARGS, kInt);
600 break;
601
602 case Instruction::IGET_WIDE:
603 EmitInsn_IGet(ARGS, kLong);
604 break;
605
606 case Instruction::IGET_OBJECT:
607 EmitInsn_IGet(ARGS, kObject);
608 break;
609
610 case Instruction::IGET_BOOLEAN:
611 EmitInsn_IGet(ARGS, kBoolean);
612 break;
613
614 case Instruction::IGET_BYTE:
615 EmitInsn_IGet(ARGS, kByte);
616 break;
617
618 case Instruction::IGET_CHAR:
619 EmitInsn_IGet(ARGS, kChar);
620 break;
621
622 case Instruction::IGET_SHORT:
623 EmitInsn_IGet(ARGS, kShort);
624 break;
625
626 case Instruction::IPUT:
627 EmitInsn_IPut(ARGS, kInt);
628 break;
629
630 case Instruction::IPUT_WIDE:
631 EmitInsn_IPut(ARGS, kLong);
632 break;
633
634 case Instruction::IPUT_OBJECT:
635 EmitInsn_IPut(ARGS, kObject);
636 break;
637
638 case Instruction::IPUT_BOOLEAN:
639 EmitInsn_IPut(ARGS, kBoolean);
640 break;
641
642 case Instruction::IPUT_BYTE:
643 EmitInsn_IPut(ARGS, kByte);
644 break;
645
646 case Instruction::IPUT_CHAR:
647 EmitInsn_IPut(ARGS, kChar);
648 break;
649
650 case Instruction::IPUT_SHORT:
651 EmitInsn_IPut(ARGS, kShort);
652 break;
653
654 case Instruction::SGET:
655 EmitInsn_SGet(ARGS, kInt);
656 break;
657
658 case Instruction::SGET_WIDE:
659 EmitInsn_SGet(ARGS, kLong);
660 break;
661
662 case Instruction::SGET_OBJECT:
663 EmitInsn_SGet(ARGS, kObject);
664 break;
665
666 case Instruction::SGET_BOOLEAN:
667 EmitInsn_SGet(ARGS, kBoolean);
668 break;
669
670 case Instruction::SGET_BYTE:
671 EmitInsn_SGet(ARGS, kByte);
672 break;
673
674 case Instruction::SGET_CHAR:
675 EmitInsn_SGet(ARGS, kChar);
676 break;
677
678 case Instruction::SGET_SHORT:
679 EmitInsn_SGet(ARGS, kShort);
680 break;
681
682 case Instruction::SPUT:
683 EmitInsn_SPut(ARGS, kInt);
684 break;
685
686 case Instruction::SPUT_WIDE:
687 EmitInsn_SPut(ARGS, kLong);
688 break;
689
690 case Instruction::SPUT_OBJECT:
691 EmitInsn_SPut(ARGS, kObject);
692 break;
693
694 case Instruction::SPUT_BOOLEAN:
695 EmitInsn_SPut(ARGS, kBoolean);
696 break;
697
698 case Instruction::SPUT_BYTE:
699 EmitInsn_SPut(ARGS, kByte);
700 break;
701
702 case Instruction::SPUT_CHAR:
703 EmitInsn_SPut(ARGS, kChar);
704 break;
705
706 case Instruction::SPUT_SHORT:
707 EmitInsn_SPut(ARGS, kShort);
708 break;
709
710
711 case Instruction::INVOKE_VIRTUAL:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800712 EmitInsn_Invoke(ARGS, kVirtual, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800713 break;
714
715 case Instruction::INVOKE_SUPER:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800716 EmitInsn_Invoke(ARGS, kSuper, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800717 break;
718
719 case Instruction::INVOKE_DIRECT:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800720 EmitInsn_Invoke(ARGS, kDirect, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800721 break;
722
723 case Instruction::INVOKE_STATIC:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800724 EmitInsn_Invoke(ARGS, kStatic, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800725 break;
726
727 case Instruction::INVOKE_INTERFACE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800728 EmitInsn_Invoke(ARGS, kInterface, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800729 break;
730
731 case Instruction::INVOKE_VIRTUAL_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800732 EmitInsn_Invoke(ARGS, kVirtual, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800733 break;
734
735 case Instruction::INVOKE_SUPER_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800736 EmitInsn_Invoke(ARGS, kSuper, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800737 break;
738
739 case Instruction::INVOKE_DIRECT_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800740 EmitInsn_Invoke(ARGS, kDirect, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800741 break;
742
743 case Instruction::INVOKE_STATIC_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800744 EmitInsn_Invoke(ARGS, kStatic, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800745 break;
746
747 case Instruction::INVOKE_INTERFACE_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800748 EmitInsn_Invoke(ARGS, kInterface, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800749 break;
750
751 case Instruction::NEG_INT:
752 EmitInsn_Neg(ARGS, kInt);
753 break;
754
755 case Instruction::NOT_INT:
756 EmitInsn_Not(ARGS, kInt);
757 break;
758
759 case Instruction::NEG_LONG:
760 EmitInsn_Neg(ARGS, kLong);
761 break;
762
763 case Instruction::NOT_LONG:
764 EmitInsn_Not(ARGS, kLong);
765 break;
766
767 case Instruction::NEG_FLOAT:
768 EmitInsn_FNeg(ARGS, kFloat);
769 break;
770
771 case Instruction::NEG_DOUBLE:
772 EmitInsn_FNeg(ARGS, kDouble);
773 break;
774
775 case Instruction::INT_TO_LONG:
776 EmitInsn_SExt(ARGS);
777 break;
778
779 case Instruction::INT_TO_FLOAT:
780 EmitInsn_IntToFP(ARGS, kInt, kFloat);
781 break;
782
783 case Instruction::INT_TO_DOUBLE:
784 EmitInsn_IntToFP(ARGS, kInt, kDouble);
785 break;
786
787 case Instruction::LONG_TO_INT:
788 EmitInsn_Trunc(ARGS);
789 break;
790
791 case Instruction::LONG_TO_FLOAT:
792 EmitInsn_IntToFP(ARGS, kLong, kFloat);
793 break;
794
795 case Instruction::LONG_TO_DOUBLE:
796 EmitInsn_IntToFP(ARGS, kLong, kDouble);
797 break;
798
799 case Instruction::FLOAT_TO_INT:
TDYa127a4746872012-04-11 23:48:55 -0700800 EmitInsn_FPToInt(ARGS, kFloat, kInt, F2I);
Logan Chien70f94b42011-12-27 17:49:11 +0800801 break;
802
803 case Instruction::FLOAT_TO_LONG:
TDYa127a4746872012-04-11 23:48:55 -0700804 EmitInsn_FPToInt(ARGS, kFloat, kLong, F2L);
Logan Chien70f94b42011-12-27 17:49:11 +0800805 break;
806
807 case Instruction::FLOAT_TO_DOUBLE:
808 EmitInsn_FExt(ARGS);
809 break;
810
811 case Instruction::DOUBLE_TO_INT:
TDYa127a4746872012-04-11 23:48:55 -0700812 EmitInsn_FPToInt(ARGS, kDouble, kInt, D2I);
Logan Chien70f94b42011-12-27 17:49:11 +0800813 break;
814
815 case Instruction::DOUBLE_TO_LONG:
TDYa127a4746872012-04-11 23:48:55 -0700816 EmitInsn_FPToInt(ARGS, kDouble, kLong, D2L);
Logan Chien70f94b42011-12-27 17:49:11 +0800817 break;
818
819 case Instruction::DOUBLE_TO_FLOAT:
820 EmitInsn_FTrunc(ARGS);
821 break;
822
823 case Instruction::INT_TO_BYTE:
824 EmitInsn_TruncAndSExt(ARGS, 8);
825 break;
826
827 case Instruction::INT_TO_CHAR:
828 EmitInsn_TruncAndZExt(ARGS, 16);
829 break;
830
831 case Instruction::INT_TO_SHORT:
832 EmitInsn_TruncAndSExt(ARGS, 16);
833 break;
834
835 case Instruction::ADD_INT:
836 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kInt, false);
837 break;
838
839 case Instruction::SUB_INT:
840 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kInt, false);
841 break;
842
843 case Instruction::MUL_INT:
844 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kInt, false);
845 break;
846
847 case Instruction::DIV_INT:
848 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kInt, false);
849 break;
850
851 case Instruction::REM_INT:
852 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kInt, false);
853 break;
854
855 case Instruction::AND_INT:
856 EmitInsn_IntArithm(ARGS, kIntArithm_And, kInt, false);
857 break;
858
859 case Instruction::OR_INT:
860 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kInt, false);
861 break;
862
863 case Instruction::XOR_INT:
864 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kInt, false);
865 break;
866
867 case Instruction::SHL_INT:
Logan Chien5539ad02012-04-02 14:36:55 +0800868 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kInt, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800869 break;
870
871 case Instruction::SHR_INT:
Logan Chien5539ad02012-04-02 14:36:55 +0800872 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kInt, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800873 break;
874
875 case Instruction::USHR_INT:
Logan Chien5539ad02012-04-02 14:36:55 +0800876 EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kInt, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800877 break;
878
879 case Instruction::ADD_LONG:
880 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kLong, false);
881 break;
882
883 case Instruction::SUB_LONG:
884 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kLong, false);
885 break;
886
887 case Instruction::MUL_LONG:
888 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kLong, false);
889 break;
890
891 case Instruction::DIV_LONG:
892 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kLong, false);
893 break;
894
895 case Instruction::REM_LONG:
896 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kLong, false);
897 break;
898
899 case Instruction::AND_LONG:
900 EmitInsn_IntArithm(ARGS, kIntArithm_And, kLong, false);
901 break;
902
903 case Instruction::OR_LONG:
904 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kLong, false);
905 break;
906
907 case Instruction::XOR_LONG:
908 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kLong, false);
909 break;
910
911 case Instruction::SHL_LONG:
Logan Chien5539ad02012-04-02 14:36:55 +0800912 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kLong, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800913 break;
914
915 case Instruction::SHR_LONG:
Logan Chien5539ad02012-04-02 14:36:55 +0800916 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kLong, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800917 break;
918
919 case Instruction::USHR_LONG:
Logan Chien5539ad02012-04-02 14:36:55 +0800920 EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kLong, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800921 break;
922
923 case Instruction::ADD_FLOAT:
924 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kFloat, false);
925 break;
926
927 case Instruction::SUB_FLOAT:
928 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kFloat, false);
929 break;
930
931 case Instruction::MUL_FLOAT:
932 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kFloat, false);
933 break;
934
935 case Instruction::DIV_FLOAT:
936 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kFloat, false);
937 break;
938
939 case Instruction::REM_FLOAT:
940 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kFloat, false);
941 break;
942
943 case Instruction::ADD_DOUBLE:
944 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kDouble, false);
945 break;
946
947 case Instruction::SUB_DOUBLE:
948 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kDouble, false);
949 break;
950
951 case Instruction::MUL_DOUBLE:
952 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kDouble, false);
953 break;
954
955 case Instruction::DIV_DOUBLE:
956 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kDouble, false);
957 break;
958
959 case Instruction::REM_DOUBLE:
960 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kDouble, false);
961 break;
962
963 case Instruction::ADD_INT_2ADDR:
964 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kInt, true);
965 break;
966
967 case Instruction::SUB_INT_2ADDR:
968 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kInt, true);
969 break;
970
971 case Instruction::MUL_INT_2ADDR:
972 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kInt, true);
973 break;
974
975 case Instruction::DIV_INT_2ADDR:
976 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kInt, true);
977 break;
978
979 case Instruction::REM_INT_2ADDR:
980 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kInt, true);
981 break;
982
983 case Instruction::AND_INT_2ADDR:
984 EmitInsn_IntArithm(ARGS, kIntArithm_And, kInt, true);
985 break;
986
987 case Instruction::OR_INT_2ADDR:
988 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kInt, true);
989 break;
990
991 case Instruction::XOR_INT_2ADDR:
992 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kInt, true);
993 break;
994
995 case Instruction::SHL_INT_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +0800996 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kInt, true);
Logan Chien70f94b42011-12-27 17:49:11 +0800997 break;
998
999 case Instruction::SHR_INT_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001000 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kInt, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001001 break;
1002
1003 case Instruction::USHR_INT_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001004 EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kInt, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001005 break;
1006
1007 case Instruction::ADD_LONG_2ADDR:
1008 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kLong, true);
1009 break;
1010
1011 case Instruction::SUB_LONG_2ADDR:
1012 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kLong, true);
1013 break;
1014
1015 case Instruction::MUL_LONG_2ADDR:
1016 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kLong, true);
1017 break;
1018
1019 case Instruction::DIV_LONG_2ADDR:
1020 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kLong, true);
1021 break;
1022
1023 case Instruction::REM_LONG_2ADDR:
1024 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kLong, true);
1025 break;
1026
1027 case Instruction::AND_LONG_2ADDR:
1028 EmitInsn_IntArithm(ARGS, kIntArithm_And, kLong, true);
1029 break;
1030
1031 case Instruction::OR_LONG_2ADDR:
1032 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kLong, true);
1033 break;
1034
1035 case Instruction::XOR_LONG_2ADDR:
1036 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kLong, true);
1037 break;
1038
1039 case Instruction::SHL_LONG_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001040 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kLong, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001041 break;
1042
1043 case Instruction::SHR_LONG_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001044 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kLong, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001045 break;
1046
1047 case Instruction::USHR_LONG_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001048 EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kLong, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001049 break;
1050
1051 case Instruction::ADD_FLOAT_2ADDR:
1052 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kFloat, true);
1053 break;
1054
1055 case Instruction::SUB_FLOAT_2ADDR:
1056 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kFloat, true);
1057 break;
1058
1059 case Instruction::MUL_FLOAT_2ADDR:
1060 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kFloat, true);
1061 break;
1062
1063 case Instruction::DIV_FLOAT_2ADDR:
1064 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kFloat, true);
1065 break;
1066
1067 case Instruction::REM_FLOAT_2ADDR:
1068 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kFloat, true);
1069 break;
1070
1071 case Instruction::ADD_DOUBLE_2ADDR:
1072 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kDouble, true);
1073 break;
1074
1075 case Instruction::SUB_DOUBLE_2ADDR:
1076 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kDouble, true);
1077 break;
1078
1079 case Instruction::MUL_DOUBLE_2ADDR:
1080 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kDouble, true);
1081 break;
1082
1083 case Instruction::DIV_DOUBLE_2ADDR:
1084 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kDouble, true);
1085 break;
1086
1087 case Instruction::REM_DOUBLE_2ADDR:
1088 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kDouble, true);
1089 break;
1090
1091 case Instruction::ADD_INT_LIT16:
1092 case Instruction::ADD_INT_LIT8:
1093 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Add);
1094 break;
1095
1096 case Instruction::RSUB_INT:
1097 case Instruction::RSUB_INT_LIT8:
1098 EmitInsn_RSubImmediate(ARGS);
1099 break;
1100
1101 case Instruction::MUL_INT_LIT16:
1102 case Instruction::MUL_INT_LIT8:
1103 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Mul);
1104 break;
1105
1106 case Instruction::DIV_INT_LIT16:
1107 case Instruction::DIV_INT_LIT8:
1108 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Div);
1109 break;
1110
1111 case Instruction::REM_INT_LIT16:
1112 case Instruction::REM_INT_LIT8:
1113 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Rem);
1114 break;
1115
1116 case Instruction::AND_INT_LIT16:
1117 case Instruction::AND_INT_LIT8:
1118 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_And);
1119 break;
1120
1121 case Instruction::OR_INT_LIT16:
1122 case Instruction::OR_INT_LIT8:
1123 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Or);
1124 break;
1125
1126 case Instruction::XOR_INT_LIT16:
1127 case Instruction::XOR_INT_LIT8:
1128 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Xor);
1129 break;
1130
1131 case Instruction::SHL_INT_LIT8:
Logan Chien5539ad02012-04-02 14:36:55 +08001132 EmitInsn_IntShiftArithmImmediate(ARGS, kIntArithm_Shl);
Logan Chien70f94b42011-12-27 17:49:11 +08001133 break;
1134
1135 case Instruction::SHR_INT_LIT8:
Logan Chien5539ad02012-04-02 14:36:55 +08001136 EmitInsn_IntShiftArithmImmediate(ARGS, kIntArithm_Shr);
Logan Chien70f94b42011-12-27 17:49:11 +08001137 break;
1138
1139 case Instruction::USHR_INT_LIT8:
Logan Chien5539ad02012-04-02 14:36:55 +08001140 EmitInsn_IntShiftArithmImmediate(ARGS, kIntArithm_UShr);
Logan Chien70f94b42011-12-27 17:49:11 +08001141 break;
1142
Logan Chien9e5f5c12012-04-10 13:51:45 +08001143 case Instruction::THROW_VERIFICATION_ERROR:
1144 EmitInsn_ThrowVerificationError(ARGS);
1145 break;
1146
Logan Chien70f94b42011-12-27 17:49:11 +08001147 case Instruction::UNUSED_3E:
1148 case Instruction::UNUSED_3F:
1149 case Instruction::UNUSED_40:
1150 case Instruction::UNUSED_41:
1151 case Instruction::UNUSED_42:
1152 case Instruction::UNUSED_43:
1153 case Instruction::UNUSED_73:
1154 case Instruction::UNUSED_79:
1155 case Instruction::UNUSED_7A:
1156 case Instruction::UNUSED_E3:
1157 case Instruction::UNUSED_E4:
1158 case Instruction::UNUSED_E5:
1159 case Instruction::UNUSED_E6:
1160 case Instruction::UNUSED_E7:
1161 case Instruction::UNUSED_E8:
1162 case Instruction::UNUSED_E9:
1163 case Instruction::UNUSED_EA:
1164 case Instruction::UNUSED_EB:
1165 case Instruction::UNUSED_EC:
Logan Chien70f94b42011-12-27 17:49:11 +08001166 case Instruction::UNUSED_EE:
1167 case Instruction::UNUSED_EF:
1168 case Instruction::UNUSED_F0:
1169 case Instruction::UNUSED_F1:
1170 case Instruction::UNUSED_F2:
1171 case Instruction::UNUSED_F3:
1172 case Instruction::UNUSED_F4:
1173 case Instruction::UNUSED_F5:
1174 case Instruction::UNUSED_F6:
1175 case Instruction::UNUSED_F7:
1176 case Instruction::UNUSED_F8:
1177 case Instruction::UNUSED_F9:
1178 case Instruction::UNUSED_FA:
1179 case Instruction::UNUSED_FB:
1180 case Instruction::UNUSED_FC:
1181 case Instruction::UNUSED_FD:
1182 case Instruction::UNUSED_FE:
1183 case Instruction::UNUSED_FF:
1184 LOG(FATAL) << "Dex file contains UNUSED bytecode: " << insn->Opcode();
1185 break;
1186 }
1187
1188#undef ARGS
1189}
1190
1191
1192void MethodCompiler::EmitInsn_Nop(uint32_t dex_pc,
1193 Instruction const* insn) {
Logan Chiene09a6b72011-12-27 17:50:21 +08001194
1195 uint16_t insn_signature = code_item_->insns_[dex_pc];
1196
1197 if (insn_signature == Instruction::kPackedSwitchSignature ||
1198 insn_signature == Instruction::kSparseSwitchSignature ||
1199 insn_signature == Instruction::kArrayDataSignature) {
1200 irb_.CreateUnreachable();
Elliott Hughesb25c3f62012-03-26 16:35:06 -07001201 } else {
Logan Chiene09a6b72011-12-27 17:50:21 +08001202 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1203 }
Shih-wei Liaod1fec812012-02-13 09:51:10 -08001204}
1205
1206
Logan Chien70f94b42011-12-27 17:49:11 +08001207void MethodCompiler::EmitInsn_Move(uint32_t dex_pc,
1208 Instruction const* insn,
1209 JType jty) {
Logan Chien48173132011-12-27 17:51:13 +08001210
Elliott Hughesadb8c672012-03-06 16:49:32 -08001211 DecodedInstruction dec_insn(insn);
Logan Chien48173132011-12-27 17:51:13 +08001212
Elliott Hughesadb8c672012-03-06 16:49:32 -08001213 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, jty, kReg);
1214 EmitStoreDalvikReg(dec_insn.vA, jty, kReg, src_value);
Logan Chien48173132011-12-27 17:51:13 +08001215
Logan Chien70f94b42011-12-27 17:49:11 +08001216 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1217}
1218
1219
1220void MethodCompiler::EmitInsn_MoveResult(uint32_t dex_pc,
1221 Instruction const* insn,
1222 JType jty) {
Logan Chien48173132011-12-27 17:51:13 +08001223
Elliott Hughesadb8c672012-03-06 16:49:32 -08001224 DecodedInstruction dec_insn(insn);
Logan Chien48173132011-12-27 17:51:13 +08001225
1226 llvm::Value* src_value = EmitLoadDalvikRetValReg(jty, kReg);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001227 EmitStoreDalvikReg(dec_insn.vA, jty, kReg, src_value);
Logan Chien48173132011-12-27 17:51:13 +08001228
Logan Chien70f94b42011-12-27 17:49:11 +08001229 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1230}
1231
1232
1233void MethodCompiler::EmitInsn_MoveException(uint32_t dex_pc,
1234 Instruction const* insn) {
Logan Chien3354cec2012-01-13 14:29:03 +08001235
Elliott Hughesadb8c672012-03-06 16:49:32 -08001236 DecodedInstruction dec_insn(insn);
Logan Chien3354cec2012-01-13 14:29:03 +08001237
1238 // Get thread-local exception field address
1239 llvm::Constant* exception_field_offset =
1240 irb_.getPtrEquivInt(Thread::ExceptionOffset().Int32Value());
1241
1242 llvm::Value* thread_object_addr =
1243 irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1244
1245 llvm::Value* exception_field_addr =
1246 irb_.CreatePtrDisp(thread_object_addr, exception_field_offset,
1247 irb_.getJObjectTy()->getPointerTo());
1248
1249 // Get exception object address
1250 llvm::Value* exception_object_addr = irb_.CreateLoad(exception_field_addr);
1251
1252 // Set thread-local exception field address to NULL
1253 irb_.CreateStore(irb_.getJNull(), exception_field_addr);
1254
1255 // Keep the exception object in the Dalvik register
Elliott Hughesadb8c672012-03-06 16:49:32 -08001256 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, exception_object_addr);
Logan Chien3354cec2012-01-13 14:29:03 +08001257
Logan Chien70f94b42011-12-27 17:49:11 +08001258 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1259}
1260
1261
1262void MethodCompiler::EmitInsn_ThrowException(uint32_t dex_pc,
1263 Instruction const* insn) {
Logan Chien6c6f12d2012-01-13 19:26:27 +08001264
Elliott Hughesadb8c672012-03-06 16:49:32 -08001265 DecodedInstruction dec_insn(insn);
Logan Chien6c6f12d2012-01-13 19:26:27 +08001266
1267 llvm::Value* exception_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001268 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chien6c6f12d2012-01-13 19:26:27 +08001269
Logan Chien8dfcbea2012-02-17 18:50:32 +08001270 EmitUpdateLineNumFromDexPC(dex_pc);
1271
Logan Chien6c6f12d2012-01-13 19:26:27 +08001272 irb_.CreateCall(irb_.GetRuntime(ThrowException), exception_addr);
1273
1274 EmitBranchExceptionLandingPad(dex_pc);
Logan Chien70f94b42011-12-27 17:49:11 +08001275}
1276
1277
Logan Chien9e5f5c12012-04-10 13:51:45 +08001278void MethodCompiler::EmitInsn_ThrowVerificationError(uint32_t dex_pc,
1279 Instruction const* insn) {
1280
1281 DecodedInstruction dec_insn(insn);
1282
1283 EmitUpdateLineNumFromDexPC(dex_pc);
1284
1285 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1286 llvm::Value* kind_value = irb_.getInt32(dec_insn.vA);
1287 llvm::Value* ref_value = irb_.getInt32(dec_insn.vB);
1288
1289 irb_.CreateCall3(irb_.GetRuntime(ThrowVerificationError),
1290 method_object_addr, kind_value, ref_value);
1291
1292 EmitBranchExceptionLandingPad(dex_pc);
1293}
1294
1295
Logan Chien70f94b42011-12-27 17:49:11 +08001296void MethodCompiler::EmitInsn_ReturnVoid(uint32_t dex_pc,
1297 Instruction const* insn) {
Logan Chien8898a272011-12-27 17:51:56 +08001298 // Garbage collection safe-point
1299 EmitGuard_GarbageCollectionSuspend(dex_pc);
1300
Logan Chien8dfcbea2012-02-17 18:50:32 +08001301 // Pop the shadow frame
1302 EmitPopShadowFrame();
1303
Logan Chien8898a272011-12-27 17:51:56 +08001304 // Return!
1305 irb_.CreateRetVoid();
Logan Chien70f94b42011-12-27 17:49:11 +08001306}
1307
1308
1309void MethodCompiler::EmitInsn_Return(uint32_t dex_pc,
1310 Instruction const* insn) {
Logan Chien8898a272011-12-27 17:51:56 +08001311
Elliott Hughesadb8c672012-03-06 16:49:32 -08001312 DecodedInstruction dec_insn(insn);
Logan Chien8898a272011-12-27 17:51:56 +08001313
1314 // Garbage collection safe-point
1315 EmitGuard_GarbageCollectionSuspend(dex_pc);
1316
Logan Chien8dfcbea2012-02-17 18:50:32 +08001317 // Pop the shadow frame
1318 EmitPopShadowFrame();
1319 // NOTE: It is important to keep this AFTER the GC safe-point. Otherwise,
1320 // the return value might be collected since the shadow stack is popped.
1321
Logan Chien8898a272011-12-27 17:51:56 +08001322 // Return!
Logan Chiendd361c92012-04-10 23:40:37 +08001323 char ret_shorty = oat_compilation_unit_->GetShorty()[0];
Elliott Hughesadb8c672012-03-06 16:49:32 -08001324 llvm::Value* retval = EmitLoadDalvikReg(dec_insn.vA, ret_shorty, kAccurate);
Logan Chien8898a272011-12-27 17:51:56 +08001325
1326 irb_.CreateRet(retval);
Logan Chien70f94b42011-12-27 17:49:11 +08001327}
1328
1329
1330void MethodCompiler::EmitInsn_LoadConstant(uint32_t dex_pc,
1331 Instruction const* insn,
1332 JType imm_jty) {
Shih-wei Liao798366e2012-02-16 09:25:33 -08001333
Elliott Hughesadb8c672012-03-06 16:49:32 -08001334 DecodedInstruction dec_insn(insn);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001335
1336 DCHECK(imm_jty == kInt || imm_jty == kLong) << imm_jty;
1337
1338 int64_t imm = 0;
1339
1340 switch (insn->Opcode()) {
1341 // 32-bit Immediate
1342 case Instruction::CONST_4:
1343 case Instruction::CONST_16:
1344 case Instruction::CONST:
1345 case Instruction::CONST_WIDE_16:
1346 case Instruction::CONST_WIDE_32:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001347 imm = static_cast<int64_t>(static_cast<int32_t>(dec_insn.vB));
Shih-wei Liao798366e2012-02-16 09:25:33 -08001348 break;
1349
1350 case Instruction::CONST_HIGH16:
1351 imm = static_cast<int64_t>(static_cast<int32_t>(
Elliott Hughesadb8c672012-03-06 16:49:32 -08001352 static_cast<uint32_t>(static_cast<uint16_t>(dec_insn.vB)) << 16));
Shih-wei Liao798366e2012-02-16 09:25:33 -08001353 break;
1354
1355 // 64-bit Immediate
1356 case Instruction::CONST_WIDE:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001357 imm = static_cast<int64_t>(dec_insn.vB_wide);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001358 break;
1359
1360 case Instruction::CONST_WIDE_HIGH16:
1361 imm = static_cast<int64_t>(
Elliott Hughesadb8c672012-03-06 16:49:32 -08001362 static_cast<uint64_t>(static_cast<uint16_t>(dec_insn.vB)) << 48);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001363 break;
1364
1365 // Unknown opcode for load constant (unreachable)
1366 default:
1367 LOG(FATAL) << "Unknown opcode for load constant: " << insn->Opcode();
1368 break;
1369 }
1370
1371 // Store the non-object register
1372 llvm::Type* imm_type = irb_.getJType(imm_jty, kAccurate);
1373 llvm::Constant* imm_value = llvm::ConstantInt::getSigned(imm_type, imm);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001374 EmitStoreDalvikReg(dec_insn.vA, imm_jty, kAccurate, imm_value);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001375
1376 // Store the object register if it is possible to be null.
1377 if (imm_jty == kInt && imm == 0) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001378 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, irb_.getJNull());
Shih-wei Liao798366e2012-02-16 09:25:33 -08001379 }
1380
Logan Chien70f94b42011-12-27 17:49:11 +08001381 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1382}
1383
1384
1385void MethodCompiler::EmitInsn_LoadConstantString(uint32_t dex_pc,
1386 Instruction const* insn) {
Logan Chienc3b4ba12012-01-16 19:52:53 +08001387
Elliott Hughesadb8c672012-03-06 16:49:32 -08001388 DecodedInstruction dec_insn(insn);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001389
Elliott Hughesadb8c672012-03-06 16:49:32 -08001390 uint32_t string_idx = dec_insn.vB;
Logan Chienc3b4ba12012-01-16 19:52:53 +08001391
1392 llvm::Value* string_field_addr = EmitLoadDexCacheStringFieldAddr(string_idx);
1393
1394 llvm::Value* string_addr = irb_.CreateLoad(string_field_addr);
1395
1396 if (!compiler_->CanAssumeStringIsPresentInDexCache(dex_cache_, string_idx)) {
1397 llvm::BasicBlock* block_str_exist =
1398 CreateBasicBlockWithDexPC(dex_pc, "str_exist");
1399
1400 llvm::BasicBlock* block_str_resolve =
1401 CreateBasicBlockWithDexPC(dex_pc, "str_resolve");
1402
1403 // Test: Is the string resolved and in the dex cache?
1404 llvm::Value* equal_null = irb_.CreateICmpEQ(string_addr, irb_.getJNull());
1405
TDYa127a849cb62012-04-01 05:59:34 -07001406 irb_.CreateCondBr(equal_null, block_str_resolve, block_str_exist);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001407
1408 // String is resolved, go to next basic block.
1409 irb_.SetInsertPoint(block_str_exist);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001410 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, string_addr);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001411 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1412
1413 // String is not resolved yet, resolve it now.
1414 irb_.SetInsertPoint(block_str_resolve);
1415
1416 llvm::Function* runtime_func = irb_.GetRuntime(ResolveString);
1417
1418 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1419
1420 llvm::Value* string_idx_value = irb_.getInt32(string_idx);
1421
Logan Chien8dfcbea2012-02-17 18:50:32 +08001422 EmitUpdateLineNumFromDexPC(dex_pc);
1423
1424 string_addr = irb_.CreateCall2(runtime_func, method_object_addr,
1425 string_idx_value);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001426
1427 EmitGuard_ExceptionLandingPad(dex_pc);
1428 }
1429
1430 // Store the string object to the Dalvik register
Elliott Hughesadb8c672012-03-06 16:49:32 -08001431 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, string_addr);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001432
Logan Chien70f94b42011-12-27 17:49:11 +08001433 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1434}
1435
1436
Logan Chien27b30252012-01-14 03:43:35 +08001437llvm::Value* MethodCompiler::EmitLoadConstantClass(uint32_t dex_pc,
1438 uint32_t type_idx) {
1439 if (!compiler_->CanAccessTypeWithoutChecks(method_idx_, dex_cache_,
1440 *dex_file_, type_idx)) {
1441 llvm::Value* type_idx_value = irb_.getInt32(type_idx);
1442
1443 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1444
1445 llvm::Function* runtime_func =
1446 irb_.GetRuntime(InitializeTypeAndVerifyAccess);
1447
Logan Chien8dfcbea2012-02-17 18:50:32 +08001448 EmitUpdateLineNumFromDexPC(dex_pc);
1449
Logan Chien27b30252012-01-14 03:43:35 +08001450 llvm::Value* type_object_addr =
1451 irb_.CreateCall2(runtime_func, type_idx_value, method_object_addr);
1452
1453 EmitGuard_ExceptionLandingPad(dex_pc);
1454
1455 return type_object_addr;
1456
1457 } else {
1458 // Try to load the class (type) object from the test cache.
1459 llvm::Value* type_field_addr =
1460 EmitLoadDexCacheResolvedTypeFieldAddr(type_idx);
1461
1462 llvm::Value* type_object_addr = irb_.CreateLoad(type_field_addr);
1463
1464 if (compiler_->CanAssumeTypeIsPresentInDexCache(dex_cache_, type_idx)) {
1465 return type_object_addr;
1466 }
1467
1468 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
1469
1470 // Test whether class (type) object is in the dex cache or not
1471 llvm::Value* equal_null =
1472 irb_.CreateICmpEQ(type_object_addr, irb_.getJNull());
1473
1474 llvm::BasicBlock* block_cont =
1475 CreateBasicBlockWithDexPC(dex_pc, "cont");
1476
1477 llvm::BasicBlock* block_load_class =
1478 CreateBasicBlockWithDexPC(dex_pc, "load_class");
1479
1480 irb_.CreateCondBr(equal_null, block_load_class, block_cont);
1481
1482 // Failback routine to load the class object
1483 irb_.SetInsertPoint(block_load_class);
1484
1485 llvm::Function* runtime_func = irb_.GetRuntime(InitializeType);
1486
1487 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
1488
1489 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1490
Logan Chien8dfcbea2012-02-17 18:50:32 +08001491 EmitUpdateLineNumFromDexPC(dex_pc);
1492
Logan Chien27b30252012-01-14 03:43:35 +08001493 llvm::Value* loaded_type_object_addr =
1494 irb_.CreateCall2(runtime_func, type_idx_value, method_object_addr);
1495
1496 EmitGuard_ExceptionLandingPad(dex_pc);
1497
1498 llvm::BasicBlock* block_after_load_class = irb_.GetInsertBlock();
1499
1500 irb_.CreateBr(block_cont);
1501
1502 // Now the class object must be loaded
1503 irb_.SetInsertPoint(block_cont);
1504
1505 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1506
1507 phi->addIncoming(type_object_addr, block_original);
1508 phi->addIncoming(loaded_type_object_addr, block_after_load_class);
1509
1510 return phi;
1511 }
1512}
1513
1514
Logan Chien70f94b42011-12-27 17:49:11 +08001515void MethodCompiler::EmitInsn_LoadConstantClass(uint32_t dex_pc,
1516 Instruction const* insn) {
Logan Chien27b30252012-01-14 03:43:35 +08001517
Elliott Hughesadb8c672012-03-06 16:49:32 -08001518 DecodedInstruction dec_insn(insn);
Logan Chien27b30252012-01-14 03:43:35 +08001519
Elliott Hughesadb8c672012-03-06 16:49:32 -08001520 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, dec_insn.vB);
1521 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, type_object_addr);
Logan Chien27b30252012-01-14 03:43:35 +08001522
Logan Chien70f94b42011-12-27 17:49:11 +08001523 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1524}
1525
1526
1527void MethodCompiler::EmitInsn_MonitorEnter(uint32_t dex_pc,
1528 Instruction const* insn) {
Logan Chien9e0dbe42012-01-13 12:11:37 +08001529
Elliott Hughesadb8c672012-03-06 16:49:32 -08001530 DecodedInstruction dec_insn(insn);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001531
1532 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001533 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001534
1535 // TODO: Slow path always. May not need NullPointerException check.
1536 EmitGuard_NullPointerException(dex_pc, object_addr);
1537
Logan Chien8dfcbea2012-02-17 18:50:32 +08001538 EmitUpdateLineNumFromDexPC(dex_pc);
1539
Logan Chien9e0dbe42012-01-13 12:11:37 +08001540 irb_.CreateCall(irb_.GetRuntime(LockObject), object_addr);
1541 EmitGuard_ExceptionLandingPad(dex_pc);
1542
Logan Chien70f94b42011-12-27 17:49:11 +08001543 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1544}
1545
1546
1547void MethodCompiler::EmitInsn_MonitorExit(uint32_t dex_pc,
1548 Instruction const* insn) {
Logan Chien9e0dbe42012-01-13 12:11:37 +08001549
Elliott Hughesadb8c672012-03-06 16:49:32 -08001550 DecodedInstruction dec_insn(insn);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001551
1552 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001553 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001554
1555 EmitGuard_NullPointerException(dex_pc, object_addr);
1556
Logan Chien8dfcbea2012-02-17 18:50:32 +08001557 EmitUpdateLineNumFromDexPC(dex_pc);
1558
Logan Chien9e0dbe42012-01-13 12:11:37 +08001559 irb_.CreateCall(irb_.GetRuntime(UnlockObject), object_addr);
1560 EmitGuard_ExceptionLandingPad(dex_pc);
1561
Logan Chien70f94b42011-12-27 17:49:11 +08001562 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1563}
1564
1565
1566void MethodCompiler::EmitInsn_CheckCast(uint32_t dex_pc,
1567 Instruction const* insn) {
Logan Chienfc880952012-01-15 23:53:10 +08001568
Elliott Hughesadb8c672012-03-06 16:49:32 -08001569 DecodedInstruction dec_insn(insn);
Logan Chienfc880952012-01-15 23:53:10 +08001570
1571 llvm::BasicBlock* block_test_class =
1572 CreateBasicBlockWithDexPC(dex_pc, "test_class");
1573
1574 llvm::BasicBlock* block_test_sub_class =
1575 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
1576
1577 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001578 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chienfc880952012-01-15 23:53:10 +08001579
1580 // Test: Is the reference equal to null? Act as no-op when it is null.
1581 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
1582
1583 irb_.CreateCondBr(equal_null,
1584 GetNextBasicBlock(dex_pc),
1585 block_test_class);
1586
1587 // Test: Is the object instantiated from the given class?
1588 irb_.SetInsertPoint(block_test_class);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001589 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, dec_insn.vB);
Logan Chienfc880952012-01-15 23:53:10 +08001590 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1591
1592 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
1593
1594 llvm::Value* object_type_field_addr =
1595 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
1596
1597 llvm::Value* object_type_object_addr =
1598 irb_.CreateLoad(object_type_field_addr);
1599
1600 llvm::Value* equal_class =
1601 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
1602
1603 irb_.CreateCondBr(equal_class,
1604 GetNextBasicBlock(dex_pc),
1605 block_test_sub_class);
1606
1607 // Test: Is the object instantiated from the subclass of the given class?
1608 irb_.SetInsertPoint(block_test_sub_class);
1609
Logan Chien8dfcbea2012-02-17 18:50:32 +08001610 EmitUpdateLineNumFromDexPC(dex_pc);
1611
Logan Chienfc880952012-01-15 23:53:10 +08001612 irb_.CreateCall2(irb_.GetRuntime(CheckCast),
1613 type_object_addr, object_type_object_addr);
1614
1615 EmitGuard_ExceptionLandingPad(dex_pc);
1616
Logan Chien70f94b42011-12-27 17:49:11 +08001617 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1618}
1619
1620
1621void MethodCompiler::EmitInsn_InstanceOf(uint32_t dex_pc,
1622 Instruction const* insn) {
Logan Chien68725e22012-01-15 22:25:34 +08001623
Elliott Hughesadb8c672012-03-06 16:49:32 -08001624 DecodedInstruction dec_insn(insn);
Logan Chien68725e22012-01-15 22:25:34 +08001625
1626 llvm::Constant* zero = irb_.getJInt(0);
1627 llvm::Constant* one = irb_.getJInt(1);
1628
1629 llvm::BasicBlock* block_nullp = CreateBasicBlockWithDexPC(dex_pc, "nullp");
1630
1631 llvm::BasicBlock* block_test_class =
1632 CreateBasicBlockWithDexPC(dex_pc, "test_class");
1633
1634 llvm::BasicBlock* block_class_equals =
1635 CreateBasicBlockWithDexPC(dex_pc, "class_eq");
1636
1637 llvm::BasicBlock* block_test_sub_class =
1638 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
1639
1640 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001641 EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
Logan Chien68725e22012-01-15 22:25:34 +08001642
1643 // Overview of the following code :
1644 // We check for null, if so, then false, otherwise check for class == . If so
1645 // then true, otherwise do callout slowpath.
1646 //
1647 // Test: Is the reference equal to null? Set 0 when it is null.
1648 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
1649
1650 irb_.CreateCondBr(equal_null, block_nullp, block_test_class);
1651
1652 irb_.SetInsertPoint(block_nullp);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001653 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, zero);
Logan Chien68725e22012-01-15 22:25:34 +08001654 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1655
1656 // Test: Is the object instantiated from the given class?
1657 irb_.SetInsertPoint(block_test_class);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001658 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, dec_insn.vC);
Logan Chien68725e22012-01-15 22:25:34 +08001659 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1660
1661 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
1662
1663 llvm::Value* object_type_field_addr =
1664 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
1665
1666 llvm::Value* object_type_object_addr =
1667 irb_.CreateLoad(object_type_field_addr);
1668
1669 llvm::Value* equal_class =
1670 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
1671
1672 irb_.CreateCondBr(equal_class, block_class_equals, block_test_sub_class);
1673
1674 irb_.SetInsertPoint(block_class_equals);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001675 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, one);
Logan Chien68725e22012-01-15 22:25:34 +08001676 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1677
1678 // Test: Is the object instantiated from the subclass of the given class?
1679 irb_.SetInsertPoint(block_test_sub_class);
1680
1681 llvm::Value* result =
1682 irb_.CreateCall2(irb_.GetRuntime(IsAssignable),
1683 type_object_addr, object_type_object_addr);
1684
Elliott Hughesadb8c672012-03-06 16:49:32 -08001685 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result);
Logan Chien68725e22012-01-15 22:25:34 +08001686
Logan Chien70f94b42011-12-27 17:49:11 +08001687 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1688}
1689
1690
Logan Chien61bb6142012-02-03 15:34:53 +08001691llvm::Value* MethodCompiler::EmitLoadArrayLength(llvm::Value* array) {
1692 // Load array length field address
1693 llvm::Constant* array_len_field_offset =
1694 irb_.getPtrEquivInt(Array::LengthOffset().Int32Value());
1695
1696 llvm::Value* array_len_field_addr =
1697 irb_.CreatePtrDisp(array, array_len_field_offset,
1698 irb_.getJIntTy()->getPointerTo());
1699
1700 // Load array length
1701 return irb_.CreateLoad(array_len_field_addr);
1702}
1703
1704
Logan Chien70f94b42011-12-27 17:49:11 +08001705void MethodCompiler::EmitInsn_ArrayLength(uint32_t dex_pc,
1706 Instruction const* insn) {
Logan Chien61bb6142012-02-03 15:34:53 +08001707
Elliott Hughesadb8c672012-03-06 16:49:32 -08001708 DecodedInstruction dec_insn(insn);
Logan Chien61bb6142012-02-03 15:34:53 +08001709
1710 // Get the array object address
Elliott Hughesadb8c672012-03-06 16:49:32 -08001711 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
Logan Chien61bb6142012-02-03 15:34:53 +08001712 EmitGuard_NullPointerException(dex_pc, array_addr);
1713
1714 // Get the array length and store it to the register
1715 llvm::Value* array_len = EmitLoadArrayLength(array_addr);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001716 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, array_len);
Logan Chien61bb6142012-02-03 15:34:53 +08001717
Logan Chien70f94b42011-12-27 17:49:11 +08001718 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1719}
1720
1721
1722void MethodCompiler::EmitInsn_NewInstance(uint32_t dex_pc,
1723 Instruction const* insn) {
Logan Chien032bdad2012-01-16 09:59:23 +08001724
Elliott Hughesadb8c672012-03-06 16:49:32 -08001725 DecodedInstruction dec_insn(insn);
Logan Chien032bdad2012-01-16 09:59:23 +08001726
1727 llvm::Function* runtime_func;
Logan Chien1a032b12012-04-11 11:43:04 +08001728 if (compiler_->CanAccessInstantiableTypeWithoutChecks(
1729 method_idx_, dex_cache_, *dex_file_, dec_insn.vB)) {
Logan Chien032bdad2012-01-16 09:59:23 +08001730 runtime_func = irb_.GetRuntime(AllocObject);
1731 } else {
1732 runtime_func = irb_.GetRuntime(AllocObjectWithAccessCheck);
1733 }
1734
Elliott Hughesadb8c672012-03-06 16:49:32 -08001735 llvm::Constant* type_index_value = irb_.getInt32(dec_insn.vB);
Logan Chien032bdad2012-01-16 09:59:23 +08001736
1737 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1738
Logan Chien8dfcbea2012-02-17 18:50:32 +08001739 EmitUpdateLineNumFromDexPC(dex_pc);
1740
Logan Chien032bdad2012-01-16 09:59:23 +08001741 llvm::Value* object_addr =
1742 irb_.CreateCall2(runtime_func, type_index_value, method_object_addr);
1743
1744 EmitGuard_ExceptionLandingPad(dex_pc);
1745
Elliott Hughesadb8c672012-03-06 16:49:32 -08001746 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, object_addr);
Logan Chien032bdad2012-01-16 09:59:23 +08001747
Logan Chien70f94b42011-12-27 17:49:11 +08001748 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1749}
1750
1751
Logan Chiena2cc6a32012-01-16 10:38:41 +08001752llvm::Value* MethodCompiler::EmitAllocNewArray(uint32_t dex_pc,
1753 int32_t length,
1754 uint32_t type_idx,
1755 bool is_filled_new_array) {
1756 llvm::Function* runtime_func;
1757
1758 bool skip_access_check =
1759 compiler_->CanAccessTypeWithoutChecks(method_idx_, dex_cache_,
1760 *dex_file_, type_idx);
1761
TDYa127a849cb62012-04-01 05:59:34 -07001762 llvm::Value* array_length_value;
1763
Logan Chiena2cc6a32012-01-16 10:38:41 +08001764 if (is_filled_new_array) {
1765 runtime_func = skip_access_check ?
1766 irb_.GetRuntime(CheckAndAllocArray) :
1767 irb_.GetRuntime(CheckAndAllocArrayWithAccessCheck);
TDYa127a849cb62012-04-01 05:59:34 -07001768 array_length_value = irb_.getInt32(length);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001769 } else {
1770 runtime_func = skip_access_check ?
1771 irb_.GetRuntime(AllocArray) :
1772 irb_.GetRuntime(AllocArrayWithAccessCheck);
TDYa127a849cb62012-04-01 05:59:34 -07001773 array_length_value = EmitLoadDalvikReg(length, kInt, kAccurate);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001774 }
1775
1776 llvm::Constant* type_index_value = irb_.getInt32(type_idx);
1777
1778 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1779
Logan Chien8dfcbea2012-02-17 18:50:32 +08001780 EmitUpdateLineNumFromDexPC(dex_pc);
1781
Logan Chiena2cc6a32012-01-16 10:38:41 +08001782 llvm::Value* object_addr =
1783 irb_.CreateCall3(runtime_func, type_index_value, method_object_addr,
1784 array_length_value);
1785
1786 EmitGuard_ExceptionLandingPad(dex_pc);
1787
1788 return object_addr;
1789}
1790
1791
Logan Chien70f94b42011-12-27 17:49:11 +08001792void MethodCompiler::EmitInsn_NewArray(uint32_t dex_pc,
1793 Instruction const* insn) {
Logan Chiena2cc6a32012-01-16 10:38:41 +08001794
Elliott Hughesadb8c672012-03-06 16:49:32 -08001795 DecodedInstruction dec_insn(insn);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001796
1797 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001798 EmitAllocNewArray(dex_pc, dec_insn.vB, dec_insn.vC, false);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001799
Elliott Hughesadb8c672012-03-06 16:49:32 -08001800 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, object_addr);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001801
Logan Chien70f94b42011-12-27 17:49:11 +08001802 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1803}
1804
1805
1806void MethodCompiler::EmitInsn_FilledNewArray(uint32_t dex_pc,
1807 Instruction const* insn,
1808 bool is_range) {
Logan Chiena85fb2f2012-01-16 12:52:56 +08001809
Elliott Hughesadb8c672012-03-06 16:49:32 -08001810 DecodedInstruction dec_insn(insn);
Logan Chiena85fb2f2012-01-16 12:52:56 +08001811
1812 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001813 EmitAllocNewArray(dex_pc, dec_insn.vA, dec_insn.vB, true);
Logan Chiena85fb2f2012-01-16 12:52:56 +08001814
Elliott Hughesadb8c672012-03-06 16:49:32 -08001815 if (dec_insn.vA > 0) {
Logan Chiendd361c92012-04-10 23:40:37 +08001816 // Resolve the element type
TDYa127183cf262012-04-11 07:53:21 -07001817 Class* klass = dex_cache_->GetResolvedType(dec_insn.vB)->GetComponentType();
Logan Chiendd361c92012-04-10 23:40:37 +08001818 // TODO: Avoid the usage of the dex_cache_. Try to figure out a better
1819 // way to distinguish [I and [L.
Logan Chiena85fb2f2012-01-16 12:52:56 +08001820 CHECK_NE(klass, static_cast<Class*>(NULL));
Logan Chiena85fb2f2012-01-16 12:52:56 +08001821
Logan Chiendd361c92012-04-10 23:40:37 +08001822 uint32_t alignment;
1823 llvm::Constant* elem_size;
1824 llvm::PointerType* field_type;
Logan Chiena85fb2f2012-01-16 12:52:56 +08001825
Logan Chiendd361c92012-04-10 23:40:37 +08001826 // NOTE: Currently filled-new-array only supports 'L', '[', and 'I'
1827 // as the element, thus we are only checking 2 cases: primitive int and
1828 // non-primitive type.
Logan Chiena85fb2f2012-01-16 12:52:56 +08001829 if (klass->IsPrimitiveInt()) {
Logan Chiendd361c92012-04-10 23:40:37 +08001830 alignment = sizeof(int32_t);
1831 elem_size = irb_.getPtrEquivInt(sizeof(int32_t));
Logan Chiena85fb2f2012-01-16 12:52:56 +08001832 field_type = irb_.getJIntTy()->getPointerTo();
1833 } else {
1834 CHECK(!klass->IsPrimitive());
Logan Chiendd361c92012-04-10 23:40:37 +08001835 alignment = irb_.getSizeOfPtrEquivInt();
1836 elem_size = irb_.getSizeOfPtrEquivIntValue();
Logan Chiena85fb2f2012-01-16 12:52:56 +08001837 field_type = irb_.getJObjectTy()->getPointerTo();
1838 }
1839
Logan Chiendd361c92012-04-10 23:40:37 +08001840 llvm::Value* data_field_offset =
1841 irb_.getPtrEquivInt(Array::DataOffset(alignment).Int32Value());
1842
1843 llvm::Value* data_field_addr =
1844 irb_.CreatePtrDisp(object_addr, data_field_offset, field_type);
1845
Logan Chiena85fb2f2012-01-16 12:52:56 +08001846 // TODO: Tune this code. Currently we are generating one instruction for
1847 // one element which may be very space consuming. Maybe changing to use
1848 // memcpy may help; however, since we can't guarantee that the alloca of
1849 // dalvik register are continuous, we can't perform such optimization yet.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001850 for (uint32_t i = 0; i < dec_insn.vA; ++i) {
Logan Chiena85fb2f2012-01-16 12:52:56 +08001851 int reg_index;
1852 if (is_range) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001853 reg_index = dec_insn.vC + i;
Logan Chiena85fb2f2012-01-16 12:52:56 +08001854 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001855 reg_index = dec_insn.arg[i];
Logan Chiena85fb2f2012-01-16 12:52:56 +08001856 }
1857
1858 llvm::Value* reg_value;
1859 if (klass->IsPrimitiveInt()) {
1860 reg_value = EmitLoadDalvikReg(reg_index, kInt, kAccurate);
1861 } else {
1862 reg_value = EmitLoadDalvikReg(reg_index, kObject, kAccurate);
1863 }
1864
1865 irb_.CreateStore(reg_value, data_field_addr);
1866
Logan Chiendd361c92012-04-10 23:40:37 +08001867 data_field_addr =
1868 irb_.CreatePtrDisp(data_field_addr, elem_size, field_type);
Logan Chiena85fb2f2012-01-16 12:52:56 +08001869 }
1870 }
1871
1872 EmitStoreDalvikRetValReg(kObject, kAccurate, object_addr);
1873
Logan Chien70f94b42011-12-27 17:49:11 +08001874 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1875}
1876
1877
1878void MethodCompiler::EmitInsn_FillArrayData(uint32_t dex_pc,
1879 Instruction const* insn) {
Logan Chiene58b6582012-01-16 17:13:13 +08001880
Elliott Hughesadb8c672012-03-06 16:49:32 -08001881 DecodedInstruction dec_insn(insn);
Logan Chiene58b6582012-01-16 17:13:13 +08001882
1883 // Read the payload
1884 struct PACKED Payload {
1885 uint16_t ident_;
1886 uint16_t elem_width_;
1887 uint32_t num_elems_;
1888 uint8_t data_[];
1889 };
1890
1891 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
Elliott Hughesadb8c672012-03-06 16:49:32 -08001892 static_cast<int32_t>(dec_insn.vB);
Logan Chiene58b6582012-01-16 17:13:13 +08001893
1894 Payload const* payload =
1895 reinterpret_cast<Payload const*>(code_item_->insns_ + payload_offset);
1896
1897 uint32_t size_in_bytes = payload->elem_width_ * payload->num_elems_;
1898
1899 // Load and check the array
Elliott Hughesadb8c672012-03-06 16:49:32 -08001900 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiene58b6582012-01-16 17:13:13 +08001901
1902 EmitGuard_NullPointerException(dex_pc, array_addr);
1903
1904 if (payload->num_elems_ > 0) {
1905 // Test: Is array length big enough?
1906 llvm::Constant* last_index = irb_.getJInt(payload->num_elems_ - 1);
1907
1908 EmitGuard_ArrayIndexOutOfBoundsException(dex_pc, array_addr, last_index);
1909
1910 // Get array data field
1911 llvm::Value* data_field_offset_value =
TDYa127b77799d2012-04-06 22:16:31 -07001912 irb_.getPtrEquivInt(Array::DataOffset(payload->elem_width_).Int32Value());
Logan Chiene58b6582012-01-16 17:13:13 +08001913
1914 llvm::Value* data_field_addr =
1915 irb_.CreatePtrDisp(array_addr, data_field_offset_value,
1916 irb_.getInt8Ty()->getPointerTo());
1917
1918 // Emit payload to bitcode constant pool
1919 std::vector<llvm::Constant*> const_pool_data;
1920 for (uint32_t i = 0; i < size_in_bytes; ++i) {
1921 const_pool_data.push_back(irb_.getInt8(payload->data_[i]));
1922 }
1923
1924 llvm::Constant* const_pool_data_array_value = llvm::ConstantArray::get(
1925 llvm::ArrayType::get(irb_.getInt8Ty(), size_in_bytes), const_pool_data);
1926
1927 llvm::Value* const_pool_data_array_addr =
1928 new llvm::GlobalVariable(*module_,
1929 const_pool_data_array_value->getType(),
1930 false, llvm::GlobalVariable::InternalLinkage,
1931 const_pool_data_array_value,
1932 "array_data_payload");
1933
1934 // Find the memcpy intrinsic
1935 llvm::Type* memcpy_arg_types[] = {
1936 llvm::Type::getInt8Ty(*context_)->getPointerTo(),
1937 llvm::Type::getInt8Ty(*context_)->getPointerTo(),
1938 llvm::Type::getInt32Ty(*context_)
1939 };
1940
1941 llvm::Function* memcpy_intrinsic =
1942 llvm::Intrinsic::getDeclaration(module_,
1943 llvm::Intrinsic::memcpy,
1944 memcpy_arg_types);
1945
1946 // Copy now!
1947 llvm::Value *args[] = {
1948 data_field_addr,
1949 irb_.CreateConstGEP2_32(const_pool_data_array_addr, 0, 0),
1950 irb_.getInt32(size_in_bytes),
1951 irb_.getInt32(0), // alignment: no guarantee
1952 irb_.getFalse() // is_volatile: false
1953 };
1954
1955 irb_.CreateCall(memcpy_intrinsic, args);
1956 }
1957
Logan Chien70f94b42011-12-27 17:49:11 +08001958 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1959}
1960
1961
1962void MethodCompiler::EmitInsn_UnconditionalBranch(uint32_t dex_pc,
1963 Instruction const* insn) {
Logan Chiena466c162011-12-27 17:55:46 +08001964
Elliott Hughesadb8c672012-03-06 16:49:32 -08001965 DecodedInstruction dec_insn(insn);
Logan Chiena466c162011-12-27 17:55:46 +08001966
Elliott Hughesadb8c672012-03-06 16:49:32 -08001967 int32_t branch_offset = dec_insn.vA;
Logan Chiena466c162011-12-27 17:55:46 +08001968
1969 if (branch_offset <= 0) {
1970 // Garbage collection safe-point on backward branch
1971 EmitGuard_GarbageCollectionSuspend(dex_pc);
1972 }
1973
1974 irb_.CreateBr(GetBasicBlock(dex_pc + branch_offset));
Logan Chien70f94b42011-12-27 17:49:11 +08001975}
1976
1977
1978void MethodCompiler::EmitInsn_PackedSwitch(uint32_t dex_pc,
1979 Instruction const* insn) {
Logan Chien7a89b6d2011-12-27 17:56:56 +08001980
Elliott Hughesadb8c672012-03-06 16:49:32 -08001981 DecodedInstruction dec_insn(insn);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001982
1983 struct PACKED Payload {
1984 uint16_t ident_;
1985 uint16_t num_cases_;
1986 int32_t first_key_;
1987 int32_t targets_[];
1988 };
1989
1990 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
Elliott Hughesadb8c672012-03-06 16:49:32 -08001991 static_cast<int32_t>(dec_insn.vB);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001992
1993 Payload const* payload =
1994 reinterpret_cast<Payload const*>(code_item_->insns_ + payload_offset);
1995
Elliott Hughesadb8c672012-03-06 16:49:32 -08001996 llvm::Value* value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001997
1998 llvm::SwitchInst* sw =
1999 irb_.CreateSwitch(value, GetNextBasicBlock(dex_pc), payload->num_cases_);
2000
2001 for (uint16_t i = 0; i < payload->num_cases_; ++i) {
2002 sw->addCase(irb_.getInt32(payload->first_key_ + i),
2003 GetBasicBlock(dex_pc + payload->targets_[i]));
2004 }
Logan Chien70f94b42011-12-27 17:49:11 +08002005}
2006
2007
2008void MethodCompiler::EmitInsn_SparseSwitch(uint32_t dex_pc,
2009 Instruction const* insn) {
Logan Chien7a89b6d2011-12-27 17:56:56 +08002010
Elliott Hughesadb8c672012-03-06 16:49:32 -08002011 DecodedInstruction dec_insn(insn);
Logan Chien7a89b6d2011-12-27 17:56:56 +08002012
2013 struct PACKED Payload {
2014 uint16_t ident_;
2015 uint16_t num_cases_;
2016 int32_t keys_and_targets_[];
2017 };
2018
2019 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
Elliott Hughesadb8c672012-03-06 16:49:32 -08002020 static_cast<int32_t>(dec_insn.vB);
Logan Chien7a89b6d2011-12-27 17:56:56 +08002021
2022 Payload const* payload =
2023 reinterpret_cast<Payload const*>(code_item_->insns_ + payload_offset);
2024
2025 int32_t const* keys = payload->keys_and_targets_;
2026 int32_t const* targets = payload->keys_and_targets_ + payload->num_cases_;
2027
Elliott Hughesadb8c672012-03-06 16:49:32 -08002028 llvm::Value* value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chien7a89b6d2011-12-27 17:56:56 +08002029
2030 llvm::SwitchInst* sw =
2031 irb_.CreateSwitch(value, GetNextBasicBlock(dex_pc), payload->num_cases_);
2032
2033 for (size_t i = 0; i < payload->num_cases_; ++i) {
2034 sw->addCase(irb_.getInt32(keys[i]), GetBasicBlock(dex_pc + targets[i]));
2035 }
Logan Chien70f94b42011-12-27 17:49:11 +08002036}
2037
2038
2039void MethodCompiler::EmitInsn_FPCompare(uint32_t dex_pc,
2040 Instruction const* insn,
2041 JType fp_jty,
2042 bool gt_bias) {
Logan Chien2c37e8e2011-12-27 17:58:46 +08002043
Elliott Hughesadb8c672012-03-06 16:49:32 -08002044 DecodedInstruction dec_insn(insn);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002045
2046 DCHECK(fp_jty == kFloat || fp_jty == kDouble) << "JType: " << fp_jty;
2047
Elliott Hughesadb8c672012-03-06 16:49:32 -08002048 llvm::Value* src1_value = EmitLoadDalvikReg(dec_insn.vB, fp_jty, kAccurate);
2049 llvm::Value* src2_value = EmitLoadDalvikReg(dec_insn.vC, fp_jty, kAccurate);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002050
2051 llvm::Value* cmp_eq = irb_.CreateFCmpOEQ(src1_value, src2_value);
2052 llvm::Value* cmp_lt;
2053
2054 if (gt_bias) {
2055 cmp_lt = irb_.CreateFCmpOLT(src1_value, src2_value);
2056 } else {
2057 cmp_lt = irb_.CreateFCmpULT(src1_value, src2_value);
2058 }
2059
2060 llvm::Value* result = EmitCompareResultSelection(cmp_eq, cmp_lt);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002061 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002062
Logan Chien70f94b42011-12-27 17:49:11 +08002063 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2064}
2065
2066
2067void MethodCompiler::EmitInsn_LongCompare(uint32_t dex_pc,
2068 Instruction const* insn) {
Logan Chien2c37e8e2011-12-27 17:58:46 +08002069
Elliott Hughesadb8c672012-03-06 16:49:32 -08002070 DecodedInstruction dec_insn(insn);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002071
Elliott Hughesadb8c672012-03-06 16:49:32 -08002072 llvm::Value* src1_value = EmitLoadDalvikReg(dec_insn.vB, kLong, kAccurate);
2073 llvm::Value* src2_value = EmitLoadDalvikReg(dec_insn.vC, kLong, kAccurate);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002074
2075 llvm::Value* cmp_eq = irb_.CreateICmpEQ(src1_value, src2_value);
2076 llvm::Value* cmp_lt = irb_.CreateICmpSLT(src1_value, src2_value);
2077
2078 llvm::Value* result = EmitCompareResultSelection(cmp_eq, cmp_lt);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002079 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002080
Logan Chien70f94b42011-12-27 17:49:11 +08002081 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2082}
2083
2084
Logan Chien2c37e8e2011-12-27 17:58:46 +08002085llvm::Value* MethodCompiler::EmitCompareResultSelection(llvm::Value* cmp_eq,
2086 llvm::Value* cmp_lt) {
2087
2088 llvm::Constant* zero = irb_.getJInt(0);
2089 llvm::Constant* pos1 = irb_.getJInt(1);
2090 llvm::Constant* neg1 = irb_.getJInt(-1);
2091
2092 llvm::Value* result_lt = irb_.CreateSelect(cmp_lt, neg1, pos1);
2093 llvm::Value* result_eq = irb_.CreateSelect(cmp_eq, zero, result_lt);
2094
2095 return result_eq;
2096}
2097
2098
Logan Chien70f94b42011-12-27 17:49:11 +08002099void MethodCompiler::EmitInsn_BinaryConditionalBranch(uint32_t dex_pc,
2100 Instruction const* insn,
2101 CondBranchKind cond) {
Logan Chiena78e3c82011-12-27 17:59:35 +08002102
Elliott Hughesadb8c672012-03-06 16:49:32 -08002103 DecodedInstruction dec_insn(insn);
Logan Chiena78e3c82011-12-27 17:59:35 +08002104
Elliott Hughesadb8c672012-03-06 16:49:32 -08002105 int8_t src1_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vA);
2106 int8_t src2_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vB);
Logan Chiena78e3c82011-12-27 17:59:35 +08002107
2108 DCHECK_NE(kRegUnknown, src1_reg_cat);
2109 DCHECK_NE(kRegUnknown, src2_reg_cat);
2110 DCHECK_NE(kRegCat2, src1_reg_cat);
2111 DCHECK_NE(kRegCat2, src2_reg_cat);
2112
Elliott Hughesadb8c672012-03-06 16:49:32 -08002113 int32_t branch_offset = dec_insn.vC;
Logan Chiena78e3c82011-12-27 17:59:35 +08002114
2115 if (branch_offset <= 0) {
2116 // Garbage collection safe-point on backward branch
2117 EmitGuard_GarbageCollectionSuspend(dex_pc);
2118 }
2119
2120 if (src1_reg_cat == kRegZero && src2_reg_cat == kRegZero) {
2121 irb_.CreateBr(GetBasicBlock(dex_pc + branch_offset));
2122 return;
2123 }
2124
2125 llvm::Value* src1_value;
2126 llvm::Value* src2_value;
2127
2128 if (src1_reg_cat != kRegZero && src2_reg_cat != kRegZero) {
2129 CHECK_EQ(src1_reg_cat, src2_reg_cat);
2130
2131 if (src1_reg_cat == kRegCat1nr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002132 src1_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
2133 src2_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002134 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002135 src1_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
2136 src2_value = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002137 }
2138 } else {
2139 DCHECK(src1_reg_cat == kRegZero ||
2140 src2_reg_cat == kRegZero);
2141
2142 if (src1_reg_cat == kRegZero) {
2143 if (src2_reg_cat == kRegCat1nr) {
2144 src1_value = irb_.getJInt(0);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002145 src2_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002146 } else {
2147 src1_value = irb_.getJNull();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002148 src2_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002149 }
2150 } else { // src2_reg_cat == kRegZero
2151 if (src2_reg_cat == kRegCat1nr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002152 src1_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002153 src2_value = irb_.getJInt(0);
2154 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002155 src1_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002156 src2_value = irb_.getJNull();
2157 }
2158 }
2159 }
2160
2161 llvm::Value* cond_value =
2162 EmitConditionResult(src1_value, src2_value, cond);
2163
2164 irb_.CreateCondBr(cond_value,
2165 GetBasicBlock(dex_pc + branch_offset),
2166 GetNextBasicBlock(dex_pc));
Logan Chien70f94b42011-12-27 17:49:11 +08002167}
2168
2169
2170void MethodCompiler::EmitInsn_UnaryConditionalBranch(uint32_t dex_pc,
2171 Instruction const* insn,
2172 CondBranchKind cond) {
Logan Chiena78e3c82011-12-27 17:59:35 +08002173
Elliott Hughesadb8c672012-03-06 16:49:32 -08002174 DecodedInstruction dec_insn(insn);
Logan Chiena78e3c82011-12-27 17:59:35 +08002175
Elliott Hughesadb8c672012-03-06 16:49:32 -08002176 int8_t src_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vA);
Logan Chiena78e3c82011-12-27 17:59:35 +08002177
2178 DCHECK_NE(kRegUnknown, src_reg_cat);
2179 DCHECK_NE(kRegCat2, src_reg_cat);
2180
Elliott Hughesadb8c672012-03-06 16:49:32 -08002181 int32_t branch_offset = dec_insn.vB;
Logan Chiena78e3c82011-12-27 17:59:35 +08002182
2183 if (branch_offset <= 0) {
2184 // Garbage collection safe-point on backward branch
2185 EmitGuard_GarbageCollectionSuspend(dex_pc);
2186 }
2187
2188 if (src_reg_cat == kRegZero) {
2189 irb_.CreateBr(GetBasicBlock(dex_pc + branch_offset));
2190 return;
2191 }
2192
2193 llvm::Value* src1_value;
2194 llvm::Value* src2_value;
2195
2196 if (src_reg_cat == kRegCat1nr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002197 src1_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002198 src2_value = irb_.getInt32(0);
2199 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002200 src1_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002201 src2_value = irb_.getJNull();
2202 }
2203
2204 llvm::Value* cond_value =
2205 EmitConditionResult(src1_value, src2_value, cond);
2206
2207 irb_.CreateCondBr(cond_value,
2208 GetBasicBlock(dex_pc + branch_offset),
2209 GetNextBasicBlock(dex_pc));
2210}
2211
2212
2213RegCategory MethodCompiler::GetInferredRegCategory(uint32_t dex_pc,
2214 uint16_t reg_idx) {
Logan Chiendd361c92012-04-10 23:40:37 +08002215
2216 Compiler::MethodReference mref(dex_file_, method_idx_);
2217
2218 InferredRegCategoryMap const* map =
Ian Rogers776ac1f2012-04-13 23:36:36 -07002219 verifier::MethodVerifier::GetInferredRegCategoryMap(mref);
Logan Chiendd361c92012-04-10 23:40:37 +08002220
Logan Chiena78e3c82011-12-27 17:59:35 +08002221 CHECK_NE(map, static_cast<InferredRegCategoryMap*>(NULL));
2222
2223 return map->GetRegCategory(dex_pc, reg_idx);
2224}
2225
2226
2227llvm::Value* MethodCompiler::EmitConditionResult(llvm::Value* lhs,
2228 llvm::Value* rhs,
2229 CondBranchKind cond) {
2230 switch (cond) {
2231 case kCondBranch_EQ:
2232 return irb_.CreateICmpEQ(lhs, rhs);
2233
2234 case kCondBranch_NE:
2235 return irb_.CreateICmpNE(lhs, rhs);
2236
2237 case kCondBranch_LT:
2238 return irb_.CreateICmpSLT(lhs, rhs);
2239
2240 case kCondBranch_GE:
2241 return irb_.CreateICmpSGE(lhs, rhs);
2242
2243 case kCondBranch_GT:
2244 return irb_.CreateICmpSGT(lhs, rhs);
2245
2246 case kCondBranch_LE:
2247 return irb_.CreateICmpSLE(lhs, rhs);
2248
2249 default: // Unreachable
2250 LOG(FATAL) << "Unknown conditional branch kind: " << cond;
2251 return NULL;
2252 }
Logan Chien70f94b42011-12-27 17:49:11 +08002253}
2254
2255
Logan Chiene27fdbb2012-01-02 23:27:26 +08002256void
2257MethodCompiler::EmitGuard_ArrayIndexOutOfBoundsException(uint32_t dex_pc,
2258 llvm::Value* array,
2259 llvm::Value* index) {
2260 llvm::Value* array_len = EmitLoadArrayLength(array);
2261
2262 llvm::Value* cmp = irb_.CreateICmpUGE(index, array_len);
2263
2264 llvm::BasicBlock* block_exception =
2265 CreateBasicBlockWithDexPC(dex_pc, "overflow");
2266
2267 llvm::BasicBlock* block_continue =
2268 CreateBasicBlockWithDexPC(dex_pc, "cont");
2269
2270 irb_.CreateCondBr(cmp, block_exception, block_continue);
2271
2272 irb_.SetInsertPoint(block_exception);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002273
2274 EmitUpdateLineNumFromDexPC(dex_pc);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002275 irb_.CreateCall2(irb_.GetRuntime(ThrowIndexOutOfBounds), index, array_len);
2276 EmitBranchExceptionLandingPad(dex_pc);
2277
2278 irb_.SetInsertPoint(block_continue);
2279}
2280
2281
2282void MethodCompiler::EmitGuard_ArrayException(uint32_t dex_pc,
2283 llvm::Value* array,
2284 llvm::Value* index) {
2285 EmitGuard_NullPointerException(dex_pc, array);
2286 EmitGuard_ArrayIndexOutOfBoundsException(dex_pc, array, index);
2287}
2288
2289
2290// Emit Array GetElementPtr
2291llvm::Value* MethodCompiler::EmitArrayGEP(llvm::Value* array_addr,
2292 llvm::Value* index_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08002293 llvm::Type* elem_type,
2294 JType elem_jty) {
2295
2296 int data_offset;
2297 if (elem_jty == kLong || elem_jty == kDouble ||
2298 (elem_jty == kObject && sizeof(uint64_t) == sizeof(Object*))) {
2299 data_offset = Array::DataOffset(sizeof(int64_t)).Int32Value();
2300 } else {
2301 data_offset = Array::DataOffset(sizeof(int32_t)).Int32Value();
2302 }
Logan Chiene27fdbb2012-01-02 23:27:26 +08002303
2304 llvm::Constant* data_offset_value =
Ian Rogers04ec04e2012-02-28 16:15:33 -08002305 irb_.getPtrEquivInt(data_offset);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002306
2307 llvm::Value* array_data_addr =
2308 irb_.CreatePtrDisp(array_addr, data_offset_value,
2309 elem_type->getPointerTo());
2310
2311 return irb_.CreateGEP(array_data_addr, index_value);
2312}
2313
2314
Logan Chien70f94b42011-12-27 17:49:11 +08002315void MethodCompiler::EmitInsn_AGet(uint32_t dex_pc,
2316 Instruction const* insn,
2317 JType elem_jty) {
Logan Chiene27fdbb2012-01-02 23:27:26 +08002318
Elliott Hughesadb8c672012-03-06 16:49:32 -08002319 DecodedInstruction dec_insn(insn);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002320
Elliott Hughesadb8c672012-03-06 16:49:32 -08002321 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
2322 llvm::Value* index_value = EmitLoadDalvikReg(dec_insn.vC, kInt, kAccurate);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002323
2324 EmitGuard_ArrayException(dex_pc, array_addr, index_value);
2325
2326 llvm::Type* elem_type = irb_.getJType(elem_jty, kArray);
2327
2328 llvm::Value* array_elem_addr =
Ian Rogers04ec04e2012-02-28 16:15:33 -08002329 EmitArrayGEP(array_addr, index_value, elem_type, elem_jty);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002330
2331 llvm::Value* array_elem_value = irb_.CreateLoad(array_elem_addr);
2332
Elliott Hughesadb8c672012-03-06 16:49:32 -08002333 EmitStoreDalvikReg(dec_insn.vA, elem_jty, kArray, array_elem_value);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002334
Logan Chien70f94b42011-12-27 17:49:11 +08002335 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2336}
2337
2338
2339void MethodCompiler::EmitInsn_APut(uint32_t dex_pc,
2340 Instruction const* insn,
2341 JType elem_jty) {
Logan Chien8dabb432012-01-02 23:29:32 +08002342
Elliott Hughesadb8c672012-03-06 16:49:32 -08002343 DecodedInstruction dec_insn(insn);
Logan Chien8dabb432012-01-02 23:29:32 +08002344
Elliott Hughesadb8c672012-03-06 16:49:32 -08002345 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
2346 llvm::Value* index_value = EmitLoadDalvikReg(dec_insn.vC, kInt, kAccurate);
Logan Chien8dabb432012-01-02 23:29:32 +08002347
2348 EmitGuard_ArrayException(dex_pc, array_addr, index_value);
2349
2350 llvm::Type* elem_type = irb_.getJType(elem_jty, kArray);
2351
2352 llvm::Value* array_elem_addr =
Ian Rogers04ec04e2012-02-28 16:15:33 -08002353 EmitArrayGEP(array_addr, index_value, elem_type, elem_jty);
Logan Chien8dabb432012-01-02 23:29:32 +08002354
Elliott Hughesadb8c672012-03-06 16:49:32 -08002355 llvm::Value* new_value = EmitLoadDalvikReg(dec_insn.vA, elem_jty, kArray);
Logan Chien8dabb432012-01-02 23:29:32 +08002356
TDYa1271b86d072012-04-05 17:38:56 -07002357 if (elem_jty == kObject) { // If put an object, check the type.
2358 llvm::Function* runtime_func = irb_.GetRuntime(CheckPutArrayElement);
2359
2360 irb_.CreateCall2(runtime_func, new_value, array_addr);
2361
2362 EmitGuard_ExceptionLandingPad(dex_pc);
2363 }
2364
Logan Chien8dabb432012-01-02 23:29:32 +08002365 irb_.CreateStore(new_value, array_elem_addr);
2366
Logan Chien70f94b42011-12-27 17:49:11 +08002367 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2368}
2369
2370
2371void MethodCompiler::EmitInsn_IGet(uint32_t dex_pc,
2372 Instruction const* insn,
2373 JType field_jty) {
Logan Chien48f1d2a2012-01-02 22:49:53 +08002374
Elliott Hughesadb8c672012-03-06 16:49:32 -08002375 DecodedInstruction dec_insn(insn);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002376
Elliott Hughesadb8c672012-03-06 16:49:32 -08002377 uint32_t reg_idx = dec_insn.vB;
2378 uint32_t field_idx = dec_insn.vC;
Logan Chien48f1d2a2012-01-02 22:49:53 +08002379
Logan Chien48f1d2a2012-01-02 22:49:53 +08002380 llvm::Value* object_addr = EmitLoadDalvikReg(reg_idx, kObject, kAccurate);
2381
2382 EmitGuard_NullPointerException(dex_pc, object_addr);
2383
2384 llvm::Value* field_value;
2385
Logan Chien933abf82012-04-11 12:24:31 +08002386 int field_offset;
2387 bool is_volatile;
2388 bool is_fast_path = compiler_->ComputeInstanceFieldInfo(
2389 field_idx, oat_compilation_unit_, field_offset, is_volatile, false);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002390
Logan Chien933abf82012-04-11 12:24:31 +08002391 if (!is_fast_path) {
Logan Chien48f1d2a2012-01-02 22:49:53 +08002392 llvm::Function* runtime_func;
2393
2394 if (field_jty == kObject) {
Logan Chien3b2b2e72012-03-06 16:11:45 +08002395 runtime_func = irb_.GetRuntime(GetObjectInstance);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002396 } else if (field_jty == kLong || field_jty == kDouble) {
Logan Chien3b2b2e72012-03-06 16:11:45 +08002397 runtime_func = irb_.GetRuntime(Get64Instance);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002398 } else {
Logan Chien3b2b2e72012-03-06 16:11:45 +08002399 runtime_func = irb_.GetRuntime(Get32Instance);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002400 }
2401
2402 llvm::ConstantInt* field_idx_value = irb_.getInt32(field_idx);
2403
2404 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2405
Logan Chien8dfcbea2012-02-17 18:50:32 +08002406 EmitUpdateLineNumFromDexPC(dex_pc);
2407
Logan Chien3b2b2e72012-03-06 16:11:45 +08002408 field_value = irb_.CreateCall3(runtime_func, field_idx_value,
2409 method_object_addr, object_addr);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002410
2411 EmitGuard_ExceptionLandingPad(dex_pc);
2412
2413 } else {
Logan Chien933abf82012-04-11 12:24:31 +08002414 DCHECK_GE(field_offset, 0);
2415
Logan Chien48f1d2a2012-01-02 22:49:53 +08002416 llvm::PointerType* field_type =
2417 irb_.getJType(field_jty, kField)->getPointerTo();
2418
Logan Chien933abf82012-04-11 12:24:31 +08002419 llvm::ConstantInt* field_offset_value = irb_.getPtrEquivInt(field_offset);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002420
2421 llvm::Value* field_addr =
Logan Chien933abf82012-04-11 12:24:31 +08002422 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002423
Logan Chien933abf82012-04-11 12:24:31 +08002424 // TODO: Check is_volatile. We need to generate atomic load instruction
2425 // when is_volatile is true.
Logan Chien48f1d2a2012-01-02 22:49:53 +08002426 field_value = irb_.CreateLoad(field_addr);
2427 }
2428
Elliott Hughesadb8c672012-03-06 16:49:32 -08002429 EmitStoreDalvikReg(dec_insn.vA, field_jty, kField, field_value);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002430
Logan Chien70f94b42011-12-27 17:49:11 +08002431 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2432}
2433
2434
2435void MethodCompiler::EmitInsn_IPut(uint32_t dex_pc,
2436 Instruction const* insn,
2437 JType field_jty) {
Logan Chiendd6aa872012-01-03 16:06:32 +08002438
Elliott Hughesadb8c672012-03-06 16:49:32 -08002439 DecodedInstruction dec_insn(insn);
Logan Chiendd6aa872012-01-03 16:06:32 +08002440
Elliott Hughesadb8c672012-03-06 16:49:32 -08002441 uint32_t reg_idx = dec_insn.vB;
2442 uint32_t field_idx = dec_insn.vC;
Logan Chiendd6aa872012-01-03 16:06:32 +08002443
Logan Chiendd6aa872012-01-03 16:06:32 +08002444 llvm::Value* object_addr = EmitLoadDalvikReg(reg_idx, kObject, kAccurate);
2445
2446 EmitGuard_NullPointerException(dex_pc, object_addr);
2447
Elliott Hughesadb8c672012-03-06 16:49:32 -08002448 llvm::Value* new_value = EmitLoadDalvikReg(dec_insn.vA, field_jty, kField);
Logan Chiendd6aa872012-01-03 16:06:32 +08002449
Logan Chien933abf82012-04-11 12:24:31 +08002450 int field_offset;
2451 bool is_volatile;
2452 bool is_fast_path = compiler_->ComputeInstanceFieldInfo(
2453 field_idx, oat_compilation_unit_, field_offset, is_volatile, true);
Logan Chiendd6aa872012-01-03 16:06:32 +08002454
Logan Chien933abf82012-04-11 12:24:31 +08002455 if (!is_fast_path) {
Logan Chiendd6aa872012-01-03 16:06:32 +08002456 llvm::Function* runtime_func;
2457
2458 if (field_jty == kObject) {
2459 runtime_func = irb_.GetRuntime(SetObjectInstance);
2460 } else if (field_jty == kLong || field_jty == kDouble) {
2461 runtime_func = irb_.GetRuntime(Set64Instance);
2462 } else {
2463 runtime_func = irb_.GetRuntime(Set32Instance);
2464 }
2465
2466 llvm::Value* field_idx_value = irb_.getInt32(field_idx);
2467
2468 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2469
Logan Chien8dfcbea2012-02-17 18:50:32 +08002470 EmitUpdateLineNumFromDexPC(dex_pc);
2471
Logan Chien3b2b2e72012-03-06 16:11:45 +08002472 irb_.CreateCall4(runtime_func, field_idx_value,
2473 method_object_addr, object_addr, new_value);
Logan Chiendd6aa872012-01-03 16:06:32 +08002474
2475 EmitGuard_ExceptionLandingPad(dex_pc);
2476
2477 } else {
Logan Chien933abf82012-04-11 12:24:31 +08002478 DCHECK_GE(field_offset, 0);
2479
Logan Chiendd6aa872012-01-03 16:06:32 +08002480 llvm::PointerType* field_type =
2481 irb_.getJType(field_jty, kField)->getPointerTo();
2482
Logan Chien933abf82012-04-11 12:24:31 +08002483 llvm::Value* field_offset_value = irb_.getPtrEquivInt(field_offset);
Logan Chiendd6aa872012-01-03 16:06:32 +08002484
2485 llvm::Value* field_addr =
Logan Chien933abf82012-04-11 12:24:31 +08002486 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
Logan Chiendd6aa872012-01-03 16:06:32 +08002487
Logan Chien933abf82012-04-11 12:24:31 +08002488 // TODO: Check is_volatile. We need to generate atomic store instruction
2489 // when is_volatile is true.
Logan Chiendd6aa872012-01-03 16:06:32 +08002490 irb_.CreateStore(new_value, field_addr);
2491 }
2492
Logan Chien70f94b42011-12-27 17:49:11 +08002493 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2494}
2495
2496
Logan Chien438c4b62012-01-17 16:06:00 +08002497llvm::Value* MethodCompiler::EmitLoadStaticStorage(uint32_t dex_pc,
2498 uint32_t type_idx) {
2499 llvm::BasicBlock* block_load_static =
2500 CreateBasicBlockWithDexPC(dex_pc, "load_static");
2501
2502 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
2503
2504 // Load static storage from dex cache
2505 llvm::Value* storage_field_addr =
2506 EmitLoadDexCacheStaticStorageFieldAddr(type_idx);
2507
2508 llvm::Value* storage_object_addr = irb_.CreateLoad(storage_field_addr);
2509
2510 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
2511
2512 // Test: Is the static storage of this class initialized?
2513 llvm::Value* equal_null =
2514 irb_.CreateICmpEQ(storage_object_addr, irb_.getJNull());
2515
2516 irb_.CreateCondBr(equal_null, block_load_static, block_cont);
2517
2518 // Failback routine to load the class object
2519 irb_.SetInsertPoint(block_load_static);
2520
2521 llvm::Function* runtime_func =
2522 irb_.GetRuntime(InitializeStaticStorage);
2523
2524 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
2525
2526 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2527
Logan Chien8dfcbea2012-02-17 18:50:32 +08002528 EmitUpdateLineNumFromDexPC(dex_pc);
2529
Logan Chien438c4b62012-01-17 16:06:00 +08002530 llvm::Value* loaded_storage_object_addr =
2531 irb_.CreateCall2(runtime_func, type_idx_value, method_object_addr);
2532
2533 EmitGuard_ExceptionLandingPad(dex_pc);
2534
2535 llvm::BasicBlock* block_after_load_static = irb_.GetInsertBlock();
2536
2537 irb_.CreateBr(block_cont);
2538
2539 // Now the class object must be loaded
2540 irb_.SetInsertPoint(block_cont);
2541
2542 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
2543
2544 phi->addIncoming(storage_object_addr, block_original);
2545 phi->addIncoming(loaded_storage_object_addr, block_after_load_static);
2546
2547 return phi;
2548}
2549
2550
Logan Chien70f94b42011-12-27 17:49:11 +08002551void MethodCompiler::EmitInsn_SGet(uint32_t dex_pc,
2552 Instruction const* insn,
2553 JType field_jty) {
Logan Chien438c4b62012-01-17 16:06:00 +08002554
Elliott Hughesadb8c672012-03-06 16:49:32 -08002555 DecodedInstruction dec_insn(insn);
Logan Chien438c4b62012-01-17 16:06:00 +08002556
Logan Chien933abf82012-04-11 12:24:31 +08002557 uint32_t field_idx = dec_insn.vB;
Logan Chien438c4b62012-01-17 16:06:00 +08002558
Logan Chien933abf82012-04-11 12:24:31 +08002559 int field_offset;
2560 int ssb_index;
2561 bool is_referrers_class;
2562 bool is_volatile;
2563
2564 bool is_fast_path = compiler_->ComputeStaticFieldInfo(
2565 field_idx, oat_compilation_unit_, field_offset, ssb_index,
2566 is_referrers_class, is_volatile, false);
Logan Chien438c4b62012-01-17 16:06:00 +08002567
2568 llvm::Value* static_field_value;
2569
Logan Chien933abf82012-04-11 12:24:31 +08002570 if (!is_fast_path) {
Logan Chien438c4b62012-01-17 16:06:00 +08002571 llvm::Function* runtime_func;
2572
2573 if (field_jty == kObject) {
2574 runtime_func = irb_.GetRuntime(GetObjectStatic);
2575 } else if (field_jty == kLong || field_jty == kDouble) {
2576 runtime_func = irb_.GetRuntime(Get64Static);
2577 } else {
2578 runtime_func = irb_.GetRuntime(Get32Static);
2579 }
2580
Elliott Hughesadb8c672012-03-06 16:49:32 -08002581 llvm::Constant* field_idx_value = irb_.getInt32(dec_insn.vB);
Logan Chien438c4b62012-01-17 16:06:00 +08002582
2583 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2584
Logan Chien8dfcbea2012-02-17 18:50:32 +08002585 EmitUpdateLineNumFromDexPC(dex_pc);
2586
Logan Chien438c4b62012-01-17 16:06:00 +08002587 static_field_value =
2588 irb_.CreateCall2(runtime_func, field_idx_value, method_object_addr);
2589
2590 EmitGuard_ExceptionLandingPad(dex_pc);
2591
2592 } else {
Logan Chien933abf82012-04-11 12:24:31 +08002593 DCHECK_GE(field_offset, 0);
Logan Chien438c4b62012-01-17 16:06:00 +08002594
Logan Chien933abf82012-04-11 12:24:31 +08002595 llvm::Value* static_storage_addr = NULL;
2596
2597 if (is_referrers_class) {
2598 // Fast path, static storage base is this method's class
2599 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2600
2601 llvm::Constant* declaring_class_offset_value =
2602 irb_.getPtrEquivInt(Method::DeclaringClassOffset().Int32Value());
2603
2604 llvm::Value* static_storage_field_addr =
2605 irb_.CreatePtrDisp(method_object_addr, declaring_class_offset_value,
2606 irb_.getJObjectTy()->getPointerTo());
2607
2608 static_storage_addr = irb_.CreateLoad(static_storage_field_addr);
2609 } else {
2610 // Medium path, static storage base in a different class which
2611 // requires checks that the other class is initialized
2612 DCHECK_GE(ssb_index, 0);
2613 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
2614 }
2615
2616 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
Logan Chien438c4b62012-01-17 16:06:00 +08002617
2618 llvm::Value* static_field_addr =
2619 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
2620 irb_.getJType(field_jty, kField)->getPointerTo());
2621
Logan Chien933abf82012-04-11 12:24:31 +08002622 // TODO: Check is_volatile. We need to generate atomic load instruction
2623 // when is_volatile is true.
Logan Chien438c4b62012-01-17 16:06:00 +08002624 static_field_value = irb_.CreateLoad(static_field_addr);
2625 }
2626
Elliott Hughesadb8c672012-03-06 16:49:32 -08002627 EmitStoreDalvikReg(dec_insn.vA, field_jty, kField, static_field_value);
Logan Chien438c4b62012-01-17 16:06:00 +08002628
Logan Chien70f94b42011-12-27 17:49:11 +08002629 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2630}
2631
2632
2633void MethodCompiler::EmitInsn_SPut(uint32_t dex_pc,
2634 Instruction const* insn,
2635 JType field_jty) {
Logan Chien14179c82012-01-17 17:06:34 +08002636
Elliott Hughesadb8c672012-03-06 16:49:32 -08002637 DecodedInstruction dec_insn(insn);
Logan Chien14179c82012-01-17 17:06:34 +08002638
Logan Chien933abf82012-04-11 12:24:31 +08002639 uint32_t field_idx = dec_insn.vB;
Logan Chien14179c82012-01-17 17:06:34 +08002640
Elliott Hughesadb8c672012-03-06 16:49:32 -08002641 llvm::Value* new_value = EmitLoadDalvikReg(dec_insn.vA, field_jty, kField);
Logan Chien14179c82012-01-17 17:06:34 +08002642
Logan Chien933abf82012-04-11 12:24:31 +08002643 int field_offset;
2644 int ssb_index;
2645 bool is_referrers_class;
2646 bool is_volatile;
2647
2648 bool is_fast_path = compiler_->ComputeStaticFieldInfo(
2649 field_idx, oat_compilation_unit_, field_offset, ssb_index,
2650 is_referrers_class, is_volatile, true);
2651
2652 if (!is_fast_path) {
Logan Chien14179c82012-01-17 17:06:34 +08002653 llvm::Function* runtime_func;
2654
2655 if (field_jty == kObject) {
2656 runtime_func = irb_.GetRuntime(SetObjectStatic);
2657 } else if (field_jty == kLong || field_jty == kDouble) {
2658 runtime_func = irb_.GetRuntime(Set64Static);
2659 } else {
2660 runtime_func = irb_.GetRuntime(Set32Static);
2661 }
2662
Elliott Hughesadb8c672012-03-06 16:49:32 -08002663 llvm::Constant* field_idx_value = irb_.getInt32(dec_insn.vB);
Logan Chien14179c82012-01-17 17:06:34 +08002664
2665 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2666
Logan Chien8dfcbea2012-02-17 18:50:32 +08002667 EmitUpdateLineNumFromDexPC(dex_pc);
2668
Logan Chien14179c82012-01-17 17:06:34 +08002669 irb_.CreateCall3(runtime_func, field_idx_value,
2670 method_object_addr, new_value);
2671
2672 EmitGuard_ExceptionLandingPad(dex_pc);
2673
2674 } else {
Logan Chien933abf82012-04-11 12:24:31 +08002675 DCHECK_GE(field_offset, 0);
Logan Chien14179c82012-01-17 17:06:34 +08002676
Logan Chien933abf82012-04-11 12:24:31 +08002677 llvm::Value* static_storage_addr = NULL;
2678
2679 if (is_referrers_class) {
2680 // Fast path, static storage base is this method's class
2681 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2682
2683 llvm::Constant* declaring_class_offset_value =
2684 irb_.getPtrEquivInt(Method::DeclaringClassOffset().Int32Value());
2685
2686 llvm::Value* static_storage_field_addr =
2687 irb_.CreatePtrDisp(method_object_addr, declaring_class_offset_value,
2688 irb_.getJObjectTy()->getPointerTo());
2689
2690 static_storage_addr = irb_.CreateLoad(static_storage_field_addr);
2691 } else {
2692 // Medium path, static storage base in a different class which
2693 // requires checks that the other class is initialized
2694 DCHECK_GE(ssb_index, 0);
2695 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
2696 }
2697
2698 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
Logan Chien14179c82012-01-17 17:06:34 +08002699
2700 llvm::Value* static_field_addr =
2701 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
2702 irb_.getJType(field_jty, kField)->getPointerTo());
2703
Logan Chien933abf82012-04-11 12:24:31 +08002704 // TODO: Check is_volatile. We need to generate atomic store instruction
2705 // when is_volatile is true.
Logan Chien14179c82012-01-17 17:06:34 +08002706 irb_.CreateStore(new_value, static_field_addr);
2707 }
2708
Logan Chien70f94b42011-12-27 17:49:11 +08002709 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2710}
2711
2712
Logan Chien1a121b92012-02-15 22:23:42 +08002713void MethodCompiler::
2714EmitLoadActualParameters(std::vector<llvm::Value*>& args,
2715 uint32_t callee_method_idx,
Elliott Hughesadb8c672012-03-06 16:49:32 -08002716 DecodedInstruction const& dec_insn,
Logan Chien7e7fabc2012-04-10 18:59:11 +08002717 InvokeArgFmt arg_fmt,
Logan Chien1a121b92012-02-15 22:23:42 +08002718 bool is_static) {
2719
2720 // Get method signature
2721 DexFile::MethodId const& method_id =
2722 dex_file_->GetMethodId(callee_method_idx);
2723
Logan Chien8faf8022012-02-24 12:25:29 +08002724 uint32_t shorty_size;
Logan Chien1a121b92012-02-15 22:23:42 +08002725 char const* shorty = dex_file_->GetMethodShorty(method_id, &shorty_size);
Logan Chien8faf8022012-02-24 12:25:29 +08002726 CHECK_GE(shorty_size, 1u);
Logan Chien1a121b92012-02-15 22:23:42 +08002727
2728 // Load argument values according to the shorty (without "this")
2729 uint16_t reg_count = 0;
2730
2731 if (!is_static) {
2732 ++reg_count; // skip the "this" pointer
2733 }
2734
Logan Chien7e7fabc2012-04-10 18:59:11 +08002735 bool is_range = (arg_fmt == kArgRange);
2736
Logan Chien8faf8022012-02-24 12:25:29 +08002737 for (uint32_t i = 1; i < shorty_size; ++i) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002738 uint32_t reg_idx = (is_range) ? (dec_insn.vC + reg_count)
2739 : (dec_insn.arg[reg_count]);
Logan Chien1a121b92012-02-15 22:23:42 +08002740
2741 args.push_back(EmitLoadDalvikReg(reg_idx, shorty[i], kAccurate));
2742
2743 ++reg_count;
2744 if (shorty[i] == 'J' || shorty[i] == 'D') {
2745 // Wide types, such as long and double, are using a pair of registers
2746 // to store the value, so we have to increase arg_reg again.
2747 ++reg_count;
2748 }
2749 }
2750
Elliott Hughesadb8c672012-03-06 16:49:32 -08002751 DCHECK_EQ(reg_count, dec_insn.vA)
Logan Chien1a121b92012-02-15 22:23:42 +08002752 << "Actual argument mismatch for callee: "
2753 << PrettyMethod(callee_method_idx, *dex_file_);
2754}
2755
TDYa12785321912012-04-01 15:24:56 -07002756llvm::Value* MethodCompiler::EmitEnsureResolved(llvm::Value* callee,
2757 llvm::Value* caller,
2758 uint32_t dex_method_idx,
TDYa1270b686e52012-04-09 22:43:35 -07002759 bool is_virtual) {
2760 // TODO: Remove this after we solve the trampoline related problems.
2761 return irb_.CreateCall4(irb_.GetRuntime(EnsureResolved),
2762 callee,
2763 caller,
2764 irb_.getInt32(dex_method_idx),
2765 irb_.getInt1(is_virtual));
TDYa12785321912012-04-01 15:24:56 -07002766}
2767
TDYa1270b686e52012-04-09 22:43:35 -07002768llvm::Value* MethodCompiler::EmitFixStub(llvm::Value* callee_method_object_addr,
2769 uint32_t method_idx,
2770 bool is_static) {
2771 // TODO: Remove this after we solve the link and trampoline related problems.
2772 llvm::Value* code_addr = irb_.CreateCall(irb_.GetRuntime(FixStub), callee_method_object_addr);
2773
2774 llvm::FunctionType* method_type = GetFunctionType(method_idx, is_static);
2775
2776 return irb_.CreatePointerCast(code_addr, method_type->getPointerTo());
TDYa12785321912012-04-01 15:24:56 -07002777}
Logan Chien1a121b92012-02-15 22:23:42 +08002778
Shih-wei Liao399ed3f2012-03-08 01:27:04 -08002779
Logan Chien7e7fabc2012-04-10 18:59:11 +08002780void MethodCompiler::EmitInsn_Invoke(uint32_t dex_pc,
2781 Instruction const* insn,
2782 InvokeType invoke_type,
2783 InvokeArgFmt arg_fmt) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002784 DecodedInstruction dec_insn(insn);
Logan Chien46fbb412012-02-15 22:29:08 +08002785
Logan Chien61c65dc2012-02-29 03:22:30 +08002786 bool is_static = (invoke_type == kStatic);
Logan Chien7e7fabc2012-04-10 18:59:11 +08002787 uint32_t callee_method_idx = dec_insn.vB;
Logan Chien61c65dc2012-02-29 03:22:30 +08002788
Logan Chien7e7fabc2012-04-10 18:59:11 +08002789 // Compute invoke related information for compiler decision
2790 int vtable_idx = -1;
Logan Chien92ad16d2012-03-18 05:48:55 +08002791 uintptr_t direct_code = 0; // Currently unused
2792 uintptr_t direct_method = 0; // Currently unused
Logan Chien61c65dc2012-02-29 03:22:30 +08002793 bool is_fast_path = compiler_->
Logan Chien7e7fabc2012-04-10 18:59:11 +08002794 ComputeInvokeInfo(callee_method_idx, oat_compilation_unit_,
Logan Chien92ad16d2012-03-18 05:48:55 +08002795 invoke_type, vtable_idx, direct_code, direct_method);
Logan Chien61c65dc2012-02-29 03:22:30 +08002796
Logan Chien7e7fabc2012-04-10 18:59:11 +08002797 // Load *this* actual parameter
Logan Chien1a121b92012-02-15 22:23:42 +08002798 llvm::Value* this_addr = NULL;
2799
2800 if (!is_static) {
2801 // Test: Is *this* parameter equal to null?
Logan Chien7e7fabc2012-04-10 18:59:11 +08002802 this_addr = (arg_fmt == kArgReg) ?
2803 EmitLoadDalvikReg(dec_insn.arg[0], kObject, kAccurate):
2804 EmitLoadDalvikReg(dec_insn.vC + 0, kObject, kAccurate);
2805
Logan Chien1a121b92012-02-15 22:23:42 +08002806 EmitGuard_NullPointerException(dex_pc, this_addr);
2807 }
2808
Logan Chien7e7fabc2012-04-10 18:59:11 +08002809 // Load the method object
TDYa1274e42a592012-04-10 20:13:54 -07002810 llvm::Value* callee_method_object_addr = NULL;
Logan Chien7e7fabc2012-04-10 18:59:11 +08002811
2812 if (!is_fast_path) {
TDYa1274e42a592012-04-10 20:13:54 -07002813 callee_method_object_addr =
Logan Chien7e7fabc2012-04-10 18:59:11 +08002814 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx, invoke_type,
2815 this_addr, dex_pc, is_fast_path);
2816 } else {
2817 switch (invoke_type) {
2818 case kStatic:
2819 case kDirect:
TDYa1274e42a592012-04-10 20:13:54 -07002820 callee_method_object_addr =
Logan Chien7e7fabc2012-04-10 18:59:11 +08002821 EmitLoadSDCalleeMethodObjectAddr(callee_method_idx);
2822 break;
2823
2824 case kVirtual:
2825 DCHECK(vtable_idx != -1);
TDYa1274e42a592012-04-10 20:13:54 -07002826 callee_method_object_addr =
Logan Chien7e7fabc2012-04-10 18:59:11 +08002827 EmitLoadVirtualCalleeMethodObjectAddr(vtable_idx, this_addr);
2828 break;
2829
2830 case kSuper:
2831 LOG(FATAL) << "invoke-super should be promoted to invoke-direct in "
2832 "the fast path.";
2833 break;
2834
2835 case kInterface:
TDYa1274e42a592012-04-10 20:13:54 -07002836 callee_method_object_addr =
Logan Chien7e7fabc2012-04-10 18:59:11 +08002837 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx,
2838 invoke_type, this_addr,
2839 dex_pc, is_fast_path);
2840 break;
2841 }
TDYa1274e42a592012-04-10 20:13:54 -07002842
2843 // Ensure the callee method object is resolved.
2844 bool is_virtual = (dec_insn.opcode == Instruction::INVOKE_VIRTUAL) ||
2845 (dec_insn.opcode == Instruction::INVOKE_VIRTUAL_RANGE) ||
2846 (dec_insn.opcode == Instruction::INVOKE_SUPER) ||
2847 (dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE);
2848
2849 llvm::Value* caller_method_object_addr = EmitLoadMethodObjectAddr();
2850
2851 callee_method_object_addr = EmitEnsureResolved(callee_method_object_addr,
2852 caller_method_object_addr,
2853 callee_method_idx,
2854 is_virtual);
2855
2856 EmitGuard_ExceptionLandingPad(dex_pc);
Logan Chien7e7fabc2012-04-10 18:59:11 +08002857 }
2858
TDYa1270b686e52012-04-09 22:43:35 -07002859#if 0
Logan Chien7e7fabc2012-04-10 18:59:11 +08002860 llvm::Value* code_field_offset_value =
2861 irb_.getPtrEquivInt(Method::GetCodeOffset().Int32Value());
TDYa12785321912012-04-01 15:24:56 -07002862
Logan Chien7e7fabc2012-04-10 18:59:11 +08002863 llvm::Value* code_field_addr =
2864 irb_.CreatePtrDisp(callee_method_object_addr, code_field_offset_value,
2865 irb_.getInt8Ty()->getPointerTo()->getPointerTo());
2866
2867 llvm::Value* code_addr = irb_.CreateLoad(code_field_addr);
2868#else
2869 // TODO: Remove this after we solve the link and trampoline related problems.
2870 llvm::Value* code_addr =
2871 EmitFixStub(callee_method_object_addr, callee_method_idx, is_static);
2872
2873 EmitGuard_ExceptionLandingPad(dex_pc);
TDYa1270b686e52012-04-09 22:43:35 -07002874#endif
TDYa12785321912012-04-01 15:24:56 -07002875
Logan Chien1a121b92012-02-15 22:23:42 +08002876 // Load the actual parameter
2877 std::vector<llvm::Value*> args;
2878
2879 args.push_back(callee_method_object_addr); // method object for callee
2880
2881 if (!is_static) {
Logan Chien7e7fabc2012-04-10 18:59:11 +08002882 DCHECK(this_addr != NULL);
Logan Chien1a121b92012-02-15 22:23:42 +08002883 args.push_back(this_addr); // "this" object for callee
2884 }
2885
2886 EmitLoadActualParameters(args, callee_method_idx, dec_insn,
Logan Chien7e7fabc2012-04-10 18:59:11 +08002887 arg_fmt, is_static);
Logan Chien1a121b92012-02-15 22:23:42 +08002888
TDYa1275bb86012012-04-11 05:57:28 -07002889#if 0
Logan Chien8dfcbea2012-02-17 18:50:32 +08002890 // Invoke callee
2891 EmitUpdateLineNumFromDexPC(dex_pc);
Logan Chien1a121b92012-02-15 22:23:42 +08002892 llvm::Value* retval = irb_.CreateCall(code_addr, args);
2893 EmitGuard_ExceptionLandingPad(dex_pc);
2894
Logan Chien7e7fabc2012-04-10 18:59:11 +08002895 uint32_t callee_access_flags = is_static ? kAccStatic : 0;
2896 UniquePtr<OatCompilationUnit> callee_oat_compilation_unit(
2897 oat_compilation_unit_->GetCallee(callee_method_idx, callee_access_flags));
2898
2899 char ret_shorty = callee_oat_compilation_unit->GetShorty()[0];
Shih-wei Liao90d50992012-02-19 03:32:05 -08002900 if (ret_shorty != 'V') {
Logan Chien1a121b92012-02-15 22:23:42 +08002901 EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval);
2902 }
TDYa1275bb86012012-04-11 05:57:28 -07002903#else
2904 uint32_t callee_access_flags = is_static ? kAccStatic : 0;
2905 UniquePtr<OatCompilationUnit> callee_oat_compilation_unit(
2906 oat_compilation_unit_->GetCallee(callee_method_idx, callee_access_flags));
2907
2908 char ret_shorty = callee_oat_compilation_unit->GetShorty()[0];
2909
2910
2911 EmitUpdateLineNumFromDexPC(dex_pc);
2912
2913
2914 llvm::BasicBlock* block_normal = CreateBasicBlockWithDexPC(dex_pc, "normal");
2915 llvm::BasicBlock* block_proxy_stub = CreateBasicBlockWithDexPC(dex_pc, "proxy");
2916 llvm::BasicBlock* block_continue = CreateBasicBlockWithDexPC(dex_pc, "cont");
2917
2918 llvm::Type* accurate_ret_type = irb_.getJType(ret_shorty, kAccurate);
2919 llvm::Value* retval_addr = NULL;
2920 if (ret_shorty != 'V') {
2921 retval_addr = irb_.CreateAlloca(accurate_ret_type);
2922 }
2923
2924
2925 // TODO: Remove this after we solve the proxy trampoline calling convention problem.
2926 llvm::Value* code_addr_int = irb_.CreatePtrToInt(code_addr, irb_.getPtrEquivIntTy());
2927 llvm::Value* proxy_stub_int = irb_.getPtrEquivInt(special_stub::kProxyStub);
2928 llvm::Value* is_proxy_stub = irb_.CreateICmpEQ(code_addr_int, proxy_stub_int);
2929 irb_.CreateCondBr(is_proxy_stub, block_proxy_stub, block_normal);
2930
2931
2932 irb_.SetInsertPoint(block_normal);
2933 {
2934 // Invoke callee
2935 llvm::Value* result = irb_.CreateCall(code_addr, args);
2936 if (ret_shorty != 'V') {
2937 irb_.CreateStore(result, retval_addr);
2938 }
2939 }
2940 irb_.CreateBr(block_continue);
2941
2942
2943 irb_.SetInsertPoint(block_proxy_stub);
2944 {
2945 llvm::Value* temp_space_addr;
2946 if (ret_shorty != 'V') {
2947 temp_space_addr = irb_.CreateAlloca(irb_.getJValueTy());
2948 args.push_back(temp_space_addr);
2949 }
2950 irb_.CreateCall(irb_.GetRuntime(ProxyInvokeHandler), args);
2951 if (ret_shorty != 'V') {
2952 llvm::Value* result_addr =
2953 irb_.CreateBitCast(temp_space_addr, accurate_ret_type->getPointerTo());
2954 llvm::Value* retval = irb_.CreateLoad(result_addr);
2955 irb_.CreateStore(retval, retval_addr);
2956 }
2957 }
2958 irb_.CreateBr(block_continue);
2959
2960
2961 irb_.SetInsertPoint(block_continue);
2962
2963 if (ret_shorty != 'V') {
2964 llvm::Value* retval = irb_.CreateLoad(retval_addr);
2965 EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval);
2966 }
2967 EmitGuard_ExceptionLandingPad(dex_pc);
2968#endif
Logan Chien1a121b92012-02-15 22:23:42 +08002969
Logan Chien70f94b42011-12-27 17:49:11 +08002970 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2971}
2972
2973
Logan Chien7e7fabc2012-04-10 18:59:11 +08002974llvm::Value* MethodCompiler::
2975EmitLoadSDCalleeMethodObjectAddr(uint32_t callee_method_idx) {
2976 llvm::Value* callee_method_object_field_addr =
2977 EmitLoadDexCacheResolvedMethodFieldAddr(callee_method_idx);
Logan Chien7caf37e2012-02-03 22:56:04 +08002978
Logan Chien7e7fabc2012-04-10 18:59:11 +08002979 return irb_.CreateLoad(callee_method_object_field_addr);
2980}
Logan Chien7caf37e2012-02-03 22:56:04 +08002981
Logan Chien7caf37e2012-02-03 22:56:04 +08002982
Logan Chien7e7fabc2012-04-10 18:59:11 +08002983llvm::Value* MethodCompiler::
2984EmitLoadVirtualCalleeMethodObjectAddr(int vtable_idx,
2985 llvm::Value* this_addr) {
2986 // Load class object of *this* pointer
2987 llvm::Constant* class_field_offset_value =
2988 irb_.getPtrEquivInt(Object::ClassOffset().Int32Value());
Logan Chien7caf37e2012-02-03 22:56:04 +08002989
Logan Chien7e7fabc2012-04-10 18:59:11 +08002990 llvm::Value* class_field_addr =
2991 irb_.CreatePtrDisp(this_addr, class_field_offset_value,
2992 irb_.getJObjectTy()->getPointerTo());
Logan Chien7caf37e2012-02-03 22:56:04 +08002993
Logan Chien7e7fabc2012-04-10 18:59:11 +08002994 llvm::Value* class_object_addr = irb_.CreateLoad(class_field_addr);
Logan Chien7caf37e2012-02-03 22:56:04 +08002995
Logan Chien7e7fabc2012-04-10 18:59:11 +08002996 // Load vtable address
2997 llvm::Constant* vtable_offset_value =
2998 irb_.getPtrEquivInt(Class::VTableOffset().Int32Value());
2999
3000 llvm::Value* vtable_field_addr =
3001 irb_.CreatePtrDisp(class_object_addr, vtable_offset_value,
3002 irb_.getJObjectTy()->getPointerTo());
3003
3004 llvm::Value* vtable_addr = irb_.CreateLoad(vtable_field_addr);
3005
3006 // Load callee method object
3007 llvm::Value* vtable_idx_value =
3008 irb_.getPtrEquivInt(static_cast<uint64_t>(vtable_idx));
3009
3010 llvm::Value* method_field_addr =
3011 EmitArrayGEP(vtable_addr, vtable_idx_value, irb_.getJObjectTy(), kObject);
3012
3013 return irb_.CreateLoad(method_field_addr);
3014}
3015
3016
3017llvm::Value* MethodCompiler::
3018EmitCallRuntimeForCalleeMethodObjectAddr(uint32_t callee_method_idx,
3019 InvokeType invoke_type,
3020 llvm::Value* this_addr,
3021 uint32_t dex_pc,
3022 bool is_fast_path) {
3023
3024 llvm::Function* runtime_func = NULL;
3025
3026 switch (invoke_type) {
3027 case kStatic:
3028 runtime_func = irb_.GetRuntime(FindStaticMethodWithAccessCheck);
3029 break;
3030
3031 case kDirect:
3032 runtime_func = irb_.GetRuntime(FindDirectMethodWithAccessCheck);
3033 break;
3034
3035 case kVirtual:
3036 runtime_func = irb_.GetRuntime(FindVirtualMethodWithAccessCheck);
3037 break;
3038
3039 case kSuper:
3040 runtime_func = irb_.GetRuntime(FindSuperMethodWithAccessCheck);
3041 break;
3042
3043 case kInterface:
3044 if (is_fast_path) {
3045 runtime_func = irb_.GetRuntime(FindInterfaceMethod);
3046 } else {
3047 runtime_func = irb_.GetRuntime(FindInterfaceMethodWithAccessCheck);
3048 }
3049 break;
3050 }
Logan Chien7caf37e2012-02-03 22:56:04 +08003051
3052 llvm::Value* callee_method_idx_value = irb_.getInt32(callee_method_idx);
3053
Logan Chien7e7fabc2012-04-10 18:59:11 +08003054 if (this_addr == NULL) {
3055 DCHECK_EQ(invoke_type, kStatic);
3056 this_addr = irb_.getJNull();
3057 }
3058
3059 llvm::Value* caller_method_object_addr = EmitLoadMethodObjectAddr();
3060
Logan Chien8dfcbea2012-02-17 18:50:32 +08003061 EmitUpdateLineNumFromDexPC(dex_pc);
3062
TDYa1270b686e52012-04-09 22:43:35 -07003063 llvm::Value* callee_method_object_addr =
Shih-wei Liao399ed3f2012-03-08 01:27:04 -08003064 irb_.CreateCall3(runtime_func,
Logan Chien7e7fabc2012-04-10 18:59:11 +08003065 callee_method_idx_value,
3066 this_addr,
3067 caller_method_object_addr);
Logan Chien7caf37e2012-02-03 22:56:04 +08003068
3069 EmitGuard_ExceptionLandingPad(dex_pc);
3070
Logan Chien7e7fabc2012-04-10 18:59:11 +08003071 return callee_method_object_addr;
Logan Chien70f94b42011-12-27 17:49:11 +08003072}
3073
3074
3075void MethodCompiler::EmitInsn_Neg(uint32_t dex_pc,
3076 Instruction const* insn,
3077 JType op_jty) {
Logan Chien1b5685f2011-12-27 18:01:14 +08003078
Elliott Hughesadb8c672012-03-06 16:49:32 -08003079 DecodedInstruction dec_insn(insn);
Logan Chien1b5685f2011-12-27 18:01:14 +08003080
3081 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3082
Elliott Hughesadb8c672012-03-06 16:49:32 -08003083 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chien1b5685f2011-12-27 18:01:14 +08003084 llvm::Value* result_value = irb_.CreateNeg(src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003085 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chien1b5685f2011-12-27 18:01:14 +08003086
Logan Chien70f94b42011-12-27 17:49:11 +08003087 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3088}
3089
3090
3091void MethodCompiler::EmitInsn_Not(uint32_t dex_pc,
3092 Instruction const* insn,
3093 JType op_jty) {
Logan Chiene53750d2011-12-27 18:02:27 +08003094
Elliott Hughesadb8c672012-03-06 16:49:32 -08003095 DecodedInstruction dec_insn(insn);
Logan Chiene53750d2011-12-27 18:02:27 +08003096
3097 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3098
Elliott Hughesadb8c672012-03-06 16:49:32 -08003099 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chiene53750d2011-12-27 18:02:27 +08003100 llvm::Value* result_value =
3101 irb_.CreateXor(src_value, static_cast<uint64_t>(-1));
3102
Elliott Hughesadb8c672012-03-06 16:49:32 -08003103 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chiene53750d2011-12-27 18:02:27 +08003104
Logan Chien70f94b42011-12-27 17:49:11 +08003105 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3106}
3107
3108
3109void MethodCompiler::EmitInsn_SExt(uint32_t dex_pc,
3110 Instruction const* insn) {
Logan Chien61752ad2011-12-27 18:03:51 +08003111
Elliott Hughesadb8c672012-03-06 16:49:32 -08003112 DecodedInstruction dec_insn(insn);
Logan Chien61752ad2011-12-27 18:03:51 +08003113
Elliott Hughesadb8c672012-03-06 16:49:32 -08003114 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chien61752ad2011-12-27 18:03:51 +08003115 llvm::Value* result_value = irb_.CreateSExt(src_value, irb_.getJLongTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003116 EmitStoreDalvikReg(dec_insn.vA, kLong, kAccurate, result_value);
Logan Chien61752ad2011-12-27 18:03:51 +08003117
Logan Chien70f94b42011-12-27 17:49:11 +08003118 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3119}
3120
3121
3122void MethodCompiler::EmitInsn_Trunc(uint32_t dex_pc,
3123 Instruction const* insn) {
Logan Chien17a57662011-12-27 18:05:14 +08003124
Elliott Hughesadb8c672012-03-06 16:49:32 -08003125 DecodedInstruction dec_insn(insn);
Logan Chien17a57662011-12-27 18:05:14 +08003126
Elliott Hughesadb8c672012-03-06 16:49:32 -08003127 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kLong, kAccurate);
Logan Chien17a57662011-12-27 18:05:14 +08003128 llvm::Value* result_value = irb_.CreateTrunc(src_value, irb_.getJIntTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003129 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chien17a57662011-12-27 18:05:14 +08003130
Logan Chien70f94b42011-12-27 17:49:11 +08003131 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3132}
3133
3134
3135void MethodCompiler::EmitInsn_TruncAndSExt(uint32_t dex_pc,
3136 Instruction const* insn,
3137 unsigned N) {
Logan Chienb6744c52011-12-27 18:06:26 +08003138
Elliott Hughesadb8c672012-03-06 16:49:32 -08003139 DecodedInstruction dec_insn(insn);
Logan Chienb6744c52011-12-27 18:06:26 +08003140
Elliott Hughesadb8c672012-03-06 16:49:32 -08003141 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chienb6744c52011-12-27 18:06:26 +08003142
3143 llvm::Value* trunc_value =
3144 irb_.CreateTrunc(src_value, llvm::Type::getIntNTy(*context_, N));
3145
3146 llvm::Value* result_value = irb_.CreateSExt(trunc_value, irb_.getJIntTy());
3147
Elliott Hughesadb8c672012-03-06 16:49:32 -08003148 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chienb6744c52011-12-27 18:06:26 +08003149
Logan Chien70f94b42011-12-27 17:49:11 +08003150 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3151}
3152
3153
3154void MethodCompiler::EmitInsn_TruncAndZExt(uint32_t dex_pc,
3155 Instruction const* insn,
3156 unsigned N) {
Logan Chienb6744c52011-12-27 18:06:26 +08003157
Elliott Hughesadb8c672012-03-06 16:49:32 -08003158 DecodedInstruction dec_insn(insn);
Logan Chienb6744c52011-12-27 18:06:26 +08003159
Elliott Hughesadb8c672012-03-06 16:49:32 -08003160 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chienb6744c52011-12-27 18:06:26 +08003161
3162 llvm::Value* trunc_value =
3163 irb_.CreateTrunc(src_value, llvm::Type::getIntNTy(*context_, N));
3164
3165 llvm::Value* result_value = irb_.CreateZExt(trunc_value, irb_.getJIntTy());
3166
Elliott Hughesadb8c672012-03-06 16:49:32 -08003167 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chienb6744c52011-12-27 18:06:26 +08003168
Logan Chien70f94b42011-12-27 17:49:11 +08003169 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3170}
3171
3172
3173void MethodCompiler::EmitInsn_FNeg(uint32_t dex_pc,
3174 Instruction const* insn,
3175 JType op_jty) {
Logan Chien7a48b092011-12-27 18:07:45 +08003176
Elliott Hughesadb8c672012-03-06 16:49:32 -08003177 DecodedInstruction dec_insn(insn);
Logan Chien7a48b092011-12-27 18:07:45 +08003178
3179 DCHECK(op_jty == kFloat || op_jty == kDouble) << op_jty;
3180
Elliott Hughesadb8c672012-03-06 16:49:32 -08003181 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chien7a48b092011-12-27 18:07:45 +08003182 llvm::Value* result_value = irb_.CreateFNeg(src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003183 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chien7a48b092011-12-27 18:07:45 +08003184
Logan Chien70f94b42011-12-27 17:49:11 +08003185 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3186}
3187
3188
3189void MethodCompiler::EmitInsn_IntToFP(uint32_t dex_pc,
3190 Instruction const* insn,
3191 JType src_jty,
3192 JType dest_jty) {
Logan Chien62dd4532011-12-27 18:09:00 +08003193
Elliott Hughesadb8c672012-03-06 16:49:32 -08003194 DecodedInstruction dec_insn(insn);
Logan Chien62dd4532011-12-27 18:09:00 +08003195
3196 DCHECK(src_jty == kInt || src_jty == kLong) << src_jty;
3197 DCHECK(dest_jty == kFloat || dest_jty == kDouble) << dest_jty;
3198
Elliott Hughesadb8c672012-03-06 16:49:32 -08003199 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, src_jty, kAccurate);
Logan Chien62dd4532011-12-27 18:09:00 +08003200 llvm::Type* dest_type = irb_.getJType(dest_jty, kAccurate);
3201 llvm::Value* dest_value = irb_.CreateSIToFP(src_value, dest_type);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003202 EmitStoreDalvikReg(dec_insn.vA, dest_jty, kAccurate, dest_value);
Logan Chien62dd4532011-12-27 18:09:00 +08003203
Logan Chien70f94b42011-12-27 17:49:11 +08003204 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3205}
3206
3207
3208void MethodCompiler::EmitInsn_FPToInt(uint32_t dex_pc,
3209 Instruction const* insn,
3210 JType src_jty,
TDYa127a4746872012-04-11 23:48:55 -07003211 JType dest_jty,
3212 runtime_support::RuntimeId runtime_func_id) {
Logan Chien12dc1752011-12-27 18:10:15 +08003213
Elliott Hughesadb8c672012-03-06 16:49:32 -08003214 DecodedInstruction dec_insn(insn);
Logan Chien12dc1752011-12-27 18:10:15 +08003215
3216 DCHECK(src_jty == kFloat || src_jty == kDouble) << src_jty;
3217 DCHECK(dest_jty == kInt || dest_jty == kLong) << dest_jty;
3218
Elliott Hughesadb8c672012-03-06 16:49:32 -08003219 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, src_jty, kAccurate);
TDYa127a4746872012-04-11 23:48:55 -07003220 llvm::Value* dest_value = irb_.CreateCall(irb_.GetRuntime(runtime_func_id), src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003221 EmitStoreDalvikReg(dec_insn.vA, dest_jty, kAccurate, dest_value);
Logan Chien12dc1752011-12-27 18:10:15 +08003222
Logan Chien70f94b42011-12-27 17:49:11 +08003223 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3224}
3225
3226
3227void MethodCompiler::EmitInsn_FExt(uint32_t dex_pc,
3228 Instruction const* insn) {
Logan Chienc56ded92011-12-27 18:10:57 +08003229
Elliott Hughesadb8c672012-03-06 16:49:32 -08003230 DecodedInstruction dec_insn(insn);
Logan Chienc56ded92011-12-27 18:10:57 +08003231
Elliott Hughesadb8c672012-03-06 16:49:32 -08003232 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kFloat, kAccurate);
Logan Chienc56ded92011-12-27 18:10:57 +08003233 llvm::Value* result_value = irb_.CreateFPExt(src_value, irb_.getJDoubleTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003234 EmitStoreDalvikReg(dec_insn.vA, kDouble, kAccurate, result_value);
Logan Chienc56ded92011-12-27 18:10:57 +08003235
Logan Chien70f94b42011-12-27 17:49:11 +08003236 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3237}
3238
3239
3240void MethodCompiler::EmitInsn_FTrunc(uint32_t dex_pc,
3241 Instruction const* insn) {
Logan Chien927744f2011-12-27 18:11:52 +08003242
Elliott Hughesadb8c672012-03-06 16:49:32 -08003243 DecodedInstruction dec_insn(insn);
Logan Chien927744f2011-12-27 18:11:52 +08003244
Elliott Hughesadb8c672012-03-06 16:49:32 -08003245 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kDouble, kAccurate);
Logan Chien927744f2011-12-27 18:11:52 +08003246 llvm::Value* result_value = irb_.CreateFPTrunc(src_value, irb_.getJFloatTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003247 EmitStoreDalvikReg(dec_insn.vA, kFloat, kAccurate, result_value);
Logan Chien927744f2011-12-27 18:11:52 +08003248
Logan Chien70f94b42011-12-27 17:49:11 +08003249 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3250}
3251
3252
3253void MethodCompiler::EmitInsn_IntArithm(uint32_t dex_pc,
3254 Instruction const* insn,
3255 IntArithmKind arithm,
3256 JType op_jty,
3257 bool is_2addr) {
Logan Chienc3f7d962011-12-27 18:13:18 +08003258
Elliott Hughesadb8c672012-03-06 16:49:32 -08003259 DecodedInstruction dec_insn(insn);
Logan Chienc3f7d962011-12-27 18:13:18 +08003260
3261 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3262
3263 llvm::Value* src1_value;
3264 llvm::Value* src2_value;
3265
3266 if (is_2addr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003267 src1_value = EmitLoadDalvikReg(dec_insn.vA, op_jty, kAccurate);
3268 src2_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chienc3f7d962011-12-27 18:13:18 +08003269 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003270 src1_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
3271 src2_value = EmitLoadDalvikReg(dec_insn.vC, op_jty, kAccurate);
Logan Chienc3f7d962011-12-27 18:13:18 +08003272 }
3273
3274 llvm::Value* result_value =
3275 EmitIntArithmResultComputation(dex_pc, src1_value, src2_value,
3276 arithm, op_jty);
3277
Elliott Hughesadb8c672012-03-06 16:49:32 -08003278 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chienc3f7d962011-12-27 18:13:18 +08003279
Logan Chien70f94b42011-12-27 17:49:11 +08003280 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3281}
3282
3283
3284void MethodCompiler::EmitInsn_IntArithmImmediate(uint32_t dex_pc,
3285 Instruction const* insn,
3286 IntArithmKind arithm) {
Logan Chienc3f7d962011-12-27 18:13:18 +08003287
Elliott Hughesadb8c672012-03-06 16:49:32 -08003288 DecodedInstruction dec_insn(insn);
Logan Chienc3f7d962011-12-27 18:13:18 +08003289
Elliott Hughesadb8c672012-03-06 16:49:32 -08003290 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chienc3f7d962011-12-27 18:13:18 +08003291
Elliott Hughesadb8c672012-03-06 16:49:32 -08003292 llvm::Value* imm_value = irb_.getInt32(dec_insn.vC);
Logan Chienc3f7d962011-12-27 18:13:18 +08003293
3294 llvm::Value* result_value =
3295 EmitIntArithmResultComputation(dex_pc, src_value, imm_value, arithm, kInt);
3296
Elliott Hughesadb8c672012-03-06 16:49:32 -08003297 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chienc3f7d962011-12-27 18:13:18 +08003298
Logan Chien70f94b42011-12-27 17:49:11 +08003299 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3300}
3301
3302
Logan Chienc3f7d962011-12-27 18:13:18 +08003303llvm::Value*
3304MethodCompiler::EmitIntArithmResultComputation(uint32_t dex_pc,
3305 llvm::Value* lhs,
3306 llvm::Value* rhs,
3307 IntArithmKind arithm,
3308 JType op_jty) {
3309 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3310
3311 switch (arithm) {
3312 case kIntArithm_Add:
3313 return irb_.CreateAdd(lhs, rhs);
3314
3315 case kIntArithm_Sub:
3316 return irb_.CreateSub(lhs, rhs);
3317
3318 case kIntArithm_Mul:
3319 return irb_.CreateMul(lhs, rhs);
3320
3321 case kIntArithm_Div:
Logan Chienc3f7d962011-12-27 18:13:18 +08003322 case kIntArithm_Rem:
TDYa127f8641ce2012-04-02 06:40:40 -07003323 return EmitIntDivRemResultComputation(dex_pc, lhs, rhs, arithm, op_jty);
Logan Chienc3f7d962011-12-27 18:13:18 +08003324
3325 case kIntArithm_And:
3326 return irb_.CreateAnd(lhs, rhs);
3327
3328 case kIntArithm_Or:
3329 return irb_.CreateOr(lhs, rhs);
3330
3331 case kIntArithm_Xor:
3332 return irb_.CreateXor(lhs, rhs);
3333
Logan Chienc3f7d962011-12-27 18:13:18 +08003334 default:
3335 LOG(FATAL) << "Unknown integer arithmetic kind: " << arithm;
3336 return NULL;
3337 }
3338}
3339
3340
TDYa127f8641ce2012-04-02 06:40:40 -07003341llvm::Value*
3342MethodCompiler::EmitIntDivRemResultComputation(uint32_t dex_pc,
3343 llvm::Value* dividend,
3344 llvm::Value* divisor,
3345 IntArithmKind arithm,
3346 JType op_jty) {
3347 // Throw exception if the divisor is 0.
3348 EmitGuard_DivZeroException(dex_pc, divisor, op_jty);
3349
3350 // Check the special case: MININT / -1 = MININT
3351 // That case will cause overflow, which is undefined behavior in llvm.
3352 // So we check the divisor is -1 or not, if the divisor is -1, we do
3353 // the special path to avoid undefined behavior.
3354 llvm::Type* op_type = irb_.getJType(op_jty, kAccurate);
3355 llvm::Value* zero = irb_.getJZero(op_jty);
3356 llvm::Value* neg_one = llvm::ConstantInt::getSigned(op_type, -1);
3357 llvm::Value* result = irb_.CreateAlloca(op_type);
3358
3359 llvm::BasicBlock* eq_neg_one = CreateBasicBlockWithDexPC(dex_pc, "eq_neg_one");
3360 llvm::BasicBlock* ne_neg_one = CreateBasicBlockWithDexPC(dex_pc, "ne_neg_one");
3361 llvm::BasicBlock* neg_one_cont = CreateBasicBlockWithDexPC(dex_pc, "neg_one_cont");
3362
3363 llvm::Value* is_equal_neg_one = EmitConditionResult(divisor, neg_one, kCondBranch_EQ);
3364 irb_.CreateCondBr(is_equal_neg_one, eq_neg_one, ne_neg_one);
3365
3366 // If divisor == -1
3367 irb_.SetInsertPoint(eq_neg_one);
3368 llvm::Value* eq_result;
3369 if (arithm == kIntArithm_Div) {
3370 // We can just change from "dividend div -1" to "neg dividend".
3371 // The sub don't care the sign/unsigned because of two's complement representation.
3372 // And the behavior is what we want:
3373 // -(2^n) (2^n)-1
3374 // MININT < k <= MAXINT -> mul k -1 = -k
3375 // MININT == k -> mul k -1 = k
3376 //
3377 // LLVM use sub to represent 'neg'
3378 eq_result = irb_.CreateSub(zero, dividend);
3379 } else {
3380 // Everything modulo -1 will be 0.
3381 eq_result = zero;
3382 }
3383 irb_.CreateStore(eq_result, result);
3384 irb_.CreateBr(neg_one_cont);
3385
3386 // If divisor != -1, just do the division.
3387 irb_.SetInsertPoint(ne_neg_one);
3388 llvm::Value* ne_result;
3389 if (arithm == kIntArithm_Div) {
3390 ne_result = irb_.CreateSDiv(dividend, divisor);
3391 } else {
3392 ne_result = irb_.CreateSRem(dividend, divisor);
3393 }
3394 irb_.CreateStore(ne_result, result);
3395 irb_.CreateBr(neg_one_cont);
3396
3397 irb_.SetInsertPoint(neg_one_cont);
3398 return irb_.CreateLoad(result);
3399}
3400
3401
Logan Chien5539ad02012-04-02 14:36:55 +08003402void MethodCompiler::EmitInsn_IntShiftArithm(uint32_t dex_pc,
3403 Instruction const* insn,
3404 IntShiftArithmKind arithm,
3405 JType op_jty,
3406 bool is_2addr) {
3407
3408 DecodedInstruction dec_insn(insn);
3409
3410 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3411
3412 llvm::Value* src1_value;
3413 llvm::Value* src2_value;
3414
3415 // NOTE: The 2nd operand of the shift arithmetic instruction is
3416 // 32-bit integer regardless of the 1st operand.
3417 if (is_2addr) {
3418 src1_value = EmitLoadDalvikReg(dec_insn.vA, op_jty, kAccurate);
3419 src2_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
3420 } else {
3421 src1_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
3422 src2_value = EmitLoadDalvikReg(dec_insn.vC, kInt, kAccurate);
3423 }
3424
3425 llvm::Value* result_value =
3426 EmitIntShiftArithmResultComputation(dex_pc, src1_value, src2_value,
3427 arithm, op_jty);
3428
3429 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
3430
3431 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3432}
3433
3434
3435void MethodCompiler::
3436EmitInsn_IntShiftArithmImmediate(uint32_t dex_pc,
3437 Instruction const* insn,
3438 IntShiftArithmKind arithm) {
3439
3440 DecodedInstruction dec_insn(insn);
3441
3442 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
3443
3444 llvm::Value* imm_value = irb_.getInt32(dec_insn.vC);
3445
3446 llvm::Value* result_value =
3447 EmitIntShiftArithmResultComputation(dex_pc, src_value, imm_value,
3448 arithm, kInt);
3449
3450 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
3451
3452 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3453}
3454
3455
3456llvm::Value*
3457MethodCompiler::EmitIntShiftArithmResultComputation(uint32_t dex_pc,
3458 llvm::Value* lhs,
3459 llvm::Value* rhs,
3460 IntShiftArithmKind arithm,
3461 JType op_jty) {
3462 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3463
3464 if (op_jty == kInt) {
3465 rhs = irb_.CreateAnd(rhs, 0x1f);
3466 } else {
3467 llvm::Value* masked_rhs = irb_.CreateAnd(rhs, 0x3f);
3468 rhs = irb_.CreateZExt(masked_rhs, irb_.getJLongTy());
3469 }
3470
3471 switch (arithm) {
3472 case kIntArithm_Shl:
3473 return irb_.CreateShl(lhs, rhs);
3474
3475 case kIntArithm_Shr:
3476 return irb_.CreateAShr(lhs, rhs);
3477
3478 case kIntArithm_UShr:
3479 return irb_.CreateLShr(lhs, rhs);
3480
3481 default:
3482 LOG(FATAL) << "Unknown integer shift arithmetic kind: " << arithm;
3483 return NULL;
3484 }
3485}
3486
3487
Logan Chien70f94b42011-12-27 17:49:11 +08003488void MethodCompiler::EmitInsn_RSubImmediate(uint32_t dex_pc,
3489 Instruction const* insn) {
Logan Chien65c62d42011-12-27 18:14:18 +08003490
Elliott Hughesadb8c672012-03-06 16:49:32 -08003491 DecodedInstruction dec_insn(insn);
Logan Chien65c62d42011-12-27 18:14:18 +08003492
Elliott Hughesadb8c672012-03-06 16:49:32 -08003493 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
3494 llvm::Value* imm_value = irb_.getInt32(dec_insn.vC);
Logan Chien65c62d42011-12-27 18:14:18 +08003495 llvm::Value* result_value = irb_.CreateSub(imm_value, src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003496 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chien65c62d42011-12-27 18:14:18 +08003497
Logan Chien70f94b42011-12-27 17:49:11 +08003498 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3499}
3500
3501
3502void MethodCompiler::EmitInsn_FPArithm(uint32_t dex_pc,
3503 Instruction const* insn,
3504 FPArithmKind arithm,
3505 JType op_jty,
3506 bool is_2addr) {
Logan Chien76e1c792011-12-27 18:15:01 +08003507
Elliott Hughesadb8c672012-03-06 16:49:32 -08003508 DecodedInstruction dec_insn(insn);
Logan Chien76e1c792011-12-27 18:15:01 +08003509
3510 DCHECK(op_jty == kFloat || op_jty == kDouble) << op_jty;
3511
3512 llvm::Value* src1_value;
3513 llvm::Value* src2_value;
3514
3515 if (is_2addr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003516 src1_value = EmitLoadDalvikReg(dec_insn.vA, op_jty, kAccurate);
3517 src2_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chien76e1c792011-12-27 18:15:01 +08003518 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003519 src1_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
3520 src2_value = EmitLoadDalvikReg(dec_insn.vC, op_jty, kAccurate);
Logan Chien76e1c792011-12-27 18:15:01 +08003521 }
3522
3523 llvm::Value* result_value =
3524 EmitFPArithmResultComputation(dex_pc, src1_value, src2_value, arithm);
3525
Elliott Hughesadb8c672012-03-06 16:49:32 -08003526 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chien76e1c792011-12-27 18:15:01 +08003527
Logan Chien70f94b42011-12-27 17:49:11 +08003528 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3529}
3530
3531
Logan Chien76e1c792011-12-27 18:15:01 +08003532llvm::Value*
3533MethodCompiler::EmitFPArithmResultComputation(uint32_t dex_pc,
3534 llvm::Value *lhs,
3535 llvm::Value *rhs,
3536 FPArithmKind arithm) {
3537 switch (arithm) {
3538 case kFPArithm_Add:
3539 return irb_.CreateFAdd(lhs, rhs);
3540
3541 case kFPArithm_Sub:
3542 return irb_.CreateFSub(lhs, rhs);
3543
3544 case kFPArithm_Mul:
3545 return irb_.CreateFMul(lhs, rhs);
3546
3547 case kFPArithm_Div:
3548 return irb_.CreateFDiv(lhs, rhs);
3549
3550 case kFPArithm_Rem:
3551 return irb_.CreateFRem(lhs, rhs);
3552
3553 default:
3554 LOG(FATAL) << "Unknown floating-point arithmetic kind: " << arithm;
3555 return NULL;
3556 }
3557}
3558
3559
Logan Chienc3f7d962011-12-27 18:13:18 +08003560void MethodCompiler::EmitGuard_DivZeroException(uint32_t dex_pc,
3561 llvm::Value* denominator,
3562 JType op_jty) {
3563 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3564
3565 llvm::Constant* zero = irb_.getJZero(op_jty);
3566
3567 llvm::Value* equal_zero = irb_.CreateICmpEQ(denominator, zero);
3568
3569 llvm::BasicBlock* block_exception = CreateBasicBlockWithDexPC(dex_pc, "div0");
3570
3571 llvm::BasicBlock* block_continue = CreateBasicBlockWithDexPC(dex_pc, "cont");
3572
3573 irb_.CreateCondBr(equal_zero, block_exception, block_continue);
3574
3575 irb_.SetInsertPoint(block_exception);
Logan Chien8dfcbea2012-02-17 18:50:32 +08003576 EmitUpdateLineNumFromDexPC(dex_pc);
Logan Chienc3f7d962011-12-27 18:13:18 +08003577 irb_.CreateCall(irb_.GetRuntime(ThrowDivZeroException));
3578 EmitBranchExceptionLandingPad(dex_pc);
3579
3580 irb_.SetInsertPoint(block_continue);
3581}
3582
3583
Logan Chien61bb6142012-02-03 15:34:53 +08003584void MethodCompiler::EmitGuard_NullPointerException(uint32_t dex_pc,
3585 llvm::Value* object) {
3586 llvm::Value* equal_null = irb_.CreateICmpEQ(object, irb_.getJNull());
3587
3588 llvm::BasicBlock* block_exception =
3589 CreateBasicBlockWithDexPC(dex_pc, "nullp");
3590
3591 llvm::BasicBlock* block_continue =
3592 CreateBasicBlockWithDexPC(dex_pc, "cont");
3593
3594 irb_.CreateCondBr(equal_null, block_exception, block_continue);
3595
3596 irb_.SetInsertPoint(block_exception);
Logan Chien8dfcbea2012-02-17 18:50:32 +08003597 EmitUpdateLineNumFromDexPC(dex_pc);
TDYa1273f9137d2012-04-08 15:59:19 -07003598 irb_.CreateCall(irb_.GetRuntime(ThrowNullPointerException), irb_.getInt32(dex_pc));
Logan Chien61bb6142012-02-03 15:34:53 +08003599 EmitBranchExceptionLandingPad(dex_pc);
3600
3601 irb_.SetInsertPoint(block_continue);
3602}
3603
3604
Logan Chienbb4d12a2012-02-17 14:10:01 +08003605llvm::Value* MethodCompiler::EmitLoadDexCacheAddr(MemberOffset offset) {
3606 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
3607
3608 llvm::Value* dex_cache_offset_value =
3609 irb_.getPtrEquivInt(offset.Int32Value());
3610
3611 llvm::Value* dex_cache_field_addr =
3612 irb_.CreatePtrDisp(method_object_addr, dex_cache_offset_value,
3613 irb_.getJObjectTy()->getPointerTo());
3614
3615 return irb_.CreateLoad(dex_cache_field_addr);
3616}
3617
3618
Logan Chienbb4d12a2012-02-17 14:10:01 +08003619llvm::Value* MethodCompiler::
3620EmitLoadDexCacheStaticStorageFieldAddr(uint32_t type_idx) {
3621 llvm::Value* static_storage_dex_cache_addr =
3622 EmitLoadDexCacheAddr(Method::DexCacheInitializedStaticStorageOffset());
3623
3624 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
3625
3626 return EmitArrayGEP(static_storage_dex_cache_addr, type_idx_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08003627 irb_.getJObjectTy(), kObject);
Logan Chienbb4d12a2012-02-17 14:10:01 +08003628}
3629
3630
3631llvm::Value* MethodCompiler::
3632EmitLoadDexCacheResolvedTypeFieldAddr(uint32_t type_idx) {
3633 llvm::Value* resolved_type_dex_cache_addr =
3634 EmitLoadDexCacheAddr(Method::DexCacheResolvedTypesOffset());
3635
3636 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
3637
3638 return EmitArrayGEP(resolved_type_dex_cache_addr, type_idx_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08003639 irb_.getJObjectTy(), kObject);
Logan Chienbb4d12a2012-02-17 14:10:01 +08003640}
3641
3642
3643llvm::Value* MethodCompiler::
Logan Chien61c65dc2012-02-29 03:22:30 +08003644EmitLoadDexCacheResolvedMethodFieldAddr(uint32_t method_idx) {
3645 llvm::Value* resolved_method_dex_cache_addr =
3646 EmitLoadDexCacheAddr(Method::DexCacheResolvedMethodsOffset());
3647
3648 llvm::Value* method_idx_value = irb_.getPtrEquivInt(method_idx);
3649
3650 return EmitArrayGEP(resolved_method_dex_cache_addr, method_idx_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08003651 irb_.getJObjectTy(), kObject);
Logan Chien61c65dc2012-02-29 03:22:30 +08003652}
3653
3654
3655llvm::Value* MethodCompiler::
Logan Chienbb4d12a2012-02-17 14:10:01 +08003656EmitLoadDexCacheStringFieldAddr(uint32_t string_idx) {
3657 llvm::Value* string_dex_cache_addr =
3658 EmitLoadDexCacheAddr(Method::DexCacheStringsOffset());
3659
3660 llvm::Value* string_idx_value = irb_.getPtrEquivInt(string_idx);
3661
3662 return EmitArrayGEP(string_dex_cache_addr, string_idx_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08003663 irb_.getJObjectTy(), kObject);
Logan Chienbb4d12a2012-02-17 14:10:01 +08003664}
3665
3666
Logan Chien83426162011-12-09 09:29:50 +08003667CompiledMethod *MethodCompiler::Compile() {
Logan Chien0b827102011-12-20 19:46:14 +08003668 // Code generation
3669 CreateFunction();
3670
3671 EmitPrologue();
3672 EmitInstructions();
Logan Chienc670a8d2011-12-20 21:25:56 +08003673 EmitPrologueLastBranch();
Logan Chien0b827102011-12-20 19:46:14 +08003674
Logan Chiend6c239a2011-12-23 15:11:45 +08003675 // Verify the generated bitcode
3676 llvm::verifyFunction(*func_, llvm::PrintMessageAction);
3677
Logan Chien8b977d32012-02-21 19:14:55 +08003678 // Add the memory usage approximation of the compilation unit
3679 cunit_->AddMemUsageApproximation(code_item_->insns_size_in_code_units_ * 900);
3680 // NOTE: From statistic, the bitcode size is 4.5 times bigger than the
3681 // Dex file. Besides, we have to convert the code unit into bytes.
3682 // Thus, we got our magic number 9.
3683
Logan Chien110bcba2012-04-16 19:11:28 +08003684 CompiledMethod* compiled_method =
3685 new CompiledMethod(cunit_->GetInstructionSet(),
3686 cunit_->GetElfIndex(),
3687 elf_func_idx_);
3688
3689 cunit_->RegisterCompiledMethod(func_, compiled_method);
3690
3691 return compiled_method;
Logan Chien0b827102011-12-20 19:46:14 +08003692}
3693
3694
3695llvm::Value* MethodCompiler::EmitLoadMethodObjectAddr() {
3696 return func_->arg_begin();
Shih-wei Liaod1fec812012-02-13 09:51:10 -08003697}
Logan Chien83426162011-12-09 09:29:50 +08003698
3699
Logan Chien5bcc04e2012-01-30 14:15:12 +08003700void MethodCompiler::EmitBranchExceptionLandingPad(uint32_t dex_pc) {
3701 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
3702 irb_.CreateBr(lpad);
3703 } else {
3704 irb_.CreateBr(GetUnwindBasicBlock());
3705 }
3706}
3707
3708
3709void MethodCompiler::EmitGuard_ExceptionLandingPad(uint32_t dex_pc) {
3710 llvm::Value* exception_pending =
3711 irb_.CreateCall(irb_.GetRuntime(IsExceptionPending));
3712
3713 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
3714
3715 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
3716 irb_.CreateCondBr(exception_pending, lpad, block_cont);
3717 } else {
3718 irb_.CreateCondBr(exception_pending, GetUnwindBasicBlock(), block_cont);
3719 }
3720
3721 irb_.SetInsertPoint(block_cont);
3722}
3723
3724
Logan Chien924072f2012-01-30 15:07:24 +08003725void MethodCompiler::EmitGuard_GarbageCollectionSuspend(uint32_t dex_pc) {
3726 llvm::Value* runtime_func = irb_.GetRuntime(TestSuspend);
Logan Chien8dfcbea2012-02-17 18:50:32 +08003727 EmitUpdateLineNumFromDexPC(dex_pc);
Logan Chien924072f2012-01-30 15:07:24 +08003728 irb_.CreateCall(runtime_func);
3729
3730 EmitGuard_ExceptionLandingPad(dex_pc);
3731}
3732
3733
Logan Chiend6c239a2011-12-23 15:11:45 +08003734llvm::BasicBlock* MethodCompiler::
3735CreateBasicBlockWithDexPC(uint32_t dex_pc, char const* postfix) {
3736 std::string name;
3737
3738 if (postfix) {
3739 StringAppendF(&name, "B%u.%s", dex_pc, postfix);
3740 } else {
3741 StringAppendF(&name, "B%u", dex_pc);
3742 }
3743
3744 return llvm::BasicBlock::Create(*context_, name, func_);
3745}
3746
3747
3748llvm::BasicBlock* MethodCompiler::GetBasicBlock(uint32_t dex_pc) {
3749 DCHECK(dex_pc < code_item_->insns_size_in_code_units_);
3750
3751 llvm::BasicBlock* basic_block = basic_blocks_[dex_pc];
3752
3753 if (!basic_block) {
3754 basic_block = CreateBasicBlockWithDexPC(dex_pc);
3755 basic_blocks_[dex_pc] = basic_block;
3756 }
3757
3758 return basic_block;
3759}
3760
3761
3762llvm::BasicBlock*
3763MethodCompiler::GetNextBasicBlock(uint32_t dex_pc) {
3764 Instruction const* insn = Instruction::At(code_item_->insns_ + dex_pc);
3765 return GetBasicBlock(dex_pc + insn->SizeInCodeUnits());
3766}
3767
3768
Logan Chien5bcc04e2012-01-30 14:15:12 +08003769int32_t MethodCompiler::GetTryItemOffset(uint32_t dex_pc) {
3770 // TODO: Since we are emitting the dex instructions in ascending order
3771 // w.r.t. address, we can cache the lastest try item offset so that we
3772 // don't have to do binary search for every query.
3773
3774 int32_t min = 0;
3775 int32_t max = code_item_->tries_size_ - 1;
3776
3777 while (min <= max) {
3778 int32_t mid = min + (max - min) / 2;
3779
3780 DexFile::TryItem const* ti = DexFile::GetTryItems(*code_item_, mid);
3781 uint32_t start = ti->start_addr_;
3782 uint32_t end = start + ti->insn_count_;
3783
3784 if (dex_pc < start) {
3785 max = mid - 1;
3786 } else if (dex_pc >= end) {
3787 min = mid + 1;
3788 } else {
3789 return mid; // found
3790 }
3791 }
3792
3793 return -1; // not found
3794}
3795
3796
3797llvm::BasicBlock* MethodCompiler::GetLandingPadBasicBlock(uint32_t dex_pc) {
3798 // Find the try item for this address in this method
3799 int32_t ti_offset = GetTryItemOffset(dex_pc);
3800
3801 if (ti_offset == -1) {
3802 return NULL; // No landing pad is available for this address.
3803 }
3804
3805 // Check for the existing landing pad basic block
3806 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
3807 llvm::BasicBlock* block_lpad = basic_block_landing_pads_[ti_offset];
3808
3809 if (block_lpad) {
3810 // We have generated landing pad for this try item already. Return the
3811 // same basic block.
3812 return block_lpad;
3813 }
3814
3815 // Get try item from code item
3816 DexFile::TryItem const* ti = DexFile::GetTryItems(*code_item_, ti_offset);
3817
3818 // Create landing pad basic block
3819 block_lpad = llvm::BasicBlock::Create(*context_,
3820 StringPrintf("lpad%d", ti_offset),
3821 func_);
3822
3823 // Change IRBuilder insert point
3824 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3825 irb_.SetInsertPoint(block_lpad);
3826
3827 // Find catch block with matching type
3828 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
3829
3830 // TODO: Maybe passing try item offset will be a better idea? For now,
3831 // we are passing dex_pc, so that we can use existing runtime support
3832 // function directly. However, in the runtime supporting function we
3833 // have to search for try item with binary search which can be
3834 // eliminated.
3835 llvm::Value* dex_pc_value = irb_.getInt32(ti->start_addr_);
3836
3837 llvm::Value* catch_handler_index_value =
3838 irb_.CreateCall2(irb_.GetRuntime(FindCatchBlock),
3839 method_object_addr, dex_pc_value);
3840
3841 // Switch instruction (Go to unwind basic block by default)
3842 llvm::SwitchInst* sw =
3843 irb_.CreateSwitch(catch_handler_index_value, GetUnwindBasicBlock());
3844
3845 // Cases with matched catch block
3846 CatchHandlerIterator iter(*code_item_, ti->start_addr_);
3847
3848 for (uint32_t c = 0; iter.HasNext(); iter.Next(), ++c) {
3849 sw->addCase(irb_.getInt32(c), GetBasicBlock(iter.GetHandlerAddress()));
3850 }
3851
3852 // Restore the orignal insert point for IRBuilder
3853 irb_.restoreIP(irb_ip_original);
3854
3855 // Cache this landing pad
3856 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
3857 basic_block_landing_pads_[ti_offset] = block_lpad;
3858
3859 return block_lpad;
3860}
3861
3862
3863llvm::BasicBlock* MethodCompiler::GetUnwindBasicBlock() {
3864 // Check the existing unwinding baisc block block
3865 if (basic_block_unwind_ != NULL) {
3866 return basic_block_unwind_;
3867 }
3868
3869 // Create new basic block for unwinding
3870 basic_block_unwind_ =
3871 llvm::BasicBlock::Create(*context_, "exception_unwind", func_);
3872
3873 // Change IRBuilder insert point
3874 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3875 irb_.SetInsertPoint(basic_block_unwind_);
3876
Logan Chien8dfcbea2012-02-17 18:50:32 +08003877 // Pop the shadow frame
3878 EmitPopShadowFrame();
3879
Logan Chien5bcc04e2012-01-30 14:15:12 +08003880 // Emit the code to return default value (zero) for the given return type.
Logan Chiendd361c92012-04-10 23:40:37 +08003881 char ret_shorty = oat_compilation_unit_->GetShorty()[0];
Logan Chien5bcc04e2012-01-30 14:15:12 +08003882 if (ret_shorty == 'V') {
3883 irb_.CreateRetVoid();
3884 } else {
3885 irb_.CreateRet(irb_.getJZero(ret_shorty));
3886 }
3887
3888 // Restore the orignal insert point for IRBuilder
3889 irb_.restoreIP(irb_ip_original);
3890
3891 return basic_block_unwind_;
3892}
3893
3894
Logan Chienc670a8d2011-12-20 21:25:56 +08003895llvm::Value* MethodCompiler::AllocDalvikLocalVarReg(RegCategory cat,
3896 uint32_t reg_idx) {
3897
3898 // Save current IR builder insert point
3899 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3900
3901 // Alloca
3902 llvm::Value* reg_addr = NULL;
3903
3904 switch (cat) {
3905 case kRegCat1nr:
3906 irb_.SetInsertPoint(basic_block_reg_alloca_);
3907 reg_addr = irb_.CreateAlloca(irb_.getJIntTy(), 0,
3908 StringPrintf("r%u", reg_idx));
3909
3910 irb_.SetInsertPoint(basic_block_reg_zero_init_);
3911 irb_.CreateStore(irb_.getJInt(0), reg_addr);
3912 break;
3913
3914 case kRegCat2:
3915 irb_.SetInsertPoint(basic_block_reg_alloca_);
3916 reg_addr = irb_.CreateAlloca(irb_.getJLongTy(), 0,
3917 StringPrintf("w%u", reg_idx));
3918
3919 irb_.SetInsertPoint(basic_block_reg_zero_init_);
3920 irb_.CreateStore(irb_.getJLong(0), reg_addr);
3921 break;
3922
3923 case kRegObject:
Logan Chien8dfcbea2012-02-17 18:50:32 +08003924 {
3925 irb_.SetInsertPoint(basic_block_shadow_frame_alloca_);
Logan Chienc670a8d2011-12-20 21:25:56 +08003926
Logan Chien8dfcbea2012-02-17 18:50:32 +08003927 llvm::Value* gep_index[] = {
3928 irb_.getInt32(0), // No pointer displacement
3929 irb_.getInt32(1), // SIRT
3930 irb_.getInt32(reg_idx) // Pointer field
3931 };
3932
3933 reg_addr = irb_.CreateGEP(shadow_frame_, gep_index,
3934 StringPrintf("p%u", reg_idx));
3935
3936 irb_.SetInsertPoint(basic_block_reg_zero_init_);
3937 irb_.CreateStore(irb_.getJNull(), reg_addr);
3938 }
Logan Chienc670a8d2011-12-20 21:25:56 +08003939 break;
3940
3941 default:
3942 LOG(FATAL) << "Unknown register category for allocation: " << cat;
3943 }
3944
3945 // Restore IRBuilder insert point
3946 irb_.restoreIP(irb_ip_original);
3947
3948 DCHECK_NE(reg_addr, static_cast<llvm::Value*>(NULL));
3949 return reg_addr;
3950}
3951
3952
3953llvm::Value* MethodCompiler::AllocDalvikRetValReg(RegCategory cat) {
3954 // Save current IR builder insert point
3955 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3956
3957 // Alloca
3958 llvm::Value* reg_addr = NULL;
3959
3960 switch (cat) {
3961 case kRegCat1nr:
3962 irb_.SetInsertPoint(basic_block_reg_alloca_);
3963 reg_addr = irb_.CreateAlloca(irb_.getJIntTy(), 0, "r_res");
3964 break;
3965
3966 case kRegCat2:
3967 irb_.SetInsertPoint(basic_block_reg_alloca_);
3968 reg_addr = irb_.CreateAlloca(irb_.getJLongTy(), 0, "w_res");
3969 break;
3970
3971 case kRegObject:
3972 irb_.SetInsertPoint(basic_block_reg_alloca_);
3973 reg_addr = irb_.CreateAlloca(irb_.getJObjectTy(), 0, "p_res");
3974 break;
3975
3976 default:
3977 LOG(FATAL) << "Unknown register category for allocation: " << cat;
3978 }
3979
3980 // Restore IRBuilder insert point
3981 irb_.restoreIP(irb_ip_original);
3982
3983 DCHECK_NE(reg_addr, static_cast<llvm::Value*>(NULL));
3984 return reg_addr;
3985}
3986
3987
Logan Chien8dfcbea2012-02-17 18:50:32 +08003988void MethodCompiler::EmitPopShadowFrame() {
3989 irb_.CreateCall(irb_.GetRuntime(PopShadowFrame));
3990}
3991
3992
3993void MethodCompiler::EmitUpdateLineNum(int32_t line_num) {
Logan Chien1b0a1b72012-03-15 06:20:17 +08003994 llvm::Value* line_num_field_addr =
3995 irb_.CreatePtrDisp(shadow_frame_,
3996 irb_.getPtrEquivInt(ShadowFrame::LineNumOffset()),
3997 irb_.getJIntTy()->getPointerTo());
Logan Chien8dfcbea2012-02-17 18:50:32 +08003998
Logan Chien8dfcbea2012-02-17 18:50:32 +08003999 llvm::ConstantInt* line_num_value = irb_.getInt32(line_num);
4000 irb_.CreateStore(line_num_value, line_num_field_addr);
4001}
4002
4003
4004void MethodCompiler::EmitUpdateLineNumFromDexPC(uint32_t dex_pc) {
Logan Chiendd361c92012-04-10 23:40:37 +08004005 EmitUpdateLineNum(
4006 dex_file_->GetLineNumFromPC(oat_compilation_unit_->IsStatic(),
4007 method_idx_, code_item_, dex_pc));
Logan Chien8dfcbea2012-02-17 18:50:32 +08004008}
4009
4010
Logan Chien83426162011-12-09 09:29:50 +08004011} // namespace compiler_llvm
4012} // namespace art