blob: 470758f0e5585dda6f5d80a47e0cbb2ccb910ced [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 {
26 class BitstreamReader;
Chris Lattner6694f602007-04-29 07:54:31 +000027 class MemoryBuffer;
Chris Lattner1663cca2007-04-24 05:48:56 +000028
29class BitcodeReaderValueList : public User {
30 std::vector<Use> Uses;
31public:
32 BitcodeReaderValueList() : User(Type::VoidTy, Value::ArgumentVal, 0, 0) {}
33
34 // vector compatibility methods
35 unsigned size() const { return getNumOperands(); }
36 void push_back(Value *V) {
37 Uses.push_back(Use(V, this));
38 OperandList = &Uses[0];
39 ++NumOperands;
40 }
41
42 Value *operator[](unsigned i) const { return getOperand(i); }
43
44 Value *back() const { return Uses.back(); }
45 void pop_back() { Uses.pop_back(); --NumOperands; }
46 bool empty() const { return NumOperands == 0; }
Chris Lattner831d4202007-04-26 03:27:58 +000047 void shrinkTo(unsigned N) {
48 assert(N < NumOperands && "Invalid shrinkTo request!");
49 Uses.resize(N);
50 NumOperands = N;
51 }
Chris Lattner1663cca2007-04-24 05:48:56 +000052 virtual void print(std::ostream&) const {}
53
54 Constant *getConstantFwdRef(unsigned Idx, const Type *Ty);
55 void initVal(unsigned Idx, Value *V) {
56 assert(Uses[Idx] == 0 && "Cannot init an already init'd Use!");
57 Uses[Idx].init(V, this);
58 }
59};
60
Chris Lattner1314b992007-04-22 06:23:29 +000061
62class BitcodeReader : public ModuleProvider {
Chris Lattner6694f602007-04-29 07:54:31 +000063 MemoryBuffer *Buffer;
Chris Lattner51ffe7c2007-05-01 04:59:48 +000064 BitstreamReader Stream;
65
Chris Lattner1314b992007-04-22 06:23:29 +000066 const char *ErrorString;
67
68 std::vector<PATypeHolder> TypeList;
Chris Lattner1663cca2007-04-24 05:48:56 +000069 BitcodeReaderValueList ValueList;
Chris Lattnerfbc1d332007-04-24 03:30:34 +000070 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
Chris Lattner44c17072007-04-26 02:46:40 +000071 std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits;
Chris Lattner51ffe7c2007-05-01 04:59:48 +000072
73 // When reading the module header, this list is populated with functions that
74 // have bodies later in the file.
75 std::vector<Function*> FunctionsWithBodies;
76
77 // After the module header has been read, the FunctionsWithBodies list is
78 // reversed. This keeps track of whether we've done this yet.
79 bool HasReversedFunctionsWithBodies;
80
81 /// DeferredFunctionInfo - When function bodies are initially scanned, this
82 /// map contains info about where to find deferred function body (in the
83 /// stream) and what linkage the original function had.
84 DenseMap<Function*, std::pair<uint64_t, unsigned> > DeferredFunctionInfo;
Chris Lattner1314b992007-04-22 06:23:29 +000085public:
Chris Lattner51ffe7c2007-05-01 04:59:48 +000086 BitcodeReader(MemoryBuffer *buffer) : Buffer(buffer), ErrorString(0) {
87 HasReversedFunctionsWithBodies = false;
88 }
Chris Lattner6694f602007-04-29 07:54:31 +000089 ~BitcodeReader();
Chris Lattner1314b992007-04-22 06:23:29 +000090
Chris Lattner6694f602007-04-29 07:54:31 +000091
92 /// releaseMemoryBuffer - This causes the reader to completely forget about
93 /// the memory buffer it contains, which prevents the buffer from being
94 /// destroyed when it is deleted.
95 void releaseMemoryBuffer() {
96 Buffer = 0;
97 }
Chris Lattner1314b992007-04-22 06:23:29 +000098
Chris Lattner51ffe7c2007-05-01 04:59:48 +000099 virtual bool materializeFunction(Function *F, std::string *ErrInfo = 0);
Chris Lattner1314b992007-04-22 06:23:29 +0000100
101 virtual Module *materializeModule(std::string *ErrInfo = 0) {
102 // FIXME: TODO
103 //if (ParseAllFunctionBodies(ErrMsg))
104 // return 0;
105 return TheModule;
106 }
107
108 bool Error(const char *Str) {
109 ErrorString = Str;
110 return true;
111 }
112 const char *getErrorString() const { return ErrorString; }
113
114 /// @brief Main interface to parsing a bitcode buffer.
115 /// @returns true if an error occurred.
Chris Lattner6694f602007-04-29 07:54:31 +0000116 bool ParseBitcode();
Chris Lattner1314b992007-04-22 06:23:29 +0000117private:
118 const Type *getTypeByID(unsigned ID, bool isTypeTable = false);
119
120 bool ParseModule(BitstreamReader &Stream, const std::string &ModuleID);
121 bool ParseTypeTable(BitstreamReader &Stream);
122 bool ParseTypeSymbolTable(BitstreamReader &Stream);
Chris Lattnerccaa4482007-04-23 21:26:05 +0000123 bool ParseValueSymbolTable(BitstreamReader &Stream);
Chris Lattnerfbc1d332007-04-24 03:30:34 +0000124 bool ParseConstants(BitstreamReader &Stream);
Chris Lattner51ffe7c2007-05-01 04:59:48 +0000125 bool ParseFunction(BitstreamReader &Stream);
Chris Lattner44c17072007-04-26 02:46:40 +0000126 bool ResolveGlobalAndAliasInits();
Chris Lattner1314b992007-04-22 06:23:29 +0000127};
128
129} // End llvm namespace
130
131#endif