Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [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 | #include "ir_builder.h" |
Logan Chien | 42e0e15 | 2012-01-13 15:42:36 +0800 | [diff] [blame] | 18 | #include "runtime_support_func.h" |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 19 | #include "stringprintf.h" |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 20 | |
| 21 | #include <llvm/Module.h> |
| 22 | |
Logan Chien | 8342616 | 2011-12-09 09:29:50 +0800 | [diff] [blame] | 23 | namespace art { |
| 24 | namespace compiler_llvm { |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 25 | |
| 26 | |
| 27 | //---------------------------------------------------------------------------- |
| 28 | // General |
| 29 | //---------------------------------------------------------------------------- |
| 30 | |
| 31 | IRBuilder::IRBuilder(llvm::LLVMContext& context, llvm::Module& module) |
Logan Chien | 6a91799 | 2012-02-17 18:43:48 +0800 | [diff] [blame] | 32 | : LLVMIRBuilder(context), module_(&module) { |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 33 | |
| 34 | // Get java object type from module |
Logan Chien | 42e0e15 | 2012-01-13 15:42:36 +0800 | [diff] [blame] | 35 | llvm::Type* jobject_struct_type = module.getTypeByName("JavaObject"); |
Logan Chien | 1b0a1b7 | 2012-03-15 06:20:17 +0800 | [diff] [blame^] | 36 | CHECK(jobject_struct_type != NULL); |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 37 | jobject_type_ = jobject_struct_type->getPointerTo(); |
Logan Chien | 42e0e15 | 2012-01-13 15:42:36 +0800 | [diff] [blame] | 38 | |
Logan Chien | f04364f | 2012-02-10 12:01:39 +0800 | [diff] [blame] | 39 | // Create JEnv* type |
| 40 | llvm::Type* jenv_struct_type = llvm::StructType::create(context, "JEnv"); |
| 41 | jenv_type_ = jenv_struct_type->getPointerTo(); |
| 42 | |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 43 | // Get Art shadow frame struct type from module |
Logan Chien | 1b0a1b7 | 2012-03-15 06:20:17 +0800 | [diff] [blame^] | 44 | art_frame_type_ = module.getTypeByName("ShadowFrame"); |
| 45 | CHECK(art_frame_type_ != NULL); |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 46 | |
Logan Chien | 42e0e15 | 2012-01-13 15:42:36 +0800 | [diff] [blame] | 47 | // Load the runtime support function declaration from module |
Logan Chien | 6a91799 | 2012-02-17 18:43:48 +0800 | [diff] [blame] | 48 | InitRuntimeSupportFuncDecl(); |
Logan Chien | 42e0e15 | 2012-01-13 15:42:36 +0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | |
| 52 | //---------------------------------------------------------------------------- |
| 53 | // Runtime Helper Function |
| 54 | //---------------------------------------------------------------------------- |
| 55 | |
Logan Chien | 6a91799 | 2012-02-17 18:43:48 +0800 | [diff] [blame] | 56 | void IRBuilder::InitRuntimeSupportFuncDecl() { |
Logan Chien | 42e0e15 | 2012-01-13 15:42:36 +0800 | [diff] [blame] | 57 | using namespace runtime_support; |
| 58 | |
| 59 | #define GET_RUNTIME_SUPPORT_FUNC_DECL(ID, NAME) \ |
| 60 | do { \ |
Logan Chien | bae4c85 | 2012-03-06 16:53:31 +0800 | [diff] [blame] | 61 | llvm::Function* fn = module_->getFunction(#NAME); \ |
| 62 | DCHECK_NE(fn, (void*)NULL) << "Function not found: " << #NAME; \ |
Logan Chien | 42e0e15 | 2012-01-13 15:42:36 +0800 | [diff] [blame] | 63 | runtime_support_func_decls_[ID] = fn; \ |
| 64 | } while (0); |
| 65 | |
| 66 | #include "runtime_support_func_list.h" |
| 67 | RUNTIME_SUPPORT_FUNC_LIST(GET_RUNTIME_SUPPORT_FUNC_DECL) |
| 68 | #undef RUNTIME_SUPPORT_FUNC_LIST |
| 69 | #undef GET_RUNTIME_SUPPORT_FUNC_DECL |
| 70 | } |
| 71 | |
| 72 | |
| 73 | llvm::Function* IRBuilder::GetRuntime(runtime_support::RuntimeId rt) const { |
| 74 | using namespace runtime_support; |
| 75 | |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 76 | if (rt >= 0 && rt < MAX_ID) { |
Logan Chien | 42e0e15 | 2012-01-13 15:42:36 +0800 | [diff] [blame] | 77 | return runtime_support_func_decls_[rt]; |
| 78 | } else { |
| 79 | LOG(ERROR) << "Unknown runtime function id: " << rt; |
| 80 | return NULL; |
| 81 | } |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | |
| 85 | //---------------------------------------------------------------------------- |
| 86 | // Type Helper Function |
| 87 | //---------------------------------------------------------------------------- |
| 88 | |
| 89 | llvm::Type* IRBuilder::getJTypeInAccurateSpace(JType jty) { |
| 90 | switch (jty) { |
| 91 | case kVoid: |
| 92 | return getJVoidTy(); |
| 93 | |
| 94 | case kBoolean: |
| 95 | return getJBooleanTy(); |
| 96 | |
| 97 | case kByte: |
| 98 | return getJByteTy(); |
| 99 | |
| 100 | case kChar: |
| 101 | return getJCharTy(); |
| 102 | |
| 103 | case kShort: |
| 104 | return getJShortTy(); |
| 105 | |
| 106 | case kInt: |
| 107 | return getJIntTy(); |
| 108 | |
| 109 | case kLong: |
| 110 | return getJLongTy(); |
| 111 | |
| 112 | case kFloat: |
| 113 | return getJFloatTy(); |
| 114 | |
| 115 | case kDouble: |
| 116 | return getJDoubleTy(); |
| 117 | |
| 118 | case kObject: |
| 119 | return getJObjectTy(); |
| 120 | } |
| 121 | |
| 122 | LOG(FATAL) << "Unknown java type: " << jty; |
| 123 | return NULL; |
| 124 | } |
| 125 | |
| 126 | |
| 127 | llvm::Type* IRBuilder::getJTypeInRegSpace(JType jty) { |
Logan Chien | 8342616 | 2011-12-09 09:29:50 +0800 | [diff] [blame] | 128 | RegCategory regcat = GetRegCategoryFromJType(jty); |
| 129 | |
| 130 | switch (regcat) { |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 131 | case kRegUnknown: |
| 132 | case kRegZero: |
Logan Chien | 8342616 | 2011-12-09 09:29:50 +0800 | [diff] [blame] | 133 | LOG(FATAL) << "Register category \"Unknown\" or \"Zero\" does not have " |
| 134 | << "the LLVM type"; |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 135 | return NULL; |
| 136 | |
| 137 | case kRegCat1nr: |
| 138 | return getInt32Ty(); |
| 139 | |
| 140 | case kRegCat2: |
| 141 | return getInt64Ty(); |
| 142 | |
| 143 | case kRegObject: |
| 144 | return getJObjectTy(); |
| 145 | } |
| 146 | |
Logan Chien | 8342616 | 2011-12-09 09:29:50 +0800 | [diff] [blame] | 147 | LOG(FATAL) << "Unknown register category: " << regcat; |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 148 | return NULL; |
| 149 | } |
| 150 | |
| 151 | |
| 152 | llvm::Type* IRBuilder::getJTypeInArraySpace(JType jty) { |
| 153 | switch (jty) { |
| 154 | case kVoid: |
Logan Chien | 8342616 | 2011-12-09 09:29:50 +0800 | [diff] [blame] | 155 | LOG(FATAL) << "void type should not be used in array type space"; |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 156 | return NULL; |
| 157 | |
| 158 | case kBoolean: |
| 159 | case kByte: |
| 160 | return getInt8Ty(); |
| 161 | |
| 162 | case kChar: |
| 163 | case kShort: |
| 164 | return getInt16Ty(); |
| 165 | |
| 166 | case kInt: |
| 167 | return getInt32Ty(); |
| 168 | |
| 169 | case kLong: |
| 170 | return getInt64Ty(); |
| 171 | |
| 172 | case kFloat: |
| 173 | return getFloatTy(); |
| 174 | |
| 175 | case kDouble: |
| 176 | return getDoubleTy(); |
| 177 | |
| 178 | case kObject: |
| 179 | return getJObjectTy(); |
| 180 | } |
| 181 | |
Logan Chien | 8342616 | 2011-12-09 09:29:50 +0800 | [diff] [blame] | 182 | LOG(FATAL) << "Unknown java type: " << jty; |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 183 | return NULL; |
| 184 | } |
Logan Chien | 8342616 | 2011-12-09 09:29:50 +0800 | [diff] [blame] | 185 | |
| 186 | |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 187 | llvm::StructType* IRBuilder::getShadowFrameTy(uint32_t sirt_size) { |
Logan Chien | 1b0a1b7 | 2012-03-15 06:20:17 +0800 | [diff] [blame^] | 188 | std::string name(StringPrintf("ShadowFrame%u", sirt_size)); |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 189 | |
| 190 | // Try to find the existing struct type definition |
| 191 | if (llvm::Type* type = module_->getTypeByName(name)) { |
| 192 | CHECK(llvm::isa<llvm::StructType>(type)); |
| 193 | return static_cast<llvm::StructType*>(type); |
| 194 | } |
| 195 | |
| 196 | // Create new struct type definition |
| 197 | llvm::Type* elem_types[] = { |
| 198 | art_frame_type_, |
| 199 | llvm::ArrayType::get(jobject_type_, sirt_size), |
| 200 | }; |
| 201 | |
| 202 | return llvm::StructType::create(elem_types, name); |
| 203 | } |
| 204 | |
| 205 | |
Logan Chien | 8342616 | 2011-12-09 09:29:50 +0800 | [diff] [blame] | 206 | } // namespace compiler_llvm |
| 207 | } // namespace art |