blob: 65c495b6699698445381462cc0eff9a36e2b236f [file] [log] [blame]
Chris Lattner1314b992007-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 Lattner1314b992007-04-22 06:23:29 +000017#include "llvm/ModuleProvider.h"
Chris Lattner1663cca2007-04-24 05:48:56 +000018#include "llvm/Type.h"
19#include "llvm/User.h"
Chris Lattner51ffe7c2007-05-01 04:59:48 +000020#include "llvm/Bitcode/BitstreamReader.h"
Chris Lattner362b4a12007-04-23 01:01:37 +000021#include "llvm/Bitcode/LLVMBitCodes.h"
Chris Lattner51ffe7c2007-05-01 04:59:48 +000022#include "llvm/ADT/DenseMap.h"
Chris Lattner1314b992007-04-22 06:23:29 +000023#include <vector>
24
25namespace llvm {
Chris Lattner6694f602007-04-29 07:54:31 +000026 class MemoryBuffer;
Chris Lattner1663cca2007-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 Lattner831d4202007-04-26 03:27:58 +000046 void shrinkTo(unsigned N) {
Chris Lattner85b7b402007-05-01 05:52:21 +000047 assert(N <= NumOperands && "Invalid shrinkTo request!");
Chris Lattner831d4202007-04-26 03:27:58 +000048 Uses.resize(N);
49 NumOperands = N;
50 }
Chris Lattner1663cca2007-04-24 05:48:56 +000051 virtual void print(std::ostream&) const {}
52
53 Constant *getConstantFwdRef(unsigned Idx, const Type *Ty);
54 void initVal(unsigned Idx, Value *V) {
55 assert(Uses[Idx] == 0 && "Cannot init an already init'd Use!");
56 Uses[Idx].init(V, this);
57 }
58};
59
Chris Lattner1314b992007-04-22 06:23:29 +000060
61class BitcodeReader : public ModuleProvider {
Chris Lattner6694f602007-04-29 07:54:31 +000062 MemoryBuffer *Buffer;
Chris Lattner51ffe7c2007-05-01 04:59:48 +000063 BitstreamReader Stream;
64
Chris Lattner1314b992007-04-22 06:23:29 +000065 const char *ErrorString;
66
67 std::vector<PATypeHolder> TypeList;
Chris Lattner1663cca2007-04-24 05:48:56 +000068 BitcodeReaderValueList ValueList;
Chris Lattnerfbc1d332007-04-24 03:30:34 +000069 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
Chris Lattner44c17072007-04-26 02:46:40 +000070 std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits;
Chris Lattner51ffe7c2007-05-01 04:59:48 +000071
Chris Lattner85b7b402007-05-01 05:52:21 +000072 /// FunctionBBs - While parsing a function body, this is a list of the basic
73 /// blocks for the function.
74 std::vector<BasicBlock*> FunctionBBs;
75
Chris Lattner51ffe7c2007-05-01 04:59:48 +000076 // When reading the module header, this list is populated with functions that
77 // have bodies later in the file.
78 std::vector<Function*> FunctionsWithBodies;
79
80 // After the module header has been read, the FunctionsWithBodies list is
81 // reversed. This keeps track of whether we've done this yet.
82 bool HasReversedFunctionsWithBodies;
83
84 /// DeferredFunctionInfo - When function bodies are initially scanned, this
85 /// map contains info about where to find deferred function body (in the
86 /// stream) and what linkage the original function had.
87 DenseMap<Function*, std::pair<uint64_t, unsigned> > DeferredFunctionInfo;
Chris Lattner1314b992007-04-22 06:23:29 +000088public:
Chris Lattner51ffe7c2007-05-01 04:59:48 +000089 BitcodeReader(MemoryBuffer *buffer) : Buffer(buffer), ErrorString(0) {
90 HasReversedFunctionsWithBodies = false;
91 }
Chris Lattner6694f602007-04-29 07:54:31 +000092 ~BitcodeReader();
Chris Lattner1314b992007-04-22 06:23:29 +000093
Chris Lattner6694f602007-04-29 07:54:31 +000094
95 /// releaseMemoryBuffer - This causes the reader to completely forget about
96 /// the memory buffer it contains, which prevents the buffer from being
97 /// destroyed when it is deleted.
98 void releaseMemoryBuffer() {
99 Buffer = 0;
100 }
Chris Lattner1314b992007-04-22 06:23:29 +0000101
Chris Lattner51ffe7c2007-05-01 04:59:48 +0000102 virtual bool materializeFunction(Function *F, std::string *ErrInfo = 0);
Chris Lattner85b7b402007-05-01 05:52:21 +0000103 virtual Module *materializeModule(std::string *ErrInfo = 0);
Chris Lattner1314b992007-04-22 06:23:29 +0000104
105 bool Error(const char *Str) {
106 ErrorString = Str;
107 return true;
108 }
109 const char *getErrorString() const { return ErrorString; }
110
111 /// @brief Main interface to parsing a bitcode buffer.
112 /// @returns true if an error occurred.
Chris Lattner6694f602007-04-29 07:54:31 +0000113 bool ParseBitcode();
Chris Lattner1314b992007-04-22 06:23:29 +0000114private:
115 const Type *getTypeByID(unsigned ID, bool isTypeTable = false);
116
Chris Lattner48a8de32007-05-01 05:01:34 +0000117 bool ParseModule(const std::string &ModuleID);
118 bool ParseTypeTable();
119 bool ParseTypeSymbolTable();
120 bool ParseValueSymbolTable();
121 bool ParseConstants();
Chris Lattner85b7b402007-05-01 05:52:21 +0000122 bool RememberAndSkipFunctionBody();
123 bool ParseFunctionBody(Function *F);
Chris Lattner44c17072007-04-26 02:46:40 +0000124 bool ResolveGlobalAndAliasInits();
Chris Lattner1314b992007-04-22 06:23:29 +0000125};
126
127} // End llvm namespace
128
129#endif