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" |
| 22 | |
| 23 | #define LOG_TAG "bcinfo" |
| 24 | #include <cutils/log.h> |
| 25 | |
| 26 | #include "llvm/ADT/OwningPtr.h" |
| 27 | #include "llvm/Bitcode/BitstreamWriter.h" |
| 28 | #include "llvm/Bitcode/ReaderWriter.h" |
| 29 | #include "llvm/LLVMContext.h" |
| 30 | #include "llvm/Module.h" |
| 31 | #include "llvm/Support/MemoryBuffer.h" |
| 32 | |
| 33 | #include <cstdlib> |
| 34 | |
| 35 | namespace bcinfo { |
| 36 | |
| 37 | /** |
| 38 | * Define minimum and maximum target API versions. These correspond to the |
| 39 | * same API levels used by the standard Android SDK. |
| 40 | * |
| 41 | * 11 - Honeycomb |
| 42 | * 12 - Honeycomb MR1 |
| 43 | * 13 - Honeycomb MR2 |
| 44 | * 14 - Ice Cream Sandwich |
| 45 | */ |
| 46 | static const unsigned int kMinimumAPIVersion = 11; |
| 47 | static const unsigned int kMaximumAPIVersion = BCINFO_API_VERSION; |
| 48 | static const unsigned int kCurrentAPIVersion = 10000; |
| 49 | |
| 50 | /** |
| 51 | * The minimum version which does not require translation (i.e. is already |
| 52 | * compatible with LLVM's default bitcode reader). |
| 53 | */ |
| 54 | static const unsigned int kMinimumUntranslatedVersion = 14; |
| 55 | |
| 56 | |
| 57 | BitcodeTranslator::BitcodeTranslator(const char *bitcode, size_t bitcodeSize, |
| 58 | unsigned int version) |
| 59 | : mBitcode(bitcode), mBitcodeSize(bitcodeSize), mTranslatedBitcode(NULL), |
| 60 | mTranslatedBitcodeSize(0), mVersion(version) { |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | |
| 65 | BitcodeTranslator::~BitcodeTranslator() { |
| 66 | if (mVersion < kMinimumUntranslatedVersion) { |
| 67 | // We didn't actually do a translation in the alternate case, so deleting |
| 68 | // the bitcode would be improper. |
| 69 | delete [] mTranslatedBitcode; |
| 70 | } |
| 71 | mTranslatedBitcode = NULL; |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | |
| 76 | bool BitcodeTranslator::translate() { |
| 77 | if (!mBitcode || !mBitcodeSize) { |
| 78 | LOGE("Invalid/empty bitcode"); |
| 79 | return false; |
| 80 | } |
| 81 | |
Stephen Hines | 7cd4c49 | 2012-03-13 19:57:37 -0700 | [diff] [blame^] | 82 | BitcodeWrapper BCWrapper(mBitcode, mBitcodeSize); |
| 83 | if (BCWrapper.getTargetAPI() != mVersion) { |
| 84 | ALOGE("Bitcode wrapper (%u) and translator (%u) disagree about target API", |
| 85 | BCWrapper.getTargetAPI(), mVersion); |
| 86 | } |
| 87 | |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 88 | if ((mVersion != kCurrentAPIVersion) && |
| 89 | ((mVersion < kMinimumAPIVersion) || |
| 90 | (mVersion > kMaximumAPIVersion))) { |
| 91 | LOGE("Invalid API version: %u is out of range ('%u' - '%u')", mVersion, |
| 92 | kMinimumAPIVersion, kMaximumAPIVersion); |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | // We currently don't need to transcode any API version higher than 14 or |
| 97 | // the current API version (i.e. 10000) |
| 98 | if (mVersion >= kMinimumUntranslatedVersion) { |
| 99 | mTranslatedBitcode = mBitcode; |
| 100 | mTranslatedBitcodeSize = mBitcodeSize; |
| 101 | return true; |
| 102 | } |
| 103 | |
| 104 | // Do the actual transcoding by invoking a 2.7-era bitcode reader that can |
| 105 | // then write the bitcode back out in a more modern (acceptable) version. |
| 106 | llvm::OwningPtr<llvm::LLVMContext> mContext(new llvm::LLVMContext()); |
| 107 | llvm::OwningPtr<llvm::MemoryBuffer> MEM( |
| 108 | llvm::MemoryBuffer::getMemBuffer( |
| 109 | llvm::StringRef(mBitcode, mBitcodeSize))); |
| 110 | std::string error; |
| 111 | |
| 112 | // Module ownership is handled by the context, so we don't need to free it. |
| 113 | llvm::Module *module = |
| 114 | llvm_2_7::ParseBitcodeFile(MEM.get(), *mContext, &error); |
| 115 | if (!module) { |
| 116 | LOGE("Could not parse bitcode file"); |
| 117 | LOGE("%s", error.c_str()); |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | std::vector<unsigned char> Buffer; |
| 122 | llvm::BitstreamWriter Stream(Buffer); |
| 123 | Buffer.reserve(mBitcodeSize); |
| 124 | llvm::WriteBitcodeToStream(module, Stream); |
| 125 | |
Stephen Hines | 7cd4c49 | 2012-03-13 19:57:37 -0700 | [diff] [blame^] | 126 | AndroidBitcodeWrapper wrapper; |
| 127 | size_t actualWrapperLen = writeAndroidBitcodeWrapper( |
| 128 | &wrapper, Buffer.size(), BCWrapper.getTargetAPI(), |
| 129 | BCWrapper.getCompilerVersion(), BCWrapper.getOptimizationLevel()); |
| 130 | if (!actualWrapperLen) { |
| 131 | ALOGE("Couldn't produce bitcode wrapper!"); |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | mTranslatedBitcodeSize = actualWrapperLen + Buffer.size(); |
| 136 | char *c = new char[mTranslatedBitcodeSize]; |
| 137 | memcpy(c, &wrapper, actualWrapperLen); |
| 138 | memcpy(c + actualWrapperLen, &Buffer.front(), Buffer.size()); |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 139 | |
| 140 | mTranslatedBitcode = c; |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 141 | |
| 142 | return true; |
| 143 | } |
| 144 | |
| 145 | } // namespace bcinfo |
| 146 | |