1 //===-- ReaderInternals.h - Definitions internal to the reader --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This header file defines various stuff that is used by the bytecode reader.
12 //===----------------------------------------------------------------------===//
14 #ifndef READER_INTERNALS_H
15 #define READER_INTERNALS_H
17 #include "llvm/Constant.h"
18 #include "llvm/DerivedTypes.h"
19 #include "llvm/Function.h"
20 #include "llvm/ModuleProvider.h"
21 #include "llvm/Bytecode/Primitives.h"
25 // Enable to trace to figure out what the heck is going on when parsing fails
26 //#define TRACE_LEVEL 10
27 //#define DEBUG_OUTPUT
29 #if TRACE_LEVEL // ByteCodeReading_TRACEr
30 #define BCR_TRACE(n, X) \
31 if (n < TRACE_LEVEL) std::cerr << std::string(n*2, ' ') << X
33 #define BCR_TRACE(n, X)
36 struct LazyFunctionInfo {
37 const unsigned char *Buf, *EndBuf;
38 unsigned FunctionSlot;
41 class BytecodeParser : public ModuleProvider {
42 BytecodeParser(const BytecodeParser &); // DO NOT IMPLEMENT
43 void operator=(const BytecodeParser &); // DO NOT IMPLEMENT
46 // Define this in case we don't see a ModuleGlobalInfo block.
47 FirstDerivedTyID = Type::FirstDerivedTyID;
55 freeTable(ModuleValues);
58 Module* releaseModule() {
59 // Since we're losing control of this Module, we must hand it back complete
60 Module *M = ModuleProvider::releaseModule();
65 void ParseBytecode(const unsigned char *Buf, unsigned Length,
66 const std::string &ModuleID);
69 std::cerr << "BytecodeParser instance!\n";
73 struct ValueList : public User {
74 ValueList() : User(Type::TypeTy, Value::TypeVal) {}
76 // vector compatibility methods
77 unsigned size() const { return getNumOperands(); }
78 void push_back(Value *V) { Operands.push_back(Use(V, this)); }
79 Value *back() const { return Operands.back(); }
80 void pop_back() { Operands.pop_back(); }
81 bool empty() const { return Operands.empty(); }
83 virtual void print(std::ostream& OS) const {
84 OS << "Bytecode Reader UseHandle!";
88 // Information about the module, extracted from the bytecode revision number.
89 unsigned char RevisionNum; // The rev # itself
90 unsigned char FirstDerivedTyID; // First variable index to use for type
91 bool hasInternalMarkerOnly; // Only types of linkage are intern/external
92 bool hasExtendedLinkageSpecs; // Supports more than 4 linkage types
93 bool hasOldStyleVarargs; // Has old version of varargs intrinsics?
94 bool hasVarArgCallPadding; // Bytecode has extra padding in vararg call
96 bool usesOldStyleVarargs; // Does this module USE old style varargs?
98 typedef std::vector<ValueList*> ValueTable;
100 ValueTable ModuleValues;
101 std::map<std::pair<unsigned,unsigned>, Value*> ForwardReferences;
103 std::vector<BasicBlock*> ParsedBasicBlocks;
105 // GlobalRefs - This maintains a mapping between <Type, Slot #>'s and forward
106 // references to global values or constants. Such values may be referenced
107 // before they are defined, and if so, the temporary object that they
108 // represent is held here.
110 typedef std::map<std::pair<const Type *, unsigned>, Value*> GlobalRefsType;
111 GlobalRefsType GlobalRefs;
113 // TypesLoaded - This vector mirrors the Values[TypeTyID] plane. It is used
114 // to deal with forward references to types.
116 typedef std::vector<PATypeHolder> TypeValuesListTy;
117 TypeValuesListTy ModuleTypeValues;
118 TypeValuesListTy FunctionTypeValues;
120 // When the ModuleGlobalInfo section is read, we create a function object for
121 // each function in the module. When the function is loaded, this function is
124 std::vector<std::pair<Function*, unsigned> > FunctionSignatureList;
126 // Constant values are read in after global variables. Because of this, we
127 // must defer setting the initializers on global variables until after module
128 // level constants have been read. In the mean time, this list keeps track of
131 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
133 // For lazy reading-in of functions, we need to save away several pieces of
134 // information about each function: its begin and end pointer in the buffer
135 // and its FunctionSlot.
137 std::map<Function*, LazyFunctionInfo*> LazyFunctionLoadMap;
140 void freeTable(ValueTable &Tab) {
141 while (!Tab.empty()) {
148 void ParseModule(const unsigned char * Buf, const unsigned char *End);
149 void materializeFunction(Function *F);
152 void ParseVersionInfo (const unsigned char *&Buf, const unsigned char *End);
153 void ParseModuleGlobalInfo(const unsigned char *&Buf, const unsigned char *E);
154 void ParseSymbolTable(const unsigned char *&Buf, const unsigned char *End,
155 SymbolTable *, Function *CurrentFunction);
156 void ParseFunction(const unsigned char *&Buf, const unsigned char *End);
157 void ParseGlobalTypes(const unsigned char *&Buf, const unsigned char *EndBuf);
159 BasicBlock *ParseBasicBlock(const unsigned char *&Buf,
160 const unsigned char *End,
163 void ParseInstruction(const unsigned char *&Buf, const unsigned char *End,
164 std::vector<unsigned> &Args, BasicBlock *BB);
166 void ParseConstantPool(const unsigned char *&Buf, const unsigned char *EndBuf,
167 ValueTable &Tab, TypeValuesListTy &TypeTab);
168 Constant *parseConstantValue(const unsigned char *&Buf,
169 const unsigned char *End,
171 void parseTypeConstants(const unsigned char *&Buf,
172 const unsigned char *EndBuf,
173 TypeValuesListTy &Tab, unsigned NumEntries);
174 const Type *parseTypeConstant(const unsigned char *&Buf,
175 const unsigned char *EndBuf);
177 Value *getValue(const Type *Ty, unsigned num, bool Create = true);
178 Value *getValue(unsigned TypeID, unsigned num, bool Create = true);
179 const Type *getType(unsigned ID);
180 BasicBlock *getBasicBlock(unsigned ID);
181 Constant *getConstantValue(const Type *Ty, unsigned num);
183 unsigned insertValue(Value *V, ValueTable &Table);
184 unsigned insertValue(Value *V, unsigned Type, ValueTable &Table);
186 unsigned getTypeSlot(const Type *Ty);
188 // resolve all references to the placeholder (if any) for the given value
189 void ResolveReferencesToValue(Value *Val, unsigned Slot);
192 template<class SuperType>
193 class PlaceholderDef : public SuperType {
195 PlaceholderDef(); // DO NOT IMPLEMENT
196 void operator=(const PlaceholderDef &); // DO NOT IMPLEMENT
198 PlaceholderDef(const Type *Ty, unsigned id) : SuperType(Ty), ID(id) {}
199 unsigned getID() { return ID; }
202 struct ConstantPlaceHolderHelper : public Constant {
203 ConstantPlaceHolderHelper(const Type *Ty)
205 virtual bool isNullValue() const { return false; }
208 typedef PlaceholderDef<ConstantPlaceHolderHelper> ConstPHolder;
210 // Some common errors we find
211 static const std::string Error_readvbr = "read_vbr(): error reading.";
212 static const std::string Error_read = "read(): error reading.";
213 static const std::string Error_inputdata = "input_data(): error reading.";
214 static const std::string Error_DestSlot = "No destination slot found.";
216 static inline void readBlock(const unsigned char *&Buf,
217 const unsigned char *EndBuf,
218 unsigned &Type, unsigned &Size) {
220 bool Result = read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size);
221 std::cerr << "StartLoc = " << ((unsigned)Buf & 4095)
222 << " Type = " << Type << " Size = " << Size << std::endl;
223 if (Result) throw Error_read;
225 if (read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size)) throw Error_read;