blob: 3935ebbfe099855d6547c7f0d39aabbc6a425415 [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 Lattner47f96bf2007-04-23 01:01:37 +000020#include "llvm/Bitcode/LLVMBitCodes.h"
Chris Lattnercaee0dc2007-04-22 06:23:29 +000021#include <vector>
22
23namespace llvm {
24 class BitstreamReader;
Chris Lattner522b7b12007-04-24 05:48:56 +000025
26class BitcodeReaderValueList : public User {
27 std::vector<Use> Uses;
28public:
29 BitcodeReaderValueList() : User(Type::VoidTy, Value::ArgumentVal, 0, 0) {}
30
31 // vector compatibility methods
32 unsigned size() const { return getNumOperands(); }
33 void push_back(Value *V) {
34 Uses.push_back(Use(V, this));
35 OperandList = &Uses[0];
36 ++NumOperands;
37 }
38
39 Value *operator[](unsigned i) const { return getOperand(i); }
40
41 Value *back() const { return Uses.back(); }
42 void pop_back() { Uses.pop_back(); --NumOperands; }
43 bool empty() const { return NumOperands == 0; }
44 virtual void print(std::ostream&) const {}
45
46 Constant *getConstantFwdRef(unsigned Idx, const Type *Ty);
47 void initVal(unsigned Idx, Value *V) {
48 assert(Uses[Idx] == 0 && "Cannot init an already init'd Use!");
49 Uses[Idx].init(V, this);
50 }
51};
52
Chris Lattnercaee0dc2007-04-22 06:23:29 +000053
54class BitcodeReader : public ModuleProvider {
55 const char *ErrorString;
56
57 std::vector<PATypeHolder> TypeList;
Chris Lattner522b7b12007-04-24 05:48:56 +000058 BitcodeReaderValueList ValueList;
Chris Lattnere16504e2007-04-24 03:30:34 +000059 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
Chris Lattnercaee0dc2007-04-22 06:23:29 +000060public:
Chris Lattnerf66d20d2007-04-24 18:15:21 +000061 BitcodeReader() : ErrorString(0) {}
Chris Lattnercaee0dc2007-04-22 06:23:29 +000062 virtual ~BitcodeReader() {}
63
64 virtual void FreeState() {}
65
66 virtual bool materializeFunction(Function *F, std::string *ErrInfo = 0) {
67 // FIXME: TODO
68 return false;
69 }
70
71 virtual Module *materializeModule(std::string *ErrInfo = 0) {
72 // FIXME: TODO
73 //if (ParseAllFunctionBodies(ErrMsg))
74 // return 0;
75 return TheModule;
76 }
77
78 bool Error(const char *Str) {
79 ErrorString = Str;
80 return true;
81 }
82 const char *getErrorString() const { return ErrorString; }
83
84 /// @brief Main interface to parsing a bitcode buffer.
85 /// @returns true if an error occurred.
86 bool ParseBitcode(unsigned char *Buf, unsigned Length,
87 const std::string &ModuleID);
88private:
89 const Type *getTypeByID(unsigned ID, bool isTypeTable = false);
90
91 bool ParseModule(BitstreamReader &Stream, const std::string &ModuleID);
92 bool ParseTypeTable(BitstreamReader &Stream);
93 bool ParseTypeSymbolTable(BitstreamReader &Stream);
Chris Lattner0b2482a2007-04-23 21:26:05 +000094 bool ParseValueSymbolTable(BitstreamReader &Stream);
Chris Lattnere16504e2007-04-24 03:30:34 +000095 bool ParseConstants(BitstreamReader &Stream);
Chris Lattnercaee0dc2007-04-22 06:23:29 +000096};
97
98} // End llvm namespace
99
100#endif