blob: c1c9e19a5e4f7df01105216095db856c21f7a9be [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"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080029#include "stl_util.h"
Logan Chien0b827102011-12-20 19:46:14 +080030#include "stringprintf.h"
31#include "utils_llvm.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080032
33#include <iomanip>
34
35#include <llvm/Analysis/Verifier.h>
Logan Chienc670a8d2011-12-20 21:25:56 +080036#include <llvm/BasicBlock.h>
Shih-wei Liaod1fec812012-02-13 09:51:10 -080037#include <llvm/Function.h>
Logan Chiena85fb2f2012-01-16 12:52:56 +080038#include <llvm/GlobalVariable.h>
39#include <llvm/Intrinsics.h>
Shih-wei Liaod1fec812012-02-13 09:51:10 -080040
Logan Chien83426162011-12-09 09:29:50 +080041namespace art {
42namespace compiler_llvm {
Shih-wei Liaod1fec812012-02-13 09:51:10 -080043
Logan Chien42e0e152012-01-13 15:42:36 +080044using namespace runtime_support;
45
Shih-wei Liaod1fec812012-02-13 09:51:10 -080046
Logan Chien8b977d32012-02-21 19:14:55 +080047MethodCompiler::MethodCompiler(CompilationUnit* cunit,
Logan Chien83426162011-12-09 09:29:50 +080048 Compiler* compiler,
Logan Chien4dd96f52012-02-29 01:26:58 +080049 OatCompilationUnit* oat_compilation_unit)
Logan Chien8b977d32012-02-21 19:14:55 +080050 : cunit_(cunit), compiler_(compiler),
51 class_linker_(oat_compilation_unit->class_linker_),
52 class_loader_(oat_compilation_unit->class_loader_),
53 dex_file_(oat_compilation_unit->dex_file_),
54 dex_cache_(oat_compilation_unit->dex_cache_),
55 code_item_(oat_compilation_unit->code_item_),
56 oat_compilation_unit_(oat_compilation_unit),
57 method_(dex_cache_->GetResolvedMethod(oat_compilation_unit->method_idx_)),
58 method_helper_(method_),
59 method_idx_(oat_compilation_unit->method_idx_),
60 access_flags_(oat_compilation_unit->access_flags_),
61 module_(cunit->GetModule()),
62 context_(cunit->GetLLVMContext()),
63 irb_(*cunit->GetIRBuilder()), func_(NULL), retval_reg_(NULL),
64 basic_block_reg_alloca_(NULL), basic_block_shadow_frame_alloca_(NULL),
65 basic_block_reg_zero_init_(NULL), basic_block_reg_arg_init_(NULL),
66 basic_blocks_(code_item_->insns_size_in_code_units_),
67 basic_block_landing_pads_(code_item_->tries_size_, NULL),
68 basic_block_unwind_(NULL), basic_block_unreachable_(NULL),
69 shadow_frame_(NULL) {
Shih-wei Liaod1fec812012-02-13 09:51:10 -080070}
71
72
73MethodCompiler::~MethodCompiler() {
Logan Chienc670a8d2011-12-20 21:25:56 +080074 STLDeleteElements(&regs_);
Shih-wei Liaod1fec812012-02-13 09:51:10 -080075}
76
77
Logan Chien0b827102011-12-20 19:46:14 +080078void MethodCompiler::CreateFunction() {
79 // LLVM function name
80 std::string func_name(LLVMLongName(method_));
81
82 // Get function type
83 llvm::FunctionType* func_type =
84 GetFunctionType(method_idx_, method_->IsStatic());
85
86 // Create function
87 func_ = llvm::Function::Create(func_type, llvm::Function::ExternalLinkage,
88 func_name, module_);
89
90 // Set argument name
91 llvm::Function::arg_iterator arg_iter(func_->arg_begin());
92 llvm::Function::arg_iterator arg_end(func_->arg_end());
93
94 DCHECK_NE(arg_iter, arg_end);
95 arg_iter->setName("method");
96 ++arg_iter;
97
98 if (!method_->IsStatic()) {
99 DCHECK_NE(arg_iter, arg_end);
100 arg_iter->setName("this");
101 ++arg_iter;
102 }
103
104 for (unsigned i = 0; arg_iter != arg_end; ++i, ++arg_iter) {
105 arg_iter->setName(StringPrintf("a%u", i));
106 }
107}
108
109
110llvm::FunctionType* MethodCompiler::GetFunctionType(uint32_t method_idx,
111 bool is_static) {
112 // Get method signature
113 DexFile::MethodId const& method_id = dex_file_->GetMethodId(method_idx);
114
Logan Chien8faf8022012-02-24 12:25:29 +0800115 uint32_t shorty_size;
Logan Chien0b827102011-12-20 19:46:14 +0800116 char const* shorty = dex_file_->GetMethodShorty(method_id, &shorty_size);
Logan Chien8faf8022012-02-24 12:25:29 +0800117 CHECK_GE(shorty_size, 1u);
Logan Chien0b827102011-12-20 19:46:14 +0800118
119 // Get return type
120 llvm::Type* ret_type = irb_.getJType(shorty[0], kAccurate);
121
122 // Get argument type
123 std::vector<llvm::Type*> args_type;
124
125 args_type.push_back(irb_.getJObjectTy()); // method object pointer
126
127 if (!is_static) {
128 args_type.push_back(irb_.getJType('L', kAccurate)); // "this" object pointer
129 }
130
Logan Chien8faf8022012-02-24 12:25:29 +0800131 for (uint32_t i = 1; i < shorty_size; ++i) {
Logan Chien0b827102011-12-20 19:46:14 +0800132 args_type.push_back(irb_.getJType(shorty[i], kAccurate));
133 }
134
135 return llvm::FunctionType::get(ret_type, args_type, false);
136}
137
138
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800139void MethodCompiler::EmitPrologue() {
Logan Chienc670a8d2011-12-20 21:25:56 +0800140 // Create basic blocks for prologue
141 basic_block_reg_alloca_ =
142 llvm::BasicBlock::Create(*context_, "prologue.alloca", func_);
143
Logan Chien8dfcbea2012-02-17 18:50:32 +0800144 basic_block_shadow_frame_alloca_ =
145 llvm::BasicBlock::Create(*context_, "prologue.shadowframe", func_);
146
Logan Chienc670a8d2011-12-20 21:25:56 +0800147 basic_block_reg_zero_init_ =
148 llvm::BasicBlock::Create(*context_, "prologue.zeroinit", func_);
149
Logan Chiend6ececa2011-12-27 16:20:15 +0800150 basic_block_reg_arg_init_ =
151 llvm::BasicBlock::Create(*context_, "prologue.arginit", func_);
152
Logan Chienc670a8d2011-12-20 21:25:56 +0800153 // Create register array
154 for (uint16_t r = 0; r < code_item_->registers_size_; ++r) {
155 regs_.push_back(DalvikReg::CreateLocalVarReg(*this, r));
156 }
157
158 retval_reg_.reset(DalvikReg::CreateRetValReg(*this));
Logan Chiend6ececa2011-12-27 16:20:15 +0800159
Logan Chien8dfcbea2012-02-17 18:50:32 +0800160 // Create Shadow Frame
161 EmitPrologueAllocShadowFrame();
162
Logan Chiend6ececa2011-12-27 16:20:15 +0800163 // Store argument to dalvik register
164 irb_.SetInsertPoint(basic_block_reg_arg_init_);
165 EmitPrologueAssignArgRegister();
166
167 // Branch to start address
168 irb_.CreateBr(GetBasicBlock(0));
Logan Chienc670a8d2011-12-20 21:25:56 +0800169}
170
171
172void MethodCompiler::EmitPrologueLastBranch() {
173 irb_.SetInsertPoint(basic_block_reg_alloca_);
Logan Chien8dfcbea2012-02-17 18:50:32 +0800174 irb_.CreateBr(basic_block_shadow_frame_alloca_);
175
176 irb_.SetInsertPoint(basic_block_shadow_frame_alloca_);
Logan Chienc670a8d2011-12-20 21:25:56 +0800177 irb_.CreateBr(basic_block_reg_zero_init_);
178
179 irb_.SetInsertPoint(basic_block_reg_zero_init_);
Logan Chiend6ececa2011-12-27 16:20:15 +0800180 irb_.CreateBr(basic_block_reg_arg_init_);
181}
182
183
Logan Chien8dfcbea2012-02-17 18:50:32 +0800184void MethodCompiler::EmitPrologueAllocShadowFrame() {
185 irb_.SetInsertPoint(basic_block_shadow_frame_alloca_);
186
187 // Allocate the shadow frame now!
188 uint32_t sirt_size = code_item_->registers_size_;
189 // TODO: registers_size_ is a bad approximation. Compute a
190 // tighter approximation at Dex verifier while performing data-flow
191 // analysis.
192
193 llvm::StructType* shadow_frame_type = irb_.getShadowFrameTy(sirt_size);
194 shadow_frame_ = irb_.CreateAlloca(shadow_frame_type);
195
196 // Zero-initialization of the shadow frame
197 llvm::ConstantAggregateZero* zero_initializer =
198 llvm::ConstantAggregateZero::get(shadow_frame_type);
199
200 irb_.CreateStore(zero_initializer, shadow_frame_);
201
202 // Variables for GetElementPtr
203 llvm::Constant* zero = irb_.getInt32(0);
204
205 llvm::Value* gep_index[] = {
206 zero, // No displacement for shadow frame pointer
207 zero, // Get the %ArtFrame data structure
208 NULL,
209 };
210
211 // Store the method pointer
212 gep_index[2] = irb_.getInt32(1);
213 llvm::Value* method_field_addr = irb_.CreateGEP(shadow_frame_, gep_index);
214 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
215 irb_.CreateStore(method_object_addr, method_field_addr);
216
217 // Store the number of the pointer slots
218 gep_index[2] = irb_.getInt32(3);
219 llvm::Value* size_field_addr = irb_.CreateGEP(shadow_frame_, gep_index);
220 llvm::ConstantInt* sirt_size_value = irb_.getInt32(sirt_size);
221 irb_.CreateStore(sirt_size_value, size_field_addr);
222
223 // Push the shadow frame
224 llvm::Value* shadow_frame_upcast =
225 irb_.CreateConstGEP2_32(shadow_frame_, 0, 0);
226
227 irb_.CreateCall(irb_.GetRuntime(PushShadowFrame), shadow_frame_upcast);
228}
229
230
Logan Chiend6ececa2011-12-27 16:20:15 +0800231void MethodCompiler::EmitPrologueAssignArgRegister() {
232 uint16_t arg_reg = code_item_->registers_size_ - code_item_->ins_size_;
233
234 llvm::Function::arg_iterator arg_iter(func_->arg_begin());
235 llvm::Function::arg_iterator arg_end(func_->arg_end());
236
237 char const* shorty = method_helper_.GetShorty();
238 int32_t shorty_size = method_helper_.GetShortyLength();
239 CHECK_LE(1, shorty_size);
240
241 ++arg_iter; // skip method object
242
243 if (!method_->IsStatic()) {
244 EmitStoreDalvikReg(arg_reg, kObject, kAccurate, arg_iter);
245 ++arg_iter;
246 ++arg_reg;
247 }
248
249 for (int32_t i = 1; i < shorty_size; ++i, ++arg_iter) {
250 EmitStoreDalvikReg(arg_reg, shorty[i], kAccurate, arg_iter);
251
252 ++arg_reg;
253 if (shorty[i] == 'J' || shorty[i] == 'D') {
254 // Wide types, such as long and double, are using a pair of registers
255 // to store the value, so we have to increase arg_reg again.
256 ++arg_reg;
257 }
258 }
259
260 DCHECK_EQ(arg_end, arg_iter);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800261}
262
263
Logan Chien83426162011-12-09 09:29:50 +0800264void MethodCompiler::EmitInstructions() {
Logan Chiend6c239a2011-12-23 15:11:45 +0800265 uint32_t dex_pc = 0;
266 while (dex_pc < code_item_->insns_size_in_code_units_) {
267 Instruction const* insn = Instruction::At(code_item_->insns_ + dex_pc);
268 EmitInstruction(dex_pc, insn);
269 dex_pc += insn->SizeInCodeUnits();
270 }
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800271}
272
273
Logan Chien83426162011-12-09 09:29:50 +0800274void MethodCompiler::EmitInstruction(uint32_t dex_pc,
275 Instruction const* insn) {
Logan Chiend6c239a2011-12-23 15:11:45 +0800276
277 // Set the IRBuilder insertion point
278 irb_.SetInsertPoint(GetBasicBlock(dex_pc));
279
Logan Chien70f94b42011-12-27 17:49:11 +0800280#define ARGS dex_pc, insn
281
282 // Dispatch the instruction
283 switch (insn->Opcode()) {
284 case Instruction::NOP:
285 EmitInsn_Nop(ARGS);
286 break;
287
288 case Instruction::MOVE:
289 case Instruction::MOVE_FROM16:
290 case Instruction::MOVE_16:
291 EmitInsn_Move(ARGS, kInt);
292 break;
293
294 case Instruction::MOVE_WIDE:
295 case Instruction::MOVE_WIDE_FROM16:
296 case Instruction::MOVE_WIDE_16:
297 EmitInsn_Move(ARGS, kLong);
298 break;
299
300 case Instruction::MOVE_OBJECT:
301 case Instruction::MOVE_OBJECT_FROM16:
302 case Instruction::MOVE_OBJECT_16:
303 EmitInsn_Move(ARGS, kObject);
304 break;
305
306 case Instruction::MOVE_RESULT:
307 EmitInsn_MoveResult(ARGS, kInt);
308 break;
309
310 case Instruction::MOVE_RESULT_WIDE:
311 EmitInsn_MoveResult(ARGS, kLong);
312 break;
313
314 case Instruction::MOVE_RESULT_OBJECT:
315 EmitInsn_MoveResult(ARGS, kObject);
316 break;
317
318 case Instruction::MOVE_EXCEPTION:
319 EmitInsn_MoveException(ARGS);
320 break;
321
322 case Instruction::RETURN_VOID:
323 EmitInsn_ReturnVoid(ARGS);
324 break;
325
326 case Instruction::RETURN:
327 case Instruction::RETURN_WIDE:
328 case Instruction::RETURN_OBJECT:
329 EmitInsn_Return(ARGS);
330 break;
331
332 case Instruction::CONST_4:
333 case Instruction::CONST_16:
334 case Instruction::CONST:
335 case Instruction::CONST_HIGH16:
336 EmitInsn_LoadConstant(ARGS, kInt);
337 break;
338
339 case Instruction::CONST_WIDE_16:
340 case Instruction::CONST_WIDE_32:
341 case Instruction::CONST_WIDE:
342 case Instruction::CONST_WIDE_HIGH16:
343 EmitInsn_LoadConstant(ARGS, kLong);
344 break;
345
346 case Instruction::CONST_STRING:
347 case Instruction::CONST_STRING_JUMBO:
348 EmitInsn_LoadConstantString(ARGS);
349 break;
350
351 case Instruction::CONST_CLASS:
352 EmitInsn_LoadConstantClass(ARGS);
353 break;
354
355 case Instruction::MONITOR_ENTER:
356 EmitInsn_MonitorEnter(ARGS);
357 break;
358
359 case Instruction::MONITOR_EXIT:
360 EmitInsn_MonitorExit(ARGS);
361 break;
362
363 case Instruction::CHECK_CAST:
364 EmitInsn_CheckCast(ARGS);
365 break;
366
367 case Instruction::INSTANCE_OF:
368 EmitInsn_InstanceOf(ARGS);
369 break;
370
371 case Instruction::ARRAY_LENGTH:
372 EmitInsn_ArrayLength(ARGS);
373 break;
374
375 case Instruction::NEW_INSTANCE:
376 EmitInsn_NewInstance(ARGS);
377 break;
378
379 case Instruction::NEW_ARRAY:
380 EmitInsn_NewArray(ARGS);
381 break;
382
383 case Instruction::FILLED_NEW_ARRAY:
384 EmitInsn_FilledNewArray(ARGS, false);
385 break;
386
387 case Instruction::FILLED_NEW_ARRAY_RANGE:
388 EmitInsn_FilledNewArray(ARGS, true);
389 break;
390
391 case Instruction::FILL_ARRAY_DATA:
392 EmitInsn_FillArrayData(ARGS);
393 break;
394
395 case Instruction::THROW:
396 EmitInsn_ThrowException(ARGS);
397 break;
398
399 case Instruction::GOTO:
400 case Instruction::GOTO_16:
401 case Instruction::GOTO_32:
402 EmitInsn_UnconditionalBranch(ARGS);
403 break;
404
405 case Instruction::PACKED_SWITCH:
406 EmitInsn_PackedSwitch(ARGS);
407 break;
408
409 case Instruction::SPARSE_SWITCH:
410 EmitInsn_SparseSwitch(ARGS);
411 break;
412
413 case Instruction::CMPL_FLOAT:
414 EmitInsn_FPCompare(ARGS, kFloat, false);
415 break;
416
417 case Instruction::CMPG_FLOAT:
418 EmitInsn_FPCompare(ARGS, kFloat, true);
419 break;
420
421 case Instruction::CMPL_DOUBLE:
422 EmitInsn_FPCompare(ARGS, kDouble, false);
423 break;
424
425 case Instruction::CMPG_DOUBLE:
426 EmitInsn_FPCompare(ARGS, kDouble, true);
427 break;
428
429 case Instruction::CMP_LONG:
430 EmitInsn_LongCompare(ARGS);
431 break;
432
433 case Instruction::IF_EQ:
434 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_EQ);
435 break;
436
437 case Instruction::IF_NE:
438 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_NE);
439 break;
440
441 case Instruction::IF_LT:
442 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_LT);
443 break;
444
445 case Instruction::IF_GE:
446 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_GE);
447 break;
448
449 case Instruction::IF_GT:
450 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_GT);
451 break;
452
453 case Instruction::IF_LE:
454 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_LE);
455 break;
456
457 case Instruction::IF_EQZ:
458 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_EQ);
459 break;
460
461 case Instruction::IF_NEZ:
462 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_NE);
463 break;
464
465 case Instruction::IF_LTZ:
466 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_LT);
467 break;
468
469 case Instruction::IF_GEZ:
470 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_GE);
471 break;
472
473 case Instruction::IF_GTZ:
474 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_GT);
475 break;
476
477 case Instruction::IF_LEZ:
478 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_LE);
479 break;
480
481 case Instruction::AGET:
482 EmitInsn_AGet(ARGS, kInt);
483 break;
484
485 case Instruction::AGET_WIDE:
486 EmitInsn_AGet(ARGS, kLong);
487 break;
488
489 case Instruction::AGET_OBJECT:
490 EmitInsn_AGet(ARGS, kObject);
491 break;
492
493 case Instruction::AGET_BOOLEAN:
494 EmitInsn_AGet(ARGS, kBoolean);
495 break;
496
497 case Instruction::AGET_BYTE:
498 EmitInsn_AGet(ARGS, kByte);
499 break;
500
501 case Instruction::AGET_CHAR:
502 EmitInsn_AGet(ARGS, kChar);
503 break;
504
505 case Instruction::AGET_SHORT:
506 EmitInsn_AGet(ARGS, kShort);
507 break;
508
509 case Instruction::APUT:
510 EmitInsn_APut(ARGS, kInt);
511 break;
512
513 case Instruction::APUT_WIDE:
514 EmitInsn_APut(ARGS, kLong);
515 break;
516
517 case Instruction::APUT_OBJECT:
518 EmitInsn_APut(ARGS, kObject);
519 break;
520
521 case Instruction::APUT_BOOLEAN:
522 EmitInsn_APut(ARGS, kBoolean);
523 break;
524
525 case Instruction::APUT_BYTE:
526 EmitInsn_APut(ARGS, kByte);
527 break;
528
529 case Instruction::APUT_CHAR:
530 EmitInsn_APut(ARGS, kChar);
531 break;
532
533 case Instruction::APUT_SHORT:
534 EmitInsn_APut(ARGS, kShort);
535 break;
536
537 case Instruction::IGET:
538 EmitInsn_IGet(ARGS, kInt);
539 break;
540
541 case Instruction::IGET_WIDE:
542 EmitInsn_IGet(ARGS, kLong);
543 break;
544
545 case Instruction::IGET_OBJECT:
546 EmitInsn_IGet(ARGS, kObject);
547 break;
548
549 case Instruction::IGET_BOOLEAN:
550 EmitInsn_IGet(ARGS, kBoolean);
551 break;
552
553 case Instruction::IGET_BYTE:
554 EmitInsn_IGet(ARGS, kByte);
555 break;
556
557 case Instruction::IGET_CHAR:
558 EmitInsn_IGet(ARGS, kChar);
559 break;
560
561 case Instruction::IGET_SHORT:
562 EmitInsn_IGet(ARGS, kShort);
563 break;
564
565 case Instruction::IPUT:
566 EmitInsn_IPut(ARGS, kInt);
567 break;
568
569 case Instruction::IPUT_WIDE:
570 EmitInsn_IPut(ARGS, kLong);
571 break;
572
573 case Instruction::IPUT_OBJECT:
574 EmitInsn_IPut(ARGS, kObject);
575 break;
576
577 case Instruction::IPUT_BOOLEAN:
578 EmitInsn_IPut(ARGS, kBoolean);
579 break;
580
581 case Instruction::IPUT_BYTE:
582 EmitInsn_IPut(ARGS, kByte);
583 break;
584
585 case Instruction::IPUT_CHAR:
586 EmitInsn_IPut(ARGS, kChar);
587 break;
588
589 case Instruction::IPUT_SHORT:
590 EmitInsn_IPut(ARGS, kShort);
591 break;
592
593 case Instruction::SGET:
594 EmitInsn_SGet(ARGS, kInt);
595 break;
596
597 case Instruction::SGET_WIDE:
598 EmitInsn_SGet(ARGS, kLong);
599 break;
600
601 case Instruction::SGET_OBJECT:
602 EmitInsn_SGet(ARGS, kObject);
603 break;
604
605 case Instruction::SGET_BOOLEAN:
606 EmitInsn_SGet(ARGS, kBoolean);
607 break;
608
609 case Instruction::SGET_BYTE:
610 EmitInsn_SGet(ARGS, kByte);
611 break;
612
613 case Instruction::SGET_CHAR:
614 EmitInsn_SGet(ARGS, kChar);
615 break;
616
617 case Instruction::SGET_SHORT:
618 EmitInsn_SGet(ARGS, kShort);
619 break;
620
621 case Instruction::SPUT:
622 EmitInsn_SPut(ARGS, kInt);
623 break;
624
625 case Instruction::SPUT_WIDE:
626 EmitInsn_SPut(ARGS, kLong);
627 break;
628
629 case Instruction::SPUT_OBJECT:
630 EmitInsn_SPut(ARGS, kObject);
631 break;
632
633 case Instruction::SPUT_BOOLEAN:
634 EmitInsn_SPut(ARGS, kBoolean);
635 break;
636
637 case Instruction::SPUT_BYTE:
638 EmitInsn_SPut(ARGS, kByte);
639 break;
640
641 case Instruction::SPUT_CHAR:
642 EmitInsn_SPut(ARGS, kChar);
643 break;
644
645 case Instruction::SPUT_SHORT:
646 EmitInsn_SPut(ARGS, kShort);
647 break;
648
649
650 case Instruction::INVOKE_VIRTUAL:
651 EmitInsn_InvokeVirtual(ARGS, false);
652 break;
653
654 case Instruction::INVOKE_SUPER:
655 EmitInsn_InvokeSuper(ARGS, false);
656 break;
657
658 case Instruction::INVOKE_DIRECT:
Logan Chien61c65dc2012-02-29 03:22:30 +0800659 EmitInsn_InvokeStaticDirect(ARGS, kDirect, /* is_range */ false);
Logan Chien70f94b42011-12-27 17:49:11 +0800660 break;
661
662 case Instruction::INVOKE_STATIC:
Logan Chien61c65dc2012-02-29 03:22:30 +0800663 EmitInsn_InvokeStaticDirect(ARGS, kStatic, /* is_range */ false);
Logan Chien70f94b42011-12-27 17:49:11 +0800664 break;
665
666 case Instruction::INVOKE_INTERFACE:
667 EmitInsn_InvokeInterface(ARGS, false);
668 break;
669
670 case Instruction::INVOKE_VIRTUAL_RANGE:
671 EmitInsn_InvokeVirtual(ARGS, true);
672 break;
673
674 case Instruction::INVOKE_SUPER_RANGE:
675 EmitInsn_InvokeSuper(ARGS, true);
676 break;
677
678 case Instruction::INVOKE_DIRECT_RANGE:
Logan Chien61c65dc2012-02-29 03:22:30 +0800679 EmitInsn_InvokeStaticDirect(ARGS, kDirect, true);
Logan Chien70f94b42011-12-27 17:49:11 +0800680 break;
681
682 case Instruction::INVOKE_STATIC_RANGE:
Logan Chien61c65dc2012-02-29 03:22:30 +0800683 EmitInsn_InvokeStaticDirect(ARGS, kStatic, true);
Logan Chien70f94b42011-12-27 17:49:11 +0800684 break;
685
686 case Instruction::INVOKE_INTERFACE_RANGE:
687 EmitInsn_InvokeInterface(ARGS, true);
688 break;
689
690 case Instruction::NEG_INT:
691 EmitInsn_Neg(ARGS, kInt);
692 break;
693
694 case Instruction::NOT_INT:
695 EmitInsn_Not(ARGS, kInt);
696 break;
697
698 case Instruction::NEG_LONG:
699 EmitInsn_Neg(ARGS, kLong);
700 break;
701
702 case Instruction::NOT_LONG:
703 EmitInsn_Not(ARGS, kLong);
704 break;
705
706 case Instruction::NEG_FLOAT:
707 EmitInsn_FNeg(ARGS, kFloat);
708 break;
709
710 case Instruction::NEG_DOUBLE:
711 EmitInsn_FNeg(ARGS, kDouble);
712 break;
713
714 case Instruction::INT_TO_LONG:
715 EmitInsn_SExt(ARGS);
716 break;
717
718 case Instruction::INT_TO_FLOAT:
719 EmitInsn_IntToFP(ARGS, kInt, kFloat);
720 break;
721
722 case Instruction::INT_TO_DOUBLE:
723 EmitInsn_IntToFP(ARGS, kInt, kDouble);
724 break;
725
726 case Instruction::LONG_TO_INT:
727 EmitInsn_Trunc(ARGS);
728 break;
729
730 case Instruction::LONG_TO_FLOAT:
731 EmitInsn_IntToFP(ARGS, kLong, kFloat);
732 break;
733
734 case Instruction::LONG_TO_DOUBLE:
735 EmitInsn_IntToFP(ARGS, kLong, kDouble);
736 break;
737
738 case Instruction::FLOAT_TO_INT:
739 EmitInsn_FPToInt(ARGS, kFloat, kInt);
740 break;
741
742 case Instruction::FLOAT_TO_LONG:
743 EmitInsn_FPToInt(ARGS, kFloat, kLong);
744 break;
745
746 case Instruction::FLOAT_TO_DOUBLE:
747 EmitInsn_FExt(ARGS);
748 break;
749
750 case Instruction::DOUBLE_TO_INT:
751 EmitInsn_FPToInt(ARGS, kDouble, kInt);
752 break;
753
754 case Instruction::DOUBLE_TO_LONG:
755 EmitInsn_FPToInt(ARGS, kDouble, kLong);
756 break;
757
758 case Instruction::DOUBLE_TO_FLOAT:
759 EmitInsn_FTrunc(ARGS);
760 break;
761
762 case Instruction::INT_TO_BYTE:
763 EmitInsn_TruncAndSExt(ARGS, 8);
764 break;
765
766 case Instruction::INT_TO_CHAR:
767 EmitInsn_TruncAndZExt(ARGS, 16);
768 break;
769
770 case Instruction::INT_TO_SHORT:
771 EmitInsn_TruncAndSExt(ARGS, 16);
772 break;
773
774 case Instruction::ADD_INT:
775 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kInt, false);
776 break;
777
778 case Instruction::SUB_INT:
779 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kInt, false);
780 break;
781
782 case Instruction::MUL_INT:
783 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kInt, false);
784 break;
785
786 case Instruction::DIV_INT:
787 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kInt, false);
788 break;
789
790 case Instruction::REM_INT:
791 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kInt, false);
792 break;
793
794 case Instruction::AND_INT:
795 EmitInsn_IntArithm(ARGS, kIntArithm_And, kInt, false);
796 break;
797
798 case Instruction::OR_INT:
799 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kInt, false);
800 break;
801
802 case Instruction::XOR_INT:
803 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kInt, false);
804 break;
805
806 case Instruction::SHL_INT:
807 EmitInsn_IntArithm(ARGS, kIntArithm_Shl, kInt, false);
808 break;
809
810 case Instruction::SHR_INT:
811 EmitInsn_IntArithm(ARGS, kIntArithm_Shr, kInt, false);
812 break;
813
814 case Instruction::USHR_INT:
815 EmitInsn_IntArithm(ARGS, kIntArithm_UShr, kInt, false);
816 break;
817
818 case Instruction::ADD_LONG:
819 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kLong, false);
820 break;
821
822 case Instruction::SUB_LONG:
823 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kLong, false);
824 break;
825
826 case Instruction::MUL_LONG:
827 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kLong, false);
828 break;
829
830 case Instruction::DIV_LONG:
831 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kLong, false);
832 break;
833
834 case Instruction::REM_LONG:
835 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kLong, false);
836 break;
837
838 case Instruction::AND_LONG:
839 EmitInsn_IntArithm(ARGS, kIntArithm_And, kLong, false);
840 break;
841
842 case Instruction::OR_LONG:
843 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kLong, false);
844 break;
845
846 case Instruction::XOR_LONG:
847 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kLong, false);
848 break;
849
850 case Instruction::SHL_LONG:
851 EmitInsn_IntArithm(ARGS, kIntArithm_Shl, kLong, false);
852 break;
853
854 case Instruction::SHR_LONG:
855 EmitInsn_IntArithm(ARGS, kIntArithm_Shr, kLong, false);
856 break;
857
858 case Instruction::USHR_LONG:
859 EmitInsn_IntArithm(ARGS, kIntArithm_UShr, kLong, false);
860 break;
861
862 case Instruction::ADD_FLOAT:
863 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kFloat, false);
864 break;
865
866 case Instruction::SUB_FLOAT:
867 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kFloat, false);
868 break;
869
870 case Instruction::MUL_FLOAT:
871 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kFloat, false);
872 break;
873
874 case Instruction::DIV_FLOAT:
875 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kFloat, false);
876 break;
877
878 case Instruction::REM_FLOAT:
879 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kFloat, false);
880 break;
881
882 case Instruction::ADD_DOUBLE:
883 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kDouble, false);
884 break;
885
886 case Instruction::SUB_DOUBLE:
887 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kDouble, false);
888 break;
889
890 case Instruction::MUL_DOUBLE:
891 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kDouble, false);
892 break;
893
894 case Instruction::DIV_DOUBLE:
895 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kDouble, false);
896 break;
897
898 case Instruction::REM_DOUBLE:
899 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kDouble, false);
900 break;
901
902 case Instruction::ADD_INT_2ADDR:
903 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kInt, true);
904 break;
905
906 case Instruction::SUB_INT_2ADDR:
907 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kInt, true);
908 break;
909
910 case Instruction::MUL_INT_2ADDR:
911 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kInt, true);
912 break;
913
914 case Instruction::DIV_INT_2ADDR:
915 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kInt, true);
916 break;
917
918 case Instruction::REM_INT_2ADDR:
919 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kInt, true);
920 break;
921
922 case Instruction::AND_INT_2ADDR:
923 EmitInsn_IntArithm(ARGS, kIntArithm_And, kInt, true);
924 break;
925
926 case Instruction::OR_INT_2ADDR:
927 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kInt, true);
928 break;
929
930 case Instruction::XOR_INT_2ADDR:
931 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kInt, true);
932 break;
933
934 case Instruction::SHL_INT_2ADDR:
935 EmitInsn_IntArithm(ARGS, kIntArithm_Shl, kInt, true);
936 break;
937
938 case Instruction::SHR_INT_2ADDR:
939 EmitInsn_IntArithm(ARGS, kIntArithm_Shr, kInt, true);
940 break;
941
942 case Instruction::USHR_INT_2ADDR:
943 EmitInsn_IntArithm(ARGS, kIntArithm_UShr, kInt, true);
944 break;
945
946 case Instruction::ADD_LONG_2ADDR:
947 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kLong, true);
948 break;
949
950 case Instruction::SUB_LONG_2ADDR:
951 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kLong, true);
952 break;
953
954 case Instruction::MUL_LONG_2ADDR:
955 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kLong, true);
956 break;
957
958 case Instruction::DIV_LONG_2ADDR:
959 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kLong, true);
960 break;
961
962 case Instruction::REM_LONG_2ADDR:
963 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kLong, true);
964 break;
965
966 case Instruction::AND_LONG_2ADDR:
967 EmitInsn_IntArithm(ARGS, kIntArithm_And, kLong, true);
968 break;
969
970 case Instruction::OR_LONG_2ADDR:
971 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kLong, true);
972 break;
973
974 case Instruction::XOR_LONG_2ADDR:
975 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kLong, true);
976 break;
977
978 case Instruction::SHL_LONG_2ADDR:
979 EmitInsn_IntArithm(ARGS, kIntArithm_Shl, kLong, true);
980 break;
981
982 case Instruction::SHR_LONG_2ADDR:
983 EmitInsn_IntArithm(ARGS, kIntArithm_Shr, kLong, true);
984 break;
985
986 case Instruction::USHR_LONG_2ADDR:
987 EmitInsn_IntArithm(ARGS, kIntArithm_UShr, kLong, true);
988 break;
989
990 case Instruction::ADD_FLOAT_2ADDR:
991 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kFloat, true);
992 break;
993
994 case Instruction::SUB_FLOAT_2ADDR:
995 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kFloat, true);
996 break;
997
998 case Instruction::MUL_FLOAT_2ADDR:
999 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kFloat, true);
1000 break;
1001
1002 case Instruction::DIV_FLOAT_2ADDR:
1003 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kFloat, true);
1004 break;
1005
1006 case Instruction::REM_FLOAT_2ADDR:
1007 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kFloat, true);
1008 break;
1009
1010 case Instruction::ADD_DOUBLE_2ADDR:
1011 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kDouble, true);
1012 break;
1013
1014 case Instruction::SUB_DOUBLE_2ADDR:
1015 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kDouble, true);
1016 break;
1017
1018 case Instruction::MUL_DOUBLE_2ADDR:
1019 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kDouble, true);
1020 break;
1021
1022 case Instruction::DIV_DOUBLE_2ADDR:
1023 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kDouble, true);
1024 break;
1025
1026 case Instruction::REM_DOUBLE_2ADDR:
1027 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kDouble, true);
1028 break;
1029
1030 case Instruction::ADD_INT_LIT16:
1031 case Instruction::ADD_INT_LIT8:
1032 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Add);
1033 break;
1034
1035 case Instruction::RSUB_INT:
1036 case Instruction::RSUB_INT_LIT8:
1037 EmitInsn_RSubImmediate(ARGS);
1038 break;
1039
1040 case Instruction::MUL_INT_LIT16:
1041 case Instruction::MUL_INT_LIT8:
1042 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Mul);
1043 break;
1044
1045 case Instruction::DIV_INT_LIT16:
1046 case Instruction::DIV_INT_LIT8:
1047 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Div);
1048 break;
1049
1050 case Instruction::REM_INT_LIT16:
1051 case Instruction::REM_INT_LIT8:
1052 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Rem);
1053 break;
1054
1055 case Instruction::AND_INT_LIT16:
1056 case Instruction::AND_INT_LIT8:
1057 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_And);
1058 break;
1059
1060 case Instruction::OR_INT_LIT16:
1061 case Instruction::OR_INT_LIT8:
1062 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Or);
1063 break;
1064
1065 case Instruction::XOR_INT_LIT16:
1066 case Instruction::XOR_INT_LIT8:
1067 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Xor);
1068 break;
1069
1070 case Instruction::SHL_INT_LIT8:
1071 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Shl);
1072 break;
1073
1074 case Instruction::SHR_INT_LIT8:
1075 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Shr);
1076 break;
1077
1078 case Instruction::USHR_INT_LIT8:
1079 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_UShr);
1080 break;
1081
1082 case Instruction::UNUSED_3E:
1083 case Instruction::UNUSED_3F:
1084 case Instruction::UNUSED_40:
1085 case Instruction::UNUSED_41:
1086 case Instruction::UNUSED_42:
1087 case Instruction::UNUSED_43:
1088 case Instruction::UNUSED_73:
1089 case Instruction::UNUSED_79:
1090 case Instruction::UNUSED_7A:
1091 case Instruction::UNUSED_E3:
1092 case Instruction::UNUSED_E4:
1093 case Instruction::UNUSED_E5:
1094 case Instruction::UNUSED_E6:
1095 case Instruction::UNUSED_E7:
1096 case Instruction::UNUSED_E8:
1097 case Instruction::UNUSED_E9:
1098 case Instruction::UNUSED_EA:
1099 case Instruction::UNUSED_EB:
1100 case Instruction::UNUSED_EC:
1101 case Instruction::THROW_VERIFICATION_ERROR:
1102 case Instruction::UNUSED_EE:
1103 case Instruction::UNUSED_EF:
1104 case Instruction::UNUSED_F0:
1105 case Instruction::UNUSED_F1:
1106 case Instruction::UNUSED_F2:
1107 case Instruction::UNUSED_F3:
1108 case Instruction::UNUSED_F4:
1109 case Instruction::UNUSED_F5:
1110 case Instruction::UNUSED_F6:
1111 case Instruction::UNUSED_F7:
1112 case Instruction::UNUSED_F8:
1113 case Instruction::UNUSED_F9:
1114 case Instruction::UNUSED_FA:
1115 case Instruction::UNUSED_FB:
1116 case Instruction::UNUSED_FC:
1117 case Instruction::UNUSED_FD:
1118 case Instruction::UNUSED_FE:
1119 case Instruction::UNUSED_FF:
1120 LOG(FATAL) << "Dex file contains UNUSED bytecode: " << insn->Opcode();
1121 break;
1122 }
1123
1124#undef ARGS
1125}
1126
1127
1128void MethodCompiler::EmitInsn_Nop(uint32_t dex_pc,
1129 Instruction const* insn) {
Logan Chiene09a6b72011-12-27 17:50:21 +08001130
1131 uint16_t insn_signature = code_item_->insns_[dex_pc];
1132
1133 if (insn_signature == Instruction::kPackedSwitchSignature ||
1134 insn_signature == Instruction::kSparseSwitchSignature ||
1135 insn_signature == Instruction::kArrayDataSignature) {
1136 irb_.CreateUnreachable();
1137 } else{
1138 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1139 }
Shih-wei Liaod1fec812012-02-13 09:51:10 -08001140}
1141
1142
Logan Chien70f94b42011-12-27 17:49:11 +08001143void MethodCompiler::EmitInsn_Move(uint32_t dex_pc,
1144 Instruction const* insn,
1145 JType jty) {
Logan Chien48173132011-12-27 17:51:13 +08001146
Elliott Hughesadb8c672012-03-06 16:49:32 -08001147 DecodedInstruction dec_insn(insn);
Logan Chien48173132011-12-27 17:51:13 +08001148
Elliott Hughesadb8c672012-03-06 16:49:32 -08001149 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, jty, kReg);
1150 EmitStoreDalvikReg(dec_insn.vA, jty, kReg, src_value);
Logan Chien48173132011-12-27 17:51:13 +08001151
Logan Chien70f94b42011-12-27 17:49:11 +08001152 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1153}
1154
1155
1156void MethodCompiler::EmitInsn_MoveResult(uint32_t dex_pc,
1157 Instruction const* insn,
1158 JType jty) {
Logan Chien48173132011-12-27 17:51:13 +08001159
Elliott Hughesadb8c672012-03-06 16:49:32 -08001160 DecodedInstruction dec_insn(insn);
Logan Chien48173132011-12-27 17:51:13 +08001161
1162 llvm::Value* src_value = EmitLoadDalvikRetValReg(jty, kReg);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001163 EmitStoreDalvikReg(dec_insn.vA, jty, kReg, src_value);
Logan Chien48173132011-12-27 17:51:13 +08001164
Logan Chien70f94b42011-12-27 17:49:11 +08001165 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1166}
1167
1168
1169void MethodCompiler::EmitInsn_MoveException(uint32_t dex_pc,
1170 Instruction const* insn) {
Logan Chien3354cec2012-01-13 14:29:03 +08001171
Elliott Hughesadb8c672012-03-06 16:49:32 -08001172 DecodedInstruction dec_insn(insn);
Logan Chien3354cec2012-01-13 14:29:03 +08001173
1174 // Get thread-local exception field address
1175 llvm::Constant* exception_field_offset =
1176 irb_.getPtrEquivInt(Thread::ExceptionOffset().Int32Value());
1177
1178 llvm::Value* thread_object_addr =
1179 irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1180
1181 llvm::Value* exception_field_addr =
1182 irb_.CreatePtrDisp(thread_object_addr, exception_field_offset,
1183 irb_.getJObjectTy()->getPointerTo());
1184
1185 // Get exception object address
1186 llvm::Value* exception_object_addr = irb_.CreateLoad(exception_field_addr);
1187
1188 // Set thread-local exception field address to NULL
1189 irb_.CreateStore(irb_.getJNull(), exception_field_addr);
1190
1191 // Keep the exception object in the Dalvik register
Elliott Hughesadb8c672012-03-06 16:49:32 -08001192 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, exception_object_addr);
Logan Chien3354cec2012-01-13 14:29:03 +08001193
Logan Chien70f94b42011-12-27 17:49:11 +08001194 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1195}
1196
1197
1198void MethodCompiler::EmitInsn_ThrowException(uint32_t dex_pc,
1199 Instruction const* insn) {
Logan Chien6c6f12d2012-01-13 19:26:27 +08001200
Elliott Hughesadb8c672012-03-06 16:49:32 -08001201 DecodedInstruction dec_insn(insn);
Logan Chien6c6f12d2012-01-13 19:26:27 +08001202
1203 llvm::Value* exception_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001204 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chien6c6f12d2012-01-13 19:26:27 +08001205
Logan Chien8dfcbea2012-02-17 18:50:32 +08001206 EmitUpdateLineNumFromDexPC(dex_pc);
1207
Logan Chien6c6f12d2012-01-13 19:26:27 +08001208 irb_.CreateCall(irb_.GetRuntime(ThrowException), exception_addr);
1209
1210 EmitBranchExceptionLandingPad(dex_pc);
Logan Chien70f94b42011-12-27 17:49:11 +08001211}
1212
1213
1214void MethodCompiler::EmitInsn_ReturnVoid(uint32_t dex_pc,
1215 Instruction const* insn) {
Logan Chien8898a272011-12-27 17:51:56 +08001216 // Garbage collection safe-point
1217 EmitGuard_GarbageCollectionSuspend(dex_pc);
1218
Logan Chien8dfcbea2012-02-17 18:50:32 +08001219 // Pop the shadow frame
1220 EmitPopShadowFrame();
1221
Logan Chien8898a272011-12-27 17:51:56 +08001222 // Return!
1223 irb_.CreateRetVoid();
Logan Chien70f94b42011-12-27 17:49:11 +08001224}
1225
1226
1227void MethodCompiler::EmitInsn_Return(uint32_t dex_pc,
1228 Instruction const* insn) {
Logan Chien8898a272011-12-27 17:51:56 +08001229
Elliott Hughesadb8c672012-03-06 16:49:32 -08001230 DecodedInstruction dec_insn(insn);
Logan Chien8898a272011-12-27 17:51:56 +08001231
1232 // Garbage collection safe-point
1233 EmitGuard_GarbageCollectionSuspend(dex_pc);
1234
Logan Chien8dfcbea2012-02-17 18:50:32 +08001235 // Pop the shadow frame
1236 EmitPopShadowFrame();
1237 // NOTE: It is important to keep this AFTER the GC safe-point. Otherwise,
1238 // the return value might be collected since the shadow stack is popped.
1239
Logan Chien8898a272011-12-27 17:51:56 +08001240 // Return!
1241 char ret_shorty = method_helper_.GetShorty()[0];
Elliott Hughesadb8c672012-03-06 16:49:32 -08001242 llvm::Value* retval = EmitLoadDalvikReg(dec_insn.vA, ret_shorty, kAccurate);
Logan Chien8898a272011-12-27 17:51:56 +08001243
1244 irb_.CreateRet(retval);
Logan Chien70f94b42011-12-27 17:49:11 +08001245}
1246
1247
1248void MethodCompiler::EmitInsn_LoadConstant(uint32_t dex_pc,
1249 Instruction const* insn,
1250 JType imm_jty) {
Shih-wei Liao798366e2012-02-16 09:25:33 -08001251
Elliott Hughesadb8c672012-03-06 16:49:32 -08001252 DecodedInstruction dec_insn(insn);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001253
1254 DCHECK(imm_jty == kInt || imm_jty == kLong) << imm_jty;
1255
1256 int64_t imm = 0;
1257
1258 switch (insn->Opcode()) {
1259 // 32-bit Immediate
1260 case Instruction::CONST_4:
1261 case Instruction::CONST_16:
1262 case Instruction::CONST:
1263 case Instruction::CONST_WIDE_16:
1264 case Instruction::CONST_WIDE_32:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001265 imm = static_cast<int64_t>(static_cast<int32_t>(dec_insn.vB));
Shih-wei Liao798366e2012-02-16 09:25:33 -08001266 break;
1267
1268 case Instruction::CONST_HIGH16:
1269 imm = static_cast<int64_t>(static_cast<int32_t>(
Elliott Hughesadb8c672012-03-06 16:49:32 -08001270 static_cast<uint32_t>(static_cast<uint16_t>(dec_insn.vB)) << 16));
Shih-wei Liao798366e2012-02-16 09:25:33 -08001271 break;
1272
1273 // 64-bit Immediate
1274 case Instruction::CONST_WIDE:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001275 imm = static_cast<int64_t>(dec_insn.vB_wide);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001276 break;
1277
1278 case Instruction::CONST_WIDE_HIGH16:
1279 imm = static_cast<int64_t>(
Elliott Hughesadb8c672012-03-06 16:49:32 -08001280 static_cast<uint64_t>(static_cast<uint16_t>(dec_insn.vB)) << 48);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001281 break;
1282
1283 // Unknown opcode for load constant (unreachable)
1284 default:
1285 LOG(FATAL) << "Unknown opcode for load constant: " << insn->Opcode();
1286 break;
1287 }
1288
1289 // Store the non-object register
1290 llvm::Type* imm_type = irb_.getJType(imm_jty, kAccurate);
1291 llvm::Constant* imm_value = llvm::ConstantInt::getSigned(imm_type, imm);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001292 EmitStoreDalvikReg(dec_insn.vA, imm_jty, kAccurate, imm_value);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001293
1294 // Store the object register if it is possible to be null.
1295 if (imm_jty == kInt && imm == 0) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001296 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, irb_.getJNull());
Shih-wei Liao798366e2012-02-16 09:25:33 -08001297 }
1298
Logan Chien70f94b42011-12-27 17:49:11 +08001299 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1300}
1301
1302
1303void MethodCompiler::EmitInsn_LoadConstantString(uint32_t dex_pc,
1304 Instruction const* insn) {
Logan Chienc3b4ba12012-01-16 19:52:53 +08001305
Elliott Hughesadb8c672012-03-06 16:49:32 -08001306 DecodedInstruction dec_insn(insn);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001307
Elliott Hughesadb8c672012-03-06 16:49:32 -08001308 uint32_t string_idx = dec_insn.vB;
Logan Chienc3b4ba12012-01-16 19:52:53 +08001309
1310 llvm::Value* string_field_addr = EmitLoadDexCacheStringFieldAddr(string_idx);
1311
1312 llvm::Value* string_addr = irb_.CreateLoad(string_field_addr);
1313
1314 if (!compiler_->CanAssumeStringIsPresentInDexCache(dex_cache_, string_idx)) {
1315 llvm::BasicBlock* block_str_exist =
1316 CreateBasicBlockWithDexPC(dex_pc, "str_exist");
1317
1318 llvm::BasicBlock* block_str_resolve =
1319 CreateBasicBlockWithDexPC(dex_pc, "str_resolve");
1320
1321 // Test: Is the string resolved and in the dex cache?
1322 llvm::Value* equal_null = irb_.CreateICmpEQ(string_addr, irb_.getJNull());
1323
1324 irb_.CreateCondBr(equal_null, block_str_exist, block_str_resolve);
1325
1326 // String is resolved, go to next basic block.
1327 irb_.SetInsertPoint(block_str_exist);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001328 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, string_addr);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001329 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1330
1331 // String is not resolved yet, resolve it now.
1332 irb_.SetInsertPoint(block_str_resolve);
1333
1334 llvm::Function* runtime_func = irb_.GetRuntime(ResolveString);
1335
1336 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1337
1338 llvm::Value* string_idx_value = irb_.getInt32(string_idx);
1339
Logan Chien8dfcbea2012-02-17 18:50:32 +08001340 EmitUpdateLineNumFromDexPC(dex_pc);
1341
1342 string_addr = irb_.CreateCall2(runtime_func, method_object_addr,
1343 string_idx_value);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001344
1345 EmitGuard_ExceptionLandingPad(dex_pc);
1346 }
1347
1348 // Store the string object to the Dalvik register
Elliott Hughesadb8c672012-03-06 16:49:32 -08001349 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, string_addr);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001350
Logan Chien70f94b42011-12-27 17:49:11 +08001351 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1352}
1353
1354
Logan Chien27b30252012-01-14 03:43:35 +08001355llvm::Value* MethodCompiler::EmitLoadConstantClass(uint32_t dex_pc,
1356 uint32_t type_idx) {
1357 if (!compiler_->CanAccessTypeWithoutChecks(method_idx_, dex_cache_,
1358 *dex_file_, type_idx)) {
1359 llvm::Value* type_idx_value = irb_.getInt32(type_idx);
1360
1361 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1362
1363 llvm::Function* runtime_func =
1364 irb_.GetRuntime(InitializeTypeAndVerifyAccess);
1365
Logan Chien8dfcbea2012-02-17 18:50:32 +08001366 EmitUpdateLineNumFromDexPC(dex_pc);
1367
Logan Chien27b30252012-01-14 03:43:35 +08001368 llvm::Value* type_object_addr =
1369 irb_.CreateCall2(runtime_func, type_idx_value, method_object_addr);
1370
1371 EmitGuard_ExceptionLandingPad(dex_pc);
1372
1373 return type_object_addr;
1374
1375 } else {
1376 // Try to load the class (type) object from the test cache.
1377 llvm::Value* type_field_addr =
1378 EmitLoadDexCacheResolvedTypeFieldAddr(type_idx);
1379
1380 llvm::Value* type_object_addr = irb_.CreateLoad(type_field_addr);
1381
1382 if (compiler_->CanAssumeTypeIsPresentInDexCache(dex_cache_, type_idx)) {
1383 return type_object_addr;
1384 }
1385
1386 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
1387
1388 // Test whether class (type) object is in the dex cache or not
1389 llvm::Value* equal_null =
1390 irb_.CreateICmpEQ(type_object_addr, irb_.getJNull());
1391
1392 llvm::BasicBlock* block_cont =
1393 CreateBasicBlockWithDexPC(dex_pc, "cont");
1394
1395 llvm::BasicBlock* block_load_class =
1396 CreateBasicBlockWithDexPC(dex_pc, "load_class");
1397
1398 irb_.CreateCondBr(equal_null, block_load_class, block_cont);
1399
1400 // Failback routine to load the class object
1401 irb_.SetInsertPoint(block_load_class);
1402
1403 llvm::Function* runtime_func = irb_.GetRuntime(InitializeType);
1404
1405 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
1406
1407 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1408
Logan Chien8dfcbea2012-02-17 18:50:32 +08001409 EmitUpdateLineNumFromDexPC(dex_pc);
1410
Logan Chien27b30252012-01-14 03:43:35 +08001411 llvm::Value* loaded_type_object_addr =
1412 irb_.CreateCall2(runtime_func, type_idx_value, method_object_addr);
1413
1414 EmitGuard_ExceptionLandingPad(dex_pc);
1415
1416 llvm::BasicBlock* block_after_load_class = irb_.GetInsertBlock();
1417
1418 irb_.CreateBr(block_cont);
1419
1420 // Now the class object must be loaded
1421 irb_.SetInsertPoint(block_cont);
1422
1423 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1424
1425 phi->addIncoming(type_object_addr, block_original);
1426 phi->addIncoming(loaded_type_object_addr, block_after_load_class);
1427
1428 return phi;
1429 }
1430}
1431
1432
Logan Chien70f94b42011-12-27 17:49:11 +08001433void MethodCompiler::EmitInsn_LoadConstantClass(uint32_t dex_pc,
1434 Instruction const* insn) {
Logan Chien27b30252012-01-14 03:43:35 +08001435
Elliott Hughesadb8c672012-03-06 16:49:32 -08001436 DecodedInstruction dec_insn(insn);
Logan Chien27b30252012-01-14 03:43:35 +08001437
Elliott Hughesadb8c672012-03-06 16:49:32 -08001438 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, dec_insn.vB);
1439 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, type_object_addr);
Logan Chien27b30252012-01-14 03:43:35 +08001440
Logan Chien70f94b42011-12-27 17:49:11 +08001441 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1442}
1443
1444
1445void MethodCompiler::EmitInsn_MonitorEnter(uint32_t dex_pc,
1446 Instruction const* insn) {
Logan Chien9e0dbe42012-01-13 12:11:37 +08001447
Elliott Hughesadb8c672012-03-06 16:49:32 -08001448 DecodedInstruction dec_insn(insn);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001449
1450 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001451 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001452
1453 // TODO: Slow path always. May not need NullPointerException check.
1454 EmitGuard_NullPointerException(dex_pc, object_addr);
1455
Logan Chien8dfcbea2012-02-17 18:50:32 +08001456 EmitUpdateLineNumFromDexPC(dex_pc);
1457
Logan Chien9e0dbe42012-01-13 12:11:37 +08001458 irb_.CreateCall(irb_.GetRuntime(LockObject), object_addr);
1459 EmitGuard_ExceptionLandingPad(dex_pc);
1460
Logan Chien70f94b42011-12-27 17:49:11 +08001461 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1462}
1463
1464
1465void MethodCompiler::EmitInsn_MonitorExit(uint32_t dex_pc,
1466 Instruction const* insn) {
Logan Chien9e0dbe42012-01-13 12:11:37 +08001467
Elliott Hughesadb8c672012-03-06 16:49:32 -08001468 DecodedInstruction dec_insn(insn);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001469
1470 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001471 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001472
1473 EmitGuard_NullPointerException(dex_pc, object_addr);
1474
Logan Chien8dfcbea2012-02-17 18:50:32 +08001475 EmitUpdateLineNumFromDexPC(dex_pc);
1476
Logan Chien9e0dbe42012-01-13 12:11:37 +08001477 irb_.CreateCall(irb_.GetRuntime(UnlockObject), object_addr);
1478 EmitGuard_ExceptionLandingPad(dex_pc);
1479
Logan Chien70f94b42011-12-27 17:49:11 +08001480 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1481}
1482
1483
1484void MethodCompiler::EmitInsn_CheckCast(uint32_t dex_pc,
1485 Instruction const* insn) {
Logan Chienfc880952012-01-15 23:53:10 +08001486
Elliott Hughesadb8c672012-03-06 16:49:32 -08001487 DecodedInstruction dec_insn(insn);
Logan Chienfc880952012-01-15 23:53:10 +08001488
1489 llvm::BasicBlock* block_test_class =
1490 CreateBasicBlockWithDexPC(dex_pc, "test_class");
1491
1492 llvm::BasicBlock* block_test_sub_class =
1493 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
1494
1495 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001496 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chienfc880952012-01-15 23:53:10 +08001497
1498 // Test: Is the reference equal to null? Act as no-op when it is null.
1499 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
1500
1501 irb_.CreateCondBr(equal_null,
1502 GetNextBasicBlock(dex_pc),
1503 block_test_class);
1504
1505 // Test: Is the object instantiated from the given class?
1506 irb_.SetInsertPoint(block_test_class);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001507 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, dec_insn.vB);
Logan Chienfc880952012-01-15 23:53:10 +08001508 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1509
1510 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
1511
1512 llvm::Value* object_type_field_addr =
1513 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
1514
1515 llvm::Value* object_type_object_addr =
1516 irb_.CreateLoad(object_type_field_addr);
1517
1518 llvm::Value* equal_class =
1519 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
1520
1521 irb_.CreateCondBr(equal_class,
1522 GetNextBasicBlock(dex_pc),
1523 block_test_sub_class);
1524
1525 // Test: Is the object instantiated from the subclass of the given class?
1526 irb_.SetInsertPoint(block_test_sub_class);
1527
Logan Chien8dfcbea2012-02-17 18:50:32 +08001528 EmitUpdateLineNumFromDexPC(dex_pc);
1529
Logan Chienfc880952012-01-15 23:53:10 +08001530 irb_.CreateCall2(irb_.GetRuntime(CheckCast),
1531 type_object_addr, object_type_object_addr);
1532
1533 EmitGuard_ExceptionLandingPad(dex_pc);
1534
Logan Chien70f94b42011-12-27 17:49:11 +08001535 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1536}
1537
1538
1539void MethodCompiler::EmitInsn_InstanceOf(uint32_t dex_pc,
1540 Instruction const* insn) {
Logan Chien68725e22012-01-15 22:25:34 +08001541
Elliott Hughesadb8c672012-03-06 16:49:32 -08001542 DecodedInstruction dec_insn(insn);
Logan Chien68725e22012-01-15 22:25:34 +08001543
1544 llvm::Constant* zero = irb_.getJInt(0);
1545 llvm::Constant* one = irb_.getJInt(1);
1546
1547 llvm::BasicBlock* block_nullp = CreateBasicBlockWithDexPC(dex_pc, "nullp");
1548
1549 llvm::BasicBlock* block_test_class =
1550 CreateBasicBlockWithDexPC(dex_pc, "test_class");
1551
1552 llvm::BasicBlock* block_class_equals =
1553 CreateBasicBlockWithDexPC(dex_pc, "class_eq");
1554
1555 llvm::BasicBlock* block_test_sub_class =
1556 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
1557
1558 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001559 EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
Logan Chien68725e22012-01-15 22:25:34 +08001560
1561 // Overview of the following code :
1562 // We check for null, if so, then false, otherwise check for class == . If so
1563 // then true, otherwise do callout slowpath.
1564 //
1565 // Test: Is the reference equal to null? Set 0 when it is null.
1566 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
1567
1568 irb_.CreateCondBr(equal_null, block_nullp, block_test_class);
1569
1570 irb_.SetInsertPoint(block_nullp);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001571 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, zero);
Logan Chien68725e22012-01-15 22:25:34 +08001572 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1573
1574 // Test: Is the object instantiated from the given class?
1575 irb_.SetInsertPoint(block_test_class);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001576 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, dec_insn.vC);
Logan Chien68725e22012-01-15 22:25:34 +08001577 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1578
1579 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
1580
1581 llvm::Value* object_type_field_addr =
1582 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
1583
1584 llvm::Value* object_type_object_addr =
1585 irb_.CreateLoad(object_type_field_addr);
1586
1587 llvm::Value* equal_class =
1588 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
1589
1590 irb_.CreateCondBr(equal_class, block_class_equals, block_test_sub_class);
1591
1592 irb_.SetInsertPoint(block_class_equals);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001593 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, one);
Logan Chien68725e22012-01-15 22:25:34 +08001594 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1595
1596 // Test: Is the object instantiated from the subclass of the given class?
1597 irb_.SetInsertPoint(block_test_sub_class);
1598
1599 llvm::Value* result =
1600 irb_.CreateCall2(irb_.GetRuntime(IsAssignable),
1601 type_object_addr, object_type_object_addr);
1602
Elliott Hughesadb8c672012-03-06 16:49:32 -08001603 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result);
Logan Chien68725e22012-01-15 22:25:34 +08001604
Logan Chien70f94b42011-12-27 17:49:11 +08001605 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1606}
1607
1608
Logan Chien61bb6142012-02-03 15:34:53 +08001609llvm::Value* MethodCompiler::EmitLoadArrayLength(llvm::Value* array) {
1610 // Load array length field address
1611 llvm::Constant* array_len_field_offset =
1612 irb_.getPtrEquivInt(Array::LengthOffset().Int32Value());
1613
1614 llvm::Value* array_len_field_addr =
1615 irb_.CreatePtrDisp(array, array_len_field_offset,
1616 irb_.getJIntTy()->getPointerTo());
1617
1618 // Load array length
1619 return irb_.CreateLoad(array_len_field_addr);
1620}
1621
1622
Logan Chien70f94b42011-12-27 17:49:11 +08001623void MethodCompiler::EmitInsn_ArrayLength(uint32_t dex_pc,
1624 Instruction const* insn) {
Logan Chien61bb6142012-02-03 15:34:53 +08001625
Elliott Hughesadb8c672012-03-06 16:49:32 -08001626 DecodedInstruction dec_insn(insn);
Logan Chien61bb6142012-02-03 15:34:53 +08001627
1628 // Get the array object address
Elliott Hughesadb8c672012-03-06 16:49:32 -08001629 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
Logan Chien61bb6142012-02-03 15:34:53 +08001630 EmitGuard_NullPointerException(dex_pc, array_addr);
1631
1632 // Get the array length and store it to the register
1633 llvm::Value* array_len = EmitLoadArrayLength(array_addr);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001634 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, array_len);
Logan Chien61bb6142012-02-03 15:34:53 +08001635
Logan Chien70f94b42011-12-27 17:49:11 +08001636 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1637}
1638
1639
1640void MethodCompiler::EmitInsn_NewInstance(uint32_t dex_pc,
1641 Instruction const* insn) {
Logan Chien032bdad2012-01-16 09:59:23 +08001642
Elliott Hughesadb8c672012-03-06 16:49:32 -08001643 DecodedInstruction dec_insn(insn);
Logan Chien032bdad2012-01-16 09:59:23 +08001644
1645 llvm::Function* runtime_func;
1646 if (compiler_->CanAccessTypeWithoutChecks(method_idx_, dex_cache_,
Elliott Hughesadb8c672012-03-06 16:49:32 -08001647 *dex_file_, dec_insn.vB)) {
Logan Chien032bdad2012-01-16 09:59:23 +08001648 runtime_func = irb_.GetRuntime(AllocObject);
1649 } else {
1650 runtime_func = irb_.GetRuntime(AllocObjectWithAccessCheck);
1651 }
1652
Elliott Hughesadb8c672012-03-06 16:49:32 -08001653 llvm::Constant* type_index_value = irb_.getInt32(dec_insn.vB);
Logan Chien032bdad2012-01-16 09:59:23 +08001654
1655 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1656
Logan Chien8dfcbea2012-02-17 18:50:32 +08001657 EmitUpdateLineNumFromDexPC(dex_pc);
1658
Logan Chien032bdad2012-01-16 09:59:23 +08001659 llvm::Value* object_addr =
1660 irb_.CreateCall2(runtime_func, type_index_value, method_object_addr);
1661
1662 EmitGuard_ExceptionLandingPad(dex_pc);
1663
Elliott Hughesadb8c672012-03-06 16:49:32 -08001664 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, object_addr);
Logan Chien032bdad2012-01-16 09:59:23 +08001665
Logan Chien70f94b42011-12-27 17:49:11 +08001666 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1667}
1668
1669
Logan Chiena2cc6a32012-01-16 10:38:41 +08001670llvm::Value* MethodCompiler::EmitAllocNewArray(uint32_t dex_pc,
1671 int32_t length,
1672 uint32_t type_idx,
1673 bool is_filled_new_array) {
1674 llvm::Function* runtime_func;
1675
1676 bool skip_access_check =
1677 compiler_->CanAccessTypeWithoutChecks(method_idx_, dex_cache_,
1678 *dex_file_, type_idx);
1679
1680 if (is_filled_new_array) {
1681 runtime_func = skip_access_check ?
1682 irb_.GetRuntime(CheckAndAllocArray) :
1683 irb_.GetRuntime(CheckAndAllocArrayWithAccessCheck);
1684 } else {
1685 runtime_func = skip_access_check ?
1686 irb_.GetRuntime(AllocArray) :
1687 irb_.GetRuntime(AllocArrayWithAccessCheck);
1688 }
1689
1690 llvm::Constant* type_index_value = irb_.getInt32(type_idx);
1691
1692 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1693
1694 llvm::Value* array_length_value = irb_.getInt32(length);
1695
Logan Chien8dfcbea2012-02-17 18:50:32 +08001696 EmitUpdateLineNumFromDexPC(dex_pc);
1697
Logan Chiena2cc6a32012-01-16 10:38:41 +08001698 llvm::Value* object_addr =
1699 irb_.CreateCall3(runtime_func, type_index_value, method_object_addr,
1700 array_length_value);
1701
1702 EmitGuard_ExceptionLandingPad(dex_pc);
1703
1704 return object_addr;
1705}
1706
1707
Logan Chien70f94b42011-12-27 17:49:11 +08001708void MethodCompiler::EmitInsn_NewArray(uint32_t dex_pc,
1709 Instruction const* insn) {
Logan Chiena2cc6a32012-01-16 10:38:41 +08001710
Elliott Hughesadb8c672012-03-06 16:49:32 -08001711 DecodedInstruction dec_insn(insn);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001712
1713 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001714 EmitAllocNewArray(dex_pc, dec_insn.vB, dec_insn.vC, false);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001715
Elliott Hughesadb8c672012-03-06 16:49:32 -08001716 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, object_addr);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001717
Logan Chien70f94b42011-12-27 17:49:11 +08001718 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1719}
1720
1721
1722void MethodCompiler::EmitInsn_FilledNewArray(uint32_t dex_pc,
1723 Instruction const* insn,
1724 bool is_range) {
Logan Chiena85fb2f2012-01-16 12:52:56 +08001725
Elliott Hughesadb8c672012-03-06 16:49:32 -08001726 DecodedInstruction dec_insn(insn);
Logan Chiena85fb2f2012-01-16 12:52:56 +08001727
1728 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001729 EmitAllocNewArray(dex_pc, dec_insn.vA, dec_insn.vB, true);
Logan Chiena85fb2f2012-01-16 12:52:56 +08001730
Elliott Hughesadb8c672012-03-06 16:49:32 -08001731 if (dec_insn.vA > 0) {
Logan Chiena85fb2f2012-01-16 12:52:56 +08001732 llvm::Value* object_addr_int =
1733 irb_.CreatePtrToInt(object_addr, irb_.getPtrEquivIntTy());
1734
Ian Rogers04ec04e2012-02-28 16:15:33 -08001735 // TODO: currently FilledNewArray doesn't support I, J, D and L, [ so computing the component
1736 // size using int alignment is safe. This code should determine the width of the FilledNewArray
1737 // component.
Logan Chiena85fb2f2012-01-16 12:52:56 +08001738 llvm::Value* data_field_offset =
Ian Rogers04ec04e2012-02-28 16:15:33 -08001739 irb_.getPtrEquivInt(Array::DataOffset(sizeof(int32_t)).Int32Value());
Logan Chiena85fb2f2012-01-16 12:52:56 +08001740
1741 llvm::Value* data_field_addr_int =
1742 irb_.CreateAdd(object_addr_int, data_field_offset);
1743
Elliott Hughesadb8c672012-03-06 16:49:32 -08001744 Class* klass = method_->GetDexCacheResolvedTypes()->Get(dec_insn.vB);
Logan Chiena85fb2f2012-01-16 12:52:56 +08001745 CHECK_NE(klass, static_cast<Class*>(NULL));
1746 // Moved this below already: CHECK(!klass->IsPrimitive() || klass->IsPrimitiveInt());
1747
1748 llvm::Constant* word_size = irb_.getSizeOfPtrEquivIntValue();
1749
1750 llvm::Type* field_type;
1751 if (klass->IsPrimitiveInt()) {
1752 field_type = irb_.getJIntTy()->getPointerTo();
1753 } else {
1754 CHECK(!klass->IsPrimitive());
1755 field_type = irb_.getJObjectTy()->getPointerTo();
1756 }
1757
1758 // TODO: Tune this code. Currently we are generating one instruction for
1759 // one element which may be very space consuming. Maybe changing to use
1760 // memcpy may help; however, since we can't guarantee that the alloca of
1761 // dalvik register are continuous, we can't perform such optimization yet.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001762 for (uint32_t i = 0; i < dec_insn.vA; ++i) {
Logan Chiena85fb2f2012-01-16 12:52:56 +08001763 llvm::Value* data_field_addr =
1764 irb_.CreateIntToPtr(data_field_addr_int, field_type);
1765
1766 int reg_index;
1767 if (is_range) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001768 reg_index = dec_insn.vC + i;
Logan Chiena85fb2f2012-01-16 12:52:56 +08001769 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001770 reg_index = dec_insn.arg[i];
Logan Chiena85fb2f2012-01-16 12:52:56 +08001771 }
1772
1773 llvm::Value* reg_value;
1774 if (klass->IsPrimitiveInt()) {
1775 reg_value = EmitLoadDalvikReg(reg_index, kInt, kAccurate);
1776 } else {
1777 reg_value = EmitLoadDalvikReg(reg_index, kObject, kAccurate);
1778 }
1779
1780 irb_.CreateStore(reg_value, data_field_addr);
1781
1782 data_field_addr_int = irb_.CreateAdd(data_field_addr_int, word_size);
1783 }
1784 }
1785
1786 EmitStoreDalvikRetValReg(kObject, kAccurate, object_addr);
1787
Logan Chien70f94b42011-12-27 17:49:11 +08001788 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1789}
1790
1791
1792void MethodCompiler::EmitInsn_FillArrayData(uint32_t dex_pc,
1793 Instruction const* insn) {
Logan Chiene58b6582012-01-16 17:13:13 +08001794
Elliott Hughesadb8c672012-03-06 16:49:32 -08001795 DecodedInstruction dec_insn(insn);
Logan Chiene58b6582012-01-16 17:13:13 +08001796
1797 // Read the payload
1798 struct PACKED Payload {
1799 uint16_t ident_;
1800 uint16_t elem_width_;
1801 uint32_t num_elems_;
1802 uint8_t data_[];
1803 };
1804
1805 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
Elliott Hughesadb8c672012-03-06 16:49:32 -08001806 static_cast<int32_t>(dec_insn.vB);
Logan Chiene58b6582012-01-16 17:13:13 +08001807
1808 Payload const* payload =
1809 reinterpret_cast<Payload const*>(code_item_->insns_ + payload_offset);
1810
1811 uint32_t size_in_bytes = payload->elem_width_ * payload->num_elems_;
1812
1813 // Load and check the array
Elliott Hughesadb8c672012-03-06 16:49:32 -08001814 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiene58b6582012-01-16 17:13:13 +08001815
1816 EmitGuard_NullPointerException(dex_pc, array_addr);
1817
1818 if (payload->num_elems_ > 0) {
1819 // Test: Is array length big enough?
1820 llvm::Constant* last_index = irb_.getJInt(payload->num_elems_ - 1);
1821
1822 EmitGuard_ArrayIndexOutOfBoundsException(dex_pc, array_addr, last_index);
1823
Ian Rogers04ec04e2012-02-28 16:15:33 -08001824 // TODO: currently FillArray doesn't support I, J, D and L, [ so computing the component
1825 // size using int alignment is safe. This code should determine the width of the FillArray
1826 // component.
Logan Chiene58b6582012-01-16 17:13:13 +08001827 // Get array data field
1828 llvm::Value* data_field_offset_value =
Ian Rogers04ec04e2012-02-28 16:15:33 -08001829 irb_.getPtrEquivInt(Array::DataOffset(sizeof(int32_t)).Int32Value());
Logan Chiene58b6582012-01-16 17:13:13 +08001830
1831 llvm::Value* data_field_addr =
1832 irb_.CreatePtrDisp(array_addr, data_field_offset_value,
1833 irb_.getInt8Ty()->getPointerTo());
1834
1835 // Emit payload to bitcode constant pool
1836 std::vector<llvm::Constant*> const_pool_data;
1837 for (uint32_t i = 0; i < size_in_bytes; ++i) {
1838 const_pool_data.push_back(irb_.getInt8(payload->data_[i]));
1839 }
1840
1841 llvm::Constant* const_pool_data_array_value = llvm::ConstantArray::get(
1842 llvm::ArrayType::get(irb_.getInt8Ty(), size_in_bytes), const_pool_data);
1843
1844 llvm::Value* const_pool_data_array_addr =
1845 new llvm::GlobalVariable(*module_,
1846 const_pool_data_array_value->getType(),
1847 false, llvm::GlobalVariable::InternalLinkage,
1848 const_pool_data_array_value,
1849 "array_data_payload");
1850
1851 // Find the memcpy intrinsic
1852 llvm::Type* memcpy_arg_types[] = {
1853 llvm::Type::getInt8Ty(*context_)->getPointerTo(),
1854 llvm::Type::getInt8Ty(*context_)->getPointerTo(),
1855 llvm::Type::getInt32Ty(*context_)
1856 };
1857
1858 llvm::Function* memcpy_intrinsic =
1859 llvm::Intrinsic::getDeclaration(module_,
1860 llvm::Intrinsic::memcpy,
1861 memcpy_arg_types);
1862
1863 // Copy now!
1864 llvm::Value *args[] = {
1865 data_field_addr,
1866 irb_.CreateConstGEP2_32(const_pool_data_array_addr, 0, 0),
1867 irb_.getInt32(size_in_bytes),
1868 irb_.getInt32(0), // alignment: no guarantee
1869 irb_.getFalse() // is_volatile: false
1870 };
1871
1872 irb_.CreateCall(memcpy_intrinsic, args);
1873 }
1874
Logan Chien70f94b42011-12-27 17:49:11 +08001875 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1876}
1877
1878
1879void MethodCompiler::EmitInsn_UnconditionalBranch(uint32_t dex_pc,
1880 Instruction const* insn) {
Logan Chiena466c162011-12-27 17:55:46 +08001881
Elliott Hughesadb8c672012-03-06 16:49:32 -08001882 DecodedInstruction dec_insn(insn);
Logan Chiena466c162011-12-27 17:55:46 +08001883
Elliott Hughesadb8c672012-03-06 16:49:32 -08001884 int32_t branch_offset = dec_insn.vA;
Logan Chiena466c162011-12-27 17:55:46 +08001885
1886 if (branch_offset <= 0) {
1887 // Garbage collection safe-point on backward branch
1888 EmitGuard_GarbageCollectionSuspend(dex_pc);
1889 }
1890
1891 irb_.CreateBr(GetBasicBlock(dex_pc + branch_offset));
Logan Chien70f94b42011-12-27 17:49:11 +08001892}
1893
1894
1895void MethodCompiler::EmitInsn_PackedSwitch(uint32_t dex_pc,
1896 Instruction const* insn) {
Logan Chien7a89b6d2011-12-27 17:56:56 +08001897
Elliott Hughesadb8c672012-03-06 16:49:32 -08001898 DecodedInstruction dec_insn(insn);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001899
1900 struct PACKED Payload {
1901 uint16_t ident_;
1902 uint16_t num_cases_;
1903 int32_t first_key_;
1904 int32_t targets_[];
1905 };
1906
1907 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
Elliott Hughesadb8c672012-03-06 16:49:32 -08001908 static_cast<int32_t>(dec_insn.vB);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001909
1910 Payload const* payload =
1911 reinterpret_cast<Payload const*>(code_item_->insns_ + payload_offset);
1912
Elliott Hughesadb8c672012-03-06 16:49:32 -08001913 llvm::Value* value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001914
1915 llvm::SwitchInst* sw =
1916 irb_.CreateSwitch(value, GetNextBasicBlock(dex_pc), payload->num_cases_);
1917
1918 for (uint16_t i = 0; i < payload->num_cases_; ++i) {
1919 sw->addCase(irb_.getInt32(payload->first_key_ + i),
1920 GetBasicBlock(dex_pc + payload->targets_[i]));
1921 }
Logan Chien70f94b42011-12-27 17:49:11 +08001922}
1923
1924
1925void MethodCompiler::EmitInsn_SparseSwitch(uint32_t dex_pc,
1926 Instruction const* insn) {
Logan Chien7a89b6d2011-12-27 17:56:56 +08001927
Elliott Hughesadb8c672012-03-06 16:49:32 -08001928 DecodedInstruction dec_insn(insn);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001929
1930 struct PACKED Payload {
1931 uint16_t ident_;
1932 uint16_t num_cases_;
1933 int32_t keys_and_targets_[];
1934 };
1935
1936 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
Elliott Hughesadb8c672012-03-06 16:49:32 -08001937 static_cast<int32_t>(dec_insn.vB);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001938
1939 Payload const* payload =
1940 reinterpret_cast<Payload const*>(code_item_->insns_ + payload_offset);
1941
1942 int32_t const* keys = payload->keys_and_targets_;
1943 int32_t const* targets = payload->keys_and_targets_ + payload->num_cases_;
1944
Elliott Hughesadb8c672012-03-06 16:49:32 -08001945 llvm::Value* value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001946
1947 llvm::SwitchInst* sw =
1948 irb_.CreateSwitch(value, GetNextBasicBlock(dex_pc), payload->num_cases_);
1949
1950 for (size_t i = 0; i < payload->num_cases_; ++i) {
1951 sw->addCase(irb_.getInt32(keys[i]), GetBasicBlock(dex_pc + targets[i]));
1952 }
Logan Chien70f94b42011-12-27 17:49:11 +08001953}
1954
1955
1956void MethodCompiler::EmitInsn_FPCompare(uint32_t dex_pc,
1957 Instruction const* insn,
1958 JType fp_jty,
1959 bool gt_bias) {
Logan Chien2c37e8e2011-12-27 17:58:46 +08001960
Elliott Hughesadb8c672012-03-06 16:49:32 -08001961 DecodedInstruction dec_insn(insn);
Logan Chien2c37e8e2011-12-27 17:58:46 +08001962
1963 DCHECK(fp_jty == kFloat || fp_jty == kDouble) << "JType: " << fp_jty;
1964
Elliott Hughesadb8c672012-03-06 16:49:32 -08001965 llvm::Value* src1_value = EmitLoadDalvikReg(dec_insn.vB, fp_jty, kAccurate);
1966 llvm::Value* src2_value = EmitLoadDalvikReg(dec_insn.vC, fp_jty, kAccurate);
Logan Chien2c37e8e2011-12-27 17:58:46 +08001967
1968 llvm::Value* cmp_eq = irb_.CreateFCmpOEQ(src1_value, src2_value);
1969 llvm::Value* cmp_lt;
1970
1971 if (gt_bias) {
1972 cmp_lt = irb_.CreateFCmpOLT(src1_value, src2_value);
1973 } else {
1974 cmp_lt = irb_.CreateFCmpULT(src1_value, src2_value);
1975 }
1976
1977 llvm::Value* result = EmitCompareResultSelection(cmp_eq, cmp_lt);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001978 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result);
Logan Chien2c37e8e2011-12-27 17:58:46 +08001979
Logan Chien70f94b42011-12-27 17:49:11 +08001980 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1981}
1982
1983
1984void MethodCompiler::EmitInsn_LongCompare(uint32_t dex_pc,
1985 Instruction const* insn) {
Logan Chien2c37e8e2011-12-27 17:58:46 +08001986
Elliott Hughesadb8c672012-03-06 16:49:32 -08001987 DecodedInstruction dec_insn(insn);
Logan Chien2c37e8e2011-12-27 17:58:46 +08001988
Elliott Hughesadb8c672012-03-06 16:49:32 -08001989 llvm::Value* src1_value = EmitLoadDalvikReg(dec_insn.vB, kLong, kAccurate);
1990 llvm::Value* src2_value = EmitLoadDalvikReg(dec_insn.vC, kLong, kAccurate);
Logan Chien2c37e8e2011-12-27 17:58:46 +08001991
1992 llvm::Value* cmp_eq = irb_.CreateICmpEQ(src1_value, src2_value);
1993 llvm::Value* cmp_lt = irb_.CreateICmpSLT(src1_value, src2_value);
1994
1995 llvm::Value* result = EmitCompareResultSelection(cmp_eq, cmp_lt);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001996 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result);
Logan Chien2c37e8e2011-12-27 17:58:46 +08001997
Logan Chien70f94b42011-12-27 17:49:11 +08001998 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1999}
2000
2001
Logan Chien2c37e8e2011-12-27 17:58:46 +08002002llvm::Value* MethodCompiler::EmitCompareResultSelection(llvm::Value* cmp_eq,
2003 llvm::Value* cmp_lt) {
2004
2005 llvm::Constant* zero = irb_.getJInt(0);
2006 llvm::Constant* pos1 = irb_.getJInt(1);
2007 llvm::Constant* neg1 = irb_.getJInt(-1);
2008
2009 llvm::Value* result_lt = irb_.CreateSelect(cmp_lt, neg1, pos1);
2010 llvm::Value* result_eq = irb_.CreateSelect(cmp_eq, zero, result_lt);
2011
2012 return result_eq;
2013}
2014
2015
Logan Chien70f94b42011-12-27 17:49:11 +08002016void MethodCompiler::EmitInsn_BinaryConditionalBranch(uint32_t dex_pc,
2017 Instruction const* insn,
2018 CondBranchKind cond) {
Logan Chiena78e3c82011-12-27 17:59:35 +08002019
Elliott Hughesadb8c672012-03-06 16:49:32 -08002020 DecodedInstruction dec_insn(insn);
Logan Chiena78e3c82011-12-27 17:59:35 +08002021
Elliott Hughesadb8c672012-03-06 16:49:32 -08002022 int8_t src1_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vA);
2023 int8_t src2_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vB);
Logan Chiena78e3c82011-12-27 17:59:35 +08002024
2025 DCHECK_NE(kRegUnknown, src1_reg_cat);
2026 DCHECK_NE(kRegUnknown, src2_reg_cat);
2027 DCHECK_NE(kRegCat2, src1_reg_cat);
2028 DCHECK_NE(kRegCat2, src2_reg_cat);
2029
Elliott Hughesadb8c672012-03-06 16:49:32 -08002030 int32_t branch_offset = dec_insn.vC;
Logan Chiena78e3c82011-12-27 17:59:35 +08002031
2032 if (branch_offset <= 0) {
2033 // Garbage collection safe-point on backward branch
2034 EmitGuard_GarbageCollectionSuspend(dex_pc);
2035 }
2036
2037 if (src1_reg_cat == kRegZero && src2_reg_cat == kRegZero) {
2038 irb_.CreateBr(GetBasicBlock(dex_pc + branch_offset));
2039 return;
2040 }
2041
2042 llvm::Value* src1_value;
2043 llvm::Value* src2_value;
2044
2045 if (src1_reg_cat != kRegZero && src2_reg_cat != kRegZero) {
2046 CHECK_EQ(src1_reg_cat, src2_reg_cat);
2047
2048 if (src1_reg_cat == kRegCat1nr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002049 src1_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
2050 src2_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002051 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002052 src1_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
2053 src2_value = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002054 }
2055 } else {
2056 DCHECK(src1_reg_cat == kRegZero ||
2057 src2_reg_cat == kRegZero);
2058
2059 if (src1_reg_cat == kRegZero) {
2060 if (src2_reg_cat == kRegCat1nr) {
2061 src1_value = irb_.getJInt(0);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002062 src2_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002063 } else {
2064 src1_value = irb_.getJNull();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002065 src2_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002066 }
2067 } else { // src2_reg_cat == kRegZero
2068 if (src2_reg_cat == kRegCat1nr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002069 src1_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002070 src2_value = irb_.getJInt(0);
2071 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002072 src1_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002073 src2_value = irb_.getJNull();
2074 }
2075 }
2076 }
2077
2078 llvm::Value* cond_value =
2079 EmitConditionResult(src1_value, src2_value, cond);
2080
2081 irb_.CreateCondBr(cond_value,
2082 GetBasicBlock(dex_pc + branch_offset),
2083 GetNextBasicBlock(dex_pc));
Logan Chien70f94b42011-12-27 17:49:11 +08002084}
2085
2086
2087void MethodCompiler::EmitInsn_UnaryConditionalBranch(uint32_t dex_pc,
2088 Instruction const* insn,
2089 CondBranchKind cond) {
Logan Chiena78e3c82011-12-27 17:59:35 +08002090
Elliott Hughesadb8c672012-03-06 16:49:32 -08002091 DecodedInstruction dec_insn(insn);
Logan Chiena78e3c82011-12-27 17:59:35 +08002092
Elliott Hughesadb8c672012-03-06 16:49:32 -08002093 int8_t src_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vA);
Logan Chiena78e3c82011-12-27 17:59:35 +08002094
2095 DCHECK_NE(kRegUnknown, src_reg_cat);
2096 DCHECK_NE(kRegCat2, src_reg_cat);
2097
Elliott Hughesadb8c672012-03-06 16:49:32 -08002098 int32_t branch_offset = dec_insn.vB;
Logan Chiena78e3c82011-12-27 17:59:35 +08002099
2100 if (branch_offset <= 0) {
2101 // Garbage collection safe-point on backward branch
2102 EmitGuard_GarbageCollectionSuspend(dex_pc);
2103 }
2104
2105 if (src_reg_cat == kRegZero) {
2106 irb_.CreateBr(GetBasicBlock(dex_pc + branch_offset));
2107 return;
2108 }
2109
2110 llvm::Value* src1_value;
2111 llvm::Value* src2_value;
2112
2113 if (src_reg_cat == kRegCat1nr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002114 src1_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002115 src2_value = irb_.getInt32(0);
2116 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002117 src1_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002118 src2_value = irb_.getJNull();
2119 }
2120
2121 llvm::Value* cond_value =
2122 EmitConditionResult(src1_value, src2_value, cond);
2123
2124 irb_.CreateCondBr(cond_value,
2125 GetBasicBlock(dex_pc + branch_offset),
2126 GetNextBasicBlock(dex_pc));
2127}
2128
2129
2130RegCategory MethodCompiler::GetInferredRegCategory(uint32_t dex_pc,
2131 uint16_t reg_idx) {
2132 InferredRegCategoryMap const* map = method_->GetInferredRegCategoryMap();
2133 CHECK_NE(map, static_cast<InferredRegCategoryMap*>(NULL));
2134
2135 return map->GetRegCategory(dex_pc, reg_idx);
2136}
2137
2138
2139llvm::Value* MethodCompiler::EmitConditionResult(llvm::Value* lhs,
2140 llvm::Value* rhs,
2141 CondBranchKind cond) {
2142 switch (cond) {
2143 case kCondBranch_EQ:
2144 return irb_.CreateICmpEQ(lhs, rhs);
2145
2146 case kCondBranch_NE:
2147 return irb_.CreateICmpNE(lhs, rhs);
2148
2149 case kCondBranch_LT:
2150 return irb_.CreateICmpSLT(lhs, rhs);
2151
2152 case kCondBranch_GE:
2153 return irb_.CreateICmpSGE(lhs, rhs);
2154
2155 case kCondBranch_GT:
2156 return irb_.CreateICmpSGT(lhs, rhs);
2157
2158 case kCondBranch_LE:
2159 return irb_.CreateICmpSLE(lhs, rhs);
2160
2161 default: // Unreachable
2162 LOG(FATAL) << "Unknown conditional branch kind: " << cond;
2163 return NULL;
2164 }
Logan Chien70f94b42011-12-27 17:49:11 +08002165}
2166
2167
Logan Chiene27fdbb2012-01-02 23:27:26 +08002168void
2169MethodCompiler::EmitGuard_ArrayIndexOutOfBoundsException(uint32_t dex_pc,
2170 llvm::Value* array,
2171 llvm::Value* index) {
2172 llvm::Value* array_len = EmitLoadArrayLength(array);
2173
2174 llvm::Value* cmp = irb_.CreateICmpUGE(index, array_len);
2175
2176 llvm::BasicBlock* block_exception =
2177 CreateBasicBlockWithDexPC(dex_pc, "overflow");
2178
2179 llvm::BasicBlock* block_continue =
2180 CreateBasicBlockWithDexPC(dex_pc, "cont");
2181
2182 irb_.CreateCondBr(cmp, block_exception, block_continue);
2183
2184 irb_.SetInsertPoint(block_exception);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002185
2186 EmitUpdateLineNumFromDexPC(dex_pc);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002187 irb_.CreateCall2(irb_.GetRuntime(ThrowIndexOutOfBounds), index, array_len);
2188 EmitBranchExceptionLandingPad(dex_pc);
2189
2190 irb_.SetInsertPoint(block_continue);
2191}
2192
2193
2194void MethodCompiler::EmitGuard_ArrayException(uint32_t dex_pc,
2195 llvm::Value* array,
2196 llvm::Value* index) {
2197 EmitGuard_NullPointerException(dex_pc, array);
2198 EmitGuard_ArrayIndexOutOfBoundsException(dex_pc, array, index);
2199}
2200
2201
2202// Emit Array GetElementPtr
2203llvm::Value* MethodCompiler::EmitArrayGEP(llvm::Value* array_addr,
2204 llvm::Value* index_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08002205 llvm::Type* elem_type,
2206 JType elem_jty) {
2207
2208 int data_offset;
2209 if (elem_jty == kLong || elem_jty == kDouble ||
2210 (elem_jty == kObject && sizeof(uint64_t) == sizeof(Object*))) {
2211 data_offset = Array::DataOffset(sizeof(int64_t)).Int32Value();
2212 } else {
2213 data_offset = Array::DataOffset(sizeof(int32_t)).Int32Value();
2214 }
Logan Chiene27fdbb2012-01-02 23:27:26 +08002215
2216 llvm::Constant* data_offset_value =
Ian Rogers04ec04e2012-02-28 16:15:33 -08002217 irb_.getPtrEquivInt(data_offset);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002218
2219 llvm::Value* array_data_addr =
2220 irb_.CreatePtrDisp(array_addr, data_offset_value,
2221 elem_type->getPointerTo());
2222
2223 return irb_.CreateGEP(array_data_addr, index_value);
2224}
2225
2226
Logan Chien70f94b42011-12-27 17:49:11 +08002227void MethodCompiler::EmitInsn_AGet(uint32_t dex_pc,
2228 Instruction const* insn,
2229 JType elem_jty) {
Logan Chiene27fdbb2012-01-02 23:27:26 +08002230
Elliott Hughesadb8c672012-03-06 16:49:32 -08002231 DecodedInstruction dec_insn(insn);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002232
Elliott Hughesadb8c672012-03-06 16:49:32 -08002233 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
2234 llvm::Value* index_value = EmitLoadDalvikReg(dec_insn.vC, kInt, kAccurate);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002235
2236 EmitGuard_ArrayException(dex_pc, array_addr, index_value);
2237
2238 llvm::Type* elem_type = irb_.getJType(elem_jty, kArray);
2239
2240 llvm::Value* array_elem_addr =
Ian Rogers04ec04e2012-02-28 16:15:33 -08002241 EmitArrayGEP(array_addr, index_value, elem_type, elem_jty);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002242
2243 llvm::Value* array_elem_value = irb_.CreateLoad(array_elem_addr);
2244
Elliott Hughesadb8c672012-03-06 16:49:32 -08002245 EmitStoreDalvikReg(dec_insn.vA, elem_jty, kArray, array_elem_value);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002246
Logan Chien70f94b42011-12-27 17:49:11 +08002247 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2248}
2249
2250
2251void MethodCompiler::EmitInsn_APut(uint32_t dex_pc,
2252 Instruction const* insn,
2253 JType elem_jty) {
Logan Chien8dabb432012-01-02 23:29:32 +08002254
Elliott Hughesadb8c672012-03-06 16:49:32 -08002255 DecodedInstruction dec_insn(insn);
Logan Chien8dabb432012-01-02 23:29:32 +08002256
Elliott Hughesadb8c672012-03-06 16:49:32 -08002257 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
2258 llvm::Value* index_value = EmitLoadDalvikReg(dec_insn.vC, kInt, kAccurate);
Logan Chien8dabb432012-01-02 23:29:32 +08002259
2260 EmitGuard_ArrayException(dex_pc, array_addr, index_value);
2261
2262 llvm::Type* elem_type = irb_.getJType(elem_jty, kArray);
2263
2264 llvm::Value* array_elem_addr =
Ian Rogers04ec04e2012-02-28 16:15:33 -08002265 EmitArrayGEP(array_addr, index_value, elem_type, elem_jty);
Logan Chien8dabb432012-01-02 23:29:32 +08002266
Elliott Hughesadb8c672012-03-06 16:49:32 -08002267 llvm::Value* new_value = EmitLoadDalvikReg(dec_insn.vA, elem_jty, kArray);
Logan Chien8dabb432012-01-02 23:29:32 +08002268
2269 irb_.CreateStore(new_value, array_elem_addr);
2270
Logan Chien70f94b42011-12-27 17:49:11 +08002271 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2272}
2273
2274
Logan Chien48f1d2a2012-01-02 22:49:53 +08002275void MethodCompiler::PrintUnresolvedFieldWarning(int32_t field_idx) {
2276 DexFile const& dex_file = method_helper_.GetDexFile();
2277 DexFile::FieldId const& field_id = dex_file.GetFieldId(field_idx);
2278
2279 LOG(WARNING) << "unable to resolve static field " << field_idx << " ("
2280 << dex_file.GetFieldName(field_id) << ") in "
2281 << dex_file.GetFieldDeclaringClassDescriptor(field_id);
2282}
2283
2284
Logan Chien70f94b42011-12-27 17:49:11 +08002285void MethodCompiler::EmitInsn_IGet(uint32_t dex_pc,
2286 Instruction const* insn,
2287 JType field_jty) {
Logan Chien48f1d2a2012-01-02 22:49:53 +08002288
Elliott Hughesadb8c672012-03-06 16:49:32 -08002289 DecodedInstruction dec_insn(insn);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002290
Elliott Hughesadb8c672012-03-06 16:49:32 -08002291 uint32_t reg_idx = dec_insn.vB;
2292 uint32_t field_idx = dec_insn.vC;
Logan Chien48f1d2a2012-01-02 22:49:53 +08002293
2294 Field* field = dex_cache_->GetResolvedField(field_idx);
2295
2296 llvm::Value* object_addr = EmitLoadDalvikReg(reg_idx, kObject, kAccurate);
2297
2298 EmitGuard_NullPointerException(dex_pc, object_addr);
2299
2300 llvm::Value* field_value;
2301
2302 if (field == NULL) {
2303 PrintUnresolvedFieldWarning(field_idx);
2304
2305 llvm::Function* runtime_func;
2306
2307 if (field_jty == kObject) {
Logan Chien3b2b2e72012-03-06 16:11:45 +08002308 runtime_func = irb_.GetRuntime(GetObjectInstance);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002309 } else if (field_jty == kLong || field_jty == kDouble) {
Logan Chien3b2b2e72012-03-06 16:11:45 +08002310 runtime_func = irb_.GetRuntime(Get64Instance);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002311 } else {
Logan Chien3b2b2e72012-03-06 16:11:45 +08002312 runtime_func = irb_.GetRuntime(Get32Instance);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002313 }
2314
2315 llvm::ConstantInt* field_idx_value = irb_.getInt32(field_idx);
2316
2317 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2318
Logan Chien8dfcbea2012-02-17 18:50:32 +08002319 EmitUpdateLineNumFromDexPC(dex_pc);
2320
Logan Chien3b2b2e72012-03-06 16:11:45 +08002321 field_value = irb_.CreateCall3(runtime_func, field_idx_value,
2322 method_object_addr, object_addr);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002323
2324 EmitGuard_ExceptionLandingPad(dex_pc);
2325
2326 } else {
2327 llvm::PointerType* field_type =
2328 irb_.getJType(field_jty, kField)->getPointerTo();
2329
2330 llvm::ConstantInt* field_offset =
2331 irb_.getPtrEquivInt(field->GetOffset().Int32Value());
2332
2333 llvm::Value* field_addr =
2334 irb_.CreatePtrDisp(object_addr, field_offset, field_type);
2335
2336 field_value = irb_.CreateLoad(field_addr);
2337 }
2338
Elliott Hughesadb8c672012-03-06 16:49:32 -08002339 EmitStoreDalvikReg(dec_insn.vA, field_jty, kField, field_value);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002340
Logan Chien70f94b42011-12-27 17:49:11 +08002341 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2342}
2343
2344
2345void MethodCompiler::EmitInsn_IPut(uint32_t dex_pc,
2346 Instruction const* insn,
2347 JType field_jty) {
Logan Chiendd6aa872012-01-03 16:06:32 +08002348
Elliott Hughesadb8c672012-03-06 16:49:32 -08002349 DecodedInstruction dec_insn(insn);
Logan Chiendd6aa872012-01-03 16:06:32 +08002350
Elliott Hughesadb8c672012-03-06 16:49:32 -08002351 uint32_t reg_idx = dec_insn.vB;
2352 uint32_t field_idx = dec_insn.vC;
Logan Chiendd6aa872012-01-03 16:06:32 +08002353
2354 Field* field = dex_cache_->GetResolvedField(field_idx);
2355
2356 llvm::Value* object_addr = EmitLoadDalvikReg(reg_idx, kObject, kAccurate);
2357
2358 EmitGuard_NullPointerException(dex_pc, object_addr);
2359
Elliott Hughesadb8c672012-03-06 16:49:32 -08002360 llvm::Value* new_value = EmitLoadDalvikReg(dec_insn.vA, field_jty, kField);
Logan Chiendd6aa872012-01-03 16:06:32 +08002361
2362 if (field == NULL) {
2363 PrintUnresolvedFieldWarning(field_idx);
2364
2365 llvm::Function* runtime_func;
2366
2367 if (field_jty == kObject) {
2368 runtime_func = irb_.GetRuntime(SetObjectInstance);
2369 } else if (field_jty == kLong || field_jty == kDouble) {
2370 runtime_func = irb_.GetRuntime(Set64Instance);
2371 } else {
2372 runtime_func = irb_.GetRuntime(Set32Instance);
2373 }
2374
2375 llvm::Value* field_idx_value = irb_.getInt32(field_idx);
2376
2377 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2378
Logan Chien8dfcbea2012-02-17 18:50:32 +08002379 EmitUpdateLineNumFromDexPC(dex_pc);
2380
Logan Chien3b2b2e72012-03-06 16:11:45 +08002381 irb_.CreateCall4(runtime_func, field_idx_value,
2382 method_object_addr, object_addr, new_value);
Logan Chiendd6aa872012-01-03 16:06:32 +08002383
2384 EmitGuard_ExceptionLandingPad(dex_pc);
2385
2386 } else {
2387 llvm::PointerType* field_type =
2388 irb_.getJType(field_jty, kField)->getPointerTo();
2389
2390 llvm::Value* field_offset =
2391 irb_.getPtrEquivInt(field->GetOffset().Int32Value());
2392
2393 llvm::Value* field_addr =
2394 irb_.CreatePtrDisp(object_addr, field_offset, field_type);
2395
2396 irb_.CreateStore(new_value, field_addr);
2397 }
2398
Logan Chien70f94b42011-12-27 17:49:11 +08002399 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2400}
2401
2402
Logan Chien2aeca0b2012-02-03 22:55:46 +08002403Method* MethodCompiler::ResolveMethod(uint32_t method_idx) {
2404 Thread* thread = Thread::Current();
2405
2406 // Save the exception state
2407 Throwable* old_exception = NULL;
2408 if (thread->IsExceptionPending()) {
2409 old_exception = thread->GetException();
2410 thread->ClearException();
2411 }
2412
2413 // Resolve the method through the class linker
2414 Method* method = class_linker_->ResolveMethod(*dex_file_, method_idx,
2415 dex_cache_, class_loader_,
2416 /* is_direct= */ false);
2417
2418 if (method == NULL) {
2419 // Ignore the exception raised during the method resolution
2420 thread->ClearException();
2421 }
2422
2423 // Restore the exception state
2424 if (old_exception != NULL) {
2425 thread->SetException(old_exception);
2426 }
2427
2428 return method;
2429}
2430
2431
Logan Chien438c4b62012-01-17 16:06:00 +08002432Field* MethodCompiler::ResolveField(uint32_t field_idx) {
2433 Thread* thread = Thread::Current();
2434
2435 // Save the exception state
2436 Throwable* old_exception = NULL;
2437 if (thread->IsExceptionPending()) {
2438 old_exception = thread->GetException();
2439 thread->ClearException();
2440 }
2441
2442 // Resolve the fields through the class linker
2443 Field* field = class_linker_->ResolveField(*dex_file_, field_idx,
2444 dex_cache_, class_loader_,
2445 /* is_static= */ true);
2446
2447 if (field == NULL) {
2448 // Ignore the exception raised during the field resolution
2449 thread->ClearException();
2450 }
2451
2452 // Restore the exception state
2453 if (old_exception != NULL) {
2454 thread->SetException(old_exception);
2455 }
2456
2457 return field;
2458}
2459
2460
2461Field* MethodCompiler::
2462FindFieldAndDeclaringTypeIdx(uint32_t field_idx,
2463 uint32_t &resolved_type_idx) {
2464
2465 // Resolve the field through the class linker
2466 Field* field = ResolveField(field_idx);
2467 if (field == NULL) {
2468 return NULL;
2469 }
2470
2471 DexFile::FieldId const& field_id = dex_file_->GetFieldId(field_idx);
2472
2473 // Search for the type_idx of the declaring class
2474 uint16_t type_idx = field_id.class_idx_;
2475 Class* klass = dex_cache_->GetResolvedTypes()->Get(type_idx);
2476
2477 // Test: Is referring_class equal to declaring_class?
2478
2479 // If true, then return the type_idx; otherwise, this field must be declared
2480 // in super class or interfaces, we have to search for declaring class.
2481
2482 if (field->GetDeclaringClass() == klass) {
2483 resolved_type_idx = type_idx;
2484 return field;
2485 }
2486
2487 // Search for the type_idx of the super class or interfaces
2488 std::string desc(FieldHelper(field).GetDeclaringClassDescriptor());
2489
2490 DexFile::StringId const* string_id = dex_file_->FindStringId(desc);
2491
2492 if (string_id == NULL) {
2493 return NULL;
2494 }
2495
2496 DexFile::TypeId const* type_id =
2497 dex_file_->FindTypeId(dex_file_->GetIndexForStringId(*string_id));
2498
2499 if (type_id == NULL) {
2500 return NULL;
2501 }
2502
2503 resolved_type_idx = dex_file_->GetIndexForTypeId(*type_id);
2504 return field;
2505}
2506
2507
2508llvm::Value* MethodCompiler::EmitLoadStaticStorage(uint32_t dex_pc,
2509 uint32_t type_idx) {
2510 llvm::BasicBlock* block_load_static =
2511 CreateBasicBlockWithDexPC(dex_pc, "load_static");
2512
2513 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
2514
2515 // Load static storage from dex cache
2516 llvm::Value* storage_field_addr =
2517 EmitLoadDexCacheStaticStorageFieldAddr(type_idx);
2518
2519 llvm::Value* storage_object_addr = irb_.CreateLoad(storage_field_addr);
2520
2521 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
2522
2523 // Test: Is the static storage of this class initialized?
2524 llvm::Value* equal_null =
2525 irb_.CreateICmpEQ(storage_object_addr, irb_.getJNull());
2526
2527 irb_.CreateCondBr(equal_null, block_load_static, block_cont);
2528
2529 // Failback routine to load the class object
2530 irb_.SetInsertPoint(block_load_static);
2531
2532 llvm::Function* runtime_func =
2533 irb_.GetRuntime(InitializeStaticStorage);
2534
2535 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
2536
2537 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2538
Logan Chien8dfcbea2012-02-17 18:50:32 +08002539 EmitUpdateLineNumFromDexPC(dex_pc);
2540
Logan Chien438c4b62012-01-17 16:06:00 +08002541 llvm::Value* loaded_storage_object_addr =
2542 irb_.CreateCall2(runtime_func, type_idx_value, method_object_addr);
2543
2544 EmitGuard_ExceptionLandingPad(dex_pc);
2545
2546 llvm::BasicBlock* block_after_load_static = irb_.GetInsertBlock();
2547
2548 irb_.CreateBr(block_cont);
2549
2550 // Now the class object must be loaded
2551 irb_.SetInsertPoint(block_cont);
2552
2553 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
2554
2555 phi->addIncoming(storage_object_addr, block_original);
2556 phi->addIncoming(loaded_storage_object_addr, block_after_load_static);
2557
2558 return phi;
2559}
2560
2561
Logan Chien70f94b42011-12-27 17:49:11 +08002562void MethodCompiler::EmitInsn_SGet(uint32_t dex_pc,
2563 Instruction const* insn,
2564 JType field_jty) {
Logan Chien438c4b62012-01-17 16:06:00 +08002565
Elliott Hughesadb8c672012-03-06 16:49:32 -08002566 DecodedInstruction dec_insn(insn);
Logan Chien438c4b62012-01-17 16:06:00 +08002567
2568 uint32_t declaring_type_idx = DexFile::kDexNoIndex;
2569
Elliott Hughesadb8c672012-03-06 16:49:32 -08002570 Field* field = FindFieldAndDeclaringTypeIdx(dec_insn.vB, declaring_type_idx);
Logan Chien438c4b62012-01-17 16:06:00 +08002571
2572 llvm::Value* static_field_value;
2573
2574 if (field == NULL) {
2575 llvm::Function* runtime_func;
2576
2577 if (field_jty == kObject) {
2578 runtime_func = irb_.GetRuntime(GetObjectStatic);
2579 } else if (field_jty == kLong || field_jty == kDouble) {
2580 runtime_func = irb_.GetRuntime(Get64Static);
2581 } else {
2582 runtime_func = irb_.GetRuntime(Get32Static);
2583 }
2584
Elliott Hughesadb8c672012-03-06 16:49:32 -08002585 llvm::Constant* field_idx_value = irb_.getInt32(dec_insn.vB);
Logan Chien438c4b62012-01-17 16:06:00 +08002586
2587 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2588
Logan Chien8dfcbea2012-02-17 18:50:32 +08002589 EmitUpdateLineNumFromDexPC(dex_pc);
2590
Logan Chien438c4b62012-01-17 16:06:00 +08002591 static_field_value =
2592 irb_.CreateCall2(runtime_func, field_idx_value, method_object_addr);
2593
2594 EmitGuard_ExceptionLandingPad(dex_pc);
2595
2596 } else {
2597 llvm::Value* static_storage_addr =
2598 EmitLoadStaticStorage(dex_pc, declaring_type_idx);
2599
2600 llvm::Value* static_field_offset_value =
2601 irb_.getPtrEquivInt(field->GetOffset().Int32Value());
2602
2603 llvm::Value* static_field_addr =
2604 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
2605 irb_.getJType(field_jty, kField)->getPointerTo());
2606
2607 static_field_value = irb_.CreateLoad(static_field_addr);
2608 }
2609
Elliott Hughesadb8c672012-03-06 16:49:32 -08002610 EmitStoreDalvikReg(dec_insn.vA, field_jty, kField, static_field_value);
Logan Chien438c4b62012-01-17 16:06:00 +08002611
Logan Chien70f94b42011-12-27 17:49:11 +08002612 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2613}
2614
2615
2616void MethodCompiler::EmitInsn_SPut(uint32_t dex_pc,
2617 Instruction const* insn,
2618 JType field_jty) {
Logan Chien14179c82012-01-17 17:06:34 +08002619
Elliott Hughesadb8c672012-03-06 16:49:32 -08002620 DecodedInstruction dec_insn(insn);
Logan Chien14179c82012-01-17 17:06:34 +08002621
2622 uint32_t declaring_type_idx = DexFile::kDexNoIndex;
2623
Elliott Hughesadb8c672012-03-06 16:49:32 -08002624 Field* field = FindFieldAndDeclaringTypeIdx(dec_insn.vB, declaring_type_idx);
Logan Chien14179c82012-01-17 17:06:34 +08002625
Elliott Hughesadb8c672012-03-06 16:49:32 -08002626 llvm::Value* new_value = EmitLoadDalvikReg(dec_insn.vA, field_jty, kField);
Logan Chien14179c82012-01-17 17:06:34 +08002627
2628 if (field == NULL) {
2629 llvm::Function* runtime_func;
2630
2631 if (field_jty == kObject) {
2632 runtime_func = irb_.GetRuntime(SetObjectStatic);
2633 } else if (field_jty == kLong || field_jty == kDouble) {
2634 runtime_func = irb_.GetRuntime(Set64Static);
2635 } else {
2636 runtime_func = irb_.GetRuntime(Set32Static);
2637 }
2638
Elliott Hughesadb8c672012-03-06 16:49:32 -08002639 llvm::Constant* field_idx_value = irb_.getInt32(dec_insn.vB);
Logan Chien14179c82012-01-17 17:06:34 +08002640
2641 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2642
Logan Chien8dfcbea2012-02-17 18:50:32 +08002643 EmitUpdateLineNumFromDexPC(dex_pc);
2644
Logan Chien14179c82012-01-17 17:06:34 +08002645 irb_.CreateCall3(runtime_func, field_idx_value,
2646 method_object_addr, new_value);
2647
2648 EmitGuard_ExceptionLandingPad(dex_pc);
2649
2650 } else {
2651 llvm::Value* static_storage_addr =
2652 EmitLoadStaticStorage(dex_pc, declaring_type_idx);
2653
2654 llvm::Value* static_field_offset_value =
2655 irb_.getPtrEquivInt(field->GetOffset().Int32Value());
2656
2657 llvm::Value* static_field_addr =
2658 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
2659 irb_.getJType(field_jty, kField)->getPointerTo());
2660
2661 irb_.CreateStore(new_value, static_field_addr);
2662 }
2663
Logan Chien70f94b42011-12-27 17:49:11 +08002664 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2665}
2666
2667
Elliott Hughesadb8c672012-03-06 16:49:32 -08002668llvm::Value* MethodCompiler::EmitLoadCalleeThis(DecodedInstruction const& dec_insn, bool is_range) {
Logan Chien1a121b92012-02-15 22:23:42 +08002669 if (is_range) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002670 return EmitLoadDalvikReg(dec_insn.vC, kObject, kAccurate);
Logan Chien1a121b92012-02-15 22:23:42 +08002671 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002672 return EmitLoadDalvikReg(dec_insn.arg[0], kObject, kAccurate);
Logan Chien1a121b92012-02-15 22:23:42 +08002673 }
2674}
2675
2676
2677void MethodCompiler::
2678EmitLoadActualParameters(std::vector<llvm::Value*>& args,
2679 uint32_t callee_method_idx,
Elliott Hughesadb8c672012-03-06 16:49:32 -08002680 DecodedInstruction const& dec_insn,
Logan Chien1a121b92012-02-15 22:23:42 +08002681 bool is_range,
2682 bool is_static) {
2683
2684 // Get method signature
2685 DexFile::MethodId const& method_id =
2686 dex_file_->GetMethodId(callee_method_idx);
2687
Logan Chien8faf8022012-02-24 12:25:29 +08002688 uint32_t shorty_size;
Logan Chien1a121b92012-02-15 22:23:42 +08002689 char const* shorty = dex_file_->GetMethodShorty(method_id, &shorty_size);
Logan Chien8faf8022012-02-24 12:25:29 +08002690 CHECK_GE(shorty_size, 1u);
Logan Chien1a121b92012-02-15 22:23:42 +08002691
2692 // Load argument values according to the shorty (without "this")
2693 uint16_t reg_count = 0;
2694
2695 if (!is_static) {
2696 ++reg_count; // skip the "this" pointer
2697 }
2698
Logan Chien8faf8022012-02-24 12:25:29 +08002699 for (uint32_t i = 1; i < shorty_size; ++i) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002700 uint32_t reg_idx = (is_range) ? (dec_insn.vC + reg_count)
2701 : (dec_insn.arg[reg_count]);
Logan Chien1a121b92012-02-15 22:23:42 +08002702
2703 args.push_back(EmitLoadDalvikReg(reg_idx, shorty[i], kAccurate));
2704
2705 ++reg_count;
2706 if (shorty[i] == 'J' || shorty[i] == 'D') {
2707 // Wide types, such as long and double, are using a pair of registers
2708 // to store the value, so we have to increase arg_reg again.
2709 ++reg_count;
2710 }
2711 }
2712
Elliott Hughesadb8c672012-03-06 16:49:32 -08002713 DCHECK_EQ(reg_count, dec_insn.vA)
Logan Chien1a121b92012-02-15 22:23:42 +08002714 << "Actual argument mismatch for callee: "
2715 << PrettyMethod(callee_method_idx, *dex_file_);
2716}
2717
2718
2719
Logan Chien70f94b42011-12-27 17:49:11 +08002720void MethodCompiler::EmitInsn_InvokeVirtual(uint32_t dex_pc,
2721 Instruction const* insn,
2722 bool is_range) {
Logan Chien46fbb412012-02-15 22:29:08 +08002723
Elliott Hughesadb8c672012-03-06 16:49:32 -08002724 DecodedInstruction dec_insn(insn);
Logan Chien46fbb412012-02-15 22:29:08 +08002725
Elliott Hughesadb8c672012-03-06 16:49:32 -08002726 uint32_t callee_method_idx = dec_insn.vB;
Logan Chien46fbb412012-02-15 22:29:08 +08002727
2728 // Find the method object at compile time
2729 Method* callee_method = ResolveMethod(callee_method_idx);
2730
2731 if (callee_method == NULL) {
2732 // Callee method can't be resolved at compile time. Emit the runtime
2733 // method resolution code.
2734
2735 UNIMPLEMENTED(FATAL) << "Method index " << callee_method_idx
2736 << " can't be resolved";
2737
2738 } else {
2739 // Test: Is *this* parameter equal to null?
2740 llvm::Value* this_addr = EmitLoadCalleeThis(dec_insn, is_range);
2741 EmitGuard_NullPointerException(dex_pc, this_addr);
2742
2743 // Load class object of *this* pointer
2744 llvm::Value* class_object_addr = EmitLoadClassObjectAddr(this_addr);
2745
2746 // Load callee method code address (branch destination)
2747 llvm::Value* vtable_addr = EmitLoadVTableAddr(class_object_addr);
2748
2749 llvm::Value* method_object_addr =
2750 EmitLoadMethodObjectAddrFromVTable(vtable_addr,
2751 callee_method->GetMethodIndex());
2752
2753 llvm::Value* code_addr =
2754 EmitLoadCodeAddr(method_object_addr, callee_method_idx, false);
2755
2756 // Load actual parameters
2757 std::vector<llvm::Value*> args;
2758 args.push_back(method_object_addr);
2759 args.push_back(this_addr);
2760 EmitLoadActualParameters(args, callee_method_idx, dec_insn,
2761 is_range, false);
2762
2763 // Invoke callee
Logan Chien8dfcbea2012-02-17 18:50:32 +08002764 EmitUpdateLineNumFromDexPC(dex_pc);
Logan Chien46fbb412012-02-15 22:29:08 +08002765 llvm::Value* retval = irb_.CreateCall(code_addr, args);
Logan Chien46fbb412012-02-15 22:29:08 +08002766 EmitGuard_ExceptionLandingPad(dex_pc);
2767
2768 MethodHelper method_helper(callee_method);
2769 char ret_shorty = method_helper.GetShorty()[0];
2770 if (ret_shorty != 'V') {
2771 EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval);
2772 }
2773 }
2774
Logan Chien70f94b42011-12-27 17:49:11 +08002775 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2776}
2777
2778
2779void MethodCompiler::EmitInsn_InvokeSuper(uint32_t dex_pc,
2780 Instruction const* insn,
2781 bool is_range) {
Logan Chiene73b6212012-01-27 01:47:29 +08002782
Elliott Hughesadb8c672012-03-06 16:49:32 -08002783 DecodedInstruction dec_insn(insn);
Logan Chiene73b6212012-01-27 01:47:29 +08002784
Elliott Hughesadb8c672012-03-06 16:49:32 -08002785 uint32_t callee_method_idx = dec_insn.vB;
Logan Chiene73b6212012-01-27 01:47:29 +08002786
2787 // Find the method object at compile time
2788 Method* callee_overiding_method = ResolveMethod(callee_method_idx);
2789
2790 if (callee_overiding_method == NULL) {
2791 // Callee method can't be resolved at compile time. Emit the runtime
2792 // method resolution code.
2793
2794 UNIMPLEMENTED(FATAL) << "Method index " << callee_method_idx
2795 << " can't be resolved";
2796
2797 } else {
2798 // CHECK: Is the instruction well-encoded or is the class hierarchy correct?
2799 Class* declaring_class = method_->GetDeclaringClass();
2800 CHECK_NE(declaring_class, static_cast<Class*>(NULL));
2801
2802 Class* super_class = declaring_class->GetSuperClass();
2803 CHECK_NE(super_class, static_cast<Class*>(NULL));
2804
2805 uint16_t callee_method_index = callee_overiding_method->GetMethodIndex();
2806 CHECK_GT(super_class->GetVTable()->GetLength(), callee_method_index);
2807
2808 Method* callee_method = super_class->GetVTable()->Get(callee_method_index);
2809 CHECK_NE(callee_method, static_cast<Method*>(NULL));
2810
2811 // Test: Is *this* parameter equal to null?
2812 llvm::Value* this_addr = EmitLoadCalleeThis(dec_insn, is_range);
2813 EmitGuard_NullPointerException(dex_pc, this_addr);
2814
2815 // Load declaring class of the caller method
2816 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2817
2818 llvm::ConstantInt* declaring_class_offset_value =
2819 irb_.getPtrEquivInt(Method::DeclaringClassOffset().Int32Value());
2820
2821 llvm::Value* declaring_class_field_addr =
2822 irb_.CreatePtrDisp(method_object_addr, declaring_class_offset_value,
2823 irb_.getJObjectTy()->getPointerTo());
2824
2825 llvm::Value* declaring_class_addr =
2826 irb_.CreateLoad(declaring_class_field_addr);
2827
2828 // Load the super class of the declaring class
2829 llvm::Value* super_class_offset_value =
2830 irb_.getPtrEquivInt(Class::SuperClassOffset().Int32Value());
2831
2832 llvm::Value* super_class_field_addr =
2833 irb_.CreatePtrDisp(declaring_class_addr, super_class_offset_value,
2834 irb_.getJObjectTy()->getPointerTo());
2835
2836 llvm::Value* super_class_addr =
2837 irb_.CreateLoad(super_class_field_addr);
2838
2839 // Load method object from virtual table
2840 llvm::Value* vtable_addr = EmitLoadVTableAddr(super_class_addr);
2841
2842 llvm::Value* callee_method_object_addr =
2843 EmitLoadMethodObjectAddrFromVTable(vtable_addr,
2844 callee_method->GetMethodIndex());
2845
2846 llvm::Value* code_addr =
2847 EmitLoadCodeAddr(callee_method_object_addr,
2848 callee_method_idx, false);
2849
2850 // Load actual parameters
2851 std::vector<llvm::Value*> args;
2852 args.push_back(callee_method_object_addr);
2853 args.push_back(this_addr);
2854 EmitLoadActualParameters(args, callee_method_idx, dec_insn,
2855 is_range, false);
2856
2857 // Invoke callee
Logan Chien8dfcbea2012-02-17 18:50:32 +08002858 EmitUpdateLineNumFromDexPC(dex_pc);
Logan Chiene73b6212012-01-27 01:47:29 +08002859 llvm::Value* retval = irb_.CreateCall(code_addr, args);
Logan Chiene73b6212012-01-27 01:47:29 +08002860 EmitGuard_ExceptionLandingPad(dex_pc);
2861
2862 MethodHelper method_helper(callee_method);
2863 char ret_shorty = method_helper.GetShorty()[0];
2864 if (ret_shorty != 'V') {
2865 EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval);
2866 }
2867 }
2868
Logan Chien70f94b42011-12-27 17:49:11 +08002869 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2870}
2871
2872
Logan Chien1a121b92012-02-15 22:23:42 +08002873void MethodCompiler::EmitInsn_InvokeStaticDirect(uint32_t dex_pc,
2874 Instruction const* insn,
Logan Chien61c65dc2012-02-29 03:22:30 +08002875 InvokeType invoke_type,
2876 bool is_range) {
Logan Chien70f94b42011-12-27 17:49:11 +08002877
Elliott Hughesadb8c672012-03-06 16:49:32 -08002878 DecodedInstruction dec_insn(insn);
Logan Chien70f94b42011-12-27 17:49:11 +08002879
Elliott Hughesadb8c672012-03-06 16:49:32 -08002880 uint32_t callee_method_idx = dec_insn.vB;
Logan Chien1a121b92012-02-15 22:23:42 +08002881
Logan Chien61c65dc2012-02-29 03:22:30 +08002882 bool is_static = (invoke_type == kStatic);
2883
2884 UniquePtr<OatCompilationUnit> callee_oatcompilation_unit(
Ian Rogers04ec04e2012-02-28 16:15:33 -08002885 oat_compilation_unit_->GetCallee(callee_method_idx, is_static ? kAccStatic : 0));
Logan Chien61c65dc2012-02-29 03:22:30 +08002886
2887 int vtable_idx = -1; // Currently unused
2888 bool is_fast_path = compiler_->
2889 ComputeInvokeInfo(callee_method_idx, callee_oatcompilation_unit.get(),
2890 invoke_type, vtable_idx);
2891
2892 CHECK(is_fast_path) << "Slow path for invoke static/direct is unimplemented";
Logan Chien1a121b92012-02-15 22:23:42 +08002893
2894 llvm::Value* this_addr = NULL;
2895
2896 if (!is_static) {
2897 // Test: Is *this* parameter equal to null?
2898 this_addr = EmitLoadCalleeThis(dec_insn, is_range);
2899 EmitGuard_NullPointerException(dex_pc, this_addr);
2900 }
2901
2902 // Load method function pointers
Logan Chien61c65dc2012-02-29 03:22:30 +08002903 llvm::Value* callee_method_object_field_addr =
2904 EmitLoadDexCacheResolvedMethodFieldAddr(callee_method_idx);
Logan Chien1a121b92012-02-15 22:23:42 +08002905
2906 llvm::Value* callee_method_object_addr =
Logan Chien61c65dc2012-02-29 03:22:30 +08002907 irb_.CreateLoad(callee_method_object_field_addr);
2908
2909 llvm::Value* code_addr =
2910 EmitLoadCodeAddr(callee_method_object_addr, callee_method_idx, is_static);
Logan Chien1a121b92012-02-15 22:23:42 +08002911
2912 // Load the actual parameter
2913 std::vector<llvm::Value*> args;
2914
2915 args.push_back(callee_method_object_addr); // method object for callee
2916
2917 if (!is_static) {
2918 args.push_back(this_addr); // "this" object for callee
2919 }
2920
2921 EmitLoadActualParameters(args, callee_method_idx, dec_insn,
2922 is_range, is_static);
2923
Logan Chien8dfcbea2012-02-17 18:50:32 +08002924 // Invoke callee
2925 EmitUpdateLineNumFromDexPC(dex_pc);
Logan Chien1a121b92012-02-15 22:23:42 +08002926 llvm::Value* retval = irb_.CreateCall(code_addr, args);
2927 EmitGuard_ExceptionLandingPad(dex_pc);
2928
Logan Chien61c65dc2012-02-29 03:22:30 +08002929 char ret_shorty = callee_oatcompilation_unit->GetShorty()[0];
Shih-wei Liao90d50992012-02-19 03:32:05 -08002930 if (ret_shorty != 'V') {
Logan Chien1a121b92012-02-15 22:23:42 +08002931 EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval);
2932 }
2933
Logan Chien70f94b42011-12-27 17:49:11 +08002934 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2935}
2936
2937
2938void MethodCompiler::EmitInsn_InvokeInterface(uint32_t dex_pc,
2939 Instruction const* insn,
2940 bool is_range) {
Logan Chien7caf37e2012-02-03 22:56:04 +08002941
Elliott Hughesadb8c672012-03-06 16:49:32 -08002942 DecodedInstruction dec_insn(insn);
Logan Chien7caf37e2012-02-03 22:56:04 +08002943
Elliott Hughesadb8c672012-03-06 16:49:32 -08002944 uint32_t callee_method_idx = dec_insn.vB;
Logan Chien7caf37e2012-02-03 22:56:04 +08002945
2946 // Resolve callee method
2947 Method* callee_method = ResolveMethod(callee_method_idx);
2948
2949 // Test: Is "this" pointer equal to null?
2950 llvm::Value* this_addr = EmitLoadCalleeThis(dec_insn, is_range);
2951 EmitGuard_NullPointerException(dex_pc, this_addr);
2952
2953 // Resolve the callee method object address at runtime
2954 llvm::Value* runtime_func = irb_.GetRuntime(FindInterfaceMethod);
2955
2956 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2957
2958 llvm::Value* callee_method_idx_value = irb_.getInt32(callee_method_idx);
2959
Logan Chien8dfcbea2012-02-17 18:50:32 +08002960 EmitUpdateLineNumFromDexPC(dex_pc);
2961
Logan Chien7caf37e2012-02-03 22:56:04 +08002962 llvm::Value* callee_method_object_addr =
2963 irb_.CreateCall2(runtime_func, callee_method_idx_value, method_object_addr);
2964
2965 EmitGuard_ExceptionLandingPad(dex_pc);
2966
2967 llvm::Value* code_addr =
2968 EmitLoadCodeAddr(callee_method_object_addr, callee_method_idx, false);
2969
2970 // Load the actual parameter
2971 std::vector<llvm::Value*> args;
2972 args.push_back(callee_method_object_addr);
2973 args.push_back(this_addr);
2974 EmitLoadActualParameters(args, callee_method_idx, dec_insn, is_range, false);
2975
Logan Chien8dfcbea2012-02-17 18:50:32 +08002976 // Invoke callee
2977 EmitUpdateLineNumFromDexPC(dex_pc);
Logan Chien7caf37e2012-02-03 22:56:04 +08002978 llvm::Value* retval = irb_.CreateCall(code_addr, args);
2979 EmitGuard_ExceptionLandingPad(dex_pc);
2980
2981 MethodHelper method_helper(callee_method);
2982 char ret_shorty = method_helper.GetShorty()[0];
2983 if (ret_shorty != 'V') {
2984 EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval);
2985 }
2986
Logan Chien70f94b42011-12-27 17:49:11 +08002987 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2988}
2989
2990
2991void MethodCompiler::EmitInsn_Neg(uint32_t dex_pc,
2992 Instruction const* insn,
2993 JType op_jty) {
Logan Chien1b5685f2011-12-27 18:01:14 +08002994
Elliott Hughesadb8c672012-03-06 16:49:32 -08002995 DecodedInstruction dec_insn(insn);
Logan Chien1b5685f2011-12-27 18:01:14 +08002996
2997 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
2998
Elliott Hughesadb8c672012-03-06 16:49:32 -08002999 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chien1b5685f2011-12-27 18:01:14 +08003000 llvm::Value* result_value = irb_.CreateNeg(src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003001 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chien1b5685f2011-12-27 18:01:14 +08003002
Logan Chien70f94b42011-12-27 17:49:11 +08003003 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3004}
3005
3006
3007void MethodCompiler::EmitInsn_Not(uint32_t dex_pc,
3008 Instruction const* insn,
3009 JType op_jty) {
Logan Chiene53750d2011-12-27 18:02:27 +08003010
Elliott Hughesadb8c672012-03-06 16:49:32 -08003011 DecodedInstruction dec_insn(insn);
Logan Chiene53750d2011-12-27 18:02:27 +08003012
3013 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3014
Elliott Hughesadb8c672012-03-06 16:49:32 -08003015 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chiene53750d2011-12-27 18:02:27 +08003016 llvm::Value* result_value =
3017 irb_.CreateXor(src_value, static_cast<uint64_t>(-1));
3018
Elliott Hughesadb8c672012-03-06 16:49:32 -08003019 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chiene53750d2011-12-27 18:02:27 +08003020
Logan Chien70f94b42011-12-27 17:49:11 +08003021 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3022}
3023
3024
3025void MethodCompiler::EmitInsn_SExt(uint32_t dex_pc,
3026 Instruction const* insn) {
Logan Chien61752ad2011-12-27 18:03:51 +08003027
Elliott Hughesadb8c672012-03-06 16:49:32 -08003028 DecodedInstruction dec_insn(insn);
Logan Chien61752ad2011-12-27 18:03:51 +08003029
Elliott Hughesadb8c672012-03-06 16:49:32 -08003030 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chien61752ad2011-12-27 18:03:51 +08003031 llvm::Value* result_value = irb_.CreateSExt(src_value, irb_.getJLongTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003032 EmitStoreDalvikReg(dec_insn.vA, kLong, kAccurate, result_value);
Logan Chien61752ad2011-12-27 18:03:51 +08003033
Logan Chien70f94b42011-12-27 17:49:11 +08003034 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3035}
3036
3037
3038void MethodCompiler::EmitInsn_Trunc(uint32_t dex_pc,
3039 Instruction const* insn) {
Logan Chien17a57662011-12-27 18:05:14 +08003040
Elliott Hughesadb8c672012-03-06 16:49:32 -08003041 DecodedInstruction dec_insn(insn);
Logan Chien17a57662011-12-27 18:05:14 +08003042
Elliott Hughesadb8c672012-03-06 16:49:32 -08003043 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kLong, kAccurate);
Logan Chien17a57662011-12-27 18:05:14 +08003044 llvm::Value* result_value = irb_.CreateTrunc(src_value, irb_.getJIntTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003045 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chien17a57662011-12-27 18:05:14 +08003046
Logan Chien70f94b42011-12-27 17:49:11 +08003047 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3048}
3049
3050
3051void MethodCompiler::EmitInsn_TruncAndSExt(uint32_t dex_pc,
3052 Instruction const* insn,
3053 unsigned N) {
Logan Chienb6744c52011-12-27 18:06:26 +08003054
Elliott Hughesadb8c672012-03-06 16:49:32 -08003055 DecodedInstruction dec_insn(insn);
Logan Chienb6744c52011-12-27 18:06:26 +08003056
Elliott Hughesadb8c672012-03-06 16:49:32 -08003057 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chienb6744c52011-12-27 18:06:26 +08003058
3059 llvm::Value* trunc_value =
3060 irb_.CreateTrunc(src_value, llvm::Type::getIntNTy(*context_, N));
3061
3062 llvm::Value* result_value = irb_.CreateSExt(trunc_value, irb_.getJIntTy());
3063
Elliott Hughesadb8c672012-03-06 16:49:32 -08003064 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chienb6744c52011-12-27 18:06:26 +08003065
Logan Chien70f94b42011-12-27 17:49:11 +08003066 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3067}
3068
3069
3070void MethodCompiler::EmitInsn_TruncAndZExt(uint32_t dex_pc,
3071 Instruction const* insn,
3072 unsigned N) {
Logan Chienb6744c52011-12-27 18:06:26 +08003073
Elliott Hughesadb8c672012-03-06 16:49:32 -08003074 DecodedInstruction dec_insn(insn);
Logan Chienb6744c52011-12-27 18:06:26 +08003075
Elliott Hughesadb8c672012-03-06 16:49:32 -08003076 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chienb6744c52011-12-27 18:06:26 +08003077
3078 llvm::Value* trunc_value =
3079 irb_.CreateTrunc(src_value, llvm::Type::getIntNTy(*context_, N));
3080
3081 llvm::Value* result_value = irb_.CreateZExt(trunc_value, irb_.getJIntTy());
3082
Elliott Hughesadb8c672012-03-06 16:49:32 -08003083 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chienb6744c52011-12-27 18:06:26 +08003084
Logan Chien70f94b42011-12-27 17:49:11 +08003085 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3086}
3087
3088
3089void MethodCompiler::EmitInsn_FNeg(uint32_t dex_pc,
3090 Instruction const* insn,
3091 JType op_jty) {
Logan Chien7a48b092011-12-27 18:07:45 +08003092
Elliott Hughesadb8c672012-03-06 16:49:32 -08003093 DecodedInstruction dec_insn(insn);
Logan Chien7a48b092011-12-27 18:07:45 +08003094
3095 DCHECK(op_jty == kFloat || op_jty == kDouble) << op_jty;
3096
Elliott Hughesadb8c672012-03-06 16:49:32 -08003097 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chien7a48b092011-12-27 18:07:45 +08003098 llvm::Value* result_value = irb_.CreateFNeg(src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003099 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chien7a48b092011-12-27 18:07:45 +08003100
Logan Chien70f94b42011-12-27 17:49:11 +08003101 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3102}
3103
3104
3105void MethodCompiler::EmitInsn_IntToFP(uint32_t dex_pc,
3106 Instruction const* insn,
3107 JType src_jty,
3108 JType dest_jty) {
Logan Chien62dd4532011-12-27 18:09:00 +08003109
Elliott Hughesadb8c672012-03-06 16:49:32 -08003110 DecodedInstruction dec_insn(insn);
Logan Chien62dd4532011-12-27 18:09:00 +08003111
3112 DCHECK(src_jty == kInt || src_jty == kLong) << src_jty;
3113 DCHECK(dest_jty == kFloat || dest_jty == kDouble) << dest_jty;
3114
Elliott Hughesadb8c672012-03-06 16:49:32 -08003115 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, src_jty, kAccurate);
Logan Chien62dd4532011-12-27 18:09:00 +08003116 llvm::Type* dest_type = irb_.getJType(dest_jty, kAccurate);
3117 llvm::Value* dest_value = irb_.CreateSIToFP(src_value, dest_type);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003118 EmitStoreDalvikReg(dec_insn.vA, dest_jty, kAccurate, dest_value);
Logan Chien62dd4532011-12-27 18:09:00 +08003119
Logan Chien70f94b42011-12-27 17:49:11 +08003120 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3121}
3122
3123
3124void MethodCompiler::EmitInsn_FPToInt(uint32_t dex_pc,
3125 Instruction const* insn,
3126 JType src_jty,
3127 JType dest_jty) {
Logan Chien12dc1752011-12-27 18:10:15 +08003128
Elliott Hughesadb8c672012-03-06 16:49:32 -08003129 DecodedInstruction dec_insn(insn);
Logan Chien12dc1752011-12-27 18:10:15 +08003130
3131 DCHECK(src_jty == kFloat || src_jty == kDouble) << src_jty;
3132 DCHECK(dest_jty == kInt || dest_jty == kLong) << dest_jty;
3133
Elliott Hughesadb8c672012-03-06 16:49:32 -08003134 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, src_jty, kAccurate);
Logan Chien12dc1752011-12-27 18:10:15 +08003135 llvm::Type* dest_type = irb_.getJType(dest_jty, kAccurate);
3136 llvm::Value* dest_value = irb_.CreateFPToSI(src_value, dest_type);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003137 EmitStoreDalvikReg(dec_insn.vA, dest_jty, kAccurate, dest_value);
Logan Chien12dc1752011-12-27 18:10:15 +08003138
Logan Chien70f94b42011-12-27 17:49:11 +08003139 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3140}
3141
3142
3143void MethodCompiler::EmitInsn_FExt(uint32_t dex_pc,
3144 Instruction const* insn) {
Logan Chienc56ded92011-12-27 18:10:57 +08003145
Elliott Hughesadb8c672012-03-06 16:49:32 -08003146 DecodedInstruction dec_insn(insn);
Logan Chienc56ded92011-12-27 18:10:57 +08003147
Elliott Hughesadb8c672012-03-06 16:49:32 -08003148 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kFloat, kAccurate);
Logan Chienc56ded92011-12-27 18:10:57 +08003149 llvm::Value* result_value = irb_.CreateFPExt(src_value, irb_.getJDoubleTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003150 EmitStoreDalvikReg(dec_insn.vA, kDouble, kAccurate, result_value);
Logan Chienc56ded92011-12-27 18:10:57 +08003151
Logan Chien70f94b42011-12-27 17:49:11 +08003152 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3153}
3154
3155
3156void MethodCompiler::EmitInsn_FTrunc(uint32_t dex_pc,
3157 Instruction const* insn) {
Logan Chien927744f2011-12-27 18:11:52 +08003158
Elliott Hughesadb8c672012-03-06 16:49:32 -08003159 DecodedInstruction dec_insn(insn);
Logan Chien927744f2011-12-27 18:11:52 +08003160
Elliott Hughesadb8c672012-03-06 16:49:32 -08003161 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kDouble, kAccurate);
Logan Chien927744f2011-12-27 18:11:52 +08003162 llvm::Value* result_value = irb_.CreateFPTrunc(src_value, irb_.getJFloatTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003163 EmitStoreDalvikReg(dec_insn.vA, kFloat, kAccurate, result_value);
Logan Chien927744f2011-12-27 18:11:52 +08003164
Logan Chien70f94b42011-12-27 17:49:11 +08003165 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3166}
3167
3168
3169void MethodCompiler::EmitInsn_IntArithm(uint32_t dex_pc,
3170 Instruction const* insn,
3171 IntArithmKind arithm,
3172 JType op_jty,
3173 bool is_2addr) {
Logan Chienc3f7d962011-12-27 18:13:18 +08003174
Elliott Hughesadb8c672012-03-06 16:49:32 -08003175 DecodedInstruction dec_insn(insn);
Logan Chienc3f7d962011-12-27 18:13:18 +08003176
3177 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3178
3179 llvm::Value* src1_value;
3180 llvm::Value* src2_value;
3181
3182 if (is_2addr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003183 src1_value = EmitLoadDalvikReg(dec_insn.vA, op_jty, kAccurate);
3184 src2_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chienc3f7d962011-12-27 18:13:18 +08003185 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003186 src1_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
3187 src2_value = EmitLoadDalvikReg(dec_insn.vC, op_jty, kAccurate);
Logan Chienc3f7d962011-12-27 18:13:18 +08003188 }
3189
3190 llvm::Value* result_value =
3191 EmitIntArithmResultComputation(dex_pc, src1_value, src2_value,
3192 arithm, op_jty);
3193
Elliott Hughesadb8c672012-03-06 16:49:32 -08003194 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chienc3f7d962011-12-27 18:13:18 +08003195
Logan Chien70f94b42011-12-27 17:49:11 +08003196 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3197}
3198
3199
3200void MethodCompiler::EmitInsn_IntArithmImmediate(uint32_t dex_pc,
3201 Instruction const* insn,
3202 IntArithmKind arithm) {
Logan Chienc3f7d962011-12-27 18:13:18 +08003203
Elliott Hughesadb8c672012-03-06 16:49:32 -08003204 DecodedInstruction dec_insn(insn);
Logan Chienc3f7d962011-12-27 18:13:18 +08003205
Elliott Hughesadb8c672012-03-06 16:49:32 -08003206 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chienc3f7d962011-12-27 18:13:18 +08003207
Elliott Hughesadb8c672012-03-06 16:49:32 -08003208 llvm::Value* imm_value = irb_.getInt32(dec_insn.vC);
Logan Chienc3f7d962011-12-27 18:13:18 +08003209
3210 llvm::Value* result_value =
3211 EmitIntArithmResultComputation(dex_pc, src_value, imm_value, arithm, kInt);
3212
Elliott Hughesadb8c672012-03-06 16:49:32 -08003213 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chienc3f7d962011-12-27 18:13:18 +08003214
Logan Chien70f94b42011-12-27 17:49:11 +08003215 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3216}
3217
3218
Logan Chienc3f7d962011-12-27 18:13:18 +08003219llvm::Value*
3220MethodCompiler::EmitIntArithmResultComputation(uint32_t dex_pc,
3221 llvm::Value* lhs,
3222 llvm::Value* rhs,
3223 IntArithmKind arithm,
3224 JType op_jty) {
3225 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3226
3227 switch (arithm) {
3228 case kIntArithm_Add:
3229 return irb_.CreateAdd(lhs, rhs);
3230
3231 case kIntArithm_Sub:
3232 return irb_.CreateSub(lhs, rhs);
3233
3234 case kIntArithm_Mul:
3235 return irb_.CreateMul(lhs, rhs);
3236
3237 case kIntArithm_Div:
3238 EmitGuard_DivZeroException(dex_pc, rhs, op_jty);
3239 return irb_.CreateSDiv(lhs, rhs);
3240
3241 case kIntArithm_Rem:
3242 EmitGuard_DivZeroException(dex_pc, rhs, op_jty);
3243 return irb_.CreateSRem(lhs, rhs);
3244
3245 case kIntArithm_And:
3246 return irb_.CreateAnd(lhs, rhs);
3247
3248 case kIntArithm_Or:
3249 return irb_.CreateOr(lhs, rhs);
3250
3251 case kIntArithm_Xor:
3252 return irb_.CreateXor(lhs, rhs);
3253
3254 case kIntArithm_Shl:
3255 if (op_jty == kLong) {
3256 return irb_.CreateShl(lhs, irb_.CreateAnd(rhs, 0x3f));
3257 } else {
3258 return irb_.CreateShl(lhs, irb_.CreateAnd(rhs, 0x1f));
3259 }
3260
3261 case kIntArithm_Shr:
3262 if (op_jty == kLong) {
3263 return irb_.CreateAShr(lhs, irb_.CreateAnd(rhs, 0x3f));
3264 } else {
3265 return irb_.CreateAShr(lhs, irb_.CreateAnd(rhs, 0x1f));
3266 }
3267
3268 case kIntArithm_UShr:
3269 if (op_jty == kLong) {
3270 return irb_.CreateLShr(lhs, irb_.CreateAnd(rhs, 0x3f));
3271 } else {
3272 return irb_.CreateLShr(lhs, irb_.CreateAnd(rhs, 0x1f));
3273 }
3274
3275 default:
3276 LOG(FATAL) << "Unknown integer arithmetic kind: " << arithm;
3277 return NULL;
3278 }
3279}
3280
3281
Logan Chien70f94b42011-12-27 17:49:11 +08003282void MethodCompiler::EmitInsn_RSubImmediate(uint32_t dex_pc,
3283 Instruction const* insn) {
Logan Chien65c62d42011-12-27 18:14:18 +08003284
Elliott Hughesadb8c672012-03-06 16:49:32 -08003285 DecodedInstruction dec_insn(insn);
Logan Chien65c62d42011-12-27 18:14:18 +08003286
Elliott Hughesadb8c672012-03-06 16:49:32 -08003287 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
3288 llvm::Value* imm_value = irb_.getInt32(dec_insn.vC);
Logan Chien65c62d42011-12-27 18:14:18 +08003289 llvm::Value* result_value = irb_.CreateSub(imm_value, src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003290 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chien65c62d42011-12-27 18:14:18 +08003291
Logan Chien70f94b42011-12-27 17:49:11 +08003292 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3293}
3294
3295
3296void MethodCompiler::EmitInsn_FPArithm(uint32_t dex_pc,
3297 Instruction const* insn,
3298 FPArithmKind arithm,
3299 JType op_jty,
3300 bool is_2addr) {
Logan Chien76e1c792011-12-27 18:15:01 +08003301
Elliott Hughesadb8c672012-03-06 16:49:32 -08003302 DecodedInstruction dec_insn(insn);
Logan Chien76e1c792011-12-27 18:15:01 +08003303
3304 DCHECK(op_jty == kFloat || op_jty == kDouble) << op_jty;
3305
3306 llvm::Value* src1_value;
3307 llvm::Value* src2_value;
3308
3309 if (is_2addr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003310 src1_value = EmitLoadDalvikReg(dec_insn.vA, op_jty, kAccurate);
3311 src2_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chien76e1c792011-12-27 18:15:01 +08003312 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003313 src1_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
3314 src2_value = EmitLoadDalvikReg(dec_insn.vC, op_jty, kAccurate);
Logan Chien76e1c792011-12-27 18:15:01 +08003315 }
3316
3317 llvm::Value* result_value =
3318 EmitFPArithmResultComputation(dex_pc, src1_value, src2_value, arithm);
3319
Elliott Hughesadb8c672012-03-06 16:49:32 -08003320 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chien76e1c792011-12-27 18:15:01 +08003321
Logan Chien70f94b42011-12-27 17:49:11 +08003322 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3323}
3324
3325
Logan Chien76e1c792011-12-27 18:15:01 +08003326llvm::Value*
3327MethodCompiler::EmitFPArithmResultComputation(uint32_t dex_pc,
3328 llvm::Value *lhs,
3329 llvm::Value *rhs,
3330 FPArithmKind arithm) {
3331 switch (arithm) {
3332 case kFPArithm_Add:
3333 return irb_.CreateFAdd(lhs, rhs);
3334
3335 case kFPArithm_Sub:
3336 return irb_.CreateFSub(lhs, rhs);
3337
3338 case kFPArithm_Mul:
3339 return irb_.CreateFMul(lhs, rhs);
3340
3341 case kFPArithm_Div:
3342 return irb_.CreateFDiv(lhs, rhs);
3343
3344 case kFPArithm_Rem:
3345 return irb_.CreateFRem(lhs, rhs);
3346
3347 default:
3348 LOG(FATAL) << "Unknown floating-point arithmetic kind: " << arithm;
3349 return NULL;
3350 }
3351}
3352
3353
Logan Chienc3f7d962011-12-27 18:13:18 +08003354void MethodCompiler::EmitGuard_DivZeroException(uint32_t dex_pc,
3355 llvm::Value* denominator,
3356 JType op_jty) {
3357 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3358
3359 llvm::Constant* zero = irb_.getJZero(op_jty);
3360
3361 llvm::Value* equal_zero = irb_.CreateICmpEQ(denominator, zero);
3362
3363 llvm::BasicBlock* block_exception = CreateBasicBlockWithDexPC(dex_pc, "div0");
3364
3365 llvm::BasicBlock* block_continue = CreateBasicBlockWithDexPC(dex_pc, "cont");
3366
3367 irb_.CreateCondBr(equal_zero, block_exception, block_continue);
3368
3369 irb_.SetInsertPoint(block_exception);
Logan Chien8dfcbea2012-02-17 18:50:32 +08003370 EmitUpdateLineNumFromDexPC(dex_pc);
Logan Chienc3f7d962011-12-27 18:13:18 +08003371 irb_.CreateCall(irb_.GetRuntime(ThrowDivZeroException));
3372 EmitBranchExceptionLandingPad(dex_pc);
3373
3374 irb_.SetInsertPoint(block_continue);
3375}
3376
3377
Logan Chien46fbb412012-02-15 22:29:08 +08003378llvm::Value* MethodCompiler::EmitLoadClassObjectAddr(llvm::Value* this_addr) {
3379 llvm::Constant* class_field_offset_value =
3380 irb_.getPtrEquivInt(Object::ClassOffset().Int32Value());
3381
3382 llvm::Value* class_field_addr =
3383 irb_.CreatePtrDisp(this_addr, class_field_offset_value,
3384 irb_.getJObjectTy()->getPointerTo());
3385
3386 return irb_.CreateLoad(class_field_addr);
3387}
3388
3389
3390llvm::Value*
3391MethodCompiler::EmitLoadVTableAddr(llvm::Value* class_object_addr) {
3392 llvm::Constant* vtable_offset_value =
3393 irb_.getPtrEquivInt(Class::VTableOffset().Int32Value());
3394
3395 llvm::Value* vtable_field_addr =
3396 irb_.CreatePtrDisp(class_object_addr, vtable_offset_value,
3397 irb_.getJObjectTy()->getPointerTo());
3398
3399 return irb_.CreateLoad(vtable_field_addr);
3400}
3401
3402
3403llvm::Value* MethodCompiler::
3404EmitLoadMethodObjectAddrFromVTable(llvm::Value* vtable_addr,
3405 uint16_t vtable_index) {
3406 llvm::Value* vtable_index_value =
3407 irb_.getPtrEquivInt(static_cast<uint64_t>(vtable_index));
3408
3409 llvm::Value* method_field_addr =
Ian Rogers04ec04e2012-02-28 16:15:33 -08003410 EmitArrayGEP(vtable_addr, vtable_index_value, irb_.getJObjectTy(), kObject);
Logan Chien46fbb412012-02-15 22:29:08 +08003411
3412 return irb_.CreateLoad(method_field_addr);
3413}
3414
3415
3416llvm::Value* MethodCompiler::EmitLoadCodeAddr(llvm::Value* method_object_addr,
3417 uint32_t method_idx,
3418 bool is_static) {
3419
3420 llvm::Value* code_field_offset_value =
3421 irb_.getPtrEquivInt(Method::GetCodeOffset().Int32Value());
3422
3423 llvm::FunctionType* method_type = GetFunctionType(method_idx, is_static);
3424
3425 llvm::Value* code_field_addr =
3426 irb_.CreatePtrDisp(method_object_addr, code_field_offset_value,
3427 method_type->getPointerTo()->getPointerTo());
3428
3429 return irb_.CreateLoad(code_field_addr);
3430}
3431
3432
Logan Chien61bb6142012-02-03 15:34:53 +08003433void MethodCompiler::EmitGuard_NullPointerException(uint32_t dex_pc,
3434 llvm::Value* object) {
3435 llvm::Value* equal_null = irb_.CreateICmpEQ(object, irb_.getJNull());
3436
3437 llvm::BasicBlock* block_exception =
3438 CreateBasicBlockWithDexPC(dex_pc, "nullp");
3439
3440 llvm::BasicBlock* block_continue =
3441 CreateBasicBlockWithDexPC(dex_pc, "cont");
3442
3443 irb_.CreateCondBr(equal_null, block_exception, block_continue);
3444
3445 irb_.SetInsertPoint(block_exception);
Logan Chien8dfcbea2012-02-17 18:50:32 +08003446 EmitUpdateLineNumFromDexPC(dex_pc);
Logan Chien61bb6142012-02-03 15:34:53 +08003447 irb_.CreateCall(irb_.GetRuntime(ThrowNullPointerException));
3448 EmitBranchExceptionLandingPad(dex_pc);
3449
3450 irb_.SetInsertPoint(block_continue);
3451}
3452
3453
Logan Chienbb4d12a2012-02-17 14:10:01 +08003454llvm::Value* MethodCompiler::EmitLoadDexCacheAddr(MemberOffset offset) {
3455 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
3456
3457 llvm::Value* dex_cache_offset_value =
3458 irb_.getPtrEquivInt(offset.Int32Value());
3459
3460 llvm::Value* dex_cache_field_addr =
3461 irb_.CreatePtrDisp(method_object_addr, dex_cache_offset_value,
3462 irb_.getJObjectTy()->getPointerTo());
3463
3464 return irb_.CreateLoad(dex_cache_field_addr);
3465}
3466
3467
Logan Chienbb4d12a2012-02-17 14:10:01 +08003468llvm::Value* MethodCompiler::
3469EmitLoadDexCacheStaticStorageFieldAddr(uint32_t type_idx) {
3470 llvm::Value* static_storage_dex_cache_addr =
3471 EmitLoadDexCacheAddr(Method::DexCacheInitializedStaticStorageOffset());
3472
3473 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
3474
3475 return EmitArrayGEP(static_storage_dex_cache_addr, type_idx_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08003476 irb_.getJObjectTy(), kObject);
Logan Chienbb4d12a2012-02-17 14:10:01 +08003477}
3478
3479
3480llvm::Value* MethodCompiler::
3481EmitLoadDexCacheResolvedTypeFieldAddr(uint32_t type_idx) {
3482 llvm::Value* resolved_type_dex_cache_addr =
3483 EmitLoadDexCacheAddr(Method::DexCacheResolvedTypesOffset());
3484
3485 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
3486
3487 return EmitArrayGEP(resolved_type_dex_cache_addr, type_idx_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08003488 irb_.getJObjectTy(), kObject);
Logan Chienbb4d12a2012-02-17 14:10:01 +08003489}
3490
3491
3492llvm::Value* MethodCompiler::
Logan Chien61c65dc2012-02-29 03:22:30 +08003493EmitLoadDexCacheResolvedMethodFieldAddr(uint32_t method_idx) {
3494 llvm::Value* resolved_method_dex_cache_addr =
3495 EmitLoadDexCacheAddr(Method::DexCacheResolvedMethodsOffset());
3496
3497 llvm::Value* method_idx_value = irb_.getPtrEquivInt(method_idx);
3498
3499 return EmitArrayGEP(resolved_method_dex_cache_addr, method_idx_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08003500 irb_.getJObjectTy(), kObject);
Logan Chien61c65dc2012-02-29 03:22:30 +08003501}
3502
3503
3504llvm::Value* MethodCompiler::
Logan Chienbb4d12a2012-02-17 14:10:01 +08003505EmitLoadDexCacheStringFieldAddr(uint32_t string_idx) {
3506 llvm::Value* string_dex_cache_addr =
3507 EmitLoadDexCacheAddr(Method::DexCacheStringsOffset());
3508
3509 llvm::Value* string_idx_value = irb_.getPtrEquivInt(string_idx);
3510
3511 return EmitArrayGEP(string_dex_cache_addr, string_idx_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08003512 irb_.getJObjectTy(), kObject);
Logan Chienbb4d12a2012-02-17 14:10:01 +08003513}
3514
3515
Logan Chien83426162011-12-09 09:29:50 +08003516CompiledMethod *MethodCompiler::Compile() {
Logan Chien0b827102011-12-20 19:46:14 +08003517 // Code generation
3518 CreateFunction();
3519
3520 EmitPrologue();
3521 EmitInstructions();
Logan Chienc670a8d2011-12-20 21:25:56 +08003522 EmitPrologueLastBranch();
Logan Chien0b827102011-12-20 19:46:14 +08003523
Logan Chiend6c239a2011-12-23 15:11:45 +08003524 // Verify the generated bitcode
3525 llvm::verifyFunction(*func_, llvm::PrintMessageAction);
3526
Logan Chien0b827102011-12-20 19:46:14 +08003527 // Delete the inferred register category map (won't be used anymore)
3528 method_->ResetInferredRegCategoryMap();
3529
Logan Chien8b977d32012-02-21 19:14:55 +08003530 // Add the memory usage approximation of the compilation unit
3531 cunit_->AddMemUsageApproximation(code_item_->insns_size_in_code_units_ * 900);
3532 // NOTE: From statistic, the bitcode size is 4.5 times bigger than the
3533 // Dex file. Besides, we have to convert the code unit into bytes.
3534 // Thus, we got our magic number 9.
3535
3536 return new CompiledMethod(cunit_->GetInstructionSet(), func_);
Logan Chien0b827102011-12-20 19:46:14 +08003537}
3538
3539
3540llvm::Value* MethodCompiler::EmitLoadMethodObjectAddr() {
3541 return func_->arg_begin();
Shih-wei Liaod1fec812012-02-13 09:51:10 -08003542}
Logan Chien83426162011-12-09 09:29:50 +08003543
3544
Logan Chien5bcc04e2012-01-30 14:15:12 +08003545void MethodCompiler::EmitBranchExceptionLandingPad(uint32_t dex_pc) {
3546 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
3547 irb_.CreateBr(lpad);
3548 } else {
3549 irb_.CreateBr(GetUnwindBasicBlock());
3550 }
3551}
3552
3553
3554void MethodCompiler::EmitGuard_ExceptionLandingPad(uint32_t dex_pc) {
3555 llvm::Value* exception_pending =
3556 irb_.CreateCall(irb_.GetRuntime(IsExceptionPending));
3557
3558 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
3559
3560 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
3561 irb_.CreateCondBr(exception_pending, lpad, block_cont);
3562 } else {
3563 irb_.CreateCondBr(exception_pending, GetUnwindBasicBlock(), block_cont);
3564 }
3565
3566 irb_.SetInsertPoint(block_cont);
3567}
3568
3569
Logan Chien924072f2012-01-30 15:07:24 +08003570void MethodCompiler::EmitGuard_GarbageCollectionSuspend(uint32_t dex_pc) {
3571 llvm::Value* runtime_func = irb_.GetRuntime(TestSuspend);
Logan Chien8dfcbea2012-02-17 18:50:32 +08003572 EmitUpdateLineNumFromDexPC(dex_pc);
Logan Chien924072f2012-01-30 15:07:24 +08003573 irb_.CreateCall(runtime_func);
3574
3575 EmitGuard_ExceptionLandingPad(dex_pc);
3576}
3577
3578
Logan Chiend6c239a2011-12-23 15:11:45 +08003579llvm::BasicBlock* MethodCompiler::
3580CreateBasicBlockWithDexPC(uint32_t dex_pc, char const* postfix) {
3581 std::string name;
3582
3583 if (postfix) {
3584 StringAppendF(&name, "B%u.%s", dex_pc, postfix);
3585 } else {
3586 StringAppendF(&name, "B%u", dex_pc);
3587 }
3588
3589 return llvm::BasicBlock::Create(*context_, name, func_);
3590}
3591
3592
3593llvm::BasicBlock* MethodCompiler::GetBasicBlock(uint32_t dex_pc) {
3594 DCHECK(dex_pc < code_item_->insns_size_in_code_units_);
3595
3596 llvm::BasicBlock* basic_block = basic_blocks_[dex_pc];
3597
3598 if (!basic_block) {
3599 basic_block = CreateBasicBlockWithDexPC(dex_pc);
3600 basic_blocks_[dex_pc] = basic_block;
3601 }
3602
3603 return basic_block;
3604}
3605
3606
3607llvm::BasicBlock*
3608MethodCompiler::GetNextBasicBlock(uint32_t dex_pc) {
3609 Instruction const* insn = Instruction::At(code_item_->insns_ + dex_pc);
3610 return GetBasicBlock(dex_pc + insn->SizeInCodeUnits());
3611}
3612
3613
Logan Chien5bcc04e2012-01-30 14:15:12 +08003614int32_t MethodCompiler::GetTryItemOffset(uint32_t dex_pc) {
3615 // TODO: Since we are emitting the dex instructions in ascending order
3616 // w.r.t. address, we can cache the lastest try item offset so that we
3617 // don't have to do binary search for every query.
3618
3619 int32_t min = 0;
3620 int32_t max = code_item_->tries_size_ - 1;
3621
3622 while (min <= max) {
3623 int32_t mid = min + (max - min) / 2;
3624
3625 DexFile::TryItem const* ti = DexFile::GetTryItems(*code_item_, mid);
3626 uint32_t start = ti->start_addr_;
3627 uint32_t end = start + ti->insn_count_;
3628
3629 if (dex_pc < start) {
3630 max = mid - 1;
3631 } else if (dex_pc >= end) {
3632 min = mid + 1;
3633 } else {
3634 return mid; // found
3635 }
3636 }
3637
3638 return -1; // not found
3639}
3640
3641
3642llvm::BasicBlock* MethodCompiler::GetLandingPadBasicBlock(uint32_t dex_pc) {
3643 // Find the try item for this address in this method
3644 int32_t ti_offset = GetTryItemOffset(dex_pc);
3645
3646 if (ti_offset == -1) {
3647 return NULL; // No landing pad is available for this address.
3648 }
3649
3650 // Check for the existing landing pad basic block
3651 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
3652 llvm::BasicBlock* block_lpad = basic_block_landing_pads_[ti_offset];
3653
3654 if (block_lpad) {
3655 // We have generated landing pad for this try item already. Return the
3656 // same basic block.
3657 return block_lpad;
3658 }
3659
3660 // Get try item from code item
3661 DexFile::TryItem const* ti = DexFile::GetTryItems(*code_item_, ti_offset);
3662
3663 // Create landing pad basic block
3664 block_lpad = llvm::BasicBlock::Create(*context_,
3665 StringPrintf("lpad%d", ti_offset),
3666 func_);
3667
3668 // Change IRBuilder insert point
3669 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3670 irb_.SetInsertPoint(block_lpad);
3671
3672 // Find catch block with matching type
3673 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
3674
3675 // TODO: Maybe passing try item offset will be a better idea? For now,
3676 // we are passing dex_pc, so that we can use existing runtime support
3677 // function directly. However, in the runtime supporting function we
3678 // have to search for try item with binary search which can be
3679 // eliminated.
3680 llvm::Value* dex_pc_value = irb_.getInt32(ti->start_addr_);
3681
3682 llvm::Value* catch_handler_index_value =
3683 irb_.CreateCall2(irb_.GetRuntime(FindCatchBlock),
3684 method_object_addr, dex_pc_value);
3685
3686 // Switch instruction (Go to unwind basic block by default)
3687 llvm::SwitchInst* sw =
3688 irb_.CreateSwitch(catch_handler_index_value, GetUnwindBasicBlock());
3689
3690 // Cases with matched catch block
3691 CatchHandlerIterator iter(*code_item_, ti->start_addr_);
3692
3693 for (uint32_t c = 0; iter.HasNext(); iter.Next(), ++c) {
3694 sw->addCase(irb_.getInt32(c), GetBasicBlock(iter.GetHandlerAddress()));
3695 }
3696
3697 // Restore the orignal insert point for IRBuilder
3698 irb_.restoreIP(irb_ip_original);
3699
3700 // Cache this landing pad
3701 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
3702 basic_block_landing_pads_[ti_offset] = block_lpad;
3703
3704 return block_lpad;
3705}
3706
3707
3708llvm::BasicBlock* MethodCompiler::GetUnwindBasicBlock() {
3709 // Check the existing unwinding baisc block block
3710 if (basic_block_unwind_ != NULL) {
3711 return basic_block_unwind_;
3712 }
3713
3714 // Create new basic block for unwinding
3715 basic_block_unwind_ =
3716 llvm::BasicBlock::Create(*context_, "exception_unwind", func_);
3717
3718 // Change IRBuilder insert point
3719 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3720 irb_.SetInsertPoint(basic_block_unwind_);
3721
Logan Chien8dfcbea2012-02-17 18:50:32 +08003722 // Pop the shadow frame
3723 EmitPopShadowFrame();
3724
Logan Chien5bcc04e2012-01-30 14:15:12 +08003725 // Emit the code to return default value (zero) for the given return type.
3726 char ret_shorty = method_helper_.GetShorty()[0];
3727 if (ret_shorty == 'V') {
3728 irb_.CreateRetVoid();
3729 } else {
3730 irb_.CreateRet(irb_.getJZero(ret_shorty));
3731 }
3732
3733 // Restore the orignal insert point for IRBuilder
3734 irb_.restoreIP(irb_ip_original);
3735
3736 return basic_block_unwind_;
3737}
3738
3739
Logan Chienc670a8d2011-12-20 21:25:56 +08003740llvm::Value* MethodCompiler::AllocDalvikLocalVarReg(RegCategory cat,
3741 uint32_t reg_idx) {
3742
3743 // Save current IR builder insert point
3744 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3745
3746 // Alloca
3747 llvm::Value* reg_addr = NULL;
3748
3749 switch (cat) {
3750 case kRegCat1nr:
3751 irb_.SetInsertPoint(basic_block_reg_alloca_);
3752 reg_addr = irb_.CreateAlloca(irb_.getJIntTy(), 0,
3753 StringPrintf("r%u", reg_idx));
3754
3755 irb_.SetInsertPoint(basic_block_reg_zero_init_);
3756 irb_.CreateStore(irb_.getJInt(0), reg_addr);
3757 break;
3758
3759 case kRegCat2:
3760 irb_.SetInsertPoint(basic_block_reg_alloca_);
3761 reg_addr = irb_.CreateAlloca(irb_.getJLongTy(), 0,
3762 StringPrintf("w%u", reg_idx));
3763
3764 irb_.SetInsertPoint(basic_block_reg_zero_init_);
3765 irb_.CreateStore(irb_.getJLong(0), reg_addr);
3766 break;
3767
3768 case kRegObject:
Logan Chien8dfcbea2012-02-17 18:50:32 +08003769 {
3770 irb_.SetInsertPoint(basic_block_shadow_frame_alloca_);
Logan Chienc670a8d2011-12-20 21:25:56 +08003771
Logan Chien8dfcbea2012-02-17 18:50:32 +08003772 llvm::Value* gep_index[] = {
3773 irb_.getInt32(0), // No pointer displacement
3774 irb_.getInt32(1), // SIRT
3775 irb_.getInt32(reg_idx) // Pointer field
3776 };
3777
3778 reg_addr = irb_.CreateGEP(shadow_frame_, gep_index,
3779 StringPrintf("p%u", reg_idx));
3780
3781 irb_.SetInsertPoint(basic_block_reg_zero_init_);
3782 irb_.CreateStore(irb_.getJNull(), reg_addr);
3783 }
Logan Chienc670a8d2011-12-20 21:25:56 +08003784 break;
3785
3786 default:
3787 LOG(FATAL) << "Unknown register category for allocation: " << cat;
3788 }
3789
3790 // Restore IRBuilder insert point
3791 irb_.restoreIP(irb_ip_original);
3792
3793 DCHECK_NE(reg_addr, static_cast<llvm::Value*>(NULL));
3794 return reg_addr;
3795}
3796
3797
3798llvm::Value* MethodCompiler::AllocDalvikRetValReg(RegCategory cat) {
3799 // Save current IR builder insert point
3800 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3801
3802 // Alloca
3803 llvm::Value* reg_addr = NULL;
3804
3805 switch (cat) {
3806 case kRegCat1nr:
3807 irb_.SetInsertPoint(basic_block_reg_alloca_);
3808 reg_addr = irb_.CreateAlloca(irb_.getJIntTy(), 0, "r_res");
3809 break;
3810
3811 case kRegCat2:
3812 irb_.SetInsertPoint(basic_block_reg_alloca_);
3813 reg_addr = irb_.CreateAlloca(irb_.getJLongTy(), 0, "w_res");
3814 break;
3815
3816 case kRegObject:
3817 irb_.SetInsertPoint(basic_block_reg_alloca_);
3818 reg_addr = irb_.CreateAlloca(irb_.getJObjectTy(), 0, "p_res");
3819 break;
3820
3821 default:
3822 LOG(FATAL) << "Unknown register category for allocation: " << cat;
3823 }
3824
3825 // Restore IRBuilder insert point
3826 irb_.restoreIP(irb_ip_original);
3827
3828 DCHECK_NE(reg_addr, static_cast<llvm::Value*>(NULL));
3829 return reg_addr;
3830}
3831
3832
Logan Chien8dfcbea2012-02-17 18:50:32 +08003833void MethodCompiler::EmitPopShadowFrame() {
3834 irb_.CreateCall(irb_.GetRuntime(PopShadowFrame));
3835}
3836
3837
3838void MethodCompiler::EmitUpdateLineNum(int32_t line_num) {
3839 llvm::Constant* zero = irb_.getInt32(0);
3840
3841 llvm::Value* gep_index[] = {
3842 zero, // No displacement for shadow frame pointer
3843 zero, // Get the %ArtFrame data structure
3844 irb_.getInt32(2),
3845 };
3846
3847 llvm::Value* line_num_field_addr = irb_.CreateGEP(shadow_frame_, gep_index);
3848 llvm::ConstantInt* line_num_value = irb_.getInt32(line_num);
3849 irb_.CreateStore(line_num_value, line_num_field_addr);
3850}
3851
3852
3853void MethodCompiler::EmitUpdateLineNumFromDexPC(uint32_t dex_pc) {
3854 EmitUpdateLineNum(dex_file_->GetLineNumFromPC(method_, dex_pc));
3855}
3856
3857
Logan Chien83426162011-12-09 09:29:50 +08003858} // namespace compiler_llvm
3859} // namespace art