blob: b38c7b0099663d32c637d4d2ccb1e604fd3f1b0e [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_IR_BUILDER_H_
18#define ART_SRC_COMPILER_LLVM_IR_BUILDER_H_
19
20#include "backend_types.h"
Logan Chien42e0e152012-01-13 15:42:36 +080021#include "runtime_support_func.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080022
23#include <llvm/Constants.h>
24#include <llvm/DerivedTypes.h>
25#include <llvm/Support/IRBuilder.h>
26#include <llvm/Type.h>
27
28#include <stdint.h>
29
30
31namespace art {
32namespace compiler_llvm {
33
34
35typedef llvm::IRBuilder<> LLVMIRBuilder;
36// NOTE: Here we define our own LLVMIRBuilder type alias, so that we can
37// switch "preserveNames" template parameter easily.
38
39
40class IRBuilder : public LLVMIRBuilder {
41 public:
42 //--------------------------------------------------------------------------
43 // General
44 //--------------------------------------------------------------------------
45
46 IRBuilder(llvm::LLVMContext& context, llvm::Module& module);
47
48
49 //--------------------------------------------------------------------------
Shih-wei Liao4c1f4252012-02-13 09:57:20 -080050 // Pointer Arithmetic Helper Function
51 //--------------------------------------------------------------------------
52
53 llvm::IntegerType* getPtrEquivIntTy() {
54 return getInt32Ty();
55 }
56
57 size_t getSizeOfPtrEquivInt() {
58 return 4;
59 }
60
61 llvm::ConstantInt* getSizeOfPtrEquivIntValue() {
62 return getPtrEquivInt(getSizeOfPtrEquivInt());
63 }
64
65 llvm::ConstantInt* getPtrEquivInt(uint64_t i) {
66 return llvm::ConstantInt::get(getPtrEquivIntTy(), i);
67 }
68
69 llvm::Value* CreatePtrDisp(llvm::Value* base,
70 llvm::Value* offset,
71 llvm::PointerType* ret_ty) {
72
73 llvm::Value* base_int = CreatePtrToInt(base, getPtrEquivIntTy());
74 llvm::Value* result_int = CreateAdd(base_int, offset);
75 llvm::Value* result = CreateIntToPtr(result_int, ret_ty);
76
77 return result;
78 }
79
80 llvm::Value* CreatePtrDisp(llvm::Value* base,
81 llvm::Value* bs,
82 llvm::Value* count,
83 llvm::Value* offset,
84 llvm::PointerType* ret_ty) {
85
86 llvm::Value* block_offset = CreateMul(bs, count);
87 llvm::Value* total_offset = CreateAdd(block_offset, offset);
88
89 return CreatePtrDisp(base, total_offset, ret_ty);
90 }
91
92
93 //--------------------------------------------------------------------------
Logan Chien42e0e152012-01-13 15:42:36 +080094 // Runtime Helper Function
95 //--------------------------------------------------------------------------
96
97 llvm::Function* GetRuntime(runtime_support::RuntimeId rt) const;
98
99
100 //--------------------------------------------------------------------------
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800101 // Type Helper Function
102 //--------------------------------------------------------------------------
103
104 llvm::Type* getJType(char shorty_jty, JTypeSpace space) {
105 return getJType(GetJTypeFromShorty(shorty_jty), space);
106 }
107
108 llvm::Type* getJType(JType jty, JTypeSpace space) {
109 switch (space) {
110 case kAccurate:
111 return getJTypeInAccurateSpace(jty);
112
113 case kReg:
114 case kField: // Currently field space is equivalent to register space.
115 return getJTypeInRegSpace(jty);
116
117 case kArray:
118 return getJTypeInArraySpace(jty);
119 }
120
Logan Chien83426162011-12-09 09:29:50 +0800121 LOG(FATAL) << "Unknown type space: " << space;
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800122 return NULL;
123 }
124
125 llvm::Type* getJVoidTy() {
126 return getVoidTy();
127 }
128
129 llvm::IntegerType* getJBooleanTy() {
130 return getInt1Ty();
131 }
132
133 llvm::IntegerType* getJByteTy() {
134 return getInt8Ty();
135 }
136
137 llvm::IntegerType* getJCharTy() {
138 return getInt16Ty();
139 }
140
141 llvm::IntegerType* getJShortTy() {
142 return getInt16Ty();
143 }
144
145 llvm::IntegerType* getJIntTy() {
146 return getInt32Ty();
147 }
148
149 llvm::IntegerType* getJLongTy() {
150 return getInt64Ty();
151 }
152
153 llvm::Type* getJFloatTy() {
154 return getFloatTy();
155 }
156
157 llvm::Type* getJDoubleTy() {
158 return getDoubleTy();
159 }
160
161 llvm::PointerType* getJObjectTy() {
162 return jobject_type_;
163 }
164
Logan Chienf04364f2012-02-10 12:01:39 +0800165 llvm::PointerType* getJEnvTy() {
166 return jenv_type_;
167 }
168
169 llvm::Type* getJValueTy() {
170 // NOTE: JValue is an union type, which may contains boolean, byte, char,
171 // short, int, long, float, double, Object. However, LLVM itself does
172 // not support union type, so we have to return a type with biggest size,
173 // then bitcast it before we use it.
174 return getJLongTy();
175 }
176
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800177
178 //--------------------------------------------------------------------------
179 // Constant Value Helper Function
180 //--------------------------------------------------------------------------
181
182 llvm::ConstantInt* getJBoolean(bool is_true) {
183 return (is_true) ? getTrue() : getFalse();
184 }
185
186 llvm::ConstantInt* getJByte(int8_t i) {
187 return llvm::ConstantInt::getSigned(getJByteTy(), i);
188 }
189
190 llvm::ConstantInt* getJChar(int16_t i) {
191 return llvm::ConstantInt::getSigned(getJCharTy(), i);
192 }
193
194 llvm::ConstantInt* getJShort(int16_t i) {
195 return llvm::ConstantInt::getSigned(getJShortTy(), i);
196 }
197
198 llvm::ConstantInt* getJInt(int32_t i) {
199 return llvm::ConstantInt::getSigned(getJIntTy(), i);
200 }
201
202 llvm::ConstantInt* getJLong(int64_t i) {
203 return llvm::ConstantInt::getSigned(getJLongTy(), i);
204 }
205
206 llvm::Constant* getJFloat(float f) {
207 return llvm::ConstantFP::get(getJFloatTy(), f);
208 }
209
210 llvm::Constant* getJDouble(double d) {
211 return llvm::ConstantFP::get(getJDoubleTy(), d);
212 }
213
214 llvm::ConstantPointerNull* getJNull() {
215 return llvm::ConstantPointerNull::get(getJObjectTy());
216 }
217
218 llvm::Constant* getJZero(char shorty_jty) {
219 return getJZero(GetJTypeFromShorty(shorty_jty));
220 }
221
222 llvm::Constant* getJZero(JType jty) {
223 switch (jty) {
224 case kVoid:
Logan Chien83426162011-12-09 09:29:50 +0800225 LOG(FATAL) << "Zero is not a value of void type";
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800226 return NULL;
227
228 case kBoolean:
229 return getJBoolean(false);
230
231 case kByte:
232 return getJByte(0);
233
234 case kChar:
235 return getJChar(0);
236
237 case kShort:
238 return getJShort(0);
239
240 case kInt:
241 return getJInt(0);
242
243 case kLong:
244 return getJLong(0);
245
246 case kFloat:
247 return getJFloat(0.0f);
248
249 case kDouble:
250 return getJDouble(0.0);
251
252 case kObject:
253 return getJNull();
254 }
255
256 LOG(FATAL) << "Unknown java type: " << jty;
257 return NULL;
258 }
259
260
261 private:
262 //--------------------------------------------------------------------------
Logan Chien42e0e152012-01-13 15:42:36 +0800263 // Runtime Helper Function (Private)
264 //--------------------------------------------------------------------------
265
266 void InitRuntimeSupportFuncDecl(llvm::Module& module);
267
268
269 //--------------------------------------------------------------------------
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800270 // Type Helper Function (Private)
271 //--------------------------------------------------------------------------
272
273 llvm::Type* getJTypeInAccurateSpace(JType jty);
274 llvm::Type* getJTypeInRegSpace(JType jty);
275 llvm::Type* getJTypeInArraySpace(JType jty);
276
277
278 private:
279 llvm::PointerType* jobject_type_;
280
Logan Chienf04364f2012-02-10 12:01:39 +0800281 llvm::PointerType* jenv_type_;
282
Logan Chien42e0e152012-01-13 15:42:36 +0800283 llvm::Function* runtime_support_func_decls_[runtime_support::MAX_ID];
284
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800285};
286
287
288} // namespace compiler_llvm
289} // namespace art
290
291#endif // ART_SRC_COMPILER_LLVM_IR_BUILDER_H_