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"
22 typedef unsigned char uchar;
24 struct RawInst { // The raw fields out of the bytecode stream...
31 vector<unsigned> *VarArgs; // Contains arg #3,4,5... if NumOperands > 3
35 class BytecodeParser {
38 // Define this in case we don't see a ModuleGlobalInfo block.
39 FirstDerivedTyID = Type::FirstDerivedTyID;
42 Module *ParseBytecode(const uchar *Buf, const uchar *EndBuf);
43 private: // All of this data is transient across calls to ParseBytecode
44 typedef vector<Value *> ValueList;
45 typedef vector<ValueList> ValueTable;
46 typedef map<const Type *, unsigned> TypeMapType;
47 ValueTable Values, LateResolveValues;
48 ValueTable ModuleValues, LateResolveModuleValues;
51 // Information read from the ModuleGlobalInfo section of the file...
52 unsigned FirstDerivedTyID;
54 // When the ModuleGlobalInfo section is read, we load the type of each method
55 // and the 'ModuleValues' slot that it lands in. We then load a placeholder
56 // into its slot to reserve it. When the method is loaded, this placeholder
59 list<pair<const MethodType *, unsigned> > MethodSignatureList;
62 bool ParseModule (const uchar * Buf, const uchar *End, Module *&);
63 bool ParseModuleGlobalInfo (const uchar *&Buf, const uchar *End, Module *);
64 bool ParseSymbolTable (const uchar *&Buf, const uchar *End);
65 bool ParseMethod (const uchar *&Buf, const uchar *End, Module *);
66 bool ParseBasicBlock (const uchar *&Buf, const uchar *End, BasicBlock *&);
67 bool ParseInstruction (const uchar *&Buf, const uchar *End, Instruction *&);
68 bool ParseRawInst (const uchar *&Buf, const uchar *End, RawInst &);
70 bool ParseConstantPool(const uchar *&Buf, const uchar *EndBuf,
71 SymTabValue::ConstantPoolType &CP, ValueTable &Tab);
74 bool parseConstPoolValue(const uchar *&Buf, const uchar *End,
75 const Type *Ty, ConstPoolVal *&V);
76 bool parseTypeConstant (const uchar *&Buf, const uchar *, ConstPoolVal *&);
78 Value *getValue(const Type *Ty, unsigned num, bool Create = true);
79 const Type *getType(unsigned ID);
81 bool insertValue(Value *D, vector<ValueList> &D);
82 bool postResolveValues(ValueTable &ValTab);
84 bool getTypeSlot(const Type *Ty, unsigned &Slot);
87 template<class SuperType>
88 class PlaceholderDef : public SuperType {
91 PlaceholderDef(const Type *Ty, unsigned id) : SuperType(Ty), ID(id) {}
92 unsigned getID() { return ID; }
95 struct InstPlaceHolderHelper : public Instruction {
96 InstPlaceHolderHelper(const Type *Ty) : Instruction(Ty, UserOp1, "") {}
97 virtual const char *getOpcodeName() const { return "placeholder"; }
99 virtual Instruction *clone() const { abort(); return 0; }
102 struct BBPlaceHolderHelper : public BasicBlock {
103 BBPlaceHolderHelper(const Type *Ty) : BasicBlock() {
104 assert(Ty->isLabelType());
108 struct MethPlaceHolderHelper : public Method {
109 MethPlaceHolderHelper(const Type *Ty)
110 : Method((const MethodType*)Ty) {
111 assert(Ty->isMethodType() && "Method placeholders must be method types!");
115 typedef PlaceholderDef<InstPlaceHolderHelper> DefPHolder;
116 typedef PlaceholderDef<BBPlaceHolderHelper> BBPHolder;
117 typedef PlaceholderDef<MethPlaceHolderHelper> MethPHolder;
119 static inline unsigned getValueIDNumberFromPlaceHolder(Value *Def) {
120 switch (Def->getType()->getPrimitiveID()) {
121 case Type::LabelTyID: return ((BBPHolder*)Def)->getID();
122 case Type::MethodTyID: return ((MethPHolder*)Def)->getID();
123 default: return ((DefPHolder*)Def)->getID();
127 static inline bool readBlock(const uchar *&Buf, const uchar *EndBuf,
128 unsigned &Type, unsigned &Size) {
130 bool Result = read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size);
131 cerr << "StartLoc = " << ((unsigned)Buf & 4095)
132 << " Type = " << Type << " Size = " << Size << endl;
135 return read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size);
140 // failure Template - This template function is used as a place to put
141 // breakpoints in to debug failures of the bytecode parser.
143 template <typename X>
144 static X failure(X Value) {