blob: 3612d2413ade6e5e4e0664ecc0d70ff7dfd17696 [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"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080020#include "compiler.h"
Logan Chiena78e3c82011-12-27 17:59:35 +080021#include "inferred_reg_category_map.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080022#include "ir_builder.h"
23#include "logging.h"
24#include "object.h"
25#include "object_utils.h"
Logan Chien42e0e152012-01-13 15:42:36 +080026#include "runtime_support_func.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080027#include "stl_util.h"
Logan Chien0b827102011-12-20 19:46:14 +080028#include "stringprintf.h"
29#include "utils_llvm.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080030
31#include <iomanip>
32
33#include <llvm/Analysis/Verifier.h>
Logan Chienc670a8d2011-12-20 21:25:56 +080034#include <llvm/BasicBlock.h>
Shih-wei Liaod1fec812012-02-13 09:51:10 -080035#include <llvm/Function.h>
36
Logan Chien83426162011-12-09 09:29:50 +080037namespace art {
38namespace compiler_llvm {
Shih-wei Liaod1fec812012-02-13 09:51:10 -080039
Logan Chien42e0e152012-01-13 15:42:36 +080040using namespace runtime_support;
41
Shih-wei Liaod1fec812012-02-13 09:51:10 -080042
Logan Chien83426162011-12-09 09:29:50 +080043MethodCompiler::MethodCompiler(InstructionSet insn_set,
44 Compiler* compiler,
45 ClassLinker* class_linker,
46 ClassLoader const* class_loader,
47 DexFile const* dex_file,
48 DexCache* dex_cache,
49 DexFile::CodeItem const* code_item,
Shih-wei Liaod1fec812012-02-13 09:51:10 -080050 uint32_t method_idx,
51 uint32_t access_flags)
52: insn_set_(insn_set),
53 compiler_(compiler), compiler_llvm_(compiler->GetCompilerLLVM()),
54 class_linker_(class_linker), class_loader_(class_loader),
55 dex_file_(dex_file), dex_cache_(dex_cache), code_item_(code_item),
56 method_(dex_cache->GetResolvedMethod(method_idx)),
57 method_helper_(method_), method_idx_(method_idx),
58 access_flags_(access_flags), module_(compiler_llvm_->GetModule()),
59 context_(compiler_llvm_->GetLLVMContext()),
Logan Chienc670a8d2011-12-20 21:25:56 +080060 irb_(*compiler_llvm_->GetIRBuilder()), func_(NULL), retval_reg_(NULL),
Logan Chiend6ececa2011-12-27 16:20:15 +080061 basic_block_reg_alloca_(NULL),
62 basic_block_reg_zero_init_(NULL), basic_block_reg_arg_init_(NULL),
Logan Chien5bcc04e2012-01-30 14:15:12 +080063 basic_blocks_(code_item->insns_size_in_code_units_),
64 basic_block_landing_pads_(code_item->tries_size_, NULL),
65 basic_block_unwind_(NULL), basic_block_unreachable_(NULL) {
Shih-wei Liaod1fec812012-02-13 09:51:10 -080066}
67
68
69MethodCompiler::~MethodCompiler() {
Logan Chienc670a8d2011-12-20 21:25:56 +080070 STLDeleteElements(&regs_);
Shih-wei Liaod1fec812012-02-13 09:51:10 -080071}
72
73
Logan Chien0b827102011-12-20 19:46:14 +080074void MethodCompiler::CreateFunction() {
75 // LLVM function name
76 std::string func_name(LLVMLongName(method_));
77
78 // Get function type
79 llvm::FunctionType* func_type =
80 GetFunctionType(method_idx_, method_->IsStatic());
81
82 // Create function
83 func_ = llvm::Function::Create(func_type, llvm::Function::ExternalLinkage,
84 func_name, module_);
85
86 // Set argument name
87 llvm::Function::arg_iterator arg_iter(func_->arg_begin());
88 llvm::Function::arg_iterator arg_end(func_->arg_end());
89
90 DCHECK_NE(arg_iter, arg_end);
91 arg_iter->setName("method");
92 ++arg_iter;
93
94 if (!method_->IsStatic()) {
95 DCHECK_NE(arg_iter, arg_end);
96 arg_iter->setName("this");
97 ++arg_iter;
98 }
99
100 for (unsigned i = 0; arg_iter != arg_end; ++i, ++arg_iter) {
101 arg_iter->setName(StringPrintf("a%u", i));
102 }
103}
104
105
106llvm::FunctionType* MethodCompiler::GetFunctionType(uint32_t method_idx,
107 bool is_static) {
108 // Get method signature
109 DexFile::MethodId const& method_id = dex_file_->GetMethodId(method_idx);
110
111 int32_t shorty_size;
112 char const* shorty = dex_file_->GetMethodShorty(method_id, &shorty_size);
113 CHECK_GE(shorty_size, 1);
114
115 // Get return type
116 llvm::Type* ret_type = irb_.getJType(shorty[0], kAccurate);
117
118 // Get argument type
119 std::vector<llvm::Type*> args_type;
120
121 args_type.push_back(irb_.getJObjectTy()); // method object pointer
122
123 if (!is_static) {
124 args_type.push_back(irb_.getJType('L', kAccurate)); // "this" object pointer
125 }
126
127 for (int32_t i = 1; i < shorty_size; ++i) {
128 args_type.push_back(irb_.getJType(shorty[i], kAccurate));
129 }
130
131 return llvm::FunctionType::get(ret_type, args_type, false);
132}
133
134
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800135void MethodCompiler::EmitPrologue() {
Logan Chienc670a8d2011-12-20 21:25:56 +0800136 // Create basic blocks for prologue
137 basic_block_reg_alloca_ =
138 llvm::BasicBlock::Create(*context_, "prologue.alloca", func_);
139
140 basic_block_reg_zero_init_ =
141 llvm::BasicBlock::Create(*context_, "prologue.zeroinit", func_);
142
Logan Chiend6ececa2011-12-27 16:20:15 +0800143 basic_block_reg_arg_init_ =
144 llvm::BasicBlock::Create(*context_, "prologue.arginit", func_);
145
Logan Chienc670a8d2011-12-20 21:25:56 +0800146 // Create register array
147 for (uint16_t r = 0; r < code_item_->registers_size_; ++r) {
148 regs_.push_back(DalvikReg::CreateLocalVarReg(*this, r));
149 }
150
151 retval_reg_.reset(DalvikReg::CreateRetValReg(*this));
Logan Chiend6ececa2011-12-27 16:20:15 +0800152
153 // Store argument to dalvik register
154 irb_.SetInsertPoint(basic_block_reg_arg_init_);
155 EmitPrologueAssignArgRegister();
156
157 // Branch to start address
158 irb_.CreateBr(GetBasicBlock(0));
Logan Chienc670a8d2011-12-20 21:25:56 +0800159}
160
161
162void MethodCompiler::EmitPrologueLastBranch() {
163 irb_.SetInsertPoint(basic_block_reg_alloca_);
164 irb_.CreateBr(basic_block_reg_zero_init_);
165
166 irb_.SetInsertPoint(basic_block_reg_zero_init_);
Logan Chiend6ececa2011-12-27 16:20:15 +0800167 irb_.CreateBr(basic_block_reg_arg_init_);
168}
169
170
171void MethodCompiler::EmitPrologueAssignArgRegister() {
172 uint16_t arg_reg = code_item_->registers_size_ - code_item_->ins_size_;
173
174 llvm::Function::arg_iterator arg_iter(func_->arg_begin());
175 llvm::Function::arg_iterator arg_end(func_->arg_end());
176
177 char const* shorty = method_helper_.GetShorty();
178 int32_t shorty_size = method_helper_.GetShortyLength();
179 CHECK_LE(1, shorty_size);
180
181 ++arg_iter; // skip method object
182
183 if (!method_->IsStatic()) {
184 EmitStoreDalvikReg(arg_reg, kObject, kAccurate, arg_iter);
185 ++arg_iter;
186 ++arg_reg;
187 }
188
189 for (int32_t i = 1; i < shorty_size; ++i, ++arg_iter) {
190 EmitStoreDalvikReg(arg_reg, shorty[i], kAccurate, arg_iter);
191
192 ++arg_reg;
193 if (shorty[i] == 'J' || shorty[i] == 'D') {
194 // Wide types, such as long and double, are using a pair of registers
195 // to store the value, so we have to increase arg_reg again.
196 ++arg_reg;
197 }
198 }
199
200 DCHECK_EQ(arg_end, arg_iter);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800201}
202
203
Logan Chien83426162011-12-09 09:29:50 +0800204void MethodCompiler::EmitInstructions() {
Logan Chiend6c239a2011-12-23 15:11:45 +0800205 uint32_t dex_pc = 0;
206 while (dex_pc < code_item_->insns_size_in_code_units_) {
207 Instruction const* insn = Instruction::At(code_item_->insns_ + dex_pc);
208 EmitInstruction(dex_pc, insn);
209 dex_pc += insn->SizeInCodeUnits();
210 }
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800211}
212
213
Logan Chien83426162011-12-09 09:29:50 +0800214void MethodCompiler::EmitInstruction(uint32_t dex_pc,
215 Instruction const* insn) {
Logan Chiend6c239a2011-12-23 15:11:45 +0800216
217 // Set the IRBuilder insertion point
218 irb_.SetInsertPoint(GetBasicBlock(dex_pc));
219
Logan Chien70f94b42011-12-27 17:49:11 +0800220#define ARGS dex_pc, insn
221
222 // Dispatch the instruction
223 switch (insn->Opcode()) {
224 case Instruction::NOP:
225 EmitInsn_Nop(ARGS);
226 break;
227
228 case Instruction::MOVE:
229 case Instruction::MOVE_FROM16:
230 case Instruction::MOVE_16:
231 EmitInsn_Move(ARGS, kInt);
232 break;
233
234 case Instruction::MOVE_WIDE:
235 case Instruction::MOVE_WIDE_FROM16:
236 case Instruction::MOVE_WIDE_16:
237 EmitInsn_Move(ARGS, kLong);
238 break;
239
240 case Instruction::MOVE_OBJECT:
241 case Instruction::MOVE_OBJECT_FROM16:
242 case Instruction::MOVE_OBJECT_16:
243 EmitInsn_Move(ARGS, kObject);
244 break;
245
246 case Instruction::MOVE_RESULT:
247 EmitInsn_MoveResult(ARGS, kInt);
248 break;
249
250 case Instruction::MOVE_RESULT_WIDE:
251 EmitInsn_MoveResult(ARGS, kLong);
252 break;
253
254 case Instruction::MOVE_RESULT_OBJECT:
255 EmitInsn_MoveResult(ARGS, kObject);
256 break;
257
258 case Instruction::MOVE_EXCEPTION:
259 EmitInsn_MoveException(ARGS);
260 break;
261
262 case Instruction::RETURN_VOID:
263 EmitInsn_ReturnVoid(ARGS);
264 break;
265
266 case Instruction::RETURN:
267 case Instruction::RETURN_WIDE:
268 case Instruction::RETURN_OBJECT:
269 EmitInsn_Return(ARGS);
270 break;
271
272 case Instruction::CONST_4:
273 case Instruction::CONST_16:
274 case Instruction::CONST:
275 case Instruction::CONST_HIGH16:
276 EmitInsn_LoadConstant(ARGS, kInt);
277 break;
278
279 case Instruction::CONST_WIDE_16:
280 case Instruction::CONST_WIDE_32:
281 case Instruction::CONST_WIDE:
282 case Instruction::CONST_WIDE_HIGH16:
283 EmitInsn_LoadConstant(ARGS, kLong);
284 break;
285
286 case Instruction::CONST_STRING:
287 case Instruction::CONST_STRING_JUMBO:
288 EmitInsn_LoadConstantString(ARGS);
289 break;
290
291 case Instruction::CONST_CLASS:
292 EmitInsn_LoadConstantClass(ARGS);
293 break;
294
295 case Instruction::MONITOR_ENTER:
296 EmitInsn_MonitorEnter(ARGS);
297 break;
298
299 case Instruction::MONITOR_EXIT:
300 EmitInsn_MonitorExit(ARGS);
301 break;
302
303 case Instruction::CHECK_CAST:
304 EmitInsn_CheckCast(ARGS);
305 break;
306
307 case Instruction::INSTANCE_OF:
308 EmitInsn_InstanceOf(ARGS);
309 break;
310
311 case Instruction::ARRAY_LENGTH:
312 EmitInsn_ArrayLength(ARGS);
313 break;
314
315 case Instruction::NEW_INSTANCE:
316 EmitInsn_NewInstance(ARGS);
317 break;
318
319 case Instruction::NEW_ARRAY:
320 EmitInsn_NewArray(ARGS);
321 break;
322
323 case Instruction::FILLED_NEW_ARRAY:
324 EmitInsn_FilledNewArray(ARGS, false);
325 break;
326
327 case Instruction::FILLED_NEW_ARRAY_RANGE:
328 EmitInsn_FilledNewArray(ARGS, true);
329 break;
330
331 case Instruction::FILL_ARRAY_DATA:
332 EmitInsn_FillArrayData(ARGS);
333 break;
334
335 case Instruction::THROW:
336 EmitInsn_ThrowException(ARGS);
337 break;
338
339 case Instruction::GOTO:
340 case Instruction::GOTO_16:
341 case Instruction::GOTO_32:
342 EmitInsn_UnconditionalBranch(ARGS);
343 break;
344
345 case Instruction::PACKED_SWITCH:
346 EmitInsn_PackedSwitch(ARGS);
347 break;
348
349 case Instruction::SPARSE_SWITCH:
350 EmitInsn_SparseSwitch(ARGS);
351 break;
352
353 case Instruction::CMPL_FLOAT:
354 EmitInsn_FPCompare(ARGS, kFloat, false);
355 break;
356
357 case Instruction::CMPG_FLOAT:
358 EmitInsn_FPCompare(ARGS, kFloat, true);
359 break;
360
361 case Instruction::CMPL_DOUBLE:
362 EmitInsn_FPCompare(ARGS, kDouble, false);
363 break;
364
365 case Instruction::CMPG_DOUBLE:
366 EmitInsn_FPCompare(ARGS, kDouble, true);
367 break;
368
369 case Instruction::CMP_LONG:
370 EmitInsn_LongCompare(ARGS);
371 break;
372
373 case Instruction::IF_EQ:
374 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_EQ);
375 break;
376
377 case Instruction::IF_NE:
378 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_NE);
379 break;
380
381 case Instruction::IF_LT:
382 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_LT);
383 break;
384
385 case Instruction::IF_GE:
386 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_GE);
387 break;
388
389 case Instruction::IF_GT:
390 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_GT);
391 break;
392
393 case Instruction::IF_LE:
394 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_LE);
395 break;
396
397 case Instruction::IF_EQZ:
398 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_EQ);
399 break;
400
401 case Instruction::IF_NEZ:
402 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_NE);
403 break;
404
405 case Instruction::IF_LTZ:
406 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_LT);
407 break;
408
409 case Instruction::IF_GEZ:
410 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_GE);
411 break;
412
413 case Instruction::IF_GTZ:
414 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_GT);
415 break;
416
417 case Instruction::IF_LEZ:
418 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_LE);
419 break;
420
421 case Instruction::AGET:
422 EmitInsn_AGet(ARGS, kInt);
423 break;
424
425 case Instruction::AGET_WIDE:
426 EmitInsn_AGet(ARGS, kLong);
427 break;
428
429 case Instruction::AGET_OBJECT:
430 EmitInsn_AGet(ARGS, kObject);
431 break;
432
433 case Instruction::AGET_BOOLEAN:
434 EmitInsn_AGet(ARGS, kBoolean);
435 break;
436
437 case Instruction::AGET_BYTE:
438 EmitInsn_AGet(ARGS, kByte);
439 break;
440
441 case Instruction::AGET_CHAR:
442 EmitInsn_AGet(ARGS, kChar);
443 break;
444
445 case Instruction::AGET_SHORT:
446 EmitInsn_AGet(ARGS, kShort);
447 break;
448
449 case Instruction::APUT:
450 EmitInsn_APut(ARGS, kInt);
451 break;
452
453 case Instruction::APUT_WIDE:
454 EmitInsn_APut(ARGS, kLong);
455 break;
456
457 case Instruction::APUT_OBJECT:
458 EmitInsn_APut(ARGS, kObject);
459 break;
460
461 case Instruction::APUT_BOOLEAN:
462 EmitInsn_APut(ARGS, kBoolean);
463 break;
464
465 case Instruction::APUT_BYTE:
466 EmitInsn_APut(ARGS, kByte);
467 break;
468
469 case Instruction::APUT_CHAR:
470 EmitInsn_APut(ARGS, kChar);
471 break;
472
473 case Instruction::APUT_SHORT:
474 EmitInsn_APut(ARGS, kShort);
475 break;
476
477 case Instruction::IGET:
478 EmitInsn_IGet(ARGS, kInt);
479 break;
480
481 case Instruction::IGET_WIDE:
482 EmitInsn_IGet(ARGS, kLong);
483 break;
484
485 case Instruction::IGET_OBJECT:
486 EmitInsn_IGet(ARGS, kObject);
487 break;
488
489 case Instruction::IGET_BOOLEAN:
490 EmitInsn_IGet(ARGS, kBoolean);
491 break;
492
493 case Instruction::IGET_BYTE:
494 EmitInsn_IGet(ARGS, kByte);
495 break;
496
497 case Instruction::IGET_CHAR:
498 EmitInsn_IGet(ARGS, kChar);
499 break;
500
501 case Instruction::IGET_SHORT:
502 EmitInsn_IGet(ARGS, kShort);
503 break;
504
505 case Instruction::IPUT:
506 EmitInsn_IPut(ARGS, kInt);
507 break;
508
509 case Instruction::IPUT_WIDE:
510 EmitInsn_IPut(ARGS, kLong);
511 break;
512
513 case Instruction::IPUT_OBJECT:
514 EmitInsn_IPut(ARGS, kObject);
515 break;
516
517 case Instruction::IPUT_BOOLEAN:
518 EmitInsn_IPut(ARGS, kBoolean);
519 break;
520
521 case Instruction::IPUT_BYTE:
522 EmitInsn_IPut(ARGS, kByte);
523 break;
524
525 case Instruction::IPUT_CHAR:
526 EmitInsn_IPut(ARGS, kChar);
527 break;
528
529 case Instruction::IPUT_SHORT:
530 EmitInsn_IPut(ARGS, kShort);
531 break;
532
533 case Instruction::SGET:
534 EmitInsn_SGet(ARGS, kInt);
535 break;
536
537 case Instruction::SGET_WIDE:
538 EmitInsn_SGet(ARGS, kLong);
539 break;
540
541 case Instruction::SGET_OBJECT:
542 EmitInsn_SGet(ARGS, kObject);
543 break;
544
545 case Instruction::SGET_BOOLEAN:
546 EmitInsn_SGet(ARGS, kBoolean);
547 break;
548
549 case Instruction::SGET_BYTE:
550 EmitInsn_SGet(ARGS, kByte);
551 break;
552
553 case Instruction::SGET_CHAR:
554 EmitInsn_SGet(ARGS, kChar);
555 break;
556
557 case Instruction::SGET_SHORT:
558 EmitInsn_SGet(ARGS, kShort);
559 break;
560
561 case Instruction::SPUT:
562 EmitInsn_SPut(ARGS, kInt);
563 break;
564
565 case Instruction::SPUT_WIDE:
566 EmitInsn_SPut(ARGS, kLong);
567 break;
568
569 case Instruction::SPUT_OBJECT:
570 EmitInsn_SPut(ARGS, kObject);
571 break;
572
573 case Instruction::SPUT_BOOLEAN:
574 EmitInsn_SPut(ARGS, kBoolean);
575 break;
576
577 case Instruction::SPUT_BYTE:
578 EmitInsn_SPut(ARGS, kByte);
579 break;
580
581 case Instruction::SPUT_CHAR:
582 EmitInsn_SPut(ARGS, kChar);
583 break;
584
585 case Instruction::SPUT_SHORT:
586 EmitInsn_SPut(ARGS, kShort);
587 break;
588
589
590 case Instruction::INVOKE_VIRTUAL:
591 EmitInsn_InvokeVirtual(ARGS, false);
592 break;
593
594 case Instruction::INVOKE_SUPER:
595 EmitInsn_InvokeSuper(ARGS, false);
596 break;
597
598 case Instruction::INVOKE_DIRECT:
599 EmitInsn_InvokeDirect(ARGS, false);
600 break;
601
602 case Instruction::INVOKE_STATIC:
603 EmitInsn_InvokeStatic(ARGS, false);
604 break;
605
606 case Instruction::INVOKE_INTERFACE:
607 EmitInsn_InvokeInterface(ARGS, false);
608 break;
609
610 case Instruction::INVOKE_VIRTUAL_RANGE:
611 EmitInsn_InvokeVirtual(ARGS, true);
612 break;
613
614 case Instruction::INVOKE_SUPER_RANGE:
615 EmitInsn_InvokeSuper(ARGS, true);
616 break;
617
618 case Instruction::INVOKE_DIRECT_RANGE:
619 EmitInsn_InvokeDirect(ARGS, true);
620 break;
621
622 case Instruction::INVOKE_STATIC_RANGE:
623 EmitInsn_InvokeStatic(ARGS, true);
624 break;
625
626 case Instruction::INVOKE_INTERFACE_RANGE:
627 EmitInsn_InvokeInterface(ARGS, true);
628 break;
629
630 case Instruction::NEG_INT:
631 EmitInsn_Neg(ARGS, kInt);
632 break;
633
634 case Instruction::NOT_INT:
635 EmitInsn_Not(ARGS, kInt);
636 break;
637
638 case Instruction::NEG_LONG:
639 EmitInsn_Neg(ARGS, kLong);
640 break;
641
642 case Instruction::NOT_LONG:
643 EmitInsn_Not(ARGS, kLong);
644 break;
645
646 case Instruction::NEG_FLOAT:
647 EmitInsn_FNeg(ARGS, kFloat);
648 break;
649
650 case Instruction::NEG_DOUBLE:
651 EmitInsn_FNeg(ARGS, kDouble);
652 break;
653
654 case Instruction::INT_TO_LONG:
655 EmitInsn_SExt(ARGS);
656 break;
657
658 case Instruction::INT_TO_FLOAT:
659 EmitInsn_IntToFP(ARGS, kInt, kFloat);
660 break;
661
662 case Instruction::INT_TO_DOUBLE:
663 EmitInsn_IntToFP(ARGS, kInt, kDouble);
664 break;
665
666 case Instruction::LONG_TO_INT:
667 EmitInsn_Trunc(ARGS);
668 break;
669
670 case Instruction::LONG_TO_FLOAT:
671 EmitInsn_IntToFP(ARGS, kLong, kFloat);
672 break;
673
674 case Instruction::LONG_TO_DOUBLE:
675 EmitInsn_IntToFP(ARGS, kLong, kDouble);
676 break;
677
678 case Instruction::FLOAT_TO_INT:
679 EmitInsn_FPToInt(ARGS, kFloat, kInt);
680 break;
681
682 case Instruction::FLOAT_TO_LONG:
683 EmitInsn_FPToInt(ARGS, kFloat, kLong);
684 break;
685
686 case Instruction::FLOAT_TO_DOUBLE:
687 EmitInsn_FExt(ARGS);
688 break;
689
690 case Instruction::DOUBLE_TO_INT:
691 EmitInsn_FPToInt(ARGS, kDouble, kInt);
692 break;
693
694 case Instruction::DOUBLE_TO_LONG:
695 EmitInsn_FPToInt(ARGS, kDouble, kLong);
696 break;
697
698 case Instruction::DOUBLE_TO_FLOAT:
699 EmitInsn_FTrunc(ARGS);
700 break;
701
702 case Instruction::INT_TO_BYTE:
703 EmitInsn_TruncAndSExt(ARGS, 8);
704 break;
705
706 case Instruction::INT_TO_CHAR:
707 EmitInsn_TruncAndZExt(ARGS, 16);
708 break;
709
710 case Instruction::INT_TO_SHORT:
711 EmitInsn_TruncAndSExt(ARGS, 16);
712 break;
713
714 case Instruction::ADD_INT:
715 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kInt, false);
716 break;
717
718 case Instruction::SUB_INT:
719 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kInt, false);
720 break;
721
722 case Instruction::MUL_INT:
723 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kInt, false);
724 break;
725
726 case Instruction::DIV_INT:
727 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kInt, false);
728 break;
729
730 case Instruction::REM_INT:
731 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kInt, false);
732 break;
733
734 case Instruction::AND_INT:
735 EmitInsn_IntArithm(ARGS, kIntArithm_And, kInt, false);
736 break;
737
738 case Instruction::OR_INT:
739 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kInt, false);
740 break;
741
742 case Instruction::XOR_INT:
743 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kInt, false);
744 break;
745
746 case Instruction::SHL_INT:
747 EmitInsn_IntArithm(ARGS, kIntArithm_Shl, kInt, false);
748 break;
749
750 case Instruction::SHR_INT:
751 EmitInsn_IntArithm(ARGS, kIntArithm_Shr, kInt, false);
752 break;
753
754 case Instruction::USHR_INT:
755 EmitInsn_IntArithm(ARGS, kIntArithm_UShr, kInt, false);
756 break;
757
758 case Instruction::ADD_LONG:
759 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kLong, false);
760 break;
761
762 case Instruction::SUB_LONG:
763 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kLong, false);
764 break;
765
766 case Instruction::MUL_LONG:
767 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kLong, false);
768 break;
769
770 case Instruction::DIV_LONG:
771 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kLong, false);
772 break;
773
774 case Instruction::REM_LONG:
775 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kLong, false);
776 break;
777
778 case Instruction::AND_LONG:
779 EmitInsn_IntArithm(ARGS, kIntArithm_And, kLong, false);
780 break;
781
782 case Instruction::OR_LONG:
783 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kLong, false);
784 break;
785
786 case Instruction::XOR_LONG:
787 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kLong, false);
788 break;
789
790 case Instruction::SHL_LONG:
791 EmitInsn_IntArithm(ARGS, kIntArithm_Shl, kLong, false);
792 break;
793
794 case Instruction::SHR_LONG:
795 EmitInsn_IntArithm(ARGS, kIntArithm_Shr, kLong, false);
796 break;
797
798 case Instruction::USHR_LONG:
799 EmitInsn_IntArithm(ARGS, kIntArithm_UShr, kLong, false);
800 break;
801
802 case Instruction::ADD_FLOAT:
803 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kFloat, false);
804 break;
805
806 case Instruction::SUB_FLOAT:
807 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kFloat, false);
808 break;
809
810 case Instruction::MUL_FLOAT:
811 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kFloat, false);
812 break;
813
814 case Instruction::DIV_FLOAT:
815 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kFloat, false);
816 break;
817
818 case Instruction::REM_FLOAT:
819 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kFloat, false);
820 break;
821
822 case Instruction::ADD_DOUBLE:
823 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kDouble, false);
824 break;
825
826 case Instruction::SUB_DOUBLE:
827 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kDouble, false);
828 break;
829
830 case Instruction::MUL_DOUBLE:
831 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kDouble, false);
832 break;
833
834 case Instruction::DIV_DOUBLE:
835 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kDouble, false);
836 break;
837
838 case Instruction::REM_DOUBLE:
839 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kDouble, false);
840 break;
841
842 case Instruction::ADD_INT_2ADDR:
843 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kInt, true);
844 break;
845
846 case Instruction::SUB_INT_2ADDR:
847 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kInt, true);
848 break;
849
850 case Instruction::MUL_INT_2ADDR:
851 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kInt, true);
852 break;
853
854 case Instruction::DIV_INT_2ADDR:
855 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kInt, true);
856 break;
857
858 case Instruction::REM_INT_2ADDR:
859 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kInt, true);
860 break;
861
862 case Instruction::AND_INT_2ADDR:
863 EmitInsn_IntArithm(ARGS, kIntArithm_And, kInt, true);
864 break;
865
866 case Instruction::OR_INT_2ADDR:
867 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kInt, true);
868 break;
869
870 case Instruction::XOR_INT_2ADDR:
871 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kInt, true);
872 break;
873
874 case Instruction::SHL_INT_2ADDR:
875 EmitInsn_IntArithm(ARGS, kIntArithm_Shl, kInt, true);
876 break;
877
878 case Instruction::SHR_INT_2ADDR:
879 EmitInsn_IntArithm(ARGS, kIntArithm_Shr, kInt, true);
880 break;
881
882 case Instruction::USHR_INT_2ADDR:
883 EmitInsn_IntArithm(ARGS, kIntArithm_UShr, kInt, true);
884 break;
885
886 case Instruction::ADD_LONG_2ADDR:
887 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kLong, true);
888 break;
889
890 case Instruction::SUB_LONG_2ADDR:
891 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kLong, true);
892 break;
893
894 case Instruction::MUL_LONG_2ADDR:
895 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kLong, true);
896 break;
897
898 case Instruction::DIV_LONG_2ADDR:
899 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kLong, true);
900 break;
901
902 case Instruction::REM_LONG_2ADDR:
903 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kLong, true);
904 break;
905
906 case Instruction::AND_LONG_2ADDR:
907 EmitInsn_IntArithm(ARGS, kIntArithm_And, kLong, true);
908 break;
909
910 case Instruction::OR_LONG_2ADDR:
911 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kLong, true);
912 break;
913
914 case Instruction::XOR_LONG_2ADDR:
915 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kLong, true);
916 break;
917
918 case Instruction::SHL_LONG_2ADDR:
919 EmitInsn_IntArithm(ARGS, kIntArithm_Shl, kLong, true);
920 break;
921
922 case Instruction::SHR_LONG_2ADDR:
923 EmitInsn_IntArithm(ARGS, kIntArithm_Shr, kLong, true);
924 break;
925
926 case Instruction::USHR_LONG_2ADDR:
927 EmitInsn_IntArithm(ARGS, kIntArithm_UShr, kLong, true);
928 break;
929
930 case Instruction::ADD_FLOAT_2ADDR:
931 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kFloat, true);
932 break;
933
934 case Instruction::SUB_FLOAT_2ADDR:
935 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kFloat, true);
936 break;
937
938 case Instruction::MUL_FLOAT_2ADDR:
939 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kFloat, true);
940 break;
941
942 case Instruction::DIV_FLOAT_2ADDR:
943 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kFloat, true);
944 break;
945
946 case Instruction::REM_FLOAT_2ADDR:
947 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kFloat, true);
948 break;
949
950 case Instruction::ADD_DOUBLE_2ADDR:
951 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kDouble, true);
952 break;
953
954 case Instruction::SUB_DOUBLE_2ADDR:
955 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kDouble, true);
956 break;
957
958 case Instruction::MUL_DOUBLE_2ADDR:
959 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kDouble, true);
960 break;
961
962 case Instruction::DIV_DOUBLE_2ADDR:
963 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kDouble, true);
964 break;
965
966 case Instruction::REM_DOUBLE_2ADDR:
967 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kDouble, true);
968 break;
969
970 case Instruction::ADD_INT_LIT16:
971 case Instruction::ADD_INT_LIT8:
972 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Add);
973 break;
974
975 case Instruction::RSUB_INT:
976 case Instruction::RSUB_INT_LIT8:
977 EmitInsn_RSubImmediate(ARGS);
978 break;
979
980 case Instruction::MUL_INT_LIT16:
981 case Instruction::MUL_INT_LIT8:
982 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Mul);
983 break;
984
985 case Instruction::DIV_INT_LIT16:
986 case Instruction::DIV_INT_LIT8:
987 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Div);
988 break;
989
990 case Instruction::REM_INT_LIT16:
991 case Instruction::REM_INT_LIT8:
992 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Rem);
993 break;
994
995 case Instruction::AND_INT_LIT16:
996 case Instruction::AND_INT_LIT8:
997 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_And);
998 break;
999
1000 case Instruction::OR_INT_LIT16:
1001 case Instruction::OR_INT_LIT8:
1002 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Or);
1003 break;
1004
1005 case Instruction::XOR_INT_LIT16:
1006 case Instruction::XOR_INT_LIT8:
1007 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Xor);
1008 break;
1009
1010 case Instruction::SHL_INT_LIT8:
1011 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Shl);
1012 break;
1013
1014 case Instruction::SHR_INT_LIT8:
1015 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Shr);
1016 break;
1017
1018 case Instruction::USHR_INT_LIT8:
1019 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_UShr);
1020 break;
1021
1022 case Instruction::UNUSED_3E:
1023 case Instruction::UNUSED_3F:
1024 case Instruction::UNUSED_40:
1025 case Instruction::UNUSED_41:
1026 case Instruction::UNUSED_42:
1027 case Instruction::UNUSED_43:
1028 case Instruction::UNUSED_73:
1029 case Instruction::UNUSED_79:
1030 case Instruction::UNUSED_7A:
1031 case Instruction::UNUSED_E3:
1032 case Instruction::UNUSED_E4:
1033 case Instruction::UNUSED_E5:
1034 case Instruction::UNUSED_E6:
1035 case Instruction::UNUSED_E7:
1036 case Instruction::UNUSED_E8:
1037 case Instruction::UNUSED_E9:
1038 case Instruction::UNUSED_EA:
1039 case Instruction::UNUSED_EB:
1040 case Instruction::UNUSED_EC:
1041 case Instruction::THROW_VERIFICATION_ERROR:
1042 case Instruction::UNUSED_EE:
1043 case Instruction::UNUSED_EF:
1044 case Instruction::UNUSED_F0:
1045 case Instruction::UNUSED_F1:
1046 case Instruction::UNUSED_F2:
1047 case Instruction::UNUSED_F3:
1048 case Instruction::UNUSED_F4:
1049 case Instruction::UNUSED_F5:
1050 case Instruction::UNUSED_F6:
1051 case Instruction::UNUSED_F7:
1052 case Instruction::UNUSED_F8:
1053 case Instruction::UNUSED_F9:
1054 case Instruction::UNUSED_FA:
1055 case Instruction::UNUSED_FB:
1056 case Instruction::UNUSED_FC:
1057 case Instruction::UNUSED_FD:
1058 case Instruction::UNUSED_FE:
1059 case Instruction::UNUSED_FF:
1060 LOG(FATAL) << "Dex file contains UNUSED bytecode: " << insn->Opcode();
1061 break;
1062 }
1063
1064#undef ARGS
1065}
1066
1067
1068void MethodCompiler::EmitInsn_Nop(uint32_t dex_pc,
1069 Instruction const* insn) {
Logan Chiene09a6b72011-12-27 17:50:21 +08001070
1071 uint16_t insn_signature = code_item_->insns_[dex_pc];
1072
1073 if (insn_signature == Instruction::kPackedSwitchSignature ||
1074 insn_signature == Instruction::kSparseSwitchSignature ||
1075 insn_signature == Instruction::kArrayDataSignature) {
1076 irb_.CreateUnreachable();
1077 } else{
1078 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1079 }
Shih-wei Liaod1fec812012-02-13 09:51:10 -08001080}
1081
1082
Logan Chien70f94b42011-12-27 17:49:11 +08001083void MethodCompiler::EmitInsn_Move(uint32_t dex_pc,
1084 Instruction const* insn,
1085 JType jty) {
Logan Chien48173132011-12-27 17:51:13 +08001086
1087 Instruction::DecodedInstruction dec_insn(insn);
1088
1089 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB_, jty, kReg);
1090 EmitStoreDalvikReg(dec_insn.vA_, jty, kReg, src_value);
1091
Logan Chien70f94b42011-12-27 17:49:11 +08001092 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1093}
1094
1095
1096void MethodCompiler::EmitInsn_MoveResult(uint32_t dex_pc,
1097 Instruction const* insn,
1098 JType jty) {
Logan Chien48173132011-12-27 17:51:13 +08001099
1100 Instruction::DecodedInstruction dec_insn(insn);
1101
1102 llvm::Value* src_value = EmitLoadDalvikRetValReg(jty, kReg);
1103 EmitStoreDalvikReg(dec_insn.vA_, jty, kReg, src_value);
1104
Logan Chien70f94b42011-12-27 17:49:11 +08001105 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1106}
1107
1108
1109void MethodCompiler::EmitInsn_MoveException(uint32_t dex_pc,
1110 Instruction const* insn) {
1111 // UNIMPLEMENTED(WARNING);
1112 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1113}
1114
1115
1116void MethodCompiler::EmitInsn_ThrowException(uint32_t dex_pc,
1117 Instruction const* insn) {
1118 // UNIMPLEMENTED(WARNING);
1119 irb_.CreateUnreachable();
1120}
1121
1122
1123void MethodCompiler::EmitInsn_ReturnVoid(uint32_t dex_pc,
1124 Instruction const* insn) {
Logan Chien8898a272011-12-27 17:51:56 +08001125 // Garbage collection safe-point
1126 EmitGuard_GarbageCollectionSuspend(dex_pc);
1127
1128 // Return!
1129 irb_.CreateRetVoid();
Logan Chien70f94b42011-12-27 17:49:11 +08001130}
1131
1132
1133void MethodCompiler::EmitInsn_Return(uint32_t dex_pc,
1134 Instruction const* insn) {
Logan Chien8898a272011-12-27 17:51:56 +08001135
1136 Instruction::DecodedInstruction dec_insn(insn);
1137
1138 // Garbage collection safe-point
1139 EmitGuard_GarbageCollectionSuspend(dex_pc);
1140
1141 // Return!
1142 char ret_shorty = method_helper_.GetShorty()[0];
1143 llvm::Value* retval = EmitLoadDalvikReg(dec_insn.vA_, ret_shorty, kAccurate);
1144
1145 irb_.CreateRet(retval);
Logan Chien70f94b42011-12-27 17:49:11 +08001146}
1147
1148
1149void MethodCompiler::EmitInsn_LoadConstant(uint32_t dex_pc,
1150 Instruction const* insn,
1151 JType imm_jty) {
Shih-wei Liao798366e2012-02-16 09:25:33 -08001152
1153 Instruction::DecodedInstruction dec_insn(insn);
1154
1155 DCHECK(imm_jty == kInt || imm_jty == kLong) << imm_jty;
1156
1157 int64_t imm = 0;
1158
1159 switch (insn->Opcode()) {
1160 // 32-bit Immediate
1161 case Instruction::CONST_4:
1162 case Instruction::CONST_16:
1163 case Instruction::CONST:
1164 case Instruction::CONST_WIDE_16:
1165 case Instruction::CONST_WIDE_32:
1166 imm = static_cast<int64_t>(static_cast<int32_t>(dec_insn.vB_));
1167 break;
1168
1169 case Instruction::CONST_HIGH16:
1170 imm = static_cast<int64_t>(static_cast<int32_t>(
1171 static_cast<uint32_t>(static_cast<uint16_t>(dec_insn.vB_)) << 16));
1172 break;
1173
1174 // 64-bit Immediate
1175 case Instruction::CONST_WIDE:
1176 imm = static_cast<int64_t>(dec_insn.vB_wide_);
1177 break;
1178
1179 case Instruction::CONST_WIDE_HIGH16:
1180 imm = static_cast<int64_t>(
1181 static_cast<uint64_t>(static_cast<uint16_t>(dec_insn.vB_)) << 48);
1182 break;
1183
1184 // Unknown opcode for load constant (unreachable)
1185 default:
1186 LOG(FATAL) << "Unknown opcode for load constant: " << insn->Opcode();
1187 break;
1188 }
1189
1190 // Store the non-object register
1191 llvm::Type* imm_type = irb_.getJType(imm_jty, kAccurate);
1192 llvm::Constant* imm_value = llvm::ConstantInt::getSigned(imm_type, imm);
1193 EmitStoreDalvikReg(dec_insn.vA_, imm_jty, kAccurate, imm_value);
1194
1195 // Store the object register if it is possible to be null.
1196 if (imm_jty == kInt && imm == 0) {
1197 EmitStoreDalvikReg(dec_insn.vA_, kObject, kAccurate, irb_.getJNull());
1198 }
1199
Logan Chien70f94b42011-12-27 17:49:11 +08001200 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1201}
1202
1203
1204void MethodCompiler::EmitInsn_LoadConstantString(uint32_t dex_pc,
1205 Instruction const* insn) {
1206 // UNIMPLEMENTED(WARNING);
1207 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1208}
1209
1210
1211void MethodCompiler::EmitInsn_LoadConstantClass(uint32_t dex_pc,
1212 Instruction const* insn) {
1213 // UNIMPLEMENTED(WARNING);
1214 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1215}
1216
1217
1218void MethodCompiler::EmitInsn_MonitorEnter(uint32_t dex_pc,
1219 Instruction const* insn) {
1220 // UNIMPLEMENTED(WARNING);
1221 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1222}
1223
1224
1225void MethodCompiler::EmitInsn_MonitorExit(uint32_t dex_pc,
1226 Instruction const* insn) {
1227 // UNIMPLEMENTED(WARNING);
1228 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1229}
1230
1231
1232void MethodCompiler::EmitInsn_CheckCast(uint32_t dex_pc,
1233 Instruction const* insn) {
1234 // UNIMPLEMENTED(WARNING);
1235 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1236}
1237
1238
1239void MethodCompiler::EmitInsn_InstanceOf(uint32_t dex_pc,
1240 Instruction const* insn) {
1241 // UNIMPLEMENTED(WARNING);
1242 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1243}
1244
1245
1246void MethodCompiler::EmitInsn_ArrayLength(uint32_t dex_pc,
1247 Instruction const* insn) {
1248 // UNIMPLEMENTED(WARNING);
1249 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1250}
1251
1252
1253void MethodCompiler::EmitInsn_NewInstance(uint32_t dex_pc,
1254 Instruction const* insn) {
1255 // UNIMPLEMENTED(WARNING);
1256 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1257}
1258
1259
1260void MethodCompiler::EmitInsn_NewArray(uint32_t dex_pc,
1261 Instruction const* insn) {
1262 // UNIMPLEMENTED(WARNING);
1263 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1264}
1265
1266
1267void MethodCompiler::EmitInsn_FilledNewArray(uint32_t dex_pc,
1268 Instruction const* insn,
1269 bool is_range) {
1270 // UNIMPLEMENTED(WARNING);
1271 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1272}
1273
1274
1275void MethodCompiler::EmitInsn_FillArrayData(uint32_t dex_pc,
1276 Instruction const* insn) {
1277 // UNIMPLEMENTED(WARNING);
1278 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1279}
1280
1281
1282void MethodCompiler::EmitInsn_UnconditionalBranch(uint32_t dex_pc,
1283 Instruction const* insn) {
Logan Chiena466c162011-12-27 17:55:46 +08001284
1285 Instruction::DecodedInstruction dec_insn(insn);
1286
1287 int32_t branch_offset = dec_insn.vA_;
1288
1289 if (branch_offset <= 0) {
1290 // Garbage collection safe-point on backward branch
1291 EmitGuard_GarbageCollectionSuspend(dex_pc);
1292 }
1293
1294 irb_.CreateBr(GetBasicBlock(dex_pc + branch_offset));
Logan Chien70f94b42011-12-27 17:49:11 +08001295}
1296
1297
1298void MethodCompiler::EmitInsn_PackedSwitch(uint32_t dex_pc,
1299 Instruction const* insn) {
Logan Chien7a89b6d2011-12-27 17:56:56 +08001300
1301 Instruction::DecodedInstruction dec_insn(insn);
1302
1303 struct PACKED Payload {
1304 uint16_t ident_;
1305 uint16_t num_cases_;
1306 int32_t first_key_;
1307 int32_t targets_[];
1308 };
1309
1310 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
1311 static_cast<int32_t>(dec_insn.vB_);
1312
1313 Payload const* payload =
1314 reinterpret_cast<Payload const*>(code_item_->insns_ + payload_offset);
1315
1316 llvm::Value* value = EmitLoadDalvikReg(dec_insn.vA_, kInt, kAccurate);
1317
1318 llvm::SwitchInst* sw =
1319 irb_.CreateSwitch(value, GetNextBasicBlock(dex_pc), payload->num_cases_);
1320
1321 for (uint16_t i = 0; i < payload->num_cases_; ++i) {
1322 sw->addCase(irb_.getInt32(payload->first_key_ + i),
1323 GetBasicBlock(dex_pc + payload->targets_[i]));
1324 }
Logan Chien70f94b42011-12-27 17:49:11 +08001325}
1326
1327
1328void MethodCompiler::EmitInsn_SparseSwitch(uint32_t dex_pc,
1329 Instruction const* insn) {
Logan Chien7a89b6d2011-12-27 17:56:56 +08001330
1331 Instruction::DecodedInstruction dec_insn(insn);
1332
1333 struct PACKED Payload {
1334 uint16_t ident_;
1335 uint16_t num_cases_;
1336 int32_t keys_and_targets_[];
1337 };
1338
1339 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
1340 static_cast<int32_t>(dec_insn.vB_);
1341
1342 Payload const* payload =
1343 reinterpret_cast<Payload const*>(code_item_->insns_ + payload_offset);
1344
1345 int32_t const* keys = payload->keys_and_targets_;
1346 int32_t const* targets = payload->keys_and_targets_ + payload->num_cases_;
1347
1348 llvm::Value* value = EmitLoadDalvikReg(dec_insn.vA_, kInt, kAccurate);
1349
1350 llvm::SwitchInst* sw =
1351 irb_.CreateSwitch(value, GetNextBasicBlock(dex_pc), payload->num_cases_);
1352
1353 for (size_t i = 0; i < payload->num_cases_; ++i) {
1354 sw->addCase(irb_.getInt32(keys[i]), GetBasicBlock(dex_pc + targets[i]));
1355 }
Logan Chien70f94b42011-12-27 17:49:11 +08001356}
1357
1358
1359void MethodCompiler::EmitInsn_FPCompare(uint32_t dex_pc,
1360 Instruction const* insn,
1361 JType fp_jty,
1362 bool gt_bias) {
Logan Chien2c37e8e2011-12-27 17:58:46 +08001363
1364 Instruction::DecodedInstruction dec_insn(insn);
1365
1366 DCHECK(fp_jty == kFloat || fp_jty == kDouble) << "JType: " << fp_jty;
1367
1368 llvm::Value* src1_value = EmitLoadDalvikReg(dec_insn.vB_, fp_jty, kAccurate);
1369 llvm::Value* src2_value = EmitLoadDalvikReg(dec_insn.vC_, fp_jty, kAccurate);
1370
1371 llvm::Value* cmp_eq = irb_.CreateFCmpOEQ(src1_value, src2_value);
1372 llvm::Value* cmp_lt;
1373
1374 if (gt_bias) {
1375 cmp_lt = irb_.CreateFCmpOLT(src1_value, src2_value);
1376 } else {
1377 cmp_lt = irb_.CreateFCmpULT(src1_value, src2_value);
1378 }
1379
1380 llvm::Value* result = EmitCompareResultSelection(cmp_eq, cmp_lt);
1381 EmitStoreDalvikReg(dec_insn.vA_, kInt, kAccurate, result);
1382
Logan Chien70f94b42011-12-27 17:49:11 +08001383 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1384}
1385
1386
1387void MethodCompiler::EmitInsn_LongCompare(uint32_t dex_pc,
1388 Instruction const* insn) {
Logan Chien2c37e8e2011-12-27 17:58:46 +08001389
1390 Instruction::DecodedInstruction dec_insn(insn);
1391
1392 llvm::Value* src1_value = EmitLoadDalvikReg(dec_insn.vB_, kLong, kAccurate);
1393 llvm::Value* src2_value = EmitLoadDalvikReg(dec_insn.vC_, kLong, kAccurate);
1394
1395 llvm::Value* cmp_eq = irb_.CreateICmpEQ(src1_value, src2_value);
1396 llvm::Value* cmp_lt = irb_.CreateICmpSLT(src1_value, src2_value);
1397
1398 llvm::Value* result = EmitCompareResultSelection(cmp_eq, cmp_lt);
1399 EmitStoreDalvikReg(dec_insn.vA_, kInt, kAccurate, result);
1400
Logan Chien70f94b42011-12-27 17:49:11 +08001401 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1402}
1403
1404
Logan Chien2c37e8e2011-12-27 17:58:46 +08001405llvm::Value* MethodCompiler::EmitCompareResultSelection(llvm::Value* cmp_eq,
1406 llvm::Value* cmp_lt) {
1407
1408 llvm::Constant* zero = irb_.getJInt(0);
1409 llvm::Constant* pos1 = irb_.getJInt(1);
1410 llvm::Constant* neg1 = irb_.getJInt(-1);
1411
1412 llvm::Value* result_lt = irb_.CreateSelect(cmp_lt, neg1, pos1);
1413 llvm::Value* result_eq = irb_.CreateSelect(cmp_eq, zero, result_lt);
1414
1415 return result_eq;
1416}
1417
1418
Logan Chien70f94b42011-12-27 17:49:11 +08001419void MethodCompiler::EmitInsn_BinaryConditionalBranch(uint32_t dex_pc,
1420 Instruction const* insn,
1421 CondBranchKind cond) {
Logan Chiena78e3c82011-12-27 17:59:35 +08001422
1423 Instruction::DecodedInstruction dec_insn(insn);
1424
1425 int8_t src1_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vA_);
1426 int8_t src2_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vB_);
1427
1428 DCHECK_NE(kRegUnknown, src1_reg_cat);
1429 DCHECK_NE(kRegUnknown, src2_reg_cat);
1430 DCHECK_NE(kRegCat2, src1_reg_cat);
1431 DCHECK_NE(kRegCat2, src2_reg_cat);
1432
1433 int32_t branch_offset = dec_insn.vC_;
1434
1435 if (branch_offset <= 0) {
1436 // Garbage collection safe-point on backward branch
1437 EmitGuard_GarbageCollectionSuspend(dex_pc);
1438 }
1439
1440 if (src1_reg_cat == kRegZero && src2_reg_cat == kRegZero) {
1441 irb_.CreateBr(GetBasicBlock(dex_pc + branch_offset));
1442 return;
1443 }
1444
1445 llvm::Value* src1_value;
1446 llvm::Value* src2_value;
1447
1448 if (src1_reg_cat != kRegZero && src2_reg_cat != kRegZero) {
1449 CHECK_EQ(src1_reg_cat, src2_reg_cat);
1450
1451 if (src1_reg_cat == kRegCat1nr) {
1452 src1_value = EmitLoadDalvikReg(dec_insn.vA_, kInt, kAccurate);
1453 src2_value = EmitLoadDalvikReg(dec_insn.vB_, kInt, kAccurate);
1454 } else {
1455 src1_value = EmitLoadDalvikReg(dec_insn.vA_, kObject, kAccurate);
1456 src2_value = EmitLoadDalvikReg(dec_insn.vB_, kObject, kAccurate);
1457 }
1458 } else {
1459 DCHECK(src1_reg_cat == kRegZero ||
1460 src2_reg_cat == kRegZero);
1461
1462 if (src1_reg_cat == kRegZero) {
1463 if (src2_reg_cat == kRegCat1nr) {
1464 src1_value = irb_.getJInt(0);
1465 src2_value = EmitLoadDalvikReg(dec_insn.vA_, kInt, kAccurate);
1466 } else {
1467 src1_value = irb_.getJNull();
1468 src2_value = EmitLoadDalvikReg(dec_insn.vA_, kObject, kAccurate);
1469 }
1470 } else { // src2_reg_cat == kRegZero
1471 if (src2_reg_cat == kRegCat1nr) {
1472 src1_value = EmitLoadDalvikReg(dec_insn.vA_, kInt, kAccurate);
1473 src2_value = irb_.getJInt(0);
1474 } else {
1475 src1_value = EmitLoadDalvikReg(dec_insn.vA_, kObject, kAccurate);
1476 src2_value = irb_.getJNull();
1477 }
1478 }
1479 }
1480
1481 llvm::Value* cond_value =
1482 EmitConditionResult(src1_value, src2_value, cond);
1483
1484 irb_.CreateCondBr(cond_value,
1485 GetBasicBlock(dex_pc + branch_offset),
1486 GetNextBasicBlock(dex_pc));
Logan Chien70f94b42011-12-27 17:49:11 +08001487}
1488
1489
1490void MethodCompiler::EmitInsn_UnaryConditionalBranch(uint32_t dex_pc,
1491 Instruction const* insn,
1492 CondBranchKind cond) {
Logan Chiena78e3c82011-12-27 17:59:35 +08001493
1494 Instruction::DecodedInstruction dec_insn(insn);
1495
1496 int8_t src_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vA_);
1497
1498 DCHECK_NE(kRegUnknown, src_reg_cat);
1499 DCHECK_NE(kRegCat2, src_reg_cat);
1500
1501 int32_t branch_offset = dec_insn.vB_;
1502
1503 if (branch_offset <= 0) {
1504 // Garbage collection safe-point on backward branch
1505 EmitGuard_GarbageCollectionSuspend(dex_pc);
1506 }
1507
1508 if (src_reg_cat == kRegZero) {
1509 irb_.CreateBr(GetBasicBlock(dex_pc + branch_offset));
1510 return;
1511 }
1512
1513 llvm::Value* src1_value;
1514 llvm::Value* src2_value;
1515
1516 if (src_reg_cat == kRegCat1nr) {
1517 src1_value = EmitLoadDalvikReg(dec_insn.vA_, kInt, kAccurate);
1518 src2_value = irb_.getInt32(0);
1519 } else {
1520 src1_value = EmitLoadDalvikReg(dec_insn.vA_, kObject, kAccurate);
1521 src2_value = irb_.getJNull();
1522 }
1523
1524 llvm::Value* cond_value =
1525 EmitConditionResult(src1_value, src2_value, cond);
1526
1527 irb_.CreateCondBr(cond_value,
1528 GetBasicBlock(dex_pc + branch_offset),
1529 GetNextBasicBlock(dex_pc));
1530}
1531
1532
1533RegCategory MethodCompiler::GetInferredRegCategory(uint32_t dex_pc,
1534 uint16_t reg_idx) {
1535 InferredRegCategoryMap const* map = method_->GetInferredRegCategoryMap();
1536 CHECK_NE(map, static_cast<InferredRegCategoryMap*>(NULL));
1537
1538 return map->GetRegCategory(dex_pc, reg_idx);
1539}
1540
1541
1542llvm::Value* MethodCompiler::EmitConditionResult(llvm::Value* lhs,
1543 llvm::Value* rhs,
1544 CondBranchKind cond) {
1545 switch (cond) {
1546 case kCondBranch_EQ:
1547 return irb_.CreateICmpEQ(lhs, rhs);
1548
1549 case kCondBranch_NE:
1550 return irb_.CreateICmpNE(lhs, rhs);
1551
1552 case kCondBranch_LT:
1553 return irb_.CreateICmpSLT(lhs, rhs);
1554
1555 case kCondBranch_GE:
1556 return irb_.CreateICmpSGE(lhs, rhs);
1557
1558 case kCondBranch_GT:
1559 return irb_.CreateICmpSGT(lhs, rhs);
1560
1561 case kCondBranch_LE:
1562 return irb_.CreateICmpSLE(lhs, rhs);
1563
1564 default: // Unreachable
1565 LOG(FATAL) << "Unknown conditional branch kind: " << cond;
1566 return NULL;
1567 }
Logan Chien70f94b42011-12-27 17:49:11 +08001568}
1569
1570
1571void MethodCompiler::EmitInsn_AGet(uint32_t dex_pc,
1572 Instruction const* insn,
1573 JType elem_jty) {
1574 // UNIMPLEMENTED(WARNING);
1575 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1576}
1577
1578
1579void MethodCompiler::EmitInsn_APut(uint32_t dex_pc,
1580 Instruction const* insn,
1581 JType elem_jty) {
1582 // UNIMPLEMENTED(WARNING);
1583 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1584}
1585
1586
1587void MethodCompiler::EmitInsn_IGet(uint32_t dex_pc,
1588 Instruction const* insn,
1589 JType field_jty) {
1590 // UNIMPLEMENTED(WARNING);
1591 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1592}
1593
1594
1595void MethodCompiler::EmitInsn_IPut(uint32_t dex_pc,
1596 Instruction const* insn,
1597 JType field_jty) {
1598 // UNIMPLEMENTED(WARNING);
1599 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1600}
1601
1602
1603void MethodCompiler::EmitInsn_SGet(uint32_t dex_pc,
1604 Instruction const* insn,
1605 JType field_jty) {
1606 // UNIMPLEMENTED(WARNING);
1607 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1608}
1609
1610
1611void MethodCompiler::EmitInsn_SPut(uint32_t dex_pc,
1612 Instruction const* insn,
1613 JType field_jty) {
1614 // UNIMPLEMENTED(WARNING);
1615 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1616}
1617
1618
1619void MethodCompiler::EmitInsn_InvokeVirtual(uint32_t dex_pc,
1620 Instruction const* insn,
1621 bool is_range) {
1622 // UNIMPLEMENTED(WARNING);
1623 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1624}
1625
1626
1627void MethodCompiler::EmitInsn_InvokeSuper(uint32_t dex_pc,
1628 Instruction const* insn,
1629 bool is_range) {
1630 // UNIMPLEMENTED(WARNING);
1631 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1632}
1633
1634
1635void MethodCompiler::EmitInsn_InvokeDirect(uint32_t dex_pc,
1636 Instruction const* insn,
1637 bool is_range) {
1638 // UNIMPLEMENTED(WARNING);
1639 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1640}
1641
1642
1643void MethodCompiler::EmitInsn_InvokeStatic(uint32_t dex_pc,
1644 Instruction const* insn,
1645 bool is_range) {
1646 // UNIMPLEMENTED(WARNING);
1647 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1648}
1649
1650
1651void MethodCompiler::EmitInsn_InvokeInterface(uint32_t dex_pc,
1652 Instruction const* insn,
1653 bool is_range) {
1654 // UNIMPLEMENTED(WARNING);
1655 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1656}
1657
1658
1659void MethodCompiler::EmitInsn_Neg(uint32_t dex_pc,
1660 Instruction const* insn,
1661 JType op_jty) {
Logan Chien1b5685f2011-12-27 18:01:14 +08001662
1663 Instruction::DecodedInstruction dec_insn(insn);
1664
1665 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
1666
1667 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB_, op_jty, kAccurate);
1668 llvm::Value* result_value = irb_.CreateNeg(src_value);
1669 EmitStoreDalvikReg(dec_insn.vA_, op_jty, kAccurate, result_value);
1670
Logan Chien70f94b42011-12-27 17:49:11 +08001671 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1672}
1673
1674
1675void MethodCompiler::EmitInsn_Not(uint32_t dex_pc,
1676 Instruction const* insn,
1677 JType op_jty) {
Logan Chiene53750d2011-12-27 18:02:27 +08001678
1679 Instruction::DecodedInstruction dec_insn(insn);
1680
1681 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
1682
1683 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB_, op_jty, kAccurate);
1684 llvm::Value* result_value =
1685 irb_.CreateXor(src_value, static_cast<uint64_t>(-1));
1686
1687 EmitStoreDalvikReg(dec_insn.vA_, op_jty, kAccurate, result_value);
1688
Logan Chien70f94b42011-12-27 17:49:11 +08001689 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1690}
1691
1692
1693void MethodCompiler::EmitInsn_SExt(uint32_t dex_pc,
1694 Instruction const* insn) {
1695 // UNIMPLEMENTED(WARNING);
1696 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1697}
1698
1699
1700void MethodCompiler::EmitInsn_Trunc(uint32_t dex_pc,
1701 Instruction const* insn) {
1702 // UNIMPLEMENTED(WARNING);
1703 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1704}
1705
1706
1707void MethodCompiler::EmitInsn_TruncAndSExt(uint32_t dex_pc,
1708 Instruction const* insn,
1709 unsigned N) {
1710 // UNIMPLEMENTED(WARNING);
1711 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1712}
1713
1714
1715void MethodCompiler::EmitInsn_TruncAndZExt(uint32_t dex_pc,
1716 Instruction const* insn,
1717 unsigned N) {
1718 // UNIMPLEMENTED(WARNING);
1719 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1720}
1721
1722
1723void MethodCompiler::EmitInsn_FNeg(uint32_t dex_pc,
1724 Instruction const* insn,
1725 JType op_jty) {
1726 // UNIMPLEMENTED(WARNING);
1727 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1728}
1729
1730
1731void MethodCompiler::EmitInsn_IntToFP(uint32_t dex_pc,
1732 Instruction const* insn,
1733 JType src_jty,
1734 JType dest_jty) {
1735 // UNIMPLEMENTED(WARNING);
1736 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1737}
1738
1739
1740void MethodCompiler::EmitInsn_FPToInt(uint32_t dex_pc,
1741 Instruction const* insn,
1742 JType src_jty,
1743 JType dest_jty) {
1744 // UNIMPLEMENTED(WARNING);
1745 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1746}
1747
1748
1749void MethodCompiler::EmitInsn_FExt(uint32_t dex_pc,
1750 Instruction const* insn) {
1751 // UNIMPLEMENTED(WARNING);
1752 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1753}
1754
1755
1756void MethodCompiler::EmitInsn_FTrunc(uint32_t dex_pc,
1757 Instruction const* insn) {
1758 // UNIMPLEMENTED(WARNING);
1759 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1760}
1761
1762
1763void MethodCompiler::EmitInsn_IntArithm(uint32_t dex_pc,
1764 Instruction const* insn,
1765 IntArithmKind arithm,
1766 JType op_jty,
1767 bool is_2addr) {
1768 // UNIMPLEMENTED(WARNING);
1769 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1770}
1771
1772
1773void MethodCompiler::EmitInsn_IntArithmImmediate(uint32_t dex_pc,
1774 Instruction const* insn,
1775 IntArithmKind arithm) {
1776 // UNIMPLEMENTED(WARNING);
1777 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1778}
1779
1780
1781void MethodCompiler::EmitInsn_RSubImmediate(uint32_t dex_pc,
1782 Instruction const* insn) {
1783 // UNIMPLEMENTED(WARNING);
1784 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1785}
1786
1787
1788void MethodCompiler::EmitInsn_FPArithm(uint32_t dex_pc,
1789 Instruction const* insn,
1790 FPArithmKind arithm,
1791 JType op_jty,
1792 bool is_2addr) {
1793 // UNIMPLEMENTED(WARNING);
1794 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1795}
1796
1797
Logan Chien83426162011-12-09 09:29:50 +08001798CompiledMethod *MethodCompiler::Compile() {
Logan Chien0b827102011-12-20 19:46:14 +08001799 // Code generation
1800 CreateFunction();
1801
1802 EmitPrologue();
1803 EmitInstructions();
Logan Chienc670a8d2011-12-20 21:25:56 +08001804 EmitPrologueLastBranch();
Logan Chien0b827102011-12-20 19:46:14 +08001805
Logan Chiend6c239a2011-12-23 15:11:45 +08001806 // Verify the generated bitcode
1807 llvm::verifyFunction(*func_, llvm::PrintMessageAction);
1808
Logan Chien0b827102011-12-20 19:46:14 +08001809 // Delete the inferred register category map (won't be used anymore)
1810 method_->ResetInferredRegCategoryMap();
1811
1812 return new CompiledMethod(insn_set_, func_);
1813}
1814
1815
1816llvm::Value* MethodCompiler::EmitLoadMethodObjectAddr() {
1817 return func_->arg_begin();
Shih-wei Liaod1fec812012-02-13 09:51:10 -08001818}
Logan Chien83426162011-12-09 09:29:50 +08001819
1820
Logan Chien5bcc04e2012-01-30 14:15:12 +08001821void MethodCompiler::EmitBranchExceptionLandingPad(uint32_t dex_pc) {
1822 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
1823 irb_.CreateBr(lpad);
1824 } else {
1825 irb_.CreateBr(GetUnwindBasicBlock());
1826 }
1827}
1828
1829
1830void MethodCompiler::EmitGuard_ExceptionLandingPad(uint32_t dex_pc) {
1831 llvm::Value* exception_pending =
1832 irb_.CreateCall(irb_.GetRuntime(IsExceptionPending));
1833
1834 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
1835
1836 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
1837 irb_.CreateCondBr(exception_pending, lpad, block_cont);
1838 } else {
1839 irb_.CreateCondBr(exception_pending, GetUnwindBasicBlock(), block_cont);
1840 }
1841
1842 irb_.SetInsertPoint(block_cont);
1843}
1844
1845
Logan Chien924072f2012-01-30 15:07:24 +08001846void MethodCompiler::EmitGuard_GarbageCollectionSuspend(uint32_t dex_pc) {
1847 llvm::Value* runtime_func = irb_.GetRuntime(TestSuspend);
1848 irb_.CreateCall(runtime_func);
1849
1850 EmitGuard_ExceptionLandingPad(dex_pc);
1851}
1852
1853
Logan Chiend6c239a2011-12-23 15:11:45 +08001854llvm::BasicBlock* MethodCompiler::
1855CreateBasicBlockWithDexPC(uint32_t dex_pc, char const* postfix) {
1856 std::string name;
1857
1858 if (postfix) {
1859 StringAppendF(&name, "B%u.%s", dex_pc, postfix);
1860 } else {
1861 StringAppendF(&name, "B%u", dex_pc);
1862 }
1863
1864 return llvm::BasicBlock::Create(*context_, name, func_);
1865}
1866
1867
1868llvm::BasicBlock* MethodCompiler::GetBasicBlock(uint32_t dex_pc) {
1869 DCHECK(dex_pc < code_item_->insns_size_in_code_units_);
1870
1871 llvm::BasicBlock* basic_block = basic_blocks_[dex_pc];
1872
1873 if (!basic_block) {
1874 basic_block = CreateBasicBlockWithDexPC(dex_pc);
1875 basic_blocks_[dex_pc] = basic_block;
1876 }
1877
1878 return basic_block;
1879}
1880
1881
1882llvm::BasicBlock*
1883MethodCompiler::GetNextBasicBlock(uint32_t dex_pc) {
1884 Instruction const* insn = Instruction::At(code_item_->insns_ + dex_pc);
1885 return GetBasicBlock(dex_pc + insn->SizeInCodeUnits());
1886}
1887
1888
Logan Chien5bcc04e2012-01-30 14:15:12 +08001889int32_t MethodCompiler::GetTryItemOffset(uint32_t dex_pc) {
1890 // TODO: Since we are emitting the dex instructions in ascending order
1891 // w.r.t. address, we can cache the lastest try item offset so that we
1892 // don't have to do binary search for every query.
1893
1894 int32_t min = 0;
1895 int32_t max = code_item_->tries_size_ - 1;
1896
1897 while (min <= max) {
1898 int32_t mid = min + (max - min) / 2;
1899
1900 DexFile::TryItem const* ti = DexFile::GetTryItems(*code_item_, mid);
1901 uint32_t start = ti->start_addr_;
1902 uint32_t end = start + ti->insn_count_;
1903
1904 if (dex_pc < start) {
1905 max = mid - 1;
1906 } else if (dex_pc >= end) {
1907 min = mid + 1;
1908 } else {
1909 return mid; // found
1910 }
1911 }
1912
1913 return -1; // not found
1914}
1915
1916
1917llvm::BasicBlock* MethodCompiler::GetLandingPadBasicBlock(uint32_t dex_pc) {
1918 // Find the try item for this address in this method
1919 int32_t ti_offset = GetTryItemOffset(dex_pc);
1920
1921 if (ti_offset == -1) {
1922 return NULL; // No landing pad is available for this address.
1923 }
1924
1925 // Check for the existing landing pad basic block
1926 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
1927 llvm::BasicBlock* block_lpad = basic_block_landing_pads_[ti_offset];
1928
1929 if (block_lpad) {
1930 // We have generated landing pad for this try item already. Return the
1931 // same basic block.
1932 return block_lpad;
1933 }
1934
1935 // Get try item from code item
1936 DexFile::TryItem const* ti = DexFile::GetTryItems(*code_item_, ti_offset);
1937
1938 // Create landing pad basic block
1939 block_lpad = llvm::BasicBlock::Create(*context_,
1940 StringPrintf("lpad%d", ti_offset),
1941 func_);
1942
1943 // Change IRBuilder insert point
1944 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
1945 irb_.SetInsertPoint(block_lpad);
1946
1947 // Find catch block with matching type
1948 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1949
1950 // TODO: Maybe passing try item offset will be a better idea? For now,
1951 // we are passing dex_pc, so that we can use existing runtime support
1952 // function directly. However, in the runtime supporting function we
1953 // have to search for try item with binary search which can be
1954 // eliminated.
1955 llvm::Value* dex_pc_value = irb_.getInt32(ti->start_addr_);
1956
1957 llvm::Value* catch_handler_index_value =
1958 irb_.CreateCall2(irb_.GetRuntime(FindCatchBlock),
1959 method_object_addr, dex_pc_value);
1960
1961 // Switch instruction (Go to unwind basic block by default)
1962 llvm::SwitchInst* sw =
1963 irb_.CreateSwitch(catch_handler_index_value, GetUnwindBasicBlock());
1964
1965 // Cases with matched catch block
1966 CatchHandlerIterator iter(*code_item_, ti->start_addr_);
1967
1968 for (uint32_t c = 0; iter.HasNext(); iter.Next(), ++c) {
1969 sw->addCase(irb_.getInt32(c), GetBasicBlock(iter.GetHandlerAddress()));
1970 }
1971
1972 // Restore the orignal insert point for IRBuilder
1973 irb_.restoreIP(irb_ip_original);
1974
1975 // Cache this landing pad
1976 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
1977 basic_block_landing_pads_[ti_offset] = block_lpad;
1978
1979 return block_lpad;
1980}
1981
1982
1983llvm::BasicBlock* MethodCompiler::GetUnwindBasicBlock() {
1984 // Check the existing unwinding baisc block block
1985 if (basic_block_unwind_ != NULL) {
1986 return basic_block_unwind_;
1987 }
1988
1989 // Create new basic block for unwinding
1990 basic_block_unwind_ =
1991 llvm::BasicBlock::Create(*context_, "exception_unwind", func_);
1992
1993 // Change IRBuilder insert point
1994 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
1995 irb_.SetInsertPoint(basic_block_unwind_);
1996
1997 // Emit the code to return default value (zero) for the given return type.
1998 char ret_shorty = method_helper_.GetShorty()[0];
1999 if (ret_shorty == 'V') {
2000 irb_.CreateRetVoid();
2001 } else {
2002 irb_.CreateRet(irb_.getJZero(ret_shorty));
2003 }
2004
2005 // Restore the orignal insert point for IRBuilder
2006 irb_.restoreIP(irb_ip_original);
2007
2008 return basic_block_unwind_;
2009}
2010
2011
Logan Chienc670a8d2011-12-20 21:25:56 +08002012llvm::Value* MethodCompiler::AllocDalvikLocalVarReg(RegCategory cat,
2013 uint32_t reg_idx) {
2014
2015 // Save current IR builder insert point
2016 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
2017
2018 // Alloca
2019 llvm::Value* reg_addr = NULL;
2020
2021 switch (cat) {
2022 case kRegCat1nr:
2023 irb_.SetInsertPoint(basic_block_reg_alloca_);
2024 reg_addr = irb_.CreateAlloca(irb_.getJIntTy(), 0,
2025 StringPrintf("r%u", reg_idx));
2026
2027 irb_.SetInsertPoint(basic_block_reg_zero_init_);
2028 irb_.CreateStore(irb_.getJInt(0), reg_addr);
2029 break;
2030
2031 case kRegCat2:
2032 irb_.SetInsertPoint(basic_block_reg_alloca_);
2033 reg_addr = irb_.CreateAlloca(irb_.getJLongTy(), 0,
2034 StringPrintf("w%u", reg_idx));
2035
2036 irb_.SetInsertPoint(basic_block_reg_zero_init_);
2037 irb_.CreateStore(irb_.getJLong(0), reg_addr);
2038 break;
2039
2040 case kRegObject:
2041 irb_.SetInsertPoint(basic_block_reg_alloca_);
2042 reg_addr = irb_.CreateAlloca(irb_.getJObjectTy(), 0,
2043 StringPrintf("p%u", reg_idx));
2044
2045 irb_.SetInsertPoint(basic_block_reg_zero_init_);
2046 irb_.CreateStore(irb_.getJNull(), reg_addr);
2047 break;
2048
2049 default:
2050 LOG(FATAL) << "Unknown register category for allocation: " << cat;
2051 }
2052
2053 // Restore IRBuilder insert point
2054 irb_.restoreIP(irb_ip_original);
2055
2056 DCHECK_NE(reg_addr, static_cast<llvm::Value*>(NULL));
2057 return reg_addr;
2058}
2059
2060
2061llvm::Value* MethodCompiler::AllocDalvikRetValReg(RegCategory cat) {
2062 // Save current IR builder insert point
2063 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
2064
2065 // Alloca
2066 llvm::Value* reg_addr = NULL;
2067
2068 switch (cat) {
2069 case kRegCat1nr:
2070 irb_.SetInsertPoint(basic_block_reg_alloca_);
2071 reg_addr = irb_.CreateAlloca(irb_.getJIntTy(), 0, "r_res");
2072 break;
2073
2074 case kRegCat2:
2075 irb_.SetInsertPoint(basic_block_reg_alloca_);
2076 reg_addr = irb_.CreateAlloca(irb_.getJLongTy(), 0, "w_res");
2077 break;
2078
2079 case kRegObject:
2080 irb_.SetInsertPoint(basic_block_reg_alloca_);
2081 reg_addr = irb_.CreateAlloca(irb_.getJObjectTy(), 0, "p_res");
2082 break;
2083
2084 default:
2085 LOG(FATAL) << "Unknown register category for allocation: " << cat;
2086 }
2087
2088 // Restore IRBuilder insert point
2089 irb_.restoreIP(irb_ip_original);
2090
2091 DCHECK_NE(reg_addr, static_cast<llvm::Value*>(NULL));
2092 return reg_addr;
2093}
2094
2095
Logan Chien83426162011-12-09 09:29:50 +08002096} // namespace compiler_llvm
2097} // namespace art