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