blob: 03ca2d7be4441c400917feb08b572ea2c9011d96 [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"
21
22#include <llvm/Constants.h>
23#include <llvm/DerivedTypes.h>
24#include <llvm/Support/IRBuilder.h>
25#include <llvm/Type.h>
26
27#include <stdint.h>
28
29
30namespace art {
31namespace compiler_llvm {
32
33
34typedef llvm::IRBuilder<> LLVMIRBuilder;
35// NOTE: Here we define our own LLVMIRBuilder type alias, so that we can
36// switch "preserveNames" template parameter easily.
37
38
39class IRBuilder : public LLVMIRBuilder {
40 public:
41 //--------------------------------------------------------------------------
42 // General
43 //--------------------------------------------------------------------------
44
45 IRBuilder(llvm::LLVMContext& context, llvm::Module& module);
46
47
48 //--------------------------------------------------------------------------
49 // Type Helper Function
50 //--------------------------------------------------------------------------
51
52 llvm::Type* getJType(char shorty_jty, JTypeSpace space) {
53 return getJType(GetJTypeFromShorty(shorty_jty), space);
54 }
55
56 llvm::Type* getJType(JType jty, JTypeSpace space) {
57 switch (space) {
58 case kAccurate:
59 return getJTypeInAccurateSpace(jty);
60
61 case kReg:
62 case kField: // Currently field space is equivalent to register space.
63 return getJTypeInRegSpace(jty);
64
65 case kArray:
66 return getJTypeInArraySpace(jty);
67 }
68
69 return NULL;
70 }
71
72 llvm::Type* getJVoidTy() {
73 return getVoidTy();
74 }
75
76 llvm::IntegerType* getJBooleanTy() {
77 return getInt1Ty();
78 }
79
80 llvm::IntegerType* getJByteTy() {
81 return getInt8Ty();
82 }
83
84 llvm::IntegerType* getJCharTy() {
85 return getInt16Ty();
86 }
87
88 llvm::IntegerType* getJShortTy() {
89 return getInt16Ty();
90 }
91
92 llvm::IntegerType* getJIntTy() {
93 return getInt32Ty();
94 }
95
96 llvm::IntegerType* getJLongTy() {
97 return getInt64Ty();
98 }
99
100 llvm::Type* getJFloatTy() {
101 return getFloatTy();
102 }
103
104 llvm::Type* getJDoubleTy() {
105 return getDoubleTy();
106 }
107
108 llvm::PointerType* getJObjectTy() {
109 return jobject_type_;
110 }
111
112
113 //--------------------------------------------------------------------------
114 // Constant Value Helper Function
115 //--------------------------------------------------------------------------
116
117 llvm::ConstantInt* getJBoolean(bool is_true) {
118 return (is_true) ? getTrue() : getFalse();
119 }
120
121 llvm::ConstantInt* getJByte(int8_t i) {
122 return llvm::ConstantInt::getSigned(getJByteTy(), i);
123 }
124
125 llvm::ConstantInt* getJChar(int16_t i) {
126 return llvm::ConstantInt::getSigned(getJCharTy(), i);
127 }
128
129 llvm::ConstantInt* getJShort(int16_t i) {
130 return llvm::ConstantInt::getSigned(getJShortTy(), i);
131 }
132
133 llvm::ConstantInt* getJInt(int32_t i) {
134 return llvm::ConstantInt::getSigned(getJIntTy(), i);
135 }
136
137 llvm::ConstantInt* getJLong(int64_t i) {
138 return llvm::ConstantInt::getSigned(getJLongTy(), i);
139 }
140
141 llvm::Constant* getJFloat(float f) {
142 return llvm::ConstantFP::get(getJFloatTy(), f);
143 }
144
145 llvm::Constant* getJDouble(double d) {
146 return llvm::ConstantFP::get(getJDoubleTy(), d);
147 }
148
149 llvm::ConstantPointerNull* getJNull() {
150 return llvm::ConstantPointerNull::get(getJObjectTy());
151 }
152
153 llvm::Constant* getJZero(char shorty_jty) {
154 return getJZero(GetJTypeFromShorty(shorty_jty));
155 }
156
157 llvm::Constant* getJZero(JType jty) {
158 switch (jty) {
159 case kVoid:
160 return NULL;
161
162 case kBoolean:
163 return getJBoolean(false);
164
165 case kByte:
166 return getJByte(0);
167
168 case kChar:
169 return getJChar(0);
170
171 case kShort:
172 return getJShort(0);
173
174 case kInt:
175 return getJInt(0);
176
177 case kLong:
178 return getJLong(0);
179
180 case kFloat:
181 return getJFloat(0.0f);
182
183 case kDouble:
184 return getJDouble(0.0);
185
186 case kObject:
187 return getJNull();
188 }
189
190 LOG(FATAL) << "Unknown java type: " << jty;
191 return NULL;
192 }
193
194
195 private:
196 //--------------------------------------------------------------------------
197 // Type Helper Function (Private)
198 //--------------------------------------------------------------------------
199
200 llvm::Type* getJTypeInAccurateSpace(JType jty);
201 llvm::Type* getJTypeInRegSpace(JType jty);
202 llvm::Type* getJTypeInArraySpace(JType jty);
203
204
205 private:
206 llvm::PointerType* jobject_type_;
207
208};
209
210
211} // namespace compiler_llvm
212} // namespace art
213
214#endif // ART_SRC_COMPILER_LLVM_IR_BUILDER_H_