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)
30 typedef unsigned char uchar;
32 struct RawInst { // The raw fields out of the bytecode stream...
39 std::vector<unsigned> *VarArgs; // Contains arg #3,4,5... if NumOperands > 3
43 class BytecodeParser : public AbstractTypeUser {
44 std::string Error; // Error message string goes here...
47 // Define this in case we don't see a ModuleGlobalInfo block.
48 FirstDerivedTyID = Type::FirstDerivedTyID;
51 Module *ParseBytecode(const uchar *Buf, const uchar *EndBuf);
53 std::string getError() const { return Error; }
55 private: // All of this data is transient across calls to ParseBytecode
56 Module *TheModule; // Current Module being read into...
58 typedef std::vector<Value *> ValueList;
59 typedef std::vector<ValueList> ValueTable;
60 ValueTable Values, LateResolveValues;
61 ValueTable ModuleValues, LateResolveModuleValues;
63 // GlobalRefs - This maintains a mapping between <Type, Slot #>'s and forward
64 // references to global values. Global values may be referenced before they
65 // are defined, and if so, the temporary object that they represent is held
68 typedef std::map<std::pair<const PointerType *, unsigned>,
69 GlobalVariable*> GlobalRefsType;
70 GlobalRefsType GlobalRefs;
72 // TypesLoaded - This vector mirrors the Values[TypeTyID] plane. It is used
73 // to deal with forward references to types.
75 typedef std::vector<PATypeHandle<Type> > TypeValuesListTy;
76 TypeValuesListTy ModuleTypeValues;
77 TypeValuesListTy MethodTypeValues;
79 // Information read from the ModuleGlobalInfo section of the file...
80 unsigned FirstDerivedTyID;
82 // When the ModuleGlobalInfo section is read, we load the type of each method
83 // and the 'ModuleValues' slot that it lands in. We then load a placeholder
84 // into its slot to reserve it. When the method is loaded, this placeholder
87 std::list<std::pair<const PointerType *, unsigned> > MethodSignatureList;
90 bool ParseModule (const uchar * Buf, const uchar *End, Module *&);
91 bool ParseModuleGlobalInfo(const uchar *&Buf, const uchar *End, Module *);
92 bool ParseSymbolTable (const uchar *&Buf, const uchar *End, SymbolTable *);
93 bool ParseMethod (const uchar *&Buf, const uchar *End, Module *);
94 bool ParseBasicBlock (const uchar *&Buf, const uchar *End, BasicBlock *&);
95 bool ParseInstruction (const uchar *&Buf, const uchar *End, Instruction *&);
96 bool ParseRawInst (const uchar *&Buf, const uchar *End, RawInst &);
98 bool ParseConstantPool(const uchar *&Buf, const uchar *EndBuf,
99 ValueTable &Tab, TypeValuesListTy &TypeTab);
100 bool parseConstantValue(const uchar *&Buf, const uchar *End,
101 const Type *Ty, Constant *&V);
102 bool parseTypeConstants(const uchar *&Buf, const uchar *EndBuf,
103 TypeValuesListTy &Tab, unsigned NumEntries);
104 const Type *parseTypeConstant(const uchar *&Buf, const uchar *EndBuf);
106 Value *getValue(const Type *Ty, unsigned num, bool Create = true);
107 const Type *getType(unsigned ID);
109 int insertValue(Value *D, std::vector<ValueList> &D); // -1 = Failure
110 bool postResolveValues(ValueTable &ValTab);
112 bool getTypeSlot(const Type *Ty, unsigned &Slot);
114 // DeclareNewGlobalValue - Patch up forward references to global values in the
115 // form of ConstantPointerRefs.
117 void DeclareNewGlobalValue(GlobalValue *GV, unsigned Slot);
119 // refineAbstractType - The callback method is invoked when one of the
120 // elements of TypeValues becomes more concrete...
122 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
125 template<class SuperType>
126 class PlaceholderDef : public SuperType {
129 PlaceholderDef(const Type *Ty, unsigned id) : SuperType(Ty), ID(id) {}
130 unsigned getID() { return ID; }
133 struct InstPlaceHolderHelper : public Instruction {
134 InstPlaceHolderHelper(const Type *Ty) : Instruction(Ty, UserOp1, "") {}
135 virtual const char *getOpcodeName() const { return "placeholder"; }
137 virtual Instruction *clone() const { abort(); return 0; }
140 struct BBPlaceHolderHelper : public BasicBlock {
141 BBPlaceHolderHelper(const Type *Ty) : BasicBlock() {
142 assert(Ty->isLabelType());
146 struct MethPlaceHolderHelper : public Method {
147 MethPlaceHolderHelper(const Type *Ty)
148 : Method(cast<const MethodType>(Ty), true) {
152 typedef PlaceholderDef<InstPlaceHolderHelper> DefPHolder;
153 typedef PlaceholderDef<BBPlaceHolderHelper> BBPHolder;
154 typedef PlaceholderDef<MethPlaceHolderHelper> MethPHolder;
156 static inline unsigned getValueIDNumberFromPlaceHolder(Value *Def) {
157 switch (Def->getType()->getPrimitiveID()) {
158 case Type::LabelTyID: return ((BBPHolder*)Def)->getID();
159 case Type::MethodTyID: return ((MethPHolder*)Def)->getID();
160 default: return ((DefPHolder*)Def)->getID();
164 static inline bool readBlock(const uchar *&Buf, const uchar *EndBuf,
165 unsigned &Type, unsigned &Size) {
167 bool Result = read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size);
168 cerr << "StartLoc = " << ((unsigned)Buf & 4095)
169 << " Type = " << Type << " Size = " << Size << endl;
172 return read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size);
177 // failure Template - This template function is used as a place to put
178 // breakpoints in to debug failures of the bytecode parser.
180 template <typename X>
181 static X failure(X Value) {