Home | History | Annotate | Download | only in bcinfo
      1 /*
      2  * Copyright 2011-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 "bcinfo/BitcodeTranslator.h"
     18 
     19 #include "bcinfo/BitcodeWrapper.h"
     20 
     21 #include "BitReader_2_7/BitReader_2_7.h"
     22 #include "BitReader_3_0/BitReader_3_0.h"
     23 
     24 #include "BitWriter_3_2/ReaderWriter_3_2.h"
     25 
     26 #define LOG_TAG "bcinfo"
     27 #include <cutils/log.h>
     28 
     29 #include "llvm/ADT/OwningPtr.h"
     30 #include "llvm/Bitcode/BitstreamWriter.h"
     31 #include "llvm/Bitcode/ReaderWriter.h"
     32 #include "llvm/IR/LLVMContext.h"
     33 #include "llvm/IR/Module.h"
     34 #include "llvm/Support/MemoryBuffer.h"
     35 #include "llvm/Support/raw_ostream.h"
     36 
     37 #include <cstdlib>
     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  *
     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
     56  */
     57 static const unsigned int kMinimumAPIVersion = 11;
     58 static const unsigned int kMaximumAPIVersion = BCINFO_API_VERSION;
     59 static const unsigned int kCurrentAPIVersion = 10000;
     60 
     61 /**
     62  * The minimum version which does not require translation (i.e. is already
     63  * compatible with LLVM's default bitcode reader).
     64  */
     65 static const unsigned int kMinimumUntranslatedVersion = 16;
     66 static const unsigned int kMinimumCompatibleVersion_LLVM_3_0 = 14;
     67 static const unsigned int kMinimumCompatibleVersion_LLVM_2_7 = 11;
     68 
     69 
     70 BitcodeTranslator::BitcodeTranslator(const char *bitcode, size_t bitcodeSize,
     71                                      unsigned int version)
     72     : mBitcode(bitcode), mBitcodeSize(bitcodeSize), mTranslatedBitcode(NULL),
     73       mTranslatedBitcodeSize(0), mVersion(version) {
     74   return;
     75 }
     76 
     77 
     78 BitcodeTranslator::~BitcodeTranslator() {
     79   if (mVersion < kMinimumUntranslatedVersion) {
     80     // We didn't actually do a translation in the alternate case, so deleting
     81     // the bitcode would be improper.
     82     delete [] mTranslatedBitcode;
     83   }
     84   mTranslatedBitcode = NULL;
     85   return;
     86 }
     87 
     88 
     89 bool BitcodeTranslator::translate() {
     90   if (!mBitcode || !mBitcodeSize) {
     91     ALOGE("Invalid/empty bitcode");
     92     return false;
     93   }
     94 
     95   BitcodeWrapper BCWrapper(mBitcode, mBitcodeSize);
     96   if (BCWrapper.getTargetAPI() != mVersion) {
     97     ALOGE("Bitcode wrapper (%u) and translator (%u) disagree about target API",
     98           BCWrapper.getTargetAPI(), mVersion);
     99   }
    100 
    101   if ((mVersion != kCurrentAPIVersion) &&
    102       ((mVersion < kMinimumAPIVersion) ||
    103        (mVersion > kMaximumAPIVersion))) {
    104     ALOGE("Invalid API version: %u is out of range ('%u' - '%u')", mVersion,
    105          kMinimumAPIVersion, kMaximumAPIVersion);
    106     return false;
    107   }
    108 
    109   // We currently don't need to transcode any API version higher than 14 or
    110   // the current API version (i.e. 10000)
    111   if (mVersion >= kMinimumUntranslatedVersion) {
    112     mTranslatedBitcode = mBitcode;
    113     mTranslatedBitcodeSize = mBitcodeSize;
    114     return true;
    115   }
    116 
    117   // Do the actual transcoding by invoking a 2.7-era bitcode reader that can
    118   // then write the bitcode back out in a more modern (acceptable) version.
    119   llvm::OwningPtr<llvm::LLVMContext> mContext(new llvm::LLVMContext());
    120   llvm::OwningPtr<llvm::MemoryBuffer> MEM(
    121     llvm::MemoryBuffer::getMemBuffer(
    122       llvm::StringRef(mBitcode, mBitcodeSize), "", false));
    123   std::string error;
    124 
    125   // Module ownership is handled by the context, so we don't need to free it.
    126   llvm::Module *module = NULL;
    127 
    128   if (mVersion >= kMinimumCompatibleVersion_LLVM_3_0) {
    129     module = llvm_3_0::ParseBitcodeFile(MEM.get(), *mContext, &error);
    130   } else if (mVersion >= kMinimumCompatibleVersion_LLVM_2_7) {
    131     module = llvm_2_7::ParseBitcodeFile(MEM.get(), *mContext, &error);
    132   } else {
    133     ALOGE("No compatible bitcode reader for API version %d", mVersion);
    134     return false;
    135   }
    136 
    137   if (!module) {
    138     ALOGE("Could not parse bitcode file");
    139     ALOGE("%s", error.c_str());
    140     return false;
    141   }
    142 
    143   std::string Buffer;
    144 
    145   llvm::raw_string_ostream OS(Buffer);
    146   // Use the LLVM 3.2 bitcode writer, instead of the top-of-tree version.
    147   llvm_3_2::WriteBitcodeToFile(module, OS);
    148   OS.flush();
    149 
    150   AndroidBitcodeWrapper wrapper;
    151   size_t actualWrapperLen = writeAndroidBitcodeWrapper(
    152       &wrapper, Buffer.size(), BCWrapper.getTargetAPI(),
    153       BCWrapper.getCompilerVersion(), BCWrapper.getOptimizationLevel());
    154   if (!actualWrapperLen) {
    155     ALOGE("Couldn't produce bitcode wrapper!");
    156     return false;
    157   }
    158 
    159   mTranslatedBitcodeSize = actualWrapperLen + Buffer.size();
    160   char *c = new char[mTranslatedBitcodeSize];
    161   memcpy(c, &wrapper, actualWrapperLen);
    162   memcpy(c + actualWrapperLen, Buffer.c_str(), Buffer.size());
    163 
    164   mTranslatedBitcode = c;
    165 
    166   return true;
    167 }
    168 
    169 }  // namespace bcinfo
    170 
    171