1 //===- IRObjectFile.cpp - IR object file implementation ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Part of the IRObjectFile class implementation.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Bitcode/ReaderWriter.h"
15 #include "llvm/IR/LLVMContext.h"
16 #include "llvm/IR/Mangler.h"
17 #include "llvm/IR/Module.h"
18 #include "llvm/Object/IRObjectFile.h"
19 #include "llvm/Support/raw_ostream.h"
21 using namespace object;
22 using std::error_code;
24 IRObjectFile::IRObjectFile(MemoryBuffer *Object, error_code &EC,
25 LLVMContext &Context, bool BufferOwned)
26 : SymbolicFile(Binary::ID_IR, Object, BufferOwned) {
27 ErrorOr<Module*> MOrErr = parseBitcodeFile(Object, Context);
28 if ((EC = MOrErr.getError()))
31 M.reset(MOrErr.get());
33 // If we have a DataLayout, setup a mangler.
34 const DataLayout *DL = M->getDataLayout();
38 Mang.reset(new Mangler(DL));
41 static const GlobalValue &getGV(DataRefImpl &Symb) {
42 return *reinterpret_cast<GlobalValue*>(Symb.p & ~uintptr_t(3));
45 static uintptr_t skipEmpty(Module::const_alias_iterator I, const Module &M) {
46 if (I == M.alias_end())
48 const GlobalValue *GV = &*I;
49 return reinterpret_cast<uintptr_t>(GV) | 2;
52 static uintptr_t skipEmpty(Module::const_global_iterator I, const Module &M) {
53 if (I == M.global_end())
54 return skipEmpty(M.alias_begin(), M);
55 const GlobalValue *GV = &*I;
56 return reinterpret_cast<uintptr_t>(GV) | 1;
59 static uintptr_t skipEmpty(Module::const_iterator I, const Module &M) {
61 return skipEmpty(M.global_begin(), M);
62 const GlobalValue *GV = &*I;
63 return reinterpret_cast<uintptr_t>(GV) | 0;
66 void IRObjectFile::moveSymbolNext(DataRefImpl &Symb) const {
67 const GlobalValue *GV = &getGV(Symb);
68 const Module &M = *GV->getParent();
72 Module::const_iterator Iter(static_cast<const Function*>(GV));
74 Res = skipEmpty(Iter, M);
78 Module::const_global_iterator Iter(static_cast<const GlobalVariable*>(GV));
80 Res = skipEmpty(Iter, M);
84 Module::const_alias_iterator Iter(static_cast<const GlobalAlias*>(GV));
86 Res = skipEmpty(Iter, M);
90 llvm_unreachable("Invalid symbol reference");
96 error_code IRObjectFile::printSymbolName(raw_ostream &OS,
97 DataRefImpl Symb) const {
98 const GlobalValue &GV = getGV(Symb);
101 Mang->getNameWithPrefix(OS, &GV, false);
105 return object_error::success;
108 uint32_t IRObjectFile::getSymbolFlags(DataRefImpl Symb) const {
109 const GlobalValue &GV = getGV(Symb);
111 uint32_t Res = BasicSymbolRef::SF_None;
112 if (GV.isDeclaration() || GV.hasAvailableExternallyLinkage())
113 Res |= BasicSymbolRef::SF_Undefined;
114 if (GV.hasPrivateLinkage())
115 Res |= BasicSymbolRef::SF_FormatSpecific;
116 if (!GV.hasLocalLinkage())
117 Res |= BasicSymbolRef::SF_Global;
118 if (GV.hasCommonLinkage())
119 Res |= BasicSymbolRef::SF_Common;
120 if (GV.hasLinkOnceLinkage() || GV.hasWeakLinkage())
121 Res |= BasicSymbolRef::SF_Weak;
126 const GlobalValue &IRObjectFile::getSymbolGV(DataRefImpl Symb) const {
127 const GlobalValue &GV = getGV(Symb);
131 basic_symbol_iterator IRObjectFile::symbol_begin_impl() const {
132 Module::const_iterator I = M->begin();
134 Ret.p = skipEmpty(I, *M);
135 return basic_symbol_iterator(BasicSymbolRef(Ret, this));
138 basic_symbol_iterator IRObjectFile::symbol_end_impl() const {
141 return basic_symbol_iterator(BasicSymbolRef(Ret, this));
144 ErrorOr<SymbolicFile *> llvm::object::SymbolicFile::createIRObjectFile(
145 MemoryBuffer *Object, LLVMContext &Context, bool BufferOwned) {
147 std::unique_ptr<IRObjectFile> Ret(
148 new IRObjectFile(Object, EC, Context, BufferOwned));
151 return Ret.release();