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/Constants.h"
18 #include "llvm/DerivedTypes.h"
19 #include "llvm/Function.h"
20 #include "llvm/ModuleProvider.h"
21 #include "llvm/Bytecode/Primitives.h"
27 // Enable to trace to figure out what the heck is going on when parsing fails
28 //#define TRACE_LEVEL 10
29 //#define DEBUG_OUTPUT
31 #if TRACE_LEVEL // ByteCodeReading_TRACEr
32 #define BCR_TRACE(n, X) \
33 if (n < TRACE_LEVEL) std::cerr << std::string(n*2, ' ') << X
35 #define BCR_TRACE(n, X)
38 struct LazyFunctionInfo {
39 const unsigned char *Buf, *EndBuf;
40 LazyFunctionInfo(const unsigned char *B = 0, const unsigned char *EB = 0)
41 : Buf(B), EndBuf(EB) {}
44 class BytecodeParser : public ModuleProvider {
45 BytecodeParser(const BytecodeParser &); // DO NOT IMPLEMENT
46 void operator=(const BytecodeParser &); // DO NOT IMPLEMENT
49 // Define this in case we don't see a ModuleGlobalInfo block.
50 FirstDerivedTyID = Type::FirstDerivedTyID;
58 freeTable(ModuleValues);
61 Module* releaseModule() {
62 // Since we're losing control of this Module, we must hand it back complete
63 Module *M = ModuleProvider::releaseModule();
68 void ParseBytecode(const unsigned char *Buf, unsigned Length,
69 const std::string &ModuleID);
72 std::cerr << "BytecodeParser instance!\n";
76 struct ValueList : public User {
77 ValueList() : User(Type::TypeTy, Value::TypeVal) {}
79 // vector compatibility methods
80 unsigned size() const { return getNumOperands(); }
81 void push_back(Value *V) { Operands.push_back(Use(V, this)); }
82 Value *back() const { return Operands.back(); }
83 void pop_back() { Operands.pop_back(); }
84 bool empty() const { return Operands.empty(); }
86 virtual void print(std::ostream& OS) const {
87 OS << "Bytecode Reader UseHandle!";
91 // Information about the module, extracted from the bytecode revision number.
92 unsigned char RevisionNum; // The rev # itself
93 unsigned char FirstDerivedTyID; // First variable index to use for type
94 bool hasInternalMarkerOnly; // Only types of linkage are intern/external
95 bool hasExtendedLinkageSpecs; // Supports more than 4 linkage types
96 bool hasOldStyleVarargs; // Has old version of varargs intrinsics?
97 bool hasVarArgCallPadding; // Bytecode has extra padding in vararg call
99 bool usesOldStyleVarargs; // Does this module USE old style varargs?
101 typedef std::vector<ValueList*> ValueTable;
103 ValueTable ModuleValues;
104 std::map<std::pair<unsigned,unsigned>, Value*> ForwardReferences;
106 std::vector<BasicBlock*> ParsedBasicBlocks;
108 // ConstantFwdRefs - This maintains a mapping between <Type, Slot #>'s and
109 // forward references to constants. Such values may be referenced before they
110 // are defined, and if so, the temporary object that they represent is held
113 typedef std::map<std::pair<const Type*,unsigned>, Constant*> ConstantRefsType;
114 ConstantRefsType ConstantFwdRefs;
116 // TypesLoaded - This vector mirrors the Values[TypeTyID] plane. It is used
117 // to deal with forward references to types.
119 typedef std::vector<PATypeHolder> TypeValuesListTy;
120 TypeValuesListTy ModuleTypeValues;
121 TypeValuesListTy FunctionTypeValues;
123 // When the ModuleGlobalInfo section is read, we create a function object for
124 // each function in the module. When the function is loaded, this function is
127 std::vector<Function*> FunctionSignatureList;
129 // Constant values are read in after global variables. Because of this, we
130 // must defer setting the initializers on global variables until after module
131 // level constants have been read. In the mean time, this list keeps track of
134 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
136 // For lazy reading-in of functions, we need to save away several pieces of
137 // information about each function: its begin and end pointer in the buffer
138 // and its FunctionSlot.
140 std::map<Function*, LazyFunctionInfo> LazyFunctionLoadMap;
143 void freeTable(ValueTable &Tab) {
144 while (!Tab.empty()) {
151 void ParseModule(const unsigned char * Buf, const unsigned char *End);
152 void materializeFunction(Function *F);
155 void ParseVersionInfo (const unsigned char *&Buf, const unsigned char *End);
156 void ParseModuleGlobalInfo(const unsigned char *&Buf, const unsigned char *E);
157 void ParseSymbolTable(const unsigned char *&Buf, const unsigned char *End,
158 SymbolTable *, Function *CurrentFunction);
159 void ParseFunction(const unsigned char *&Buf, const unsigned char *End);
160 void ParseGlobalTypes(const unsigned char *&Buf, const unsigned char *EndBuf);
162 BasicBlock *ParseBasicBlock(const unsigned char *&Buf,
163 const unsigned char *End,
165 unsigned ParseInstructionList(Function *F, const unsigned char *&Buf,
166 const unsigned char *EndBuf);
168 void ParseInstruction(const unsigned char *&Buf, const unsigned char *End,
169 std::vector<unsigned> &Args, BasicBlock *BB);
171 void ParseConstantPool(const unsigned char *&Buf, const unsigned char *EndBuf,
172 ValueTable &Tab, TypeValuesListTy &TypeTab);
173 Constant *parseConstantValue(const unsigned char *&Buf,
174 const unsigned char *End,
176 void parseTypeConstants(const unsigned char *&Buf,
177 const unsigned char *EndBuf,
178 TypeValuesListTy &Tab, unsigned NumEntries);
179 const Type *parseTypeConstant(const unsigned char *&Buf,
180 const unsigned char *EndBuf);
182 Value *getValue(unsigned TypeID, unsigned num, bool Create = true);
183 const Type *getType(unsigned ID);
184 BasicBlock *getBasicBlock(unsigned ID);
185 Constant *getConstantValue(unsigned TypeID, unsigned num);
186 Constant *getConstantValue(const Type *Ty, unsigned num) {
187 return getConstantValue(getTypeSlot(Ty), num);
190 unsigned insertValue(Value *V, unsigned Type, ValueTable &Table);
192 unsigned getTypeSlot(const Type *Ty);
194 // resolve all references to the placeholder (if any) for the given constant
195 void ResolveReferencesToConstant(Constant *C, unsigned Slot);
198 template<class SuperType>
199 class PlaceholderDef : public SuperType {
201 PlaceholderDef(); // DO NOT IMPLEMENT
202 void operator=(const PlaceholderDef &); // DO NOT IMPLEMENT
204 PlaceholderDef(const Type *Ty, unsigned id) : SuperType(Ty), ID(id) {}
205 unsigned getID() { return ID; }
208 struct ConstantPlaceHolderHelper : public ConstantExpr {
209 ConstantPlaceHolderHelper(const Type *Ty)
210 : ConstantExpr(Instruction::UserOp1, Constant::getNullValue(Ty), Ty) {}
213 typedef PlaceholderDef<ConstantPlaceHolderHelper> ConstPHolder;
215 // Some common errors we find
216 static const std::string Error_readvbr = "read_vbr(): error reading.";
217 static const std::string Error_read = "read(): error reading.";
218 static const std::string Error_inputdata = "input_data(): error reading.";
219 static const std::string Error_DestSlot = "No destination slot found.";
221 static inline void readBlock(const unsigned char *&Buf,
222 const unsigned char *EndBuf,
223 unsigned &Type, unsigned &Size) {
225 bool Result = read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size);
226 std::cerr << "StartLoc = " << ((unsigned)Buf & 4095)
227 << " Type = " << Type << " Size = " << Size << "\n";
228 if (Result) throw Error_read;
230 if (read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size)) throw Error_read;
234 } // End llvm namespace