blob: 91b57a677f41f1c24834d4073aec808fb76a7c31 [file] [log] [blame]
Shih-wei Liaod1fec812012-02-13 09:51:10 -08001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "method_compiler.h"
18
Logan Chienfca7e872011-12-20 20:08:22 +080019#include "backend_types.h"
Logan Chien8b977d32012-02-21 19:14:55 +080020#include "compilation_unit.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080021#include "compiler.h"
Logan Chiena78e3c82011-12-27 17:59:35 +080022#include "inferred_reg_category_map.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080023#include "ir_builder.h"
24#include "logging.h"
Logan Chien4dd96f52012-02-29 01:26:58 +080025#include "oat_compilation_unit.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080026#include "object.h"
27#include "object_utils.h"
Logan Chien42e0e152012-01-13 15:42:36 +080028#include "runtime_support_func.h"
TDYa1275bb86012012-04-11 05:57:28 -070029#include "runtime_support_llvm.h"
Logan Chien1b0a1b72012-03-15 06:20:17 +080030#include "shadow_frame.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080031#include "stl_util.h"
Logan Chien0b827102011-12-20 19:46:14 +080032#include "stringprintf.h"
33#include "utils_llvm.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070034#include "verifier/method_verifier.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080035
36#include <iomanip>
37
38#include <llvm/Analysis/Verifier.h>
Logan Chienc670a8d2011-12-20 21:25:56 +080039#include <llvm/BasicBlock.h>
Shih-wei Liaod1fec812012-02-13 09:51:10 -080040#include <llvm/Function.h>
Logan Chiena85fb2f2012-01-16 12:52:56 +080041#include <llvm/GlobalVariable.h>
42#include <llvm/Intrinsics.h>
Shih-wei Liaod1fec812012-02-13 09:51:10 -080043
Logan Chien83426162011-12-09 09:29:50 +080044namespace art {
45namespace compiler_llvm {
Shih-wei Liaod1fec812012-02-13 09:51:10 -080046
Logan Chien42e0e152012-01-13 15:42:36 +080047using namespace runtime_support;
48
Shih-wei Liaod1fec812012-02-13 09:51:10 -080049
Logan Chien8b977d32012-02-21 19:14:55 +080050MethodCompiler::MethodCompiler(CompilationUnit* cunit,
Logan Chien83426162011-12-09 09:29:50 +080051 Compiler* compiler,
Logan Chien4dd96f52012-02-29 01:26:58 +080052 OatCompilationUnit* oat_compilation_unit)
Logan Chien8b977d32012-02-21 19:14:55 +080053 : cunit_(cunit), compiler_(compiler),
54 class_linker_(oat_compilation_unit->class_linker_),
55 class_loader_(oat_compilation_unit->class_loader_),
56 dex_file_(oat_compilation_unit->dex_file_),
57 dex_cache_(oat_compilation_unit->dex_cache_),
58 code_item_(oat_compilation_unit->code_item_),
59 oat_compilation_unit_(oat_compilation_unit),
Logan Chien8b977d32012-02-21 19:14:55 +080060 method_idx_(oat_compilation_unit->method_idx_),
61 access_flags_(oat_compilation_unit->access_flags_),
62 module_(cunit->GetModule()),
63 context_(cunit->GetLLVMContext()),
64 irb_(*cunit->GetIRBuilder()), func_(NULL), retval_reg_(NULL),
Ian Rogers776ac1f2012-04-13 23:36:36 -070065 basic_block_stack_overflow_(NULL),
Logan Chien8b977d32012-02-21 19:14:55 +080066 basic_block_reg_alloca_(NULL), basic_block_shadow_frame_alloca_(NULL),
67 basic_block_reg_zero_init_(NULL), basic_block_reg_arg_init_(NULL),
68 basic_blocks_(code_item_->insns_size_in_code_units_),
69 basic_block_landing_pads_(code_item_->tries_size_, NULL),
70 basic_block_unwind_(NULL), basic_block_unreachable_(NULL),
Logan Chien937105a2012-04-02 02:37:37 +080071 shadow_frame_(NULL), elf_func_idx_(cunit_->AcquireUniqueElfFuncIndex()) {
Shih-wei Liaod1fec812012-02-13 09:51:10 -080072}
73
74
75MethodCompiler::~MethodCompiler() {
Logan Chienc670a8d2011-12-20 21:25:56 +080076 STLDeleteElements(&regs_);
Shih-wei Liaod1fec812012-02-13 09:51:10 -080077}
78
79
Logan Chien0b827102011-12-20 19:46:14 +080080void MethodCompiler::CreateFunction() {
81 // LLVM function name
Logan Chien937105a2012-04-02 02:37:37 +080082 std::string func_name(ElfFuncName(elf_func_idx_));
Logan Chien0b827102011-12-20 19:46:14 +080083
84 // Get function type
85 llvm::FunctionType* func_type =
Logan Chiendd361c92012-04-10 23:40:37 +080086 GetFunctionType(method_idx_, oat_compilation_unit_->IsStatic());
Logan Chien0b827102011-12-20 19:46:14 +080087
88 // Create function
89 func_ = llvm::Function::Create(func_type, llvm::Function::ExternalLinkage,
90 func_name, module_);
91
92 // Set argument name
93 llvm::Function::arg_iterator arg_iter(func_->arg_begin());
94 llvm::Function::arg_iterator arg_end(func_->arg_end());
95
96 DCHECK_NE(arg_iter, arg_end);
97 arg_iter->setName("method");
98 ++arg_iter;
99
Logan Chiendd361c92012-04-10 23:40:37 +0800100 if (!oat_compilation_unit_->IsStatic()) {
Logan Chien0b827102011-12-20 19:46:14 +0800101 DCHECK_NE(arg_iter, arg_end);
102 arg_iter->setName("this");
103 ++arg_iter;
104 }
105
106 for (unsigned i = 0; arg_iter != arg_end; ++i, ++arg_iter) {
107 arg_iter->setName(StringPrintf("a%u", i));
108 }
109}
110
111
112llvm::FunctionType* MethodCompiler::GetFunctionType(uint32_t method_idx,
113 bool is_static) {
114 // Get method signature
115 DexFile::MethodId const& method_id = dex_file_->GetMethodId(method_idx);
116
Logan Chien8faf8022012-02-24 12:25:29 +0800117 uint32_t shorty_size;
Logan Chien0b827102011-12-20 19:46:14 +0800118 char const* shorty = dex_file_->GetMethodShorty(method_id, &shorty_size);
Logan Chien8faf8022012-02-24 12:25:29 +0800119 CHECK_GE(shorty_size, 1u);
Logan Chien0b827102011-12-20 19:46:14 +0800120
121 // Get return type
122 llvm::Type* ret_type = irb_.getJType(shorty[0], kAccurate);
123
124 // Get argument type
125 std::vector<llvm::Type*> args_type;
126
127 args_type.push_back(irb_.getJObjectTy()); // method object pointer
128
129 if (!is_static) {
130 args_type.push_back(irb_.getJType('L', kAccurate)); // "this" object pointer
131 }
132
Logan Chien8faf8022012-02-24 12:25:29 +0800133 for (uint32_t i = 1; i < shorty_size; ++i) {
Logan Chien0b827102011-12-20 19:46:14 +0800134 args_type.push_back(irb_.getJType(shorty[i], kAccurate));
135 }
136
137 return llvm::FunctionType::get(ret_type, args_type, false);
138}
139
140
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800141void MethodCompiler::EmitPrologue() {
Logan Chienc670a8d2011-12-20 21:25:56 +0800142 // Create basic blocks for prologue
TDYa1274165a832012-04-03 17:47:16 -0700143 basic_block_stack_overflow_ =
144 llvm::BasicBlock::Create(*context_, "prologue.stack_overflow_check", func_);
145
Logan Chienc670a8d2011-12-20 21:25:56 +0800146 basic_block_reg_alloca_ =
147 llvm::BasicBlock::Create(*context_, "prologue.alloca", func_);
148
Logan Chien8dfcbea2012-02-17 18:50:32 +0800149 basic_block_shadow_frame_alloca_ =
150 llvm::BasicBlock::Create(*context_, "prologue.shadowframe", func_);
151
Logan Chienc670a8d2011-12-20 21:25:56 +0800152 basic_block_reg_zero_init_ =
153 llvm::BasicBlock::Create(*context_, "prologue.zeroinit", func_);
154
Logan Chiend6ececa2011-12-27 16:20:15 +0800155 basic_block_reg_arg_init_ =
156 llvm::BasicBlock::Create(*context_, "prologue.arginit", func_);
157
TDYa1274165a832012-04-03 17:47:16 -0700158 // Before alloca, check stack overflow.
159 EmitStackOverflowCheck();
160
Logan Chienc670a8d2011-12-20 21:25:56 +0800161 // Create register array
162 for (uint16_t r = 0; r < code_item_->registers_size_; ++r) {
163 regs_.push_back(DalvikReg::CreateLocalVarReg(*this, r));
164 }
165
166 retval_reg_.reset(DalvikReg::CreateRetValReg(*this));
Logan Chiend6ececa2011-12-27 16:20:15 +0800167
Logan Chien8dfcbea2012-02-17 18:50:32 +0800168 // Create Shadow Frame
169 EmitPrologueAllocShadowFrame();
170
Logan Chiend6ececa2011-12-27 16:20:15 +0800171 // Store argument to dalvik register
172 irb_.SetInsertPoint(basic_block_reg_arg_init_);
173 EmitPrologueAssignArgRegister();
174
175 // Branch to start address
176 irb_.CreateBr(GetBasicBlock(0));
Logan Chienc670a8d2011-12-20 21:25:56 +0800177}
178
179
TDYa1274165a832012-04-03 17:47:16 -0700180void MethodCompiler::EmitStackOverflowCheck() {
181 irb_.SetInsertPoint(basic_block_stack_overflow_);
182
183 // Call llvm intrinsic function to get frame address.
184 llvm::Function* frameaddress =
185 llvm::Intrinsic::getDeclaration(module_, llvm::Intrinsic::frameaddress);
186
187 // The type of llvm::frameaddress is: i8* @llvm.frameaddress(i32)
188 llvm::Value* frame_address = irb_.CreateCall(frameaddress, irb_.getInt32(0));
189
190 // Cast i8* to int
191 frame_address = irb_.CreatePtrToInt(frame_address, irb_.getPtrEquivIntTy());
192
193 // Get thread.stack_end_
194 llvm::Value* thread_object_addr =
195 irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
196
197 llvm::Value* stack_end_addr =
198 irb_.CreatePtrDisp(thread_object_addr,
199 irb_.getPtrEquivInt(Thread::StackEndOffset().Int32Value()),
200 irb_.getPtrEquivIntTy()->getPointerTo());
201
202 llvm::Value* stack_end = irb_.CreateLoad(stack_end_addr);
203
204 // Check the frame address < thread.stack_end_ ?
205 llvm::Value* is_stack_overflow = irb_.CreateICmpULT(frame_address, stack_end);
206
207 llvm::BasicBlock* block_exception =
208 llvm::BasicBlock::Create(*context_, "stack_overflow", func_);
209
210 llvm::BasicBlock* block_continue =
211 llvm::BasicBlock::Create(*context_, "stack_overflow_cont", func_);
212
213 irb_.CreateCondBr(is_stack_overflow, block_exception, block_continue);
214
215 // If stack overflow, throw exception.
216 irb_.SetInsertPoint(block_exception);
217 irb_.CreateCall(irb_.GetRuntime(ThrowStackOverflowException));
218
219 // Unwind.
Logan Chiendd361c92012-04-10 23:40:37 +0800220 char ret_shorty = oat_compilation_unit_->GetShorty()[0];
TDYa1274165a832012-04-03 17:47:16 -0700221 if (ret_shorty == 'V') {
222 irb_.CreateRetVoid();
223 } else {
224 irb_.CreateRet(irb_.getJZero(ret_shorty));
225 }
226
227 basic_block_stack_overflow_ = block_continue;
228}
229
230
Logan Chienc670a8d2011-12-20 21:25:56 +0800231void MethodCompiler::EmitPrologueLastBranch() {
TDYa1274165a832012-04-03 17:47:16 -0700232 irb_.SetInsertPoint(basic_block_stack_overflow_);
233 irb_.CreateBr(basic_block_reg_alloca_);
234
Logan Chienc670a8d2011-12-20 21:25:56 +0800235 irb_.SetInsertPoint(basic_block_reg_alloca_);
Logan Chien8dfcbea2012-02-17 18:50:32 +0800236 irb_.CreateBr(basic_block_shadow_frame_alloca_);
237
238 irb_.SetInsertPoint(basic_block_shadow_frame_alloca_);
Logan Chienc670a8d2011-12-20 21:25:56 +0800239 irb_.CreateBr(basic_block_reg_zero_init_);
240
241 irb_.SetInsertPoint(basic_block_reg_zero_init_);
Logan Chiend6ececa2011-12-27 16:20:15 +0800242 irb_.CreateBr(basic_block_reg_arg_init_);
243}
244
245
Logan Chien8dfcbea2012-02-17 18:50:32 +0800246void MethodCompiler::EmitPrologueAllocShadowFrame() {
247 irb_.SetInsertPoint(basic_block_shadow_frame_alloca_);
248
249 // Allocate the shadow frame now!
250 uint32_t sirt_size = code_item_->registers_size_;
251 // TODO: registers_size_ is a bad approximation. Compute a
252 // tighter approximation at Dex verifier while performing data-flow
253 // analysis.
254
255 llvm::StructType* shadow_frame_type = irb_.getShadowFrameTy(sirt_size);
256 shadow_frame_ = irb_.CreateAlloca(shadow_frame_type);
257
258 // Zero-initialization of the shadow frame
259 llvm::ConstantAggregateZero* zero_initializer =
260 llvm::ConstantAggregateZero::get(shadow_frame_type);
261
262 irb_.CreateStore(zero_initializer, shadow_frame_);
263
Logan Chien8dfcbea2012-02-17 18:50:32 +0800264 // Store the method pointer
Logan Chien1b0a1b72012-03-15 06:20:17 +0800265 llvm::Value* method_field_addr =
266 irb_.CreatePtrDisp(shadow_frame_,
267 irb_.getPtrEquivInt(ShadowFrame::MethodOffset()),
268 irb_.getJObjectTy()->getPointerTo());
269
Logan Chien8dfcbea2012-02-17 18:50:32 +0800270 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
271 irb_.CreateStore(method_object_addr, method_field_addr);
272
273 // Store the number of the pointer slots
Logan Chien1b0a1b72012-03-15 06:20:17 +0800274 llvm::ConstantInt* num_of_refs_offset =
275 irb_.getPtrEquivInt(ShadowFrame::NumberOfReferencesOffset());
276
277 llvm::Value* num_of_refs_field_addr =
278 irb_.CreatePtrDisp(shadow_frame_, num_of_refs_offset,
279 irb_.getJIntTy()->getPointerTo());
280
281 llvm::ConstantInt* num_of_refs_value = irb_.getJInt(sirt_size);
282 irb_.CreateStore(num_of_refs_value, num_of_refs_field_addr);
Logan Chien8dfcbea2012-02-17 18:50:32 +0800283
284 // Push the shadow frame
285 llvm::Value* shadow_frame_upcast =
286 irb_.CreateConstGEP2_32(shadow_frame_, 0, 0);
287
288 irb_.CreateCall(irb_.GetRuntime(PushShadowFrame), shadow_frame_upcast);
289}
290
291
Logan Chiend6ececa2011-12-27 16:20:15 +0800292void MethodCompiler::EmitPrologueAssignArgRegister() {
293 uint16_t arg_reg = code_item_->registers_size_ - code_item_->ins_size_;
294
295 llvm::Function::arg_iterator arg_iter(func_->arg_begin());
296 llvm::Function::arg_iterator arg_end(func_->arg_end());
297
Logan Chiendd361c92012-04-10 23:40:37 +0800298 uint32_t shorty_size = 0;
299 char const* shorty = oat_compilation_unit_->GetShorty(&shorty_size);
300 CHECK_GE(shorty_size, 1u);
Logan Chiend6ececa2011-12-27 16:20:15 +0800301
302 ++arg_iter; // skip method object
303
Logan Chiendd361c92012-04-10 23:40:37 +0800304 if (!oat_compilation_unit_->IsStatic()) {
Logan Chiend6ececa2011-12-27 16:20:15 +0800305 EmitStoreDalvikReg(arg_reg, kObject, kAccurate, arg_iter);
306 ++arg_iter;
307 ++arg_reg;
308 }
309
Logan Chiendd361c92012-04-10 23:40:37 +0800310 for (uint32_t i = 1; i < shorty_size; ++i, ++arg_iter) {
Logan Chiend6ececa2011-12-27 16:20:15 +0800311 EmitStoreDalvikReg(arg_reg, shorty[i], kAccurate, arg_iter);
312
313 ++arg_reg;
314 if (shorty[i] == 'J' || shorty[i] == 'D') {
315 // Wide types, such as long and double, are using a pair of registers
316 // to store the value, so we have to increase arg_reg again.
317 ++arg_reg;
318 }
319 }
320
321 DCHECK_EQ(arg_end, arg_iter);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800322}
323
324
Logan Chien83426162011-12-09 09:29:50 +0800325void MethodCompiler::EmitInstructions() {
Logan Chiend6c239a2011-12-23 15:11:45 +0800326 uint32_t dex_pc = 0;
327 while (dex_pc < code_item_->insns_size_in_code_units_) {
328 Instruction const* insn = Instruction::At(code_item_->insns_ + dex_pc);
329 EmitInstruction(dex_pc, insn);
330 dex_pc += insn->SizeInCodeUnits();
331 }
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800332}
333
334
Logan Chien83426162011-12-09 09:29:50 +0800335void MethodCompiler::EmitInstruction(uint32_t dex_pc,
336 Instruction const* insn) {
Logan Chiend6c239a2011-12-23 15:11:45 +0800337
338 // Set the IRBuilder insertion point
339 irb_.SetInsertPoint(GetBasicBlock(dex_pc));
340
Logan Chien70f94b42011-12-27 17:49:11 +0800341#define ARGS dex_pc, insn
342
343 // Dispatch the instruction
344 switch (insn->Opcode()) {
345 case Instruction::NOP:
346 EmitInsn_Nop(ARGS);
347 break;
348
349 case Instruction::MOVE:
350 case Instruction::MOVE_FROM16:
351 case Instruction::MOVE_16:
352 EmitInsn_Move(ARGS, kInt);
353 break;
354
355 case Instruction::MOVE_WIDE:
356 case Instruction::MOVE_WIDE_FROM16:
357 case Instruction::MOVE_WIDE_16:
358 EmitInsn_Move(ARGS, kLong);
359 break;
360
361 case Instruction::MOVE_OBJECT:
362 case Instruction::MOVE_OBJECT_FROM16:
363 case Instruction::MOVE_OBJECT_16:
364 EmitInsn_Move(ARGS, kObject);
365 break;
366
367 case Instruction::MOVE_RESULT:
368 EmitInsn_MoveResult(ARGS, kInt);
369 break;
370
371 case Instruction::MOVE_RESULT_WIDE:
372 EmitInsn_MoveResult(ARGS, kLong);
373 break;
374
375 case Instruction::MOVE_RESULT_OBJECT:
376 EmitInsn_MoveResult(ARGS, kObject);
377 break;
378
379 case Instruction::MOVE_EXCEPTION:
380 EmitInsn_MoveException(ARGS);
381 break;
382
383 case Instruction::RETURN_VOID:
384 EmitInsn_ReturnVoid(ARGS);
385 break;
386
387 case Instruction::RETURN:
388 case Instruction::RETURN_WIDE:
389 case Instruction::RETURN_OBJECT:
390 EmitInsn_Return(ARGS);
391 break;
392
393 case Instruction::CONST_4:
394 case Instruction::CONST_16:
395 case Instruction::CONST:
396 case Instruction::CONST_HIGH16:
397 EmitInsn_LoadConstant(ARGS, kInt);
398 break;
399
400 case Instruction::CONST_WIDE_16:
401 case Instruction::CONST_WIDE_32:
402 case Instruction::CONST_WIDE:
403 case Instruction::CONST_WIDE_HIGH16:
404 EmitInsn_LoadConstant(ARGS, kLong);
405 break;
406
407 case Instruction::CONST_STRING:
408 case Instruction::CONST_STRING_JUMBO:
409 EmitInsn_LoadConstantString(ARGS);
410 break;
411
412 case Instruction::CONST_CLASS:
413 EmitInsn_LoadConstantClass(ARGS);
414 break;
415
416 case Instruction::MONITOR_ENTER:
417 EmitInsn_MonitorEnter(ARGS);
418 break;
419
420 case Instruction::MONITOR_EXIT:
421 EmitInsn_MonitorExit(ARGS);
422 break;
423
424 case Instruction::CHECK_CAST:
425 EmitInsn_CheckCast(ARGS);
426 break;
427
428 case Instruction::INSTANCE_OF:
429 EmitInsn_InstanceOf(ARGS);
430 break;
431
432 case Instruction::ARRAY_LENGTH:
433 EmitInsn_ArrayLength(ARGS);
434 break;
435
436 case Instruction::NEW_INSTANCE:
437 EmitInsn_NewInstance(ARGS);
438 break;
439
440 case Instruction::NEW_ARRAY:
441 EmitInsn_NewArray(ARGS);
442 break;
443
444 case Instruction::FILLED_NEW_ARRAY:
445 EmitInsn_FilledNewArray(ARGS, false);
446 break;
447
448 case Instruction::FILLED_NEW_ARRAY_RANGE:
449 EmitInsn_FilledNewArray(ARGS, true);
450 break;
451
452 case Instruction::FILL_ARRAY_DATA:
453 EmitInsn_FillArrayData(ARGS);
454 break;
455
456 case Instruction::THROW:
457 EmitInsn_ThrowException(ARGS);
458 break;
459
460 case Instruction::GOTO:
461 case Instruction::GOTO_16:
462 case Instruction::GOTO_32:
463 EmitInsn_UnconditionalBranch(ARGS);
464 break;
465
466 case Instruction::PACKED_SWITCH:
467 EmitInsn_PackedSwitch(ARGS);
468 break;
469
470 case Instruction::SPARSE_SWITCH:
471 EmitInsn_SparseSwitch(ARGS);
472 break;
473
474 case Instruction::CMPL_FLOAT:
475 EmitInsn_FPCompare(ARGS, kFloat, false);
476 break;
477
478 case Instruction::CMPG_FLOAT:
479 EmitInsn_FPCompare(ARGS, kFloat, true);
480 break;
481
482 case Instruction::CMPL_DOUBLE:
483 EmitInsn_FPCompare(ARGS, kDouble, false);
484 break;
485
486 case Instruction::CMPG_DOUBLE:
487 EmitInsn_FPCompare(ARGS, kDouble, true);
488 break;
489
490 case Instruction::CMP_LONG:
491 EmitInsn_LongCompare(ARGS);
492 break;
493
494 case Instruction::IF_EQ:
495 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_EQ);
496 break;
497
498 case Instruction::IF_NE:
499 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_NE);
500 break;
501
502 case Instruction::IF_LT:
503 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_LT);
504 break;
505
506 case Instruction::IF_GE:
507 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_GE);
508 break;
509
510 case Instruction::IF_GT:
511 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_GT);
512 break;
513
514 case Instruction::IF_LE:
515 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_LE);
516 break;
517
518 case Instruction::IF_EQZ:
519 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_EQ);
520 break;
521
522 case Instruction::IF_NEZ:
523 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_NE);
524 break;
525
526 case Instruction::IF_LTZ:
527 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_LT);
528 break;
529
530 case Instruction::IF_GEZ:
531 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_GE);
532 break;
533
534 case Instruction::IF_GTZ:
535 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_GT);
536 break;
537
538 case Instruction::IF_LEZ:
539 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_LE);
540 break;
541
542 case Instruction::AGET:
543 EmitInsn_AGet(ARGS, kInt);
544 break;
545
546 case Instruction::AGET_WIDE:
547 EmitInsn_AGet(ARGS, kLong);
548 break;
549
550 case Instruction::AGET_OBJECT:
551 EmitInsn_AGet(ARGS, kObject);
552 break;
553
554 case Instruction::AGET_BOOLEAN:
555 EmitInsn_AGet(ARGS, kBoolean);
556 break;
557
558 case Instruction::AGET_BYTE:
559 EmitInsn_AGet(ARGS, kByte);
560 break;
561
562 case Instruction::AGET_CHAR:
563 EmitInsn_AGet(ARGS, kChar);
564 break;
565
566 case Instruction::AGET_SHORT:
567 EmitInsn_AGet(ARGS, kShort);
568 break;
569
570 case Instruction::APUT:
571 EmitInsn_APut(ARGS, kInt);
572 break;
573
574 case Instruction::APUT_WIDE:
575 EmitInsn_APut(ARGS, kLong);
576 break;
577
578 case Instruction::APUT_OBJECT:
579 EmitInsn_APut(ARGS, kObject);
580 break;
581
582 case Instruction::APUT_BOOLEAN:
583 EmitInsn_APut(ARGS, kBoolean);
584 break;
585
586 case Instruction::APUT_BYTE:
587 EmitInsn_APut(ARGS, kByte);
588 break;
589
590 case Instruction::APUT_CHAR:
591 EmitInsn_APut(ARGS, kChar);
592 break;
593
594 case Instruction::APUT_SHORT:
595 EmitInsn_APut(ARGS, kShort);
596 break;
597
598 case Instruction::IGET:
599 EmitInsn_IGet(ARGS, kInt);
600 break;
601
602 case Instruction::IGET_WIDE:
603 EmitInsn_IGet(ARGS, kLong);
604 break;
605
606 case Instruction::IGET_OBJECT:
607 EmitInsn_IGet(ARGS, kObject);
608 break;
609
610 case Instruction::IGET_BOOLEAN:
611 EmitInsn_IGet(ARGS, kBoolean);
612 break;
613
614 case Instruction::IGET_BYTE:
615 EmitInsn_IGet(ARGS, kByte);
616 break;
617
618 case Instruction::IGET_CHAR:
619 EmitInsn_IGet(ARGS, kChar);
620 break;
621
622 case Instruction::IGET_SHORT:
623 EmitInsn_IGet(ARGS, kShort);
624 break;
625
626 case Instruction::IPUT:
627 EmitInsn_IPut(ARGS, kInt);
628 break;
629
630 case Instruction::IPUT_WIDE:
631 EmitInsn_IPut(ARGS, kLong);
632 break;
633
634 case Instruction::IPUT_OBJECT:
635 EmitInsn_IPut(ARGS, kObject);
636 break;
637
638 case Instruction::IPUT_BOOLEAN:
639 EmitInsn_IPut(ARGS, kBoolean);
640 break;
641
642 case Instruction::IPUT_BYTE:
643 EmitInsn_IPut(ARGS, kByte);
644 break;
645
646 case Instruction::IPUT_CHAR:
647 EmitInsn_IPut(ARGS, kChar);
648 break;
649
650 case Instruction::IPUT_SHORT:
651 EmitInsn_IPut(ARGS, kShort);
652 break;
653
654 case Instruction::SGET:
655 EmitInsn_SGet(ARGS, kInt);
656 break;
657
658 case Instruction::SGET_WIDE:
659 EmitInsn_SGet(ARGS, kLong);
660 break;
661
662 case Instruction::SGET_OBJECT:
663 EmitInsn_SGet(ARGS, kObject);
664 break;
665
666 case Instruction::SGET_BOOLEAN:
667 EmitInsn_SGet(ARGS, kBoolean);
668 break;
669
670 case Instruction::SGET_BYTE:
671 EmitInsn_SGet(ARGS, kByte);
672 break;
673
674 case Instruction::SGET_CHAR:
675 EmitInsn_SGet(ARGS, kChar);
676 break;
677
678 case Instruction::SGET_SHORT:
679 EmitInsn_SGet(ARGS, kShort);
680 break;
681
682 case Instruction::SPUT:
683 EmitInsn_SPut(ARGS, kInt);
684 break;
685
686 case Instruction::SPUT_WIDE:
687 EmitInsn_SPut(ARGS, kLong);
688 break;
689
690 case Instruction::SPUT_OBJECT:
691 EmitInsn_SPut(ARGS, kObject);
692 break;
693
694 case Instruction::SPUT_BOOLEAN:
695 EmitInsn_SPut(ARGS, kBoolean);
696 break;
697
698 case Instruction::SPUT_BYTE:
699 EmitInsn_SPut(ARGS, kByte);
700 break;
701
702 case Instruction::SPUT_CHAR:
703 EmitInsn_SPut(ARGS, kChar);
704 break;
705
706 case Instruction::SPUT_SHORT:
707 EmitInsn_SPut(ARGS, kShort);
708 break;
709
710
711 case Instruction::INVOKE_VIRTUAL:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800712 EmitInsn_Invoke(ARGS, kVirtual, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800713 break;
714
715 case Instruction::INVOKE_SUPER:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800716 EmitInsn_Invoke(ARGS, kSuper, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800717 break;
718
719 case Instruction::INVOKE_DIRECT:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800720 EmitInsn_Invoke(ARGS, kDirect, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800721 break;
722
723 case Instruction::INVOKE_STATIC:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800724 EmitInsn_Invoke(ARGS, kStatic, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800725 break;
726
727 case Instruction::INVOKE_INTERFACE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800728 EmitInsn_Invoke(ARGS, kInterface, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800729 break;
730
731 case Instruction::INVOKE_VIRTUAL_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800732 EmitInsn_Invoke(ARGS, kVirtual, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800733 break;
734
735 case Instruction::INVOKE_SUPER_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800736 EmitInsn_Invoke(ARGS, kSuper, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800737 break;
738
739 case Instruction::INVOKE_DIRECT_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800740 EmitInsn_Invoke(ARGS, kDirect, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800741 break;
742
743 case Instruction::INVOKE_STATIC_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800744 EmitInsn_Invoke(ARGS, kStatic, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800745 break;
746
747 case Instruction::INVOKE_INTERFACE_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800748 EmitInsn_Invoke(ARGS, kInterface, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800749 break;
750
751 case Instruction::NEG_INT:
752 EmitInsn_Neg(ARGS, kInt);
753 break;
754
755 case Instruction::NOT_INT:
756 EmitInsn_Not(ARGS, kInt);
757 break;
758
759 case Instruction::NEG_LONG:
760 EmitInsn_Neg(ARGS, kLong);
761 break;
762
763 case Instruction::NOT_LONG:
764 EmitInsn_Not(ARGS, kLong);
765 break;
766
767 case Instruction::NEG_FLOAT:
768 EmitInsn_FNeg(ARGS, kFloat);
769 break;
770
771 case Instruction::NEG_DOUBLE:
772 EmitInsn_FNeg(ARGS, kDouble);
773 break;
774
775 case Instruction::INT_TO_LONG:
776 EmitInsn_SExt(ARGS);
777 break;
778
779 case Instruction::INT_TO_FLOAT:
780 EmitInsn_IntToFP(ARGS, kInt, kFloat);
781 break;
782
783 case Instruction::INT_TO_DOUBLE:
784 EmitInsn_IntToFP(ARGS, kInt, kDouble);
785 break;
786
787 case Instruction::LONG_TO_INT:
788 EmitInsn_Trunc(ARGS);
789 break;
790
791 case Instruction::LONG_TO_FLOAT:
792 EmitInsn_IntToFP(ARGS, kLong, kFloat);
793 break;
794
795 case Instruction::LONG_TO_DOUBLE:
796 EmitInsn_IntToFP(ARGS, kLong, kDouble);
797 break;
798
799 case Instruction::FLOAT_TO_INT:
TDYa127a4746872012-04-11 23:48:55 -0700800 EmitInsn_FPToInt(ARGS, kFloat, kInt, F2I);
Logan Chien70f94b42011-12-27 17:49:11 +0800801 break;
802
803 case Instruction::FLOAT_TO_LONG:
TDYa127a4746872012-04-11 23:48:55 -0700804 EmitInsn_FPToInt(ARGS, kFloat, kLong, F2L);
Logan Chien70f94b42011-12-27 17:49:11 +0800805 break;
806
807 case Instruction::FLOAT_TO_DOUBLE:
808 EmitInsn_FExt(ARGS);
809 break;
810
811 case Instruction::DOUBLE_TO_INT:
TDYa127a4746872012-04-11 23:48:55 -0700812 EmitInsn_FPToInt(ARGS, kDouble, kInt, D2I);
Logan Chien70f94b42011-12-27 17:49:11 +0800813 break;
814
815 case Instruction::DOUBLE_TO_LONG:
TDYa127a4746872012-04-11 23:48:55 -0700816 EmitInsn_FPToInt(ARGS, kDouble, kLong, D2L);
Logan Chien70f94b42011-12-27 17:49:11 +0800817 break;
818
819 case Instruction::DOUBLE_TO_FLOAT:
820 EmitInsn_FTrunc(ARGS);
821 break;
822
823 case Instruction::INT_TO_BYTE:
824 EmitInsn_TruncAndSExt(ARGS, 8);
825 break;
826
827 case Instruction::INT_TO_CHAR:
828 EmitInsn_TruncAndZExt(ARGS, 16);
829 break;
830
831 case Instruction::INT_TO_SHORT:
832 EmitInsn_TruncAndSExt(ARGS, 16);
833 break;
834
835 case Instruction::ADD_INT:
836 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kInt, false);
837 break;
838
839 case Instruction::SUB_INT:
840 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kInt, false);
841 break;
842
843 case Instruction::MUL_INT:
844 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kInt, false);
845 break;
846
847 case Instruction::DIV_INT:
848 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kInt, false);
849 break;
850
851 case Instruction::REM_INT:
852 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kInt, false);
853 break;
854
855 case Instruction::AND_INT:
856 EmitInsn_IntArithm(ARGS, kIntArithm_And, kInt, false);
857 break;
858
859 case Instruction::OR_INT:
860 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kInt, false);
861 break;
862
863 case Instruction::XOR_INT:
864 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kInt, false);
865 break;
866
867 case Instruction::SHL_INT:
Logan Chien5539ad02012-04-02 14:36:55 +0800868 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kInt, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800869 break;
870
871 case Instruction::SHR_INT:
Logan Chien5539ad02012-04-02 14:36:55 +0800872 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kInt, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800873 break;
874
875 case Instruction::USHR_INT:
Logan Chien5539ad02012-04-02 14:36:55 +0800876 EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kInt, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800877 break;
878
879 case Instruction::ADD_LONG:
880 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kLong, false);
881 break;
882
883 case Instruction::SUB_LONG:
884 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kLong, false);
885 break;
886
887 case Instruction::MUL_LONG:
888 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kLong, false);
889 break;
890
891 case Instruction::DIV_LONG:
892 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kLong, false);
893 break;
894
895 case Instruction::REM_LONG:
896 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kLong, false);
897 break;
898
899 case Instruction::AND_LONG:
900 EmitInsn_IntArithm(ARGS, kIntArithm_And, kLong, false);
901 break;
902
903 case Instruction::OR_LONG:
904 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kLong, false);
905 break;
906
907 case Instruction::XOR_LONG:
908 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kLong, false);
909 break;
910
911 case Instruction::SHL_LONG:
Logan Chien5539ad02012-04-02 14:36:55 +0800912 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kLong, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800913 break;
914
915 case Instruction::SHR_LONG:
Logan Chien5539ad02012-04-02 14:36:55 +0800916 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kLong, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800917 break;
918
919 case Instruction::USHR_LONG:
Logan Chien5539ad02012-04-02 14:36:55 +0800920 EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kLong, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800921 break;
922
923 case Instruction::ADD_FLOAT:
924 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kFloat, false);
925 break;
926
927 case Instruction::SUB_FLOAT:
928 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kFloat, false);
929 break;
930
931 case Instruction::MUL_FLOAT:
932 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kFloat, false);
933 break;
934
935 case Instruction::DIV_FLOAT:
936 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kFloat, false);
937 break;
938
939 case Instruction::REM_FLOAT:
940 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kFloat, false);
941 break;
942
943 case Instruction::ADD_DOUBLE:
944 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kDouble, false);
945 break;
946
947 case Instruction::SUB_DOUBLE:
948 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kDouble, false);
949 break;
950
951 case Instruction::MUL_DOUBLE:
952 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kDouble, false);
953 break;
954
955 case Instruction::DIV_DOUBLE:
956 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kDouble, false);
957 break;
958
959 case Instruction::REM_DOUBLE:
960 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kDouble, false);
961 break;
962
963 case Instruction::ADD_INT_2ADDR:
964 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kInt, true);
965 break;
966
967 case Instruction::SUB_INT_2ADDR:
968 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kInt, true);
969 break;
970
971 case Instruction::MUL_INT_2ADDR:
972 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kInt, true);
973 break;
974
975 case Instruction::DIV_INT_2ADDR:
976 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kInt, true);
977 break;
978
979 case Instruction::REM_INT_2ADDR:
980 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kInt, true);
981 break;
982
983 case Instruction::AND_INT_2ADDR:
984 EmitInsn_IntArithm(ARGS, kIntArithm_And, kInt, true);
985 break;
986
987 case Instruction::OR_INT_2ADDR:
988 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kInt, true);
989 break;
990
991 case Instruction::XOR_INT_2ADDR:
992 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kInt, true);
993 break;
994
995 case Instruction::SHL_INT_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +0800996 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kInt, true);
Logan Chien70f94b42011-12-27 17:49:11 +0800997 break;
998
999 case Instruction::SHR_INT_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001000 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kInt, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001001 break;
1002
1003 case Instruction::USHR_INT_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001004 EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kInt, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001005 break;
1006
1007 case Instruction::ADD_LONG_2ADDR:
1008 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kLong, true);
1009 break;
1010
1011 case Instruction::SUB_LONG_2ADDR:
1012 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kLong, true);
1013 break;
1014
1015 case Instruction::MUL_LONG_2ADDR:
1016 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kLong, true);
1017 break;
1018
1019 case Instruction::DIV_LONG_2ADDR:
1020 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kLong, true);
1021 break;
1022
1023 case Instruction::REM_LONG_2ADDR:
1024 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kLong, true);
1025 break;
1026
1027 case Instruction::AND_LONG_2ADDR:
1028 EmitInsn_IntArithm(ARGS, kIntArithm_And, kLong, true);
1029 break;
1030
1031 case Instruction::OR_LONG_2ADDR:
1032 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kLong, true);
1033 break;
1034
1035 case Instruction::XOR_LONG_2ADDR:
1036 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kLong, true);
1037 break;
1038
1039 case Instruction::SHL_LONG_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001040 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kLong, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001041 break;
1042
1043 case Instruction::SHR_LONG_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001044 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kLong, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001045 break;
1046
1047 case Instruction::USHR_LONG_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001048 EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kLong, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001049 break;
1050
1051 case Instruction::ADD_FLOAT_2ADDR:
1052 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kFloat, true);
1053 break;
1054
1055 case Instruction::SUB_FLOAT_2ADDR:
1056 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kFloat, true);
1057 break;
1058
1059 case Instruction::MUL_FLOAT_2ADDR:
1060 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kFloat, true);
1061 break;
1062
1063 case Instruction::DIV_FLOAT_2ADDR:
1064 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kFloat, true);
1065 break;
1066
1067 case Instruction::REM_FLOAT_2ADDR:
1068 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kFloat, true);
1069 break;
1070
1071 case Instruction::ADD_DOUBLE_2ADDR:
1072 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kDouble, true);
1073 break;
1074
1075 case Instruction::SUB_DOUBLE_2ADDR:
1076 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kDouble, true);
1077 break;
1078
1079 case Instruction::MUL_DOUBLE_2ADDR:
1080 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kDouble, true);
1081 break;
1082
1083 case Instruction::DIV_DOUBLE_2ADDR:
1084 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kDouble, true);
1085 break;
1086
1087 case Instruction::REM_DOUBLE_2ADDR:
1088 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kDouble, true);
1089 break;
1090
1091 case Instruction::ADD_INT_LIT16:
1092 case Instruction::ADD_INT_LIT8:
1093 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Add);
1094 break;
1095
1096 case Instruction::RSUB_INT:
1097 case Instruction::RSUB_INT_LIT8:
1098 EmitInsn_RSubImmediate(ARGS);
1099 break;
1100
1101 case Instruction::MUL_INT_LIT16:
1102 case Instruction::MUL_INT_LIT8:
1103 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Mul);
1104 break;
1105
1106 case Instruction::DIV_INT_LIT16:
1107 case Instruction::DIV_INT_LIT8:
1108 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Div);
1109 break;
1110
1111 case Instruction::REM_INT_LIT16:
1112 case Instruction::REM_INT_LIT8:
1113 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Rem);
1114 break;
1115
1116 case Instruction::AND_INT_LIT16:
1117 case Instruction::AND_INT_LIT8:
1118 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_And);
1119 break;
1120
1121 case Instruction::OR_INT_LIT16:
1122 case Instruction::OR_INT_LIT8:
1123 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Or);
1124 break;
1125
1126 case Instruction::XOR_INT_LIT16:
1127 case Instruction::XOR_INT_LIT8:
1128 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Xor);
1129 break;
1130
1131 case Instruction::SHL_INT_LIT8:
Logan Chien5539ad02012-04-02 14:36:55 +08001132 EmitInsn_IntShiftArithmImmediate(ARGS, kIntArithm_Shl);
Logan Chien70f94b42011-12-27 17:49:11 +08001133 break;
1134
1135 case Instruction::SHR_INT_LIT8:
Logan Chien5539ad02012-04-02 14:36:55 +08001136 EmitInsn_IntShiftArithmImmediate(ARGS, kIntArithm_Shr);
Logan Chien70f94b42011-12-27 17:49:11 +08001137 break;
1138
1139 case Instruction::USHR_INT_LIT8:
Logan Chien5539ad02012-04-02 14:36:55 +08001140 EmitInsn_IntShiftArithmImmediate(ARGS, kIntArithm_UShr);
Logan Chien70f94b42011-12-27 17:49:11 +08001141 break;
1142
Logan Chien9e5f5c12012-04-10 13:51:45 +08001143 case Instruction::THROW_VERIFICATION_ERROR:
1144 EmitInsn_ThrowVerificationError(ARGS);
1145 break;
1146
Logan Chien70f94b42011-12-27 17:49:11 +08001147 case Instruction::UNUSED_3E:
1148 case Instruction::UNUSED_3F:
1149 case Instruction::UNUSED_40:
1150 case Instruction::UNUSED_41:
1151 case Instruction::UNUSED_42:
1152 case Instruction::UNUSED_43:
1153 case Instruction::UNUSED_73:
1154 case Instruction::UNUSED_79:
1155 case Instruction::UNUSED_7A:
1156 case Instruction::UNUSED_E3:
1157 case Instruction::UNUSED_E4:
1158 case Instruction::UNUSED_E5:
1159 case Instruction::UNUSED_E6:
1160 case Instruction::UNUSED_E7:
1161 case Instruction::UNUSED_E8:
1162 case Instruction::UNUSED_E9:
1163 case Instruction::UNUSED_EA:
1164 case Instruction::UNUSED_EB:
1165 case Instruction::UNUSED_EC:
Logan Chien70f94b42011-12-27 17:49:11 +08001166 case Instruction::UNUSED_EE:
1167 case Instruction::UNUSED_EF:
1168 case Instruction::UNUSED_F0:
1169 case Instruction::UNUSED_F1:
1170 case Instruction::UNUSED_F2:
1171 case Instruction::UNUSED_F3:
1172 case Instruction::UNUSED_F4:
1173 case Instruction::UNUSED_F5:
1174 case Instruction::UNUSED_F6:
1175 case Instruction::UNUSED_F7:
1176 case Instruction::UNUSED_F8:
1177 case Instruction::UNUSED_F9:
1178 case Instruction::UNUSED_FA:
1179 case Instruction::UNUSED_FB:
1180 case Instruction::UNUSED_FC:
1181 case Instruction::UNUSED_FD:
1182 case Instruction::UNUSED_FE:
1183 case Instruction::UNUSED_FF:
1184 LOG(FATAL) << "Dex file contains UNUSED bytecode: " << insn->Opcode();
1185 break;
1186 }
1187
1188#undef ARGS
1189}
1190
1191
1192void MethodCompiler::EmitInsn_Nop(uint32_t dex_pc,
1193 Instruction const* insn) {
Logan Chiene09a6b72011-12-27 17:50:21 +08001194
1195 uint16_t insn_signature = code_item_->insns_[dex_pc];
1196
1197 if (insn_signature == Instruction::kPackedSwitchSignature ||
1198 insn_signature == Instruction::kSparseSwitchSignature ||
1199 insn_signature == Instruction::kArrayDataSignature) {
1200 irb_.CreateUnreachable();
Elliott Hughesb25c3f62012-03-26 16:35:06 -07001201 } else {
Logan Chiene09a6b72011-12-27 17:50:21 +08001202 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1203 }
Shih-wei Liaod1fec812012-02-13 09:51:10 -08001204}
1205
1206
Logan Chien70f94b42011-12-27 17:49:11 +08001207void MethodCompiler::EmitInsn_Move(uint32_t dex_pc,
1208 Instruction const* insn,
1209 JType jty) {
Logan Chien48173132011-12-27 17:51:13 +08001210
Elliott Hughesadb8c672012-03-06 16:49:32 -08001211 DecodedInstruction dec_insn(insn);
Logan Chien48173132011-12-27 17:51:13 +08001212
Elliott Hughesadb8c672012-03-06 16:49:32 -08001213 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, jty, kReg);
1214 EmitStoreDalvikReg(dec_insn.vA, jty, kReg, src_value);
Logan Chien48173132011-12-27 17:51:13 +08001215
Logan Chien70f94b42011-12-27 17:49:11 +08001216 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1217}
1218
1219
1220void MethodCompiler::EmitInsn_MoveResult(uint32_t dex_pc,
1221 Instruction const* insn,
1222 JType jty) {
Logan Chien48173132011-12-27 17:51:13 +08001223
Elliott Hughesadb8c672012-03-06 16:49:32 -08001224 DecodedInstruction dec_insn(insn);
Logan Chien48173132011-12-27 17:51:13 +08001225
1226 llvm::Value* src_value = EmitLoadDalvikRetValReg(jty, kReg);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001227 EmitStoreDalvikReg(dec_insn.vA, jty, kReg, src_value);
Logan Chien48173132011-12-27 17:51:13 +08001228
Logan Chien70f94b42011-12-27 17:49:11 +08001229 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1230}
1231
1232
1233void MethodCompiler::EmitInsn_MoveException(uint32_t dex_pc,
1234 Instruction const* insn) {
Logan Chien3354cec2012-01-13 14:29:03 +08001235
Elliott Hughesadb8c672012-03-06 16:49:32 -08001236 DecodedInstruction dec_insn(insn);
Logan Chien3354cec2012-01-13 14:29:03 +08001237
1238 // Get thread-local exception field address
1239 llvm::Constant* exception_field_offset =
1240 irb_.getPtrEquivInt(Thread::ExceptionOffset().Int32Value());
1241
1242 llvm::Value* thread_object_addr =
1243 irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1244
1245 llvm::Value* exception_field_addr =
1246 irb_.CreatePtrDisp(thread_object_addr, exception_field_offset,
1247 irb_.getJObjectTy()->getPointerTo());
1248
1249 // Get exception object address
1250 llvm::Value* exception_object_addr = irb_.CreateLoad(exception_field_addr);
1251
1252 // Set thread-local exception field address to NULL
1253 irb_.CreateStore(irb_.getJNull(), exception_field_addr);
1254
1255 // Keep the exception object in the Dalvik register
Elliott Hughesadb8c672012-03-06 16:49:32 -08001256 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, exception_object_addr);
Logan Chien3354cec2012-01-13 14:29:03 +08001257
Logan Chien70f94b42011-12-27 17:49:11 +08001258 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1259}
1260
1261
1262void MethodCompiler::EmitInsn_ThrowException(uint32_t dex_pc,
1263 Instruction const* insn) {
Logan Chien6c6f12d2012-01-13 19:26:27 +08001264
Elliott Hughesadb8c672012-03-06 16:49:32 -08001265 DecodedInstruction dec_insn(insn);
Logan Chien6c6f12d2012-01-13 19:26:27 +08001266
1267 llvm::Value* exception_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001268 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chien6c6f12d2012-01-13 19:26:27 +08001269
TDYa127c8dc1012012-04-19 07:03:33 -07001270 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001271
Logan Chien6c6f12d2012-01-13 19:26:27 +08001272 irb_.CreateCall(irb_.GetRuntime(ThrowException), exception_addr);
1273
1274 EmitBranchExceptionLandingPad(dex_pc);
Logan Chien70f94b42011-12-27 17:49:11 +08001275}
1276
1277
Logan Chien9e5f5c12012-04-10 13:51:45 +08001278void MethodCompiler::EmitInsn_ThrowVerificationError(uint32_t dex_pc,
1279 Instruction const* insn) {
1280
1281 DecodedInstruction dec_insn(insn);
1282
TDYa127c8dc1012012-04-19 07:03:33 -07001283 EmitUpdateDexPC(dex_pc);
Logan Chien9e5f5c12012-04-10 13:51:45 +08001284
1285 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1286 llvm::Value* kind_value = irb_.getInt32(dec_insn.vA);
1287 llvm::Value* ref_value = irb_.getInt32(dec_insn.vB);
1288
1289 irb_.CreateCall3(irb_.GetRuntime(ThrowVerificationError),
1290 method_object_addr, kind_value, ref_value);
1291
1292 EmitBranchExceptionLandingPad(dex_pc);
1293}
1294
1295
Logan Chien70f94b42011-12-27 17:49:11 +08001296void MethodCompiler::EmitInsn_ReturnVoid(uint32_t dex_pc,
1297 Instruction const* insn) {
Logan Chien8898a272011-12-27 17:51:56 +08001298 // Garbage collection safe-point
1299 EmitGuard_GarbageCollectionSuspend(dex_pc);
1300
Logan Chien8dfcbea2012-02-17 18:50:32 +08001301 // Pop the shadow frame
1302 EmitPopShadowFrame();
1303
Logan Chien8898a272011-12-27 17:51:56 +08001304 // Return!
1305 irb_.CreateRetVoid();
Logan Chien70f94b42011-12-27 17:49:11 +08001306}
1307
1308
1309void MethodCompiler::EmitInsn_Return(uint32_t dex_pc,
1310 Instruction const* insn) {
Logan Chien8898a272011-12-27 17:51:56 +08001311
Elliott Hughesadb8c672012-03-06 16:49:32 -08001312 DecodedInstruction dec_insn(insn);
Logan Chien8898a272011-12-27 17:51:56 +08001313
1314 // Garbage collection safe-point
1315 EmitGuard_GarbageCollectionSuspend(dex_pc);
1316
Logan Chien8dfcbea2012-02-17 18:50:32 +08001317 // Pop the shadow frame
1318 EmitPopShadowFrame();
1319 // NOTE: It is important to keep this AFTER the GC safe-point. Otherwise,
1320 // the return value might be collected since the shadow stack is popped.
1321
Logan Chien8898a272011-12-27 17:51:56 +08001322 // Return!
Logan Chiendd361c92012-04-10 23:40:37 +08001323 char ret_shorty = oat_compilation_unit_->GetShorty()[0];
Elliott Hughesadb8c672012-03-06 16:49:32 -08001324 llvm::Value* retval = EmitLoadDalvikReg(dec_insn.vA, ret_shorty, kAccurate);
Logan Chien8898a272011-12-27 17:51:56 +08001325
1326 irb_.CreateRet(retval);
Logan Chien70f94b42011-12-27 17:49:11 +08001327}
1328
1329
1330void MethodCompiler::EmitInsn_LoadConstant(uint32_t dex_pc,
1331 Instruction const* insn,
1332 JType imm_jty) {
Shih-wei Liao798366e2012-02-16 09:25:33 -08001333
Elliott Hughesadb8c672012-03-06 16:49:32 -08001334 DecodedInstruction dec_insn(insn);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001335
1336 DCHECK(imm_jty == kInt || imm_jty == kLong) << imm_jty;
1337
1338 int64_t imm = 0;
1339
1340 switch (insn->Opcode()) {
1341 // 32-bit Immediate
1342 case Instruction::CONST_4:
1343 case Instruction::CONST_16:
1344 case Instruction::CONST:
1345 case Instruction::CONST_WIDE_16:
1346 case Instruction::CONST_WIDE_32:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001347 imm = static_cast<int64_t>(static_cast<int32_t>(dec_insn.vB));
Shih-wei Liao798366e2012-02-16 09:25:33 -08001348 break;
1349
1350 case Instruction::CONST_HIGH16:
1351 imm = static_cast<int64_t>(static_cast<int32_t>(
Elliott Hughesadb8c672012-03-06 16:49:32 -08001352 static_cast<uint32_t>(static_cast<uint16_t>(dec_insn.vB)) << 16));
Shih-wei Liao798366e2012-02-16 09:25:33 -08001353 break;
1354
1355 // 64-bit Immediate
1356 case Instruction::CONST_WIDE:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001357 imm = static_cast<int64_t>(dec_insn.vB_wide);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001358 break;
1359
1360 case Instruction::CONST_WIDE_HIGH16:
1361 imm = static_cast<int64_t>(
Elliott Hughesadb8c672012-03-06 16:49:32 -08001362 static_cast<uint64_t>(static_cast<uint16_t>(dec_insn.vB)) << 48);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001363 break;
1364
1365 // Unknown opcode for load constant (unreachable)
1366 default:
1367 LOG(FATAL) << "Unknown opcode for load constant: " << insn->Opcode();
1368 break;
1369 }
1370
1371 // Store the non-object register
1372 llvm::Type* imm_type = irb_.getJType(imm_jty, kAccurate);
1373 llvm::Constant* imm_value = llvm::ConstantInt::getSigned(imm_type, imm);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001374 EmitStoreDalvikReg(dec_insn.vA, imm_jty, kAccurate, imm_value);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001375
1376 // Store the object register if it is possible to be null.
1377 if (imm_jty == kInt && imm == 0) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001378 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, irb_.getJNull());
Shih-wei Liao798366e2012-02-16 09:25:33 -08001379 }
1380
Logan Chien70f94b42011-12-27 17:49:11 +08001381 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1382}
1383
1384
1385void MethodCompiler::EmitInsn_LoadConstantString(uint32_t dex_pc,
1386 Instruction const* insn) {
Logan Chienc3b4ba12012-01-16 19:52:53 +08001387
Elliott Hughesadb8c672012-03-06 16:49:32 -08001388 DecodedInstruction dec_insn(insn);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001389
Elliott Hughesadb8c672012-03-06 16:49:32 -08001390 uint32_t string_idx = dec_insn.vB;
Logan Chienc3b4ba12012-01-16 19:52:53 +08001391
1392 llvm::Value* string_field_addr = EmitLoadDexCacheStringFieldAddr(string_idx);
1393
1394 llvm::Value* string_addr = irb_.CreateLoad(string_field_addr);
1395
1396 if (!compiler_->CanAssumeStringIsPresentInDexCache(dex_cache_, string_idx)) {
1397 llvm::BasicBlock* block_str_exist =
1398 CreateBasicBlockWithDexPC(dex_pc, "str_exist");
1399
1400 llvm::BasicBlock* block_str_resolve =
1401 CreateBasicBlockWithDexPC(dex_pc, "str_resolve");
1402
1403 // Test: Is the string resolved and in the dex cache?
1404 llvm::Value* equal_null = irb_.CreateICmpEQ(string_addr, irb_.getJNull());
1405
TDYa127a849cb62012-04-01 05:59:34 -07001406 irb_.CreateCondBr(equal_null, block_str_resolve, block_str_exist);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001407
1408 // String is resolved, go to next basic block.
1409 irb_.SetInsertPoint(block_str_exist);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001410 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, string_addr);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001411 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1412
1413 // String is not resolved yet, resolve it now.
1414 irb_.SetInsertPoint(block_str_resolve);
1415
1416 llvm::Function* runtime_func = irb_.GetRuntime(ResolveString);
1417
1418 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1419
1420 llvm::Value* string_idx_value = irb_.getInt32(string_idx);
1421
TDYa127c8dc1012012-04-19 07:03:33 -07001422 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001423
1424 string_addr = irb_.CreateCall2(runtime_func, method_object_addr,
1425 string_idx_value);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001426
1427 EmitGuard_ExceptionLandingPad(dex_pc);
1428 }
1429
1430 // Store the string object to the Dalvik register
Elliott Hughesadb8c672012-03-06 16:49:32 -08001431 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, string_addr);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001432
Logan Chien70f94b42011-12-27 17:49:11 +08001433 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1434}
1435
1436
Logan Chien27b30252012-01-14 03:43:35 +08001437llvm::Value* MethodCompiler::EmitLoadConstantClass(uint32_t dex_pc,
1438 uint32_t type_idx) {
1439 if (!compiler_->CanAccessTypeWithoutChecks(method_idx_, dex_cache_,
1440 *dex_file_, type_idx)) {
1441 llvm::Value* type_idx_value = irb_.getInt32(type_idx);
1442
1443 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1444
TDYa127706e9b62012-04-19 12:24:26 -07001445 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1446
Logan Chien27b30252012-01-14 03:43:35 +08001447 llvm::Function* runtime_func =
1448 irb_.GetRuntime(InitializeTypeAndVerifyAccess);
1449
TDYa127c8dc1012012-04-19 07:03:33 -07001450 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001451
Logan Chien27b30252012-01-14 03:43:35 +08001452 llvm::Value* type_object_addr =
TDYa127706e9b62012-04-19 12:24:26 -07001453 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
Logan Chien27b30252012-01-14 03:43:35 +08001454
1455 EmitGuard_ExceptionLandingPad(dex_pc);
1456
1457 return type_object_addr;
1458
1459 } else {
1460 // Try to load the class (type) object from the test cache.
1461 llvm::Value* type_field_addr =
1462 EmitLoadDexCacheResolvedTypeFieldAddr(type_idx);
1463
1464 llvm::Value* type_object_addr = irb_.CreateLoad(type_field_addr);
1465
1466 if (compiler_->CanAssumeTypeIsPresentInDexCache(dex_cache_, type_idx)) {
1467 return type_object_addr;
1468 }
1469
1470 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
1471
1472 // Test whether class (type) object is in the dex cache or not
1473 llvm::Value* equal_null =
1474 irb_.CreateICmpEQ(type_object_addr, irb_.getJNull());
1475
1476 llvm::BasicBlock* block_cont =
1477 CreateBasicBlockWithDexPC(dex_pc, "cont");
1478
1479 llvm::BasicBlock* block_load_class =
1480 CreateBasicBlockWithDexPC(dex_pc, "load_class");
1481
1482 irb_.CreateCondBr(equal_null, block_load_class, block_cont);
1483
1484 // Failback routine to load the class object
1485 irb_.SetInsertPoint(block_load_class);
1486
1487 llvm::Function* runtime_func = irb_.GetRuntime(InitializeType);
1488
1489 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
1490
1491 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1492
TDYa127706e9b62012-04-19 12:24:26 -07001493 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1494
TDYa127c8dc1012012-04-19 07:03:33 -07001495 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001496
Logan Chien27b30252012-01-14 03:43:35 +08001497 llvm::Value* loaded_type_object_addr =
TDYa127706e9b62012-04-19 12:24:26 -07001498 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
Logan Chien27b30252012-01-14 03:43:35 +08001499
1500 EmitGuard_ExceptionLandingPad(dex_pc);
1501
1502 llvm::BasicBlock* block_after_load_class = irb_.GetInsertBlock();
1503
1504 irb_.CreateBr(block_cont);
1505
1506 // Now the class object must be loaded
1507 irb_.SetInsertPoint(block_cont);
1508
1509 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1510
1511 phi->addIncoming(type_object_addr, block_original);
1512 phi->addIncoming(loaded_type_object_addr, block_after_load_class);
1513
1514 return phi;
1515 }
1516}
1517
1518
Logan Chien70f94b42011-12-27 17:49:11 +08001519void MethodCompiler::EmitInsn_LoadConstantClass(uint32_t dex_pc,
1520 Instruction const* insn) {
Logan Chien27b30252012-01-14 03:43:35 +08001521
Elliott Hughesadb8c672012-03-06 16:49:32 -08001522 DecodedInstruction dec_insn(insn);
Logan Chien27b30252012-01-14 03:43:35 +08001523
Elliott Hughesadb8c672012-03-06 16:49:32 -08001524 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, dec_insn.vB);
1525 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, type_object_addr);
Logan Chien27b30252012-01-14 03:43:35 +08001526
Logan Chien70f94b42011-12-27 17:49:11 +08001527 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1528}
1529
1530
1531void MethodCompiler::EmitInsn_MonitorEnter(uint32_t dex_pc,
1532 Instruction const* insn) {
Logan Chien9e0dbe42012-01-13 12:11:37 +08001533
Elliott Hughesadb8c672012-03-06 16:49:32 -08001534 DecodedInstruction dec_insn(insn);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001535
1536 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001537 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001538
1539 // TODO: Slow path always. May not need NullPointerException check.
1540 EmitGuard_NullPointerException(dex_pc, object_addr);
1541
TDYa127c8dc1012012-04-19 07:03:33 -07001542 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001543
TDYa127706e9b62012-04-19 12:24:26 -07001544 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1545
1546 irb_.CreateCall2(irb_.GetRuntime(LockObject), object_addr, thread_object_addr);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001547 EmitGuard_ExceptionLandingPad(dex_pc);
1548
Logan Chien70f94b42011-12-27 17:49:11 +08001549 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1550}
1551
1552
1553void MethodCompiler::EmitInsn_MonitorExit(uint32_t dex_pc,
1554 Instruction const* insn) {
Logan Chien9e0dbe42012-01-13 12:11:37 +08001555
Elliott Hughesadb8c672012-03-06 16:49:32 -08001556 DecodedInstruction dec_insn(insn);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001557
1558 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001559 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001560
1561 EmitGuard_NullPointerException(dex_pc, object_addr);
1562
TDYa127c8dc1012012-04-19 07:03:33 -07001563 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001564
TDYa127706e9b62012-04-19 12:24:26 -07001565 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1566
1567 irb_.CreateCall2(irb_.GetRuntime(UnlockObject), object_addr, thread_object_addr);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001568 EmitGuard_ExceptionLandingPad(dex_pc);
1569
Logan Chien70f94b42011-12-27 17:49:11 +08001570 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1571}
1572
1573
1574void MethodCompiler::EmitInsn_CheckCast(uint32_t dex_pc,
1575 Instruction const* insn) {
Logan Chienfc880952012-01-15 23:53:10 +08001576
Elliott Hughesadb8c672012-03-06 16:49:32 -08001577 DecodedInstruction dec_insn(insn);
Logan Chienfc880952012-01-15 23:53:10 +08001578
1579 llvm::BasicBlock* block_test_class =
1580 CreateBasicBlockWithDexPC(dex_pc, "test_class");
1581
1582 llvm::BasicBlock* block_test_sub_class =
1583 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
1584
1585 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001586 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chienfc880952012-01-15 23:53:10 +08001587
1588 // Test: Is the reference equal to null? Act as no-op when it is null.
1589 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
1590
1591 irb_.CreateCondBr(equal_null,
1592 GetNextBasicBlock(dex_pc),
1593 block_test_class);
1594
1595 // Test: Is the object instantiated from the given class?
1596 irb_.SetInsertPoint(block_test_class);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001597 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, dec_insn.vB);
Logan Chienfc880952012-01-15 23:53:10 +08001598 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1599
1600 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
1601
1602 llvm::Value* object_type_field_addr =
1603 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
1604
1605 llvm::Value* object_type_object_addr =
1606 irb_.CreateLoad(object_type_field_addr);
1607
1608 llvm::Value* equal_class =
1609 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
1610
1611 irb_.CreateCondBr(equal_class,
1612 GetNextBasicBlock(dex_pc),
1613 block_test_sub_class);
1614
1615 // Test: Is the object instantiated from the subclass of the given class?
1616 irb_.SetInsertPoint(block_test_sub_class);
1617
TDYa127c8dc1012012-04-19 07:03:33 -07001618 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001619
Logan Chienfc880952012-01-15 23:53:10 +08001620 irb_.CreateCall2(irb_.GetRuntime(CheckCast),
1621 type_object_addr, object_type_object_addr);
1622
1623 EmitGuard_ExceptionLandingPad(dex_pc);
1624
Logan Chien70f94b42011-12-27 17:49:11 +08001625 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1626}
1627
1628
1629void MethodCompiler::EmitInsn_InstanceOf(uint32_t dex_pc,
1630 Instruction const* insn) {
Logan Chien68725e22012-01-15 22:25:34 +08001631
Elliott Hughesadb8c672012-03-06 16:49:32 -08001632 DecodedInstruction dec_insn(insn);
Logan Chien68725e22012-01-15 22:25:34 +08001633
1634 llvm::Constant* zero = irb_.getJInt(0);
1635 llvm::Constant* one = irb_.getJInt(1);
1636
1637 llvm::BasicBlock* block_nullp = CreateBasicBlockWithDexPC(dex_pc, "nullp");
1638
1639 llvm::BasicBlock* block_test_class =
1640 CreateBasicBlockWithDexPC(dex_pc, "test_class");
1641
1642 llvm::BasicBlock* block_class_equals =
1643 CreateBasicBlockWithDexPC(dex_pc, "class_eq");
1644
1645 llvm::BasicBlock* block_test_sub_class =
1646 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
1647
1648 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001649 EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
Logan Chien68725e22012-01-15 22:25:34 +08001650
1651 // Overview of the following code :
1652 // We check for null, if so, then false, otherwise check for class == . If so
1653 // then true, otherwise do callout slowpath.
1654 //
1655 // Test: Is the reference equal to null? Set 0 when it is null.
1656 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
1657
1658 irb_.CreateCondBr(equal_null, block_nullp, block_test_class);
1659
1660 irb_.SetInsertPoint(block_nullp);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001661 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, zero);
Logan Chien68725e22012-01-15 22:25:34 +08001662 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1663
1664 // Test: Is the object instantiated from the given class?
1665 irb_.SetInsertPoint(block_test_class);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001666 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, dec_insn.vC);
Logan Chien68725e22012-01-15 22:25:34 +08001667 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1668
1669 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
1670
1671 llvm::Value* object_type_field_addr =
1672 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
1673
1674 llvm::Value* object_type_object_addr =
1675 irb_.CreateLoad(object_type_field_addr);
1676
1677 llvm::Value* equal_class =
1678 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
1679
1680 irb_.CreateCondBr(equal_class, block_class_equals, block_test_sub_class);
1681
1682 irb_.SetInsertPoint(block_class_equals);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001683 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, one);
Logan Chien68725e22012-01-15 22:25:34 +08001684 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1685
1686 // Test: Is the object instantiated from the subclass of the given class?
1687 irb_.SetInsertPoint(block_test_sub_class);
1688
1689 llvm::Value* result =
1690 irb_.CreateCall2(irb_.GetRuntime(IsAssignable),
1691 type_object_addr, object_type_object_addr);
1692
Elliott Hughesadb8c672012-03-06 16:49:32 -08001693 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result);
Logan Chien68725e22012-01-15 22:25:34 +08001694
Logan Chien70f94b42011-12-27 17:49:11 +08001695 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1696}
1697
1698
Logan Chien61bb6142012-02-03 15:34:53 +08001699llvm::Value* MethodCompiler::EmitLoadArrayLength(llvm::Value* array) {
1700 // Load array length field address
1701 llvm::Constant* array_len_field_offset =
1702 irb_.getPtrEquivInt(Array::LengthOffset().Int32Value());
1703
1704 llvm::Value* array_len_field_addr =
1705 irb_.CreatePtrDisp(array, array_len_field_offset,
1706 irb_.getJIntTy()->getPointerTo());
1707
1708 // Load array length
1709 return irb_.CreateLoad(array_len_field_addr);
1710}
1711
1712
Logan Chien70f94b42011-12-27 17:49:11 +08001713void MethodCompiler::EmitInsn_ArrayLength(uint32_t dex_pc,
1714 Instruction const* insn) {
Logan Chien61bb6142012-02-03 15:34:53 +08001715
Elliott Hughesadb8c672012-03-06 16:49:32 -08001716 DecodedInstruction dec_insn(insn);
Logan Chien61bb6142012-02-03 15:34:53 +08001717
1718 // Get the array object address
Elliott Hughesadb8c672012-03-06 16:49:32 -08001719 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
Logan Chien61bb6142012-02-03 15:34:53 +08001720 EmitGuard_NullPointerException(dex_pc, array_addr);
1721
1722 // Get the array length and store it to the register
1723 llvm::Value* array_len = EmitLoadArrayLength(array_addr);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001724 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, array_len);
Logan Chien61bb6142012-02-03 15:34:53 +08001725
Logan Chien70f94b42011-12-27 17:49:11 +08001726 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1727}
1728
1729
1730void MethodCompiler::EmitInsn_NewInstance(uint32_t dex_pc,
1731 Instruction const* insn) {
Logan Chien032bdad2012-01-16 09:59:23 +08001732
Elliott Hughesadb8c672012-03-06 16:49:32 -08001733 DecodedInstruction dec_insn(insn);
Logan Chien032bdad2012-01-16 09:59:23 +08001734
1735 llvm::Function* runtime_func;
Logan Chien1a032b12012-04-11 11:43:04 +08001736 if (compiler_->CanAccessInstantiableTypeWithoutChecks(
1737 method_idx_, dex_cache_, *dex_file_, dec_insn.vB)) {
Logan Chien032bdad2012-01-16 09:59:23 +08001738 runtime_func = irb_.GetRuntime(AllocObject);
1739 } else {
1740 runtime_func = irb_.GetRuntime(AllocObjectWithAccessCheck);
1741 }
1742
Elliott Hughesadb8c672012-03-06 16:49:32 -08001743 llvm::Constant* type_index_value = irb_.getInt32(dec_insn.vB);
Logan Chien032bdad2012-01-16 09:59:23 +08001744
1745 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1746
TDYa127da83d972012-04-18 00:21:49 -07001747 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1748
TDYa127c8dc1012012-04-19 07:03:33 -07001749 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001750
Logan Chien032bdad2012-01-16 09:59:23 +08001751 llvm::Value* object_addr =
TDYa127da83d972012-04-18 00:21:49 -07001752 irb_.CreateCall3(runtime_func, type_index_value, method_object_addr, thread_object_addr);
Logan Chien032bdad2012-01-16 09:59:23 +08001753
1754 EmitGuard_ExceptionLandingPad(dex_pc);
1755
Elliott Hughesadb8c672012-03-06 16:49:32 -08001756 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, object_addr);
Logan Chien032bdad2012-01-16 09:59:23 +08001757
Logan Chien70f94b42011-12-27 17:49:11 +08001758 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1759}
1760
1761
Logan Chiena2cc6a32012-01-16 10:38:41 +08001762llvm::Value* MethodCompiler::EmitAllocNewArray(uint32_t dex_pc,
1763 int32_t length,
1764 uint32_t type_idx,
1765 bool is_filled_new_array) {
1766 llvm::Function* runtime_func;
1767
1768 bool skip_access_check =
1769 compiler_->CanAccessTypeWithoutChecks(method_idx_, dex_cache_,
1770 *dex_file_, type_idx);
1771
TDYa127a849cb62012-04-01 05:59:34 -07001772 llvm::Value* array_length_value;
1773
Logan Chiena2cc6a32012-01-16 10:38:41 +08001774 if (is_filled_new_array) {
1775 runtime_func = skip_access_check ?
1776 irb_.GetRuntime(CheckAndAllocArray) :
1777 irb_.GetRuntime(CheckAndAllocArrayWithAccessCheck);
TDYa127a849cb62012-04-01 05:59:34 -07001778 array_length_value = irb_.getInt32(length);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001779 } else {
1780 runtime_func = skip_access_check ?
1781 irb_.GetRuntime(AllocArray) :
1782 irb_.GetRuntime(AllocArrayWithAccessCheck);
TDYa127a849cb62012-04-01 05:59:34 -07001783 array_length_value = EmitLoadDalvikReg(length, kInt, kAccurate);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001784 }
1785
1786 llvm::Constant* type_index_value = irb_.getInt32(type_idx);
1787
1788 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1789
TDYa127da83d972012-04-18 00:21:49 -07001790 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1791
TDYa127c8dc1012012-04-19 07:03:33 -07001792 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001793
Logan Chiena2cc6a32012-01-16 10:38:41 +08001794 llvm::Value* object_addr =
TDYa127da83d972012-04-18 00:21:49 -07001795 irb_.CreateCall4(runtime_func, type_index_value, method_object_addr,
1796 array_length_value, thread_object_addr);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001797
1798 EmitGuard_ExceptionLandingPad(dex_pc);
1799
1800 return object_addr;
1801}
1802
1803
Logan Chien70f94b42011-12-27 17:49:11 +08001804void MethodCompiler::EmitInsn_NewArray(uint32_t dex_pc,
1805 Instruction const* insn) {
Logan Chiena2cc6a32012-01-16 10:38:41 +08001806
Elliott Hughesadb8c672012-03-06 16:49:32 -08001807 DecodedInstruction dec_insn(insn);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001808
1809 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001810 EmitAllocNewArray(dex_pc, dec_insn.vB, dec_insn.vC, false);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001811
Elliott Hughesadb8c672012-03-06 16:49:32 -08001812 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, object_addr);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001813
Logan Chien70f94b42011-12-27 17:49:11 +08001814 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1815}
1816
1817
1818void MethodCompiler::EmitInsn_FilledNewArray(uint32_t dex_pc,
1819 Instruction const* insn,
1820 bool is_range) {
Logan Chiena85fb2f2012-01-16 12:52:56 +08001821
Elliott Hughesadb8c672012-03-06 16:49:32 -08001822 DecodedInstruction dec_insn(insn);
Logan Chiena85fb2f2012-01-16 12:52:56 +08001823
1824 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001825 EmitAllocNewArray(dex_pc, dec_insn.vA, dec_insn.vB, true);
Logan Chiena85fb2f2012-01-16 12:52:56 +08001826
Elliott Hughesadb8c672012-03-06 16:49:32 -08001827 if (dec_insn.vA > 0) {
Logan Chiendd361c92012-04-10 23:40:37 +08001828 // Resolve the element type
TDYa127183cf262012-04-11 07:53:21 -07001829 Class* klass = dex_cache_->GetResolvedType(dec_insn.vB)->GetComponentType();
Logan Chiendd361c92012-04-10 23:40:37 +08001830 // TODO: Avoid the usage of the dex_cache_. Try to figure out a better
1831 // way to distinguish [I and [L.
Logan Chiena85fb2f2012-01-16 12:52:56 +08001832 CHECK_NE(klass, static_cast<Class*>(NULL));
Logan Chiena85fb2f2012-01-16 12:52:56 +08001833
Logan Chiendd361c92012-04-10 23:40:37 +08001834 uint32_t alignment;
1835 llvm::Constant* elem_size;
1836 llvm::PointerType* field_type;
Logan Chiena85fb2f2012-01-16 12:52:56 +08001837
Logan Chiendd361c92012-04-10 23:40:37 +08001838 // NOTE: Currently filled-new-array only supports 'L', '[', and 'I'
1839 // as the element, thus we are only checking 2 cases: primitive int and
1840 // non-primitive type.
Logan Chiena85fb2f2012-01-16 12:52:56 +08001841 if (klass->IsPrimitiveInt()) {
Logan Chiendd361c92012-04-10 23:40:37 +08001842 alignment = sizeof(int32_t);
1843 elem_size = irb_.getPtrEquivInt(sizeof(int32_t));
Logan Chiena85fb2f2012-01-16 12:52:56 +08001844 field_type = irb_.getJIntTy()->getPointerTo();
1845 } else {
1846 CHECK(!klass->IsPrimitive());
Logan Chiendd361c92012-04-10 23:40:37 +08001847 alignment = irb_.getSizeOfPtrEquivInt();
1848 elem_size = irb_.getSizeOfPtrEquivIntValue();
Logan Chiena85fb2f2012-01-16 12:52:56 +08001849 field_type = irb_.getJObjectTy()->getPointerTo();
1850 }
1851
Logan Chiendd361c92012-04-10 23:40:37 +08001852 llvm::Value* data_field_offset =
1853 irb_.getPtrEquivInt(Array::DataOffset(alignment).Int32Value());
1854
1855 llvm::Value* data_field_addr =
1856 irb_.CreatePtrDisp(object_addr, data_field_offset, field_type);
1857
Logan Chiena85fb2f2012-01-16 12:52:56 +08001858 // TODO: Tune this code. Currently we are generating one instruction for
1859 // one element which may be very space consuming. Maybe changing to use
1860 // memcpy may help; however, since we can't guarantee that the alloca of
1861 // dalvik register are continuous, we can't perform such optimization yet.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001862 for (uint32_t i = 0; i < dec_insn.vA; ++i) {
Logan Chiena85fb2f2012-01-16 12:52:56 +08001863 int reg_index;
1864 if (is_range) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001865 reg_index = dec_insn.vC + i;
Logan Chiena85fb2f2012-01-16 12:52:56 +08001866 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001867 reg_index = dec_insn.arg[i];
Logan Chiena85fb2f2012-01-16 12:52:56 +08001868 }
1869
1870 llvm::Value* reg_value;
1871 if (klass->IsPrimitiveInt()) {
1872 reg_value = EmitLoadDalvikReg(reg_index, kInt, kAccurate);
1873 } else {
1874 reg_value = EmitLoadDalvikReg(reg_index, kObject, kAccurate);
1875 }
1876
1877 irb_.CreateStore(reg_value, data_field_addr);
1878
Logan Chiendd361c92012-04-10 23:40:37 +08001879 data_field_addr =
1880 irb_.CreatePtrDisp(data_field_addr, elem_size, field_type);
Logan Chiena85fb2f2012-01-16 12:52:56 +08001881 }
1882 }
1883
1884 EmitStoreDalvikRetValReg(kObject, kAccurate, object_addr);
1885
Logan Chien70f94b42011-12-27 17:49:11 +08001886 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1887}
1888
1889
1890void MethodCompiler::EmitInsn_FillArrayData(uint32_t dex_pc,
1891 Instruction const* insn) {
Logan Chiene58b6582012-01-16 17:13:13 +08001892
Elliott Hughesadb8c672012-03-06 16:49:32 -08001893 DecodedInstruction dec_insn(insn);
Logan Chiene58b6582012-01-16 17:13:13 +08001894
1895 // Read the payload
1896 struct PACKED Payload {
1897 uint16_t ident_;
1898 uint16_t elem_width_;
1899 uint32_t num_elems_;
1900 uint8_t data_[];
1901 };
1902
1903 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
Elliott Hughesadb8c672012-03-06 16:49:32 -08001904 static_cast<int32_t>(dec_insn.vB);
Logan Chiene58b6582012-01-16 17:13:13 +08001905
1906 Payload const* payload =
1907 reinterpret_cast<Payload const*>(code_item_->insns_ + payload_offset);
1908
1909 uint32_t size_in_bytes = payload->elem_width_ * payload->num_elems_;
1910
1911 // Load and check the array
Elliott Hughesadb8c672012-03-06 16:49:32 -08001912 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiene58b6582012-01-16 17:13:13 +08001913
1914 EmitGuard_NullPointerException(dex_pc, array_addr);
1915
1916 if (payload->num_elems_ > 0) {
1917 // Test: Is array length big enough?
1918 llvm::Constant* last_index = irb_.getJInt(payload->num_elems_ - 1);
1919
1920 EmitGuard_ArrayIndexOutOfBoundsException(dex_pc, array_addr, last_index);
1921
1922 // Get array data field
1923 llvm::Value* data_field_offset_value =
TDYa127b77799d2012-04-06 22:16:31 -07001924 irb_.getPtrEquivInt(Array::DataOffset(payload->elem_width_).Int32Value());
Logan Chiene58b6582012-01-16 17:13:13 +08001925
1926 llvm::Value* data_field_addr =
1927 irb_.CreatePtrDisp(array_addr, data_field_offset_value,
1928 irb_.getInt8Ty()->getPointerTo());
1929
1930 // Emit payload to bitcode constant pool
1931 std::vector<llvm::Constant*> const_pool_data;
1932 for (uint32_t i = 0; i < size_in_bytes; ++i) {
1933 const_pool_data.push_back(irb_.getInt8(payload->data_[i]));
1934 }
1935
1936 llvm::Constant* const_pool_data_array_value = llvm::ConstantArray::get(
1937 llvm::ArrayType::get(irb_.getInt8Ty(), size_in_bytes), const_pool_data);
1938
1939 llvm::Value* const_pool_data_array_addr =
1940 new llvm::GlobalVariable(*module_,
1941 const_pool_data_array_value->getType(),
1942 false, llvm::GlobalVariable::InternalLinkage,
1943 const_pool_data_array_value,
1944 "array_data_payload");
1945
1946 // Find the memcpy intrinsic
1947 llvm::Type* memcpy_arg_types[] = {
1948 llvm::Type::getInt8Ty(*context_)->getPointerTo(),
1949 llvm::Type::getInt8Ty(*context_)->getPointerTo(),
1950 llvm::Type::getInt32Ty(*context_)
1951 };
1952
1953 llvm::Function* memcpy_intrinsic =
1954 llvm::Intrinsic::getDeclaration(module_,
1955 llvm::Intrinsic::memcpy,
1956 memcpy_arg_types);
1957
1958 // Copy now!
1959 llvm::Value *args[] = {
1960 data_field_addr,
1961 irb_.CreateConstGEP2_32(const_pool_data_array_addr, 0, 0),
1962 irb_.getInt32(size_in_bytes),
1963 irb_.getInt32(0), // alignment: no guarantee
1964 irb_.getFalse() // is_volatile: false
1965 };
1966
1967 irb_.CreateCall(memcpy_intrinsic, args);
1968 }
1969
Logan Chien70f94b42011-12-27 17:49:11 +08001970 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1971}
1972
1973
1974void MethodCompiler::EmitInsn_UnconditionalBranch(uint32_t dex_pc,
1975 Instruction const* insn) {
Logan Chiena466c162011-12-27 17:55:46 +08001976
Elliott Hughesadb8c672012-03-06 16:49:32 -08001977 DecodedInstruction dec_insn(insn);
Logan Chiena466c162011-12-27 17:55:46 +08001978
Elliott Hughesadb8c672012-03-06 16:49:32 -08001979 int32_t branch_offset = dec_insn.vA;
Logan Chiena466c162011-12-27 17:55:46 +08001980
1981 if (branch_offset <= 0) {
1982 // Garbage collection safe-point on backward branch
1983 EmitGuard_GarbageCollectionSuspend(dex_pc);
1984 }
1985
1986 irb_.CreateBr(GetBasicBlock(dex_pc + branch_offset));
Logan Chien70f94b42011-12-27 17:49:11 +08001987}
1988
1989
1990void MethodCompiler::EmitInsn_PackedSwitch(uint32_t dex_pc,
1991 Instruction const* insn) {
Logan Chien7a89b6d2011-12-27 17:56:56 +08001992
Elliott Hughesadb8c672012-03-06 16:49:32 -08001993 DecodedInstruction dec_insn(insn);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001994
1995 struct PACKED Payload {
1996 uint16_t ident_;
1997 uint16_t num_cases_;
1998 int32_t first_key_;
1999 int32_t targets_[];
2000 };
2001
2002 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
Elliott Hughesadb8c672012-03-06 16:49:32 -08002003 static_cast<int32_t>(dec_insn.vB);
Logan Chien7a89b6d2011-12-27 17:56:56 +08002004
2005 Payload const* payload =
2006 reinterpret_cast<Payload const*>(code_item_->insns_ + payload_offset);
2007
Elliott Hughesadb8c672012-03-06 16:49:32 -08002008 llvm::Value* value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chien7a89b6d2011-12-27 17:56:56 +08002009
2010 llvm::SwitchInst* sw =
2011 irb_.CreateSwitch(value, GetNextBasicBlock(dex_pc), payload->num_cases_);
2012
2013 for (uint16_t i = 0; i < payload->num_cases_; ++i) {
2014 sw->addCase(irb_.getInt32(payload->first_key_ + i),
2015 GetBasicBlock(dex_pc + payload->targets_[i]));
2016 }
Logan Chien70f94b42011-12-27 17:49:11 +08002017}
2018
2019
2020void MethodCompiler::EmitInsn_SparseSwitch(uint32_t dex_pc,
2021 Instruction const* insn) {
Logan Chien7a89b6d2011-12-27 17:56:56 +08002022
Elliott Hughesadb8c672012-03-06 16:49:32 -08002023 DecodedInstruction dec_insn(insn);
Logan Chien7a89b6d2011-12-27 17:56:56 +08002024
2025 struct PACKED Payload {
2026 uint16_t ident_;
2027 uint16_t num_cases_;
2028 int32_t keys_and_targets_[];
2029 };
2030
2031 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
Elliott Hughesadb8c672012-03-06 16:49:32 -08002032 static_cast<int32_t>(dec_insn.vB);
Logan Chien7a89b6d2011-12-27 17:56:56 +08002033
2034 Payload const* payload =
2035 reinterpret_cast<Payload const*>(code_item_->insns_ + payload_offset);
2036
2037 int32_t const* keys = payload->keys_and_targets_;
2038 int32_t const* targets = payload->keys_and_targets_ + payload->num_cases_;
2039
Elliott Hughesadb8c672012-03-06 16:49:32 -08002040 llvm::Value* value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chien7a89b6d2011-12-27 17:56:56 +08002041
2042 llvm::SwitchInst* sw =
2043 irb_.CreateSwitch(value, GetNextBasicBlock(dex_pc), payload->num_cases_);
2044
2045 for (size_t i = 0; i < payload->num_cases_; ++i) {
2046 sw->addCase(irb_.getInt32(keys[i]), GetBasicBlock(dex_pc + targets[i]));
2047 }
Logan Chien70f94b42011-12-27 17:49:11 +08002048}
2049
2050
2051void MethodCompiler::EmitInsn_FPCompare(uint32_t dex_pc,
2052 Instruction const* insn,
2053 JType fp_jty,
2054 bool gt_bias) {
Logan Chien2c37e8e2011-12-27 17:58:46 +08002055
Elliott Hughesadb8c672012-03-06 16:49:32 -08002056 DecodedInstruction dec_insn(insn);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002057
2058 DCHECK(fp_jty == kFloat || fp_jty == kDouble) << "JType: " << fp_jty;
2059
Elliott Hughesadb8c672012-03-06 16:49:32 -08002060 llvm::Value* src1_value = EmitLoadDalvikReg(dec_insn.vB, fp_jty, kAccurate);
2061 llvm::Value* src2_value = EmitLoadDalvikReg(dec_insn.vC, fp_jty, kAccurate);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002062
2063 llvm::Value* cmp_eq = irb_.CreateFCmpOEQ(src1_value, src2_value);
2064 llvm::Value* cmp_lt;
2065
2066 if (gt_bias) {
2067 cmp_lt = irb_.CreateFCmpOLT(src1_value, src2_value);
2068 } else {
2069 cmp_lt = irb_.CreateFCmpULT(src1_value, src2_value);
2070 }
2071
2072 llvm::Value* result = EmitCompareResultSelection(cmp_eq, cmp_lt);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002073 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002074
Logan Chien70f94b42011-12-27 17:49:11 +08002075 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2076}
2077
2078
2079void MethodCompiler::EmitInsn_LongCompare(uint32_t dex_pc,
2080 Instruction const* insn) {
Logan Chien2c37e8e2011-12-27 17:58:46 +08002081
Elliott Hughesadb8c672012-03-06 16:49:32 -08002082 DecodedInstruction dec_insn(insn);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002083
Elliott Hughesadb8c672012-03-06 16:49:32 -08002084 llvm::Value* src1_value = EmitLoadDalvikReg(dec_insn.vB, kLong, kAccurate);
2085 llvm::Value* src2_value = EmitLoadDalvikReg(dec_insn.vC, kLong, kAccurate);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002086
2087 llvm::Value* cmp_eq = irb_.CreateICmpEQ(src1_value, src2_value);
2088 llvm::Value* cmp_lt = irb_.CreateICmpSLT(src1_value, src2_value);
2089
2090 llvm::Value* result = EmitCompareResultSelection(cmp_eq, cmp_lt);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002091 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002092
Logan Chien70f94b42011-12-27 17:49:11 +08002093 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2094}
2095
2096
Logan Chien2c37e8e2011-12-27 17:58:46 +08002097llvm::Value* MethodCompiler::EmitCompareResultSelection(llvm::Value* cmp_eq,
2098 llvm::Value* cmp_lt) {
2099
2100 llvm::Constant* zero = irb_.getJInt(0);
2101 llvm::Constant* pos1 = irb_.getJInt(1);
2102 llvm::Constant* neg1 = irb_.getJInt(-1);
2103
2104 llvm::Value* result_lt = irb_.CreateSelect(cmp_lt, neg1, pos1);
2105 llvm::Value* result_eq = irb_.CreateSelect(cmp_eq, zero, result_lt);
2106
2107 return result_eq;
2108}
2109
2110
Logan Chien70f94b42011-12-27 17:49:11 +08002111void MethodCompiler::EmitInsn_BinaryConditionalBranch(uint32_t dex_pc,
2112 Instruction const* insn,
2113 CondBranchKind cond) {
Logan Chiena78e3c82011-12-27 17:59:35 +08002114
Elliott Hughesadb8c672012-03-06 16:49:32 -08002115 DecodedInstruction dec_insn(insn);
Logan Chiena78e3c82011-12-27 17:59:35 +08002116
Elliott Hughesadb8c672012-03-06 16:49:32 -08002117 int8_t src1_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vA);
2118 int8_t src2_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vB);
Logan Chiena78e3c82011-12-27 17:59:35 +08002119
2120 DCHECK_NE(kRegUnknown, src1_reg_cat);
2121 DCHECK_NE(kRegUnknown, src2_reg_cat);
2122 DCHECK_NE(kRegCat2, src1_reg_cat);
2123 DCHECK_NE(kRegCat2, src2_reg_cat);
2124
Elliott Hughesadb8c672012-03-06 16:49:32 -08002125 int32_t branch_offset = dec_insn.vC;
Logan Chiena78e3c82011-12-27 17:59:35 +08002126
2127 if (branch_offset <= 0) {
2128 // Garbage collection safe-point on backward branch
2129 EmitGuard_GarbageCollectionSuspend(dex_pc);
2130 }
2131
2132 if (src1_reg_cat == kRegZero && src2_reg_cat == kRegZero) {
2133 irb_.CreateBr(GetBasicBlock(dex_pc + branch_offset));
2134 return;
2135 }
2136
2137 llvm::Value* src1_value;
2138 llvm::Value* src2_value;
2139
2140 if (src1_reg_cat != kRegZero && src2_reg_cat != kRegZero) {
2141 CHECK_EQ(src1_reg_cat, src2_reg_cat);
2142
2143 if (src1_reg_cat == kRegCat1nr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002144 src1_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
2145 src2_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002146 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002147 src1_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
2148 src2_value = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002149 }
2150 } else {
2151 DCHECK(src1_reg_cat == kRegZero ||
2152 src2_reg_cat == kRegZero);
2153
2154 if (src1_reg_cat == kRegZero) {
2155 if (src2_reg_cat == kRegCat1nr) {
2156 src1_value = irb_.getJInt(0);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002157 src2_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002158 } else {
2159 src1_value = irb_.getJNull();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002160 src2_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002161 }
2162 } else { // src2_reg_cat == kRegZero
2163 if (src2_reg_cat == kRegCat1nr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002164 src1_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002165 src2_value = irb_.getJInt(0);
2166 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002167 src1_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002168 src2_value = irb_.getJNull();
2169 }
2170 }
2171 }
2172
2173 llvm::Value* cond_value =
2174 EmitConditionResult(src1_value, src2_value, cond);
2175
2176 irb_.CreateCondBr(cond_value,
2177 GetBasicBlock(dex_pc + branch_offset),
2178 GetNextBasicBlock(dex_pc));
Logan Chien70f94b42011-12-27 17:49:11 +08002179}
2180
2181
2182void MethodCompiler::EmitInsn_UnaryConditionalBranch(uint32_t dex_pc,
2183 Instruction const* insn,
2184 CondBranchKind cond) {
Logan Chiena78e3c82011-12-27 17:59:35 +08002185
Elliott Hughesadb8c672012-03-06 16:49:32 -08002186 DecodedInstruction dec_insn(insn);
Logan Chiena78e3c82011-12-27 17:59:35 +08002187
Elliott Hughesadb8c672012-03-06 16:49:32 -08002188 int8_t src_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vA);
Logan Chiena78e3c82011-12-27 17:59:35 +08002189
2190 DCHECK_NE(kRegUnknown, src_reg_cat);
2191 DCHECK_NE(kRegCat2, src_reg_cat);
2192
Elliott Hughesadb8c672012-03-06 16:49:32 -08002193 int32_t branch_offset = dec_insn.vB;
Logan Chiena78e3c82011-12-27 17:59:35 +08002194
2195 if (branch_offset <= 0) {
2196 // Garbage collection safe-point on backward branch
2197 EmitGuard_GarbageCollectionSuspend(dex_pc);
2198 }
2199
2200 if (src_reg_cat == kRegZero) {
2201 irb_.CreateBr(GetBasicBlock(dex_pc + branch_offset));
2202 return;
2203 }
2204
2205 llvm::Value* src1_value;
2206 llvm::Value* src2_value;
2207
2208 if (src_reg_cat == kRegCat1nr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002209 src1_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002210 src2_value = irb_.getInt32(0);
2211 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002212 src1_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002213 src2_value = irb_.getJNull();
2214 }
2215
2216 llvm::Value* cond_value =
2217 EmitConditionResult(src1_value, src2_value, cond);
2218
2219 irb_.CreateCondBr(cond_value,
2220 GetBasicBlock(dex_pc + branch_offset),
2221 GetNextBasicBlock(dex_pc));
2222}
2223
2224
2225RegCategory MethodCompiler::GetInferredRegCategory(uint32_t dex_pc,
2226 uint16_t reg_idx) {
Logan Chiendd361c92012-04-10 23:40:37 +08002227
2228 Compiler::MethodReference mref(dex_file_, method_idx_);
2229
2230 InferredRegCategoryMap const* map =
Ian Rogers776ac1f2012-04-13 23:36:36 -07002231 verifier::MethodVerifier::GetInferredRegCategoryMap(mref);
Logan Chiendd361c92012-04-10 23:40:37 +08002232
Logan Chiena78e3c82011-12-27 17:59:35 +08002233 CHECK_NE(map, static_cast<InferredRegCategoryMap*>(NULL));
2234
2235 return map->GetRegCategory(dex_pc, reg_idx);
2236}
2237
2238
2239llvm::Value* MethodCompiler::EmitConditionResult(llvm::Value* lhs,
2240 llvm::Value* rhs,
2241 CondBranchKind cond) {
2242 switch (cond) {
2243 case kCondBranch_EQ:
2244 return irb_.CreateICmpEQ(lhs, rhs);
2245
2246 case kCondBranch_NE:
2247 return irb_.CreateICmpNE(lhs, rhs);
2248
2249 case kCondBranch_LT:
2250 return irb_.CreateICmpSLT(lhs, rhs);
2251
2252 case kCondBranch_GE:
2253 return irb_.CreateICmpSGE(lhs, rhs);
2254
2255 case kCondBranch_GT:
2256 return irb_.CreateICmpSGT(lhs, rhs);
2257
2258 case kCondBranch_LE:
2259 return irb_.CreateICmpSLE(lhs, rhs);
2260
2261 default: // Unreachable
2262 LOG(FATAL) << "Unknown conditional branch kind: " << cond;
2263 return NULL;
2264 }
Logan Chien70f94b42011-12-27 17:49:11 +08002265}
2266
TDYa12783bb6622012-04-17 02:20:34 -07002267void MethodCompiler::EmitMarkGCCard(llvm::Value* value, llvm::Value* target_addr) {
2268 // Using runtime support, let the target can override by InlineAssembly.
2269 llvm::Function* runtime_func = irb_.GetRuntime(MarkGCCard);
2270
2271 irb_.CreateCall2(runtime_func, value, target_addr);
2272}
Logan Chien70f94b42011-12-27 17:49:11 +08002273
Logan Chiene27fdbb2012-01-02 23:27:26 +08002274void
2275MethodCompiler::EmitGuard_ArrayIndexOutOfBoundsException(uint32_t dex_pc,
2276 llvm::Value* array,
2277 llvm::Value* index) {
2278 llvm::Value* array_len = EmitLoadArrayLength(array);
2279
2280 llvm::Value* cmp = irb_.CreateICmpUGE(index, array_len);
2281
2282 llvm::BasicBlock* block_exception =
2283 CreateBasicBlockWithDexPC(dex_pc, "overflow");
2284
2285 llvm::BasicBlock* block_continue =
2286 CreateBasicBlockWithDexPC(dex_pc, "cont");
2287
2288 irb_.CreateCondBr(cmp, block_exception, block_continue);
2289
2290 irb_.SetInsertPoint(block_exception);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002291
TDYa127c8dc1012012-04-19 07:03:33 -07002292 EmitUpdateDexPC(dex_pc);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002293 irb_.CreateCall2(irb_.GetRuntime(ThrowIndexOutOfBounds), index, array_len);
2294 EmitBranchExceptionLandingPad(dex_pc);
2295
2296 irb_.SetInsertPoint(block_continue);
2297}
2298
2299
2300void MethodCompiler::EmitGuard_ArrayException(uint32_t dex_pc,
2301 llvm::Value* array,
2302 llvm::Value* index) {
2303 EmitGuard_NullPointerException(dex_pc, array);
2304 EmitGuard_ArrayIndexOutOfBoundsException(dex_pc, array, index);
2305}
2306
2307
2308// Emit Array GetElementPtr
2309llvm::Value* MethodCompiler::EmitArrayGEP(llvm::Value* array_addr,
2310 llvm::Value* index_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08002311 llvm::Type* elem_type,
2312 JType elem_jty) {
2313
2314 int data_offset;
2315 if (elem_jty == kLong || elem_jty == kDouble ||
2316 (elem_jty == kObject && sizeof(uint64_t) == sizeof(Object*))) {
2317 data_offset = Array::DataOffset(sizeof(int64_t)).Int32Value();
2318 } else {
2319 data_offset = Array::DataOffset(sizeof(int32_t)).Int32Value();
2320 }
Logan Chiene27fdbb2012-01-02 23:27:26 +08002321
2322 llvm::Constant* data_offset_value =
Ian Rogers04ec04e2012-02-28 16:15:33 -08002323 irb_.getPtrEquivInt(data_offset);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002324
2325 llvm::Value* array_data_addr =
2326 irb_.CreatePtrDisp(array_addr, data_offset_value,
2327 elem_type->getPointerTo());
2328
2329 return irb_.CreateGEP(array_data_addr, index_value);
2330}
2331
2332
Logan Chien70f94b42011-12-27 17:49:11 +08002333void MethodCompiler::EmitInsn_AGet(uint32_t dex_pc,
2334 Instruction const* insn,
2335 JType elem_jty) {
Logan Chiene27fdbb2012-01-02 23:27:26 +08002336
Elliott Hughesadb8c672012-03-06 16:49:32 -08002337 DecodedInstruction dec_insn(insn);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002338
Elliott Hughesadb8c672012-03-06 16:49:32 -08002339 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
2340 llvm::Value* index_value = EmitLoadDalvikReg(dec_insn.vC, kInt, kAccurate);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002341
2342 EmitGuard_ArrayException(dex_pc, array_addr, index_value);
2343
2344 llvm::Type* elem_type = irb_.getJType(elem_jty, kArray);
2345
2346 llvm::Value* array_elem_addr =
Ian Rogers04ec04e2012-02-28 16:15:33 -08002347 EmitArrayGEP(array_addr, index_value, elem_type, elem_jty);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002348
2349 llvm::Value* array_elem_value = irb_.CreateLoad(array_elem_addr);
2350
Elliott Hughesadb8c672012-03-06 16:49:32 -08002351 EmitStoreDalvikReg(dec_insn.vA, elem_jty, kArray, array_elem_value);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002352
Logan Chien70f94b42011-12-27 17:49:11 +08002353 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2354}
2355
2356
2357void MethodCompiler::EmitInsn_APut(uint32_t dex_pc,
2358 Instruction const* insn,
2359 JType elem_jty) {
Logan Chien8dabb432012-01-02 23:29:32 +08002360
Elliott Hughesadb8c672012-03-06 16:49:32 -08002361 DecodedInstruction dec_insn(insn);
Logan Chien8dabb432012-01-02 23:29:32 +08002362
Elliott Hughesadb8c672012-03-06 16:49:32 -08002363 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
2364 llvm::Value* index_value = EmitLoadDalvikReg(dec_insn.vC, kInt, kAccurate);
Logan Chien8dabb432012-01-02 23:29:32 +08002365
2366 EmitGuard_ArrayException(dex_pc, array_addr, index_value);
2367
2368 llvm::Type* elem_type = irb_.getJType(elem_jty, kArray);
2369
2370 llvm::Value* array_elem_addr =
Ian Rogers04ec04e2012-02-28 16:15:33 -08002371 EmitArrayGEP(array_addr, index_value, elem_type, elem_jty);
Logan Chien8dabb432012-01-02 23:29:32 +08002372
Elliott Hughesadb8c672012-03-06 16:49:32 -08002373 llvm::Value* new_value = EmitLoadDalvikReg(dec_insn.vA, elem_jty, kArray);
Logan Chien8dabb432012-01-02 23:29:32 +08002374
TDYa12783bb6622012-04-17 02:20:34 -07002375 if (elem_jty == kObject) { // If put an object, check the type, and mark GC card table.
TDYa1271b86d072012-04-05 17:38:56 -07002376 llvm::Function* runtime_func = irb_.GetRuntime(CheckPutArrayElement);
2377
2378 irb_.CreateCall2(runtime_func, new_value, array_addr);
2379
2380 EmitGuard_ExceptionLandingPad(dex_pc);
TDYa12783bb6622012-04-17 02:20:34 -07002381
2382 EmitMarkGCCard(new_value, array_addr);
TDYa1271b86d072012-04-05 17:38:56 -07002383 }
2384
Logan Chien8dabb432012-01-02 23:29:32 +08002385 irb_.CreateStore(new_value, array_elem_addr);
2386
Logan Chien70f94b42011-12-27 17:49:11 +08002387 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2388}
2389
2390
2391void MethodCompiler::EmitInsn_IGet(uint32_t dex_pc,
2392 Instruction const* insn,
2393 JType field_jty) {
Logan Chien48f1d2a2012-01-02 22:49:53 +08002394
Elliott Hughesadb8c672012-03-06 16:49:32 -08002395 DecodedInstruction dec_insn(insn);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002396
Elliott Hughesadb8c672012-03-06 16:49:32 -08002397 uint32_t reg_idx = dec_insn.vB;
2398 uint32_t field_idx = dec_insn.vC;
Logan Chien48f1d2a2012-01-02 22:49:53 +08002399
Logan Chien48f1d2a2012-01-02 22:49:53 +08002400 llvm::Value* object_addr = EmitLoadDalvikReg(reg_idx, kObject, kAccurate);
2401
2402 EmitGuard_NullPointerException(dex_pc, object_addr);
2403
2404 llvm::Value* field_value;
2405
Logan Chien933abf82012-04-11 12:24:31 +08002406 int field_offset;
2407 bool is_volatile;
2408 bool is_fast_path = compiler_->ComputeInstanceFieldInfo(
2409 field_idx, oat_compilation_unit_, field_offset, is_volatile, false);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002410
Logan Chien933abf82012-04-11 12:24:31 +08002411 if (!is_fast_path) {
Logan Chien48f1d2a2012-01-02 22:49:53 +08002412 llvm::Function* runtime_func;
2413
2414 if (field_jty == kObject) {
Logan Chien3b2b2e72012-03-06 16:11:45 +08002415 runtime_func = irb_.GetRuntime(GetObjectInstance);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002416 } else if (field_jty == kLong || field_jty == kDouble) {
Logan Chien3b2b2e72012-03-06 16:11:45 +08002417 runtime_func = irb_.GetRuntime(Get64Instance);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002418 } else {
Logan Chien3b2b2e72012-03-06 16:11:45 +08002419 runtime_func = irb_.GetRuntime(Get32Instance);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002420 }
2421
2422 llvm::ConstantInt* field_idx_value = irb_.getInt32(field_idx);
2423
2424 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2425
TDYa127c8dc1012012-04-19 07:03:33 -07002426 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002427
Logan Chien3b2b2e72012-03-06 16:11:45 +08002428 field_value = irb_.CreateCall3(runtime_func, field_idx_value,
2429 method_object_addr, object_addr);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002430
2431 EmitGuard_ExceptionLandingPad(dex_pc);
2432
2433 } else {
Logan Chien933abf82012-04-11 12:24:31 +08002434 DCHECK_GE(field_offset, 0);
2435
Logan Chien48f1d2a2012-01-02 22:49:53 +08002436 llvm::PointerType* field_type =
2437 irb_.getJType(field_jty, kField)->getPointerTo();
2438
Logan Chien933abf82012-04-11 12:24:31 +08002439 llvm::ConstantInt* field_offset_value = irb_.getPtrEquivInt(field_offset);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002440
2441 llvm::Value* field_addr =
Logan Chien933abf82012-04-11 12:24:31 +08002442 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002443
Logan Chien933abf82012-04-11 12:24:31 +08002444 // TODO: Check is_volatile. We need to generate atomic load instruction
2445 // when is_volatile is true.
Logan Chien48f1d2a2012-01-02 22:49:53 +08002446 field_value = irb_.CreateLoad(field_addr);
2447 }
2448
Elliott Hughesadb8c672012-03-06 16:49:32 -08002449 EmitStoreDalvikReg(dec_insn.vA, field_jty, kField, field_value);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002450
Logan Chien70f94b42011-12-27 17:49:11 +08002451 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2452}
2453
2454
2455void MethodCompiler::EmitInsn_IPut(uint32_t dex_pc,
2456 Instruction const* insn,
2457 JType field_jty) {
Logan Chiendd6aa872012-01-03 16:06:32 +08002458
Elliott Hughesadb8c672012-03-06 16:49:32 -08002459 DecodedInstruction dec_insn(insn);
Logan Chiendd6aa872012-01-03 16:06:32 +08002460
Elliott Hughesadb8c672012-03-06 16:49:32 -08002461 uint32_t reg_idx = dec_insn.vB;
2462 uint32_t field_idx = dec_insn.vC;
Logan Chiendd6aa872012-01-03 16:06:32 +08002463
Logan Chiendd6aa872012-01-03 16:06:32 +08002464 llvm::Value* object_addr = EmitLoadDalvikReg(reg_idx, kObject, kAccurate);
2465
2466 EmitGuard_NullPointerException(dex_pc, object_addr);
2467
Elliott Hughesadb8c672012-03-06 16:49:32 -08002468 llvm::Value* new_value = EmitLoadDalvikReg(dec_insn.vA, field_jty, kField);
Logan Chiendd6aa872012-01-03 16:06:32 +08002469
Logan Chien933abf82012-04-11 12:24:31 +08002470 int field_offset;
2471 bool is_volatile;
2472 bool is_fast_path = compiler_->ComputeInstanceFieldInfo(
2473 field_idx, oat_compilation_unit_, field_offset, is_volatile, true);
Logan Chiendd6aa872012-01-03 16:06:32 +08002474
Logan Chien933abf82012-04-11 12:24:31 +08002475 if (!is_fast_path) {
Logan Chiendd6aa872012-01-03 16:06:32 +08002476 llvm::Function* runtime_func;
2477
2478 if (field_jty == kObject) {
2479 runtime_func = irb_.GetRuntime(SetObjectInstance);
2480 } else if (field_jty == kLong || field_jty == kDouble) {
2481 runtime_func = irb_.GetRuntime(Set64Instance);
2482 } else {
2483 runtime_func = irb_.GetRuntime(Set32Instance);
2484 }
2485
2486 llvm::Value* field_idx_value = irb_.getInt32(field_idx);
2487
2488 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2489
TDYa127c8dc1012012-04-19 07:03:33 -07002490 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002491
Logan Chien3b2b2e72012-03-06 16:11:45 +08002492 irb_.CreateCall4(runtime_func, field_idx_value,
2493 method_object_addr, object_addr, new_value);
Logan Chiendd6aa872012-01-03 16:06:32 +08002494
2495 EmitGuard_ExceptionLandingPad(dex_pc);
2496
2497 } else {
Logan Chien933abf82012-04-11 12:24:31 +08002498 DCHECK_GE(field_offset, 0);
2499
Logan Chiendd6aa872012-01-03 16:06:32 +08002500 llvm::PointerType* field_type =
2501 irb_.getJType(field_jty, kField)->getPointerTo();
2502
Logan Chien933abf82012-04-11 12:24:31 +08002503 llvm::Value* field_offset_value = irb_.getPtrEquivInt(field_offset);
Logan Chiendd6aa872012-01-03 16:06:32 +08002504
2505 llvm::Value* field_addr =
Logan Chien933abf82012-04-11 12:24:31 +08002506 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
Logan Chiendd6aa872012-01-03 16:06:32 +08002507
Logan Chien933abf82012-04-11 12:24:31 +08002508 // TODO: Check is_volatile. We need to generate atomic store instruction
2509 // when is_volatile is true.
Logan Chiendd6aa872012-01-03 16:06:32 +08002510 irb_.CreateStore(new_value, field_addr);
TDYa12783bb6622012-04-17 02:20:34 -07002511
2512 if (field_jty == kObject) { // If put an object, mark the GC card table.
2513 EmitMarkGCCard(new_value, object_addr);
2514 }
Logan Chiendd6aa872012-01-03 16:06:32 +08002515 }
2516
Logan Chien70f94b42011-12-27 17:49:11 +08002517 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2518}
2519
2520
Logan Chien438c4b62012-01-17 16:06:00 +08002521llvm::Value* MethodCompiler::EmitLoadStaticStorage(uint32_t dex_pc,
2522 uint32_t type_idx) {
2523 llvm::BasicBlock* block_load_static =
2524 CreateBasicBlockWithDexPC(dex_pc, "load_static");
2525
2526 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
2527
2528 // Load static storage from dex cache
2529 llvm::Value* storage_field_addr =
2530 EmitLoadDexCacheStaticStorageFieldAddr(type_idx);
2531
2532 llvm::Value* storage_object_addr = irb_.CreateLoad(storage_field_addr);
2533
2534 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
2535
2536 // Test: Is the static storage of this class initialized?
2537 llvm::Value* equal_null =
2538 irb_.CreateICmpEQ(storage_object_addr, irb_.getJNull());
2539
2540 irb_.CreateCondBr(equal_null, block_load_static, block_cont);
2541
2542 // Failback routine to load the class object
2543 irb_.SetInsertPoint(block_load_static);
2544
2545 llvm::Function* runtime_func =
2546 irb_.GetRuntime(InitializeStaticStorage);
2547
2548 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
2549
2550 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2551
TDYa127706e9b62012-04-19 12:24:26 -07002552 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
2553
TDYa127c8dc1012012-04-19 07:03:33 -07002554 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002555
Logan Chien438c4b62012-01-17 16:06:00 +08002556 llvm::Value* loaded_storage_object_addr =
TDYa127706e9b62012-04-19 12:24:26 -07002557 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
Logan Chien438c4b62012-01-17 16:06:00 +08002558
2559 EmitGuard_ExceptionLandingPad(dex_pc);
2560
2561 llvm::BasicBlock* block_after_load_static = irb_.GetInsertBlock();
2562
2563 irb_.CreateBr(block_cont);
2564
2565 // Now the class object must be loaded
2566 irb_.SetInsertPoint(block_cont);
2567
2568 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
2569
2570 phi->addIncoming(storage_object_addr, block_original);
2571 phi->addIncoming(loaded_storage_object_addr, block_after_load_static);
2572
2573 return phi;
2574}
2575
2576
Logan Chien70f94b42011-12-27 17:49:11 +08002577void MethodCompiler::EmitInsn_SGet(uint32_t dex_pc,
2578 Instruction const* insn,
2579 JType field_jty) {
Logan Chien438c4b62012-01-17 16:06:00 +08002580
Elliott Hughesadb8c672012-03-06 16:49:32 -08002581 DecodedInstruction dec_insn(insn);
Logan Chien438c4b62012-01-17 16:06:00 +08002582
Logan Chien933abf82012-04-11 12:24:31 +08002583 uint32_t field_idx = dec_insn.vB;
Logan Chien438c4b62012-01-17 16:06:00 +08002584
Logan Chien933abf82012-04-11 12:24:31 +08002585 int field_offset;
2586 int ssb_index;
2587 bool is_referrers_class;
2588 bool is_volatile;
2589
2590 bool is_fast_path = compiler_->ComputeStaticFieldInfo(
2591 field_idx, oat_compilation_unit_, field_offset, ssb_index,
2592 is_referrers_class, is_volatile, false);
Logan Chien438c4b62012-01-17 16:06:00 +08002593
2594 llvm::Value* static_field_value;
2595
Logan Chien933abf82012-04-11 12:24:31 +08002596 if (!is_fast_path) {
Logan Chien438c4b62012-01-17 16:06:00 +08002597 llvm::Function* runtime_func;
2598
2599 if (field_jty == kObject) {
2600 runtime_func = irb_.GetRuntime(GetObjectStatic);
2601 } else if (field_jty == kLong || field_jty == kDouble) {
2602 runtime_func = irb_.GetRuntime(Get64Static);
2603 } else {
2604 runtime_func = irb_.GetRuntime(Get32Static);
2605 }
2606
Elliott Hughesadb8c672012-03-06 16:49:32 -08002607 llvm::Constant* field_idx_value = irb_.getInt32(dec_insn.vB);
Logan Chien438c4b62012-01-17 16:06:00 +08002608
2609 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2610
TDYa127c8dc1012012-04-19 07:03:33 -07002611 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002612
Logan Chien438c4b62012-01-17 16:06:00 +08002613 static_field_value =
2614 irb_.CreateCall2(runtime_func, field_idx_value, method_object_addr);
2615
2616 EmitGuard_ExceptionLandingPad(dex_pc);
2617
2618 } else {
Logan Chien933abf82012-04-11 12:24:31 +08002619 DCHECK_GE(field_offset, 0);
Logan Chien438c4b62012-01-17 16:06:00 +08002620
Logan Chien933abf82012-04-11 12:24:31 +08002621 llvm::Value* static_storage_addr = NULL;
2622
2623 if (is_referrers_class) {
2624 // Fast path, static storage base is this method's class
2625 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2626
2627 llvm::Constant* declaring_class_offset_value =
2628 irb_.getPtrEquivInt(Method::DeclaringClassOffset().Int32Value());
2629
2630 llvm::Value* static_storage_field_addr =
2631 irb_.CreatePtrDisp(method_object_addr, declaring_class_offset_value,
2632 irb_.getJObjectTy()->getPointerTo());
2633
2634 static_storage_addr = irb_.CreateLoad(static_storage_field_addr);
2635 } else {
2636 // Medium path, static storage base in a different class which
2637 // requires checks that the other class is initialized
2638 DCHECK_GE(ssb_index, 0);
2639 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
2640 }
2641
2642 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
Logan Chien438c4b62012-01-17 16:06:00 +08002643
2644 llvm::Value* static_field_addr =
2645 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
2646 irb_.getJType(field_jty, kField)->getPointerTo());
2647
Logan Chien933abf82012-04-11 12:24:31 +08002648 // TODO: Check is_volatile. We need to generate atomic load instruction
2649 // when is_volatile is true.
Logan Chien438c4b62012-01-17 16:06:00 +08002650 static_field_value = irb_.CreateLoad(static_field_addr);
2651 }
2652
Elliott Hughesadb8c672012-03-06 16:49:32 -08002653 EmitStoreDalvikReg(dec_insn.vA, field_jty, kField, static_field_value);
Logan Chien438c4b62012-01-17 16:06:00 +08002654
Logan Chien70f94b42011-12-27 17:49:11 +08002655 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2656}
2657
2658
2659void MethodCompiler::EmitInsn_SPut(uint32_t dex_pc,
2660 Instruction const* insn,
2661 JType field_jty) {
Logan Chien14179c82012-01-17 17:06:34 +08002662
Elliott Hughesadb8c672012-03-06 16:49:32 -08002663 DecodedInstruction dec_insn(insn);
Logan Chien14179c82012-01-17 17:06:34 +08002664
Logan Chien933abf82012-04-11 12:24:31 +08002665 uint32_t field_idx = dec_insn.vB;
Logan Chien14179c82012-01-17 17:06:34 +08002666
Elliott Hughesadb8c672012-03-06 16:49:32 -08002667 llvm::Value* new_value = EmitLoadDalvikReg(dec_insn.vA, field_jty, kField);
Logan Chien14179c82012-01-17 17:06:34 +08002668
Logan Chien933abf82012-04-11 12:24:31 +08002669 int field_offset;
2670 int ssb_index;
2671 bool is_referrers_class;
2672 bool is_volatile;
2673
2674 bool is_fast_path = compiler_->ComputeStaticFieldInfo(
2675 field_idx, oat_compilation_unit_, field_offset, ssb_index,
2676 is_referrers_class, is_volatile, true);
2677
2678 if (!is_fast_path) {
Logan Chien14179c82012-01-17 17:06:34 +08002679 llvm::Function* runtime_func;
2680
2681 if (field_jty == kObject) {
2682 runtime_func = irb_.GetRuntime(SetObjectStatic);
2683 } else if (field_jty == kLong || field_jty == kDouble) {
2684 runtime_func = irb_.GetRuntime(Set64Static);
2685 } else {
2686 runtime_func = irb_.GetRuntime(Set32Static);
2687 }
2688
Elliott Hughesadb8c672012-03-06 16:49:32 -08002689 llvm::Constant* field_idx_value = irb_.getInt32(dec_insn.vB);
Logan Chien14179c82012-01-17 17:06:34 +08002690
2691 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2692
TDYa127c8dc1012012-04-19 07:03:33 -07002693 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002694
Logan Chien14179c82012-01-17 17:06:34 +08002695 irb_.CreateCall3(runtime_func, field_idx_value,
2696 method_object_addr, new_value);
2697
2698 EmitGuard_ExceptionLandingPad(dex_pc);
2699
2700 } else {
Logan Chien933abf82012-04-11 12:24:31 +08002701 DCHECK_GE(field_offset, 0);
Logan Chien14179c82012-01-17 17:06:34 +08002702
Logan Chien933abf82012-04-11 12:24:31 +08002703 llvm::Value* static_storage_addr = NULL;
2704
2705 if (is_referrers_class) {
2706 // Fast path, static storage base is this method's class
2707 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2708
2709 llvm::Constant* declaring_class_offset_value =
2710 irb_.getPtrEquivInt(Method::DeclaringClassOffset().Int32Value());
2711
2712 llvm::Value* static_storage_field_addr =
2713 irb_.CreatePtrDisp(method_object_addr, declaring_class_offset_value,
2714 irb_.getJObjectTy()->getPointerTo());
2715
2716 static_storage_addr = irb_.CreateLoad(static_storage_field_addr);
2717 } else {
2718 // Medium path, static storage base in a different class which
2719 // requires checks that the other class is initialized
2720 DCHECK_GE(ssb_index, 0);
2721 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
2722 }
2723
2724 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
Logan Chien14179c82012-01-17 17:06:34 +08002725
2726 llvm::Value* static_field_addr =
2727 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
2728 irb_.getJType(field_jty, kField)->getPointerTo());
2729
Logan Chien933abf82012-04-11 12:24:31 +08002730 // TODO: Check is_volatile. We need to generate atomic store instruction
2731 // when is_volatile is true.
Logan Chien14179c82012-01-17 17:06:34 +08002732 irb_.CreateStore(new_value, static_field_addr);
TDYa12783bb6622012-04-17 02:20:34 -07002733
2734 if (field_jty == kObject) { // If put an object, mark the GC card table.
2735 EmitMarkGCCard(new_value, static_storage_addr);
2736 }
Logan Chien14179c82012-01-17 17:06:34 +08002737 }
2738
Logan Chien70f94b42011-12-27 17:49:11 +08002739 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2740}
2741
2742
Logan Chien1a121b92012-02-15 22:23:42 +08002743void MethodCompiler::
2744EmitLoadActualParameters(std::vector<llvm::Value*>& args,
2745 uint32_t callee_method_idx,
Elliott Hughesadb8c672012-03-06 16:49:32 -08002746 DecodedInstruction const& dec_insn,
Logan Chien7e7fabc2012-04-10 18:59:11 +08002747 InvokeArgFmt arg_fmt,
Logan Chien1a121b92012-02-15 22:23:42 +08002748 bool is_static) {
2749
2750 // Get method signature
2751 DexFile::MethodId const& method_id =
2752 dex_file_->GetMethodId(callee_method_idx);
2753
Logan Chien8faf8022012-02-24 12:25:29 +08002754 uint32_t shorty_size;
Logan Chien1a121b92012-02-15 22:23:42 +08002755 char const* shorty = dex_file_->GetMethodShorty(method_id, &shorty_size);
Logan Chien8faf8022012-02-24 12:25:29 +08002756 CHECK_GE(shorty_size, 1u);
Logan Chien1a121b92012-02-15 22:23:42 +08002757
2758 // Load argument values according to the shorty (without "this")
2759 uint16_t reg_count = 0;
2760
2761 if (!is_static) {
2762 ++reg_count; // skip the "this" pointer
2763 }
2764
Logan Chien7e7fabc2012-04-10 18:59:11 +08002765 bool is_range = (arg_fmt == kArgRange);
2766
Logan Chien8faf8022012-02-24 12:25:29 +08002767 for (uint32_t i = 1; i < shorty_size; ++i) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002768 uint32_t reg_idx = (is_range) ? (dec_insn.vC + reg_count)
2769 : (dec_insn.arg[reg_count]);
Logan Chien1a121b92012-02-15 22:23:42 +08002770
2771 args.push_back(EmitLoadDalvikReg(reg_idx, shorty[i], kAccurate));
2772
2773 ++reg_count;
2774 if (shorty[i] == 'J' || shorty[i] == 'D') {
2775 // Wide types, such as long and double, are using a pair of registers
2776 // to store the value, so we have to increase arg_reg again.
2777 ++reg_count;
2778 }
2779 }
2780
Elliott Hughesadb8c672012-03-06 16:49:32 -08002781 DCHECK_EQ(reg_count, dec_insn.vA)
Logan Chien1a121b92012-02-15 22:23:42 +08002782 << "Actual argument mismatch for callee: "
2783 << PrettyMethod(callee_method_idx, *dex_file_);
2784}
2785
TDYa1270b686e52012-04-09 22:43:35 -07002786llvm::Value* MethodCompiler::EmitFixStub(llvm::Value* callee_method_object_addr,
2787 uint32_t method_idx,
2788 bool is_static) {
2789 // TODO: Remove this after we solve the link and trampoline related problems.
2790 llvm::Value* code_addr = irb_.CreateCall(irb_.GetRuntime(FixStub), callee_method_object_addr);
2791
2792 llvm::FunctionType* method_type = GetFunctionType(method_idx, is_static);
2793
2794 return irb_.CreatePointerCast(code_addr, method_type->getPointerTo());
TDYa12785321912012-04-01 15:24:56 -07002795}
Logan Chien1a121b92012-02-15 22:23:42 +08002796
Shih-wei Liao399ed3f2012-03-08 01:27:04 -08002797
Logan Chien7e7fabc2012-04-10 18:59:11 +08002798void MethodCompiler::EmitInsn_Invoke(uint32_t dex_pc,
2799 Instruction const* insn,
2800 InvokeType invoke_type,
2801 InvokeArgFmt arg_fmt) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002802 DecodedInstruction dec_insn(insn);
Logan Chien46fbb412012-02-15 22:29:08 +08002803
Logan Chien61c65dc2012-02-29 03:22:30 +08002804 bool is_static = (invoke_type == kStatic);
Logan Chien7e7fabc2012-04-10 18:59:11 +08002805 uint32_t callee_method_idx = dec_insn.vB;
Logan Chien61c65dc2012-02-29 03:22:30 +08002806
Logan Chien7e7fabc2012-04-10 18:59:11 +08002807 // Compute invoke related information for compiler decision
2808 int vtable_idx = -1;
Logan Chien92ad16d2012-03-18 05:48:55 +08002809 uintptr_t direct_code = 0; // Currently unused
2810 uintptr_t direct_method = 0; // Currently unused
Logan Chien61c65dc2012-02-29 03:22:30 +08002811 bool is_fast_path = compiler_->
Logan Chien7e7fabc2012-04-10 18:59:11 +08002812 ComputeInvokeInfo(callee_method_idx, oat_compilation_unit_,
Logan Chien92ad16d2012-03-18 05:48:55 +08002813 invoke_type, vtable_idx, direct_code, direct_method);
Logan Chien61c65dc2012-02-29 03:22:30 +08002814
Logan Chien7e7fabc2012-04-10 18:59:11 +08002815 // Load *this* actual parameter
Logan Chien1a121b92012-02-15 22:23:42 +08002816 llvm::Value* this_addr = NULL;
2817
2818 if (!is_static) {
2819 // Test: Is *this* parameter equal to null?
Logan Chien7e7fabc2012-04-10 18:59:11 +08002820 this_addr = (arg_fmt == kArgReg) ?
2821 EmitLoadDalvikReg(dec_insn.arg[0], kObject, kAccurate):
2822 EmitLoadDalvikReg(dec_insn.vC + 0, kObject, kAccurate);
2823
Logan Chien1a121b92012-02-15 22:23:42 +08002824 EmitGuard_NullPointerException(dex_pc, this_addr);
2825 }
2826
Logan Chien7e7fabc2012-04-10 18:59:11 +08002827 // Load the method object
TDYa1274e42a592012-04-10 20:13:54 -07002828 llvm::Value* callee_method_object_addr = NULL;
Logan Chien7e7fabc2012-04-10 18:59:11 +08002829
2830 if (!is_fast_path) {
TDYa1274e42a592012-04-10 20:13:54 -07002831 callee_method_object_addr =
Logan Chien7e7fabc2012-04-10 18:59:11 +08002832 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx, invoke_type,
2833 this_addr, dex_pc, is_fast_path);
2834 } else {
2835 switch (invoke_type) {
2836 case kStatic:
2837 case kDirect:
TDYa1274e42a592012-04-10 20:13:54 -07002838 callee_method_object_addr =
Logan Chien7e7fabc2012-04-10 18:59:11 +08002839 EmitLoadSDCalleeMethodObjectAddr(callee_method_idx);
2840 break;
2841
2842 case kVirtual:
2843 DCHECK(vtable_idx != -1);
TDYa1274e42a592012-04-10 20:13:54 -07002844 callee_method_object_addr =
Logan Chien7e7fabc2012-04-10 18:59:11 +08002845 EmitLoadVirtualCalleeMethodObjectAddr(vtable_idx, this_addr);
2846 break;
2847
2848 case kSuper:
2849 LOG(FATAL) << "invoke-super should be promoted to invoke-direct in "
2850 "the fast path.";
2851 break;
2852
2853 case kInterface:
TDYa1274e42a592012-04-10 20:13:54 -07002854 callee_method_object_addr =
Logan Chien7e7fabc2012-04-10 18:59:11 +08002855 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx,
2856 invoke_type, this_addr,
2857 dex_pc, is_fast_path);
2858 break;
2859 }
2860 }
2861
TDYa1270b686e52012-04-09 22:43:35 -07002862#if 0
Logan Chien7e7fabc2012-04-10 18:59:11 +08002863 llvm::Value* code_field_offset_value =
2864 irb_.getPtrEquivInt(Method::GetCodeOffset().Int32Value());
TDYa12785321912012-04-01 15:24:56 -07002865
Logan Chien7e7fabc2012-04-10 18:59:11 +08002866 llvm::Value* code_field_addr =
2867 irb_.CreatePtrDisp(callee_method_object_addr, code_field_offset_value,
2868 irb_.getInt8Ty()->getPointerTo()->getPointerTo());
2869
2870 llvm::Value* code_addr = irb_.CreateLoad(code_field_addr);
2871#else
2872 // TODO: Remove this after we solve the link and trampoline related problems.
2873 llvm::Value* code_addr =
2874 EmitFixStub(callee_method_object_addr, callee_method_idx, is_static);
2875
2876 EmitGuard_ExceptionLandingPad(dex_pc);
TDYa1270b686e52012-04-09 22:43:35 -07002877#endif
TDYa12785321912012-04-01 15:24:56 -07002878
Logan Chien1a121b92012-02-15 22:23:42 +08002879 // Load the actual parameter
2880 std::vector<llvm::Value*> args;
2881
2882 args.push_back(callee_method_object_addr); // method object for callee
2883
2884 if (!is_static) {
Logan Chien7e7fabc2012-04-10 18:59:11 +08002885 DCHECK(this_addr != NULL);
Logan Chien1a121b92012-02-15 22:23:42 +08002886 args.push_back(this_addr); // "this" object for callee
2887 }
2888
2889 EmitLoadActualParameters(args, callee_method_idx, dec_insn,
Logan Chien7e7fabc2012-04-10 18:59:11 +08002890 arg_fmt, is_static);
Logan Chien1a121b92012-02-15 22:23:42 +08002891
TDYa1275bb86012012-04-11 05:57:28 -07002892#if 0
Logan Chien8dfcbea2012-02-17 18:50:32 +08002893 // Invoke callee
TDYa127c8dc1012012-04-19 07:03:33 -07002894 EmitUpdateDexPC(dex_pc);
Logan Chien1a121b92012-02-15 22:23:42 +08002895 llvm::Value* retval = irb_.CreateCall(code_addr, args);
2896 EmitGuard_ExceptionLandingPad(dex_pc);
2897
Logan Chien7e7fabc2012-04-10 18:59:11 +08002898 uint32_t callee_access_flags = is_static ? kAccStatic : 0;
2899 UniquePtr<OatCompilationUnit> callee_oat_compilation_unit(
2900 oat_compilation_unit_->GetCallee(callee_method_idx, callee_access_flags));
2901
2902 char ret_shorty = callee_oat_compilation_unit->GetShorty()[0];
Shih-wei Liao90d50992012-02-19 03:32:05 -08002903 if (ret_shorty != 'V') {
Logan Chien1a121b92012-02-15 22:23:42 +08002904 EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval);
2905 }
TDYa1275bb86012012-04-11 05:57:28 -07002906#else
2907 uint32_t callee_access_flags = is_static ? kAccStatic : 0;
2908 UniquePtr<OatCompilationUnit> callee_oat_compilation_unit(
2909 oat_compilation_unit_->GetCallee(callee_method_idx, callee_access_flags));
2910
2911 char ret_shorty = callee_oat_compilation_unit->GetShorty()[0];
2912
2913
TDYa127c8dc1012012-04-19 07:03:33 -07002914 EmitUpdateDexPC(dex_pc);
TDYa1275bb86012012-04-11 05:57:28 -07002915
2916
2917 llvm::BasicBlock* block_normal = CreateBasicBlockWithDexPC(dex_pc, "normal");
2918 llvm::BasicBlock* block_proxy_stub = CreateBasicBlockWithDexPC(dex_pc, "proxy");
2919 llvm::BasicBlock* block_continue = CreateBasicBlockWithDexPC(dex_pc, "cont");
2920
2921 llvm::Type* accurate_ret_type = irb_.getJType(ret_shorty, kAccurate);
2922 llvm::Value* retval_addr = NULL;
2923 if (ret_shorty != 'V') {
2924 retval_addr = irb_.CreateAlloca(accurate_ret_type);
2925 }
2926
2927
2928 // TODO: Remove this after we solve the proxy trampoline calling convention problem.
2929 llvm::Value* code_addr_int = irb_.CreatePtrToInt(code_addr, irb_.getPtrEquivIntTy());
2930 llvm::Value* proxy_stub_int = irb_.getPtrEquivInt(special_stub::kProxyStub);
2931 llvm::Value* is_proxy_stub = irb_.CreateICmpEQ(code_addr_int, proxy_stub_int);
2932 irb_.CreateCondBr(is_proxy_stub, block_proxy_stub, block_normal);
2933
2934
2935 irb_.SetInsertPoint(block_normal);
2936 {
2937 // Invoke callee
2938 llvm::Value* result = irb_.CreateCall(code_addr, args);
2939 if (ret_shorty != 'V') {
2940 irb_.CreateStore(result, retval_addr);
2941 }
2942 }
2943 irb_.CreateBr(block_continue);
2944
2945
2946 irb_.SetInsertPoint(block_proxy_stub);
2947 {
2948 llvm::Value* temp_space_addr;
2949 if (ret_shorty != 'V') {
2950 temp_space_addr = irb_.CreateAlloca(irb_.getJValueTy());
2951 args.push_back(temp_space_addr);
2952 }
2953 irb_.CreateCall(irb_.GetRuntime(ProxyInvokeHandler), args);
2954 if (ret_shorty != 'V') {
2955 llvm::Value* result_addr =
2956 irb_.CreateBitCast(temp_space_addr, accurate_ret_type->getPointerTo());
2957 llvm::Value* retval = irb_.CreateLoad(result_addr);
2958 irb_.CreateStore(retval, retval_addr);
2959 }
2960 }
2961 irb_.CreateBr(block_continue);
2962
2963
2964 irb_.SetInsertPoint(block_continue);
2965
2966 if (ret_shorty != 'V') {
2967 llvm::Value* retval = irb_.CreateLoad(retval_addr);
2968 EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval);
2969 }
2970 EmitGuard_ExceptionLandingPad(dex_pc);
2971#endif
Logan Chien1a121b92012-02-15 22:23:42 +08002972
Logan Chien70f94b42011-12-27 17:49:11 +08002973 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2974}
2975
2976
Logan Chien7e7fabc2012-04-10 18:59:11 +08002977llvm::Value* MethodCompiler::
2978EmitLoadSDCalleeMethodObjectAddr(uint32_t callee_method_idx) {
2979 llvm::Value* callee_method_object_field_addr =
2980 EmitLoadDexCacheResolvedMethodFieldAddr(callee_method_idx);
Logan Chien7caf37e2012-02-03 22:56:04 +08002981
Logan Chien7e7fabc2012-04-10 18:59:11 +08002982 return irb_.CreateLoad(callee_method_object_field_addr);
2983}
Logan Chien7caf37e2012-02-03 22:56:04 +08002984
Logan Chien7caf37e2012-02-03 22:56:04 +08002985
Logan Chien7e7fabc2012-04-10 18:59:11 +08002986llvm::Value* MethodCompiler::
2987EmitLoadVirtualCalleeMethodObjectAddr(int vtable_idx,
2988 llvm::Value* this_addr) {
2989 // Load class object of *this* pointer
2990 llvm::Constant* class_field_offset_value =
2991 irb_.getPtrEquivInt(Object::ClassOffset().Int32Value());
Logan Chien7caf37e2012-02-03 22:56:04 +08002992
Logan Chien7e7fabc2012-04-10 18:59:11 +08002993 llvm::Value* class_field_addr =
2994 irb_.CreatePtrDisp(this_addr, class_field_offset_value,
2995 irb_.getJObjectTy()->getPointerTo());
Logan Chien7caf37e2012-02-03 22:56:04 +08002996
Logan Chien7e7fabc2012-04-10 18:59:11 +08002997 llvm::Value* class_object_addr = irb_.CreateLoad(class_field_addr);
Logan Chien7caf37e2012-02-03 22:56:04 +08002998
Logan Chien7e7fabc2012-04-10 18:59:11 +08002999 // Load vtable address
3000 llvm::Constant* vtable_offset_value =
3001 irb_.getPtrEquivInt(Class::VTableOffset().Int32Value());
3002
3003 llvm::Value* vtable_field_addr =
3004 irb_.CreatePtrDisp(class_object_addr, vtable_offset_value,
3005 irb_.getJObjectTy()->getPointerTo());
3006
3007 llvm::Value* vtable_addr = irb_.CreateLoad(vtable_field_addr);
3008
3009 // Load callee method object
3010 llvm::Value* vtable_idx_value =
3011 irb_.getPtrEquivInt(static_cast<uint64_t>(vtable_idx));
3012
3013 llvm::Value* method_field_addr =
3014 EmitArrayGEP(vtable_addr, vtable_idx_value, irb_.getJObjectTy(), kObject);
3015
3016 return irb_.CreateLoad(method_field_addr);
3017}
3018
3019
3020llvm::Value* MethodCompiler::
3021EmitCallRuntimeForCalleeMethodObjectAddr(uint32_t callee_method_idx,
3022 InvokeType invoke_type,
3023 llvm::Value* this_addr,
3024 uint32_t dex_pc,
3025 bool is_fast_path) {
3026
3027 llvm::Function* runtime_func = NULL;
3028
3029 switch (invoke_type) {
3030 case kStatic:
3031 runtime_func = irb_.GetRuntime(FindStaticMethodWithAccessCheck);
3032 break;
3033
3034 case kDirect:
3035 runtime_func = irb_.GetRuntime(FindDirectMethodWithAccessCheck);
3036 break;
3037
3038 case kVirtual:
3039 runtime_func = irb_.GetRuntime(FindVirtualMethodWithAccessCheck);
3040 break;
3041
3042 case kSuper:
3043 runtime_func = irb_.GetRuntime(FindSuperMethodWithAccessCheck);
3044 break;
3045
3046 case kInterface:
3047 if (is_fast_path) {
3048 runtime_func = irb_.GetRuntime(FindInterfaceMethod);
3049 } else {
3050 runtime_func = irb_.GetRuntime(FindInterfaceMethodWithAccessCheck);
3051 }
3052 break;
3053 }
Logan Chien7caf37e2012-02-03 22:56:04 +08003054
3055 llvm::Value* callee_method_idx_value = irb_.getInt32(callee_method_idx);
3056
Logan Chien7e7fabc2012-04-10 18:59:11 +08003057 if (this_addr == NULL) {
3058 DCHECK_EQ(invoke_type, kStatic);
3059 this_addr = irb_.getJNull();
3060 }
3061
3062 llvm::Value* caller_method_object_addr = EmitLoadMethodObjectAddr();
3063
TDYa127da83d972012-04-18 00:21:49 -07003064 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
3065
TDYa127c8dc1012012-04-19 07:03:33 -07003066 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08003067
TDYa1270b686e52012-04-09 22:43:35 -07003068 llvm::Value* callee_method_object_addr =
TDYa127da83d972012-04-18 00:21:49 -07003069 irb_.CreateCall4(runtime_func,
Logan Chien7e7fabc2012-04-10 18:59:11 +08003070 callee_method_idx_value,
3071 this_addr,
TDYa127da83d972012-04-18 00:21:49 -07003072 caller_method_object_addr,
3073 thread_object_addr);
Logan Chien7caf37e2012-02-03 22:56:04 +08003074
3075 EmitGuard_ExceptionLandingPad(dex_pc);
3076
Logan Chien7e7fabc2012-04-10 18:59:11 +08003077 return callee_method_object_addr;
Logan Chien70f94b42011-12-27 17:49:11 +08003078}
3079
3080
3081void MethodCompiler::EmitInsn_Neg(uint32_t dex_pc,
3082 Instruction const* insn,
3083 JType op_jty) {
Logan Chien1b5685f2011-12-27 18:01:14 +08003084
Elliott Hughesadb8c672012-03-06 16:49:32 -08003085 DecodedInstruction dec_insn(insn);
Logan Chien1b5685f2011-12-27 18:01:14 +08003086
3087 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3088
Elliott Hughesadb8c672012-03-06 16:49:32 -08003089 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chien1b5685f2011-12-27 18:01:14 +08003090 llvm::Value* result_value = irb_.CreateNeg(src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003091 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chien1b5685f2011-12-27 18:01:14 +08003092
Logan Chien70f94b42011-12-27 17:49:11 +08003093 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3094}
3095
3096
3097void MethodCompiler::EmitInsn_Not(uint32_t dex_pc,
3098 Instruction const* insn,
3099 JType op_jty) {
Logan Chiene53750d2011-12-27 18:02:27 +08003100
Elliott Hughesadb8c672012-03-06 16:49:32 -08003101 DecodedInstruction dec_insn(insn);
Logan Chiene53750d2011-12-27 18:02:27 +08003102
3103 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3104
Elliott Hughesadb8c672012-03-06 16:49:32 -08003105 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chiene53750d2011-12-27 18:02:27 +08003106 llvm::Value* result_value =
3107 irb_.CreateXor(src_value, static_cast<uint64_t>(-1));
3108
Elliott Hughesadb8c672012-03-06 16:49:32 -08003109 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chiene53750d2011-12-27 18:02:27 +08003110
Logan Chien70f94b42011-12-27 17:49:11 +08003111 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3112}
3113
3114
3115void MethodCompiler::EmitInsn_SExt(uint32_t dex_pc,
3116 Instruction const* insn) {
Logan Chien61752ad2011-12-27 18:03:51 +08003117
Elliott Hughesadb8c672012-03-06 16:49:32 -08003118 DecodedInstruction dec_insn(insn);
Logan Chien61752ad2011-12-27 18:03:51 +08003119
Elliott Hughesadb8c672012-03-06 16:49:32 -08003120 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chien61752ad2011-12-27 18:03:51 +08003121 llvm::Value* result_value = irb_.CreateSExt(src_value, irb_.getJLongTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003122 EmitStoreDalvikReg(dec_insn.vA, kLong, kAccurate, result_value);
Logan Chien61752ad2011-12-27 18:03:51 +08003123
Logan Chien70f94b42011-12-27 17:49:11 +08003124 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3125}
3126
3127
3128void MethodCompiler::EmitInsn_Trunc(uint32_t dex_pc,
3129 Instruction const* insn) {
Logan Chien17a57662011-12-27 18:05:14 +08003130
Elliott Hughesadb8c672012-03-06 16:49:32 -08003131 DecodedInstruction dec_insn(insn);
Logan Chien17a57662011-12-27 18:05:14 +08003132
Elliott Hughesadb8c672012-03-06 16:49:32 -08003133 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kLong, kAccurate);
Logan Chien17a57662011-12-27 18:05:14 +08003134 llvm::Value* result_value = irb_.CreateTrunc(src_value, irb_.getJIntTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003135 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chien17a57662011-12-27 18:05:14 +08003136
Logan Chien70f94b42011-12-27 17:49:11 +08003137 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3138}
3139
3140
3141void MethodCompiler::EmitInsn_TruncAndSExt(uint32_t dex_pc,
3142 Instruction const* insn,
3143 unsigned N) {
Logan Chienb6744c52011-12-27 18:06:26 +08003144
Elliott Hughesadb8c672012-03-06 16:49:32 -08003145 DecodedInstruction dec_insn(insn);
Logan Chienb6744c52011-12-27 18:06:26 +08003146
Elliott Hughesadb8c672012-03-06 16:49:32 -08003147 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chienb6744c52011-12-27 18:06:26 +08003148
3149 llvm::Value* trunc_value =
3150 irb_.CreateTrunc(src_value, llvm::Type::getIntNTy(*context_, N));
3151
3152 llvm::Value* result_value = irb_.CreateSExt(trunc_value, irb_.getJIntTy());
3153
Elliott Hughesadb8c672012-03-06 16:49:32 -08003154 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chienb6744c52011-12-27 18:06:26 +08003155
Logan Chien70f94b42011-12-27 17:49:11 +08003156 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3157}
3158
3159
3160void MethodCompiler::EmitInsn_TruncAndZExt(uint32_t dex_pc,
3161 Instruction const* insn,
3162 unsigned N) {
Logan Chienb6744c52011-12-27 18:06:26 +08003163
Elliott Hughesadb8c672012-03-06 16:49:32 -08003164 DecodedInstruction dec_insn(insn);
Logan Chienb6744c52011-12-27 18:06:26 +08003165
Elliott Hughesadb8c672012-03-06 16:49:32 -08003166 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chienb6744c52011-12-27 18:06:26 +08003167
3168 llvm::Value* trunc_value =
3169 irb_.CreateTrunc(src_value, llvm::Type::getIntNTy(*context_, N));
3170
3171 llvm::Value* result_value = irb_.CreateZExt(trunc_value, irb_.getJIntTy());
3172
Elliott Hughesadb8c672012-03-06 16:49:32 -08003173 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chienb6744c52011-12-27 18:06:26 +08003174
Logan Chien70f94b42011-12-27 17:49:11 +08003175 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3176}
3177
3178
3179void MethodCompiler::EmitInsn_FNeg(uint32_t dex_pc,
3180 Instruction const* insn,
3181 JType op_jty) {
Logan Chien7a48b092011-12-27 18:07:45 +08003182
Elliott Hughesadb8c672012-03-06 16:49:32 -08003183 DecodedInstruction dec_insn(insn);
Logan Chien7a48b092011-12-27 18:07:45 +08003184
3185 DCHECK(op_jty == kFloat || op_jty == kDouble) << op_jty;
3186
Elliott Hughesadb8c672012-03-06 16:49:32 -08003187 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chien7a48b092011-12-27 18:07:45 +08003188 llvm::Value* result_value = irb_.CreateFNeg(src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003189 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chien7a48b092011-12-27 18:07:45 +08003190
Logan Chien70f94b42011-12-27 17:49:11 +08003191 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3192}
3193
3194
3195void MethodCompiler::EmitInsn_IntToFP(uint32_t dex_pc,
3196 Instruction const* insn,
3197 JType src_jty,
3198 JType dest_jty) {
Logan Chien62dd4532011-12-27 18:09:00 +08003199
Elliott Hughesadb8c672012-03-06 16:49:32 -08003200 DecodedInstruction dec_insn(insn);
Logan Chien62dd4532011-12-27 18:09:00 +08003201
3202 DCHECK(src_jty == kInt || src_jty == kLong) << src_jty;
3203 DCHECK(dest_jty == kFloat || dest_jty == kDouble) << dest_jty;
3204
Elliott Hughesadb8c672012-03-06 16:49:32 -08003205 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, src_jty, kAccurate);
Logan Chien62dd4532011-12-27 18:09:00 +08003206 llvm::Type* dest_type = irb_.getJType(dest_jty, kAccurate);
3207 llvm::Value* dest_value = irb_.CreateSIToFP(src_value, dest_type);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003208 EmitStoreDalvikReg(dec_insn.vA, dest_jty, kAccurate, dest_value);
Logan Chien62dd4532011-12-27 18:09:00 +08003209
Logan Chien70f94b42011-12-27 17:49:11 +08003210 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3211}
3212
3213
3214void MethodCompiler::EmitInsn_FPToInt(uint32_t dex_pc,
3215 Instruction const* insn,
3216 JType src_jty,
TDYa127a4746872012-04-11 23:48:55 -07003217 JType dest_jty,
3218 runtime_support::RuntimeId runtime_func_id) {
Logan Chien12dc1752011-12-27 18:10:15 +08003219
Elliott Hughesadb8c672012-03-06 16:49:32 -08003220 DecodedInstruction dec_insn(insn);
Logan Chien12dc1752011-12-27 18:10:15 +08003221
3222 DCHECK(src_jty == kFloat || src_jty == kDouble) << src_jty;
3223 DCHECK(dest_jty == kInt || dest_jty == kLong) << dest_jty;
3224
Elliott Hughesadb8c672012-03-06 16:49:32 -08003225 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, src_jty, kAccurate);
TDYa127a4746872012-04-11 23:48:55 -07003226 llvm::Value* dest_value = irb_.CreateCall(irb_.GetRuntime(runtime_func_id), src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003227 EmitStoreDalvikReg(dec_insn.vA, dest_jty, kAccurate, dest_value);
Logan Chien12dc1752011-12-27 18:10:15 +08003228
Logan Chien70f94b42011-12-27 17:49:11 +08003229 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3230}
3231
3232
3233void MethodCompiler::EmitInsn_FExt(uint32_t dex_pc,
3234 Instruction const* insn) {
Logan Chienc56ded92011-12-27 18:10:57 +08003235
Elliott Hughesadb8c672012-03-06 16:49:32 -08003236 DecodedInstruction dec_insn(insn);
Logan Chienc56ded92011-12-27 18:10:57 +08003237
Elliott Hughesadb8c672012-03-06 16:49:32 -08003238 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kFloat, kAccurate);
Logan Chienc56ded92011-12-27 18:10:57 +08003239 llvm::Value* result_value = irb_.CreateFPExt(src_value, irb_.getJDoubleTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003240 EmitStoreDalvikReg(dec_insn.vA, kDouble, kAccurate, result_value);
Logan Chienc56ded92011-12-27 18:10:57 +08003241
Logan Chien70f94b42011-12-27 17:49:11 +08003242 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3243}
3244
3245
3246void MethodCompiler::EmitInsn_FTrunc(uint32_t dex_pc,
3247 Instruction const* insn) {
Logan Chien927744f2011-12-27 18:11:52 +08003248
Elliott Hughesadb8c672012-03-06 16:49:32 -08003249 DecodedInstruction dec_insn(insn);
Logan Chien927744f2011-12-27 18:11:52 +08003250
Elliott Hughesadb8c672012-03-06 16:49:32 -08003251 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kDouble, kAccurate);
Logan Chien927744f2011-12-27 18:11:52 +08003252 llvm::Value* result_value = irb_.CreateFPTrunc(src_value, irb_.getJFloatTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003253 EmitStoreDalvikReg(dec_insn.vA, kFloat, kAccurate, result_value);
Logan Chien927744f2011-12-27 18:11:52 +08003254
Logan Chien70f94b42011-12-27 17:49:11 +08003255 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3256}
3257
3258
3259void MethodCompiler::EmitInsn_IntArithm(uint32_t dex_pc,
3260 Instruction const* insn,
3261 IntArithmKind arithm,
3262 JType op_jty,
3263 bool is_2addr) {
Logan Chienc3f7d962011-12-27 18:13:18 +08003264
Elliott Hughesadb8c672012-03-06 16:49:32 -08003265 DecodedInstruction dec_insn(insn);
Logan Chienc3f7d962011-12-27 18:13:18 +08003266
3267 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3268
3269 llvm::Value* src1_value;
3270 llvm::Value* src2_value;
3271
3272 if (is_2addr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003273 src1_value = EmitLoadDalvikReg(dec_insn.vA, op_jty, kAccurate);
3274 src2_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chienc3f7d962011-12-27 18:13:18 +08003275 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003276 src1_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
3277 src2_value = EmitLoadDalvikReg(dec_insn.vC, op_jty, kAccurate);
Logan Chienc3f7d962011-12-27 18:13:18 +08003278 }
3279
3280 llvm::Value* result_value =
3281 EmitIntArithmResultComputation(dex_pc, src1_value, src2_value,
3282 arithm, op_jty);
3283
Elliott Hughesadb8c672012-03-06 16:49:32 -08003284 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chienc3f7d962011-12-27 18:13:18 +08003285
Logan Chien70f94b42011-12-27 17:49:11 +08003286 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3287}
3288
3289
3290void MethodCompiler::EmitInsn_IntArithmImmediate(uint32_t dex_pc,
3291 Instruction const* insn,
3292 IntArithmKind arithm) {
Logan Chienc3f7d962011-12-27 18:13:18 +08003293
Elliott Hughesadb8c672012-03-06 16:49:32 -08003294 DecodedInstruction dec_insn(insn);
Logan Chienc3f7d962011-12-27 18:13:18 +08003295
Elliott Hughesadb8c672012-03-06 16:49:32 -08003296 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chienc3f7d962011-12-27 18:13:18 +08003297
Elliott Hughesadb8c672012-03-06 16:49:32 -08003298 llvm::Value* imm_value = irb_.getInt32(dec_insn.vC);
Logan Chienc3f7d962011-12-27 18:13:18 +08003299
3300 llvm::Value* result_value =
3301 EmitIntArithmResultComputation(dex_pc, src_value, imm_value, arithm, kInt);
3302
Elliott Hughesadb8c672012-03-06 16:49:32 -08003303 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chienc3f7d962011-12-27 18:13:18 +08003304
Logan Chien70f94b42011-12-27 17:49:11 +08003305 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3306}
3307
3308
Logan Chienc3f7d962011-12-27 18:13:18 +08003309llvm::Value*
3310MethodCompiler::EmitIntArithmResultComputation(uint32_t dex_pc,
3311 llvm::Value* lhs,
3312 llvm::Value* rhs,
3313 IntArithmKind arithm,
3314 JType op_jty) {
3315 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3316
3317 switch (arithm) {
3318 case kIntArithm_Add:
3319 return irb_.CreateAdd(lhs, rhs);
3320
3321 case kIntArithm_Sub:
3322 return irb_.CreateSub(lhs, rhs);
3323
3324 case kIntArithm_Mul:
3325 return irb_.CreateMul(lhs, rhs);
3326
3327 case kIntArithm_Div:
Logan Chienc3f7d962011-12-27 18:13:18 +08003328 case kIntArithm_Rem:
TDYa127f8641ce2012-04-02 06:40:40 -07003329 return EmitIntDivRemResultComputation(dex_pc, lhs, rhs, arithm, op_jty);
Logan Chienc3f7d962011-12-27 18:13:18 +08003330
3331 case kIntArithm_And:
3332 return irb_.CreateAnd(lhs, rhs);
3333
3334 case kIntArithm_Or:
3335 return irb_.CreateOr(lhs, rhs);
3336
3337 case kIntArithm_Xor:
3338 return irb_.CreateXor(lhs, rhs);
3339
Logan Chienc3f7d962011-12-27 18:13:18 +08003340 default:
3341 LOG(FATAL) << "Unknown integer arithmetic kind: " << arithm;
3342 return NULL;
3343 }
3344}
3345
3346
TDYa127f8641ce2012-04-02 06:40:40 -07003347llvm::Value*
3348MethodCompiler::EmitIntDivRemResultComputation(uint32_t dex_pc,
3349 llvm::Value* dividend,
3350 llvm::Value* divisor,
3351 IntArithmKind arithm,
3352 JType op_jty) {
3353 // Throw exception if the divisor is 0.
3354 EmitGuard_DivZeroException(dex_pc, divisor, op_jty);
3355
3356 // Check the special case: MININT / -1 = MININT
3357 // That case will cause overflow, which is undefined behavior in llvm.
3358 // So we check the divisor is -1 or not, if the divisor is -1, we do
3359 // the special path to avoid undefined behavior.
3360 llvm::Type* op_type = irb_.getJType(op_jty, kAccurate);
3361 llvm::Value* zero = irb_.getJZero(op_jty);
3362 llvm::Value* neg_one = llvm::ConstantInt::getSigned(op_type, -1);
3363 llvm::Value* result = irb_.CreateAlloca(op_type);
3364
3365 llvm::BasicBlock* eq_neg_one = CreateBasicBlockWithDexPC(dex_pc, "eq_neg_one");
3366 llvm::BasicBlock* ne_neg_one = CreateBasicBlockWithDexPC(dex_pc, "ne_neg_one");
3367 llvm::BasicBlock* neg_one_cont = CreateBasicBlockWithDexPC(dex_pc, "neg_one_cont");
3368
3369 llvm::Value* is_equal_neg_one = EmitConditionResult(divisor, neg_one, kCondBranch_EQ);
3370 irb_.CreateCondBr(is_equal_neg_one, eq_neg_one, ne_neg_one);
3371
3372 // If divisor == -1
3373 irb_.SetInsertPoint(eq_neg_one);
3374 llvm::Value* eq_result;
3375 if (arithm == kIntArithm_Div) {
3376 // We can just change from "dividend div -1" to "neg dividend".
3377 // The sub don't care the sign/unsigned because of two's complement representation.
3378 // And the behavior is what we want:
3379 // -(2^n) (2^n)-1
3380 // MININT < k <= MAXINT -> mul k -1 = -k
3381 // MININT == k -> mul k -1 = k
3382 //
3383 // LLVM use sub to represent 'neg'
3384 eq_result = irb_.CreateSub(zero, dividend);
3385 } else {
3386 // Everything modulo -1 will be 0.
3387 eq_result = zero;
3388 }
3389 irb_.CreateStore(eq_result, result);
3390 irb_.CreateBr(neg_one_cont);
3391
3392 // If divisor != -1, just do the division.
3393 irb_.SetInsertPoint(ne_neg_one);
3394 llvm::Value* ne_result;
3395 if (arithm == kIntArithm_Div) {
3396 ne_result = irb_.CreateSDiv(dividend, divisor);
3397 } else {
3398 ne_result = irb_.CreateSRem(dividend, divisor);
3399 }
3400 irb_.CreateStore(ne_result, result);
3401 irb_.CreateBr(neg_one_cont);
3402
3403 irb_.SetInsertPoint(neg_one_cont);
3404 return irb_.CreateLoad(result);
3405}
3406
3407
Logan Chien5539ad02012-04-02 14:36:55 +08003408void MethodCompiler::EmitInsn_IntShiftArithm(uint32_t dex_pc,
3409 Instruction const* insn,
3410 IntShiftArithmKind arithm,
3411 JType op_jty,
3412 bool is_2addr) {
3413
3414 DecodedInstruction dec_insn(insn);
3415
3416 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3417
3418 llvm::Value* src1_value;
3419 llvm::Value* src2_value;
3420
3421 // NOTE: The 2nd operand of the shift arithmetic instruction is
3422 // 32-bit integer regardless of the 1st operand.
3423 if (is_2addr) {
3424 src1_value = EmitLoadDalvikReg(dec_insn.vA, op_jty, kAccurate);
3425 src2_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
3426 } else {
3427 src1_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
3428 src2_value = EmitLoadDalvikReg(dec_insn.vC, kInt, kAccurate);
3429 }
3430
3431 llvm::Value* result_value =
3432 EmitIntShiftArithmResultComputation(dex_pc, src1_value, src2_value,
3433 arithm, op_jty);
3434
3435 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
3436
3437 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3438}
3439
3440
3441void MethodCompiler::
3442EmitInsn_IntShiftArithmImmediate(uint32_t dex_pc,
3443 Instruction const* insn,
3444 IntShiftArithmKind arithm) {
3445
3446 DecodedInstruction dec_insn(insn);
3447
3448 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
3449
3450 llvm::Value* imm_value = irb_.getInt32(dec_insn.vC);
3451
3452 llvm::Value* result_value =
3453 EmitIntShiftArithmResultComputation(dex_pc, src_value, imm_value,
3454 arithm, kInt);
3455
3456 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
3457
3458 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3459}
3460
3461
3462llvm::Value*
3463MethodCompiler::EmitIntShiftArithmResultComputation(uint32_t dex_pc,
3464 llvm::Value* lhs,
3465 llvm::Value* rhs,
3466 IntShiftArithmKind arithm,
3467 JType op_jty) {
3468 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3469
3470 if (op_jty == kInt) {
3471 rhs = irb_.CreateAnd(rhs, 0x1f);
3472 } else {
3473 llvm::Value* masked_rhs = irb_.CreateAnd(rhs, 0x3f);
3474 rhs = irb_.CreateZExt(masked_rhs, irb_.getJLongTy());
3475 }
3476
3477 switch (arithm) {
3478 case kIntArithm_Shl:
3479 return irb_.CreateShl(lhs, rhs);
3480
3481 case kIntArithm_Shr:
3482 return irb_.CreateAShr(lhs, rhs);
3483
3484 case kIntArithm_UShr:
3485 return irb_.CreateLShr(lhs, rhs);
3486
3487 default:
3488 LOG(FATAL) << "Unknown integer shift arithmetic kind: " << arithm;
3489 return NULL;
3490 }
3491}
3492
3493
Logan Chien70f94b42011-12-27 17:49:11 +08003494void MethodCompiler::EmitInsn_RSubImmediate(uint32_t dex_pc,
3495 Instruction const* insn) {
Logan Chien65c62d42011-12-27 18:14:18 +08003496
Elliott Hughesadb8c672012-03-06 16:49:32 -08003497 DecodedInstruction dec_insn(insn);
Logan Chien65c62d42011-12-27 18:14:18 +08003498
Elliott Hughesadb8c672012-03-06 16:49:32 -08003499 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
3500 llvm::Value* imm_value = irb_.getInt32(dec_insn.vC);
Logan Chien65c62d42011-12-27 18:14:18 +08003501 llvm::Value* result_value = irb_.CreateSub(imm_value, src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003502 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chien65c62d42011-12-27 18:14:18 +08003503
Logan Chien70f94b42011-12-27 17:49:11 +08003504 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3505}
3506
3507
3508void MethodCompiler::EmitInsn_FPArithm(uint32_t dex_pc,
3509 Instruction const* insn,
3510 FPArithmKind arithm,
3511 JType op_jty,
3512 bool is_2addr) {
Logan Chien76e1c792011-12-27 18:15:01 +08003513
Elliott Hughesadb8c672012-03-06 16:49:32 -08003514 DecodedInstruction dec_insn(insn);
Logan Chien76e1c792011-12-27 18:15:01 +08003515
3516 DCHECK(op_jty == kFloat || op_jty == kDouble) << op_jty;
3517
3518 llvm::Value* src1_value;
3519 llvm::Value* src2_value;
3520
3521 if (is_2addr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003522 src1_value = EmitLoadDalvikReg(dec_insn.vA, op_jty, kAccurate);
3523 src2_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chien76e1c792011-12-27 18:15:01 +08003524 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003525 src1_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
3526 src2_value = EmitLoadDalvikReg(dec_insn.vC, op_jty, kAccurate);
Logan Chien76e1c792011-12-27 18:15:01 +08003527 }
3528
3529 llvm::Value* result_value =
3530 EmitFPArithmResultComputation(dex_pc, src1_value, src2_value, arithm);
3531
Elliott Hughesadb8c672012-03-06 16:49:32 -08003532 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chien76e1c792011-12-27 18:15:01 +08003533
Logan Chien70f94b42011-12-27 17:49:11 +08003534 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3535}
3536
3537
Logan Chien76e1c792011-12-27 18:15:01 +08003538llvm::Value*
3539MethodCompiler::EmitFPArithmResultComputation(uint32_t dex_pc,
3540 llvm::Value *lhs,
3541 llvm::Value *rhs,
3542 FPArithmKind arithm) {
3543 switch (arithm) {
3544 case kFPArithm_Add:
3545 return irb_.CreateFAdd(lhs, rhs);
3546
3547 case kFPArithm_Sub:
3548 return irb_.CreateFSub(lhs, rhs);
3549
3550 case kFPArithm_Mul:
3551 return irb_.CreateFMul(lhs, rhs);
3552
3553 case kFPArithm_Div:
3554 return irb_.CreateFDiv(lhs, rhs);
3555
3556 case kFPArithm_Rem:
3557 return irb_.CreateFRem(lhs, rhs);
3558
3559 default:
3560 LOG(FATAL) << "Unknown floating-point arithmetic kind: " << arithm;
3561 return NULL;
3562 }
3563}
3564
3565
Logan Chienc3f7d962011-12-27 18:13:18 +08003566void MethodCompiler::EmitGuard_DivZeroException(uint32_t dex_pc,
3567 llvm::Value* denominator,
3568 JType op_jty) {
3569 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3570
3571 llvm::Constant* zero = irb_.getJZero(op_jty);
3572
3573 llvm::Value* equal_zero = irb_.CreateICmpEQ(denominator, zero);
3574
3575 llvm::BasicBlock* block_exception = CreateBasicBlockWithDexPC(dex_pc, "div0");
3576
3577 llvm::BasicBlock* block_continue = CreateBasicBlockWithDexPC(dex_pc, "cont");
3578
3579 irb_.CreateCondBr(equal_zero, block_exception, block_continue);
3580
3581 irb_.SetInsertPoint(block_exception);
TDYa127c8dc1012012-04-19 07:03:33 -07003582 EmitUpdateDexPC(dex_pc);
Logan Chienc3f7d962011-12-27 18:13:18 +08003583 irb_.CreateCall(irb_.GetRuntime(ThrowDivZeroException));
3584 EmitBranchExceptionLandingPad(dex_pc);
3585
3586 irb_.SetInsertPoint(block_continue);
3587}
3588
3589
Logan Chien61bb6142012-02-03 15:34:53 +08003590void MethodCompiler::EmitGuard_NullPointerException(uint32_t dex_pc,
3591 llvm::Value* object) {
3592 llvm::Value* equal_null = irb_.CreateICmpEQ(object, irb_.getJNull());
3593
3594 llvm::BasicBlock* block_exception =
3595 CreateBasicBlockWithDexPC(dex_pc, "nullp");
3596
3597 llvm::BasicBlock* block_continue =
3598 CreateBasicBlockWithDexPC(dex_pc, "cont");
3599
3600 irb_.CreateCondBr(equal_null, block_exception, block_continue);
3601
3602 irb_.SetInsertPoint(block_exception);
TDYa127c8dc1012012-04-19 07:03:33 -07003603 EmitUpdateDexPC(dex_pc);
TDYa1273f9137d2012-04-08 15:59:19 -07003604 irb_.CreateCall(irb_.GetRuntime(ThrowNullPointerException), irb_.getInt32(dex_pc));
Logan Chien61bb6142012-02-03 15:34:53 +08003605 EmitBranchExceptionLandingPad(dex_pc);
3606
3607 irb_.SetInsertPoint(block_continue);
3608}
3609
3610
Logan Chienbb4d12a2012-02-17 14:10:01 +08003611llvm::Value* MethodCompiler::EmitLoadDexCacheAddr(MemberOffset offset) {
3612 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
3613
3614 llvm::Value* dex_cache_offset_value =
3615 irb_.getPtrEquivInt(offset.Int32Value());
3616
3617 llvm::Value* dex_cache_field_addr =
3618 irb_.CreatePtrDisp(method_object_addr, dex_cache_offset_value,
3619 irb_.getJObjectTy()->getPointerTo());
3620
3621 return irb_.CreateLoad(dex_cache_field_addr);
3622}
3623
3624
Logan Chienbb4d12a2012-02-17 14:10:01 +08003625llvm::Value* MethodCompiler::
3626EmitLoadDexCacheStaticStorageFieldAddr(uint32_t type_idx) {
3627 llvm::Value* static_storage_dex_cache_addr =
3628 EmitLoadDexCacheAddr(Method::DexCacheInitializedStaticStorageOffset());
3629
3630 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
3631
3632 return EmitArrayGEP(static_storage_dex_cache_addr, type_idx_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08003633 irb_.getJObjectTy(), kObject);
Logan Chienbb4d12a2012-02-17 14:10:01 +08003634}
3635
3636
3637llvm::Value* MethodCompiler::
3638EmitLoadDexCacheResolvedTypeFieldAddr(uint32_t type_idx) {
3639 llvm::Value* resolved_type_dex_cache_addr =
3640 EmitLoadDexCacheAddr(Method::DexCacheResolvedTypesOffset());
3641
3642 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
3643
3644 return EmitArrayGEP(resolved_type_dex_cache_addr, type_idx_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08003645 irb_.getJObjectTy(), kObject);
Logan Chienbb4d12a2012-02-17 14:10:01 +08003646}
3647
3648
3649llvm::Value* MethodCompiler::
Logan Chien61c65dc2012-02-29 03:22:30 +08003650EmitLoadDexCacheResolvedMethodFieldAddr(uint32_t method_idx) {
3651 llvm::Value* resolved_method_dex_cache_addr =
3652 EmitLoadDexCacheAddr(Method::DexCacheResolvedMethodsOffset());
3653
3654 llvm::Value* method_idx_value = irb_.getPtrEquivInt(method_idx);
3655
3656 return EmitArrayGEP(resolved_method_dex_cache_addr, method_idx_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08003657 irb_.getJObjectTy(), kObject);
Logan Chien61c65dc2012-02-29 03:22:30 +08003658}
3659
3660
3661llvm::Value* MethodCompiler::
Logan Chienbb4d12a2012-02-17 14:10:01 +08003662EmitLoadDexCacheStringFieldAddr(uint32_t string_idx) {
3663 llvm::Value* string_dex_cache_addr =
3664 EmitLoadDexCacheAddr(Method::DexCacheStringsOffset());
3665
3666 llvm::Value* string_idx_value = irb_.getPtrEquivInt(string_idx);
3667
3668 return EmitArrayGEP(string_dex_cache_addr, string_idx_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08003669 irb_.getJObjectTy(), kObject);
Logan Chienbb4d12a2012-02-17 14:10:01 +08003670}
3671
3672
Logan Chien83426162011-12-09 09:29:50 +08003673CompiledMethod *MethodCompiler::Compile() {
Logan Chien0b827102011-12-20 19:46:14 +08003674 // Code generation
3675 CreateFunction();
3676
3677 EmitPrologue();
3678 EmitInstructions();
Logan Chienc670a8d2011-12-20 21:25:56 +08003679 EmitPrologueLastBranch();
Logan Chien0b827102011-12-20 19:46:14 +08003680
Logan Chiend6c239a2011-12-23 15:11:45 +08003681 // Verify the generated bitcode
3682 llvm::verifyFunction(*func_, llvm::PrintMessageAction);
3683
Logan Chien8b977d32012-02-21 19:14:55 +08003684 // Add the memory usage approximation of the compilation unit
3685 cunit_->AddMemUsageApproximation(code_item_->insns_size_in_code_units_ * 900);
3686 // NOTE: From statistic, the bitcode size is 4.5 times bigger than the
3687 // Dex file. Besides, we have to convert the code unit into bytes.
3688 // Thus, we got our magic number 9.
3689
Logan Chien110bcba2012-04-16 19:11:28 +08003690 CompiledMethod* compiled_method =
3691 new CompiledMethod(cunit_->GetInstructionSet(),
3692 cunit_->GetElfIndex(),
3693 elf_func_idx_);
3694
3695 cunit_->RegisterCompiledMethod(func_, compiled_method);
3696
3697 return compiled_method;
Logan Chien0b827102011-12-20 19:46:14 +08003698}
3699
3700
3701llvm::Value* MethodCompiler::EmitLoadMethodObjectAddr() {
3702 return func_->arg_begin();
Shih-wei Liaod1fec812012-02-13 09:51:10 -08003703}
Logan Chien83426162011-12-09 09:29:50 +08003704
3705
Logan Chien5bcc04e2012-01-30 14:15:12 +08003706void MethodCompiler::EmitBranchExceptionLandingPad(uint32_t dex_pc) {
3707 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
3708 irb_.CreateBr(lpad);
3709 } else {
3710 irb_.CreateBr(GetUnwindBasicBlock());
3711 }
3712}
3713
3714
3715void MethodCompiler::EmitGuard_ExceptionLandingPad(uint32_t dex_pc) {
3716 llvm::Value* exception_pending =
3717 irb_.CreateCall(irb_.GetRuntime(IsExceptionPending));
3718
3719 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
3720
3721 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
3722 irb_.CreateCondBr(exception_pending, lpad, block_cont);
3723 } else {
3724 irb_.CreateCondBr(exception_pending, GetUnwindBasicBlock(), block_cont);
3725 }
3726
3727 irb_.SetInsertPoint(block_cont);
3728}
3729
3730
Logan Chien924072f2012-01-30 15:07:24 +08003731void MethodCompiler::EmitGuard_GarbageCollectionSuspend(uint32_t dex_pc) {
3732 llvm::Value* runtime_func = irb_.GetRuntime(TestSuspend);
TDYa127c8dc1012012-04-19 07:03:33 -07003733 EmitUpdateDexPC(dex_pc);
Logan Chien924072f2012-01-30 15:07:24 +08003734 irb_.CreateCall(runtime_func);
3735
3736 EmitGuard_ExceptionLandingPad(dex_pc);
3737}
3738
3739
Logan Chiend6c239a2011-12-23 15:11:45 +08003740llvm::BasicBlock* MethodCompiler::
3741CreateBasicBlockWithDexPC(uint32_t dex_pc, char const* postfix) {
3742 std::string name;
3743
3744 if (postfix) {
3745 StringAppendF(&name, "B%u.%s", dex_pc, postfix);
3746 } else {
3747 StringAppendF(&name, "B%u", dex_pc);
3748 }
3749
3750 return llvm::BasicBlock::Create(*context_, name, func_);
3751}
3752
3753
3754llvm::BasicBlock* MethodCompiler::GetBasicBlock(uint32_t dex_pc) {
3755 DCHECK(dex_pc < code_item_->insns_size_in_code_units_);
3756
3757 llvm::BasicBlock* basic_block = basic_blocks_[dex_pc];
3758
3759 if (!basic_block) {
3760 basic_block = CreateBasicBlockWithDexPC(dex_pc);
3761 basic_blocks_[dex_pc] = basic_block;
3762 }
3763
3764 return basic_block;
3765}
3766
3767
3768llvm::BasicBlock*
3769MethodCompiler::GetNextBasicBlock(uint32_t dex_pc) {
3770 Instruction const* insn = Instruction::At(code_item_->insns_ + dex_pc);
3771 return GetBasicBlock(dex_pc + insn->SizeInCodeUnits());
3772}
3773
3774
Logan Chien5bcc04e2012-01-30 14:15:12 +08003775int32_t MethodCompiler::GetTryItemOffset(uint32_t dex_pc) {
3776 // TODO: Since we are emitting the dex instructions in ascending order
3777 // w.r.t. address, we can cache the lastest try item offset so that we
3778 // don't have to do binary search for every query.
3779
3780 int32_t min = 0;
3781 int32_t max = code_item_->tries_size_ - 1;
3782
3783 while (min <= max) {
3784 int32_t mid = min + (max - min) / 2;
3785
3786 DexFile::TryItem const* ti = DexFile::GetTryItems(*code_item_, mid);
3787 uint32_t start = ti->start_addr_;
3788 uint32_t end = start + ti->insn_count_;
3789
3790 if (dex_pc < start) {
3791 max = mid - 1;
3792 } else if (dex_pc >= end) {
3793 min = mid + 1;
3794 } else {
3795 return mid; // found
3796 }
3797 }
3798
3799 return -1; // not found
3800}
3801
3802
3803llvm::BasicBlock* MethodCompiler::GetLandingPadBasicBlock(uint32_t dex_pc) {
3804 // Find the try item for this address in this method
3805 int32_t ti_offset = GetTryItemOffset(dex_pc);
3806
3807 if (ti_offset == -1) {
3808 return NULL; // No landing pad is available for this address.
3809 }
3810
3811 // Check for the existing landing pad basic block
3812 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
3813 llvm::BasicBlock* block_lpad = basic_block_landing_pads_[ti_offset];
3814
3815 if (block_lpad) {
3816 // We have generated landing pad for this try item already. Return the
3817 // same basic block.
3818 return block_lpad;
3819 }
3820
3821 // Get try item from code item
3822 DexFile::TryItem const* ti = DexFile::GetTryItems(*code_item_, ti_offset);
3823
3824 // Create landing pad basic block
3825 block_lpad = llvm::BasicBlock::Create(*context_,
3826 StringPrintf("lpad%d", ti_offset),
3827 func_);
3828
3829 // Change IRBuilder insert point
3830 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3831 irb_.SetInsertPoint(block_lpad);
3832
3833 // Find catch block with matching type
3834 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
3835
3836 // TODO: Maybe passing try item offset will be a better idea? For now,
3837 // we are passing dex_pc, so that we can use existing runtime support
3838 // function directly. However, in the runtime supporting function we
3839 // have to search for try item with binary search which can be
3840 // eliminated.
3841 llvm::Value* dex_pc_value = irb_.getInt32(ti->start_addr_);
3842
3843 llvm::Value* catch_handler_index_value =
3844 irb_.CreateCall2(irb_.GetRuntime(FindCatchBlock),
3845 method_object_addr, dex_pc_value);
3846
3847 // Switch instruction (Go to unwind basic block by default)
3848 llvm::SwitchInst* sw =
3849 irb_.CreateSwitch(catch_handler_index_value, GetUnwindBasicBlock());
3850
3851 // Cases with matched catch block
3852 CatchHandlerIterator iter(*code_item_, ti->start_addr_);
3853
3854 for (uint32_t c = 0; iter.HasNext(); iter.Next(), ++c) {
3855 sw->addCase(irb_.getInt32(c), GetBasicBlock(iter.GetHandlerAddress()));
3856 }
3857
3858 // Restore the orignal insert point for IRBuilder
3859 irb_.restoreIP(irb_ip_original);
3860
3861 // Cache this landing pad
3862 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
3863 basic_block_landing_pads_[ti_offset] = block_lpad;
3864
3865 return block_lpad;
3866}
3867
3868
3869llvm::BasicBlock* MethodCompiler::GetUnwindBasicBlock() {
3870 // Check the existing unwinding baisc block block
3871 if (basic_block_unwind_ != NULL) {
3872 return basic_block_unwind_;
3873 }
3874
3875 // Create new basic block for unwinding
3876 basic_block_unwind_ =
3877 llvm::BasicBlock::Create(*context_, "exception_unwind", func_);
3878
3879 // Change IRBuilder insert point
3880 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3881 irb_.SetInsertPoint(basic_block_unwind_);
3882
Logan Chien8dfcbea2012-02-17 18:50:32 +08003883 // Pop the shadow frame
3884 EmitPopShadowFrame();
3885
Logan Chien5bcc04e2012-01-30 14:15:12 +08003886 // Emit the code to return default value (zero) for the given return type.
Logan Chiendd361c92012-04-10 23:40:37 +08003887 char ret_shorty = oat_compilation_unit_->GetShorty()[0];
Logan Chien5bcc04e2012-01-30 14:15:12 +08003888 if (ret_shorty == 'V') {
3889 irb_.CreateRetVoid();
3890 } else {
3891 irb_.CreateRet(irb_.getJZero(ret_shorty));
3892 }
3893
3894 // Restore the orignal insert point for IRBuilder
3895 irb_.restoreIP(irb_ip_original);
3896
3897 return basic_block_unwind_;
3898}
3899
3900
Logan Chienc670a8d2011-12-20 21:25:56 +08003901llvm::Value* MethodCompiler::AllocDalvikLocalVarReg(RegCategory cat,
3902 uint32_t reg_idx) {
3903
3904 // Save current IR builder insert point
3905 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3906
3907 // Alloca
3908 llvm::Value* reg_addr = NULL;
3909
3910 switch (cat) {
3911 case kRegCat1nr:
3912 irb_.SetInsertPoint(basic_block_reg_alloca_);
3913 reg_addr = irb_.CreateAlloca(irb_.getJIntTy(), 0,
3914 StringPrintf("r%u", reg_idx));
3915
3916 irb_.SetInsertPoint(basic_block_reg_zero_init_);
3917 irb_.CreateStore(irb_.getJInt(0), reg_addr);
3918 break;
3919
3920 case kRegCat2:
3921 irb_.SetInsertPoint(basic_block_reg_alloca_);
3922 reg_addr = irb_.CreateAlloca(irb_.getJLongTy(), 0,
3923 StringPrintf("w%u", reg_idx));
3924
3925 irb_.SetInsertPoint(basic_block_reg_zero_init_);
3926 irb_.CreateStore(irb_.getJLong(0), reg_addr);
3927 break;
3928
3929 case kRegObject:
Logan Chien8dfcbea2012-02-17 18:50:32 +08003930 {
3931 irb_.SetInsertPoint(basic_block_shadow_frame_alloca_);
Logan Chienc670a8d2011-12-20 21:25:56 +08003932
Logan Chien8dfcbea2012-02-17 18:50:32 +08003933 llvm::Value* gep_index[] = {
3934 irb_.getInt32(0), // No pointer displacement
3935 irb_.getInt32(1), // SIRT
3936 irb_.getInt32(reg_idx) // Pointer field
3937 };
3938
3939 reg_addr = irb_.CreateGEP(shadow_frame_, gep_index,
3940 StringPrintf("p%u", reg_idx));
3941
3942 irb_.SetInsertPoint(basic_block_reg_zero_init_);
3943 irb_.CreateStore(irb_.getJNull(), reg_addr);
3944 }
Logan Chienc670a8d2011-12-20 21:25:56 +08003945 break;
3946
3947 default:
3948 LOG(FATAL) << "Unknown register category for allocation: " << cat;
3949 }
3950
3951 // Restore IRBuilder insert point
3952 irb_.restoreIP(irb_ip_original);
3953
3954 DCHECK_NE(reg_addr, static_cast<llvm::Value*>(NULL));
3955 return reg_addr;
3956}
3957
3958
3959llvm::Value* MethodCompiler::AllocDalvikRetValReg(RegCategory cat) {
3960 // Save current IR builder insert point
3961 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3962
3963 // Alloca
3964 llvm::Value* reg_addr = NULL;
3965
3966 switch (cat) {
3967 case kRegCat1nr:
3968 irb_.SetInsertPoint(basic_block_reg_alloca_);
3969 reg_addr = irb_.CreateAlloca(irb_.getJIntTy(), 0, "r_res");
3970 break;
3971
3972 case kRegCat2:
3973 irb_.SetInsertPoint(basic_block_reg_alloca_);
3974 reg_addr = irb_.CreateAlloca(irb_.getJLongTy(), 0, "w_res");
3975 break;
3976
3977 case kRegObject:
3978 irb_.SetInsertPoint(basic_block_reg_alloca_);
3979 reg_addr = irb_.CreateAlloca(irb_.getJObjectTy(), 0, "p_res");
3980 break;
3981
3982 default:
3983 LOG(FATAL) << "Unknown register category for allocation: " << cat;
3984 }
3985
3986 // Restore IRBuilder insert point
3987 irb_.restoreIP(irb_ip_original);
3988
3989 DCHECK_NE(reg_addr, static_cast<llvm::Value*>(NULL));
3990 return reg_addr;
3991}
3992
3993
Logan Chien8dfcbea2012-02-17 18:50:32 +08003994void MethodCompiler::EmitPopShadowFrame() {
3995 irb_.CreateCall(irb_.GetRuntime(PopShadowFrame));
3996}
3997
3998
TDYa127c8dc1012012-04-19 07:03:33 -07003999void MethodCompiler::EmitUpdateDexPC(uint32_t dex_pc) {
4000 irb_.StoreToObjectOffset(shadow_frame_,
4001 ShadowFrame::DexPCOffset(),
4002 irb_.getInt32(dex_pc));
Logan Chien8dfcbea2012-02-17 18:50:32 +08004003}
4004
4005
Logan Chien83426162011-12-09 09:29:50 +08004006} // namespace compiler_llvm
4007} // namespace art