| Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 1 | /* |
| Stephen Hines | 7cd4c49 | 2012-03-13 19:57:37 -0700 | [diff] [blame] | 2 | * Copyright 2011-2012, The Android Open Source Project |
| Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 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 "bcinfo/BitcodeTranslator.h" |
| 18 | |
| Stephen Hines | 7cd4c49 | 2012-03-13 19:57:37 -0700 | [diff] [blame] | 19 | #include "bcinfo/BitcodeWrapper.h" |
| 20 | |
| Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 21 | #include "BitReader_2_7/BitReader_2_7.h" |
| Logan Chien | c7d67a7 | 2011-12-16 17:06:49 +0800 | [diff] [blame] | 22 | #include "BitReader_3_0/BitReader_3_0.h" |
| Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 23 | |
| Stephen Hines | 8dbca8e | 2013-06-21 16:30:26 -0700 | [diff] [blame] | 24 | #include "BitWriter_3_2/ReaderWriter_3_2.h" |
| 25 | |
| Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 26 | #define LOG_TAG "bcinfo" |
| 27 | #include <cutils/log.h> |
| 28 | |
| Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 29 | #include "llvm/Bitcode/BitstreamWriter.h" |
| 30 | #include "llvm/Bitcode/ReaderWriter.h" |
| Stephen Hines | b730e23 | 2013-01-09 15:31:36 -0800 | [diff] [blame] | 31 | #include "llvm/IR/LLVMContext.h" |
| 32 | #include "llvm/IR/Module.h" |
| Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 33 | #include "llvm/Support/MemoryBuffer.h" |
| Shih-wei Liao | c73b521 | 2012-03-05 09:51:44 -0800 | [diff] [blame] | 34 | #include "llvm/Support/raw_ostream.h" |
| Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 35 | |
| 36 | #include <cstdlib> |
| Chris Wailes | 881cda4 | 2014-06-23 11:27:41 -0700 | [diff] [blame] | 37 | #include <climits> |
| Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 38 | |
| 39 | namespace bcinfo { |
| 40 | |
| 41 | /** |
| 42 | * Define minimum and maximum target API versions. These correspond to the |
| 43 | * same API levels used by the standard Android SDK. |
| 44 | * |
| Logan Chien | c7d67a7 | 2011-12-16 17:06:49 +0800 | [diff] [blame] | 45 | * LLVM 2.7 |
| 46 | * 11 - Honeycomb |
| 47 | * 12 - Honeycomb MR1 |
| 48 | * 13 - Honeycomb MR2 |
| 49 | * |
| 50 | * LLVM 3.0 |
| 51 | * 14 - Ice Cream Sandwich |
| 52 | * 15 - Ice Cream Sandwich MR1 |
| 53 | * |
| 54 | * LLVM 3.1 |
| 55 | * 16 - Ice Cream Sandwich MR2 |
| Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 56 | */ |
| Chris Wailes | 881cda4 | 2014-06-23 11:27:41 -0700 | [diff] [blame] | 57 | static const unsigned int kMinimumAPIVersion = 11; |
| 58 | static const unsigned int kMaximumAPIVersion = RS_VERSION; |
| 59 | static const unsigned int kCurrentAPIVersion = 10000; |
| 60 | static const unsigned int kDevelopmentAPIVersion = UINT_MAX; |
| Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 61 | |
| 62 | /** |
| 63 | * The minimum version which does not require translation (i.e. is already |
| 64 | * compatible with LLVM's default bitcode reader). |
| 65 | */ |
| Logan Chien | c7d67a7 | 2011-12-16 17:06:49 +0800 | [diff] [blame] | 66 | static const unsigned int kMinimumUntranslatedVersion = 16; |
| 67 | static const unsigned int kMinimumCompatibleVersion_LLVM_3_0 = 14; |
| 68 | static const unsigned int kMinimumCompatibleVersion_LLVM_2_7 = 11; |
| Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 69 | |
| 70 | |
| 71 | BitcodeTranslator::BitcodeTranslator(const char *bitcode, size_t bitcodeSize, |
| 72 | unsigned int version) |
| Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 73 | : mBitcode(bitcode), mBitcodeSize(bitcodeSize), mTranslatedBitcode(nullptr), |
| Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 74 | mTranslatedBitcodeSize(0), mVersion(version) { |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | |
| 79 | BitcodeTranslator::~BitcodeTranslator() { |
| 80 | if (mVersion < kMinimumUntranslatedVersion) { |
| 81 | // We didn't actually do a translation in the alternate case, so deleting |
| 82 | // the bitcode would be improper. |
| 83 | delete [] mTranslatedBitcode; |
| 84 | } |
| Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 85 | mTranslatedBitcode = nullptr; |
| Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 86 | return; |
| 87 | } |
| 88 | |
| 89 | |
| 90 | bool BitcodeTranslator::translate() { |
| 91 | if (!mBitcode || !mBitcodeSize) { |
| Steve Block | 10c1412 | 2012-01-08 10:15:06 +0000 | [diff] [blame] | 92 | ALOGE("Invalid/empty bitcode"); |
| Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 93 | return false; |
| 94 | } |
| 95 | |
| Stephen Hines | 7cd4c49 | 2012-03-13 19:57:37 -0700 | [diff] [blame] | 96 | BitcodeWrapper BCWrapper(mBitcode, mBitcodeSize); |
| 97 | if (BCWrapper.getTargetAPI() != mVersion) { |
| 98 | ALOGE("Bitcode wrapper (%u) and translator (%u) disagree about target API", |
| 99 | BCWrapper.getTargetAPI(), mVersion); |
| 100 | } |
| 101 | |
| Chris Wailes | 881cda4 | 2014-06-23 11:27:41 -0700 | [diff] [blame] | 102 | if ((mVersion != kDevelopmentAPIVersion) && |
| 103 | (mVersion != kCurrentAPIVersion) && |
| 104 | ((mVersion < kMinimumAPIVersion) || |
| 105 | (mVersion > kMaximumAPIVersion))) { |
| Steve Block | 10c1412 | 2012-01-08 10:15:06 +0000 | [diff] [blame] | 106 | ALOGE("Invalid API version: %u is out of range ('%u' - '%u')", mVersion, |
| Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 107 | kMinimumAPIVersion, kMaximumAPIVersion); |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | // We currently don't need to transcode any API version higher than 14 or |
| 112 | // the current API version (i.e. 10000) |
| 113 | if (mVersion >= kMinimumUntranslatedVersion) { |
| 114 | mTranslatedBitcode = mBitcode; |
| 115 | mTranslatedBitcodeSize = mBitcodeSize; |
| 116 | return true; |
| 117 | } |
| 118 | |
| 119 | // Do the actual transcoding by invoking a 2.7-era bitcode reader that can |
| 120 | // then write the bitcode back out in a more modern (acceptable) version. |
| Stephen Hines | d0993af | 2014-07-15 16:49:25 -0700 | [diff] [blame] | 121 | std::unique_ptr<llvm::LLVMContext> mContext(new llvm::LLVMContext()); |
| 122 | std::unique_ptr<llvm::MemoryBuffer> MEM( |
| Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 123 | llvm::MemoryBuffer::getMemBuffer( |
| Stephen Hines | e708ffe | 2012-05-03 15:18:49 -0700 | [diff] [blame] | 124 | llvm::StringRef(mBitcode, mBitcodeSize), "", false)); |
| Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 125 | std::string error; |
| Stephen Hines | 5793613 | 2014-11-25 17:54:59 -0800 | [diff] [blame] | 126 | llvm::ErrorOr<llvm::MemoryBufferRef> MBOrErr = MEM->getMemBufferRef(); |
| Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 127 | |
| Stephen Hines | 5793613 | 2014-11-25 17:54:59 -0800 | [diff] [blame] | 128 | llvm::ErrorOr<llvm::Module *> MOrErr(nullptr); |
| Logan Chien | c7d67a7 | 2011-12-16 17:06:49 +0800 | [diff] [blame] | 129 | |
| 130 | if (mVersion >= kMinimumCompatibleVersion_LLVM_3_0) { |
| Stephen Hines | 5793613 | 2014-11-25 17:54:59 -0800 | [diff] [blame] | 131 | MOrErr = llvm_3_0::parseBitcodeFile(*MBOrErr, *mContext); |
| Logan Chien | c7d67a7 | 2011-12-16 17:06:49 +0800 | [diff] [blame] | 132 | } else if (mVersion >= kMinimumCompatibleVersion_LLVM_2_7) { |
| Stephen Hines | 5793613 | 2014-11-25 17:54:59 -0800 | [diff] [blame] | 133 | MOrErr = llvm_2_7::parseBitcodeFile(*MBOrErr, *mContext); |
| Logan Chien | c7d67a7 | 2011-12-16 17:06:49 +0800 | [diff] [blame] | 134 | } else { |
| Steve Block | 10c1412 | 2012-01-08 10:15:06 +0000 | [diff] [blame] | 135 | ALOGE("No compatible bitcode reader for API version %d", mVersion); |
| Logan Chien | c7d67a7 | 2011-12-16 17:06:49 +0800 | [diff] [blame] | 136 | return false; |
| 137 | } |
| 138 | |
| Stephen Hines | 5793613 | 2014-11-25 17:54:59 -0800 | [diff] [blame] | 139 | if (std::error_code EC = MOrErr.getError()) { |
| Steve Block | 10c1412 | 2012-01-08 10:15:06 +0000 | [diff] [blame] | 140 | ALOGE("Could not parse bitcode file"); |
| Stephen Hines | 5793613 | 2014-11-25 17:54:59 -0800 | [diff] [blame] | 141 | ALOGE("%s", EC.message().c_str()); |
| Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 142 | return false; |
| 143 | } |
| 144 | |
| Stephen Hines | 5793613 | 2014-11-25 17:54:59 -0800 | [diff] [blame] | 145 | // Module ownership is handled by the context, so we don't need to free it. |
| 146 | llvm::Module *module = MOrErr.get(); |
| 147 | |
| Shih-wei Liao | c73b521 | 2012-03-05 09:51:44 -0800 | [diff] [blame] | 148 | std::string Buffer; |
| 149 | |
| 150 | llvm::raw_string_ostream OS(Buffer); |
| Stephen Hines | 8dbca8e | 2013-06-21 16:30:26 -0700 | [diff] [blame] | 151 | // Use the LLVM 3.2 bitcode writer, instead of the top-of-tree version. |
| 152 | llvm_3_2::WriteBitcodeToFile(module, OS); |
| Shih-wei Liao | c73b521 | 2012-03-05 09:51:44 -0800 | [diff] [blame] | 153 | OS.flush(); |
| Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 154 | |
| Stephen Hines | 7cd4c49 | 2012-03-13 19:57:37 -0700 | [diff] [blame] | 155 | AndroidBitcodeWrapper wrapper; |
| 156 | size_t actualWrapperLen = writeAndroidBitcodeWrapper( |
| Stephen Hines | dc31b1c | 2014-12-17 19:23:28 -0800 | [diff] [blame] | 157 | &wrapper, Buffer.size(), kMinimumUntranslatedVersion, |
| Stephen Hines | 7cd4c49 | 2012-03-13 19:57:37 -0700 | [diff] [blame] | 158 | BCWrapper.getCompilerVersion(), BCWrapper.getOptimizationLevel()); |
| 159 | if (!actualWrapperLen) { |
| 160 | ALOGE("Couldn't produce bitcode wrapper!"); |
| 161 | return false; |
| 162 | } |
| 163 | |
| 164 | mTranslatedBitcodeSize = actualWrapperLen + Buffer.size(); |
| 165 | char *c = new char[mTranslatedBitcodeSize]; |
| 166 | memcpy(c, &wrapper, actualWrapperLen); |
| Stephen Hines | e9c850f | 2012-03-21 15:24:25 -0700 | [diff] [blame] | 167 | memcpy(c + actualWrapperLen, Buffer.c_str(), Buffer.size()); |
| Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 168 | |
| 169 | mTranslatedBitcode = c; |
| Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 170 | |
| 171 | return true; |
| 172 | } |
| 173 | |
| 174 | } // namespace bcinfo |