Source.cpp revision d2a5a0eab7a1273797029702652e50b2ed9e6a6d
1/* 2 * Copyright 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 "Source.h" 18 19#include <new> 20 21#include <llvm/Bitcode/ReaderWriter.h> 22#include <llvm/LLVMContext.h> 23#include <llvm/Linker.h> 24#include <llvm/Module.h> 25#include <llvm/Support/MemoryBuffer.h> 26#include <llvm/Support/system_error.h> 27 28#include "BCCContext.h" 29#include "BCCContextImpl.h" 30#include "DebugHelper.h" 31 32namespace { 33 34// Helper function to load the bitcode. This uses "bitcode lazy load" feature to 35// reduce the startup time. On success, return the LLVM module object created 36// and take the ownership of input memory buffer (i.e., pInput). On error, 37// return NULL and will NOT take the ownership of pInput. 38static inline llvm::Module *helper_load_bitcode(llvm::LLVMContext &pContext, 39 llvm::MemoryBuffer *pInput) { 40 std::string error; 41 llvm::Module *module = llvm::getLazyBitcodeModule(pInput, pContext, &error); 42 43 if (module == NULL) { 44 ALOGE("Unable to parse the given bitcode file `%s'! (%s)", 45 pInput->getBufferIdentifier(), error.c_str()); 46 } 47 48 return module; 49} 50 51} // end anonymous namespace 52 53namespace bcc { 54 55Source *Source::CreateFromBuffer(BCCContext &pContext, 56 const char *pName, 57 const char *pBitcode, 58 size_t pBitcodeSize) { 59 llvm::StringRef input_data(pBitcode, pBitcodeSize); 60 llvm::MemoryBuffer *input_memory = 61 llvm::MemoryBuffer::getMemBuffer(input_data, pName); 62 63 if (input_memory == NULL) { 64 ALOGE("Unable to load bitcode `%s' from buffer!", pName); 65 return NULL; 66 } 67 68 llvm::Module *module = helper_load_bitcode(pContext.mImpl->mLLVMContext, 69 input_memory); 70 if (module == NULL) { 71 delete input_memory; 72 return NULL; 73 } 74 75 Source *result = CreateFromModule(pContext, *module, /* pNoDelete */false); 76 if (result == NULL) { 77 delete module; 78 } 79 80 return result; 81} 82 83 84Source *Source::CreateFromFile(BCCContext &pContext, const std::string &pPath) { 85 llvm::OwningPtr<llvm::MemoryBuffer> input_data; 86 87 llvm::error_code ec = llvm::MemoryBuffer::getFile(pPath, input_data); 88 if (ec != llvm::error_code::success()) { 89 ALOGE("Failed to load bitcode from path %s! (%s)", pPath.c_str(), 90 ec.message().c_str()); 91 return NULL; 92 } 93 94 llvm::MemoryBuffer *input_memory = input_data.take(); 95 llvm::Module *module = helper_load_bitcode(pContext.mImpl->mLLVMContext, 96 input_memory); 97 if (module == NULL) { 98 delete input_memory; 99 return NULL; 100 } 101 102 Source *result = CreateFromModule(pContext, *module, /* pNoDelete */false); 103 if (result == NULL) { 104 delete module; 105 } 106 107 return result; 108} 109 110 111Source *Source::CreateFromModule(BCCContext &pContext, llvm::Module &pModule, 112 bool pNoDelete) { 113 Source *result = new (std::nothrow) Source(pContext, pModule, pNoDelete); 114 if (result == NULL) { 115 ALOGE("Out of memory during Source object allocation for `%s'!", 116 pModule.getModuleIdentifier().c_str()); 117 } 118 return result; 119} 120 121Source::Source(BCCContext &pContext, llvm::Module &pModule, bool pNoDelete) 122 : mContext(pContext), mModule(&pModule), mNoDelete(pNoDelete) { 123 pContext.addSource(*this); 124} 125 126Source::~Source() { 127 mContext.removeSource(*this); 128 if (!mNoDelete) 129 delete mModule; 130} 131 132bool Source::merge(Source &pSource, bool pPreserveSource) { 133 std::string error; 134 llvm::Linker::LinkerMode mode = 135 ((pPreserveSource) ? llvm::Linker::PreserveSource : 136 llvm::Linker::DestroySource); 137 138 if (llvm::Linker::LinkModules(mModule, &pSource.getModule(), 139 mode, &error) != 0) { 140 ALOGE("Failed to link source `%s' with `%s' (%s)!", 141 getIdentifier().c_str(), 142 pSource.getIdentifier().c_str(), 143 error.c_str()); 144 return false; 145 } 146 147 if (!pPreserveSource) { 148 pSource.mNoDelete = true; 149 delete &pSource; 150 } 151 152 return true; 153} 154 155Source *Source::CreateEmpty(BCCContext &pContext, const std::string &pName) { 156 // Create an empty module 157 llvm::Module *module = 158 new (std::nothrow) llvm::Module(pName, pContext.mImpl->mLLVMContext); 159 160 if (module == NULL) { 161 ALOGE("Out of memory when creating empty LLVM module `%s'!", pName.c_str()); 162 return NULL; 163 } 164 165 Source *result = CreateFromModule(pContext, *module, /* pNoDelete */false); 166 if (result == NULL) { 167 delete module; 168 } 169 170 return result; 171} 172 173const std::string &Source::getIdentifier() const { 174 return mModule->getModuleIdentifier(); 175} 176 177} // namespace bcc 178