blob: e044c54b44bd87e122a0d0ca06d7560a731f02ca [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 Chiendd361c92012-04-10 23:40:37 +080022#include "dex_verifier.h"
Logan Chiena78e3c82011-12-27 17:59:35 +080023#include "inferred_reg_category_map.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080024#include "ir_builder.h"
25#include "logging.h"
Logan Chien4dd96f52012-02-29 01:26:58 +080026#include "oat_compilation_unit.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080027#include "object.h"
28#include "object_utils.h"
Logan Chien42e0e152012-01-13 15:42:36 +080029#include "runtime_support_func.h"
TDYa1275bb86012012-04-11 05:57:28 -070030#include "runtime_support_llvm.h"
Logan Chien1b0a1b72012-03-15 06:20:17 +080031#include "shadow_frame.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080032#include "stl_util.h"
Logan Chien0b827102011-12-20 19:46:14 +080033#include "stringprintf.h"
34#include "utils_llvm.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),
65 basic_block_reg_alloca_(NULL), basic_block_shadow_frame_alloca_(NULL),
66 basic_block_reg_zero_init_(NULL), basic_block_reg_arg_init_(NULL),
67 basic_blocks_(code_item_->insns_size_in_code_units_),
68 basic_block_landing_pads_(code_item_->tries_size_, NULL),
69 basic_block_unwind_(NULL), basic_block_unreachable_(NULL),
Logan Chien937105a2012-04-02 02:37:37 +080070 shadow_frame_(NULL), elf_func_idx_(cunit_->AcquireUniqueElfFuncIndex()) {
Shih-wei Liaod1fec812012-02-13 09:51:10 -080071}
72
73
74MethodCompiler::~MethodCompiler() {
Logan Chienc670a8d2011-12-20 21:25:56 +080075 STLDeleteElements(&regs_);
Shih-wei Liaod1fec812012-02-13 09:51:10 -080076}
77
78
Logan Chien0b827102011-12-20 19:46:14 +080079void MethodCompiler::CreateFunction() {
80 // LLVM function name
Logan Chien937105a2012-04-02 02:37:37 +080081 std::string func_name(ElfFuncName(elf_func_idx_));
Logan Chien0b827102011-12-20 19:46:14 +080082
83 // Get function type
84 llvm::FunctionType* func_type =
Logan Chiendd361c92012-04-10 23:40:37 +080085 GetFunctionType(method_idx_, oat_compilation_unit_->IsStatic());
Logan Chien0b827102011-12-20 19:46:14 +080086
87 // Create function
88 func_ = llvm::Function::Create(func_type, llvm::Function::ExternalLinkage,
89 func_name, module_);
90
91 // Set argument name
92 llvm::Function::arg_iterator arg_iter(func_->arg_begin());
93 llvm::Function::arg_iterator arg_end(func_->arg_end());
94
95 DCHECK_NE(arg_iter, arg_end);
96 arg_iter->setName("method");
97 ++arg_iter;
98
Logan Chiendd361c92012-04-10 23:40:37 +080099 if (!oat_compilation_unit_->IsStatic()) {
Logan Chien0b827102011-12-20 19:46:14 +0800100 DCHECK_NE(arg_iter, arg_end);
101 arg_iter->setName("this");
102 ++arg_iter;
103 }
104
105 for (unsigned i = 0; arg_iter != arg_end; ++i, ++arg_iter) {
106 arg_iter->setName(StringPrintf("a%u", i));
107 }
108}
109
110
111llvm::FunctionType* MethodCompiler::GetFunctionType(uint32_t method_idx,
112 bool is_static) {
113 // Get method signature
114 DexFile::MethodId const& method_id = dex_file_->GetMethodId(method_idx);
115
Logan Chien8faf8022012-02-24 12:25:29 +0800116 uint32_t shorty_size;
Logan Chien0b827102011-12-20 19:46:14 +0800117 char const* shorty = dex_file_->GetMethodShorty(method_id, &shorty_size);
Logan Chien8faf8022012-02-24 12:25:29 +0800118 CHECK_GE(shorty_size, 1u);
Logan Chien0b827102011-12-20 19:46:14 +0800119
120 // Get return type
121 llvm::Type* ret_type = irb_.getJType(shorty[0], kAccurate);
122
123 // Get argument type
124 std::vector<llvm::Type*> args_type;
125
126 args_type.push_back(irb_.getJObjectTy()); // method object pointer
127
128 if (!is_static) {
129 args_type.push_back(irb_.getJType('L', kAccurate)); // "this" object pointer
130 }
131
Logan Chien8faf8022012-02-24 12:25:29 +0800132 for (uint32_t i = 1; i < shorty_size; ++i) {
Logan Chien0b827102011-12-20 19:46:14 +0800133 args_type.push_back(irb_.getJType(shorty[i], kAccurate));
134 }
135
136 return llvm::FunctionType::get(ret_type, args_type, false);
137}
138
139
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800140void MethodCompiler::EmitPrologue() {
Logan Chienc670a8d2011-12-20 21:25:56 +0800141 // Create basic blocks for prologue
TDYa1274165a832012-04-03 17:47:16 -0700142 basic_block_stack_overflow_ =
143 llvm::BasicBlock::Create(*context_, "prologue.stack_overflow_check", func_);
144
Logan Chienc670a8d2011-12-20 21:25:56 +0800145 basic_block_reg_alloca_ =
146 llvm::BasicBlock::Create(*context_, "prologue.alloca", func_);
147
Logan Chien8dfcbea2012-02-17 18:50:32 +0800148 basic_block_shadow_frame_alloca_ =
149 llvm::BasicBlock::Create(*context_, "prologue.shadowframe", func_);
150
Logan Chienc670a8d2011-12-20 21:25:56 +0800151 basic_block_reg_zero_init_ =
152 llvm::BasicBlock::Create(*context_, "prologue.zeroinit", func_);
153
Logan Chiend6ececa2011-12-27 16:20:15 +0800154 basic_block_reg_arg_init_ =
155 llvm::BasicBlock::Create(*context_, "prologue.arginit", func_);
156
TDYa1274165a832012-04-03 17:47:16 -0700157 // Before alloca, check stack overflow.
158 EmitStackOverflowCheck();
159
Logan Chienc670a8d2011-12-20 21:25:56 +0800160 // Create register array
161 for (uint16_t r = 0; r < code_item_->registers_size_; ++r) {
162 regs_.push_back(DalvikReg::CreateLocalVarReg(*this, r));
163 }
164
165 retval_reg_.reset(DalvikReg::CreateRetValReg(*this));
Logan Chiend6ececa2011-12-27 16:20:15 +0800166
Logan Chien8dfcbea2012-02-17 18:50:32 +0800167 // Create Shadow Frame
168 EmitPrologueAllocShadowFrame();
169
Logan Chiend6ececa2011-12-27 16:20:15 +0800170 // Store argument to dalvik register
171 irb_.SetInsertPoint(basic_block_reg_arg_init_);
172 EmitPrologueAssignArgRegister();
173
174 // Branch to start address
175 irb_.CreateBr(GetBasicBlock(0));
Logan Chienc670a8d2011-12-20 21:25:56 +0800176}
177
178
TDYa1274165a832012-04-03 17:47:16 -0700179void MethodCompiler::EmitStackOverflowCheck() {
180 irb_.SetInsertPoint(basic_block_stack_overflow_);
181
182 // Call llvm intrinsic function to get frame address.
183 llvm::Function* frameaddress =
184 llvm::Intrinsic::getDeclaration(module_, llvm::Intrinsic::frameaddress);
185
186 // The type of llvm::frameaddress is: i8* @llvm.frameaddress(i32)
187 llvm::Value* frame_address = irb_.CreateCall(frameaddress, irb_.getInt32(0));
188
189 // Cast i8* to int
190 frame_address = irb_.CreatePtrToInt(frame_address, irb_.getPtrEquivIntTy());
191
192 // Get thread.stack_end_
193 llvm::Value* thread_object_addr =
194 irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
195
196 llvm::Value* stack_end_addr =
197 irb_.CreatePtrDisp(thread_object_addr,
198 irb_.getPtrEquivInt(Thread::StackEndOffset().Int32Value()),
199 irb_.getPtrEquivIntTy()->getPointerTo());
200
201 llvm::Value* stack_end = irb_.CreateLoad(stack_end_addr);
202
203 // Check the frame address < thread.stack_end_ ?
204 llvm::Value* is_stack_overflow = irb_.CreateICmpULT(frame_address, stack_end);
205
206 llvm::BasicBlock* block_exception =
207 llvm::BasicBlock::Create(*context_, "stack_overflow", func_);
208
209 llvm::BasicBlock* block_continue =
210 llvm::BasicBlock::Create(*context_, "stack_overflow_cont", func_);
211
212 irb_.CreateCondBr(is_stack_overflow, block_exception, block_continue);
213
214 // If stack overflow, throw exception.
215 irb_.SetInsertPoint(block_exception);
216 irb_.CreateCall(irb_.GetRuntime(ThrowStackOverflowException));
217
218 // Unwind.
Logan Chiendd361c92012-04-10 23:40:37 +0800219 char ret_shorty = oat_compilation_unit_->GetShorty()[0];
TDYa1274165a832012-04-03 17:47:16 -0700220 if (ret_shorty == 'V') {
221 irb_.CreateRetVoid();
222 } else {
223 irb_.CreateRet(irb_.getJZero(ret_shorty));
224 }
225
226 basic_block_stack_overflow_ = block_continue;
227}
228
229
Logan Chienc670a8d2011-12-20 21:25:56 +0800230void MethodCompiler::EmitPrologueLastBranch() {
TDYa1274165a832012-04-03 17:47:16 -0700231 irb_.SetInsertPoint(basic_block_stack_overflow_);
232 irb_.CreateBr(basic_block_reg_alloca_);
233
Logan Chienc670a8d2011-12-20 21:25:56 +0800234 irb_.SetInsertPoint(basic_block_reg_alloca_);
Logan Chien8dfcbea2012-02-17 18:50:32 +0800235 irb_.CreateBr(basic_block_shadow_frame_alloca_);
236
237 irb_.SetInsertPoint(basic_block_shadow_frame_alloca_);
Logan Chienc670a8d2011-12-20 21:25:56 +0800238 irb_.CreateBr(basic_block_reg_zero_init_);
239
240 irb_.SetInsertPoint(basic_block_reg_zero_init_);
Logan Chiend6ececa2011-12-27 16:20:15 +0800241 irb_.CreateBr(basic_block_reg_arg_init_);
242}
243
244
Logan Chien8dfcbea2012-02-17 18:50:32 +0800245void MethodCompiler::EmitPrologueAllocShadowFrame() {
246 irb_.SetInsertPoint(basic_block_shadow_frame_alloca_);
247
248 // Allocate the shadow frame now!
249 uint32_t sirt_size = code_item_->registers_size_;
250 // TODO: registers_size_ is a bad approximation. Compute a
251 // tighter approximation at Dex verifier while performing data-flow
252 // analysis.
253
254 llvm::StructType* shadow_frame_type = irb_.getShadowFrameTy(sirt_size);
255 shadow_frame_ = irb_.CreateAlloca(shadow_frame_type);
256
257 // Zero-initialization of the shadow frame
258 llvm::ConstantAggregateZero* zero_initializer =
259 llvm::ConstantAggregateZero::get(shadow_frame_type);
260
261 irb_.CreateStore(zero_initializer, shadow_frame_);
262
Logan Chien8dfcbea2012-02-17 18:50:32 +0800263 // Store the method pointer
Logan Chien1b0a1b72012-03-15 06:20:17 +0800264 llvm::Value* method_field_addr =
265 irb_.CreatePtrDisp(shadow_frame_,
266 irb_.getPtrEquivInt(ShadowFrame::MethodOffset()),
267 irb_.getJObjectTy()->getPointerTo());
268
Logan Chien8dfcbea2012-02-17 18:50:32 +0800269 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
270 irb_.CreateStore(method_object_addr, method_field_addr);
271
272 // Store the number of the pointer slots
Logan Chien1b0a1b72012-03-15 06:20:17 +0800273 llvm::ConstantInt* num_of_refs_offset =
274 irb_.getPtrEquivInt(ShadowFrame::NumberOfReferencesOffset());
275
276 llvm::Value* num_of_refs_field_addr =
277 irb_.CreatePtrDisp(shadow_frame_, num_of_refs_offset,
278 irb_.getJIntTy()->getPointerTo());
279
280 llvm::ConstantInt* num_of_refs_value = irb_.getJInt(sirt_size);
281 irb_.CreateStore(num_of_refs_value, num_of_refs_field_addr);
Logan Chien8dfcbea2012-02-17 18:50:32 +0800282
283 // Push the shadow frame
284 llvm::Value* shadow_frame_upcast =
285 irb_.CreateConstGEP2_32(shadow_frame_, 0, 0);
286
287 irb_.CreateCall(irb_.GetRuntime(PushShadowFrame), shadow_frame_upcast);
288}
289
290
Logan Chiend6ececa2011-12-27 16:20:15 +0800291void MethodCompiler::EmitPrologueAssignArgRegister() {
292 uint16_t arg_reg = code_item_->registers_size_ - code_item_->ins_size_;
293
294 llvm::Function::arg_iterator arg_iter(func_->arg_begin());
295 llvm::Function::arg_iterator arg_end(func_->arg_end());
296
Logan Chiendd361c92012-04-10 23:40:37 +0800297 uint32_t shorty_size = 0;
298 char const* shorty = oat_compilation_unit_->GetShorty(&shorty_size);
299 CHECK_GE(shorty_size, 1u);
Logan Chiend6ececa2011-12-27 16:20:15 +0800300
301 ++arg_iter; // skip method object
302
Logan Chiendd361c92012-04-10 23:40:37 +0800303 if (!oat_compilation_unit_->IsStatic()) {
Logan Chiend6ececa2011-12-27 16:20:15 +0800304 EmitStoreDalvikReg(arg_reg, kObject, kAccurate, arg_iter);
305 ++arg_iter;
306 ++arg_reg;
307 }
308
Logan Chiendd361c92012-04-10 23:40:37 +0800309 for (uint32_t i = 1; i < shorty_size; ++i, ++arg_iter) {
Logan Chiend6ececa2011-12-27 16:20:15 +0800310 EmitStoreDalvikReg(arg_reg, shorty[i], kAccurate, arg_iter);
311
312 ++arg_reg;
313 if (shorty[i] == 'J' || shorty[i] == 'D') {
314 // Wide types, such as long and double, are using a pair of registers
315 // to store the value, so we have to increase arg_reg again.
316 ++arg_reg;
317 }
318 }
319
320 DCHECK_EQ(arg_end, arg_iter);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800321}
322
323
Logan Chien83426162011-12-09 09:29:50 +0800324void MethodCompiler::EmitInstructions() {
Logan Chiend6c239a2011-12-23 15:11:45 +0800325 uint32_t dex_pc = 0;
326 while (dex_pc < code_item_->insns_size_in_code_units_) {
327 Instruction const* insn = Instruction::At(code_item_->insns_ + dex_pc);
328 EmitInstruction(dex_pc, insn);
329 dex_pc += insn->SizeInCodeUnits();
330 }
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800331}
332
333
Logan Chien83426162011-12-09 09:29:50 +0800334void MethodCompiler::EmitInstruction(uint32_t dex_pc,
335 Instruction const* insn) {
Logan Chiend6c239a2011-12-23 15:11:45 +0800336
337 // Set the IRBuilder insertion point
338 irb_.SetInsertPoint(GetBasicBlock(dex_pc));
339
Logan Chien70f94b42011-12-27 17:49:11 +0800340#define ARGS dex_pc, insn
341
342 // Dispatch the instruction
343 switch (insn->Opcode()) {
344 case Instruction::NOP:
345 EmitInsn_Nop(ARGS);
346 break;
347
348 case Instruction::MOVE:
349 case Instruction::MOVE_FROM16:
350 case Instruction::MOVE_16:
351 EmitInsn_Move(ARGS, kInt);
352 break;
353
354 case Instruction::MOVE_WIDE:
355 case Instruction::MOVE_WIDE_FROM16:
356 case Instruction::MOVE_WIDE_16:
357 EmitInsn_Move(ARGS, kLong);
358 break;
359
360 case Instruction::MOVE_OBJECT:
361 case Instruction::MOVE_OBJECT_FROM16:
362 case Instruction::MOVE_OBJECT_16:
363 EmitInsn_Move(ARGS, kObject);
364 break;
365
366 case Instruction::MOVE_RESULT:
367 EmitInsn_MoveResult(ARGS, kInt);
368 break;
369
370 case Instruction::MOVE_RESULT_WIDE:
371 EmitInsn_MoveResult(ARGS, kLong);
372 break;
373
374 case Instruction::MOVE_RESULT_OBJECT:
375 EmitInsn_MoveResult(ARGS, kObject);
376 break;
377
378 case Instruction::MOVE_EXCEPTION:
379 EmitInsn_MoveException(ARGS);
380 break;
381
382 case Instruction::RETURN_VOID:
383 EmitInsn_ReturnVoid(ARGS);
384 break;
385
386 case Instruction::RETURN:
387 case Instruction::RETURN_WIDE:
388 case Instruction::RETURN_OBJECT:
389 EmitInsn_Return(ARGS);
390 break;
391
392 case Instruction::CONST_4:
393 case Instruction::CONST_16:
394 case Instruction::CONST:
395 case Instruction::CONST_HIGH16:
396 EmitInsn_LoadConstant(ARGS, kInt);
397 break;
398
399 case Instruction::CONST_WIDE_16:
400 case Instruction::CONST_WIDE_32:
401 case Instruction::CONST_WIDE:
402 case Instruction::CONST_WIDE_HIGH16:
403 EmitInsn_LoadConstant(ARGS, kLong);
404 break;
405
406 case Instruction::CONST_STRING:
407 case Instruction::CONST_STRING_JUMBO:
408 EmitInsn_LoadConstantString(ARGS);
409 break;
410
411 case Instruction::CONST_CLASS:
412 EmitInsn_LoadConstantClass(ARGS);
413 break;
414
415 case Instruction::MONITOR_ENTER:
416 EmitInsn_MonitorEnter(ARGS);
417 break;
418
419 case Instruction::MONITOR_EXIT:
420 EmitInsn_MonitorExit(ARGS);
421 break;
422
423 case Instruction::CHECK_CAST:
424 EmitInsn_CheckCast(ARGS);
425 break;
426
427 case Instruction::INSTANCE_OF:
428 EmitInsn_InstanceOf(ARGS);
429 break;
430
431 case Instruction::ARRAY_LENGTH:
432 EmitInsn_ArrayLength(ARGS);
433 break;
434
435 case Instruction::NEW_INSTANCE:
436 EmitInsn_NewInstance(ARGS);
437 break;
438
439 case Instruction::NEW_ARRAY:
440 EmitInsn_NewArray(ARGS);
441 break;
442
443 case Instruction::FILLED_NEW_ARRAY:
444 EmitInsn_FilledNewArray(ARGS, false);
445 break;
446
447 case Instruction::FILLED_NEW_ARRAY_RANGE:
448 EmitInsn_FilledNewArray(ARGS, true);
449 break;
450
451 case Instruction::FILL_ARRAY_DATA:
452 EmitInsn_FillArrayData(ARGS);
453 break;
454
455 case Instruction::THROW:
456 EmitInsn_ThrowException(ARGS);
457 break;
458
459 case Instruction::GOTO:
460 case Instruction::GOTO_16:
461 case Instruction::GOTO_32:
462 EmitInsn_UnconditionalBranch(ARGS);
463 break;
464
465 case Instruction::PACKED_SWITCH:
466 EmitInsn_PackedSwitch(ARGS);
467 break;
468
469 case Instruction::SPARSE_SWITCH:
470 EmitInsn_SparseSwitch(ARGS);
471 break;
472
473 case Instruction::CMPL_FLOAT:
474 EmitInsn_FPCompare(ARGS, kFloat, false);
475 break;
476
477 case Instruction::CMPG_FLOAT:
478 EmitInsn_FPCompare(ARGS, kFloat, true);
479 break;
480
481 case Instruction::CMPL_DOUBLE:
482 EmitInsn_FPCompare(ARGS, kDouble, false);
483 break;
484
485 case Instruction::CMPG_DOUBLE:
486 EmitInsn_FPCompare(ARGS, kDouble, true);
487 break;
488
489 case Instruction::CMP_LONG:
490 EmitInsn_LongCompare(ARGS);
491 break;
492
493 case Instruction::IF_EQ:
494 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_EQ);
495 break;
496
497 case Instruction::IF_NE:
498 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_NE);
499 break;
500
501 case Instruction::IF_LT:
502 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_LT);
503 break;
504
505 case Instruction::IF_GE:
506 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_GE);
507 break;
508
509 case Instruction::IF_GT:
510 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_GT);
511 break;
512
513 case Instruction::IF_LE:
514 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_LE);
515 break;
516
517 case Instruction::IF_EQZ:
518 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_EQ);
519 break;
520
521 case Instruction::IF_NEZ:
522 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_NE);
523 break;
524
525 case Instruction::IF_LTZ:
526 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_LT);
527 break;
528
529 case Instruction::IF_GEZ:
530 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_GE);
531 break;
532
533 case Instruction::IF_GTZ:
534 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_GT);
535 break;
536
537 case Instruction::IF_LEZ:
538 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_LE);
539 break;
540
541 case Instruction::AGET:
542 EmitInsn_AGet(ARGS, kInt);
543 break;
544
545 case Instruction::AGET_WIDE:
546 EmitInsn_AGet(ARGS, kLong);
547 break;
548
549 case Instruction::AGET_OBJECT:
550 EmitInsn_AGet(ARGS, kObject);
551 break;
552
553 case Instruction::AGET_BOOLEAN:
554 EmitInsn_AGet(ARGS, kBoolean);
555 break;
556
557 case Instruction::AGET_BYTE:
558 EmitInsn_AGet(ARGS, kByte);
559 break;
560
561 case Instruction::AGET_CHAR:
562 EmitInsn_AGet(ARGS, kChar);
563 break;
564
565 case Instruction::AGET_SHORT:
566 EmitInsn_AGet(ARGS, kShort);
567 break;
568
569 case Instruction::APUT:
570 EmitInsn_APut(ARGS, kInt);
571 break;
572
573 case Instruction::APUT_WIDE:
574 EmitInsn_APut(ARGS, kLong);
575 break;
576
577 case Instruction::APUT_OBJECT:
578 EmitInsn_APut(ARGS, kObject);
579 break;
580
581 case Instruction::APUT_BOOLEAN:
582 EmitInsn_APut(ARGS, kBoolean);
583 break;
584
585 case Instruction::APUT_BYTE:
586 EmitInsn_APut(ARGS, kByte);
587 break;
588
589 case Instruction::APUT_CHAR:
590 EmitInsn_APut(ARGS, kChar);
591 break;
592
593 case Instruction::APUT_SHORT:
594 EmitInsn_APut(ARGS, kShort);
595 break;
596
597 case Instruction::IGET:
598 EmitInsn_IGet(ARGS, kInt);
599 break;
600
601 case Instruction::IGET_WIDE:
602 EmitInsn_IGet(ARGS, kLong);
603 break;
604
605 case Instruction::IGET_OBJECT:
606 EmitInsn_IGet(ARGS, kObject);
607 break;
608
609 case Instruction::IGET_BOOLEAN:
610 EmitInsn_IGet(ARGS, kBoolean);
611 break;
612
613 case Instruction::IGET_BYTE:
614 EmitInsn_IGet(ARGS, kByte);
615 break;
616
617 case Instruction::IGET_CHAR:
618 EmitInsn_IGet(ARGS, kChar);
619 break;
620
621 case Instruction::IGET_SHORT:
622 EmitInsn_IGet(ARGS, kShort);
623 break;
624
625 case Instruction::IPUT:
626 EmitInsn_IPut(ARGS, kInt);
627 break;
628
629 case Instruction::IPUT_WIDE:
630 EmitInsn_IPut(ARGS, kLong);
631 break;
632
633 case Instruction::IPUT_OBJECT:
634 EmitInsn_IPut(ARGS, kObject);
635 break;
636
637 case Instruction::IPUT_BOOLEAN:
638 EmitInsn_IPut(ARGS, kBoolean);
639 break;
640
641 case Instruction::IPUT_BYTE:
642 EmitInsn_IPut(ARGS, kByte);
643 break;
644
645 case Instruction::IPUT_CHAR:
646 EmitInsn_IPut(ARGS, kChar);
647 break;
648
649 case Instruction::IPUT_SHORT:
650 EmitInsn_IPut(ARGS, kShort);
651 break;
652
653 case Instruction::SGET:
654 EmitInsn_SGet(ARGS, kInt);
655 break;
656
657 case Instruction::SGET_WIDE:
658 EmitInsn_SGet(ARGS, kLong);
659 break;
660
661 case Instruction::SGET_OBJECT:
662 EmitInsn_SGet(ARGS, kObject);
663 break;
664
665 case Instruction::SGET_BOOLEAN:
666 EmitInsn_SGet(ARGS, kBoolean);
667 break;
668
669 case Instruction::SGET_BYTE:
670 EmitInsn_SGet(ARGS, kByte);
671 break;
672
673 case Instruction::SGET_CHAR:
674 EmitInsn_SGet(ARGS, kChar);
675 break;
676
677 case Instruction::SGET_SHORT:
678 EmitInsn_SGet(ARGS, kShort);
679 break;
680
681 case Instruction::SPUT:
682 EmitInsn_SPut(ARGS, kInt);
683 break;
684
685 case Instruction::SPUT_WIDE:
686 EmitInsn_SPut(ARGS, kLong);
687 break;
688
689 case Instruction::SPUT_OBJECT:
690 EmitInsn_SPut(ARGS, kObject);
691 break;
692
693 case Instruction::SPUT_BOOLEAN:
694 EmitInsn_SPut(ARGS, kBoolean);
695 break;
696
697 case Instruction::SPUT_BYTE:
698 EmitInsn_SPut(ARGS, kByte);
699 break;
700
701 case Instruction::SPUT_CHAR:
702 EmitInsn_SPut(ARGS, kChar);
703 break;
704
705 case Instruction::SPUT_SHORT:
706 EmitInsn_SPut(ARGS, kShort);
707 break;
708
709
710 case Instruction::INVOKE_VIRTUAL:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800711 EmitInsn_Invoke(ARGS, kVirtual, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800712 break;
713
714 case Instruction::INVOKE_SUPER:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800715 EmitInsn_Invoke(ARGS, kSuper, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800716 break;
717
718 case Instruction::INVOKE_DIRECT:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800719 EmitInsn_Invoke(ARGS, kDirect, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800720 break;
721
722 case Instruction::INVOKE_STATIC:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800723 EmitInsn_Invoke(ARGS, kStatic, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800724 break;
725
726 case Instruction::INVOKE_INTERFACE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800727 EmitInsn_Invoke(ARGS, kInterface, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800728 break;
729
730 case Instruction::INVOKE_VIRTUAL_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800731 EmitInsn_Invoke(ARGS, kVirtual, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800732 break;
733
734 case Instruction::INVOKE_SUPER_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800735 EmitInsn_Invoke(ARGS, kSuper, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800736 break;
737
738 case Instruction::INVOKE_DIRECT_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800739 EmitInsn_Invoke(ARGS, kDirect, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800740 break;
741
742 case Instruction::INVOKE_STATIC_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800743 EmitInsn_Invoke(ARGS, kStatic, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800744 break;
745
746 case Instruction::INVOKE_INTERFACE_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800747 EmitInsn_Invoke(ARGS, kInterface, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800748 break;
749
750 case Instruction::NEG_INT:
751 EmitInsn_Neg(ARGS, kInt);
752 break;
753
754 case Instruction::NOT_INT:
755 EmitInsn_Not(ARGS, kInt);
756 break;
757
758 case Instruction::NEG_LONG:
759 EmitInsn_Neg(ARGS, kLong);
760 break;
761
762 case Instruction::NOT_LONG:
763 EmitInsn_Not(ARGS, kLong);
764 break;
765
766 case Instruction::NEG_FLOAT:
767 EmitInsn_FNeg(ARGS, kFloat);
768 break;
769
770 case Instruction::NEG_DOUBLE:
771 EmitInsn_FNeg(ARGS, kDouble);
772 break;
773
774 case Instruction::INT_TO_LONG:
775 EmitInsn_SExt(ARGS);
776 break;
777
778 case Instruction::INT_TO_FLOAT:
779 EmitInsn_IntToFP(ARGS, kInt, kFloat);
780 break;
781
782 case Instruction::INT_TO_DOUBLE:
783 EmitInsn_IntToFP(ARGS, kInt, kDouble);
784 break;
785
786 case Instruction::LONG_TO_INT:
787 EmitInsn_Trunc(ARGS);
788 break;
789
790 case Instruction::LONG_TO_FLOAT:
791 EmitInsn_IntToFP(ARGS, kLong, kFloat);
792 break;
793
794 case Instruction::LONG_TO_DOUBLE:
795 EmitInsn_IntToFP(ARGS, kLong, kDouble);
796 break;
797
798 case Instruction::FLOAT_TO_INT:
TDYa127a4746872012-04-11 23:48:55 -0700799 EmitInsn_FPToInt(ARGS, kFloat, kInt, F2I);
Logan Chien70f94b42011-12-27 17:49:11 +0800800 break;
801
802 case Instruction::FLOAT_TO_LONG:
TDYa127a4746872012-04-11 23:48:55 -0700803 EmitInsn_FPToInt(ARGS, kFloat, kLong, F2L);
Logan Chien70f94b42011-12-27 17:49:11 +0800804 break;
805
806 case Instruction::FLOAT_TO_DOUBLE:
807 EmitInsn_FExt(ARGS);
808 break;
809
810 case Instruction::DOUBLE_TO_INT:
TDYa127a4746872012-04-11 23:48:55 -0700811 EmitInsn_FPToInt(ARGS, kDouble, kInt, D2I);
Logan Chien70f94b42011-12-27 17:49:11 +0800812 break;
813
814 case Instruction::DOUBLE_TO_LONG:
TDYa127a4746872012-04-11 23:48:55 -0700815 EmitInsn_FPToInt(ARGS, kDouble, kLong, D2L);
Logan Chien70f94b42011-12-27 17:49:11 +0800816 break;
817
818 case Instruction::DOUBLE_TO_FLOAT:
819 EmitInsn_FTrunc(ARGS);
820 break;
821
822 case Instruction::INT_TO_BYTE:
823 EmitInsn_TruncAndSExt(ARGS, 8);
824 break;
825
826 case Instruction::INT_TO_CHAR:
827 EmitInsn_TruncAndZExt(ARGS, 16);
828 break;
829
830 case Instruction::INT_TO_SHORT:
831 EmitInsn_TruncAndSExt(ARGS, 16);
832 break;
833
834 case Instruction::ADD_INT:
835 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kInt, false);
836 break;
837
838 case Instruction::SUB_INT:
839 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kInt, false);
840 break;
841
842 case Instruction::MUL_INT:
843 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kInt, false);
844 break;
845
846 case Instruction::DIV_INT:
847 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kInt, false);
848 break;
849
850 case Instruction::REM_INT:
851 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kInt, false);
852 break;
853
854 case Instruction::AND_INT:
855 EmitInsn_IntArithm(ARGS, kIntArithm_And, kInt, false);
856 break;
857
858 case Instruction::OR_INT:
859 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kInt, false);
860 break;
861
862 case Instruction::XOR_INT:
863 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kInt, false);
864 break;
865
866 case Instruction::SHL_INT:
Logan Chien5539ad02012-04-02 14:36:55 +0800867 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kInt, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800868 break;
869
870 case Instruction::SHR_INT:
Logan Chien5539ad02012-04-02 14:36:55 +0800871 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kInt, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800872 break;
873
874 case Instruction::USHR_INT:
Logan Chien5539ad02012-04-02 14:36:55 +0800875 EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kInt, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800876 break;
877
878 case Instruction::ADD_LONG:
879 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kLong, false);
880 break;
881
882 case Instruction::SUB_LONG:
883 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kLong, false);
884 break;
885
886 case Instruction::MUL_LONG:
887 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kLong, false);
888 break;
889
890 case Instruction::DIV_LONG:
891 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kLong, false);
892 break;
893
894 case Instruction::REM_LONG:
895 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kLong, false);
896 break;
897
898 case Instruction::AND_LONG:
899 EmitInsn_IntArithm(ARGS, kIntArithm_And, kLong, false);
900 break;
901
902 case Instruction::OR_LONG:
903 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kLong, false);
904 break;
905
906 case Instruction::XOR_LONG:
907 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kLong, false);
908 break;
909
910 case Instruction::SHL_LONG:
Logan Chien5539ad02012-04-02 14:36:55 +0800911 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kLong, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800912 break;
913
914 case Instruction::SHR_LONG:
Logan Chien5539ad02012-04-02 14:36:55 +0800915 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kLong, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800916 break;
917
918 case Instruction::USHR_LONG:
Logan Chien5539ad02012-04-02 14:36:55 +0800919 EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kLong, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800920 break;
921
922 case Instruction::ADD_FLOAT:
923 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kFloat, false);
924 break;
925
926 case Instruction::SUB_FLOAT:
927 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kFloat, false);
928 break;
929
930 case Instruction::MUL_FLOAT:
931 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kFloat, false);
932 break;
933
934 case Instruction::DIV_FLOAT:
935 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kFloat, false);
936 break;
937
938 case Instruction::REM_FLOAT:
939 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kFloat, false);
940 break;
941
942 case Instruction::ADD_DOUBLE:
943 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kDouble, false);
944 break;
945
946 case Instruction::SUB_DOUBLE:
947 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kDouble, false);
948 break;
949
950 case Instruction::MUL_DOUBLE:
951 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kDouble, false);
952 break;
953
954 case Instruction::DIV_DOUBLE:
955 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kDouble, false);
956 break;
957
958 case Instruction::REM_DOUBLE:
959 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kDouble, false);
960 break;
961
962 case Instruction::ADD_INT_2ADDR:
963 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kInt, true);
964 break;
965
966 case Instruction::SUB_INT_2ADDR:
967 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kInt, true);
968 break;
969
970 case Instruction::MUL_INT_2ADDR:
971 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kInt, true);
972 break;
973
974 case Instruction::DIV_INT_2ADDR:
975 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kInt, true);
976 break;
977
978 case Instruction::REM_INT_2ADDR:
979 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kInt, true);
980 break;
981
982 case Instruction::AND_INT_2ADDR:
983 EmitInsn_IntArithm(ARGS, kIntArithm_And, kInt, true);
984 break;
985
986 case Instruction::OR_INT_2ADDR:
987 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kInt, true);
988 break;
989
990 case Instruction::XOR_INT_2ADDR:
991 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kInt, true);
992 break;
993
994 case Instruction::SHL_INT_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +0800995 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kInt, true);
Logan Chien70f94b42011-12-27 17:49:11 +0800996 break;
997
998 case Instruction::SHR_INT_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +0800999 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kInt, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001000 break;
1001
1002 case Instruction::USHR_INT_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001003 EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kInt, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001004 break;
1005
1006 case Instruction::ADD_LONG_2ADDR:
1007 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kLong, true);
1008 break;
1009
1010 case Instruction::SUB_LONG_2ADDR:
1011 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kLong, true);
1012 break;
1013
1014 case Instruction::MUL_LONG_2ADDR:
1015 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kLong, true);
1016 break;
1017
1018 case Instruction::DIV_LONG_2ADDR:
1019 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kLong, true);
1020 break;
1021
1022 case Instruction::REM_LONG_2ADDR:
1023 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kLong, true);
1024 break;
1025
1026 case Instruction::AND_LONG_2ADDR:
1027 EmitInsn_IntArithm(ARGS, kIntArithm_And, kLong, true);
1028 break;
1029
1030 case Instruction::OR_LONG_2ADDR:
1031 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kLong, true);
1032 break;
1033
1034 case Instruction::XOR_LONG_2ADDR:
1035 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kLong, true);
1036 break;
1037
1038 case Instruction::SHL_LONG_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001039 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kLong, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001040 break;
1041
1042 case Instruction::SHR_LONG_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001043 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kLong, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001044 break;
1045
1046 case Instruction::USHR_LONG_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001047 EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kLong, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001048 break;
1049
1050 case Instruction::ADD_FLOAT_2ADDR:
1051 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kFloat, true);
1052 break;
1053
1054 case Instruction::SUB_FLOAT_2ADDR:
1055 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kFloat, true);
1056 break;
1057
1058 case Instruction::MUL_FLOAT_2ADDR:
1059 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kFloat, true);
1060 break;
1061
1062 case Instruction::DIV_FLOAT_2ADDR:
1063 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kFloat, true);
1064 break;
1065
1066 case Instruction::REM_FLOAT_2ADDR:
1067 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kFloat, true);
1068 break;
1069
1070 case Instruction::ADD_DOUBLE_2ADDR:
1071 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kDouble, true);
1072 break;
1073
1074 case Instruction::SUB_DOUBLE_2ADDR:
1075 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kDouble, true);
1076 break;
1077
1078 case Instruction::MUL_DOUBLE_2ADDR:
1079 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kDouble, true);
1080 break;
1081
1082 case Instruction::DIV_DOUBLE_2ADDR:
1083 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kDouble, true);
1084 break;
1085
1086 case Instruction::REM_DOUBLE_2ADDR:
1087 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kDouble, true);
1088 break;
1089
1090 case Instruction::ADD_INT_LIT16:
1091 case Instruction::ADD_INT_LIT8:
1092 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Add);
1093 break;
1094
1095 case Instruction::RSUB_INT:
1096 case Instruction::RSUB_INT_LIT8:
1097 EmitInsn_RSubImmediate(ARGS);
1098 break;
1099
1100 case Instruction::MUL_INT_LIT16:
1101 case Instruction::MUL_INT_LIT8:
1102 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Mul);
1103 break;
1104
1105 case Instruction::DIV_INT_LIT16:
1106 case Instruction::DIV_INT_LIT8:
1107 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Div);
1108 break;
1109
1110 case Instruction::REM_INT_LIT16:
1111 case Instruction::REM_INT_LIT8:
1112 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Rem);
1113 break;
1114
1115 case Instruction::AND_INT_LIT16:
1116 case Instruction::AND_INT_LIT8:
1117 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_And);
1118 break;
1119
1120 case Instruction::OR_INT_LIT16:
1121 case Instruction::OR_INT_LIT8:
1122 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Or);
1123 break;
1124
1125 case Instruction::XOR_INT_LIT16:
1126 case Instruction::XOR_INT_LIT8:
1127 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Xor);
1128 break;
1129
1130 case Instruction::SHL_INT_LIT8:
Logan Chien5539ad02012-04-02 14:36:55 +08001131 EmitInsn_IntShiftArithmImmediate(ARGS, kIntArithm_Shl);
Logan Chien70f94b42011-12-27 17:49:11 +08001132 break;
1133
1134 case Instruction::SHR_INT_LIT8:
Logan Chien5539ad02012-04-02 14:36:55 +08001135 EmitInsn_IntShiftArithmImmediate(ARGS, kIntArithm_Shr);
Logan Chien70f94b42011-12-27 17:49:11 +08001136 break;
1137
1138 case Instruction::USHR_INT_LIT8:
Logan Chien5539ad02012-04-02 14:36:55 +08001139 EmitInsn_IntShiftArithmImmediate(ARGS, kIntArithm_UShr);
Logan Chien70f94b42011-12-27 17:49:11 +08001140 break;
1141
Logan Chien9e5f5c12012-04-10 13:51:45 +08001142 case Instruction::THROW_VERIFICATION_ERROR:
1143 EmitInsn_ThrowVerificationError(ARGS);
1144 break;
1145
Logan Chien70f94b42011-12-27 17:49:11 +08001146 case Instruction::UNUSED_3E:
1147 case Instruction::UNUSED_3F:
1148 case Instruction::UNUSED_40:
1149 case Instruction::UNUSED_41:
1150 case Instruction::UNUSED_42:
1151 case Instruction::UNUSED_43:
1152 case Instruction::UNUSED_73:
1153 case Instruction::UNUSED_79:
1154 case Instruction::UNUSED_7A:
1155 case Instruction::UNUSED_E3:
1156 case Instruction::UNUSED_E4:
1157 case Instruction::UNUSED_E5:
1158 case Instruction::UNUSED_E6:
1159 case Instruction::UNUSED_E7:
1160 case Instruction::UNUSED_E8:
1161 case Instruction::UNUSED_E9:
1162 case Instruction::UNUSED_EA:
1163 case Instruction::UNUSED_EB:
1164 case Instruction::UNUSED_EC:
Logan Chien70f94b42011-12-27 17:49:11 +08001165 case Instruction::UNUSED_EE:
1166 case Instruction::UNUSED_EF:
1167 case Instruction::UNUSED_F0:
1168 case Instruction::UNUSED_F1:
1169 case Instruction::UNUSED_F2:
1170 case Instruction::UNUSED_F3:
1171 case Instruction::UNUSED_F4:
1172 case Instruction::UNUSED_F5:
1173 case Instruction::UNUSED_F6:
1174 case Instruction::UNUSED_F7:
1175 case Instruction::UNUSED_F8:
1176 case Instruction::UNUSED_F9:
1177 case Instruction::UNUSED_FA:
1178 case Instruction::UNUSED_FB:
1179 case Instruction::UNUSED_FC:
1180 case Instruction::UNUSED_FD:
1181 case Instruction::UNUSED_FE:
1182 case Instruction::UNUSED_FF:
1183 LOG(FATAL) << "Dex file contains UNUSED bytecode: " << insn->Opcode();
1184 break;
1185 }
1186
1187#undef ARGS
1188}
1189
1190
1191void MethodCompiler::EmitInsn_Nop(uint32_t dex_pc,
1192 Instruction const* insn) {
Logan Chiene09a6b72011-12-27 17:50:21 +08001193
1194 uint16_t insn_signature = code_item_->insns_[dex_pc];
1195
1196 if (insn_signature == Instruction::kPackedSwitchSignature ||
1197 insn_signature == Instruction::kSparseSwitchSignature ||
1198 insn_signature == Instruction::kArrayDataSignature) {
1199 irb_.CreateUnreachable();
Elliott Hughesb25c3f62012-03-26 16:35:06 -07001200 } else {
Logan Chiene09a6b72011-12-27 17:50:21 +08001201 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1202 }
Shih-wei Liaod1fec812012-02-13 09:51:10 -08001203}
1204
1205
Logan Chien70f94b42011-12-27 17:49:11 +08001206void MethodCompiler::EmitInsn_Move(uint32_t dex_pc,
1207 Instruction const* insn,
1208 JType jty) {
Logan Chien48173132011-12-27 17:51:13 +08001209
Elliott Hughesadb8c672012-03-06 16:49:32 -08001210 DecodedInstruction dec_insn(insn);
Logan Chien48173132011-12-27 17:51:13 +08001211
Elliott Hughesadb8c672012-03-06 16:49:32 -08001212 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, jty, kReg);
1213 EmitStoreDalvikReg(dec_insn.vA, jty, kReg, src_value);
Logan Chien48173132011-12-27 17:51:13 +08001214
Logan Chien70f94b42011-12-27 17:49:11 +08001215 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1216}
1217
1218
1219void MethodCompiler::EmitInsn_MoveResult(uint32_t dex_pc,
1220 Instruction const* insn,
1221 JType jty) {
Logan Chien48173132011-12-27 17:51:13 +08001222
Elliott Hughesadb8c672012-03-06 16:49:32 -08001223 DecodedInstruction dec_insn(insn);
Logan Chien48173132011-12-27 17:51:13 +08001224
1225 llvm::Value* src_value = EmitLoadDalvikRetValReg(jty, kReg);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001226 EmitStoreDalvikReg(dec_insn.vA, jty, kReg, src_value);
Logan Chien48173132011-12-27 17:51:13 +08001227
Logan Chien70f94b42011-12-27 17:49:11 +08001228 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1229}
1230
1231
1232void MethodCompiler::EmitInsn_MoveException(uint32_t dex_pc,
1233 Instruction const* insn) {
Logan Chien3354cec2012-01-13 14:29:03 +08001234
Elliott Hughesadb8c672012-03-06 16:49:32 -08001235 DecodedInstruction dec_insn(insn);
Logan Chien3354cec2012-01-13 14:29:03 +08001236
1237 // Get thread-local exception field address
1238 llvm::Constant* exception_field_offset =
1239 irb_.getPtrEquivInt(Thread::ExceptionOffset().Int32Value());
1240
1241 llvm::Value* thread_object_addr =
1242 irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1243
1244 llvm::Value* exception_field_addr =
1245 irb_.CreatePtrDisp(thread_object_addr, exception_field_offset,
1246 irb_.getJObjectTy()->getPointerTo());
1247
1248 // Get exception object address
1249 llvm::Value* exception_object_addr = irb_.CreateLoad(exception_field_addr);
1250
1251 // Set thread-local exception field address to NULL
1252 irb_.CreateStore(irb_.getJNull(), exception_field_addr);
1253
1254 // Keep the exception object in the Dalvik register
Elliott Hughesadb8c672012-03-06 16:49:32 -08001255 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, exception_object_addr);
Logan Chien3354cec2012-01-13 14:29:03 +08001256
Logan Chien70f94b42011-12-27 17:49:11 +08001257 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1258}
1259
1260
1261void MethodCompiler::EmitInsn_ThrowException(uint32_t dex_pc,
1262 Instruction const* insn) {
Logan Chien6c6f12d2012-01-13 19:26:27 +08001263
Elliott Hughesadb8c672012-03-06 16:49:32 -08001264 DecodedInstruction dec_insn(insn);
Logan Chien6c6f12d2012-01-13 19:26:27 +08001265
1266 llvm::Value* exception_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001267 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chien6c6f12d2012-01-13 19:26:27 +08001268
Logan Chien8dfcbea2012-02-17 18:50:32 +08001269 EmitUpdateLineNumFromDexPC(dex_pc);
1270
Logan Chien6c6f12d2012-01-13 19:26:27 +08001271 irb_.CreateCall(irb_.GetRuntime(ThrowException), exception_addr);
1272
1273 EmitBranchExceptionLandingPad(dex_pc);
Logan Chien70f94b42011-12-27 17:49:11 +08001274}
1275
1276
Logan Chien9e5f5c12012-04-10 13:51:45 +08001277void MethodCompiler::EmitInsn_ThrowVerificationError(uint32_t dex_pc,
1278 Instruction const* insn) {
1279
1280 DecodedInstruction dec_insn(insn);
1281
1282 EmitUpdateLineNumFromDexPC(dex_pc);
1283
1284 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1285 llvm::Value* kind_value = irb_.getInt32(dec_insn.vA);
1286 llvm::Value* ref_value = irb_.getInt32(dec_insn.vB);
1287
1288 irb_.CreateCall3(irb_.GetRuntime(ThrowVerificationError),
1289 method_object_addr, kind_value, ref_value);
1290
1291 EmitBranchExceptionLandingPad(dex_pc);
1292}
1293
1294
Logan Chien70f94b42011-12-27 17:49:11 +08001295void MethodCompiler::EmitInsn_ReturnVoid(uint32_t dex_pc,
1296 Instruction const* insn) {
Logan Chien8898a272011-12-27 17:51:56 +08001297 // Garbage collection safe-point
1298 EmitGuard_GarbageCollectionSuspend(dex_pc);
1299
Logan Chien8dfcbea2012-02-17 18:50:32 +08001300 // Pop the shadow frame
1301 EmitPopShadowFrame();
1302
Logan Chien8898a272011-12-27 17:51:56 +08001303 // Return!
1304 irb_.CreateRetVoid();
Logan Chien70f94b42011-12-27 17:49:11 +08001305}
1306
1307
1308void MethodCompiler::EmitInsn_Return(uint32_t dex_pc,
1309 Instruction const* insn) {
Logan Chien8898a272011-12-27 17:51:56 +08001310
Elliott Hughesadb8c672012-03-06 16:49:32 -08001311 DecodedInstruction dec_insn(insn);
Logan Chien8898a272011-12-27 17:51:56 +08001312
1313 // Garbage collection safe-point
1314 EmitGuard_GarbageCollectionSuspend(dex_pc);
1315
Logan Chien8dfcbea2012-02-17 18:50:32 +08001316 // Pop the shadow frame
1317 EmitPopShadowFrame();
1318 // NOTE: It is important to keep this AFTER the GC safe-point. Otherwise,
1319 // the return value might be collected since the shadow stack is popped.
1320
Logan Chien8898a272011-12-27 17:51:56 +08001321 // Return!
Logan Chiendd361c92012-04-10 23:40:37 +08001322 char ret_shorty = oat_compilation_unit_->GetShorty()[0];
Elliott Hughesadb8c672012-03-06 16:49:32 -08001323 llvm::Value* retval = EmitLoadDalvikReg(dec_insn.vA, ret_shorty, kAccurate);
Logan Chien8898a272011-12-27 17:51:56 +08001324
1325 irb_.CreateRet(retval);
Logan Chien70f94b42011-12-27 17:49:11 +08001326}
1327
1328
1329void MethodCompiler::EmitInsn_LoadConstant(uint32_t dex_pc,
1330 Instruction const* insn,
1331 JType imm_jty) {
Shih-wei Liao798366e2012-02-16 09:25:33 -08001332
Elliott Hughesadb8c672012-03-06 16:49:32 -08001333 DecodedInstruction dec_insn(insn);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001334
1335 DCHECK(imm_jty == kInt || imm_jty == kLong) << imm_jty;
1336
1337 int64_t imm = 0;
1338
1339 switch (insn->Opcode()) {
1340 // 32-bit Immediate
1341 case Instruction::CONST_4:
1342 case Instruction::CONST_16:
1343 case Instruction::CONST:
1344 case Instruction::CONST_WIDE_16:
1345 case Instruction::CONST_WIDE_32:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001346 imm = static_cast<int64_t>(static_cast<int32_t>(dec_insn.vB));
Shih-wei Liao798366e2012-02-16 09:25:33 -08001347 break;
1348
1349 case Instruction::CONST_HIGH16:
1350 imm = static_cast<int64_t>(static_cast<int32_t>(
Elliott Hughesadb8c672012-03-06 16:49:32 -08001351 static_cast<uint32_t>(static_cast<uint16_t>(dec_insn.vB)) << 16));
Shih-wei Liao798366e2012-02-16 09:25:33 -08001352 break;
1353
1354 // 64-bit Immediate
1355 case Instruction::CONST_WIDE:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001356 imm = static_cast<int64_t>(dec_insn.vB_wide);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001357 break;
1358
1359 case Instruction::CONST_WIDE_HIGH16:
1360 imm = static_cast<int64_t>(
Elliott Hughesadb8c672012-03-06 16:49:32 -08001361 static_cast<uint64_t>(static_cast<uint16_t>(dec_insn.vB)) << 48);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001362 break;
1363
1364 // Unknown opcode for load constant (unreachable)
1365 default:
1366 LOG(FATAL) << "Unknown opcode for load constant: " << insn->Opcode();
1367 break;
1368 }
1369
1370 // Store the non-object register
1371 llvm::Type* imm_type = irb_.getJType(imm_jty, kAccurate);
1372 llvm::Constant* imm_value = llvm::ConstantInt::getSigned(imm_type, imm);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001373 EmitStoreDalvikReg(dec_insn.vA, imm_jty, kAccurate, imm_value);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001374
1375 // Store the object register if it is possible to be null.
1376 if (imm_jty == kInt && imm == 0) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001377 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, irb_.getJNull());
Shih-wei Liao798366e2012-02-16 09:25:33 -08001378 }
1379
Logan Chien70f94b42011-12-27 17:49:11 +08001380 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1381}
1382
1383
1384void MethodCompiler::EmitInsn_LoadConstantString(uint32_t dex_pc,
1385 Instruction const* insn) {
Logan Chienc3b4ba12012-01-16 19:52:53 +08001386
Elliott Hughesadb8c672012-03-06 16:49:32 -08001387 DecodedInstruction dec_insn(insn);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001388
Elliott Hughesadb8c672012-03-06 16:49:32 -08001389 uint32_t string_idx = dec_insn.vB;
Logan Chienc3b4ba12012-01-16 19:52:53 +08001390
1391 llvm::Value* string_field_addr = EmitLoadDexCacheStringFieldAddr(string_idx);
1392
1393 llvm::Value* string_addr = irb_.CreateLoad(string_field_addr);
1394
1395 if (!compiler_->CanAssumeStringIsPresentInDexCache(dex_cache_, string_idx)) {
1396 llvm::BasicBlock* block_str_exist =
1397 CreateBasicBlockWithDexPC(dex_pc, "str_exist");
1398
1399 llvm::BasicBlock* block_str_resolve =
1400 CreateBasicBlockWithDexPC(dex_pc, "str_resolve");
1401
1402 // Test: Is the string resolved and in the dex cache?
1403 llvm::Value* equal_null = irb_.CreateICmpEQ(string_addr, irb_.getJNull());
1404
TDYa127a849cb62012-04-01 05:59:34 -07001405 irb_.CreateCondBr(equal_null, block_str_resolve, block_str_exist);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001406
1407 // String is resolved, go to next basic block.
1408 irb_.SetInsertPoint(block_str_exist);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001409 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, string_addr);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001410 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1411
1412 // String is not resolved yet, resolve it now.
1413 irb_.SetInsertPoint(block_str_resolve);
1414
1415 llvm::Function* runtime_func = irb_.GetRuntime(ResolveString);
1416
1417 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1418
1419 llvm::Value* string_idx_value = irb_.getInt32(string_idx);
1420
Logan Chien8dfcbea2012-02-17 18:50:32 +08001421 EmitUpdateLineNumFromDexPC(dex_pc);
1422
1423 string_addr = irb_.CreateCall2(runtime_func, method_object_addr,
1424 string_idx_value);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001425
1426 EmitGuard_ExceptionLandingPad(dex_pc);
1427 }
1428
1429 // Store the string object to the Dalvik register
Elliott Hughesadb8c672012-03-06 16:49:32 -08001430 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, string_addr);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001431
Logan Chien70f94b42011-12-27 17:49:11 +08001432 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1433}
1434
1435
Logan Chien27b30252012-01-14 03:43:35 +08001436llvm::Value* MethodCompiler::EmitLoadConstantClass(uint32_t dex_pc,
1437 uint32_t type_idx) {
1438 if (!compiler_->CanAccessTypeWithoutChecks(method_idx_, dex_cache_,
1439 *dex_file_, type_idx)) {
1440 llvm::Value* type_idx_value = irb_.getInt32(type_idx);
1441
1442 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1443
1444 llvm::Function* runtime_func =
1445 irb_.GetRuntime(InitializeTypeAndVerifyAccess);
1446
Logan Chien8dfcbea2012-02-17 18:50:32 +08001447 EmitUpdateLineNumFromDexPC(dex_pc);
1448
Logan Chien27b30252012-01-14 03:43:35 +08001449 llvm::Value* type_object_addr =
1450 irb_.CreateCall2(runtime_func, type_idx_value, method_object_addr);
1451
1452 EmitGuard_ExceptionLandingPad(dex_pc);
1453
1454 return type_object_addr;
1455
1456 } else {
1457 // Try to load the class (type) object from the test cache.
1458 llvm::Value* type_field_addr =
1459 EmitLoadDexCacheResolvedTypeFieldAddr(type_idx);
1460
1461 llvm::Value* type_object_addr = irb_.CreateLoad(type_field_addr);
1462
1463 if (compiler_->CanAssumeTypeIsPresentInDexCache(dex_cache_, type_idx)) {
1464 return type_object_addr;
1465 }
1466
1467 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
1468
1469 // Test whether class (type) object is in the dex cache or not
1470 llvm::Value* equal_null =
1471 irb_.CreateICmpEQ(type_object_addr, irb_.getJNull());
1472
1473 llvm::BasicBlock* block_cont =
1474 CreateBasicBlockWithDexPC(dex_pc, "cont");
1475
1476 llvm::BasicBlock* block_load_class =
1477 CreateBasicBlockWithDexPC(dex_pc, "load_class");
1478
1479 irb_.CreateCondBr(equal_null, block_load_class, block_cont);
1480
1481 // Failback routine to load the class object
1482 irb_.SetInsertPoint(block_load_class);
1483
1484 llvm::Function* runtime_func = irb_.GetRuntime(InitializeType);
1485
1486 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
1487
1488 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1489
Logan Chien8dfcbea2012-02-17 18:50:32 +08001490 EmitUpdateLineNumFromDexPC(dex_pc);
1491
Logan Chien27b30252012-01-14 03:43:35 +08001492 llvm::Value* loaded_type_object_addr =
1493 irb_.CreateCall2(runtime_func, type_idx_value, method_object_addr);
1494
1495 EmitGuard_ExceptionLandingPad(dex_pc);
1496
1497 llvm::BasicBlock* block_after_load_class = irb_.GetInsertBlock();
1498
1499 irb_.CreateBr(block_cont);
1500
1501 // Now the class object must be loaded
1502 irb_.SetInsertPoint(block_cont);
1503
1504 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1505
1506 phi->addIncoming(type_object_addr, block_original);
1507 phi->addIncoming(loaded_type_object_addr, block_after_load_class);
1508
1509 return phi;
1510 }
1511}
1512
1513
Logan Chien70f94b42011-12-27 17:49:11 +08001514void MethodCompiler::EmitInsn_LoadConstantClass(uint32_t dex_pc,
1515 Instruction const* insn) {
Logan Chien27b30252012-01-14 03:43:35 +08001516
Elliott Hughesadb8c672012-03-06 16:49:32 -08001517 DecodedInstruction dec_insn(insn);
Logan Chien27b30252012-01-14 03:43:35 +08001518
Elliott Hughesadb8c672012-03-06 16:49:32 -08001519 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, dec_insn.vB);
1520 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, type_object_addr);
Logan Chien27b30252012-01-14 03:43:35 +08001521
Logan Chien70f94b42011-12-27 17:49:11 +08001522 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1523}
1524
1525
1526void MethodCompiler::EmitInsn_MonitorEnter(uint32_t dex_pc,
1527 Instruction const* insn) {
Logan Chien9e0dbe42012-01-13 12:11:37 +08001528
Elliott Hughesadb8c672012-03-06 16:49:32 -08001529 DecodedInstruction dec_insn(insn);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001530
1531 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001532 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001533
1534 // TODO: Slow path always. May not need NullPointerException check.
1535 EmitGuard_NullPointerException(dex_pc, object_addr);
1536
Logan Chien8dfcbea2012-02-17 18:50:32 +08001537 EmitUpdateLineNumFromDexPC(dex_pc);
1538
Logan Chien9e0dbe42012-01-13 12:11:37 +08001539 irb_.CreateCall(irb_.GetRuntime(LockObject), object_addr);
1540 EmitGuard_ExceptionLandingPad(dex_pc);
1541
Logan Chien70f94b42011-12-27 17:49:11 +08001542 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1543}
1544
1545
1546void MethodCompiler::EmitInsn_MonitorExit(uint32_t dex_pc,
1547 Instruction const* insn) {
Logan Chien9e0dbe42012-01-13 12:11:37 +08001548
Elliott Hughesadb8c672012-03-06 16:49:32 -08001549 DecodedInstruction dec_insn(insn);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001550
1551 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001552 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001553
1554 EmitGuard_NullPointerException(dex_pc, object_addr);
1555
Logan Chien8dfcbea2012-02-17 18:50:32 +08001556 EmitUpdateLineNumFromDexPC(dex_pc);
1557
Logan Chien9e0dbe42012-01-13 12:11:37 +08001558 irb_.CreateCall(irb_.GetRuntime(UnlockObject), object_addr);
1559 EmitGuard_ExceptionLandingPad(dex_pc);
1560
Logan Chien70f94b42011-12-27 17:49:11 +08001561 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1562}
1563
1564
1565void MethodCompiler::EmitInsn_CheckCast(uint32_t dex_pc,
1566 Instruction const* insn) {
Logan Chienfc880952012-01-15 23:53:10 +08001567
Elliott Hughesadb8c672012-03-06 16:49:32 -08001568 DecodedInstruction dec_insn(insn);
Logan Chienfc880952012-01-15 23:53:10 +08001569
1570 llvm::BasicBlock* block_test_class =
1571 CreateBasicBlockWithDexPC(dex_pc, "test_class");
1572
1573 llvm::BasicBlock* block_test_sub_class =
1574 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
1575
1576 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001577 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chienfc880952012-01-15 23:53:10 +08001578
1579 // Test: Is the reference equal to null? Act as no-op when it is null.
1580 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
1581
1582 irb_.CreateCondBr(equal_null,
1583 GetNextBasicBlock(dex_pc),
1584 block_test_class);
1585
1586 // Test: Is the object instantiated from the given class?
1587 irb_.SetInsertPoint(block_test_class);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001588 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, dec_insn.vB);
Logan Chienfc880952012-01-15 23:53:10 +08001589 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1590
1591 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
1592
1593 llvm::Value* object_type_field_addr =
1594 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
1595
1596 llvm::Value* object_type_object_addr =
1597 irb_.CreateLoad(object_type_field_addr);
1598
1599 llvm::Value* equal_class =
1600 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
1601
1602 irb_.CreateCondBr(equal_class,
1603 GetNextBasicBlock(dex_pc),
1604 block_test_sub_class);
1605
1606 // Test: Is the object instantiated from the subclass of the given class?
1607 irb_.SetInsertPoint(block_test_sub_class);
1608
Logan Chien8dfcbea2012-02-17 18:50:32 +08001609 EmitUpdateLineNumFromDexPC(dex_pc);
1610
Logan Chienfc880952012-01-15 23:53:10 +08001611 irb_.CreateCall2(irb_.GetRuntime(CheckCast),
1612 type_object_addr, object_type_object_addr);
1613
1614 EmitGuard_ExceptionLandingPad(dex_pc);
1615
Logan Chien70f94b42011-12-27 17:49:11 +08001616 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1617}
1618
1619
1620void MethodCompiler::EmitInsn_InstanceOf(uint32_t dex_pc,
1621 Instruction const* insn) {
Logan Chien68725e22012-01-15 22:25:34 +08001622
Elliott Hughesadb8c672012-03-06 16:49:32 -08001623 DecodedInstruction dec_insn(insn);
Logan Chien68725e22012-01-15 22:25:34 +08001624
1625 llvm::Constant* zero = irb_.getJInt(0);
1626 llvm::Constant* one = irb_.getJInt(1);
1627
1628 llvm::BasicBlock* block_nullp = CreateBasicBlockWithDexPC(dex_pc, "nullp");
1629
1630 llvm::BasicBlock* block_test_class =
1631 CreateBasicBlockWithDexPC(dex_pc, "test_class");
1632
1633 llvm::BasicBlock* block_class_equals =
1634 CreateBasicBlockWithDexPC(dex_pc, "class_eq");
1635
1636 llvm::BasicBlock* block_test_sub_class =
1637 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
1638
1639 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001640 EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
Logan Chien68725e22012-01-15 22:25:34 +08001641
1642 // Overview of the following code :
1643 // We check for null, if so, then false, otherwise check for class == . If so
1644 // then true, otherwise do callout slowpath.
1645 //
1646 // Test: Is the reference equal to null? Set 0 when it is null.
1647 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
1648
1649 irb_.CreateCondBr(equal_null, block_nullp, block_test_class);
1650
1651 irb_.SetInsertPoint(block_nullp);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001652 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, zero);
Logan Chien68725e22012-01-15 22:25:34 +08001653 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1654
1655 // Test: Is the object instantiated from the given class?
1656 irb_.SetInsertPoint(block_test_class);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001657 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, dec_insn.vC);
Logan Chien68725e22012-01-15 22:25:34 +08001658 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1659
1660 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
1661
1662 llvm::Value* object_type_field_addr =
1663 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
1664
1665 llvm::Value* object_type_object_addr =
1666 irb_.CreateLoad(object_type_field_addr);
1667
1668 llvm::Value* equal_class =
1669 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
1670
1671 irb_.CreateCondBr(equal_class, block_class_equals, block_test_sub_class);
1672
1673 irb_.SetInsertPoint(block_class_equals);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001674 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, one);
Logan Chien68725e22012-01-15 22:25:34 +08001675 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1676
1677 // Test: Is the object instantiated from the subclass of the given class?
1678 irb_.SetInsertPoint(block_test_sub_class);
1679
1680 llvm::Value* result =
1681 irb_.CreateCall2(irb_.GetRuntime(IsAssignable),
1682 type_object_addr, object_type_object_addr);
1683
Elliott Hughesadb8c672012-03-06 16:49:32 -08001684 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result);
Logan Chien68725e22012-01-15 22:25:34 +08001685
Logan Chien70f94b42011-12-27 17:49:11 +08001686 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1687}
1688
1689
Logan Chien61bb6142012-02-03 15:34:53 +08001690llvm::Value* MethodCompiler::EmitLoadArrayLength(llvm::Value* array) {
1691 // Load array length field address
1692 llvm::Constant* array_len_field_offset =
1693 irb_.getPtrEquivInt(Array::LengthOffset().Int32Value());
1694
1695 llvm::Value* array_len_field_addr =
1696 irb_.CreatePtrDisp(array, array_len_field_offset,
1697 irb_.getJIntTy()->getPointerTo());
1698
1699 // Load array length
1700 return irb_.CreateLoad(array_len_field_addr);
1701}
1702
1703
Logan Chien70f94b42011-12-27 17:49:11 +08001704void MethodCompiler::EmitInsn_ArrayLength(uint32_t dex_pc,
1705 Instruction const* insn) {
Logan Chien61bb6142012-02-03 15:34:53 +08001706
Elliott Hughesadb8c672012-03-06 16:49:32 -08001707 DecodedInstruction dec_insn(insn);
Logan Chien61bb6142012-02-03 15:34:53 +08001708
1709 // Get the array object address
Elliott Hughesadb8c672012-03-06 16:49:32 -08001710 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
Logan Chien61bb6142012-02-03 15:34:53 +08001711 EmitGuard_NullPointerException(dex_pc, array_addr);
1712
1713 // Get the array length and store it to the register
1714 llvm::Value* array_len = EmitLoadArrayLength(array_addr);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001715 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, array_len);
Logan Chien61bb6142012-02-03 15:34:53 +08001716
Logan Chien70f94b42011-12-27 17:49:11 +08001717 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1718}
1719
1720
1721void MethodCompiler::EmitInsn_NewInstance(uint32_t dex_pc,
1722 Instruction const* insn) {
Logan Chien032bdad2012-01-16 09:59:23 +08001723
Elliott Hughesadb8c672012-03-06 16:49:32 -08001724 DecodedInstruction dec_insn(insn);
Logan Chien032bdad2012-01-16 09:59:23 +08001725
1726 llvm::Function* runtime_func;
Logan Chien1a032b12012-04-11 11:43:04 +08001727 if (compiler_->CanAccessInstantiableTypeWithoutChecks(
1728 method_idx_, dex_cache_, *dex_file_, dec_insn.vB)) {
Logan Chien032bdad2012-01-16 09:59:23 +08001729 runtime_func = irb_.GetRuntime(AllocObject);
1730 } else {
1731 runtime_func = irb_.GetRuntime(AllocObjectWithAccessCheck);
1732 }
1733
Elliott Hughesadb8c672012-03-06 16:49:32 -08001734 llvm::Constant* type_index_value = irb_.getInt32(dec_insn.vB);
Logan Chien032bdad2012-01-16 09:59:23 +08001735
1736 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1737
Logan Chien8dfcbea2012-02-17 18:50:32 +08001738 EmitUpdateLineNumFromDexPC(dex_pc);
1739
Logan Chien032bdad2012-01-16 09:59:23 +08001740 llvm::Value* object_addr =
1741 irb_.CreateCall2(runtime_func, type_index_value, method_object_addr);
1742
1743 EmitGuard_ExceptionLandingPad(dex_pc);
1744
Elliott Hughesadb8c672012-03-06 16:49:32 -08001745 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, object_addr);
Logan Chien032bdad2012-01-16 09:59:23 +08001746
Logan Chien70f94b42011-12-27 17:49:11 +08001747 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1748}
1749
1750
Logan Chiena2cc6a32012-01-16 10:38:41 +08001751llvm::Value* MethodCompiler::EmitAllocNewArray(uint32_t dex_pc,
1752 int32_t length,
1753 uint32_t type_idx,
1754 bool is_filled_new_array) {
1755 llvm::Function* runtime_func;
1756
1757 bool skip_access_check =
1758 compiler_->CanAccessTypeWithoutChecks(method_idx_, dex_cache_,
1759 *dex_file_, type_idx);
1760
TDYa127a849cb62012-04-01 05:59:34 -07001761 llvm::Value* array_length_value;
1762
Logan Chiena2cc6a32012-01-16 10:38:41 +08001763 if (is_filled_new_array) {
1764 runtime_func = skip_access_check ?
1765 irb_.GetRuntime(CheckAndAllocArray) :
1766 irb_.GetRuntime(CheckAndAllocArrayWithAccessCheck);
TDYa127a849cb62012-04-01 05:59:34 -07001767 array_length_value = irb_.getInt32(length);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001768 } else {
1769 runtime_func = skip_access_check ?
1770 irb_.GetRuntime(AllocArray) :
1771 irb_.GetRuntime(AllocArrayWithAccessCheck);
TDYa127a849cb62012-04-01 05:59:34 -07001772 array_length_value = EmitLoadDalvikReg(length, kInt, kAccurate);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001773 }
1774
1775 llvm::Constant* type_index_value = irb_.getInt32(type_idx);
1776
1777 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1778
Logan Chien8dfcbea2012-02-17 18:50:32 +08001779 EmitUpdateLineNumFromDexPC(dex_pc);
1780
Logan Chiena2cc6a32012-01-16 10:38:41 +08001781 llvm::Value* object_addr =
1782 irb_.CreateCall3(runtime_func, type_index_value, method_object_addr,
1783 array_length_value);
1784
1785 EmitGuard_ExceptionLandingPad(dex_pc);
1786
1787 return object_addr;
1788}
1789
1790
Logan Chien70f94b42011-12-27 17:49:11 +08001791void MethodCompiler::EmitInsn_NewArray(uint32_t dex_pc,
1792 Instruction const* insn) {
Logan Chiena2cc6a32012-01-16 10:38:41 +08001793
Elliott Hughesadb8c672012-03-06 16:49:32 -08001794 DecodedInstruction dec_insn(insn);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001795
1796 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001797 EmitAllocNewArray(dex_pc, dec_insn.vB, dec_insn.vC, false);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001798
Elliott Hughesadb8c672012-03-06 16:49:32 -08001799 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, object_addr);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001800
Logan Chien70f94b42011-12-27 17:49:11 +08001801 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1802}
1803
1804
1805void MethodCompiler::EmitInsn_FilledNewArray(uint32_t dex_pc,
1806 Instruction const* insn,
1807 bool is_range) {
Logan Chiena85fb2f2012-01-16 12:52:56 +08001808
Elliott Hughesadb8c672012-03-06 16:49:32 -08001809 DecodedInstruction dec_insn(insn);
Logan Chiena85fb2f2012-01-16 12:52:56 +08001810
1811 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001812 EmitAllocNewArray(dex_pc, dec_insn.vA, dec_insn.vB, true);
Logan Chiena85fb2f2012-01-16 12:52:56 +08001813
Elliott Hughesadb8c672012-03-06 16:49:32 -08001814 if (dec_insn.vA > 0) {
Logan Chiendd361c92012-04-10 23:40:37 +08001815 // Resolve the element type
TDYa127183cf262012-04-11 07:53:21 -07001816 Class* klass = dex_cache_->GetResolvedType(dec_insn.vB)->GetComponentType();
Logan Chiendd361c92012-04-10 23:40:37 +08001817 // TODO: Avoid the usage of the dex_cache_. Try to figure out a better
1818 // way to distinguish [I and [L.
Logan Chiena85fb2f2012-01-16 12:52:56 +08001819 CHECK_NE(klass, static_cast<Class*>(NULL));
Logan Chiena85fb2f2012-01-16 12:52:56 +08001820
Logan Chiendd361c92012-04-10 23:40:37 +08001821 uint32_t alignment;
1822 llvm::Constant* elem_size;
1823 llvm::PointerType* field_type;
Logan Chiena85fb2f2012-01-16 12:52:56 +08001824
Logan Chiendd361c92012-04-10 23:40:37 +08001825 // NOTE: Currently filled-new-array only supports 'L', '[', and 'I'
1826 // as the element, thus we are only checking 2 cases: primitive int and
1827 // non-primitive type.
Logan Chiena85fb2f2012-01-16 12:52:56 +08001828 if (klass->IsPrimitiveInt()) {
Logan Chiendd361c92012-04-10 23:40:37 +08001829 alignment = sizeof(int32_t);
1830 elem_size = irb_.getPtrEquivInt(sizeof(int32_t));
Logan Chiena85fb2f2012-01-16 12:52:56 +08001831 field_type = irb_.getJIntTy()->getPointerTo();
1832 } else {
1833 CHECK(!klass->IsPrimitive());
Logan Chiendd361c92012-04-10 23:40:37 +08001834 alignment = irb_.getSizeOfPtrEquivInt();
1835 elem_size = irb_.getSizeOfPtrEquivIntValue();
Logan Chiena85fb2f2012-01-16 12:52:56 +08001836 field_type = irb_.getJObjectTy()->getPointerTo();
1837 }
1838
Logan Chiendd361c92012-04-10 23:40:37 +08001839 llvm::Value* data_field_offset =
1840 irb_.getPtrEquivInt(Array::DataOffset(alignment).Int32Value());
1841
1842 llvm::Value* data_field_addr =
1843 irb_.CreatePtrDisp(object_addr, data_field_offset, field_type);
1844
Logan Chiena85fb2f2012-01-16 12:52:56 +08001845 // TODO: Tune this code. Currently we are generating one instruction for
1846 // one element which may be very space consuming. Maybe changing to use
1847 // memcpy may help; however, since we can't guarantee that the alloca of
1848 // dalvik register are continuous, we can't perform such optimization yet.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001849 for (uint32_t i = 0; i < dec_insn.vA; ++i) {
Logan Chiena85fb2f2012-01-16 12:52:56 +08001850 int reg_index;
1851 if (is_range) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001852 reg_index = dec_insn.vC + i;
Logan Chiena85fb2f2012-01-16 12:52:56 +08001853 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001854 reg_index = dec_insn.arg[i];
Logan Chiena85fb2f2012-01-16 12:52:56 +08001855 }
1856
1857 llvm::Value* reg_value;
1858 if (klass->IsPrimitiveInt()) {
1859 reg_value = EmitLoadDalvikReg(reg_index, kInt, kAccurate);
1860 } else {
1861 reg_value = EmitLoadDalvikReg(reg_index, kObject, kAccurate);
1862 }
1863
1864 irb_.CreateStore(reg_value, data_field_addr);
1865
Logan Chiendd361c92012-04-10 23:40:37 +08001866 data_field_addr =
1867 irb_.CreatePtrDisp(data_field_addr, elem_size, field_type);
Logan Chiena85fb2f2012-01-16 12:52:56 +08001868 }
1869 }
1870
1871 EmitStoreDalvikRetValReg(kObject, kAccurate, object_addr);
1872
Logan Chien70f94b42011-12-27 17:49:11 +08001873 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1874}
1875
1876
1877void MethodCompiler::EmitInsn_FillArrayData(uint32_t dex_pc,
1878 Instruction const* insn) {
Logan Chiene58b6582012-01-16 17:13:13 +08001879
Elliott Hughesadb8c672012-03-06 16:49:32 -08001880 DecodedInstruction dec_insn(insn);
Logan Chiene58b6582012-01-16 17:13:13 +08001881
1882 // Read the payload
1883 struct PACKED Payload {
1884 uint16_t ident_;
1885 uint16_t elem_width_;
1886 uint32_t num_elems_;
1887 uint8_t data_[];
1888 };
1889
1890 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
Elliott Hughesadb8c672012-03-06 16:49:32 -08001891 static_cast<int32_t>(dec_insn.vB);
Logan Chiene58b6582012-01-16 17:13:13 +08001892
1893 Payload const* payload =
1894 reinterpret_cast<Payload const*>(code_item_->insns_ + payload_offset);
1895
1896 uint32_t size_in_bytes = payload->elem_width_ * payload->num_elems_;
1897
1898 // Load and check the array
Elliott Hughesadb8c672012-03-06 16:49:32 -08001899 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiene58b6582012-01-16 17:13:13 +08001900
1901 EmitGuard_NullPointerException(dex_pc, array_addr);
1902
1903 if (payload->num_elems_ > 0) {
1904 // Test: Is array length big enough?
1905 llvm::Constant* last_index = irb_.getJInt(payload->num_elems_ - 1);
1906
1907 EmitGuard_ArrayIndexOutOfBoundsException(dex_pc, array_addr, last_index);
1908
1909 // Get array data field
1910 llvm::Value* data_field_offset_value =
TDYa127b77799d2012-04-06 22:16:31 -07001911 irb_.getPtrEquivInt(Array::DataOffset(payload->elem_width_).Int32Value());
Logan Chiene58b6582012-01-16 17:13:13 +08001912
1913 llvm::Value* data_field_addr =
1914 irb_.CreatePtrDisp(array_addr, data_field_offset_value,
1915 irb_.getInt8Ty()->getPointerTo());
1916
1917 // Emit payload to bitcode constant pool
1918 std::vector<llvm::Constant*> const_pool_data;
1919 for (uint32_t i = 0; i < size_in_bytes; ++i) {
1920 const_pool_data.push_back(irb_.getInt8(payload->data_[i]));
1921 }
1922
1923 llvm::Constant* const_pool_data_array_value = llvm::ConstantArray::get(
1924 llvm::ArrayType::get(irb_.getInt8Ty(), size_in_bytes), const_pool_data);
1925
1926 llvm::Value* const_pool_data_array_addr =
1927 new llvm::GlobalVariable(*module_,
1928 const_pool_data_array_value->getType(),
1929 false, llvm::GlobalVariable::InternalLinkage,
1930 const_pool_data_array_value,
1931 "array_data_payload");
1932
1933 // Find the memcpy intrinsic
1934 llvm::Type* memcpy_arg_types[] = {
1935 llvm::Type::getInt8Ty(*context_)->getPointerTo(),
1936 llvm::Type::getInt8Ty(*context_)->getPointerTo(),
1937 llvm::Type::getInt32Ty(*context_)
1938 };
1939
1940 llvm::Function* memcpy_intrinsic =
1941 llvm::Intrinsic::getDeclaration(module_,
1942 llvm::Intrinsic::memcpy,
1943 memcpy_arg_types);
1944
1945 // Copy now!
1946 llvm::Value *args[] = {
1947 data_field_addr,
1948 irb_.CreateConstGEP2_32(const_pool_data_array_addr, 0, 0),
1949 irb_.getInt32(size_in_bytes),
1950 irb_.getInt32(0), // alignment: no guarantee
1951 irb_.getFalse() // is_volatile: false
1952 };
1953
1954 irb_.CreateCall(memcpy_intrinsic, args);
1955 }
1956
Logan Chien70f94b42011-12-27 17:49:11 +08001957 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1958}
1959
1960
1961void MethodCompiler::EmitInsn_UnconditionalBranch(uint32_t dex_pc,
1962 Instruction const* insn) {
Logan Chiena466c162011-12-27 17:55:46 +08001963
Elliott Hughesadb8c672012-03-06 16:49:32 -08001964 DecodedInstruction dec_insn(insn);
Logan Chiena466c162011-12-27 17:55:46 +08001965
Elliott Hughesadb8c672012-03-06 16:49:32 -08001966 int32_t branch_offset = dec_insn.vA;
Logan Chiena466c162011-12-27 17:55:46 +08001967
1968 if (branch_offset <= 0) {
1969 // Garbage collection safe-point on backward branch
1970 EmitGuard_GarbageCollectionSuspend(dex_pc);
1971 }
1972
1973 irb_.CreateBr(GetBasicBlock(dex_pc + branch_offset));
Logan Chien70f94b42011-12-27 17:49:11 +08001974}
1975
1976
1977void MethodCompiler::EmitInsn_PackedSwitch(uint32_t dex_pc,
1978 Instruction const* insn) {
Logan Chien7a89b6d2011-12-27 17:56:56 +08001979
Elliott Hughesadb8c672012-03-06 16:49:32 -08001980 DecodedInstruction dec_insn(insn);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001981
1982 struct PACKED Payload {
1983 uint16_t ident_;
1984 uint16_t num_cases_;
1985 int32_t first_key_;
1986 int32_t targets_[];
1987 };
1988
1989 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
Elliott Hughesadb8c672012-03-06 16:49:32 -08001990 static_cast<int32_t>(dec_insn.vB);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001991
1992 Payload const* payload =
1993 reinterpret_cast<Payload const*>(code_item_->insns_ + payload_offset);
1994
Elliott Hughesadb8c672012-03-06 16:49:32 -08001995 llvm::Value* value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001996
1997 llvm::SwitchInst* sw =
1998 irb_.CreateSwitch(value, GetNextBasicBlock(dex_pc), payload->num_cases_);
1999
2000 for (uint16_t i = 0; i < payload->num_cases_; ++i) {
2001 sw->addCase(irb_.getInt32(payload->first_key_ + i),
2002 GetBasicBlock(dex_pc + payload->targets_[i]));
2003 }
Logan Chien70f94b42011-12-27 17:49:11 +08002004}
2005
2006
2007void MethodCompiler::EmitInsn_SparseSwitch(uint32_t dex_pc,
2008 Instruction const* insn) {
Logan Chien7a89b6d2011-12-27 17:56:56 +08002009
Elliott Hughesadb8c672012-03-06 16:49:32 -08002010 DecodedInstruction dec_insn(insn);
Logan Chien7a89b6d2011-12-27 17:56:56 +08002011
2012 struct PACKED Payload {
2013 uint16_t ident_;
2014 uint16_t num_cases_;
2015 int32_t keys_and_targets_[];
2016 };
2017
2018 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
Elliott Hughesadb8c672012-03-06 16:49:32 -08002019 static_cast<int32_t>(dec_insn.vB);
Logan Chien7a89b6d2011-12-27 17:56:56 +08002020
2021 Payload const* payload =
2022 reinterpret_cast<Payload const*>(code_item_->insns_ + payload_offset);
2023
2024 int32_t const* keys = payload->keys_and_targets_;
2025 int32_t const* targets = payload->keys_and_targets_ + payload->num_cases_;
2026
Elliott Hughesadb8c672012-03-06 16:49:32 -08002027 llvm::Value* value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chien7a89b6d2011-12-27 17:56:56 +08002028
2029 llvm::SwitchInst* sw =
2030 irb_.CreateSwitch(value, GetNextBasicBlock(dex_pc), payload->num_cases_);
2031
2032 for (size_t i = 0; i < payload->num_cases_; ++i) {
2033 sw->addCase(irb_.getInt32(keys[i]), GetBasicBlock(dex_pc + targets[i]));
2034 }
Logan Chien70f94b42011-12-27 17:49:11 +08002035}
2036
2037
2038void MethodCompiler::EmitInsn_FPCompare(uint32_t dex_pc,
2039 Instruction const* insn,
2040 JType fp_jty,
2041 bool gt_bias) {
Logan Chien2c37e8e2011-12-27 17:58:46 +08002042
Elliott Hughesadb8c672012-03-06 16:49:32 -08002043 DecodedInstruction dec_insn(insn);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002044
2045 DCHECK(fp_jty == kFloat || fp_jty == kDouble) << "JType: " << fp_jty;
2046
Elliott Hughesadb8c672012-03-06 16:49:32 -08002047 llvm::Value* src1_value = EmitLoadDalvikReg(dec_insn.vB, fp_jty, kAccurate);
2048 llvm::Value* src2_value = EmitLoadDalvikReg(dec_insn.vC, fp_jty, kAccurate);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002049
2050 llvm::Value* cmp_eq = irb_.CreateFCmpOEQ(src1_value, src2_value);
2051 llvm::Value* cmp_lt;
2052
2053 if (gt_bias) {
2054 cmp_lt = irb_.CreateFCmpOLT(src1_value, src2_value);
2055 } else {
2056 cmp_lt = irb_.CreateFCmpULT(src1_value, src2_value);
2057 }
2058
2059 llvm::Value* result = EmitCompareResultSelection(cmp_eq, cmp_lt);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002060 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002061
Logan Chien70f94b42011-12-27 17:49:11 +08002062 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2063}
2064
2065
2066void MethodCompiler::EmitInsn_LongCompare(uint32_t dex_pc,
2067 Instruction const* insn) {
Logan Chien2c37e8e2011-12-27 17:58:46 +08002068
Elliott Hughesadb8c672012-03-06 16:49:32 -08002069 DecodedInstruction dec_insn(insn);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002070
Elliott Hughesadb8c672012-03-06 16:49:32 -08002071 llvm::Value* src1_value = EmitLoadDalvikReg(dec_insn.vB, kLong, kAccurate);
2072 llvm::Value* src2_value = EmitLoadDalvikReg(dec_insn.vC, kLong, kAccurate);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002073
2074 llvm::Value* cmp_eq = irb_.CreateICmpEQ(src1_value, src2_value);
2075 llvm::Value* cmp_lt = irb_.CreateICmpSLT(src1_value, src2_value);
2076
2077 llvm::Value* result = EmitCompareResultSelection(cmp_eq, cmp_lt);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002078 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002079
Logan Chien70f94b42011-12-27 17:49:11 +08002080 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2081}
2082
2083
Logan Chien2c37e8e2011-12-27 17:58:46 +08002084llvm::Value* MethodCompiler::EmitCompareResultSelection(llvm::Value* cmp_eq,
2085 llvm::Value* cmp_lt) {
2086
2087 llvm::Constant* zero = irb_.getJInt(0);
2088 llvm::Constant* pos1 = irb_.getJInt(1);
2089 llvm::Constant* neg1 = irb_.getJInt(-1);
2090
2091 llvm::Value* result_lt = irb_.CreateSelect(cmp_lt, neg1, pos1);
2092 llvm::Value* result_eq = irb_.CreateSelect(cmp_eq, zero, result_lt);
2093
2094 return result_eq;
2095}
2096
2097
Logan Chien70f94b42011-12-27 17:49:11 +08002098void MethodCompiler::EmitInsn_BinaryConditionalBranch(uint32_t dex_pc,
2099 Instruction const* insn,
2100 CondBranchKind cond) {
Logan Chiena78e3c82011-12-27 17:59:35 +08002101
Elliott Hughesadb8c672012-03-06 16:49:32 -08002102 DecodedInstruction dec_insn(insn);
Logan Chiena78e3c82011-12-27 17:59:35 +08002103
Elliott Hughesadb8c672012-03-06 16:49:32 -08002104 int8_t src1_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vA);
2105 int8_t src2_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vB);
Logan Chiena78e3c82011-12-27 17:59:35 +08002106
2107 DCHECK_NE(kRegUnknown, src1_reg_cat);
2108 DCHECK_NE(kRegUnknown, src2_reg_cat);
2109 DCHECK_NE(kRegCat2, src1_reg_cat);
2110 DCHECK_NE(kRegCat2, src2_reg_cat);
2111
Elliott Hughesadb8c672012-03-06 16:49:32 -08002112 int32_t branch_offset = dec_insn.vC;
Logan Chiena78e3c82011-12-27 17:59:35 +08002113
2114 if (branch_offset <= 0) {
2115 // Garbage collection safe-point on backward branch
2116 EmitGuard_GarbageCollectionSuspend(dex_pc);
2117 }
2118
2119 if (src1_reg_cat == kRegZero && src2_reg_cat == kRegZero) {
2120 irb_.CreateBr(GetBasicBlock(dex_pc + branch_offset));
2121 return;
2122 }
2123
2124 llvm::Value* src1_value;
2125 llvm::Value* src2_value;
2126
2127 if (src1_reg_cat != kRegZero && src2_reg_cat != kRegZero) {
2128 CHECK_EQ(src1_reg_cat, src2_reg_cat);
2129
2130 if (src1_reg_cat == kRegCat1nr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002131 src1_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
2132 src2_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002133 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002134 src1_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
2135 src2_value = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002136 }
2137 } else {
2138 DCHECK(src1_reg_cat == kRegZero ||
2139 src2_reg_cat == kRegZero);
2140
2141 if (src1_reg_cat == kRegZero) {
2142 if (src2_reg_cat == kRegCat1nr) {
2143 src1_value = irb_.getJInt(0);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002144 src2_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002145 } else {
2146 src1_value = irb_.getJNull();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002147 src2_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002148 }
2149 } else { // src2_reg_cat == kRegZero
2150 if (src2_reg_cat == kRegCat1nr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002151 src1_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002152 src2_value = irb_.getJInt(0);
2153 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002154 src1_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002155 src2_value = irb_.getJNull();
2156 }
2157 }
2158 }
2159
2160 llvm::Value* cond_value =
2161 EmitConditionResult(src1_value, src2_value, cond);
2162
2163 irb_.CreateCondBr(cond_value,
2164 GetBasicBlock(dex_pc + branch_offset),
2165 GetNextBasicBlock(dex_pc));
Logan Chien70f94b42011-12-27 17:49:11 +08002166}
2167
2168
2169void MethodCompiler::EmitInsn_UnaryConditionalBranch(uint32_t dex_pc,
2170 Instruction const* insn,
2171 CondBranchKind cond) {
Logan Chiena78e3c82011-12-27 17:59:35 +08002172
Elliott Hughesadb8c672012-03-06 16:49:32 -08002173 DecodedInstruction dec_insn(insn);
Logan Chiena78e3c82011-12-27 17:59:35 +08002174
Elliott Hughesadb8c672012-03-06 16:49:32 -08002175 int8_t src_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vA);
Logan Chiena78e3c82011-12-27 17:59:35 +08002176
2177 DCHECK_NE(kRegUnknown, src_reg_cat);
2178 DCHECK_NE(kRegCat2, src_reg_cat);
2179
Elliott Hughesadb8c672012-03-06 16:49:32 -08002180 int32_t branch_offset = dec_insn.vB;
Logan Chiena78e3c82011-12-27 17:59:35 +08002181
2182 if (branch_offset <= 0) {
2183 // Garbage collection safe-point on backward branch
2184 EmitGuard_GarbageCollectionSuspend(dex_pc);
2185 }
2186
2187 if (src_reg_cat == kRegZero) {
2188 irb_.CreateBr(GetBasicBlock(dex_pc + branch_offset));
2189 return;
2190 }
2191
2192 llvm::Value* src1_value;
2193 llvm::Value* src2_value;
2194
2195 if (src_reg_cat == kRegCat1nr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002196 src1_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002197 src2_value = irb_.getInt32(0);
2198 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002199 src1_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002200 src2_value = irb_.getJNull();
2201 }
2202
2203 llvm::Value* cond_value =
2204 EmitConditionResult(src1_value, src2_value, cond);
2205
2206 irb_.CreateCondBr(cond_value,
2207 GetBasicBlock(dex_pc + branch_offset),
2208 GetNextBasicBlock(dex_pc));
2209}
2210
2211
2212RegCategory MethodCompiler::GetInferredRegCategory(uint32_t dex_pc,
2213 uint16_t reg_idx) {
Logan Chiendd361c92012-04-10 23:40:37 +08002214
2215 Compiler::MethodReference mref(dex_file_, method_idx_);
2216
2217 InferredRegCategoryMap const* map =
2218 verifier::DexVerifier::GetInferredRegCategoryMap(mref);
2219
Logan Chiena78e3c82011-12-27 17:59:35 +08002220 CHECK_NE(map, static_cast<InferredRegCategoryMap*>(NULL));
2221
2222 return map->GetRegCategory(dex_pc, reg_idx);
2223}
2224
2225
2226llvm::Value* MethodCompiler::EmitConditionResult(llvm::Value* lhs,
2227 llvm::Value* rhs,
2228 CondBranchKind cond) {
2229 switch (cond) {
2230 case kCondBranch_EQ:
2231 return irb_.CreateICmpEQ(lhs, rhs);
2232
2233 case kCondBranch_NE:
2234 return irb_.CreateICmpNE(lhs, rhs);
2235
2236 case kCondBranch_LT:
2237 return irb_.CreateICmpSLT(lhs, rhs);
2238
2239 case kCondBranch_GE:
2240 return irb_.CreateICmpSGE(lhs, rhs);
2241
2242 case kCondBranch_GT:
2243 return irb_.CreateICmpSGT(lhs, rhs);
2244
2245 case kCondBranch_LE:
2246 return irb_.CreateICmpSLE(lhs, rhs);
2247
2248 default: // Unreachable
2249 LOG(FATAL) << "Unknown conditional branch kind: " << cond;
2250 return NULL;
2251 }
Logan Chien70f94b42011-12-27 17:49:11 +08002252}
2253
2254
Logan Chiene27fdbb2012-01-02 23:27:26 +08002255void
2256MethodCompiler::EmitGuard_ArrayIndexOutOfBoundsException(uint32_t dex_pc,
2257 llvm::Value* array,
2258 llvm::Value* index) {
2259 llvm::Value* array_len = EmitLoadArrayLength(array);
2260
2261 llvm::Value* cmp = irb_.CreateICmpUGE(index, array_len);
2262
2263 llvm::BasicBlock* block_exception =
2264 CreateBasicBlockWithDexPC(dex_pc, "overflow");
2265
2266 llvm::BasicBlock* block_continue =
2267 CreateBasicBlockWithDexPC(dex_pc, "cont");
2268
2269 irb_.CreateCondBr(cmp, block_exception, block_continue);
2270
2271 irb_.SetInsertPoint(block_exception);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002272
2273 EmitUpdateLineNumFromDexPC(dex_pc);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002274 irb_.CreateCall2(irb_.GetRuntime(ThrowIndexOutOfBounds), index, array_len);
2275 EmitBranchExceptionLandingPad(dex_pc);
2276
2277 irb_.SetInsertPoint(block_continue);
2278}
2279
2280
2281void MethodCompiler::EmitGuard_ArrayException(uint32_t dex_pc,
2282 llvm::Value* array,
2283 llvm::Value* index) {
2284 EmitGuard_NullPointerException(dex_pc, array);
2285 EmitGuard_ArrayIndexOutOfBoundsException(dex_pc, array, index);
2286}
2287
2288
2289// Emit Array GetElementPtr
2290llvm::Value* MethodCompiler::EmitArrayGEP(llvm::Value* array_addr,
2291 llvm::Value* index_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08002292 llvm::Type* elem_type,
2293 JType elem_jty) {
2294
2295 int data_offset;
2296 if (elem_jty == kLong || elem_jty == kDouble ||
2297 (elem_jty == kObject && sizeof(uint64_t) == sizeof(Object*))) {
2298 data_offset = Array::DataOffset(sizeof(int64_t)).Int32Value();
2299 } else {
2300 data_offset = Array::DataOffset(sizeof(int32_t)).Int32Value();
2301 }
Logan Chiene27fdbb2012-01-02 23:27:26 +08002302
2303 llvm::Constant* data_offset_value =
Ian Rogers04ec04e2012-02-28 16:15:33 -08002304 irb_.getPtrEquivInt(data_offset);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002305
2306 llvm::Value* array_data_addr =
2307 irb_.CreatePtrDisp(array_addr, data_offset_value,
2308 elem_type->getPointerTo());
2309
2310 return irb_.CreateGEP(array_data_addr, index_value);
2311}
2312
2313
Logan Chien70f94b42011-12-27 17:49:11 +08002314void MethodCompiler::EmitInsn_AGet(uint32_t dex_pc,
2315 Instruction const* insn,
2316 JType elem_jty) {
Logan Chiene27fdbb2012-01-02 23:27:26 +08002317
Elliott Hughesadb8c672012-03-06 16:49:32 -08002318 DecodedInstruction dec_insn(insn);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002319
Elliott Hughesadb8c672012-03-06 16:49:32 -08002320 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
2321 llvm::Value* index_value = EmitLoadDalvikReg(dec_insn.vC, kInt, kAccurate);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002322
2323 EmitGuard_ArrayException(dex_pc, array_addr, index_value);
2324
2325 llvm::Type* elem_type = irb_.getJType(elem_jty, kArray);
2326
2327 llvm::Value* array_elem_addr =
Ian Rogers04ec04e2012-02-28 16:15:33 -08002328 EmitArrayGEP(array_addr, index_value, elem_type, elem_jty);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002329
2330 llvm::Value* array_elem_value = irb_.CreateLoad(array_elem_addr);
2331
Elliott Hughesadb8c672012-03-06 16:49:32 -08002332 EmitStoreDalvikReg(dec_insn.vA, elem_jty, kArray, array_elem_value);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002333
Logan Chien70f94b42011-12-27 17:49:11 +08002334 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2335}
2336
2337
2338void MethodCompiler::EmitInsn_APut(uint32_t dex_pc,
2339 Instruction const* insn,
2340 JType elem_jty) {
Logan Chien8dabb432012-01-02 23:29:32 +08002341
Elliott Hughesadb8c672012-03-06 16:49:32 -08002342 DecodedInstruction dec_insn(insn);
Logan Chien8dabb432012-01-02 23:29:32 +08002343
Elliott Hughesadb8c672012-03-06 16:49:32 -08002344 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
2345 llvm::Value* index_value = EmitLoadDalvikReg(dec_insn.vC, kInt, kAccurate);
Logan Chien8dabb432012-01-02 23:29:32 +08002346
2347 EmitGuard_ArrayException(dex_pc, array_addr, index_value);
2348
2349 llvm::Type* elem_type = irb_.getJType(elem_jty, kArray);
2350
2351 llvm::Value* array_elem_addr =
Ian Rogers04ec04e2012-02-28 16:15:33 -08002352 EmitArrayGEP(array_addr, index_value, elem_type, elem_jty);
Logan Chien8dabb432012-01-02 23:29:32 +08002353
Elliott Hughesadb8c672012-03-06 16:49:32 -08002354 llvm::Value* new_value = EmitLoadDalvikReg(dec_insn.vA, elem_jty, kArray);
Logan Chien8dabb432012-01-02 23:29:32 +08002355
TDYa1271b86d072012-04-05 17:38:56 -07002356 if (elem_jty == kObject) { // If put an object, check the type.
2357 llvm::Function* runtime_func = irb_.GetRuntime(CheckPutArrayElement);
2358
2359 irb_.CreateCall2(runtime_func, new_value, array_addr);
2360
2361 EmitGuard_ExceptionLandingPad(dex_pc);
2362 }
2363
Logan Chien8dabb432012-01-02 23:29:32 +08002364 irb_.CreateStore(new_value, array_elem_addr);
2365
Logan Chien70f94b42011-12-27 17:49:11 +08002366 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2367}
2368
2369
2370void MethodCompiler::EmitInsn_IGet(uint32_t dex_pc,
2371 Instruction const* insn,
2372 JType field_jty) {
Logan Chien48f1d2a2012-01-02 22:49:53 +08002373
Elliott Hughesadb8c672012-03-06 16:49:32 -08002374 DecodedInstruction dec_insn(insn);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002375
Elliott Hughesadb8c672012-03-06 16:49:32 -08002376 uint32_t reg_idx = dec_insn.vB;
2377 uint32_t field_idx = dec_insn.vC;
Logan Chien48f1d2a2012-01-02 22:49:53 +08002378
Logan Chien48f1d2a2012-01-02 22:49:53 +08002379 llvm::Value* object_addr = EmitLoadDalvikReg(reg_idx, kObject, kAccurate);
2380
2381 EmitGuard_NullPointerException(dex_pc, object_addr);
2382
2383 llvm::Value* field_value;
2384
Logan Chien933abf82012-04-11 12:24:31 +08002385 int field_offset;
2386 bool is_volatile;
2387 bool is_fast_path = compiler_->ComputeInstanceFieldInfo(
2388 field_idx, oat_compilation_unit_, field_offset, is_volatile, false);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002389
Logan Chien933abf82012-04-11 12:24:31 +08002390 if (!is_fast_path) {
Logan Chien48f1d2a2012-01-02 22:49:53 +08002391 llvm::Function* runtime_func;
2392
2393 if (field_jty == kObject) {
Logan Chien3b2b2e72012-03-06 16:11:45 +08002394 runtime_func = irb_.GetRuntime(GetObjectInstance);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002395 } else if (field_jty == kLong || field_jty == kDouble) {
Logan Chien3b2b2e72012-03-06 16:11:45 +08002396 runtime_func = irb_.GetRuntime(Get64Instance);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002397 } else {
Logan Chien3b2b2e72012-03-06 16:11:45 +08002398 runtime_func = irb_.GetRuntime(Get32Instance);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002399 }
2400
2401 llvm::ConstantInt* field_idx_value = irb_.getInt32(field_idx);
2402
2403 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2404
Logan Chien8dfcbea2012-02-17 18:50:32 +08002405 EmitUpdateLineNumFromDexPC(dex_pc);
2406
Logan Chien3b2b2e72012-03-06 16:11:45 +08002407 field_value = irb_.CreateCall3(runtime_func, field_idx_value,
2408 method_object_addr, object_addr);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002409
2410 EmitGuard_ExceptionLandingPad(dex_pc);
2411
2412 } else {
Logan Chien933abf82012-04-11 12:24:31 +08002413 DCHECK_GE(field_offset, 0);
2414
Logan Chien48f1d2a2012-01-02 22:49:53 +08002415 llvm::PointerType* field_type =
2416 irb_.getJType(field_jty, kField)->getPointerTo();
2417
Logan Chien933abf82012-04-11 12:24:31 +08002418 llvm::ConstantInt* field_offset_value = irb_.getPtrEquivInt(field_offset);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002419
2420 llvm::Value* field_addr =
Logan Chien933abf82012-04-11 12:24:31 +08002421 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002422
Logan Chien933abf82012-04-11 12:24:31 +08002423 // TODO: Check is_volatile. We need to generate atomic load instruction
2424 // when is_volatile is true.
Logan Chien48f1d2a2012-01-02 22:49:53 +08002425 field_value = irb_.CreateLoad(field_addr);
2426 }
2427
Elliott Hughesadb8c672012-03-06 16:49:32 -08002428 EmitStoreDalvikReg(dec_insn.vA, field_jty, kField, field_value);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002429
Logan Chien70f94b42011-12-27 17:49:11 +08002430 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2431}
2432
2433
2434void MethodCompiler::EmitInsn_IPut(uint32_t dex_pc,
2435 Instruction const* insn,
2436 JType field_jty) {
Logan Chiendd6aa872012-01-03 16:06:32 +08002437
Elliott Hughesadb8c672012-03-06 16:49:32 -08002438 DecodedInstruction dec_insn(insn);
Logan Chiendd6aa872012-01-03 16:06:32 +08002439
Elliott Hughesadb8c672012-03-06 16:49:32 -08002440 uint32_t reg_idx = dec_insn.vB;
2441 uint32_t field_idx = dec_insn.vC;
Logan Chiendd6aa872012-01-03 16:06:32 +08002442
Logan Chiendd6aa872012-01-03 16:06:32 +08002443 llvm::Value* object_addr = EmitLoadDalvikReg(reg_idx, kObject, kAccurate);
2444
2445 EmitGuard_NullPointerException(dex_pc, object_addr);
2446
Elliott Hughesadb8c672012-03-06 16:49:32 -08002447 llvm::Value* new_value = EmitLoadDalvikReg(dec_insn.vA, field_jty, kField);
Logan Chiendd6aa872012-01-03 16:06:32 +08002448
Logan Chien933abf82012-04-11 12:24:31 +08002449 int field_offset;
2450 bool is_volatile;
2451 bool is_fast_path = compiler_->ComputeInstanceFieldInfo(
2452 field_idx, oat_compilation_unit_, field_offset, is_volatile, true);
Logan Chiendd6aa872012-01-03 16:06:32 +08002453
Logan Chien933abf82012-04-11 12:24:31 +08002454 if (!is_fast_path) {
Logan Chiendd6aa872012-01-03 16:06:32 +08002455 llvm::Function* runtime_func;
2456
2457 if (field_jty == kObject) {
2458 runtime_func = irb_.GetRuntime(SetObjectInstance);
2459 } else if (field_jty == kLong || field_jty == kDouble) {
2460 runtime_func = irb_.GetRuntime(Set64Instance);
2461 } else {
2462 runtime_func = irb_.GetRuntime(Set32Instance);
2463 }
2464
2465 llvm::Value* field_idx_value = irb_.getInt32(field_idx);
2466
2467 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2468
Logan Chien8dfcbea2012-02-17 18:50:32 +08002469 EmitUpdateLineNumFromDexPC(dex_pc);
2470
Logan Chien3b2b2e72012-03-06 16:11:45 +08002471 irb_.CreateCall4(runtime_func, field_idx_value,
2472 method_object_addr, object_addr, new_value);
Logan Chiendd6aa872012-01-03 16:06:32 +08002473
2474 EmitGuard_ExceptionLandingPad(dex_pc);
2475
2476 } else {
Logan Chien933abf82012-04-11 12:24:31 +08002477 DCHECK_GE(field_offset, 0);
2478
Logan Chiendd6aa872012-01-03 16:06:32 +08002479 llvm::PointerType* field_type =
2480 irb_.getJType(field_jty, kField)->getPointerTo();
2481
Logan Chien933abf82012-04-11 12:24:31 +08002482 llvm::Value* field_offset_value = irb_.getPtrEquivInt(field_offset);
Logan Chiendd6aa872012-01-03 16:06:32 +08002483
2484 llvm::Value* field_addr =
Logan Chien933abf82012-04-11 12:24:31 +08002485 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
Logan Chiendd6aa872012-01-03 16:06:32 +08002486
Logan Chien933abf82012-04-11 12:24:31 +08002487 // TODO: Check is_volatile. We need to generate atomic store instruction
2488 // when is_volatile is true.
Logan Chiendd6aa872012-01-03 16:06:32 +08002489 irb_.CreateStore(new_value, field_addr);
2490 }
2491
Logan Chien70f94b42011-12-27 17:49:11 +08002492 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2493}
2494
2495
Logan Chien438c4b62012-01-17 16:06:00 +08002496llvm::Value* MethodCompiler::EmitLoadStaticStorage(uint32_t dex_pc,
2497 uint32_t type_idx) {
2498 llvm::BasicBlock* block_load_static =
2499 CreateBasicBlockWithDexPC(dex_pc, "load_static");
2500
2501 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
2502
2503 // Load static storage from dex cache
2504 llvm::Value* storage_field_addr =
2505 EmitLoadDexCacheStaticStorageFieldAddr(type_idx);
2506
2507 llvm::Value* storage_object_addr = irb_.CreateLoad(storage_field_addr);
2508
2509 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
2510
2511 // Test: Is the static storage of this class initialized?
2512 llvm::Value* equal_null =
2513 irb_.CreateICmpEQ(storage_object_addr, irb_.getJNull());
2514
2515 irb_.CreateCondBr(equal_null, block_load_static, block_cont);
2516
2517 // Failback routine to load the class object
2518 irb_.SetInsertPoint(block_load_static);
2519
2520 llvm::Function* runtime_func =
2521 irb_.GetRuntime(InitializeStaticStorage);
2522
2523 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
2524
2525 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2526
Logan Chien8dfcbea2012-02-17 18:50:32 +08002527 EmitUpdateLineNumFromDexPC(dex_pc);
2528
Logan Chien438c4b62012-01-17 16:06:00 +08002529 llvm::Value* loaded_storage_object_addr =
2530 irb_.CreateCall2(runtime_func, type_idx_value, method_object_addr);
2531
2532 EmitGuard_ExceptionLandingPad(dex_pc);
2533
2534 llvm::BasicBlock* block_after_load_static = irb_.GetInsertBlock();
2535
2536 irb_.CreateBr(block_cont);
2537
2538 // Now the class object must be loaded
2539 irb_.SetInsertPoint(block_cont);
2540
2541 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
2542
2543 phi->addIncoming(storage_object_addr, block_original);
2544 phi->addIncoming(loaded_storage_object_addr, block_after_load_static);
2545
2546 return phi;
2547}
2548
2549
Logan Chien70f94b42011-12-27 17:49:11 +08002550void MethodCompiler::EmitInsn_SGet(uint32_t dex_pc,
2551 Instruction const* insn,
2552 JType field_jty) {
Logan Chien438c4b62012-01-17 16:06:00 +08002553
Elliott Hughesadb8c672012-03-06 16:49:32 -08002554 DecodedInstruction dec_insn(insn);
Logan Chien438c4b62012-01-17 16:06:00 +08002555
Logan Chien933abf82012-04-11 12:24:31 +08002556 uint32_t field_idx = dec_insn.vB;
Logan Chien438c4b62012-01-17 16:06:00 +08002557
Logan Chien933abf82012-04-11 12:24:31 +08002558 int field_offset;
2559 int ssb_index;
2560 bool is_referrers_class;
2561 bool is_volatile;
2562
2563 bool is_fast_path = compiler_->ComputeStaticFieldInfo(
2564 field_idx, oat_compilation_unit_, field_offset, ssb_index,
2565 is_referrers_class, is_volatile, false);
Logan Chien438c4b62012-01-17 16:06:00 +08002566
2567 llvm::Value* static_field_value;
2568
Logan Chien933abf82012-04-11 12:24:31 +08002569 if (!is_fast_path) {
Logan Chien438c4b62012-01-17 16:06:00 +08002570 llvm::Function* runtime_func;
2571
2572 if (field_jty == kObject) {
2573 runtime_func = irb_.GetRuntime(GetObjectStatic);
2574 } else if (field_jty == kLong || field_jty == kDouble) {
2575 runtime_func = irb_.GetRuntime(Get64Static);
2576 } else {
2577 runtime_func = irb_.GetRuntime(Get32Static);
2578 }
2579
Elliott Hughesadb8c672012-03-06 16:49:32 -08002580 llvm::Constant* field_idx_value = irb_.getInt32(dec_insn.vB);
Logan Chien438c4b62012-01-17 16:06:00 +08002581
2582 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2583
Logan Chien8dfcbea2012-02-17 18:50:32 +08002584 EmitUpdateLineNumFromDexPC(dex_pc);
2585
Logan Chien438c4b62012-01-17 16:06:00 +08002586 static_field_value =
2587 irb_.CreateCall2(runtime_func, field_idx_value, method_object_addr);
2588
2589 EmitGuard_ExceptionLandingPad(dex_pc);
2590
2591 } else {
Logan Chien933abf82012-04-11 12:24:31 +08002592 DCHECK_GE(field_offset, 0);
Logan Chien438c4b62012-01-17 16:06:00 +08002593
Logan Chien933abf82012-04-11 12:24:31 +08002594 llvm::Value* static_storage_addr = NULL;
2595
2596 if (is_referrers_class) {
2597 // Fast path, static storage base is this method's class
2598 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2599
2600 llvm::Constant* declaring_class_offset_value =
2601 irb_.getPtrEquivInt(Method::DeclaringClassOffset().Int32Value());
2602
2603 llvm::Value* static_storage_field_addr =
2604 irb_.CreatePtrDisp(method_object_addr, declaring_class_offset_value,
2605 irb_.getJObjectTy()->getPointerTo());
2606
2607 static_storage_addr = irb_.CreateLoad(static_storage_field_addr);
2608 } else {
2609 // Medium path, static storage base in a different class which
2610 // requires checks that the other class is initialized
2611 DCHECK_GE(ssb_index, 0);
2612 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
2613 }
2614
2615 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
Logan Chien438c4b62012-01-17 16:06:00 +08002616
2617 llvm::Value* static_field_addr =
2618 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
2619 irb_.getJType(field_jty, kField)->getPointerTo());
2620
Logan Chien933abf82012-04-11 12:24:31 +08002621 // TODO: Check is_volatile. We need to generate atomic load instruction
2622 // when is_volatile is true.
Logan Chien438c4b62012-01-17 16:06:00 +08002623 static_field_value = irb_.CreateLoad(static_field_addr);
2624 }
2625
Elliott Hughesadb8c672012-03-06 16:49:32 -08002626 EmitStoreDalvikReg(dec_insn.vA, field_jty, kField, static_field_value);
Logan Chien438c4b62012-01-17 16:06:00 +08002627
Logan Chien70f94b42011-12-27 17:49:11 +08002628 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2629}
2630
2631
2632void MethodCompiler::EmitInsn_SPut(uint32_t dex_pc,
2633 Instruction const* insn,
2634 JType field_jty) {
Logan Chien14179c82012-01-17 17:06:34 +08002635
Elliott Hughesadb8c672012-03-06 16:49:32 -08002636 DecodedInstruction dec_insn(insn);
Logan Chien14179c82012-01-17 17:06:34 +08002637
Logan Chien933abf82012-04-11 12:24:31 +08002638 uint32_t field_idx = dec_insn.vB;
Logan Chien14179c82012-01-17 17:06:34 +08002639
Elliott Hughesadb8c672012-03-06 16:49:32 -08002640 llvm::Value* new_value = EmitLoadDalvikReg(dec_insn.vA, field_jty, kField);
Logan Chien14179c82012-01-17 17:06:34 +08002641
Logan Chien933abf82012-04-11 12:24:31 +08002642 int field_offset;
2643 int ssb_index;
2644 bool is_referrers_class;
2645 bool is_volatile;
2646
2647 bool is_fast_path = compiler_->ComputeStaticFieldInfo(
2648 field_idx, oat_compilation_unit_, field_offset, ssb_index,
2649 is_referrers_class, is_volatile, true);
2650
2651 if (!is_fast_path) {
Logan Chien14179c82012-01-17 17:06:34 +08002652 llvm::Function* runtime_func;
2653
2654 if (field_jty == kObject) {
2655 runtime_func = irb_.GetRuntime(SetObjectStatic);
2656 } else if (field_jty == kLong || field_jty == kDouble) {
2657 runtime_func = irb_.GetRuntime(Set64Static);
2658 } else {
2659 runtime_func = irb_.GetRuntime(Set32Static);
2660 }
2661
Elliott Hughesadb8c672012-03-06 16:49:32 -08002662 llvm::Constant* field_idx_value = irb_.getInt32(dec_insn.vB);
Logan Chien14179c82012-01-17 17:06:34 +08002663
2664 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2665
Logan Chien8dfcbea2012-02-17 18:50:32 +08002666 EmitUpdateLineNumFromDexPC(dex_pc);
2667
Logan Chien14179c82012-01-17 17:06:34 +08002668 irb_.CreateCall3(runtime_func, field_idx_value,
2669 method_object_addr, new_value);
2670
2671 EmitGuard_ExceptionLandingPad(dex_pc);
2672
2673 } else {
Logan Chien933abf82012-04-11 12:24:31 +08002674 DCHECK_GE(field_offset, 0);
Logan Chien14179c82012-01-17 17:06:34 +08002675
Logan Chien933abf82012-04-11 12:24:31 +08002676 llvm::Value* static_storage_addr = NULL;
2677
2678 if (is_referrers_class) {
2679 // Fast path, static storage base is this method's class
2680 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2681
2682 llvm::Constant* declaring_class_offset_value =
2683 irb_.getPtrEquivInt(Method::DeclaringClassOffset().Int32Value());
2684
2685 llvm::Value* static_storage_field_addr =
2686 irb_.CreatePtrDisp(method_object_addr, declaring_class_offset_value,
2687 irb_.getJObjectTy()->getPointerTo());
2688
2689 static_storage_addr = irb_.CreateLoad(static_storage_field_addr);
2690 } else {
2691 // Medium path, static storage base in a different class which
2692 // requires checks that the other class is initialized
2693 DCHECK_GE(ssb_index, 0);
2694 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
2695 }
2696
2697 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
Logan Chien14179c82012-01-17 17:06:34 +08002698
2699 llvm::Value* static_field_addr =
2700 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
2701 irb_.getJType(field_jty, kField)->getPointerTo());
2702
Logan Chien933abf82012-04-11 12:24:31 +08002703 // TODO: Check is_volatile. We need to generate atomic store instruction
2704 // when is_volatile is true.
Logan Chien14179c82012-01-17 17:06:34 +08002705 irb_.CreateStore(new_value, static_field_addr);
2706 }
2707
Logan Chien70f94b42011-12-27 17:49:11 +08002708 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2709}
2710
2711
Logan Chien1a121b92012-02-15 22:23:42 +08002712void MethodCompiler::
2713EmitLoadActualParameters(std::vector<llvm::Value*>& args,
2714 uint32_t callee_method_idx,
Elliott Hughesadb8c672012-03-06 16:49:32 -08002715 DecodedInstruction const& dec_insn,
Logan Chien7e7fabc2012-04-10 18:59:11 +08002716 InvokeArgFmt arg_fmt,
Logan Chien1a121b92012-02-15 22:23:42 +08002717 bool is_static) {
2718
2719 // Get method signature
2720 DexFile::MethodId const& method_id =
2721 dex_file_->GetMethodId(callee_method_idx);
2722
Logan Chien8faf8022012-02-24 12:25:29 +08002723 uint32_t shorty_size;
Logan Chien1a121b92012-02-15 22:23:42 +08002724 char const* shorty = dex_file_->GetMethodShorty(method_id, &shorty_size);
Logan Chien8faf8022012-02-24 12:25:29 +08002725 CHECK_GE(shorty_size, 1u);
Logan Chien1a121b92012-02-15 22:23:42 +08002726
2727 // Load argument values according to the shorty (without "this")
2728 uint16_t reg_count = 0;
2729
2730 if (!is_static) {
2731 ++reg_count; // skip the "this" pointer
2732 }
2733
Logan Chien7e7fabc2012-04-10 18:59:11 +08002734 bool is_range = (arg_fmt == kArgRange);
2735
Logan Chien8faf8022012-02-24 12:25:29 +08002736 for (uint32_t i = 1; i < shorty_size; ++i) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002737 uint32_t reg_idx = (is_range) ? (dec_insn.vC + reg_count)
2738 : (dec_insn.arg[reg_count]);
Logan Chien1a121b92012-02-15 22:23:42 +08002739
2740 args.push_back(EmitLoadDalvikReg(reg_idx, shorty[i], kAccurate));
2741
2742 ++reg_count;
2743 if (shorty[i] == 'J' || shorty[i] == 'D') {
2744 // Wide types, such as long and double, are using a pair of registers
2745 // to store the value, so we have to increase arg_reg again.
2746 ++reg_count;
2747 }
2748 }
2749
Elliott Hughesadb8c672012-03-06 16:49:32 -08002750 DCHECK_EQ(reg_count, dec_insn.vA)
Logan Chien1a121b92012-02-15 22:23:42 +08002751 << "Actual argument mismatch for callee: "
2752 << PrettyMethod(callee_method_idx, *dex_file_);
2753}
2754
TDYa12785321912012-04-01 15:24:56 -07002755llvm::Value* MethodCompiler::EmitEnsureResolved(llvm::Value* callee,
2756 llvm::Value* caller,
2757 uint32_t dex_method_idx,
TDYa1270b686e52012-04-09 22:43:35 -07002758 bool is_virtual) {
2759 // TODO: Remove this after we solve the trampoline related problems.
2760 return irb_.CreateCall4(irb_.GetRuntime(EnsureResolved),
2761 callee,
2762 caller,
2763 irb_.getInt32(dex_method_idx),
2764 irb_.getInt1(is_virtual));
TDYa12785321912012-04-01 15:24:56 -07002765}
2766
TDYa1270b686e52012-04-09 22:43:35 -07002767llvm::Value* MethodCompiler::EmitFixStub(llvm::Value* callee_method_object_addr,
2768 uint32_t method_idx,
2769 bool is_static) {
2770 // TODO: Remove this after we solve the link and trampoline related problems.
2771 llvm::Value* code_addr = irb_.CreateCall(irb_.GetRuntime(FixStub), callee_method_object_addr);
2772
2773 llvm::FunctionType* method_type = GetFunctionType(method_idx, is_static);
2774
2775 return irb_.CreatePointerCast(code_addr, method_type->getPointerTo());
TDYa12785321912012-04-01 15:24:56 -07002776}
Logan Chien1a121b92012-02-15 22:23:42 +08002777
Shih-wei Liao399ed3f2012-03-08 01:27:04 -08002778
Logan Chien7e7fabc2012-04-10 18:59:11 +08002779void MethodCompiler::EmitInsn_Invoke(uint32_t dex_pc,
2780 Instruction const* insn,
2781 InvokeType invoke_type,
2782 InvokeArgFmt arg_fmt) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002783 DecodedInstruction dec_insn(insn);
Logan Chien46fbb412012-02-15 22:29:08 +08002784
Logan Chien61c65dc2012-02-29 03:22:30 +08002785 bool is_static = (invoke_type == kStatic);
Logan Chien7e7fabc2012-04-10 18:59:11 +08002786 uint32_t callee_method_idx = dec_insn.vB;
Logan Chien61c65dc2012-02-29 03:22:30 +08002787
Logan Chien7e7fabc2012-04-10 18:59:11 +08002788 // Compute invoke related information for compiler decision
2789 int vtable_idx = -1;
Logan Chien92ad16d2012-03-18 05:48:55 +08002790 uintptr_t direct_code = 0; // Currently unused
2791 uintptr_t direct_method = 0; // Currently unused
Logan Chien61c65dc2012-02-29 03:22:30 +08002792 bool is_fast_path = compiler_->
Logan Chien7e7fabc2012-04-10 18:59:11 +08002793 ComputeInvokeInfo(callee_method_idx, oat_compilation_unit_,
Logan Chien92ad16d2012-03-18 05:48:55 +08002794 invoke_type, vtable_idx, direct_code, direct_method);
Logan Chien61c65dc2012-02-29 03:22:30 +08002795
Logan Chien7e7fabc2012-04-10 18:59:11 +08002796 // Load *this* actual parameter
Logan Chien1a121b92012-02-15 22:23:42 +08002797 llvm::Value* this_addr = NULL;
2798
2799 if (!is_static) {
2800 // Test: Is *this* parameter equal to null?
Logan Chien7e7fabc2012-04-10 18:59:11 +08002801 this_addr = (arg_fmt == kArgReg) ?
2802 EmitLoadDalvikReg(dec_insn.arg[0], kObject, kAccurate):
2803 EmitLoadDalvikReg(dec_insn.vC + 0, kObject, kAccurate);
2804
Logan Chien1a121b92012-02-15 22:23:42 +08002805 EmitGuard_NullPointerException(dex_pc, this_addr);
2806 }
2807
Logan Chien7e7fabc2012-04-10 18:59:11 +08002808 // Load the method object
TDYa1274e42a592012-04-10 20:13:54 -07002809 llvm::Value* callee_method_object_addr = NULL;
Logan Chien7e7fabc2012-04-10 18:59:11 +08002810
2811 if (!is_fast_path) {
TDYa1274e42a592012-04-10 20:13:54 -07002812 callee_method_object_addr =
Logan Chien7e7fabc2012-04-10 18:59:11 +08002813 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx, invoke_type,
2814 this_addr, dex_pc, is_fast_path);
2815 } else {
2816 switch (invoke_type) {
2817 case kStatic:
2818 case kDirect:
TDYa1274e42a592012-04-10 20:13:54 -07002819 callee_method_object_addr =
Logan Chien7e7fabc2012-04-10 18:59:11 +08002820 EmitLoadSDCalleeMethodObjectAddr(callee_method_idx);
2821 break;
2822
2823 case kVirtual:
2824 DCHECK(vtable_idx != -1);
TDYa1274e42a592012-04-10 20:13:54 -07002825 callee_method_object_addr =
Logan Chien7e7fabc2012-04-10 18:59:11 +08002826 EmitLoadVirtualCalleeMethodObjectAddr(vtable_idx, this_addr);
2827 break;
2828
2829 case kSuper:
2830 LOG(FATAL) << "invoke-super should be promoted to invoke-direct in "
2831 "the fast path.";
2832 break;
2833
2834 case kInterface:
TDYa1274e42a592012-04-10 20:13:54 -07002835 callee_method_object_addr =
Logan Chien7e7fabc2012-04-10 18:59:11 +08002836 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx,
2837 invoke_type, this_addr,
2838 dex_pc, is_fast_path);
2839 break;
2840 }
TDYa1274e42a592012-04-10 20:13:54 -07002841
2842 // Ensure the callee method object is resolved.
2843 bool is_virtual = (dec_insn.opcode == Instruction::INVOKE_VIRTUAL) ||
2844 (dec_insn.opcode == Instruction::INVOKE_VIRTUAL_RANGE) ||
2845 (dec_insn.opcode == Instruction::INVOKE_SUPER) ||
2846 (dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE);
2847
2848 llvm::Value* caller_method_object_addr = EmitLoadMethodObjectAddr();
2849
2850 callee_method_object_addr = EmitEnsureResolved(callee_method_object_addr,
2851 caller_method_object_addr,
2852 callee_method_idx,
2853 is_virtual);
2854
2855 EmitGuard_ExceptionLandingPad(dex_pc);
Logan Chien7e7fabc2012-04-10 18:59:11 +08002856 }
2857
TDYa1270b686e52012-04-09 22:43:35 -07002858#if 0
Logan Chien7e7fabc2012-04-10 18:59:11 +08002859 llvm::Value* code_field_offset_value =
2860 irb_.getPtrEquivInt(Method::GetCodeOffset().Int32Value());
TDYa12785321912012-04-01 15:24:56 -07002861
Logan Chien7e7fabc2012-04-10 18:59:11 +08002862 llvm::Value* code_field_addr =
2863 irb_.CreatePtrDisp(callee_method_object_addr, code_field_offset_value,
2864 irb_.getInt8Ty()->getPointerTo()->getPointerTo());
2865
2866 llvm::Value* code_addr = irb_.CreateLoad(code_field_addr);
2867#else
2868 // TODO: Remove this after we solve the link and trampoline related problems.
2869 llvm::Value* code_addr =
2870 EmitFixStub(callee_method_object_addr, callee_method_idx, is_static);
2871
2872 EmitGuard_ExceptionLandingPad(dex_pc);
TDYa1270b686e52012-04-09 22:43:35 -07002873#endif
TDYa12785321912012-04-01 15:24:56 -07002874
Logan Chien1a121b92012-02-15 22:23:42 +08002875 // Load the actual parameter
2876 std::vector<llvm::Value*> args;
2877
2878 args.push_back(callee_method_object_addr); // method object for callee
2879
2880 if (!is_static) {
Logan Chien7e7fabc2012-04-10 18:59:11 +08002881 DCHECK(this_addr != NULL);
Logan Chien1a121b92012-02-15 22:23:42 +08002882 args.push_back(this_addr); // "this" object for callee
2883 }
2884
2885 EmitLoadActualParameters(args, callee_method_idx, dec_insn,
Logan Chien7e7fabc2012-04-10 18:59:11 +08002886 arg_fmt, is_static);
Logan Chien1a121b92012-02-15 22:23:42 +08002887
TDYa1275bb86012012-04-11 05:57:28 -07002888#if 0
Logan Chien8dfcbea2012-02-17 18:50:32 +08002889 // Invoke callee
2890 EmitUpdateLineNumFromDexPC(dex_pc);
Logan Chien1a121b92012-02-15 22:23:42 +08002891 llvm::Value* retval = irb_.CreateCall(code_addr, args);
2892 EmitGuard_ExceptionLandingPad(dex_pc);
2893
Logan Chien7e7fabc2012-04-10 18:59:11 +08002894 uint32_t callee_access_flags = is_static ? kAccStatic : 0;
2895 UniquePtr<OatCompilationUnit> callee_oat_compilation_unit(
2896 oat_compilation_unit_->GetCallee(callee_method_idx, callee_access_flags));
2897
2898 char ret_shorty = callee_oat_compilation_unit->GetShorty()[0];
Shih-wei Liao90d50992012-02-19 03:32:05 -08002899 if (ret_shorty != 'V') {
Logan Chien1a121b92012-02-15 22:23:42 +08002900 EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval);
2901 }
TDYa1275bb86012012-04-11 05:57:28 -07002902#else
2903 uint32_t callee_access_flags = is_static ? kAccStatic : 0;
2904 UniquePtr<OatCompilationUnit> callee_oat_compilation_unit(
2905 oat_compilation_unit_->GetCallee(callee_method_idx, callee_access_flags));
2906
2907 char ret_shorty = callee_oat_compilation_unit->GetShorty()[0];
2908
2909
2910 EmitUpdateLineNumFromDexPC(dex_pc);
2911
2912
2913 llvm::BasicBlock* block_normal = CreateBasicBlockWithDexPC(dex_pc, "normal");
2914 llvm::BasicBlock* block_proxy_stub = CreateBasicBlockWithDexPC(dex_pc, "proxy");
2915 llvm::BasicBlock* block_continue = CreateBasicBlockWithDexPC(dex_pc, "cont");
2916
2917 llvm::Type* accurate_ret_type = irb_.getJType(ret_shorty, kAccurate);
2918 llvm::Value* retval_addr = NULL;
2919 if (ret_shorty != 'V') {
2920 retval_addr = irb_.CreateAlloca(accurate_ret_type);
2921 }
2922
2923
2924 // TODO: Remove this after we solve the proxy trampoline calling convention problem.
2925 llvm::Value* code_addr_int = irb_.CreatePtrToInt(code_addr, irb_.getPtrEquivIntTy());
2926 llvm::Value* proxy_stub_int = irb_.getPtrEquivInt(special_stub::kProxyStub);
2927 llvm::Value* is_proxy_stub = irb_.CreateICmpEQ(code_addr_int, proxy_stub_int);
2928 irb_.CreateCondBr(is_proxy_stub, block_proxy_stub, block_normal);
2929
2930
2931 irb_.SetInsertPoint(block_normal);
2932 {
2933 // Invoke callee
2934 llvm::Value* result = irb_.CreateCall(code_addr, args);
2935 if (ret_shorty != 'V') {
2936 irb_.CreateStore(result, retval_addr);
2937 }
2938 }
2939 irb_.CreateBr(block_continue);
2940
2941
2942 irb_.SetInsertPoint(block_proxy_stub);
2943 {
2944 llvm::Value* temp_space_addr;
2945 if (ret_shorty != 'V') {
2946 temp_space_addr = irb_.CreateAlloca(irb_.getJValueTy());
2947 args.push_back(temp_space_addr);
2948 }
2949 irb_.CreateCall(irb_.GetRuntime(ProxyInvokeHandler), args);
2950 if (ret_shorty != 'V') {
2951 llvm::Value* result_addr =
2952 irb_.CreateBitCast(temp_space_addr, accurate_ret_type->getPointerTo());
2953 llvm::Value* retval = irb_.CreateLoad(result_addr);
2954 irb_.CreateStore(retval, retval_addr);
2955 }
2956 }
2957 irb_.CreateBr(block_continue);
2958
2959
2960 irb_.SetInsertPoint(block_continue);
2961
2962 if (ret_shorty != 'V') {
2963 llvm::Value* retval = irb_.CreateLoad(retval_addr);
2964 EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval);
2965 }
2966 EmitGuard_ExceptionLandingPad(dex_pc);
2967#endif
Logan Chien1a121b92012-02-15 22:23:42 +08002968
Logan Chien70f94b42011-12-27 17:49:11 +08002969 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2970}
2971
2972
Logan Chien7e7fabc2012-04-10 18:59:11 +08002973llvm::Value* MethodCompiler::
2974EmitLoadSDCalleeMethodObjectAddr(uint32_t callee_method_idx) {
2975 llvm::Value* callee_method_object_field_addr =
2976 EmitLoadDexCacheResolvedMethodFieldAddr(callee_method_idx);
Logan Chien7caf37e2012-02-03 22:56:04 +08002977
Logan Chien7e7fabc2012-04-10 18:59:11 +08002978 return irb_.CreateLoad(callee_method_object_field_addr);
2979}
Logan Chien7caf37e2012-02-03 22:56:04 +08002980
Logan Chien7caf37e2012-02-03 22:56:04 +08002981
Logan Chien7e7fabc2012-04-10 18:59:11 +08002982llvm::Value* MethodCompiler::
2983EmitLoadVirtualCalleeMethodObjectAddr(int vtable_idx,
2984 llvm::Value* this_addr) {
2985 // Load class object of *this* pointer
2986 llvm::Constant* class_field_offset_value =
2987 irb_.getPtrEquivInt(Object::ClassOffset().Int32Value());
Logan Chien7caf37e2012-02-03 22:56:04 +08002988
Logan Chien7e7fabc2012-04-10 18:59:11 +08002989 llvm::Value* class_field_addr =
2990 irb_.CreatePtrDisp(this_addr, class_field_offset_value,
2991 irb_.getJObjectTy()->getPointerTo());
Logan Chien7caf37e2012-02-03 22:56:04 +08002992
Logan Chien7e7fabc2012-04-10 18:59:11 +08002993 llvm::Value* class_object_addr = irb_.CreateLoad(class_field_addr);
Logan Chien7caf37e2012-02-03 22:56:04 +08002994
Logan Chien7e7fabc2012-04-10 18:59:11 +08002995 // Load vtable address
2996 llvm::Constant* vtable_offset_value =
2997 irb_.getPtrEquivInt(Class::VTableOffset().Int32Value());
2998
2999 llvm::Value* vtable_field_addr =
3000 irb_.CreatePtrDisp(class_object_addr, vtable_offset_value,
3001 irb_.getJObjectTy()->getPointerTo());
3002
3003 llvm::Value* vtable_addr = irb_.CreateLoad(vtable_field_addr);
3004
3005 // Load callee method object
3006 llvm::Value* vtable_idx_value =
3007 irb_.getPtrEquivInt(static_cast<uint64_t>(vtable_idx));
3008
3009 llvm::Value* method_field_addr =
3010 EmitArrayGEP(vtable_addr, vtable_idx_value, irb_.getJObjectTy(), kObject);
3011
3012 return irb_.CreateLoad(method_field_addr);
3013}
3014
3015
3016llvm::Value* MethodCompiler::
3017EmitCallRuntimeForCalleeMethodObjectAddr(uint32_t callee_method_idx,
3018 InvokeType invoke_type,
3019 llvm::Value* this_addr,
3020 uint32_t dex_pc,
3021 bool is_fast_path) {
3022
3023 llvm::Function* runtime_func = NULL;
3024
3025 switch (invoke_type) {
3026 case kStatic:
3027 runtime_func = irb_.GetRuntime(FindStaticMethodWithAccessCheck);
3028 break;
3029
3030 case kDirect:
3031 runtime_func = irb_.GetRuntime(FindDirectMethodWithAccessCheck);
3032 break;
3033
3034 case kVirtual:
3035 runtime_func = irb_.GetRuntime(FindVirtualMethodWithAccessCheck);
3036 break;
3037
3038 case kSuper:
3039 runtime_func = irb_.GetRuntime(FindSuperMethodWithAccessCheck);
3040 break;
3041
3042 case kInterface:
3043 if (is_fast_path) {
3044 runtime_func = irb_.GetRuntime(FindInterfaceMethod);
3045 } else {
3046 runtime_func = irb_.GetRuntime(FindInterfaceMethodWithAccessCheck);
3047 }
3048 break;
3049 }
Logan Chien7caf37e2012-02-03 22:56:04 +08003050
3051 llvm::Value* callee_method_idx_value = irb_.getInt32(callee_method_idx);
3052
Logan Chien7e7fabc2012-04-10 18:59:11 +08003053 if (this_addr == NULL) {
3054 DCHECK_EQ(invoke_type, kStatic);
3055 this_addr = irb_.getJNull();
3056 }
3057
3058 llvm::Value* caller_method_object_addr = EmitLoadMethodObjectAddr();
3059
Logan Chien8dfcbea2012-02-17 18:50:32 +08003060 EmitUpdateLineNumFromDexPC(dex_pc);
3061
TDYa1270b686e52012-04-09 22:43:35 -07003062 llvm::Value* callee_method_object_addr =
Shih-wei Liao399ed3f2012-03-08 01:27:04 -08003063 irb_.CreateCall3(runtime_func,
Logan Chien7e7fabc2012-04-10 18:59:11 +08003064 callee_method_idx_value,
3065 this_addr,
3066 caller_method_object_addr);
Logan Chien7caf37e2012-02-03 22:56:04 +08003067
3068 EmitGuard_ExceptionLandingPad(dex_pc);
3069
Logan Chien7e7fabc2012-04-10 18:59:11 +08003070 return callee_method_object_addr;
Logan Chien70f94b42011-12-27 17:49:11 +08003071}
3072
3073
3074void MethodCompiler::EmitInsn_Neg(uint32_t dex_pc,
3075 Instruction const* insn,
3076 JType op_jty) {
Logan Chien1b5685f2011-12-27 18:01:14 +08003077
Elliott Hughesadb8c672012-03-06 16:49:32 -08003078 DecodedInstruction dec_insn(insn);
Logan Chien1b5685f2011-12-27 18:01:14 +08003079
3080 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3081
Elliott Hughesadb8c672012-03-06 16:49:32 -08003082 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chien1b5685f2011-12-27 18:01:14 +08003083 llvm::Value* result_value = irb_.CreateNeg(src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003084 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chien1b5685f2011-12-27 18:01:14 +08003085
Logan Chien70f94b42011-12-27 17:49:11 +08003086 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3087}
3088
3089
3090void MethodCompiler::EmitInsn_Not(uint32_t dex_pc,
3091 Instruction const* insn,
3092 JType op_jty) {
Logan Chiene53750d2011-12-27 18:02:27 +08003093
Elliott Hughesadb8c672012-03-06 16:49:32 -08003094 DecodedInstruction dec_insn(insn);
Logan Chiene53750d2011-12-27 18:02:27 +08003095
3096 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3097
Elliott Hughesadb8c672012-03-06 16:49:32 -08003098 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chiene53750d2011-12-27 18:02:27 +08003099 llvm::Value* result_value =
3100 irb_.CreateXor(src_value, static_cast<uint64_t>(-1));
3101
Elliott Hughesadb8c672012-03-06 16:49:32 -08003102 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chiene53750d2011-12-27 18:02:27 +08003103
Logan Chien70f94b42011-12-27 17:49:11 +08003104 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3105}
3106
3107
3108void MethodCompiler::EmitInsn_SExt(uint32_t dex_pc,
3109 Instruction const* insn) {
Logan Chien61752ad2011-12-27 18:03:51 +08003110
Elliott Hughesadb8c672012-03-06 16:49:32 -08003111 DecodedInstruction dec_insn(insn);
Logan Chien61752ad2011-12-27 18:03:51 +08003112
Elliott Hughesadb8c672012-03-06 16:49:32 -08003113 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chien61752ad2011-12-27 18:03:51 +08003114 llvm::Value* result_value = irb_.CreateSExt(src_value, irb_.getJLongTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003115 EmitStoreDalvikReg(dec_insn.vA, kLong, kAccurate, result_value);
Logan Chien61752ad2011-12-27 18:03:51 +08003116
Logan Chien70f94b42011-12-27 17:49:11 +08003117 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3118}
3119
3120
3121void MethodCompiler::EmitInsn_Trunc(uint32_t dex_pc,
3122 Instruction const* insn) {
Logan Chien17a57662011-12-27 18:05:14 +08003123
Elliott Hughesadb8c672012-03-06 16:49:32 -08003124 DecodedInstruction dec_insn(insn);
Logan Chien17a57662011-12-27 18:05:14 +08003125
Elliott Hughesadb8c672012-03-06 16:49:32 -08003126 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kLong, kAccurate);
Logan Chien17a57662011-12-27 18:05:14 +08003127 llvm::Value* result_value = irb_.CreateTrunc(src_value, irb_.getJIntTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003128 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chien17a57662011-12-27 18:05:14 +08003129
Logan Chien70f94b42011-12-27 17:49:11 +08003130 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3131}
3132
3133
3134void MethodCompiler::EmitInsn_TruncAndSExt(uint32_t dex_pc,
3135 Instruction const* insn,
3136 unsigned N) {
Logan Chienb6744c52011-12-27 18:06:26 +08003137
Elliott Hughesadb8c672012-03-06 16:49:32 -08003138 DecodedInstruction dec_insn(insn);
Logan Chienb6744c52011-12-27 18:06:26 +08003139
Elliott Hughesadb8c672012-03-06 16:49:32 -08003140 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chienb6744c52011-12-27 18:06:26 +08003141
3142 llvm::Value* trunc_value =
3143 irb_.CreateTrunc(src_value, llvm::Type::getIntNTy(*context_, N));
3144
3145 llvm::Value* result_value = irb_.CreateSExt(trunc_value, irb_.getJIntTy());
3146
Elliott Hughesadb8c672012-03-06 16:49:32 -08003147 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chienb6744c52011-12-27 18:06:26 +08003148
Logan Chien70f94b42011-12-27 17:49:11 +08003149 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3150}
3151
3152
3153void MethodCompiler::EmitInsn_TruncAndZExt(uint32_t dex_pc,
3154 Instruction const* insn,
3155 unsigned N) {
Logan Chienb6744c52011-12-27 18:06:26 +08003156
Elliott Hughesadb8c672012-03-06 16:49:32 -08003157 DecodedInstruction dec_insn(insn);
Logan Chienb6744c52011-12-27 18:06:26 +08003158
Elliott Hughesadb8c672012-03-06 16:49:32 -08003159 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chienb6744c52011-12-27 18:06:26 +08003160
3161 llvm::Value* trunc_value =
3162 irb_.CreateTrunc(src_value, llvm::Type::getIntNTy(*context_, N));
3163
3164 llvm::Value* result_value = irb_.CreateZExt(trunc_value, irb_.getJIntTy());
3165
Elliott Hughesadb8c672012-03-06 16:49:32 -08003166 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chienb6744c52011-12-27 18:06:26 +08003167
Logan Chien70f94b42011-12-27 17:49:11 +08003168 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3169}
3170
3171
3172void MethodCompiler::EmitInsn_FNeg(uint32_t dex_pc,
3173 Instruction const* insn,
3174 JType op_jty) {
Logan Chien7a48b092011-12-27 18:07:45 +08003175
Elliott Hughesadb8c672012-03-06 16:49:32 -08003176 DecodedInstruction dec_insn(insn);
Logan Chien7a48b092011-12-27 18:07:45 +08003177
3178 DCHECK(op_jty == kFloat || op_jty == kDouble) << op_jty;
3179
Elliott Hughesadb8c672012-03-06 16:49:32 -08003180 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chien7a48b092011-12-27 18:07:45 +08003181 llvm::Value* result_value = irb_.CreateFNeg(src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003182 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chien7a48b092011-12-27 18:07:45 +08003183
Logan Chien70f94b42011-12-27 17:49:11 +08003184 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3185}
3186
3187
3188void MethodCompiler::EmitInsn_IntToFP(uint32_t dex_pc,
3189 Instruction const* insn,
3190 JType src_jty,
3191 JType dest_jty) {
Logan Chien62dd4532011-12-27 18:09:00 +08003192
Elliott Hughesadb8c672012-03-06 16:49:32 -08003193 DecodedInstruction dec_insn(insn);
Logan Chien62dd4532011-12-27 18:09:00 +08003194
3195 DCHECK(src_jty == kInt || src_jty == kLong) << src_jty;
3196 DCHECK(dest_jty == kFloat || dest_jty == kDouble) << dest_jty;
3197
Elliott Hughesadb8c672012-03-06 16:49:32 -08003198 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, src_jty, kAccurate);
Logan Chien62dd4532011-12-27 18:09:00 +08003199 llvm::Type* dest_type = irb_.getJType(dest_jty, kAccurate);
3200 llvm::Value* dest_value = irb_.CreateSIToFP(src_value, dest_type);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003201 EmitStoreDalvikReg(dec_insn.vA, dest_jty, kAccurate, dest_value);
Logan Chien62dd4532011-12-27 18:09:00 +08003202
Logan Chien70f94b42011-12-27 17:49:11 +08003203 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3204}
3205
3206
3207void MethodCompiler::EmitInsn_FPToInt(uint32_t dex_pc,
3208 Instruction const* insn,
3209 JType src_jty,
TDYa127a4746872012-04-11 23:48:55 -07003210 JType dest_jty,
3211 runtime_support::RuntimeId runtime_func_id) {
Logan Chien12dc1752011-12-27 18:10:15 +08003212
Elliott Hughesadb8c672012-03-06 16:49:32 -08003213 DecodedInstruction dec_insn(insn);
Logan Chien12dc1752011-12-27 18:10:15 +08003214
3215 DCHECK(src_jty == kFloat || src_jty == kDouble) << src_jty;
3216 DCHECK(dest_jty == kInt || dest_jty == kLong) << dest_jty;
3217
Elliott Hughesadb8c672012-03-06 16:49:32 -08003218 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, src_jty, kAccurate);
TDYa127a4746872012-04-11 23:48:55 -07003219 llvm::Value* dest_value = irb_.CreateCall(irb_.GetRuntime(runtime_func_id), src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003220 EmitStoreDalvikReg(dec_insn.vA, dest_jty, kAccurate, dest_value);
Logan Chien12dc1752011-12-27 18:10:15 +08003221
Logan Chien70f94b42011-12-27 17:49:11 +08003222 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3223}
3224
3225
3226void MethodCompiler::EmitInsn_FExt(uint32_t dex_pc,
3227 Instruction const* insn) {
Logan Chienc56ded92011-12-27 18:10:57 +08003228
Elliott Hughesadb8c672012-03-06 16:49:32 -08003229 DecodedInstruction dec_insn(insn);
Logan Chienc56ded92011-12-27 18:10:57 +08003230
Elliott Hughesadb8c672012-03-06 16:49:32 -08003231 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kFloat, kAccurate);
Logan Chienc56ded92011-12-27 18:10:57 +08003232 llvm::Value* result_value = irb_.CreateFPExt(src_value, irb_.getJDoubleTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003233 EmitStoreDalvikReg(dec_insn.vA, kDouble, kAccurate, result_value);
Logan Chienc56ded92011-12-27 18:10:57 +08003234
Logan Chien70f94b42011-12-27 17:49:11 +08003235 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3236}
3237
3238
3239void MethodCompiler::EmitInsn_FTrunc(uint32_t dex_pc,
3240 Instruction const* insn) {
Logan Chien927744f2011-12-27 18:11:52 +08003241
Elliott Hughesadb8c672012-03-06 16:49:32 -08003242 DecodedInstruction dec_insn(insn);
Logan Chien927744f2011-12-27 18:11:52 +08003243
Elliott Hughesadb8c672012-03-06 16:49:32 -08003244 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kDouble, kAccurate);
Logan Chien927744f2011-12-27 18:11:52 +08003245 llvm::Value* result_value = irb_.CreateFPTrunc(src_value, irb_.getJFloatTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003246 EmitStoreDalvikReg(dec_insn.vA, kFloat, kAccurate, result_value);
Logan Chien927744f2011-12-27 18:11:52 +08003247
Logan Chien70f94b42011-12-27 17:49:11 +08003248 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3249}
3250
3251
3252void MethodCompiler::EmitInsn_IntArithm(uint32_t dex_pc,
3253 Instruction const* insn,
3254 IntArithmKind arithm,
3255 JType op_jty,
3256 bool is_2addr) {
Logan Chienc3f7d962011-12-27 18:13:18 +08003257
Elliott Hughesadb8c672012-03-06 16:49:32 -08003258 DecodedInstruction dec_insn(insn);
Logan Chienc3f7d962011-12-27 18:13:18 +08003259
3260 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3261
3262 llvm::Value* src1_value;
3263 llvm::Value* src2_value;
3264
3265 if (is_2addr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003266 src1_value = EmitLoadDalvikReg(dec_insn.vA, op_jty, kAccurate);
3267 src2_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chienc3f7d962011-12-27 18:13:18 +08003268 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003269 src1_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
3270 src2_value = EmitLoadDalvikReg(dec_insn.vC, op_jty, kAccurate);
Logan Chienc3f7d962011-12-27 18:13:18 +08003271 }
3272
3273 llvm::Value* result_value =
3274 EmitIntArithmResultComputation(dex_pc, src1_value, src2_value,
3275 arithm, op_jty);
3276
Elliott Hughesadb8c672012-03-06 16:49:32 -08003277 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chienc3f7d962011-12-27 18:13:18 +08003278
Logan Chien70f94b42011-12-27 17:49:11 +08003279 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3280}
3281
3282
3283void MethodCompiler::EmitInsn_IntArithmImmediate(uint32_t dex_pc,
3284 Instruction const* insn,
3285 IntArithmKind arithm) {
Logan Chienc3f7d962011-12-27 18:13:18 +08003286
Elliott Hughesadb8c672012-03-06 16:49:32 -08003287 DecodedInstruction dec_insn(insn);
Logan Chienc3f7d962011-12-27 18:13:18 +08003288
Elliott Hughesadb8c672012-03-06 16:49:32 -08003289 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chienc3f7d962011-12-27 18:13:18 +08003290
Elliott Hughesadb8c672012-03-06 16:49:32 -08003291 llvm::Value* imm_value = irb_.getInt32(dec_insn.vC);
Logan Chienc3f7d962011-12-27 18:13:18 +08003292
3293 llvm::Value* result_value =
3294 EmitIntArithmResultComputation(dex_pc, src_value, imm_value, arithm, kInt);
3295
Elliott Hughesadb8c672012-03-06 16:49:32 -08003296 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chienc3f7d962011-12-27 18:13:18 +08003297
Logan Chien70f94b42011-12-27 17:49:11 +08003298 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3299}
3300
3301
Logan Chienc3f7d962011-12-27 18:13:18 +08003302llvm::Value*
3303MethodCompiler::EmitIntArithmResultComputation(uint32_t dex_pc,
3304 llvm::Value* lhs,
3305 llvm::Value* rhs,
3306 IntArithmKind arithm,
3307 JType op_jty) {
3308 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3309
3310 switch (arithm) {
3311 case kIntArithm_Add:
3312 return irb_.CreateAdd(lhs, rhs);
3313
3314 case kIntArithm_Sub:
3315 return irb_.CreateSub(lhs, rhs);
3316
3317 case kIntArithm_Mul:
3318 return irb_.CreateMul(lhs, rhs);
3319
3320 case kIntArithm_Div:
Logan Chienc3f7d962011-12-27 18:13:18 +08003321 case kIntArithm_Rem:
TDYa127f8641ce2012-04-02 06:40:40 -07003322 return EmitIntDivRemResultComputation(dex_pc, lhs, rhs, arithm, op_jty);
Logan Chienc3f7d962011-12-27 18:13:18 +08003323
3324 case kIntArithm_And:
3325 return irb_.CreateAnd(lhs, rhs);
3326
3327 case kIntArithm_Or:
3328 return irb_.CreateOr(lhs, rhs);
3329
3330 case kIntArithm_Xor:
3331 return irb_.CreateXor(lhs, rhs);
3332
Logan Chienc3f7d962011-12-27 18:13:18 +08003333 default:
3334 LOG(FATAL) << "Unknown integer arithmetic kind: " << arithm;
3335 return NULL;
3336 }
3337}
3338
3339
TDYa127f8641ce2012-04-02 06:40:40 -07003340llvm::Value*
3341MethodCompiler::EmitIntDivRemResultComputation(uint32_t dex_pc,
3342 llvm::Value* dividend,
3343 llvm::Value* divisor,
3344 IntArithmKind arithm,
3345 JType op_jty) {
3346 // Throw exception if the divisor is 0.
3347 EmitGuard_DivZeroException(dex_pc, divisor, op_jty);
3348
3349 // Check the special case: MININT / -1 = MININT
3350 // That case will cause overflow, which is undefined behavior in llvm.
3351 // So we check the divisor is -1 or not, if the divisor is -1, we do
3352 // the special path to avoid undefined behavior.
3353 llvm::Type* op_type = irb_.getJType(op_jty, kAccurate);
3354 llvm::Value* zero = irb_.getJZero(op_jty);
3355 llvm::Value* neg_one = llvm::ConstantInt::getSigned(op_type, -1);
3356 llvm::Value* result = irb_.CreateAlloca(op_type);
3357
3358 llvm::BasicBlock* eq_neg_one = CreateBasicBlockWithDexPC(dex_pc, "eq_neg_one");
3359 llvm::BasicBlock* ne_neg_one = CreateBasicBlockWithDexPC(dex_pc, "ne_neg_one");
3360 llvm::BasicBlock* neg_one_cont = CreateBasicBlockWithDexPC(dex_pc, "neg_one_cont");
3361
3362 llvm::Value* is_equal_neg_one = EmitConditionResult(divisor, neg_one, kCondBranch_EQ);
3363 irb_.CreateCondBr(is_equal_neg_one, eq_neg_one, ne_neg_one);
3364
3365 // If divisor == -1
3366 irb_.SetInsertPoint(eq_neg_one);
3367 llvm::Value* eq_result;
3368 if (arithm == kIntArithm_Div) {
3369 // We can just change from "dividend div -1" to "neg dividend".
3370 // The sub don't care the sign/unsigned because of two's complement representation.
3371 // And the behavior is what we want:
3372 // -(2^n) (2^n)-1
3373 // MININT < k <= MAXINT -> mul k -1 = -k
3374 // MININT == k -> mul k -1 = k
3375 //
3376 // LLVM use sub to represent 'neg'
3377 eq_result = irb_.CreateSub(zero, dividend);
3378 } else {
3379 // Everything modulo -1 will be 0.
3380 eq_result = zero;
3381 }
3382 irb_.CreateStore(eq_result, result);
3383 irb_.CreateBr(neg_one_cont);
3384
3385 // If divisor != -1, just do the division.
3386 irb_.SetInsertPoint(ne_neg_one);
3387 llvm::Value* ne_result;
3388 if (arithm == kIntArithm_Div) {
3389 ne_result = irb_.CreateSDiv(dividend, divisor);
3390 } else {
3391 ne_result = irb_.CreateSRem(dividend, divisor);
3392 }
3393 irb_.CreateStore(ne_result, result);
3394 irb_.CreateBr(neg_one_cont);
3395
3396 irb_.SetInsertPoint(neg_one_cont);
3397 return irb_.CreateLoad(result);
3398}
3399
3400
Logan Chien5539ad02012-04-02 14:36:55 +08003401void MethodCompiler::EmitInsn_IntShiftArithm(uint32_t dex_pc,
3402 Instruction const* insn,
3403 IntShiftArithmKind arithm,
3404 JType op_jty,
3405 bool is_2addr) {
3406
3407 DecodedInstruction dec_insn(insn);
3408
3409 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3410
3411 llvm::Value* src1_value;
3412 llvm::Value* src2_value;
3413
3414 // NOTE: The 2nd operand of the shift arithmetic instruction is
3415 // 32-bit integer regardless of the 1st operand.
3416 if (is_2addr) {
3417 src1_value = EmitLoadDalvikReg(dec_insn.vA, op_jty, kAccurate);
3418 src2_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
3419 } else {
3420 src1_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
3421 src2_value = EmitLoadDalvikReg(dec_insn.vC, kInt, kAccurate);
3422 }
3423
3424 llvm::Value* result_value =
3425 EmitIntShiftArithmResultComputation(dex_pc, src1_value, src2_value,
3426 arithm, op_jty);
3427
3428 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
3429
3430 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3431}
3432
3433
3434void MethodCompiler::
3435EmitInsn_IntShiftArithmImmediate(uint32_t dex_pc,
3436 Instruction const* insn,
3437 IntShiftArithmKind arithm) {
3438
3439 DecodedInstruction dec_insn(insn);
3440
3441 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
3442
3443 llvm::Value* imm_value = irb_.getInt32(dec_insn.vC);
3444
3445 llvm::Value* result_value =
3446 EmitIntShiftArithmResultComputation(dex_pc, src_value, imm_value,
3447 arithm, kInt);
3448
3449 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
3450
3451 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3452}
3453
3454
3455llvm::Value*
3456MethodCompiler::EmitIntShiftArithmResultComputation(uint32_t dex_pc,
3457 llvm::Value* lhs,
3458 llvm::Value* rhs,
3459 IntShiftArithmKind arithm,
3460 JType op_jty) {
3461 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3462
3463 if (op_jty == kInt) {
3464 rhs = irb_.CreateAnd(rhs, 0x1f);
3465 } else {
3466 llvm::Value* masked_rhs = irb_.CreateAnd(rhs, 0x3f);
3467 rhs = irb_.CreateZExt(masked_rhs, irb_.getJLongTy());
3468 }
3469
3470 switch (arithm) {
3471 case kIntArithm_Shl:
3472 return irb_.CreateShl(lhs, rhs);
3473
3474 case kIntArithm_Shr:
3475 return irb_.CreateAShr(lhs, rhs);
3476
3477 case kIntArithm_UShr:
3478 return irb_.CreateLShr(lhs, rhs);
3479
3480 default:
3481 LOG(FATAL) << "Unknown integer shift arithmetic kind: " << arithm;
3482 return NULL;
3483 }
3484}
3485
3486
Logan Chien70f94b42011-12-27 17:49:11 +08003487void MethodCompiler::EmitInsn_RSubImmediate(uint32_t dex_pc,
3488 Instruction const* insn) {
Logan Chien65c62d42011-12-27 18:14:18 +08003489
Elliott Hughesadb8c672012-03-06 16:49:32 -08003490 DecodedInstruction dec_insn(insn);
Logan Chien65c62d42011-12-27 18:14:18 +08003491
Elliott Hughesadb8c672012-03-06 16:49:32 -08003492 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
3493 llvm::Value* imm_value = irb_.getInt32(dec_insn.vC);
Logan Chien65c62d42011-12-27 18:14:18 +08003494 llvm::Value* result_value = irb_.CreateSub(imm_value, src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003495 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chien65c62d42011-12-27 18:14:18 +08003496
Logan Chien70f94b42011-12-27 17:49:11 +08003497 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3498}
3499
3500
3501void MethodCompiler::EmitInsn_FPArithm(uint32_t dex_pc,
3502 Instruction const* insn,
3503 FPArithmKind arithm,
3504 JType op_jty,
3505 bool is_2addr) {
Logan Chien76e1c792011-12-27 18:15:01 +08003506
Elliott Hughesadb8c672012-03-06 16:49:32 -08003507 DecodedInstruction dec_insn(insn);
Logan Chien76e1c792011-12-27 18:15:01 +08003508
3509 DCHECK(op_jty == kFloat || op_jty == kDouble) << op_jty;
3510
3511 llvm::Value* src1_value;
3512 llvm::Value* src2_value;
3513
3514 if (is_2addr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003515 src1_value = EmitLoadDalvikReg(dec_insn.vA, op_jty, kAccurate);
3516 src2_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chien76e1c792011-12-27 18:15:01 +08003517 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003518 src1_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
3519 src2_value = EmitLoadDalvikReg(dec_insn.vC, op_jty, kAccurate);
Logan Chien76e1c792011-12-27 18:15:01 +08003520 }
3521
3522 llvm::Value* result_value =
3523 EmitFPArithmResultComputation(dex_pc, src1_value, src2_value, arithm);
3524
Elliott Hughesadb8c672012-03-06 16:49:32 -08003525 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chien76e1c792011-12-27 18:15:01 +08003526
Logan Chien70f94b42011-12-27 17:49:11 +08003527 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3528}
3529
3530
Logan Chien76e1c792011-12-27 18:15:01 +08003531llvm::Value*
3532MethodCompiler::EmitFPArithmResultComputation(uint32_t dex_pc,
3533 llvm::Value *lhs,
3534 llvm::Value *rhs,
3535 FPArithmKind arithm) {
3536 switch (arithm) {
3537 case kFPArithm_Add:
3538 return irb_.CreateFAdd(lhs, rhs);
3539
3540 case kFPArithm_Sub:
3541 return irb_.CreateFSub(lhs, rhs);
3542
3543 case kFPArithm_Mul:
3544 return irb_.CreateFMul(lhs, rhs);
3545
3546 case kFPArithm_Div:
3547 return irb_.CreateFDiv(lhs, rhs);
3548
3549 case kFPArithm_Rem:
3550 return irb_.CreateFRem(lhs, rhs);
3551
3552 default:
3553 LOG(FATAL) << "Unknown floating-point arithmetic kind: " << arithm;
3554 return NULL;
3555 }
3556}
3557
3558
Logan Chienc3f7d962011-12-27 18:13:18 +08003559void MethodCompiler::EmitGuard_DivZeroException(uint32_t dex_pc,
3560 llvm::Value* denominator,
3561 JType op_jty) {
3562 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3563
3564 llvm::Constant* zero = irb_.getJZero(op_jty);
3565
3566 llvm::Value* equal_zero = irb_.CreateICmpEQ(denominator, zero);
3567
3568 llvm::BasicBlock* block_exception = CreateBasicBlockWithDexPC(dex_pc, "div0");
3569
3570 llvm::BasicBlock* block_continue = CreateBasicBlockWithDexPC(dex_pc, "cont");
3571
3572 irb_.CreateCondBr(equal_zero, block_exception, block_continue);
3573
3574 irb_.SetInsertPoint(block_exception);
Logan Chien8dfcbea2012-02-17 18:50:32 +08003575 EmitUpdateLineNumFromDexPC(dex_pc);
Logan Chienc3f7d962011-12-27 18:13:18 +08003576 irb_.CreateCall(irb_.GetRuntime(ThrowDivZeroException));
3577 EmitBranchExceptionLandingPad(dex_pc);
3578
3579 irb_.SetInsertPoint(block_continue);
3580}
3581
3582
Logan Chien61bb6142012-02-03 15:34:53 +08003583void MethodCompiler::EmitGuard_NullPointerException(uint32_t dex_pc,
3584 llvm::Value* object) {
3585 llvm::Value* equal_null = irb_.CreateICmpEQ(object, irb_.getJNull());
3586
3587 llvm::BasicBlock* block_exception =
3588 CreateBasicBlockWithDexPC(dex_pc, "nullp");
3589
3590 llvm::BasicBlock* block_continue =
3591 CreateBasicBlockWithDexPC(dex_pc, "cont");
3592
3593 irb_.CreateCondBr(equal_null, block_exception, block_continue);
3594
3595 irb_.SetInsertPoint(block_exception);
Logan Chien8dfcbea2012-02-17 18:50:32 +08003596 EmitUpdateLineNumFromDexPC(dex_pc);
TDYa1273f9137d2012-04-08 15:59:19 -07003597 irb_.CreateCall(irb_.GetRuntime(ThrowNullPointerException), irb_.getInt32(dex_pc));
Logan Chien61bb6142012-02-03 15:34:53 +08003598 EmitBranchExceptionLandingPad(dex_pc);
3599
3600 irb_.SetInsertPoint(block_continue);
3601}
3602
3603
Logan Chienbb4d12a2012-02-17 14:10:01 +08003604llvm::Value* MethodCompiler::EmitLoadDexCacheAddr(MemberOffset offset) {
3605 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
3606
3607 llvm::Value* dex_cache_offset_value =
3608 irb_.getPtrEquivInt(offset.Int32Value());
3609
3610 llvm::Value* dex_cache_field_addr =
3611 irb_.CreatePtrDisp(method_object_addr, dex_cache_offset_value,
3612 irb_.getJObjectTy()->getPointerTo());
3613
3614 return irb_.CreateLoad(dex_cache_field_addr);
3615}
3616
3617
Logan Chienbb4d12a2012-02-17 14:10:01 +08003618llvm::Value* MethodCompiler::
3619EmitLoadDexCacheStaticStorageFieldAddr(uint32_t type_idx) {
3620 llvm::Value* static_storage_dex_cache_addr =
3621 EmitLoadDexCacheAddr(Method::DexCacheInitializedStaticStorageOffset());
3622
3623 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
3624
3625 return EmitArrayGEP(static_storage_dex_cache_addr, type_idx_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08003626 irb_.getJObjectTy(), kObject);
Logan Chienbb4d12a2012-02-17 14:10:01 +08003627}
3628
3629
3630llvm::Value* MethodCompiler::
3631EmitLoadDexCacheResolvedTypeFieldAddr(uint32_t type_idx) {
3632 llvm::Value* resolved_type_dex_cache_addr =
3633 EmitLoadDexCacheAddr(Method::DexCacheResolvedTypesOffset());
3634
3635 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
3636
3637 return EmitArrayGEP(resolved_type_dex_cache_addr, type_idx_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08003638 irb_.getJObjectTy(), kObject);
Logan Chienbb4d12a2012-02-17 14:10:01 +08003639}
3640
3641
3642llvm::Value* MethodCompiler::
Logan Chien61c65dc2012-02-29 03:22:30 +08003643EmitLoadDexCacheResolvedMethodFieldAddr(uint32_t method_idx) {
3644 llvm::Value* resolved_method_dex_cache_addr =
3645 EmitLoadDexCacheAddr(Method::DexCacheResolvedMethodsOffset());
3646
3647 llvm::Value* method_idx_value = irb_.getPtrEquivInt(method_idx);
3648
3649 return EmitArrayGEP(resolved_method_dex_cache_addr, method_idx_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08003650 irb_.getJObjectTy(), kObject);
Logan Chien61c65dc2012-02-29 03:22:30 +08003651}
3652
3653
3654llvm::Value* MethodCompiler::
Logan Chienbb4d12a2012-02-17 14:10:01 +08003655EmitLoadDexCacheStringFieldAddr(uint32_t string_idx) {
3656 llvm::Value* string_dex_cache_addr =
3657 EmitLoadDexCacheAddr(Method::DexCacheStringsOffset());
3658
3659 llvm::Value* string_idx_value = irb_.getPtrEquivInt(string_idx);
3660
3661 return EmitArrayGEP(string_dex_cache_addr, string_idx_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08003662 irb_.getJObjectTy(), kObject);
Logan Chienbb4d12a2012-02-17 14:10:01 +08003663}
3664
3665
Logan Chien83426162011-12-09 09:29:50 +08003666CompiledMethod *MethodCompiler::Compile() {
Logan Chien0b827102011-12-20 19:46:14 +08003667 // Code generation
3668 CreateFunction();
3669
3670 EmitPrologue();
3671 EmitInstructions();
Logan Chienc670a8d2011-12-20 21:25:56 +08003672 EmitPrologueLastBranch();
Logan Chien0b827102011-12-20 19:46:14 +08003673
Logan Chiend6c239a2011-12-23 15:11:45 +08003674 // Verify the generated bitcode
3675 llvm::verifyFunction(*func_, llvm::PrintMessageAction);
3676
Logan Chien8b977d32012-02-21 19:14:55 +08003677 // Add the memory usage approximation of the compilation unit
3678 cunit_->AddMemUsageApproximation(code_item_->insns_size_in_code_units_ * 900);
3679 // NOTE: From statistic, the bitcode size is 4.5 times bigger than the
3680 // Dex file. Besides, we have to convert the code unit into bytes.
3681 // Thus, we got our magic number 9.
3682
Logan Chien6920bce2012-03-17 21:44:01 +08003683 return new CompiledMethod(cunit_->GetInstructionSet(),
Logan Chien937105a2012-04-02 02:37:37 +08003684 cunit_->GetElfIndex(),
3685 elf_func_idx_);
Logan Chien0b827102011-12-20 19:46:14 +08003686}
3687
3688
3689llvm::Value* MethodCompiler::EmitLoadMethodObjectAddr() {
3690 return func_->arg_begin();
Shih-wei Liaod1fec812012-02-13 09:51:10 -08003691}
Logan Chien83426162011-12-09 09:29:50 +08003692
3693
Logan Chien5bcc04e2012-01-30 14:15:12 +08003694void MethodCompiler::EmitBranchExceptionLandingPad(uint32_t dex_pc) {
3695 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
3696 irb_.CreateBr(lpad);
3697 } else {
3698 irb_.CreateBr(GetUnwindBasicBlock());
3699 }
3700}
3701
3702
3703void MethodCompiler::EmitGuard_ExceptionLandingPad(uint32_t dex_pc) {
3704 llvm::Value* exception_pending =
3705 irb_.CreateCall(irb_.GetRuntime(IsExceptionPending));
3706
3707 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
3708
3709 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
3710 irb_.CreateCondBr(exception_pending, lpad, block_cont);
3711 } else {
3712 irb_.CreateCondBr(exception_pending, GetUnwindBasicBlock(), block_cont);
3713 }
3714
3715 irb_.SetInsertPoint(block_cont);
3716}
3717
3718
Logan Chien924072f2012-01-30 15:07:24 +08003719void MethodCompiler::EmitGuard_GarbageCollectionSuspend(uint32_t dex_pc) {
3720 llvm::Value* runtime_func = irb_.GetRuntime(TestSuspend);
Logan Chien8dfcbea2012-02-17 18:50:32 +08003721 EmitUpdateLineNumFromDexPC(dex_pc);
Logan Chien924072f2012-01-30 15:07:24 +08003722 irb_.CreateCall(runtime_func);
3723
3724 EmitGuard_ExceptionLandingPad(dex_pc);
3725}
3726
3727
Logan Chiend6c239a2011-12-23 15:11:45 +08003728llvm::BasicBlock* MethodCompiler::
3729CreateBasicBlockWithDexPC(uint32_t dex_pc, char const* postfix) {
3730 std::string name;
3731
3732 if (postfix) {
3733 StringAppendF(&name, "B%u.%s", dex_pc, postfix);
3734 } else {
3735 StringAppendF(&name, "B%u", dex_pc);
3736 }
3737
3738 return llvm::BasicBlock::Create(*context_, name, func_);
3739}
3740
3741
3742llvm::BasicBlock* MethodCompiler::GetBasicBlock(uint32_t dex_pc) {
3743 DCHECK(dex_pc < code_item_->insns_size_in_code_units_);
3744
3745 llvm::BasicBlock* basic_block = basic_blocks_[dex_pc];
3746
3747 if (!basic_block) {
3748 basic_block = CreateBasicBlockWithDexPC(dex_pc);
3749 basic_blocks_[dex_pc] = basic_block;
3750 }
3751
3752 return basic_block;
3753}
3754
3755
3756llvm::BasicBlock*
3757MethodCompiler::GetNextBasicBlock(uint32_t dex_pc) {
3758 Instruction const* insn = Instruction::At(code_item_->insns_ + dex_pc);
3759 return GetBasicBlock(dex_pc + insn->SizeInCodeUnits());
3760}
3761
3762
Logan Chien5bcc04e2012-01-30 14:15:12 +08003763int32_t MethodCompiler::GetTryItemOffset(uint32_t dex_pc) {
3764 // TODO: Since we are emitting the dex instructions in ascending order
3765 // w.r.t. address, we can cache the lastest try item offset so that we
3766 // don't have to do binary search for every query.
3767
3768 int32_t min = 0;
3769 int32_t max = code_item_->tries_size_ - 1;
3770
3771 while (min <= max) {
3772 int32_t mid = min + (max - min) / 2;
3773
3774 DexFile::TryItem const* ti = DexFile::GetTryItems(*code_item_, mid);
3775 uint32_t start = ti->start_addr_;
3776 uint32_t end = start + ti->insn_count_;
3777
3778 if (dex_pc < start) {
3779 max = mid - 1;
3780 } else if (dex_pc >= end) {
3781 min = mid + 1;
3782 } else {
3783 return mid; // found
3784 }
3785 }
3786
3787 return -1; // not found
3788}
3789
3790
3791llvm::BasicBlock* MethodCompiler::GetLandingPadBasicBlock(uint32_t dex_pc) {
3792 // Find the try item for this address in this method
3793 int32_t ti_offset = GetTryItemOffset(dex_pc);
3794
3795 if (ti_offset == -1) {
3796 return NULL; // No landing pad is available for this address.
3797 }
3798
3799 // Check for the existing landing pad basic block
3800 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
3801 llvm::BasicBlock* block_lpad = basic_block_landing_pads_[ti_offset];
3802
3803 if (block_lpad) {
3804 // We have generated landing pad for this try item already. Return the
3805 // same basic block.
3806 return block_lpad;
3807 }
3808
3809 // Get try item from code item
3810 DexFile::TryItem const* ti = DexFile::GetTryItems(*code_item_, ti_offset);
3811
3812 // Create landing pad basic block
3813 block_lpad = llvm::BasicBlock::Create(*context_,
3814 StringPrintf("lpad%d", ti_offset),
3815 func_);
3816
3817 // Change IRBuilder insert point
3818 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3819 irb_.SetInsertPoint(block_lpad);
3820
3821 // Find catch block with matching type
3822 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
3823
3824 // TODO: Maybe passing try item offset will be a better idea? For now,
3825 // we are passing dex_pc, so that we can use existing runtime support
3826 // function directly. However, in the runtime supporting function we
3827 // have to search for try item with binary search which can be
3828 // eliminated.
3829 llvm::Value* dex_pc_value = irb_.getInt32(ti->start_addr_);
3830
3831 llvm::Value* catch_handler_index_value =
3832 irb_.CreateCall2(irb_.GetRuntime(FindCatchBlock),
3833 method_object_addr, dex_pc_value);
3834
3835 // Switch instruction (Go to unwind basic block by default)
3836 llvm::SwitchInst* sw =
3837 irb_.CreateSwitch(catch_handler_index_value, GetUnwindBasicBlock());
3838
3839 // Cases with matched catch block
3840 CatchHandlerIterator iter(*code_item_, ti->start_addr_);
3841
3842 for (uint32_t c = 0; iter.HasNext(); iter.Next(), ++c) {
3843 sw->addCase(irb_.getInt32(c), GetBasicBlock(iter.GetHandlerAddress()));
3844 }
3845
3846 // Restore the orignal insert point for IRBuilder
3847 irb_.restoreIP(irb_ip_original);
3848
3849 // Cache this landing pad
3850 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
3851 basic_block_landing_pads_[ti_offset] = block_lpad;
3852
3853 return block_lpad;
3854}
3855
3856
3857llvm::BasicBlock* MethodCompiler::GetUnwindBasicBlock() {
3858 // Check the existing unwinding baisc block block
3859 if (basic_block_unwind_ != NULL) {
3860 return basic_block_unwind_;
3861 }
3862
3863 // Create new basic block for unwinding
3864 basic_block_unwind_ =
3865 llvm::BasicBlock::Create(*context_, "exception_unwind", func_);
3866
3867 // Change IRBuilder insert point
3868 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3869 irb_.SetInsertPoint(basic_block_unwind_);
3870
Logan Chien8dfcbea2012-02-17 18:50:32 +08003871 // Pop the shadow frame
3872 EmitPopShadowFrame();
3873
Logan Chien5bcc04e2012-01-30 14:15:12 +08003874 // Emit the code to return default value (zero) for the given return type.
Logan Chiendd361c92012-04-10 23:40:37 +08003875 char ret_shorty = oat_compilation_unit_->GetShorty()[0];
Logan Chien5bcc04e2012-01-30 14:15:12 +08003876 if (ret_shorty == 'V') {
3877 irb_.CreateRetVoid();
3878 } else {
3879 irb_.CreateRet(irb_.getJZero(ret_shorty));
3880 }
3881
3882 // Restore the orignal insert point for IRBuilder
3883 irb_.restoreIP(irb_ip_original);
3884
3885 return basic_block_unwind_;
3886}
3887
3888
Logan Chienc670a8d2011-12-20 21:25:56 +08003889llvm::Value* MethodCompiler::AllocDalvikLocalVarReg(RegCategory cat,
3890 uint32_t reg_idx) {
3891
3892 // Save current IR builder insert point
3893 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3894
3895 // Alloca
3896 llvm::Value* reg_addr = NULL;
3897
3898 switch (cat) {
3899 case kRegCat1nr:
3900 irb_.SetInsertPoint(basic_block_reg_alloca_);
3901 reg_addr = irb_.CreateAlloca(irb_.getJIntTy(), 0,
3902 StringPrintf("r%u", reg_idx));
3903
3904 irb_.SetInsertPoint(basic_block_reg_zero_init_);
3905 irb_.CreateStore(irb_.getJInt(0), reg_addr);
3906 break;
3907
3908 case kRegCat2:
3909 irb_.SetInsertPoint(basic_block_reg_alloca_);
3910 reg_addr = irb_.CreateAlloca(irb_.getJLongTy(), 0,
3911 StringPrintf("w%u", reg_idx));
3912
3913 irb_.SetInsertPoint(basic_block_reg_zero_init_);
3914 irb_.CreateStore(irb_.getJLong(0), reg_addr);
3915 break;
3916
3917 case kRegObject:
Logan Chien8dfcbea2012-02-17 18:50:32 +08003918 {
3919 irb_.SetInsertPoint(basic_block_shadow_frame_alloca_);
Logan Chienc670a8d2011-12-20 21:25:56 +08003920
Logan Chien8dfcbea2012-02-17 18:50:32 +08003921 llvm::Value* gep_index[] = {
3922 irb_.getInt32(0), // No pointer displacement
3923 irb_.getInt32(1), // SIRT
3924 irb_.getInt32(reg_idx) // Pointer field
3925 };
3926
3927 reg_addr = irb_.CreateGEP(shadow_frame_, gep_index,
3928 StringPrintf("p%u", reg_idx));
3929
3930 irb_.SetInsertPoint(basic_block_reg_zero_init_);
3931 irb_.CreateStore(irb_.getJNull(), reg_addr);
3932 }
Logan Chienc670a8d2011-12-20 21:25:56 +08003933 break;
3934
3935 default:
3936 LOG(FATAL) << "Unknown register category for allocation: " << cat;
3937 }
3938
3939 // Restore IRBuilder insert point
3940 irb_.restoreIP(irb_ip_original);
3941
3942 DCHECK_NE(reg_addr, static_cast<llvm::Value*>(NULL));
3943 return reg_addr;
3944}
3945
3946
3947llvm::Value* MethodCompiler::AllocDalvikRetValReg(RegCategory cat) {
3948 // Save current IR builder insert point
3949 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3950
3951 // Alloca
3952 llvm::Value* reg_addr = NULL;
3953
3954 switch (cat) {
3955 case kRegCat1nr:
3956 irb_.SetInsertPoint(basic_block_reg_alloca_);
3957 reg_addr = irb_.CreateAlloca(irb_.getJIntTy(), 0, "r_res");
3958 break;
3959
3960 case kRegCat2:
3961 irb_.SetInsertPoint(basic_block_reg_alloca_);
3962 reg_addr = irb_.CreateAlloca(irb_.getJLongTy(), 0, "w_res");
3963 break;
3964
3965 case kRegObject:
3966 irb_.SetInsertPoint(basic_block_reg_alloca_);
3967 reg_addr = irb_.CreateAlloca(irb_.getJObjectTy(), 0, "p_res");
3968 break;
3969
3970 default:
3971 LOG(FATAL) << "Unknown register category for allocation: " << cat;
3972 }
3973
3974 // Restore IRBuilder insert point
3975 irb_.restoreIP(irb_ip_original);
3976
3977 DCHECK_NE(reg_addr, static_cast<llvm::Value*>(NULL));
3978 return reg_addr;
3979}
3980
3981
Logan Chien8dfcbea2012-02-17 18:50:32 +08003982void MethodCompiler::EmitPopShadowFrame() {
3983 irb_.CreateCall(irb_.GetRuntime(PopShadowFrame));
3984}
3985
3986
3987void MethodCompiler::EmitUpdateLineNum(int32_t line_num) {
Logan Chien1b0a1b72012-03-15 06:20:17 +08003988 llvm::Value* line_num_field_addr =
3989 irb_.CreatePtrDisp(shadow_frame_,
3990 irb_.getPtrEquivInt(ShadowFrame::LineNumOffset()),
3991 irb_.getJIntTy()->getPointerTo());
Logan Chien8dfcbea2012-02-17 18:50:32 +08003992
Logan Chien8dfcbea2012-02-17 18:50:32 +08003993 llvm::ConstantInt* line_num_value = irb_.getInt32(line_num);
3994 irb_.CreateStore(line_num_value, line_num_field_addr);
3995}
3996
3997
3998void MethodCompiler::EmitUpdateLineNumFromDexPC(uint32_t dex_pc) {
Logan Chiendd361c92012-04-10 23:40:37 +08003999 EmitUpdateLineNum(
4000 dex_file_->GetLineNumFromPC(oat_compilation_unit_->IsStatic(),
4001 method_idx_, code_item_, dex_pc));
Logan Chien8dfcbea2012-02-17 18:50:32 +08004002}
4003
4004
Logan Chien83426162011-12-09 09:29:50 +08004005} // namespace compiler_llvm
4006} // namespace art