1 //===-- ReaderInternals.h - Definitions internal to the reader ---*- C++ -*--=//
3 // This header file defines various stuff that is used by the bytecode reader.
5 //===----------------------------------------------------------------------===//
7 #ifndef READER_INTERNALS_H
8 #define READER_INTERNALS_H
10 #include "llvm/Constant.h"
11 #include "llvm/DerivedTypes.h"
12 #include "llvm/Function.h"
13 #include "llvm/ModuleProvider.h"
14 #include "llvm/Bytecode/Primitives.h"
18 // Enable to trace to figure out what the heck is going on when parsing fails
19 //#define TRACE_LEVEL 10
21 #if TRACE_LEVEL // ByteCodeReading_TRACEr
22 #define BCR_TRACE(n, X) \
23 if (n < TRACE_LEVEL) std::cerr << std::string(n*2, ' ') << X
25 #define BCR_TRACE(n, X)
28 struct LazyFunctionInfo {
29 const unsigned char *Buf, *EndBuf;
30 unsigned FunctionSlot;
33 class BytecodeParser : public ModuleProvider {
34 BytecodeParser(const BytecodeParser &); // DO NOT IMPLEMENT
35 void operator=(const BytecodeParser &); // DO NOT IMPLEMENT
38 // Define this in case we don't see a ModuleGlobalInfo block.
39 FirstDerivedTyID = Type::FirstDerivedTyID;
47 freeTable(ModuleValues);
50 Module* releaseModule() {
51 // Since we're losing control of this Module, we must hand it back complete
52 Module *M = ModuleProvider::releaseModule();
57 void ParseBytecode(const unsigned char *Buf, unsigned Length,
58 const std::string &ModuleID);
61 std::cerr << "BytecodeParser instance!\n";
65 struct ValueList : public User {
66 ValueList() : User(Type::TypeTy, Value::TypeVal) {}
68 // vector compatibility methods
69 unsigned size() const { return getNumOperands(); }
70 void push_back(Value *V) { Operands.push_back(Use(V, this)); }
71 Value *back() const { return Operands.back(); }
72 void pop_back() { Operands.pop_back(); }
73 bool empty() const { return Operands.empty(); }
75 virtual void print(std::ostream& OS) const {
76 OS << "Bytecode Reader UseHandle!";
80 // Information about the module, extracted from the bytecode revision number.
81 unsigned char RevisionNum; // The rev # itself
82 unsigned char FirstDerivedTyID; // First variable index to use for type
83 bool HasImplicitZeroInitializer; // Is entry 0 of every slot implicity zeros?
84 bool hasInternalMarkerOnly; // Only types of linkage are intern/external
86 typedef std::vector<ValueList*> ValueTable;
88 ValueTable ModuleValues;
89 std::map<std::pair<unsigned,unsigned>, Value*> ForwardReferences;
91 std::vector<BasicBlock*> ParsedBasicBlocks;
93 // GlobalRefs - This maintains a mapping between <Type, Slot #>'s and forward
94 // references to global values or constants. Such values may be referenced
95 // before they are defined, and if so, the temporary object that they
96 // represent is held here.
98 typedef std::map<std::pair<const Type *, unsigned>, Value*> GlobalRefsType;
99 GlobalRefsType GlobalRefs;
101 // TypesLoaded - This vector mirrors the Values[TypeTyID] plane. It is used
102 // to deal with forward references to types.
104 typedef std::vector<PATypeHolder> TypeValuesListTy;
105 TypeValuesListTy ModuleTypeValues;
106 TypeValuesListTy FunctionTypeValues;
108 // When the ModuleGlobalInfo section is read, we create a function object for
109 // each function in the module. When the function is loaded, this function is
112 std::vector<std::pair<Function*, unsigned> > FunctionSignatureList;
114 // Constant values are read in after global variables. Because of this, we
115 // must defer setting the initializers on global variables until after module
116 // level constants have been read. In the mean time, this list keeps track of
119 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
121 // For lazy reading-in of functions, we need to save away several pieces of
122 // information about each function: its begin and end pointer in the buffer
123 // and its FunctionSlot.
125 std::map<Function*, LazyFunctionInfo*> LazyFunctionLoadMap;
128 void freeTable(ValueTable &Tab) {
129 while (!Tab.empty()) {
136 void ParseModule(const unsigned char * Buf, const unsigned char *End);
137 void materializeFunction(Function *F);
140 void ParseVersionInfo (const unsigned char *&Buf, const unsigned char *End);
141 void ParseModuleGlobalInfo(const unsigned char *&Buf, const unsigned char *E);
142 void ParseSymbolTable(const unsigned char *&Buf, const unsigned char *End,
143 SymbolTable *, Function *CurrentFunction);
144 void ParseFunction(const unsigned char *&Buf, const unsigned char *End);
145 void ParseGlobalTypes(const unsigned char *&Buf, const unsigned char *EndBuf);
147 BasicBlock *ParseBasicBlock(const unsigned char *&Buf,
148 const unsigned char *End,
151 Instruction *ParseInstruction(const unsigned char *&Buf,
152 const unsigned char *End);
154 void ParseConstantPool(const unsigned char *&Buf, const unsigned char *EndBuf,
155 ValueTable &Tab, TypeValuesListTy &TypeTab);
156 Constant *parseConstantValue(const unsigned char *&Buf,
157 const unsigned char *End,
159 void parseTypeConstants(const unsigned char *&Buf,
160 const unsigned char *EndBuf,
161 TypeValuesListTy &Tab, unsigned NumEntries);
162 const Type *parseTypeConstant(const unsigned char *&Buf,
163 const unsigned char *EndBuf);
165 Value *getValue(const Type *Ty, unsigned num, bool Create = true);
166 Value *getValue(unsigned TypeID, unsigned num, bool Create = true);
167 const Type *getType(unsigned ID);
168 BasicBlock *getBasicBlock(unsigned ID);
169 Constant *getConstantValue(const Type *Ty, unsigned num);
171 unsigned insertValue(Value *V, ValueTable &Table);
173 unsigned getTypeSlot(const Type *Ty);
175 // resolve all references to the placeholder (if any) for the given value
176 void ResolveReferencesToValue(Value *Val, unsigned Slot);
179 template<class SuperType>
180 class PlaceholderDef : public SuperType {
182 PlaceholderDef(); // DO NOT IMPLEMENT
183 void operator=(const PlaceholderDef &); // DO NOT IMPLEMENT
185 PlaceholderDef(const Type *Ty, unsigned id) : SuperType(Ty), ID(id) {}
186 unsigned getID() { return ID; }
189 struct ConstantPlaceHolderHelper : public Constant {
190 ConstantPlaceHolderHelper(const Type *Ty)
192 virtual bool isNullValue() const { return false; }
195 typedef PlaceholderDef<ConstantPlaceHolderHelper> ConstPHolder;
197 // Some common errors we find
198 static const std::string Error_readvbr = "read_vbr(): error reading.";
199 static const std::string Error_read = "read(): error reading.";
200 static const std::string Error_inputdata = "input_data(): error reading.";
201 static const std::string Error_DestSlot = "No destination slot found.";
203 static inline void readBlock(const unsigned char *&Buf,
204 const unsigned char *EndBuf,
205 unsigned &Type, unsigned &Size) {
207 bool Result = read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size);
208 std::cerr << "StartLoc = " << ((unsigned)Buf & 4095)
209 << " Type = " << Type << " Size = " << Size << endl;
210 if (Result) throw Error_read;
212 if (read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size)) throw Error_read;