blob: e537310ecaf7f0fb7972e26dfab0e564ad8f0979 [file] [log] [blame]
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001//===- BitcodeReader.h - Internal BitcodeReader impl ------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This header defines the BitcodeReader class.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef BITCODE_READER_H
15#define BITCODE_READER_H
16
Chris Lattnercaee0dc2007-04-22 06:23:29 +000017#include "llvm/ModuleProvider.h"
Chris Lattner522b7b12007-04-24 05:48:56 +000018#include "llvm/Type.h"
19#include "llvm/User.h"
Chris Lattner48f84872007-05-01 04:59:48 +000020#include "llvm/Bitcode/BitstreamReader.h"
Chris Lattner47f96bf2007-04-23 01:01:37 +000021#include "llvm/Bitcode/LLVMBitCodes.h"
Chris Lattner48f84872007-05-01 04:59:48 +000022#include "llvm/ADT/DenseMap.h"
Chris Lattnercaee0dc2007-04-22 06:23:29 +000023#include <vector>
24
25namespace llvm {
Chris Lattnerc453f762007-04-29 07:54:31 +000026 class MemoryBuffer;
Chris Lattner522b7b12007-04-24 05:48:56 +000027
28class BitcodeReaderValueList : public User {
29 std::vector<Use> Uses;
30public:
31 BitcodeReaderValueList() : User(Type::VoidTy, Value::ArgumentVal, 0, 0) {}
32
33 // vector compatibility methods
34 unsigned size() const { return getNumOperands(); }
35 void push_back(Value *V) {
36 Uses.push_back(Use(V, this));
37 OperandList = &Uses[0];
38 ++NumOperands;
39 }
40
41 Value *operator[](unsigned i) const { return getOperand(i); }
42
43 Value *back() const { return Uses.back(); }
44 void pop_back() { Uses.pop_back(); --NumOperands; }
45 bool empty() const { return NumOperands == 0; }
Chris Lattner198f34a2007-04-26 03:27:58 +000046 void shrinkTo(unsigned N) {
Chris Lattner980e5aa2007-05-01 05:52:21 +000047 assert(N <= NumOperands && "Invalid shrinkTo request!");
Chris Lattner198f34a2007-04-26 03:27:58 +000048 Uses.resize(N);
49 NumOperands = N;
50 }
Chris Lattner522b7b12007-04-24 05:48:56 +000051 virtual void print(std::ostream&) const {}
52
53 Constant *getConstantFwdRef(unsigned Idx, const Type *Ty);
Chris Lattnera7c49aa2007-05-01 07:01:57 +000054 Value *getValueFwdRef(unsigned Idx, const Type *Ty);
55
56 void AssignValue(Value *V, unsigned Idx) {
57 if (Idx == size()) {
58 push_back(V);
59 } else if (Value *OldV = getOperand(Idx)) {
60 // If there was a forward reference to this value, replace it.
61 setOperand(Idx, V);
62 OldV->replaceAllUsesWith(V);
63 delete OldV;
64 } else {
65 initVal(Idx, V);
66 }
67 }
68
69private:
Chris Lattner522b7b12007-04-24 05:48:56 +000070 void initVal(unsigned Idx, Value *V) {
71 assert(Uses[Idx] == 0 && "Cannot init an already init'd Use!");
72 Uses[Idx].init(V, this);
73 }
74};
75
Chris Lattnercaee0dc2007-04-22 06:23:29 +000076
77class BitcodeReader : public ModuleProvider {
Chris Lattnerc453f762007-04-29 07:54:31 +000078 MemoryBuffer *Buffer;
Chris Lattner48f84872007-05-01 04:59:48 +000079 BitstreamReader Stream;
80
Chris Lattnercaee0dc2007-04-22 06:23:29 +000081 const char *ErrorString;
82
83 std::vector<PATypeHolder> TypeList;
Chris Lattner522b7b12007-04-24 05:48:56 +000084 BitcodeReaderValueList ValueList;
Chris Lattnere16504e2007-04-24 03:30:34 +000085 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
Chris Lattner07d98b42007-04-26 02:46:40 +000086 std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits;
Chris Lattner48f84872007-05-01 04:59:48 +000087
Chris Lattner980e5aa2007-05-01 05:52:21 +000088 /// FunctionBBs - While parsing a function body, this is a list of the basic
89 /// blocks for the function.
90 std::vector<BasicBlock*> FunctionBBs;
91
Chris Lattner48f84872007-05-01 04:59:48 +000092 // When reading the module header, this list is populated with functions that
93 // have bodies later in the file.
94 std::vector<Function*> FunctionsWithBodies;
95
96 // After the module header has been read, the FunctionsWithBodies list is
97 // reversed. This keeps track of whether we've done this yet.
98 bool HasReversedFunctionsWithBodies;
99
100 /// DeferredFunctionInfo - When function bodies are initially scanned, this
101 /// map contains info about where to find deferred function body (in the
102 /// stream) and what linkage the original function had.
103 DenseMap<Function*, std::pair<uint64_t, unsigned> > DeferredFunctionInfo;
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000104public:
Chris Lattner48f84872007-05-01 04:59:48 +0000105 BitcodeReader(MemoryBuffer *buffer) : Buffer(buffer), ErrorString(0) {
106 HasReversedFunctionsWithBodies = false;
107 }
Chris Lattnerc453f762007-04-29 07:54:31 +0000108 ~BitcodeReader();
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000109
Chris Lattnerc453f762007-04-29 07:54:31 +0000110
111 /// releaseMemoryBuffer - This causes the reader to completely forget about
112 /// the memory buffer it contains, which prevents the buffer from being
113 /// destroyed when it is deleted.
114 void releaseMemoryBuffer() {
115 Buffer = 0;
116 }
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000117
Chris Lattner48f84872007-05-01 04:59:48 +0000118 virtual bool materializeFunction(Function *F, std::string *ErrInfo = 0);
Chris Lattner980e5aa2007-05-01 05:52:21 +0000119 virtual Module *materializeModule(std::string *ErrInfo = 0);
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000120
121 bool Error(const char *Str) {
122 ErrorString = Str;
123 return true;
124 }
125 const char *getErrorString() const { return ErrorString; }
126
127 /// @brief Main interface to parsing a bitcode buffer.
128 /// @returns true if an error occurred.
Chris Lattnerc453f762007-04-29 07:54:31 +0000129 bool ParseBitcode();
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000130private:
131 const Type *getTypeByID(unsigned ID, bool isTypeTable = false);
Chris Lattnera7c49aa2007-05-01 07:01:57 +0000132 Value *getFnValueByID(unsigned ID, const Type *Ty) {
133 return ValueList.getValueFwdRef(ID, Ty);
134 }
Chris Lattnerf4c8e522007-05-02 05:46:45 +0000135 BasicBlock *getBasicBlock(unsigned ID) const {
136 if (ID >= FunctionBBs.size()) return 0; // Invalid ID
137 return FunctionBBs[ID];
138 }
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000139
Chris Lattner86697142007-05-01 05:01:34 +0000140 bool ParseModule(const std::string &ModuleID);
141 bool ParseTypeTable();
142 bool ParseTypeSymbolTable();
143 bool ParseValueSymbolTable();
144 bool ParseConstants();
Chris Lattner980e5aa2007-05-01 05:52:21 +0000145 bool RememberAndSkipFunctionBody();
146 bool ParseFunctionBody(Function *F);
Chris Lattner07d98b42007-04-26 02:46:40 +0000147 bool ResolveGlobalAndAliasInits();
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000148};
149
150} // End llvm namespace
151
152#endif