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"
20 // Enable to trace to figure out what the heck is going on when parsing fails
21 //#define TRACE_LEVEL 10
23 #if TRACE_LEVEL // ByteCodeReading_TRACEr
24 #define BCR_TRACE(n, X) \
25 if (n < TRACE_LEVEL) std::cerr << std::string(n*2, ' ') << X
27 #define BCR_TRACE(n, X)
30 struct RawInst { // The raw fields out of the bytecode stream...
37 std::vector<unsigned> *VarArgs; // Contains arg #3,4,5... if NumOperands > 3
41 struct LazyFunctionInfo {
42 const unsigned char *Buf, *EndBuf;
43 unsigned FunctionSlot;
46 class BytecodeParser : public AbstractTypeUser, public AbstractModuleProvider {
47 BytecodeParser(const BytecodeParser &); // DO NOT IMPLEMENT
48 void operator=(const BytecodeParser &); // DO NOT IMPLEMENT
51 // Define this in case we don't see a ModuleGlobalInfo block.
52 FirstDerivedTyID = Type::FirstDerivedTyID;
60 freeTable(LateResolveValues);
61 freeTable(ModuleValues);
64 Module* releaseModule() {
65 // Since we're losing control of this Module, we must hand it back complete
68 Module *tempM = TheModule;
73 void ParseBytecode(const unsigned char *Buf, unsigned Length,
74 const std::string &ModuleID);
77 std::cerr << "BytecodeParser instance!\n";
80 private: // All of this data is transient across calls to ParseBytecode
81 struct ValueList : public User {
82 ValueList() : User(Type::TypeTy, Value::TypeVal) {
86 // vector compatibility methods
87 unsigned size() const { return getNumOperands(); }
88 void push_back(Value *V) { Operands.push_back(Use(V, this)); }
89 Value *back() const { return Operands.back(); }
90 void pop_back() { Operands.pop_back(); }
91 bool empty() const { return Operands.empty(); }
93 virtual void print(std::ostream& OS) const {
94 OS << "Bytecode Reader UseHandle!";
98 // Information about the module, extracted from the bytecode revision number.
99 unsigned char RevisionNum; // The rev # itself
100 unsigned char FirstDerivedTyID; // First variable index to use for type
101 bool HasImplicitZeroInitializer; // Is entry 0 of every slot implicity zeros?
102 bool hasInternalMarkerOnly; // Only types of linkage are intern/external
104 typedef std::vector<ValueList*> ValueTable;
105 ValueTable Values, LateResolveValues;
106 ValueTable ModuleValues;
108 // GlobalRefs - This maintains a mapping between <Type, Slot #>'s and forward
109 // references to global values or constants. Such values may be referenced
110 // before they are defined, and if so, the temporary object that they
111 // represent is held here.
113 typedef std::map<std::pair<const Type *, unsigned>, Value*> GlobalRefsType;
114 GlobalRefsType GlobalRefs;
116 // TypesLoaded - This vector mirrors the Values[TypeTyID] plane. It is used
117 // to deal with forward references to types.
119 typedef std::vector<PATypeHandle> 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<std::pair<Function*, unsigned> > 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,
159 void ParseFunction(const unsigned char *&Buf, const unsigned char *End);
160 void ParseGlobalTypes(const unsigned char *&Buf, const unsigned char *EndBuf);
162 std::auto_ptr<BasicBlock>
163 ParseBasicBlock(const unsigned char *&Buf, const unsigned char *End);
165 bool ParseInstruction (const unsigned char *&Buf, const unsigned char *End,
167 bool ParseRawInst (const unsigned char *&Buf, const unsigned char *End,
170 void ParseConstantPool(const unsigned char *&Buf, const unsigned char *EndBuf,
171 ValueTable &Tab, TypeValuesListTy &TypeTab);
172 void parseConstantValue(const unsigned char *&Buf, const unsigned char *End,
173 const Type *Ty, Constant *&V);
174 void parseTypeConstants(const unsigned char *&Buf,
175 const unsigned char *EndBuf,
176 TypeValuesListTy &Tab, unsigned NumEntries);
177 const Type *parseTypeConstant(const unsigned char *&Buf,
178 const unsigned char *EndBuf);
180 Value *getValue(const Type *Ty, unsigned num, bool Create = true);
181 const Type *getType(unsigned ID);
182 Constant *getConstantValue(const Type *Ty, unsigned num);
184 int insertValue(Value *V, ValueTable &Table); // -1 = Failure
185 void setValueTo(ValueTable &D, unsigned Slot, Value *V);
186 void postResolveValues(ValueTable &ValTab);
188 void getTypeSlot(const Type *Ty, unsigned &Slot);
190 // resolve all references to the placeholder (if any) for the given value
191 void ResolveReferencesToValue(Value *Val, unsigned Slot);
194 // refineAbstractType - The callback method is invoked when one of the
195 // elements of TypeValues becomes more concrete...
197 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
200 template<class SuperType>
201 class PlaceholderDef : public SuperType {
203 PlaceholderDef(); // DO NOT IMPLEMENT
204 void operator=(const PlaceholderDef &); // DO NOT IMPLEMENT
206 PlaceholderDef(const Type *Ty, unsigned id) : SuperType(Ty), ID(id) {}
207 unsigned getID() { return ID; }
210 struct InstPlaceHolderHelper : public Instruction {
211 InstPlaceHolderHelper(const Type *Ty) : Instruction(Ty, UserOp1, "") {}
212 virtual const char *getOpcodeName() const { return "placeholder"; }
214 virtual Instruction *clone() const { abort(); return 0; }
217 struct BBPlaceHolderHelper : public BasicBlock {
218 BBPlaceHolderHelper(const Type *Ty) : BasicBlock() {
219 assert(Ty == Type::LabelTy);
223 struct ConstantPlaceHolderHelper : public Constant {
224 ConstantPlaceHolderHelper(const Type *Ty)
226 virtual bool isNullValue() const { return false; }
229 typedef PlaceholderDef<InstPlaceHolderHelper> ValPHolder;
230 typedef PlaceholderDef<BBPlaceHolderHelper> BBPHolder;
231 typedef PlaceholderDef<ConstantPlaceHolderHelper> ConstPHolder;
233 // Some common errors we find
234 static const std::string Error_readvbr = "read_vbr(): error reading.";
235 static const std::string Error_read = "read(): error reading.";
236 static const std::string Error_inputdata = "input_data(): error reading.";
237 static const std::string Error_DestSlot = "No destination slot found.";
239 static inline unsigned getValueIDNumberFromPlaceHolder(Value *Val) {
240 if (isa<Constant>(Val))
241 return ((ConstPHolder*)Val)->getID();
243 // else discriminate by type
244 switch (Val->getType()->getPrimitiveID()) {
245 case Type::LabelTyID: return ((BBPHolder*)Val)->getID();
246 default: return ((ValPHolder*)Val)->getID();
250 static inline void readBlock(const unsigned char *&Buf,
251 const unsigned char *EndBuf,
252 unsigned &Type, unsigned &Size) {
254 bool Result = read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size);
255 std::cerr << "StartLoc = " << ((unsigned)Buf & 4095)
256 << " Type = " << Type << " Size = " << Size << endl;
257 if (Result) throw Error_read;
259 if (read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size)) throw Error_read;