blob: 2b0fe1981f51f01f61f5aa0ef5262cb103541337 [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) {
1085 // UNIMPLEMENTED(WARNING);
1086 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1087}
1088
1089
1090void MethodCompiler::EmitInsn_MoveResult(uint32_t dex_pc,
1091 Instruction const* insn,
1092 JType jty) {
1093 // UNIMPLEMENTED(WARNING);
1094 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1095}
1096
1097
1098void MethodCompiler::EmitInsn_MoveException(uint32_t dex_pc,
1099 Instruction const* insn) {
1100 // UNIMPLEMENTED(WARNING);
1101 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1102}
1103
1104
1105void MethodCompiler::EmitInsn_ThrowException(uint32_t dex_pc,
1106 Instruction const* insn) {
1107 // UNIMPLEMENTED(WARNING);
1108 irb_.CreateUnreachable();
1109}
1110
1111
1112void MethodCompiler::EmitInsn_ReturnVoid(uint32_t dex_pc,
1113 Instruction const* insn) {
1114 // UNIMPLEMENTED(WARNING);
1115 irb_.CreateUnreachable();
1116}
1117
1118
1119void MethodCompiler::EmitInsn_Return(uint32_t dex_pc,
1120 Instruction const* insn) {
1121 // UNIMPLEMENTED(WARNING);
1122 irb_.CreateUnreachable();
1123}
1124
1125
1126void MethodCompiler::EmitInsn_LoadConstant(uint32_t dex_pc,
1127 Instruction const* insn,
1128 JType imm_jty) {
1129 // UNIMPLEMENTED(WARNING);
1130 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1131}
1132
1133
1134void MethodCompiler::EmitInsn_LoadConstantString(uint32_t dex_pc,
1135 Instruction const* insn) {
1136 // UNIMPLEMENTED(WARNING);
1137 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1138}
1139
1140
1141void MethodCompiler::EmitInsn_LoadConstantClass(uint32_t dex_pc,
1142 Instruction const* insn) {
1143 // UNIMPLEMENTED(WARNING);
1144 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1145}
1146
1147
1148void MethodCompiler::EmitInsn_MonitorEnter(uint32_t dex_pc,
1149 Instruction const* insn) {
1150 // UNIMPLEMENTED(WARNING);
1151 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1152}
1153
1154
1155void MethodCompiler::EmitInsn_MonitorExit(uint32_t dex_pc,
1156 Instruction const* insn) {
1157 // UNIMPLEMENTED(WARNING);
1158 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1159}
1160
1161
1162void MethodCompiler::EmitInsn_CheckCast(uint32_t dex_pc,
1163 Instruction const* insn) {
1164 // UNIMPLEMENTED(WARNING);
1165 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1166}
1167
1168
1169void MethodCompiler::EmitInsn_InstanceOf(uint32_t dex_pc,
1170 Instruction const* insn) {
1171 // UNIMPLEMENTED(WARNING);
1172 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1173}
1174
1175
1176void MethodCompiler::EmitInsn_ArrayLength(uint32_t dex_pc,
1177 Instruction const* insn) {
1178 // UNIMPLEMENTED(WARNING);
1179 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1180}
1181
1182
1183void MethodCompiler::EmitInsn_NewInstance(uint32_t dex_pc,
1184 Instruction const* insn) {
1185 // UNIMPLEMENTED(WARNING);
1186 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1187}
1188
1189
1190void MethodCompiler::EmitInsn_NewArray(uint32_t dex_pc,
1191 Instruction const* insn) {
1192 // UNIMPLEMENTED(WARNING);
1193 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1194}
1195
1196
1197void MethodCompiler::EmitInsn_FilledNewArray(uint32_t dex_pc,
1198 Instruction const* insn,
1199 bool is_range) {
1200 // UNIMPLEMENTED(WARNING);
1201 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1202}
1203
1204
1205void MethodCompiler::EmitInsn_FillArrayData(uint32_t dex_pc,
1206 Instruction const* insn) {
1207 // UNIMPLEMENTED(WARNING);
1208 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1209}
1210
1211
1212void MethodCompiler::EmitInsn_UnconditionalBranch(uint32_t dex_pc,
1213 Instruction const* insn) {
1214 // UNIMPLEMENTED(WARNING);
1215 irb_.CreateUnreachable();
1216}
1217
1218
1219void MethodCompiler::EmitInsn_PackedSwitch(uint32_t dex_pc,
1220 Instruction const* insn) {
1221 // UNIMPLEMENTED(WARNING);
1222 irb_.CreateUnreachable();
1223}
1224
1225
1226void MethodCompiler::EmitInsn_SparseSwitch(uint32_t dex_pc,
1227 Instruction const* insn) {
1228 // UNIMPLEMENTED(WARNING);
1229 irb_.CreateUnreachable();
1230}
1231
1232
1233void MethodCompiler::EmitInsn_FPCompare(uint32_t dex_pc,
1234 Instruction const* insn,
1235 JType fp_jty,
1236 bool gt_bias) {
1237 // UNIMPLEMENTED(WARNING);
1238 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1239}
1240
1241
1242void MethodCompiler::EmitInsn_LongCompare(uint32_t dex_pc,
1243 Instruction const* insn) {
1244 // UNIMPLEMENTED(WARNING);
1245 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1246}
1247
1248
1249void MethodCompiler::EmitInsn_BinaryConditionalBranch(uint32_t dex_pc,
1250 Instruction const* insn,
1251 CondBranchKind cond) {
1252 // UNIMPLEMENTED(WARNING);
1253 irb_.CreateUnreachable();
1254}
1255
1256
1257void MethodCompiler::EmitInsn_UnaryConditionalBranch(uint32_t dex_pc,
1258 Instruction const* insn,
1259 CondBranchKind cond) {
1260 // UNIMPLEMENTED(WARNING);
1261 irb_.CreateUnreachable();
1262}
1263
1264
1265void MethodCompiler::EmitInsn_AGet(uint32_t dex_pc,
1266 Instruction const* insn,
1267 JType elem_jty) {
1268 // UNIMPLEMENTED(WARNING);
1269 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1270}
1271
1272
1273void MethodCompiler::EmitInsn_APut(uint32_t dex_pc,
1274 Instruction const* insn,
1275 JType elem_jty) {
1276 // UNIMPLEMENTED(WARNING);
1277 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1278}
1279
1280
1281void MethodCompiler::EmitInsn_IGet(uint32_t dex_pc,
1282 Instruction const* insn,
1283 JType field_jty) {
1284 // UNIMPLEMENTED(WARNING);
1285 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1286}
1287
1288
1289void MethodCompiler::EmitInsn_IPut(uint32_t dex_pc,
1290 Instruction const* insn,
1291 JType field_jty) {
1292 // UNIMPLEMENTED(WARNING);
1293 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1294}
1295
1296
1297void MethodCompiler::EmitInsn_SGet(uint32_t dex_pc,
1298 Instruction const* insn,
1299 JType field_jty) {
1300 // UNIMPLEMENTED(WARNING);
1301 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1302}
1303
1304
1305void MethodCompiler::EmitInsn_SPut(uint32_t dex_pc,
1306 Instruction const* insn,
1307 JType field_jty) {
1308 // UNIMPLEMENTED(WARNING);
1309 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1310}
1311
1312
1313void MethodCompiler::EmitInsn_InvokeVirtual(uint32_t dex_pc,
1314 Instruction const* insn,
1315 bool is_range) {
1316 // UNIMPLEMENTED(WARNING);
1317 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1318}
1319
1320
1321void MethodCompiler::EmitInsn_InvokeSuper(uint32_t dex_pc,
1322 Instruction const* insn,
1323 bool is_range) {
1324 // UNIMPLEMENTED(WARNING);
1325 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1326}
1327
1328
1329void MethodCompiler::EmitInsn_InvokeDirect(uint32_t dex_pc,
1330 Instruction const* insn,
1331 bool is_range) {
1332 // UNIMPLEMENTED(WARNING);
1333 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1334}
1335
1336
1337void MethodCompiler::EmitInsn_InvokeStatic(uint32_t dex_pc,
1338 Instruction const* insn,
1339 bool is_range) {
1340 // UNIMPLEMENTED(WARNING);
1341 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1342}
1343
1344
1345void MethodCompiler::EmitInsn_InvokeInterface(uint32_t dex_pc,
1346 Instruction const* insn,
1347 bool is_range) {
1348 // UNIMPLEMENTED(WARNING);
1349 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1350}
1351
1352
1353void MethodCompiler::EmitInsn_Neg(uint32_t dex_pc,
1354 Instruction const* insn,
1355 JType op_jty) {
1356 // UNIMPLEMENTED(WARNING);
1357 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1358}
1359
1360
1361void MethodCompiler::EmitInsn_Not(uint32_t dex_pc,
1362 Instruction const* insn,
1363 JType op_jty) {
1364 // UNIMPLEMENTED(WARNING);
1365 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1366}
1367
1368
1369void MethodCompiler::EmitInsn_SExt(uint32_t dex_pc,
1370 Instruction const* insn) {
1371 // UNIMPLEMENTED(WARNING);
1372 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1373}
1374
1375
1376void MethodCompiler::EmitInsn_Trunc(uint32_t dex_pc,
1377 Instruction const* insn) {
1378 // UNIMPLEMENTED(WARNING);
1379 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1380}
1381
1382
1383void MethodCompiler::EmitInsn_TruncAndSExt(uint32_t dex_pc,
1384 Instruction const* insn,
1385 unsigned N) {
1386 // UNIMPLEMENTED(WARNING);
1387 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1388}
1389
1390
1391void MethodCompiler::EmitInsn_TruncAndZExt(uint32_t dex_pc,
1392 Instruction const* insn,
1393 unsigned N) {
1394 // UNIMPLEMENTED(WARNING);
1395 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1396}
1397
1398
1399void MethodCompiler::EmitInsn_FNeg(uint32_t dex_pc,
1400 Instruction const* insn,
1401 JType op_jty) {
1402 // UNIMPLEMENTED(WARNING);
1403 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1404}
1405
1406
1407void MethodCompiler::EmitInsn_IntToFP(uint32_t dex_pc,
1408 Instruction const* insn,
1409 JType src_jty,
1410 JType dest_jty) {
1411 // UNIMPLEMENTED(WARNING);
1412 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1413}
1414
1415
1416void MethodCompiler::EmitInsn_FPToInt(uint32_t dex_pc,
1417 Instruction const* insn,
1418 JType src_jty,
1419 JType dest_jty) {
1420 // UNIMPLEMENTED(WARNING);
1421 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1422}
1423
1424
1425void MethodCompiler::EmitInsn_FExt(uint32_t dex_pc,
1426 Instruction const* insn) {
1427 // UNIMPLEMENTED(WARNING);
1428 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1429}
1430
1431
1432void MethodCompiler::EmitInsn_FTrunc(uint32_t dex_pc,
1433 Instruction const* insn) {
1434 // UNIMPLEMENTED(WARNING);
1435 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1436}
1437
1438
1439void MethodCompiler::EmitInsn_IntArithm(uint32_t dex_pc,
1440 Instruction const* insn,
1441 IntArithmKind arithm,
1442 JType op_jty,
1443 bool is_2addr) {
1444 // UNIMPLEMENTED(WARNING);
1445 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1446}
1447
1448
1449void MethodCompiler::EmitInsn_IntArithmImmediate(uint32_t dex_pc,
1450 Instruction const* insn,
1451 IntArithmKind arithm) {
1452 // UNIMPLEMENTED(WARNING);
1453 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1454}
1455
1456
1457void MethodCompiler::EmitInsn_RSubImmediate(uint32_t dex_pc,
1458 Instruction const* insn) {
1459 // UNIMPLEMENTED(WARNING);
1460 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1461}
1462
1463
1464void MethodCompiler::EmitInsn_FPArithm(uint32_t dex_pc,
1465 Instruction const* insn,
1466 FPArithmKind arithm,
1467 JType op_jty,
1468 bool is_2addr) {
1469 // UNIMPLEMENTED(WARNING);
1470 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1471}
1472
1473
Logan Chien83426162011-12-09 09:29:50 +08001474CompiledMethod *MethodCompiler::Compile() {
Logan Chien0b827102011-12-20 19:46:14 +08001475 // Code generation
1476 CreateFunction();
1477
1478 EmitPrologue();
1479 EmitInstructions();
Logan Chienc670a8d2011-12-20 21:25:56 +08001480 EmitPrologueLastBranch();
Logan Chien0b827102011-12-20 19:46:14 +08001481
Logan Chiend6c239a2011-12-23 15:11:45 +08001482 // Verify the generated bitcode
1483 llvm::verifyFunction(*func_, llvm::PrintMessageAction);
1484
Logan Chien0b827102011-12-20 19:46:14 +08001485 // Delete the inferred register category map (won't be used anymore)
1486 method_->ResetInferredRegCategoryMap();
1487
1488 return new CompiledMethod(insn_set_, func_);
1489}
1490
1491
1492llvm::Value* MethodCompiler::EmitLoadMethodObjectAddr() {
1493 return func_->arg_begin();
Shih-wei Liaod1fec812012-02-13 09:51:10 -08001494}
Logan Chien83426162011-12-09 09:29:50 +08001495
1496
Logan Chien5bcc04e2012-01-30 14:15:12 +08001497void MethodCompiler::EmitBranchExceptionLandingPad(uint32_t dex_pc) {
1498 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
1499 irb_.CreateBr(lpad);
1500 } else {
1501 irb_.CreateBr(GetUnwindBasicBlock());
1502 }
1503}
1504
1505
1506void MethodCompiler::EmitGuard_ExceptionLandingPad(uint32_t dex_pc) {
1507 llvm::Value* exception_pending =
1508 irb_.CreateCall(irb_.GetRuntime(IsExceptionPending));
1509
1510 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
1511
1512 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
1513 irb_.CreateCondBr(exception_pending, lpad, block_cont);
1514 } else {
1515 irb_.CreateCondBr(exception_pending, GetUnwindBasicBlock(), block_cont);
1516 }
1517
1518 irb_.SetInsertPoint(block_cont);
1519}
1520
1521
Logan Chien924072f2012-01-30 15:07:24 +08001522void MethodCompiler::EmitGuard_GarbageCollectionSuspend(uint32_t dex_pc) {
1523 llvm::Value* runtime_func = irb_.GetRuntime(TestSuspend);
1524 irb_.CreateCall(runtime_func);
1525
1526 EmitGuard_ExceptionLandingPad(dex_pc);
1527}
1528
1529
Logan Chiend6c239a2011-12-23 15:11:45 +08001530llvm::BasicBlock* MethodCompiler::
1531CreateBasicBlockWithDexPC(uint32_t dex_pc, char const* postfix) {
1532 std::string name;
1533
1534 if (postfix) {
1535 StringAppendF(&name, "B%u.%s", dex_pc, postfix);
1536 } else {
1537 StringAppendF(&name, "B%u", dex_pc);
1538 }
1539
1540 return llvm::BasicBlock::Create(*context_, name, func_);
1541}
1542
1543
1544llvm::BasicBlock* MethodCompiler::GetBasicBlock(uint32_t dex_pc) {
1545 DCHECK(dex_pc < code_item_->insns_size_in_code_units_);
1546
1547 llvm::BasicBlock* basic_block = basic_blocks_[dex_pc];
1548
1549 if (!basic_block) {
1550 basic_block = CreateBasicBlockWithDexPC(dex_pc);
1551 basic_blocks_[dex_pc] = basic_block;
1552 }
1553
1554 return basic_block;
1555}
1556
1557
1558llvm::BasicBlock*
1559MethodCompiler::GetNextBasicBlock(uint32_t dex_pc) {
1560 Instruction const* insn = Instruction::At(code_item_->insns_ + dex_pc);
1561 return GetBasicBlock(dex_pc + insn->SizeInCodeUnits());
1562}
1563
1564
Logan Chien5bcc04e2012-01-30 14:15:12 +08001565int32_t MethodCompiler::GetTryItemOffset(uint32_t dex_pc) {
1566 // TODO: Since we are emitting the dex instructions in ascending order
1567 // w.r.t. address, we can cache the lastest try item offset so that we
1568 // don't have to do binary search for every query.
1569
1570 int32_t min = 0;
1571 int32_t max = code_item_->tries_size_ - 1;
1572
1573 while (min <= max) {
1574 int32_t mid = min + (max - min) / 2;
1575
1576 DexFile::TryItem const* ti = DexFile::GetTryItems(*code_item_, mid);
1577 uint32_t start = ti->start_addr_;
1578 uint32_t end = start + ti->insn_count_;
1579
1580 if (dex_pc < start) {
1581 max = mid - 1;
1582 } else if (dex_pc >= end) {
1583 min = mid + 1;
1584 } else {
1585 return mid; // found
1586 }
1587 }
1588
1589 return -1; // not found
1590}
1591
1592
1593llvm::BasicBlock* MethodCompiler::GetLandingPadBasicBlock(uint32_t dex_pc) {
1594 // Find the try item for this address in this method
1595 int32_t ti_offset = GetTryItemOffset(dex_pc);
1596
1597 if (ti_offset == -1) {
1598 return NULL; // No landing pad is available for this address.
1599 }
1600
1601 // Check for the existing landing pad basic block
1602 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
1603 llvm::BasicBlock* block_lpad = basic_block_landing_pads_[ti_offset];
1604
1605 if (block_lpad) {
1606 // We have generated landing pad for this try item already. Return the
1607 // same basic block.
1608 return block_lpad;
1609 }
1610
1611 // Get try item from code item
1612 DexFile::TryItem const* ti = DexFile::GetTryItems(*code_item_, ti_offset);
1613
1614 // Create landing pad basic block
1615 block_lpad = llvm::BasicBlock::Create(*context_,
1616 StringPrintf("lpad%d", ti_offset),
1617 func_);
1618
1619 // Change IRBuilder insert point
1620 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
1621 irb_.SetInsertPoint(block_lpad);
1622
1623 // Find catch block with matching type
1624 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1625
1626 // TODO: Maybe passing try item offset will be a better idea? For now,
1627 // we are passing dex_pc, so that we can use existing runtime support
1628 // function directly. However, in the runtime supporting function we
1629 // have to search for try item with binary search which can be
1630 // eliminated.
1631 llvm::Value* dex_pc_value = irb_.getInt32(ti->start_addr_);
1632
1633 llvm::Value* catch_handler_index_value =
1634 irb_.CreateCall2(irb_.GetRuntime(FindCatchBlock),
1635 method_object_addr, dex_pc_value);
1636
1637 // Switch instruction (Go to unwind basic block by default)
1638 llvm::SwitchInst* sw =
1639 irb_.CreateSwitch(catch_handler_index_value, GetUnwindBasicBlock());
1640
1641 // Cases with matched catch block
1642 CatchHandlerIterator iter(*code_item_, ti->start_addr_);
1643
1644 for (uint32_t c = 0; iter.HasNext(); iter.Next(), ++c) {
1645 sw->addCase(irb_.getInt32(c), GetBasicBlock(iter.GetHandlerAddress()));
1646 }
1647
1648 // Restore the orignal insert point for IRBuilder
1649 irb_.restoreIP(irb_ip_original);
1650
1651 // Cache this landing pad
1652 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
1653 basic_block_landing_pads_[ti_offset] = block_lpad;
1654
1655 return block_lpad;
1656}
1657
1658
1659llvm::BasicBlock* MethodCompiler::GetUnwindBasicBlock() {
1660 // Check the existing unwinding baisc block block
1661 if (basic_block_unwind_ != NULL) {
1662 return basic_block_unwind_;
1663 }
1664
1665 // Create new basic block for unwinding
1666 basic_block_unwind_ =
1667 llvm::BasicBlock::Create(*context_, "exception_unwind", func_);
1668
1669 // Change IRBuilder insert point
1670 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
1671 irb_.SetInsertPoint(basic_block_unwind_);
1672
1673 // Emit the code to return default value (zero) for the given return type.
1674 char ret_shorty = method_helper_.GetShorty()[0];
1675 if (ret_shorty == 'V') {
1676 irb_.CreateRetVoid();
1677 } else {
1678 irb_.CreateRet(irb_.getJZero(ret_shorty));
1679 }
1680
1681 // Restore the orignal insert point for IRBuilder
1682 irb_.restoreIP(irb_ip_original);
1683
1684 return basic_block_unwind_;
1685}
1686
1687
Logan Chienc670a8d2011-12-20 21:25:56 +08001688llvm::Value* MethodCompiler::AllocDalvikLocalVarReg(RegCategory cat,
1689 uint32_t reg_idx) {
1690
1691 // Save current IR builder insert point
1692 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
1693
1694 // Alloca
1695 llvm::Value* reg_addr = NULL;
1696
1697 switch (cat) {
1698 case kRegCat1nr:
1699 irb_.SetInsertPoint(basic_block_reg_alloca_);
1700 reg_addr = irb_.CreateAlloca(irb_.getJIntTy(), 0,
1701 StringPrintf("r%u", reg_idx));
1702
1703 irb_.SetInsertPoint(basic_block_reg_zero_init_);
1704 irb_.CreateStore(irb_.getJInt(0), reg_addr);
1705 break;
1706
1707 case kRegCat2:
1708 irb_.SetInsertPoint(basic_block_reg_alloca_);
1709 reg_addr = irb_.CreateAlloca(irb_.getJLongTy(), 0,
1710 StringPrintf("w%u", reg_idx));
1711
1712 irb_.SetInsertPoint(basic_block_reg_zero_init_);
1713 irb_.CreateStore(irb_.getJLong(0), reg_addr);
1714 break;
1715
1716 case kRegObject:
1717 irb_.SetInsertPoint(basic_block_reg_alloca_);
1718 reg_addr = irb_.CreateAlloca(irb_.getJObjectTy(), 0,
1719 StringPrintf("p%u", reg_idx));
1720
1721 irb_.SetInsertPoint(basic_block_reg_zero_init_);
1722 irb_.CreateStore(irb_.getJNull(), reg_addr);
1723 break;
1724
1725 default:
1726 LOG(FATAL) << "Unknown register category for allocation: " << cat;
1727 }
1728
1729 // Restore IRBuilder insert point
1730 irb_.restoreIP(irb_ip_original);
1731
1732 DCHECK_NE(reg_addr, static_cast<llvm::Value*>(NULL));
1733 return reg_addr;
1734}
1735
1736
1737llvm::Value* MethodCompiler::AllocDalvikRetValReg(RegCategory cat) {
1738 // Save current IR builder insert point
1739 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
1740
1741 // Alloca
1742 llvm::Value* reg_addr = NULL;
1743
1744 switch (cat) {
1745 case kRegCat1nr:
1746 irb_.SetInsertPoint(basic_block_reg_alloca_);
1747 reg_addr = irb_.CreateAlloca(irb_.getJIntTy(), 0, "r_res");
1748 break;
1749
1750 case kRegCat2:
1751 irb_.SetInsertPoint(basic_block_reg_alloca_);
1752 reg_addr = irb_.CreateAlloca(irb_.getJLongTy(), 0, "w_res");
1753 break;
1754
1755 case kRegObject:
1756 irb_.SetInsertPoint(basic_block_reg_alloca_);
1757 reg_addr = irb_.CreateAlloca(irb_.getJObjectTy(), 0, "p_res");
1758 break;
1759
1760 default:
1761 LOG(FATAL) << "Unknown register category for allocation: " << cat;
1762 }
1763
1764 // Restore IRBuilder insert point
1765 irb_.restoreIP(irb_ip_original);
1766
1767 DCHECK_NE(reg_addr, static_cast<llvm::Value*>(NULL));
1768 return reg_addr;
1769}
1770
1771
Logan Chien83426162011-12-09 09:29:50 +08001772} // namespace compiler_llvm
1773} // namespace art