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" |
Elliott Hughes | e222ee0 | 2012-12-13 14:41:43 -0800 | [diff] [blame] | 18 | |
| 19 | #include "base/stringprintf.h" |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 20 | |
Brian Carlstrom | 37d4879 | 2013-03-22 14:14:45 -0700 | [diff] [blame] | 21 | #include <llvm/IR/Module.h> |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 22 | |
Logan Chien | 8342616 | 2011-12-09 09:29:50 +0800 | [diff] [blame] | 23 | namespace art { |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 24 | namespace llvm { |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 25 | |
| 26 | |
| 27 | //---------------------------------------------------------------------------- |
| 28 | // General |
| 29 | //---------------------------------------------------------------------------- |
| 30 | |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 31 | IRBuilder::IRBuilder(::llvm::LLVMContext& context, ::llvm::Module& module, |
Ian Rogers | 76ae4fe | 2013-02-27 16:03:41 -0800 | [diff] [blame] | 32 | IntrinsicHelper& intrinsic_helper) |
| 33 | : LLVMIRBuilder(context), module_(&module), mdb_(context), java_object_type_(NULL), |
| 34 | java_method_type_(NULL), java_thread_type_(NULL), intrinsic_helper_(intrinsic_helper) { |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 35 | // Get java object type from module |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 36 | ::llvm::Type* jobject_struct_type = module.getTypeByName("JavaObject"); |
Logan Chien | 1b0a1b7 | 2012-03-15 06:20:17 +0800 | [diff] [blame] | 37 | CHECK(jobject_struct_type != NULL); |
Ian Rogers | 76ae4fe | 2013-02-27 16:03:41 -0800 | [diff] [blame] | 38 | java_object_type_ = jobject_struct_type->getPointerTo(); |
| 39 | |
| 40 | // If type of Method is not explicitly defined in the module, use JavaObject* |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 41 | ::llvm::Type* type = module.getTypeByName("Method"); |
Ian Rogers | 76ae4fe | 2013-02-27 16:03:41 -0800 | [diff] [blame] | 42 | if (type != NULL) { |
| 43 | java_method_type_ = type->getPointerTo(); |
| 44 | } else { |
| 45 | java_method_type_ = java_object_type_; |
| 46 | } |
| 47 | |
| 48 | // If type of Thread is not explicitly defined in the module, use JavaObject* |
| 49 | type = module.getTypeByName("Thread"); |
| 50 | if (type != NULL) { |
| 51 | java_thread_type_ = type->getPointerTo(); |
| 52 | } else { |
| 53 | java_thread_type_ = java_object_type_; |
| 54 | } |
Logan Chien | 42e0e15 | 2012-01-13 15:42:36 +0800 | [diff] [blame] | 55 | |
Logan Chien | f04364f | 2012-02-10 12:01:39 +0800 | [diff] [blame] | 56 | // Create JEnv* type |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 57 | ::llvm::Type* jenv_struct_type = ::llvm::StructType::create(context, "JEnv"); |
Logan Chien | f04364f | 2012-02-10 12:01:39 +0800 | [diff] [blame] | 58 | jenv_type_ = jenv_struct_type->getPointerTo(); |
| 59 | |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 60 | // Get Art shadow frame struct type from module |
Logan Chien | 1b0a1b7 | 2012-03-15 06:20:17 +0800 | [diff] [blame] | 61 | art_frame_type_ = module.getTypeByName("ShadowFrame"); |
| 62 | CHECK(art_frame_type_ != NULL); |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 63 | |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 64 | runtime_support_ = NULL; |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | |
| 68 | //---------------------------------------------------------------------------- |
| 69 | // Type Helper Function |
| 70 | //---------------------------------------------------------------------------- |
| 71 | |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 72 | ::llvm::Type* IRBuilder::getJType(JType jty) { |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 73 | switch (jty) { |
| 74 | case kVoid: |
| 75 | return getJVoidTy(); |
| 76 | |
| 77 | case kBoolean: |
| 78 | return getJBooleanTy(); |
| 79 | |
| 80 | case kByte: |
| 81 | return getJByteTy(); |
| 82 | |
| 83 | case kChar: |
| 84 | return getJCharTy(); |
| 85 | |
| 86 | case kShort: |
| 87 | return getJShortTy(); |
| 88 | |
| 89 | case kInt: |
| 90 | return getJIntTy(); |
| 91 | |
| 92 | case kLong: |
| 93 | return getJLongTy(); |
| 94 | |
| 95 | case kFloat: |
| 96 | return getJFloatTy(); |
| 97 | |
| 98 | case kDouble: |
| 99 | return getJDoubleTy(); |
| 100 | |
| 101 | case kObject: |
| 102 | return getJObjectTy(); |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 103 | |
TDYa127 | 706e7db | 2012-05-06 00:05:33 -0700 | [diff] [blame] | 104 | default: |
| 105 | LOG(FATAL) << "Unknown java type: " << jty; |
| 106 | return NULL; |
| 107 | } |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 110 | ::llvm::StructType* IRBuilder::getShadowFrameTy(uint32_t vreg_size) { |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 111 | std::string name(StringPrintf("ShadowFrame%u", vreg_size)); |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 112 | |
| 113 | // Try to find the existing struct type definition |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 114 | if (::llvm::Type* type = module_->getTypeByName(name)) { |
| 115 | CHECK(::llvm::isa< ::llvm::StructType>(type)); |
| 116 | return static_cast< ::llvm::StructType*>(type); |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | // Create new struct type definition |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 120 | ::llvm::Type* elem_types[] = { |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 121 | art_frame_type_, |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 122 | ::llvm::ArrayType::get(getInt32Ty(), vreg_size), |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 123 | }; |
| 124 | |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 125 | return ::llvm::StructType::create(elem_types, name); |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 129 | } // namespace llvm |
| 130 | } // namespace art |