1f12b8282SRafael Espindola //===- IRObjectFile.cpp - IR object file implementation ---------*- C++ -*-===//
2f12b8282SRafael Espindola //
3f12b8282SRafael Espindola //                     The LLVM Compiler Infrastructure
4f12b8282SRafael Espindola //
5f12b8282SRafael Espindola // This file is distributed under the University of Illinois Open Source
6f12b8282SRafael Espindola // License. See LICENSE.TXT for details.
7f12b8282SRafael Espindola //
8f12b8282SRafael Espindola //===----------------------------------------------------------------------===//
9f12b8282SRafael Espindola //
10f12b8282SRafael Espindola // Part of the IRObjectFile class implementation.
11f12b8282SRafael Espindola //
12f12b8282SRafael Espindola //===----------------------------------------------------------------------===//
13f12b8282SRafael Espindola 
14f12b8282SRafael Espindola #include "llvm/Bitcode/ReaderWriter.h"
15f12b8282SRafael Espindola #include "llvm/IR/LLVMContext.h"
16a51f0f83SRafael Espindola #include "llvm/IR/Mangler.h"
17f12b8282SRafael Espindola #include "llvm/IR/Module.h"
18f12b8282SRafael Espindola #include "llvm/Object/IRObjectFile.h"
1923f04061SRafael Espindola #include "llvm/Support/raw_ostream.h"
20f12b8282SRafael Espindola using namespace llvm;
21f12b8282SRafael Espindola using namespace object;
22f12b8282SRafael Espindola 
23db4ed0bdSRafael Espindola IRObjectFile::IRObjectFile(MemoryBuffer *Object, std::error_code &EC,
24f12b8282SRafael Espindola                            LLVMContext &Context, bool BufferOwned)
25f12b8282SRafael Espindola     : SymbolicFile(Binary::ID_IR, Object, BufferOwned) {
26*8af5cb2cSRafael Espindola   ErrorOr<Module *> MOrErr =
27*8af5cb2cSRafael Espindola       getLazyBitcodeModule(Object, Context, /*BufferOwned*/ false);
28f12b8282SRafael Espindola   if ((EC = MOrErr.getError()))
29f12b8282SRafael Espindola     return;
30f12b8282SRafael Espindola 
31f12b8282SRafael Espindola   M.reset(MOrErr.get());
32a51f0f83SRafael Espindola 
33a51f0f83SRafael Espindola   // If we have a DataLayout, setup a mangler.
34a51f0f83SRafael Espindola   const DataLayout *DL = M->getDataLayout();
35a51f0f83SRafael Espindola   if (!DL)
36a51f0f83SRafael Espindola     return;
37a51f0f83SRafael Espindola 
38a51f0f83SRafael Espindola   Mang.reset(new Mangler(DL));
39f12b8282SRafael Espindola }
40f12b8282SRafael Espindola 
41f12b8282SRafael Espindola static const GlobalValue &getGV(DataRefImpl &Symb) {
42f12b8282SRafael Espindola   return *reinterpret_cast<GlobalValue*>(Symb.p & ~uintptr_t(3));
43f12b8282SRafael Espindola }
44f12b8282SRafael Espindola 
45f12b8282SRafael Espindola static uintptr_t skipEmpty(Module::const_alias_iterator I, const Module &M) {
46f12b8282SRafael Espindola   if (I == M.alias_end())
47f12b8282SRafael Espindola     return 3;
48f12b8282SRafael Espindola   const GlobalValue *GV = &*I;
49f12b8282SRafael Espindola   return reinterpret_cast<uintptr_t>(GV) | 2;
50f12b8282SRafael Espindola }
51f12b8282SRafael Espindola 
52f12b8282SRafael Espindola static uintptr_t skipEmpty(Module::const_global_iterator I, const Module &M) {
53f12b8282SRafael Espindola   if (I == M.global_end())
54f12b8282SRafael Espindola     return skipEmpty(M.alias_begin(), M);
55f12b8282SRafael Espindola   const GlobalValue *GV = &*I;
56f12b8282SRafael Espindola   return reinterpret_cast<uintptr_t>(GV) | 1;
57f12b8282SRafael Espindola }
58f12b8282SRafael Espindola 
59f12b8282SRafael Espindola static uintptr_t skipEmpty(Module::const_iterator I, const Module &M) {
60f12b8282SRafael Espindola   if (I == M.end())
61f12b8282SRafael Espindola     return skipEmpty(M.global_begin(), M);
62f12b8282SRafael Espindola   const GlobalValue *GV = &*I;
63f12b8282SRafael Espindola   return reinterpret_cast<uintptr_t>(GV) | 0;
64f12b8282SRafael Espindola }
65f12b8282SRafael Espindola 
66f12b8282SRafael Espindola void IRObjectFile::moveSymbolNext(DataRefImpl &Symb) const {
67f12b8282SRafael Espindola   const GlobalValue *GV = &getGV(Symb);
68f12b8282SRafael Espindola   const Module &M = *GV->getParent();
69f12b8282SRafael Espindola   uintptr_t Res;
70f12b8282SRafael Espindola   switch (Symb.p & 3) {
71f12b8282SRafael Espindola   case 0: {
72f12b8282SRafael Espindola     Module::const_iterator Iter(static_cast<const Function*>(GV));
73f12b8282SRafael Espindola     ++Iter;
74f12b8282SRafael Espindola     Res = skipEmpty(Iter, M);
75f12b8282SRafael Espindola     break;
76f12b8282SRafael Espindola   }
77f12b8282SRafael Espindola   case 1: {
78f12b8282SRafael Espindola     Module::const_global_iterator Iter(static_cast<const GlobalVariable*>(GV));
79f12b8282SRafael Espindola     ++Iter;
80f12b8282SRafael Espindola     Res = skipEmpty(Iter, M);
81f12b8282SRafael Espindola     break;
82f12b8282SRafael Espindola   }
83f12b8282SRafael Espindola   case 2: {
84f12b8282SRafael Espindola     Module::const_alias_iterator Iter(static_cast<const GlobalAlias*>(GV));
85f12b8282SRafael Espindola     ++Iter;
86f12b8282SRafael Espindola     Res = skipEmpty(Iter, M);
87f12b8282SRafael Espindola     break;
88f12b8282SRafael Espindola   }
89f12b8282SRafael Espindola   case 3:
90f12b8282SRafael Espindola     llvm_unreachable("Invalid symbol reference");
91f12b8282SRafael Espindola   }
92f12b8282SRafael Espindola 
93f12b8282SRafael Espindola   Symb.p = Res;
94f12b8282SRafael Espindola }
95f12b8282SRafael Espindola 
96db4ed0bdSRafael Espindola std::error_code IRObjectFile::printSymbolName(raw_ostream &OS,
97f12b8282SRafael Espindola                                               DataRefImpl Symb) const {
98f12b8282SRafael Espindola   const GlobalValue &GV = getGV(Symb);
99a51f0f83SRafael Espindola 
100a51f0f83SRafael Espindola   if (Mang)
101a51f0f83SRafael Espindola     Mang->getNameWithPrefix(OS, &GV, false);
102a51f0f83SRafael Espindola   else
103f12b8282SRafael Espindola     OS << GV.getName();
104a51f0f83SRafael Espindola 
105f12b8282SRafael Espindola   return object_error::success;
106f12b8282SRafael Espindola }
107f12b8282SRafael Espindola 
108*8af5cb2cSRafael Espindola static bool isDeclaration(const GlobalValue &V) {
109*8af5cb2cSRafael Espindola   if (V.hasAvailableExternallyLinkage())
110*8af5cb2cSRafael Espindola     return true;
111*8af5cb2cSRafael Espindola 
112*8af5cb2cSRafael Espindola   if (V.isMaterializable())
113*8af5cb2cSRafael Espindola     return false;
114*8af5cb2cSRafael Espindola 
115*8af5cb2cSRafael Espindola   return V.isDeclaration();
116*8af5cb2cSRafael Espindola }
117*8af5cb2cSRafael Espindola 
118f12b8282SRafael Espindola uint32_t IRObjectFile::getSymbolFlags(DataRefImpl Symb) const {
119f12b8282SRafael Espindola   const GlobalValue &GV = getGV(Symb);
120f12b8282SRafael Espindola 
121f12b8282SRafael Espindola   uint32_t Res = BasicSymbolRef::SF_None;
122*8af5cb2cSRafael Espindola   if (isDeclaration(GV))
123f12b8282SRafael Espindola     Res |= BasicSymbolRef::SF_Undefined;
1242fb5bc33SRafael Espindola   if (GV.hasPrivateLinkage())
125f12b8282SRafael Espindola     Res |= BasicSymbolRef::SF_FormatSpecific;
126f12b8282SRafael Espindola   if (!GV.hasLocalLinkage())
127f12b8282SRafael Espindola     Res |= BasicSymbolRef::SF_Global;
128f12b8282SRafael Espindola   if (GV.hasCommonLinkage())
129f12b8282SRafael Espindola     Res |= BasicSymbolRef::SF_Common;
130f12b8282SRafael Espindola   if (GV.hasLinkOnceLinkage() || GV.hasWeakLinkage())
131f12b8282SRafael Espindola     Res |= BasicSymbolRef::SF_Weak;
132f12b8282SRafael Espindola 
133f12b8282SRafael Espindola   return Res;
134f12b8282SRafael Espindola }
135f12b8282SRafael Espindola 
136f12b8282SRafael Espindola const GlobalValue &IRObjectFile::getSymbolGV(DataRefImpl Symb) const {
137f12b8282SRafael Espindola   const GlobalValue &GV = getGV(Symb);
138f12b8282SRafael Espindola   return GV;
139f12b8282SRafael Espindola }
140f12b8282SRafael Espindola 
141f12b8282SRafael Espindola basic_symbol_iterator IRObjectFile::symbol_begin_impl() const {
142f12b8282SRafael Espindola   Module::const_iterator I = M->begin();
143f12b8282SRafael Espindola   DataRefImpl Ret;
144f12b8282SRafael Espindola   Ret.p = skipEmpty(I, *M);
145f12b8282SRafael Espindola   return basic_symbol_iterator(BasicSymbolRef(Ret, this));
146f12b8282SRafael Espindola }
147f12b8282SRafael Espindola 
148f12b8282SRafael Espindola basic_symbol_iterator IRObjectFile::symbol_end_impl() const {
149f12b8282SRafael Espindola   DataRefImpl Ret;
150f12b8282SRafael Espindola   Ret.p = 3;
151f12b8282SRafael Espindola   return basic_symbol_iterator(BasicSymbolRef(Ret, this));
152f12b8282SRafael Espindola }
153f12b8282SRafael Espindola 
154f12b8282SRafael Espindola ErrorOr<SymbolicFile *> llvm::object::SymbolicFile::createIRObjectFile(
155f12b8282SRafael Espindola     MemoryBuffer *Object, LLVMContext &Context, bool BufferOwned) {
156db4ed0bdSRafael Espindola   std::error_code EC;
15756440fd8SAhmed Charles   std::unique_ptr<IRObjectFile> Ret(
158f12b8282SRafael Espindola       new IRObjectFile(Object, EC, Context, BufferOwned));
159f12b8282SRafael Espindola   if (EC)
160f12b8282SRafael Espindola     return EC;
16196c9d95fSAhmed Charles   return Ret.release();
162f12b8282SRafael Espindola }
163