blob: 491ced3920036950ed2305b1b782266c53c7f3ad [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#ifndef ART_SRC_COMPILER_LLVM_METHOD_COMPILER_H_
18#define ART_SRC_COMPILER_LLVM_METHOD_COMPILER_H_
19
Logan Chienfca7e872011-12-20 20:08:22 +080020#include "backend_types.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080021#include "constants.h"
Logan Chienc670a8d2011-12-20 21:25:56 +080022#include "dalvik_reg.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080023#include "dex_file.h"
24#include "dex_instruction.h"
25#include "object_utils.h"
26
27#include <llvm/Support/IRBuilder.h>
28
29#include <vector>
30
31#include <stdint.h>
32
33
34namespace art {
35 class ClassLinker;
36 class ClassLoader;
37 class CompiledMethod;
38 class Compiler;
39 class DexCache;
40}
41
42
43namespace llvm {
44 class AllocaInst;
45 class BasicBlock;
46 class Function;
47 class FunctionType;
48 class LLVMContext;
49 class Module;
50 class Type;
51}
52
53
54namespace art {
55namespace compiler_llvm {
56
57class CompilerLLVM;
58class IRBuilder;
59
60class MethodCompiler {
61 private:
Logan Chien83426162011-12-09 09:29:50 +080062 InstructionSet insn_set_;
63 Compiler* compiler_;
64 compiler_llvm::CompilerLLVM* compiler_llvm_;
Shih-wei Liaod1fec812012-02-13 09:51:10 -080065
Logan Chien83426162011-12-09 09:29:50 +080066 ClassLinker* class_linker_;
67 ClassLoader const* class_loader_;
Shih-wei Liaod1fec812012-02-13 09:51:10 -080068
Logan Chien83426162011-12-09 09:29:50 +080069 DexFile const* dex_file_;
70 DexCache* dex_cache_;
71 DexFile::CodeItem const* code_item_;
Shih-wei Liaod1fec812012-02-13 09:51:10 -080072
Logan Chien83426162011-12-09 09:29:50 +080073 Method* method_;
74 MethodHelper method_helper_;
Shih-wei Liaod1fec812012-02-13 09:51:10 -080075
76 uint32_t method_idx_;
77 uint32_t access_flags_;
78
79 llvm::Module* module_;
80 llvm::LLVMContext* context_;
81 IRBuilder& irb_;
82 llvm::Function* func_;
83
Logan Chienc670a8d2011-12-20 21:25:56 +080084 std::vector<DalvikReg*> regs_;
85 UniquePtr<DalvikReg> retval_reg_;
Logan Chiend6c239a2011-12-23 15:11:45 +080086
Logan Chienc670a8d2011-12-20 21:25:56 +080087 llvm::BasicBlock* basic_block_reg_alloca_;
88 llvm::BasicBlock* basic_block_reg_zero_init_;
Logan Chiend6ececa2011-12-27 16:20:15 +080089 llvm::BasicBlock* basic_block_reg_arg_init_;
Logan Chiend6c239a2011-12-23 15:11:45 +080090 std::vector<llvm::BasicBlock*> basic_blocks_;
91
Logan Chien5bcc04e2012-01-30 14:15:12 +080092 std::vector<llvm::BasicBlock*> basic_block_landing_pads_;
93 llvm::BasicBlock* basic_block_unwind_;
94 llvm::BasicBlock* basic_block_unreachable_;
95
Logan Chiend6c239a2011-12-23 15:11:45 +080096
Shih-wei Liaod1fec812012-02-13 09:51:10 -080097 public:
Logan Chien83426162011-12-09 09:29:50 +080098 MethodCompiler(InstructionSet insn_set,
99 Compiler* compiler,
100 ClassLinker* class_linker,
101 ClassLoader const* class_loader,
102 DexFile const* dex_file,
103 DexCache* dex_cache,
104 DexFile::CodeItem const* code_item,
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800105 uint32_t method_idx,
106 uint32_t access_flags);
107
108 ~MethodCompiler();
109
Logan Chien83426162011-12-09 09:29:50 +0800110 CompiledMethod* Compile();
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800111
Logan Chienc670a8d2011-12-20 21:25:56 +0800112
113 // Code generation helper function
114
115 IRBuilder& GetIRBuilder() const {
116 return irb_;
117 }
118
119
120 // Register helper function
121
122 llvm::Value* AllocDalvikLocalVarReg(RegCategory cat, uint32_t reg_idx);
123
124 llvm::Value* AllocDalvikRetValReg(RegCategory cat);
125
126
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800127 private:
128 void CreateFunction();
129
Logan Chienc670a8d2011-12-20 21:25:56 +0800130
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800131 void EmitPrologue();
Logan Chienc670a8d2011-12-20 21:25:56 +0800132 void EmitPrologueLastBranch();
Logan Chiend6ececa2011-12-27 16:20:15 +0800133 void EmitPrologueAssignArgRegister();
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800134 void EmitInstructions();
Logan Chien83426162011-12-09 09:29:50 +0800135 void EmitInstruction(uint32_t dex_pc, Instruction const* insn);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800136
Logan Chien0b827102011-12-20 19:46:14 +0800137
138 // Code generation helper function
139
140 llvm::Value* EmitLoadMethodObjectAddr();
141
142 llvm::FunctionType* GetFunctionType(uint32_t method_idx, bool is_static);
143
Logan Chien5bcc04e2012-01-30 14:15:12 +0800144 void EmitGuard_ExceptionLandingPad(uint32_t dex_pc);
145
146 void EmitBranchExceptionLandingPad(uint32_t dex_pc);
147
Logan Chiend6c239a2011-12-23 15:11:45 +0800148
149 // Basic block helper functions
150 llvm::BasicBlock* GetBasicBlock(uint32_t dex_pc);
151
152 llvm::BasicBlock* GetNextBasicBlock(uint32_t dex_pc);
153
154 llvm::BasicBlock* CreateBasicBlockWithDexPC(uint32_t dex_pc,
155 char const* postfix = NULL);
156
Logan Chien5bcc04e2012-01-30 14:15:12 +0800157 int32_t GetTryItemOffset(uint32_t dex_pc);
158
159 llvm::BasicBlock* GetLandingPadBasicBlock(uint32_t dex_pc);
160
161 llvm::BasicBlock* GetUnwindBasicBlock();
162
Logan Chienc670a8d2011-12-20 21:25:56 +0800163
164 // Register helper function
165
166 llvm::Value* EmitLoadDalvikReg(uint32_t reg_idx, JType jty,
167 JTypeSpace space) {
168 return regs_[reg_idx]->GetValue(jty, space);
169 }
170
171 llvm::Value* EmitLoadDalvikReg(uint32_t reg_idx, char shorty,
172 JTypeSpace space) {
173 return EmitLoadDalvikReg(reg_idx, GetJTypeFromShorty(shorty), space);
174 }
175
176 void EmitStoreDalvikReg(uint32_t reg_idx, JType jty,
177 JTypeSpace space, llvm::Value* new_value) {
178 regs_[reg_idx]->SetValue(jty, space, new_value);
179 }
180
181 void EmitStoreDalvikReg(uint32_t reg_idx, char shorty,
182 JTypeSpace space, llvm::Value* new_value) {
183 EmitStoreDalvikReg(reg_idx, GetJTypeFromShorty(shorty), space, new_value);
184 }
185
186 llvm::Value* EmitLoadDalvikRetValReg(JType jty, JTypeSpace space) {
187 return retval_reg_->GetValue(jty, space);
188 }
189
190 llvm::Value* EmitLoadDalvikRetValReg(char shorty, JTypeSpace space) {
191 return EmitLoadDalvikRetValReg(GetJTypeFromShorty(shorty), space);
192 }
193
194 void EmitStoreDalvikRetValReg(JType jty, JTypeSpace space,
195 llvm::Value* new_value) {
196 retval_reg_->SetValue(jty, space, new_value);
197 }
198
199 void EmitStoreDalvikRetValReg(char shorty, JTypeSpace space,
200 llvm::Value* new_value) {
201 EmitStoreDalvikRetValReg(GetJTypeFromShorty(shorty), space, new_value);
202 }
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800203};
204
205
206} // namespace compiler_llvm
207} // namespace art
208
209#endif // ART_SRC_COMPILER_LLVM_METHOD_COMPILER_H_