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/Function.h"
12 #include "llvm/BasicBlock.h"
13 #include "llvm/Instruction.h"
14 #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 #define BCR_TRACE(n, X) if (n < TRACE_LEVEL) std::cerr << std::string(n*2, ' ') << X
26 #define BCR_TRACE(n, X)
29 typedef unsigned char uchar;
31 struct RawInst { // The raw fields out of the bytecode stream...
38 std::vector<unsigned> *VarArgs; // Contains arg #3,4,5... if NumOperands > 3
42 class BytecodeParser : public AbstractTypeUser {
43 std::string Error; // Error message string goes here...
46 // Define this in case we don't see a ModuleGlobalInfo block.
47 FirstDerivedTyID = Type::FirstDerivedTyID;
50 Module *ParseBytecode(const uchar *Buf, const uchar *EndBuf);
52 std::string getError() const { return Error; }
55 std::cerr << "BytecodeParser instance!\n";
58 private: // All of this data is transient across calls to ParseBytecode
59 Module *TheModule; // Current Module being read into...
61 typedef std::vector<Value *> ValueList;
62 typedef std::vector<ValueList> ValueTable;
63 ValueTable Values, LateResolveValues;
64 ValueTable ModuleValues, LateResolveModuleValues;
66 // GlobalRefs - This maintains a mapping between <Type, Slot #>'s and forward
67 // references to global values. Global values may be referenced before they
68 // are defined, and if so, the temporary object that they represent is held
71 typedef std::map<std::pair<const PointerType *, unsigned>,
72 GlobalVariable*> GlobalRefsType;
73 GlobalRefsType GlobalRefs;
75 // TypesLoaded - This vector mirrors the Values[TypeTyID] plane. It is used
76 // to deal with forward references to types.
78 typedef std::vector<PATypeHandle<Type> > TypeValuesListTy;
79 TypeValuesListTy ModuleTypeValues;
80 TypeValuesListTy MethodTypeValues;
82 // Information read from the ModuleGlobalInfo section of the file...
83 unsigned FirstDerivedTyID;
85 // When the ModuleGlobalInfo section is read, we load the type of each method
86 // and the 'ModuleValues' slot that it lands in. We then load a placeholder
87 // into its slot to reserve it. When the method is loaded, this placeholder
90 std::list<std::pair<const PointerType *, unsigned> > MethodSignatureList;
93 bool ParseModule (const uchar * Buf, const uchar *End, Module *&);
94 bool ParseModuleGlobalInfo(const uchar *&Buf, const uchar *End, Module *);
95 bool ParseSymbolTable (const uchar *&Buf, const uchar *End, SymbolTable *);
96 bool ParseMethod (const uchar *&Buf, const uchar *End, Module *);
97 bool ParseBasicBlock (const uchar *&Buf, const uchar *End, BasicBlock *&);
98 bool ParseInstruction (const uchar *&Buf, const uchar *End, Instruction *&);
99 bool ParseRawInst (const uchar *&Buf, const uchar *End, RawInst &);
101 bool ParseConstantPool(const uchar *&Buf, const uchar *EndBuf,
102 ValueTable &Tab, TypeValuesListTy &TypeTab);
103 bool parseConstantValue(const uchar *&Buf, const uchar *End,
104 const Type *Ty, Constant *&V);
105 bool parseTypeConstants(const uchar *&Buf, const uchar *EndBuf,
106 TypeValuesListTy &Tab, unsigned NumEntries);
107 const Type *parseTypeConstant(const uchar *&Buf, const uchar *EndBuf);
109 Value *getValue(const Type *Ty, unsigned num, bool Create = true);
110 const Type *getType(unsigned ID);
112 int insertValue(Value *D, std::vector<ValueList> &D); // -1 = Failure
113 bool postResolveValues(ValueTable &ValTab);
115 bool getTypeSlot(const Type *Ty, unsigned &Slot);
117 // DeclareNewGlobalValue - Patch up forward references to global values in the
118 // form of ConstantPointerRefs.
120 void DeclareNewGlobalValue(GlobalValue *GV, unsigned Slot);
122 // refineAbstractType - The callback method is invoked when one of the
123 // elements of TypeValues becomes more concrete...
125 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
128 template<class SuperType>
129 class PlaceholderDef : public SuperType {
132 PlaceholderDef(const Type *Ty, unsigned id) : SuperType(Ty), ID(id) {}
133 unsigned getID() { return ID; }
136 struct InstPlaceHolderHelper : public Instruction {
137 InstPlaceHolderHelper(const Type *Ty) : Instruction(Ty, UserOp1, "") {}
138 virtual const char *getOpcodeName() const { return "placeholder"; }
140 virtual Instruction *clone() const { abort(); return 0; }
143 struct BBPlaceHolderHelper : public BasicBlock {
144 BBPlaceHolderHelper(const Type *Ty) : BasicBlock() {
145 assert(Ty == Type::LabelTy);
149 struct MethPlaceHolderHelper : public Function {
150 MethPlaceHolderHelper(const Type *Ty)
151 : Function(cast<const FunctionType>(Ty), true) {
155 typedef PlaceholderDef<InstPlaceHolderHelper> DefPHolder;
156 typedef PlaceholderDef<BBPlaceHolderHelper> BBPHolder;
157 typedef PlaceholderDef<MethPlaceHolderHelper> MethPHolder;
159 static inline unsigned getValueIDNumberFromPlaceHolder(Value *Def) {
160 switch (Def->getType()->getPrimitiveID()) {
161 case Type::LabelTyID: return ((BBPHolder*)Def)->getID();
162 case Type::FunctionTyID: return ((MethPHolder*)Def)->getID();
163 default: return ((DefPHolder*)Def)->getID();
167 static inline bool readBlock(const uchar *&Buf, const uchar *EndBuf,
168 unsigned &Type, unsigned &Size) {
170 bool Result = read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size);
171 std::cerr << "StartLoc = " << ((unsigned)Buf & 4095)
172 << " Type = " << Type << " Size = " << Size << endl;
175 return read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size);
180 // failure Template - This template function is used as a place to put
181 // breakpoints in to debug failures of the bytecode parser.
183 template <typename X>
184 static X failure(X Value) {