blob: 8a1bbc466c3afac65ad9c3b869d71f5eb1c070c6 [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"
TDYa127d668a062012-04-13 12:36:57 -070021#include "runtime_support_builder.h"
Logan Chien42e0e152012-01-13 15:42:36 +080022#include "runtime_support_func.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080023
24#include <llvm/Constants.h>
25#include <llvm/DerivedTypes.h>
26#include <llvm/Support/IRBuilder.h>
27#include <llvm/Type.h>
28
29#include <stdint.h>
30
31
32namespace art {
33namespace compiler_llvm {
34
35
36typedef llvm::IRBuilder<> LLVMIRBuilder;
37// NOTE: Here we define our own LLVMIRBuilder type alias, so that we can
38// switch "preserveNames" template parameter easily.
39
40
41class IRBuilder : public LLVMIRBuilder {
42 public:
43 //--------------------------------------------------------------------------
44 // General
45 //--------------------------------------------------------------------------
46
47 IRBuilder(llvm::LLVMContext& context, llvm::Module& module);
48
49
50 //--------------------------------------------------------------------------
Shih-wei Liao4c1f4252012-02-13 09:57:20 -080051 // Pointer Arithmetic Helper Function
52 //--------------------------------------------------------------------------
53
54 llvm::IntegerType* getPtrEquivIntTy() {
55 return getInt32Ty();
56 }
57
58 size_t getSizeOfPtrEquivInt() {
59 return 4;
60 }
61
62 llvm::ConstantInt* getSizeOfPtrEquivIntValue() {
63 return getPtrEquivInt(getSizeOfPtrEquivInt());
64 }
65
TDYa127ee1f59b2012-04-25 00:56:40 -070066 llvm::ConstantInt* getPtrEquivInt(int64_t i) {
Shih-wei Liao4c1f4252012-02-13 09:57:20 -080067 return llvm::ConstantInt::get(getPtrEquivIntTy(), i);
68 }
69
70 llvm::Value* CreatePtrDisp(llvm::Value* base,
71 llvm::Value* offset,
72 llvm::PointerType* ret_ty) {
73
74 llvm::Value* base_int = CreatePtrToInt(base, getPtrEquivIntTy());
75 llvm::Value* result_int = CreateAdd(base_int, offset);
76 llvm::Value* result = CreateIntToPtr(result_int, ret_ty);
77
78 return result;
79 }
80
81 llvm::Value* CreatePtrDisp(llvm::Value* base,
82 llvm::Value* bs,
83 llvm::Value* count,
84 llvm::Value* offset,
85 llvm::PointerType* ret_ty) {
86
87 llvm::Value* block_offset = CreateMul(bs, count);
88 llvm::Value* total_offset = CreateAdd(block_offset, offset);
89
90 return CreatePtrDisp(base, total_offset, ret_ty);
91 }
92
TDYa127ee1f59b2012-04-25 00:56:40 -070093 llvm::Value* LoadFromObjectOffset(llvm::Value* object_addr, int64_t offset, llvm::Type* type) {
TDYa1275bb86012012-04-11 05:57:28 -070094 // Convert offset to llvm::value
95 llvm::Value* llvm_offset = getPtrEquivInt(offset);
96 // Calculate the value's address
97 llvm::Value* value_addr = CreatePtrDisp(object_addr, llvm_offset, type->getPointerTo());
98 // Load
99 return CreateLoad(value_addr);
100 }
101
TDYa127ee1f59b2012-04-25 00:56:40 -0700102 void StoreToObjectOffset(llvm::Value* object_addr, int64_t offset, llvm::Value* new_value) {
TDYa1275bb86012012-04-11 05:57:28 -0700103 // Convert offset to llvm::value
104 llvm::Value* llvm_offset = getPtrEquivInt(offset);
105 // Calculate the value's address
106 llvm::Value* value_addr = CreatePtrDisp(object_addr,
107 llvm_offset,
108 new_value->getType()->getPointerTo());
109 // Store
110 CreateStore(new_value, value_addr);
111 }
112
Shih-wei Liao4c1f4252012-02-13 09:57:20 -0800113
114 //--------------------------------------------------------------------------
Logan Chien42e0e152012-01-13 15:42:36 +0800115 // Runtime Helper Function
116 //--------------------------------------------------------------------------
117
TDYa127d668a062012-04-13 12:36:57 -0700118 llvm::Function* GetRuntime(runtime_support::RuntimeId rt) {
119 return runtime_support_->GetRuntimeSupportFunction(rt);
120 }
121
122 void SetRuntimeSupport(RuntimeSupportBuilder* runtime_support) {
123 // Can only set once. We can't do this on constructor, because RuntimeSupportBuilder needs
124 // IRBuilder.
125 if (runtime_support_ == NULL && runtime_support != NULL) {
126 runtime_support_ = runtime_support;
127 }
128 }
Logan Chien42e0e152012-01-13 15:42:36 +0800129
130
131 //--------------------------------------------------------------------------
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800132 // Type Helper Function
133 //--------------------------------------------------------------------------
134
135 llvm::Type* getJType(char shorty_jty, JTypeSpace space) {
136 return getJType(GetJTypeFromShorty(shorty_jty), space);
137 }
138
139 llvm::Type* getJType(JType jty, JTypeSpace space) {
140 switch (space) {
141 case kAccurate:
142 return getJTypeInAccurateSpace(jty);
143
144 case kReg:
145 case kField: // Currently field space is equivalent to register space.
146 return getJTypeInRegSpace(jty);
147
148 case kArray:
149 return getJTypeInArraySpace(jty);
150 }
151
Logan Chien83426162011-12-09 09:29:50 +0800152 LOG(FATAL) << "Unknown type space: " << space;
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800153 return NULL;
154 }
155
156 llvm::Type* getJVoidTy() {
157 return getVoidTy();
158 }
159
160 llvm::IntegerType* getJBooleanTy() {
161 return getInt1Ty();
162 }
163
164 llvm::IntegerType* getJByteTy() {
165 return getInt8Ty();
166 }
167
168 llvm::IntegerType* getJCharTy() {
169 return getInt16Ty();
170 }
171
172 llvm::IntegerType* getJShortTy() {
173 return getInt16Ty();
174 }
175
176 llvm::IntegerType* getJIntTy() {
177 return getInt32Ty();
178 }
179
180 llvm::IntegerType* getJLongTy() {
181 return getInt64Ty();
182 }
183
184 llvm::Type* getJFloatTy() {
185 return getFloatTy();
186 }
187
188 llvm::Type* getJDoubleTy() {
189 return getDoubleTy();
190 }
191
192 llvm::PointerType* getJObjectTy() {
193 return jobject_type_;
194 }
195
Logan Chienf04364f2012-02-10 12:01:39 +0800196 llvm::PointerType* getJEnvTy() {
197 return jenv_type_;
198 }
199
200 llvm::Type* getJValueTy() {
201 // NOTE: JValue is an union type, which may contains boolean, byte, char,
202 // short, int, long, float, double, Object. However, LLVM itself does
203 // not support union type, so we have to return a type with biggest size,
204 // then bitcast it before we use it.
205 return getJLongTy();
206 }
207
Logan Chien8dfcbea2012-02-17 18:50:32 +0800208 llvm::StructType* getShadowFrameTy(uint32_t sirt_size);
209
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800210
211 //--------------------------------------------------------------------------
212 // Constant Value Helper Function
213 //--------------------------------------------------------------------------
214
215 llvm::ConstantInt* getJBoolean(bool is_true) {
216 return (is_true) ? getTrue() : getFalse();
217 }
218
219 llvm::ConstantInt* getJByte(int8_t i) {
220 return llvm::ConstantInt::getSigned(getJByteTy(), i);
221 }
222
223 llvm::ConstantInt* getJChar(int16_t i) {
224 return llvm::ConstantInt::getSigned(getJCharTy(), i);
225 }
226
227 llvm::ConstantInt* getJShort(int16_t i) {
228 return llvm::ConstantInt::getSigned(getJShortTy(), i);
229 }
230
231 llvm::ConstantInt* getJInt(int32_t i) {
232 return llvm::ConstantInt::getSigned(getJIntTy(), i);
233 }
234
235 llvm::ConstantInt* getJLong(int64_t i) {
236 return llvm::ConstantInt::getSigned(getJLongTy(), i);
237 }
238
239 llvm::Constant* getJFloat(float f) {
240 return llvm::ConstantFP::get(getJFloatTy(), f);
241 }
242
243 llvm::Constant* getJDouble(double d) {
244 return llvm::ConstantFP::get(getJDoubleTy(), d);
245 }
246
247 llvm::ConstantPointerNull* getJNull() {
248 return llvm::ConstantPointerNull::get(getJObjectTy());
249 }
250
251 llvm::Constant* getJZero(char shorty_jty) {
252 return getJZero(GetJTypeFromShorty(shorty_jty));
253 }
254
255 llvm::Constant* getJZero(JType jty) {
256 switch (jty) {
257 case kVoid:
Logan Chien83426162011-12-09 09:29:50 +0800258 LOG(FATAL) << "Zero is not a value of void type";
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800259 return NULL;
260
261 case kBoolean:
262 return getJBoolean(false);
263
264 case kByte:
265 return getJByte(0);
266
267 case kChar:
268 return getJChar(0);
269
270 case kShort:
271 return getJShort(0);
272
273 case kInt:
274 return getJInt(0);
275
276 case kLong:
277 return getJLong(0);
278
279 case kFloat:
280 return getJFloat(0.0f);
281
282 case kDouble:
283 return getJDouble(0.0);
284
285 case kObject:
286 return getJNull();
287 }
288
289 LOG(FATAL) << "Unknown java type: " << jty;
290 return NULL;
291 }
292
293
294 private:
295 //--------------------------------------------------------------------------
296 // Type Helper Function (Private)
297 //--------------------------------------------------------------------------
298
299 llvm::Type* getJTypeInAccurateSpace(JType jty);
300 llvm::Type* getJTypeInRegSpace(JType jty);
301 llvm::Type* getJTypeInArraySpace(JType jty);
302
303
304 private:
Logan Chien6a917992012-02-17 18:43:48 +0800305 llvm::Module* module_;
306
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800307 llvm::PointerType* jobject_type_;
308
Logan Chienf04364f2012-02-10 12:01:39 +0800309 llvm::PointerType* jenv_type_;
310
Logan Chien8dfcbea2012-02-17 18:50:32 +0800311 llvm::StructType* art_frame_type_;
312
TDYa127d668a062012-04-13 12:36:57 -0700313 RuntimeSupportBuilder* runtime_support_;
Logan Chien42e0e152012-01-13 15:42:36 +0800314
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800315};
316
317
318} // namespace compiler_llvm
319} // namespace art
320
321#endif // ART_SRC_COMPILER_LLVM_IR_BUILDER_H_