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