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/Module.h"
17 #include "llvm/Object/IRObjectFile.h"
18 #include "llvm/Support/raw_ostream.h"
20 using namespace object;
22 IRObjectFile::IRObjectFile(MemoryBuffer *Object, error_code &EC,
23 LLVMContext &Context, bool BufferOwned)
24 : SymbolicFile(Binary::ID_IR, Object, BufferOwned) {
25 ErrorOr<Module*> MOrErr = parseBitcodeFile(Object, Context);
26 if ((EC = MOrErr.getError()))
29 M.reset(MOrErr.get());
32 static const GlobalValue &getGV(DataRefImpl &Symb) {
33 return *reinterpret_cast<GlobalValue*>(Symb.p & ~uintptr_t(3));
36 static uintptr_t skipEmpty(Module::const_alias_iterator I, const Module &M) {
37 if (I == M.alias_end())
39 const GlobalValue *GV = &*I;
40 return reinterpret_cast<uintptr_t>(GV) | 2;
43 static uintptr_t skipEmpty(Module::const_global_iterator I, const Module &M) {
44 if (I == M.global_end())
45 return skipEmpty(M.alias_begin(), M);
46 const GlobalValue *GV = &*I;
47 return reinterpret_cast<uintptr_t>(GV) | 1;
50 static uintptr_t skipEmpty(Module::const_iterator I, const Module &M) {
52 return skipEmpty(M.global_begin(), M);
53 const GlobalValue *GV = &*I;
54 return reinterpret_cast<uintptr_t>(GV) | 0;
57 void IRObjectFile::moveSymbolNext(DataRefImpl &Symb) const {
58 const GlobalValue *GV = &getGV(Symb);
59 const Module &M = *GV->getParent();
63 Module::const_iterator Iter(static_cast<const Function*>(GV));
65 Res = skipEmpty(Iter, M);
69 Module::const_global_iterator Iter(static_cast<const GlobalVariable*>(GV));
71 Res = skipEmpty(Iter, M);
75 Module::const_alias_iterator Iter(static_cast<const GlobalAlias*>(GV));
77 Res = skipEmpty(Iter, M);
81 llvm_unreachable("Invalid symbol reference");
87 error_code IRObjectFile::printSymbolName(raw_ostream &OS,
88 DataRefImpl Symb) const {
89 // FIXME: This should use the Mangler.
90 const GlobalValue &GV = getGV(Symb);
92 return object_error::success;
95 uint32_t IRObjectFile::getSymbolFlags(DataRefImpl Symb) const {
96 const GlobalValue &GV = getGV(Symb);
98 uint32_t Res = BasicSymbolRef::SF_None;
99 if (GV.isDeclaration() || GV.hasAvailableExternallyLinkage())
100 Res |= BasicSymbolRef::SF_Undefined;
101 if (GV.hasPrivateLinkage() || GV.hasLinkerPrivateLinkage() ||
102 GV.hasLinkerPrivateWeakLinkage())
103 Res |= BasicSymbolRef::SF_FormatSpecific;
104 if (!GV.hasLocalLinkage())
105 Res |= BasicSymbolRef::SF_Global;
106 if (GV.hasCommonLinkage())
107 Res |= BasicSymbolRef::SF_Common;
108 if (GV.hasLinkOnceLinkage() || GV.hasWeakLinkage())
109 Res |= BasicSymbolRef::SF_Weak;
114 const GlobalValue &IRObjectFile::getSymbolGV(DataRefImpl Symb) const {
115 const GlobalValue &GV = getGV(Symb);
119 basic_symbol_iterator IRObjectFile::symbol_begin_impl() const {
120 Module::const_iterator I = M->begin();
122 Ret.p = skipEmpty(I, *M);
123 return basic_symbol_iterator(BasicSymbolRef(Ret, this));
126 basic_symbol_iterator IRObjectFile::symbol_end_impl() const {
129 return basic_symbol_iterator(BasicSymbolRef(Ret, this));
132 ErrorOr<SymbolicFile *> llvm::object::SymbolicFile::createIRObjectFile(
133 MemoryBuffer *Object, LLVMContext &Context, bool BufferOwned) {
135 OwningPtr<IRObjectFile> Ret(
136 new IRObjectFile(Object, EC, Context, BufferOwned));