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/Instruction.h"
18 // Enable to trace to figure out what the heck is going on when parsing fails
21 #if TRACE_LEVEL // ByteCodeReading_TRACEer
22 #include "llvm/Assembly/Writer.h"
23 #define BCR_TRACE(n, X) if (n < TRACE_LEVEL) cerr << string(n*2, ' ') << X
25 #define BCR_TRACE(n, X)
33 typedef unsigned char uchar;
35 struct RawInst { // The raw fields out of the bytecode stream...
42 vector<unsigned> *VarArgs; // Contains arg #3,4,5... if NumOperands > 3
46 class BytecodeParser : public AbstractTypeUser {
49 // Define this in case we don't see a ModuleGlobalInfo block.
50 FirstDerivedTyID = Type::FirstDerivedTyID;
53 Module *ParseBytecode(const uchar *Buf, const uchar *EndBuf);
54 private: // All of this data is transient across calls to ParseBytecode
55 typedef vector<Value *> ValueList;
56 typedef vector<ValueList> ValueTable;
57 ValueTable Values, LateResolveValues;
58 ValueTable ModuleValues, LateResolveModuleValues;
60 // TypesLoaded - This vector mirrors the Values[TypeTyID] plane. It is used
61 // to deal with forward references to types.
63 typedef vector<PATypeHandle<Type> > TypeValuesListTy;
64 TypeValuesListTy ModuleTypeValues;
65 TypeValuesListTy MethodTypeValues;
67 // Information read from the ModuleGlobalInfo section of the file...
68 unsigned FirstDerivedTyID;
70 // When the ModuleGlobalInfo section is read, we load the type of each method
71 // and the 'ModuleValues' slot that it lands in. We then load a placeholder
72 // into its slot to reserve it. When the method is loaded, this placeholder
75 list<pair<const MethodType *, unsigned> > MethodSignatureList;
78 bool ParseModule (const uchar * Buf, const uchar *End, Module *&);
79 bool ParseModuleGlobalInfo (const uchar *&Buf, const uchar *End, Module *);
80 bool ParseSymbolTable (const uchar *&Buf, const uchar *End, SymbolTable *);
81 bool ParseMethod (const uchar *&Buf, const uchar *End, Module *);
82 bool ParseBasicBlock (const uchar *&Buf, const uchar *End, BasicBlock *&);
83 bool ParseInstruction (const uchar *&Buf, const uchar *End, Instruction *&);
84 bool ParseRawInst (const uchar *&Buf, const uchar *End, RawInst &);
86 bool ParseConstantPool(const uchar *&Buf, const uchar *EndBuf,
87 ValueTable &Tab, TypeValuesListTy &TypeTab);
88 bool parseConstPoolValue(const uchar *&Buf, const uchar *End,
89 const Type *Ty, ConstPoolVal *&V);
90 bool parseTypeConstants(const uchar *&Buf, const uchar *EndBuf,
91 TypeValuesListTy &Tab, unsigned NumEntries);
92 const Type *parseTypeConstant(const uchar *&Buf, const uchar *EndBuf);
94 Value *getValue(const Type *Ty, unsigned num, bool Create = true);
95 const Type *getType(unsigned ID);
97 bool insertValue(Value *D, vector<ValueList> &D);
98 bool postResolveValues(ValueTable &ValTab);
100 bool getTypeSlot(const Type *Ty, unsigned &Slot);
103 // refineAbstractType - The callback method is invoked when one of the
104 // elements of TypeValues becomes more concrete...
106 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
109 template<class SuperType>
110 class PlaceholderDef : public SuperType {
113 PlaceholderDef(const Type *Ty, unsigned id) : SuperType(Ty), ID(id) {}
114 unsigned getID() { return ID; }
117 struct InstPlaceHolderHelper : public Instruction {
118 InstPlaceHolderHelper(const Type *Ty) : Instruction(Ty, UserOp1, "") {}
119 virtual const char *getOpcodeName() const { return "placeholder"; }
121 virtual Instruction *clone() const { abort(); return 0; }
124 struct BBPlaceHolderHelper : public BasicBlock {
125 BBPlaceHolderHelper(const Type *Ty) : BasicBlock() {
126 assert(Ty->isLabelType());
130 struct MethPlaceHolderHelper : public Method {
131 MethPlaceHolderHelper(const Type *Ty)
132 : Method(cast<const MethodType>(Ty)) {
136 typedef PlaceholderDef<InstPlaceHolderHelper> DefPHolder;
137 typedef PlaceholderDef<BBPlaceHolderHelper> BBPHolder;
138 typedef PlaceholderDef<MethPlaceHolderHelper> MethPHolder;
140 static inline unsigned getValueIDNumberFromPlaceHolder(Value *Def) {
141 switch (Def->getType()->getPrimitiveID()) {
142 case Type::LabelTyID: return ((BBPHolder*)Def)->getID();
143 case Type::MethodTyID: return ((MethPHolder*)Def)->getID();
144 default: return ((DefPHolder*)Def)->getID();
148 static inline bool readBlock(const uchar *&Buf, const uchar *EndBuf,
149 unsigned &Type, unsigned &Size) {
151 bool Result = read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size);
152 cerr << "StartLoc = " << ((unsigned)Buf & 4095)
153 << " Type = " << Type << " Size = " << Size << endl;
156 return read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size);
161 // failure Template - This template function is used as a place to put
162 // breakpoints in to debug failures of the bytecode parser.
164 template <typename X>
165 static X failure(X Value) {