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/Bytecode/Primitives.h"
11 #include "llvm/SymTabValue.h"
12 #include "llvm/Method.h"
13 #include "llvm/BasicBlock.h"
14 #include "llvm/Instruction.h"
15 #include "llvm/DerivedTypes.h"
20 // Enable to trace to figure out what the heck is going on when parsing fails
23 #if TRACE_LEVEL // ByteCodeReading_TRACEer
24 #include "llvm/Assembly/Writer.h"
25 #define BCR_TRACE(n, X) if (n < TRACE_LEVEL) cerr << std::string(n*2, ' ') << X
27 #define BCR_TRACE(n, X)
36 typedef unsigned char uchar;
38 struct RawInst { // The raw fields out of the bytecode stream...
45 std::vector<unsigned> *VarArgs; // Contains arg #3,4,5... if NumOperands > 3
49 class BytecodeParser : public AbstractTypeUser {
50 std::string Error; // Error message string goes here...
53 // Define this in case we don't see a ModuleGlobalInfo block.
54 FirstDerivedTyID = Type::FirstDerivedTyID;
57 Module *ParseBytecode(const uchar *Buf, const uchar *EndBuf);
59 std::string getError() const { return Error; }
61 private: // All of this data is transient across calls to ParseBytecode
62 Module *TheModule; // Current Module being read into...
64 typedef std::vector<Value *> ValueList;
65 typedef std::vector<ValueList> ValueTable;
66 ValueTable Values, LateResolveValues;
67 ValueTable ModuleValues, LateResolveModuleValues;
69 // GlobalRefs - This maintains a mapping between <Type, Slot #>'s and forward
70 // references to global values. Global values may be referenced before they
71 // are defined, and if so, the temporary object that they represent is held
74 typedef std::map<std::pair<const PointerType *, unsigned>,
75 GlobalVariable*> GlobalRefsType;
76 GlobalRefsType GlobalRefs;
78 // TypesLoaded - This vector mirrors the Values[TypeTyID] plane. It is used
79 // to deal with forward references to types.
81 typedef std::vector<PATypeHandle<Type> > TypeValuesListTy;
82 TypeValuesListTy ModuleTypeValues;
83 TypeValuesListTy MethodTypeValues;
85 // Information read from the ModuleGlobalInfo section of the file...
86 unsigned FirstDerivedTyID;
88 // When the ModuleGlobalInfo section is read, we load the type of each method
89 // and the 'ModuleValues' slot that it lands in. We then load a placeholder
90 // into its slot to reserve it. When the method is loaded, this placeholder
93 std::list<std::pair<const PointerType *, unsigned> > MethodSignatureList;
96 bool ParseModule (const uchar * Buf, const uchar *End, Module *&);
97 bool ParseModuleGlobalInfo(const uchar *&Buf, const uchar *End, Module *);
98 bool ParseSymbolTable (const uchar *&Buf, const uchar *End, SymbolTable *);
99 bool ParseMethod (const uchar *&Buf, const uchar *End, Module *);
100 bool ParseBasicBlock (const uchar *&Buf, const uchar *End, BasicBlock *&);
101 bool ParseInstruction (const uchar *&Buf, const uchar *End, Instruction *&);
102 bool ParseRawInst (const uchar *&Buf, const uchar *End, RawInst &);
104 bool ParseConstantPool(const uchar *&Buf, const uchar *EndBuf,
105 ValueTable &Tab, TypeValuesListTy &TypeTab);
106 bool parseConstantValue(const uchar *&Buf, const uchar *End,
107 const Type *Ty, Constant *&V);
108 bool parseTypeConstants(const uchar *&Buf, const uchar *EndBuf,
109 TypeValuesListTy &Tab, unsigned NumEntries);
110 const Type *parseTypeConstant(const uchar *&Buf, const uchar *EndBuf);
112 Value *getValue(const Type *Ty, unsigned num, bool Create = true);
113 const Type *getType(unsigned ID);
115 int insertValue(Value *D, std::vector<ValueList> &D); // -1 = Failure
116 bool postResolveValues(ValueTable &ValTab);
118 bool getTypeSlot(const Type *Ty, unsigned &Slot);
120 // DeclareNewGlobalValue - Patch up forward references to global values in the
121 // form of ConstantPointerRefs.
123 void DeclareNewGlobalValue(GlobalValue *GV, unsigned Slot);
125 // refineAbstractType - The callback method is invoked when one of the
126 // elements of TypeValues becomes more concrete...
128 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
131 template<class SuperType>
132 class PlaceholderDef : public SuperType {
135 PlaceholderDef(const Type *Ty, unsigned id) : SuperType(Ty), ID(id) {}
136 unsigned getID() { return ID; }
139 struct InstPlaceHolderHelper : public Instruction {
140 InstPlaceHolderHelper(const Type *Ty) : Instruction(Ty, UserOp1, "") {}
141 virtual const char *getOpcodeName() const { return "placeholder"; }
143 virtual Instruction *clone() const { abort(); return 0; }
146 struct BBPlaceHolderHelper : public BasicBlock {
147 BBPlaceHolderHelper(const Type *Ty) : BasicBlock() {
148 assert(Ty->isLabelType());
152 struct MethPlaceHolderHelper : public Method {
153 MethPlaceHolderHelper(const Type *Ty)
154 : Method(cast<const MethodType>(Ty), true) {
158 typedef PlaceholderDef<InstPlaceHolderHelper> DefPHolder;
159 typedef PlaceholderDef<BBPlaceHolderHelper> BBPHolder;
160 typedef PlaceholderDef<MethPlaceHolderHelper> MethPHolder;
162 static inline unsigned getValueIDNumberFromPlaceHolder(Value *Def) {
163 switch (Def->getType()->getPrimitiveID()) {
164 case Type::LabelTyID: return ((BBPHolder*)Def)->getID();
165 case Type::MethodTyID: return ((MethPHolder*)Def)->getID();
166 default: return ((DefPHolder*)Def)->getID();
170 static inline bool readBlock(const uchar *&Buf, const uchar *EndBuf,
171 unsigned &Type, unsigned &Size) {
173 bool Result = read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size);
174 cerr << "StartLoc = " << ((unsigned)Buf & 4095)
175 << " Type = " << Type << " Size = " << Size << endl;
178 return read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size);
183 // failure Template - This template function is used as a place to put
184 // breakpoints in to debug failures of the bytecode parser.
186 template <typename X>
187 static X failure(X Value) {