blob: 783465796352f02bf8efdd9a90a72a9f0908bed9 [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 Chiend6c239a2011-12-23 15:11:45 +080089 std::vector<llvm::BasicBlock*> basic_blocks_;
90
91
Shih-wei Liaod1fec812012-02-13 09:51:10 -080092 public:
Logan Chien83426162011-12-09 09:29:50 +080093 MethodCompiler(InstructionSet insn_set,
94 Compiler* compiler,
95 ClassLinker* class_linker,
96 ClassLoader const* class_loader,
97 DexFile const* dex_file,
98 DexCache* dex_cache,
99 DexFile::CodeItem const* code_item,
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800100 uint32_t method_idx,
101 uint32_t access_flags);
102
103 ~MethodCompiler();
104
Logan Chien83426162011-12-09 09:29:50 +0800105 CompiledMethod* Compile();
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800106
Logan Chienc670a8d2011-12-20 21:25:56 +0800107
108 // Code generation helper function
109
110 IRBuilder& GetIRBuilder() const {
111 return irb_;
112 }
113
114
115 // Register helper function
116
117 llvm::Value* AllocDalvikLocalVarReg(RegCategory cat, uint32_t reg_idx);
118
119 llvm::Value* AllocDalvikRetValReg(RegCategory cat);
120
121
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800122 private:
123 void CreateFunction();
124
Logan Chienc670a8d2011-12-20 21:25:56 +0800125
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800126 void EmitPrologue();
Logan Chienc670a8d2011-12-20 21:25:56 +0800127 void EmitPrologueLastBranch();
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800128 void EmitInstructions();
Logan Chien83426162011-12-09 09:29:50 +0800129 void EmitInstruction(uint32_t dex_pc, Instruction const* insn);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800130
Logan Chien0b827102011-12-20 19:46:14 +0800131
132 // Code generation helper function
133
134 llvm::Value* EmitLoadMethodObjectAddr();
135
136 llvm::FunctionType* GetFunctionType(uint32_t method_idx, bool is_static);
137
Logan Chiend6c239a2011-12-23 15:11:45 +0800138
139 // Basic block helper functions
140 llvm::BasicBlock* GetBasicBlock(uint32_t dex_pc);
141
142 llvm::BasicBlock* GetNextBasicBlock(uint32_t dex_pc);
143
144 llvm::BasicBlock* CreateBasicBlockWithDexPC(uint32_t dex_pc,
145 char const* postfix = NULL);
146
Logan Chienc670a8d2011-12-20 21:25:56 +0800147
148 // Register helper function
149
150 llvm::Value* EmitLoadDalvikReg(uint32_t reg_idx, JType jty,
151 JTypeSpace space) {
152 return regs_[reg_idx]->GetValue(jty, space);
153 }
154
155 llvm::Value* EmitLoadDalvikReg(uint32_t reg_idx, char shorty,
156 JTypeSpace space) {
157 return EmitLoadDalvikReg(reg_idx, GetJTypeFromShorty(shorty), space);
158 }
159
160 void EmitStoreDalvikReg(uint32_t reg_idx, JType jty,
161 JTypeSpace space, llvm::Value* new_value) {
162 regs_[reg_idx]->SetValue(jty, space, new_value);
163 }
164
165 void EmitStoreDalvikReg(uint32_t reg_idx, char shorty,
166 JTypeSpace space, llvm::Value* new_value) {
167 EmitStoreDalvikReg(reg_idx, GetJTypeFromShorty(shorty), space, new_value);
168 }
169
170 llvm::Value* EmitLoadDalvikRetValReg(JType jty, JTypeSpace space) {
171 return retval_reg_->GetValue(jty, space);
172 }
173
174 llvm::Value* EmitLoadDalvikRetValReg(char shorty, JTypeSpace space) {
175 return EmitLoadDalvikRetValReg(GetJTypeFromShorty(shorty), space);
176 }
177
178 void EmitStoreDalvikRetValReg(JType jty, JTypeSpace space,
179 llvm::Value* new_value) {
180 retval_reg_->SetValue(jty, space, new_value);
181 }
182
183 void EmitStoreDalvikRetValReg(char shorty, JTypeSpace space,
184 llvm::Value* new_value) {
185 EmitStoreDalvikRetValReg(GetJTypeFromShorty(shorty), space, new_value);
186 }
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800187};
188
189
190} // namespace compiler_llvm
191} // namespace art
192
193#endif // ART_SRC_COMPILER_LLVM_METHOD_COMPILER_H_