Shih-wei Liao | e94d9b2 | 2012-05-22 09:01:24 -0700 | [diff] [blame] | 1 | /* |
| 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_GREENLAND_IR_BUILDER_H_ |
| 18 | #define ART_SRC_GREENLAND_IR_BUILDER_H_ |
| 19 | |
| 20 | #include "backend_types.h" |
| 21 | #include "intrinsic_helper.h" |
| 22 | |
| 23 | #include "logging.h" |
| 24 | |
| 25 | #include <llvm/Support/IRBuilder.h> |
buzbee | 2cfc639 | 2012-05-07 14:51:40 -0700 | [diff] [blame] | 26 | #include <llvm/Support/NoFolder.h> |
| 27 | #include <llvm/Metadata.h> |
Shih-wei Liao | e94d9b2 | 2012-05-22 09:01:24 -0700 | [diff] [blame] | 28 | |
| 29 | namespace llvm { |
| 30 | class Module; |
| 31 | } |
| 32 | |
| 33 | namespace art { |
| 34 | namespace greenland { |
| 35 | |
buzbee | 2cfc639 | 2012-05-07 14:51:40 -0700 | [diff] [blame] | 36 | class InserterWithDexOffset |
| 37 | : public llvm::IRBuilderDefaultInserter<true> { |
| 38 | public: |
| 39 | InserterWithDexOffset() : node_(NULL) {} |
| 40 | void InsertHelper(llvm::Instruction *I, const llvm::Twine &Name, |
| 41 | llvm::BasicBlock *BB, |
| 42 | llvm::BasicBlock::iterator InsertPt) const { |
| 43 | llvm::IRBuilderDefaultInserter<true>::InsertHelper(I, Name, BB, InsertPt); |
| 44 | if (node_ != NULL) { |
| 45 | I->setMetadata("DexOff", node_); |
| 46 | } |
| 47 | } |
| 48 | void SetDexOffset(llvm::MDNode* node) { |
| 49 | node_ = node; |
| 50 | } |
| 51 | private: |
| 52 | llvm::MDNode* node_; |
| 53 | }; |
| 54 | |
| 55 | typedef llvm::IRBuilder<true, llvm::NoFolder, InserterWithDexOffset> LLVMIRBuilder; |
Shih-wei Liao | e94d9b2 | 2012-05-22 09:01:24 -0700 | [diff] [blame] | 56 | |
| 57 | class IRBuilder : public LLVMIRBuilder { |
| 58 | public: |
| 59 | IRBuilder(llvm::LLVMContext& context, llvm::Module& module, |
| 60 | IntrinsicHelper& intrinsic_helper); |
| 61 | ~IRBuilder() { } |
| 62 | |
| 63 | public: |
| 64 | //-------------------------------------------------------------------------- |
| 65 | // Pointer Arithmetic Helper Function |
| 66 | //-------------------------------------------------------------------------- |
| 67 | llvm::IntegerType* GetPtrEquivIntTy() { |
| 68 | return getInt32Ty(); |
| 69 | } |
| 70 | |
| 71 | llvm::ConstantInt* GetPtrEquivInt(int64_t i) { |
| 72 | return llvm::ConstantInt::get(GetPtrEquivIntTy(), i); |
| 73 | } |
| 74 | |
| 75 | //-------------------------------------------------------------------------- |
| 76 | // Intrinsic Helper Functions |
| 77 | //-------------------------------------------------------------------------- |
| 78 | llvm::Function* GetIntrinsics(IntrinsicHelper::IntrinsicId instr_id) { |
| 79 | return intrinsic_helper_.GetIntrinsicFunction(instr_id); |
| 80 | } |
| 81 | |
| 82 | //-------------------------------------------------------------------------- |
| 83 | // Type Helper Functions |
| 84 | //-------------------------------------------------------------------------- |
| 85 | llvm::Type* GetJType(char shorty_jty, JTypeSpace space) { |
| 86 | return GetJType(GetJTypeFromShorty(shorty_jty), space); |
| 87 | } |
| 88 | |
| 89 | llvm::Type* GetJType(JType jty, JTypeSpace space) { |
| 90 | switch (space) { |
| 91 | case kAccurate: |
| 92 | return GetJTypeInAccurateSpace(jty); |
| 93 | |
| 94 | case kReg: |
| 95 | case kField: // Currently field space is equivalent to register space. |
| 96 | return GetJTypeInRegSpace(jty); |
| 97 | |
| 98 | case kArray: |
| 99 | return GetJTypeInArraySpace(jty); |
| 100 | } |
| 101 | |
| 102 | LOG(FATAL) << "Unknown type space: " << space; |
| 103 | return NULL; |
| 104 | } |
| 105 | |
| 106 | llvm::Type* GetJVoidTy() { |
| 107 | return getVoidTy(); |
| 108 | } |
| 109 | |
| 110 | llvm::IntegerType* GetJBooleanTy() { |
| 111 | return getInt1Ty(); |
| 112 | } |
| 113 | |
| 114 | llvm::IntegerType* GetJByteTy() { |
| 115 | return getInt8Ty(); |
| 116 | } |
| 117 | |
| 118 | llvm::IntegerType* GetJCharTy() { |
| 119 | return getInt16Ty(); |
| 120 | } |
| 121 | |
| 122 | llvm::IntegerType* GetJShortTy() { |
| 123 | return getInt16Ty(); |
| 124 | } |
| 125 | |
| 126 | llvm::IntegerType* GetJIntTy() { |
| 127 | return getInt32Ty(); |
| 128 | } |
| 129 | |
| 130 | llvm::IntegerType* GetJLongTy() { |
| 131 | return getInt64Ty(); |
| 132 | } |
| 133 | |
| 134 | llvm::Type* GetJFloatTy() { |
| 135 | return getFloatTy(); |
| 136 | } |
| 137 | |
| 138 | llvm::Type* GetJDoubleTy() { |
| 139 | return getDoubleTy(); |
| 140 | } |
| 141 | |
| 142 | llvm::PointerType* GetJObjectTy() { |
| 143 | return java_object_type_; |
| 144 | } |
| 145 | |
| 146 | llvm::PointerType* GetJMethodTy() { |
| 147 | return java_method_type_; |
| 148 | } |
| 149 | |
| 150 | llvm::PointerType* GetJThreadTy() { |
| 151 | return java_thread_type_; |
| 152 | } |
| 153 | |
| 154 | //-------------------------------------------------------------------------- |
| 155 | // Constant Value Helper Function |
| 156 | //-------------------------------------------------------------------------- |
| 157 | llvm::ConstantInt* GetJBoolean(bool is_true) { |
| 158 | return (is_true) ? getTrue() : getFalse(); |
| 159 | } |
| 160 | |
| 161 | llvm::ConstantInt* GetJByte(int8_t i) { |
| 162 | return llvm::ConstantInt::getSigned(GetJByteTy(), i); |
| 163 | } |
| 164 | |
| 165 | llvm::ConstantInt* GetJChar(int16_t i) { |
| 166 | return llvm::ConstantInt::getSigned(GetJCharTy(), i); |
| 167 | } |
| 168 | |
| 169 | llvm::ConstantInt* GetJShort(int16_t i) { |
| 170 | return llvm::ConstantInt::getSigned(GetJShortTy(), i); |
| 171 | } |
| 172 | |
| 173 | llvm::ConstantInt* GetJInt(int32_t i) { |
| 174 | return llvm::ConstantInt::getSigned(GetJIntTy(), i); |
| 175 | } |
| 176 | |
| 177 | llvm::ConstantInt* GetJLong(int64_t i) { |
| 178 | return llvm::ConstantInt::getSigned(GetJLongTy(), i); |
| 179 | } |
| 180 | |
| 181 | llvm::Constant* GetJFloat(float f) { |
| 182 | return llvm::ConstantFP::get(GetJFloatTy(), f); |
| 183 | } |
| 184 | |
| 185 | llvm::Constant* GetJDouble(double d) { |
| 186 | return llvm::ConstantFP::get(GetJDoubleTy(), d); |
| 187 | } |
| 188 | |
| 189 | llvm::ConstantPointerNull* GetJNull() { |
| 190 | return llvm::ConstantPointerNull::get(GetJObjectTy()); |
| 191 | } |
| 192 | |
| 193 | llvm::Constant* GetJZero(char shorty_jty) { |
| 194 | return GetJZero(GetJTypeFromShorty(shorty_jty)); |
| 195 | } |
| 196 | |
| 197 | llvm::Constant* GetJZero(JType jty) { |
| 198 | switch (jty) { |
| 199 | case kVoid: |
| 200 | LOG(FATAL) << "Zero is not a value of void type"; |
| 201 | return NULL; |
| 202 | |
| 203 | case kBoolean: |
| 204 | return GetJBoolean(false); |
| 205 | |
| 206 | case kByte: |
| 207 | return GetJByte(0); |
| 208 | |
| 209 | case kChar: |
| 210 | return GetJChar(0); |
| 211 | |
| 212 | case kShort: |
| 213 | return GetJShort(0); |
| 214 | |
| 215 | case kInt: |
| 216 | return GetJInt(0); |
| 217 | |
| 218 | case kLong: |
| 219 | return GetJLong(0); |
| 220 | |
| 221 | case kFloat: |
| 222 | return GetJFloat(0.0f); |
| 223 | |
| 224 | case kDouble: |
| 225 | return GetJDouble(0.0); |
| 226 | |
| 227 | case kObject: |
| 228 | return GetJNull(); |
| 229 | |
| 230 | default: |
| 231 | LOG(FATAL) << "Unknown java type: " << jty; |
| 232 | return NULL; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | private: |
| 237 | llvm::Type* GetJTypeInAccurateSpace(JType jty); |
| 238 | llvm::Type* GetJTypeInRegSpace(JType jty); |
| 239 | llvm::Type* GetJTypeInArraySpace(JType jty); |
| 240 | |
| 241 | llvm::PointerType* java_object_type_; |
| 242 | llvm::PointerType* java_method_type_; |
| 243 | llvm::PointerType* java_thread_type_; |
| 244 | |
| 245 | IntrinsicHelper& intrinsic_helper_; |
| 246 | }; |
| 247 | |
| 248 | } // namespace greenland |
| 249 | } // namespace art |
| 250 | |
| 251 | #endif // ART_SRC_GREENLAND_IR_BUILDER_H_ |