blob: a8aba26c5aef81a7a224cf3aca125af7e0b91659 [file] [log] [blame]
Rafael Espindolaf12b8282014-02-21 20:10:59 +00001//===- IRObjectFile.cpp - IR object file implementation ---------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Part of the IRObjectFile class implementation.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Bitcode/ReaderWriter.h"
15#include "llvm/IR/LLVMContext.h"
Rafael Espindolaa51f0f82014-02-28 02:17:23 +000016#include "llvm/IR/Mangler.h"
Rafael Espindolaf12b8282014-02-21 20:10:59 +000017#include "llvm/IR/Module.h"
18#include "llvm/Object/IRObjectFile.h"
Rafael Espindola23f04062014-02-21 20:21:55 +000019#include "llvm/Support/raw_ostream.h"
Rafael Espindolaf12b8282014-02-21 20:10:59 +000020using namespace llvm;
21using namespace object;
22
23IRObjectFile::IRObjectFile(MemoryBuffer *Object, error_code &EC,
24 LLVMContext &Context, bool BufferOwned)
25 : SymbolicFile(Binary::ID_IR, Object, BufferOwned) {
26 ErrorOr<Module*> MOrErr = parseBitcodeFile(Object, Context);
27 if ((EC = MOrErr.getError()))
28 return;
29
30 M.reset(MOrErr.get());
Rafael Espindolaa51f0f82014-02-28 02:17:23 +000031
32 // If we have a DataLayout, setup a mangler.
33 const DataLayout *DL = M->getDataLayout();
34 if (!DL)
35 return;
36
37 Mang.reset(new Mangler(DL));
Rafael Espindolaf12b8282014-02-21 20:10:59 +000038}
39
40static const GlobalValue &getGV(DataRefImpl &Symb) {
41 return *reinterpret_cast<GlobalValue*>(Symb.p & ~uintptr_t(3));
42}
43
44static uintptr_t skipEmpty(Module::const_alias_iterator I, const Module &M) {
45 if (I == M.alias_end())
46 return 3;
47 const GlobalValue *GV = &*I;
48 return reinterpret_cast<uintptr_t>(GV) | 2;
49}
50
51static uintptr_t skipEmpty(Module::const_global_iterator I, const Module &M) {
52 if (I == M.global_end())
53 return skipEmpty(M.alias_begin(), M);
54 const GlobalValue *GV = &*I;
55 return reinterpret_cast<uintptr_t>(GV) | 1;
56}
57
58static uintptr_t skipEmpty(Module::const_iterator I, const Module &M) {
59 if (I == M.end())
60 return skipEmpty(M.global_begin(), M);
61 const GlobalValue *GV = &*I;
62 return reinterpret_cast<uintptr_t>(GV) | 0;
63}
64
65void IRObjectFile::moveSymbolNext(DataRefImpl &Symb) const {
66 const GlobalValue *GV = &getGV(Symb);
67 const Module &M = *GV->getParent();
68 uintptr_t Res;
69 switch (Symb.p & 3) {
70 case 0: {
71 Module::const_iterator Iter(static_cast<const Function*>(GV));
72 ++Iter;
73 Res = skipEmpty(Iter, M);
74 break;
75 }
76 case 1: {
77 Module::const_global_iterator Iter(static_cast<const GlobalVariable*>(GV));
78 ++Iter;
79 Res = skipEmpty(Iter, M);
80 break;
81 }
82 case 2: {
83 Module::const_alias_iterator Iter(static_cast<const GlobalAlias*>(GV));
84 ++Iter;
85 Res = skipEmpty(Iter, M);
86 break;
87 }
88 case 3:
89 llvm_unreachable("Invalid symbol reference");
90 }
91
92 Symb.p = Res;
93}
94
95error_code IRObjectFile::printSymbolName(raw_ostream &OS,
96 DataRefImpl Symb) const {
Rafael Espindolaf12b8282014-02-21 20:10:59 +000097 const GlobalValue &GV = getGV(Symb);
Rafael Espindolaa51f0f82014-02-28 02:17:23 +000098
99 if (Mang)
100 Mang->getNameWithPrefix(OS, &GV, false);
101 else
102 OS << GV.getName();
103
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000104 return object_error::success;
105}
106
107uint32_t IRObjectFile::getSymbolFlags(DataRefImpl Symb) const {
108 const GlobalValue &GV = getGV(Symb);
109
110 uint32_t Res = BasicSymbolRef::SF_None;
111 if (GV.isDeclaration() || GV.hasAvailableExternallyLinkage())
112 Res |= BasicSymbolRef::SF_Undefined;
Rafael Espindola2fb5bc32014-03-13 23:18:37 +0000113 if (GV.hasPrivateLinkage())
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000114 Res |= BasicSymbolRef::SF_FormatSpecific;
115 if (!GV.hasLocalLinkage())
116 Res |= BasicSymbolRef::SF_Global;
117 if (GV.hasCommonLinkage())
118 Res |= BasicSymbolRef::SF_Common;
119 if (GV.hasLinkOnceLinkage() || GV.hasWeakLinkage())
120 Res |= BasicSymbolRef::SF_Weak;
121
122 return Res;
123}
124
125const GlobalValue &IRObjectFile::getSymbolGV(DataRefImpl Symb) const {
126 const GlobalValue &GV = getGV(Symb);
127 return GV;
128}
129
130basic_symbol_iterator IRObjectFile::symbol_begin_impl() const {
131 Module::const_iterator I = M->begin();
132 DataRefImpl Ret;
133 Ret.p = skipEmpty(I, *M);
134 return basic_symbol_iterator(BasicSymbolRef(Ret, this));
135}
136
137basic_symbol_iterator IRObjectFile::symbol_end_impl() const {
138 DataRefImpl Ret;
139 Ret.p = 3;
140 return basic_symbol_iterator(BasicSymbolRef(Ret, this));
141}
142
143ErrorOr<SymbolicFile *> llvm::object::SymbolicFile::createIRObjectFile(
144 MemoryBuffer *Object, LLVMContext &Context, bool BufferOwned) {
145 error_code EC;
Ahmed Charles56440fd2014-03-06 05:51:42 +0000146 std::unique_ptr<IRObjectFile> Ret(
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000147 new IRObjectFile(Object, EC, Context, BufferOwned));
148 if (EC)
149 return EC;
Ahmed Charles96c9d952014-03-05 10:19:29 +0000150 return Ret.release();
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000151}