blob: b3977f6c123b342d8c46b56956182f4f2ea43fb2 [file] [log] [blame]
Chris Lattner00950542001-06-06 20:29:01 +00001//===-- ReaderInternals.h - Definitions internal to the reader ---*- C++ -*--=//
2//
3// This header file defines various stuff that is used by the bytecode reader.
4//
5//===----------------------------------------------------------------------===//
6
7#ifndef READER_INTERNALS_H
8#define READER_INTERNALS_H
9
10#include "llvm/Bytecode/Primitives.h"
11#include "llvm/SymTabValue.h"
12#include "llvm/Method.h"
13#include "llvm/Instruction.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000014#include "llvm/DerivedTypes.h"
Chris Lattner00950542001-06-06 20:29:01 +000015#include <map>
16#include <utility>
Chris Lattner3ff43872001-09-28 22:56:31 +000017#include <list>
Chris Lattner00950542001-06-06 20:29:01 +000018
Chris Lattner1d670cc2001-09-07 16:37:43 +000019// Enable to trace to figure out what the heck is going on when parsing fails
Chris Lattnerf6086082001-10-24 05:14:35 +000020#define TRACE_LEVEL 0
Chris Lattner1d670cc2001-09-07 16:37:43 +000021
22#if TRACE_LEVEL // ByteCodeReading_TRACEer
23#include "llvm/Assembly/Writer.h"
24#define BCR_TRACE(n, X) if (n < TRACE_LEVEL) cerr << string(n*2, ' ') << X
25#else
26#define BCR_TRACE(n, X)
27#endif
28
Chris Lattner00950542001-06-06 20:29:01 +000029class BasicBlock;
30class Method;
31class Module;
32class Type;
Chris Lattner05950c32001-10-13 06:47:01 +000033class PointerType;
Chris Lattner00950542001-06-06 20:29:01 +000034
35typedef unsigned char uchar;
36
37struct RawInst { // The raw fields out of the bytecode stream...
38 unsigned NumOperands;
39 unsigned Opcode;
40 const Type *Ty;
41 unsigned Arg1, Arg2;
42 union {
43 unsigned Arg3;
44 vector<unsigned> *VarArgs; // Contains arg #3,4,5... if NumOperands > 3
45 };
46};
47
Chris Lattner1d670cc2001-09-07 16:37:43 +000048class BytecodeParser : public AbstractTypeUser {
Chris Lattnerd6b65252001-10-24 01:15:12 +000049 string Error; // Error message string goes here...
Chris Lattner00950542001-06-06 20:29:01 +000050public:
51 BytecodeParser() {
52 // Define this in case we don't see a ModuleGlobalInfo block.
53 FirstDerivedTyID = Type::FirstDerivedTyID;
54 }
55
56 Module *ParseBytecode(const uchar *Buf, const uchar *EndBuf);
Chris Lattnerd6b65252001-10-24 01:15:12 +000057
58 string getError() const { return Error; }
59
Chris Lattner00950542001-06-06 20:29:01 +000060private: // All of this data is transient across calls to ParseBytecode
Chris Lattner05950c32001-10-13 06:47:01 +000061 Module *TheModule; // Current Module being read into...
62
Chris Lattner00950542001-06-06 20:29:01 +000063 typedef vector<Value *> ValueList;
64 typedef vector<ValueList> ValueTable;
Chris Lattner00950542001-06-06 20:29:01 +000065 ValueTable Values, LateResolveValues;
66 ValueTable ModuleValues, LateResolveModuleValues;
Chris Lattner1d670cc2001-09-07 16:37:43 +000067
Chris Lattner05950c32001-10-13 06:47:01 +000068 // GlobalRefs - This maintains a mapping between <Type, Slot #>'s and forward
69 // references to global values. Global values may be referenced before they
70 // are defined, and if so, the temporary object that they represent is held
71 // here.
72 //
73 typedef map<pair<const PointerType *, unsigned>, GlobalVariable*>
74 GlobalRefsType;
75 GlobalRefsType GlobalRefs;
76
Chris Lattner1d670cc2001-09-07 16:37:43 +000077 // TypesLoaded - This vector mirrors the Values[TypeTyID] plane. It is used
78 // to deal with forward references to types.
79 //
80 typedef vector<PATypeHandle<Type> > TypeValuesListTy;
81 TypeValuesListTy ModuleTypeValues;
82 TypeValuesListTy MethodTypeValues;
Chris Lattner00950542001-06-06 20:29:01 +000083
84 // Information read from the ModuleGlobalInfo section of the file...
85 unsigned FirstDerivedTyID;
86
87 // When the ModuleGlobalInfo section is read, we load the type of each method
88 // and the 'ModuleValues' slot that it lands in. We then load a placeholder
89 // into its slot to reserve it. When the method is loaded, this placeholder
90 // is replaced.
91 //
Chris Lattneref9c23f2001-10-03 14:53:21 +000092 list<pair<const PointerType *, unsigned> > MethodSignatureList;
Chris Lattner00950542001-06-06 20:29:01 +000093
94private:
95 bool ParseModule (const uchar * Buf, const uchar *End, Module *&);
96 bool ParseModuleGlobalInfo (const uchar *&Buf, const uchar *End, Module *);
Chris Lattner1d670cc2001-09-07 16:37:43 +000097 bool ParseSymbolTable (const uchar *&Buf, const uchar *End, SymbolTable *);
98 bool ParseMethod (const uchar *&Buf, const uchar *End, Module *);
Chris Lattner00950542001-06-06 20:29:01 +000099 bool ParseBasicBlock (const uchar *&Buf, const uchar *End, BasicBlock *&);
100 bool ParseInstruction (const uchar *&Buf, const uchar *End, Instruction *&);
101 bool ParseRawInst (const uchar *&Buf, const uchar *End, RawInst &);
102
103 bool ParseConstantPool(const uchar *&Buf, const uchar *EndBuf,
Chris Lattner1d670cc2001-09-07 16:37:43 +0000104 ValueTable &Tab, TypeValuesListTy &TypeTab);
Chris Lattner00950542001-06-06 20:29:01 +0000105 bool parseConstPoolValue(const uchar *&Buf, const uchar *End,
106 const Type *Ty, ConstPoolVal *&V);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000107 bool parseTypeConstants(const uchar *&Buf, const uchar *EndBuf,
108 TypeValuesListTy &Tab, unsigned NumEntries);
109 const Type *parseTypeConstant(const uchar *&Buf, const uchar *EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +0000110
111 Value *getValue(const Type *Ty, unsigned num, bool Create = true);
112 const Type *getType(unsigned ID);
113
Chris Lattner05950c32001-10-13 06:47:01 +0000114 int insertValue(Value *D, vector<ValueList> &D); // -1 = Failure
Chris Lattner00950542001-06-06 20:29:01 +0000115 bool postResolveValues(ValueTable &ValTab);
116
117 bool getTypeSlot(const Type *Ty, unsigned &Slot);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000118
Chris Lattner05950c32001-10-13 06:47:01 +0000119 // DeclareNewGlobalValue - Patch up forward references to global values in the
Chris Lattnerc18545d2001-10-15 13:21:42 +0000120 // form of ConstPoolPointerRefs.
Chris Lattner05950c32001-10-13 06:47:01 +0000121 //
122 void DeclareNewGlobalValue(GlobalValue *GV, unsigned Slot);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000123
124 // refineAbstractType - The callback method is invoked when one of the
125 // elements of TypeValues becomes more concrete...
126 //
127 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
Chris Lattner00950542001-06-06 20:29:01 +0000128};
129
130template<class SuperType>
131class PlaceholderDef : public SuperType {
132 unsigned ID;
133public:
134 PlaceholderDef(const Type *Ty, unsigned id) : SuperType(Ty), ID(id) {}
135 unsigned getID() { return ID; }
136};
137
138struct InstPlaceHolderHelper : public Instruction {
139 InstPlaceHolderHelper(const Type *Ty) : Instruction(Ty, UserOp1, "") {}
Chris Lattnera41f50d2001-07-07 19:24:15 +0000140 virtual const char *getOpcodeName() const { return "placeholder"; }
Chris Lattner00950542001-06-06 20:29:01 +0000141
142 virtual Instruction *clone() const { abort(); return 0; }
Chris Lattner00950542001-06-06 20:29:01 +0000143};
144
145struct BBPlaceHolderHelper : public BasicBlock {
146 BBPlaceHolderHelper(const Type *Ty) : BasicBlock() {
147 assert(Ty->isLabelType());
148 }
149};
150
151struct MethPlaceHolderHelper : public Method {
152 MethPlaceHolderHelper(const Type *Ty)
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000153 : Method(cast<const MethodType>(Ty), true) {
Chris Lattner00950542001-06-06 20:29:01 +0000154 }
155};
156
157typedef PlaceholderDef<InstPlaceHolderHelper> DefPHolder;
158typedef PlaceholderDef<BBPlaceHolderHelper> BBPHolder;
159typedef PlaceholderDef<MethPlaceHolderHelper> MethPHolder;
160
161static inline unsigned getValueIDNumberFromPlaceHolder(Value *Def) {
162 switch (Def->getType()->getPrimitiveID()) {
163 case Type::LabelTyID: return ((BBPHolder*)Def)->getID();
164 case Type::MethodTyID: return ((MethPHolder*)Def)->getID();
165 default: return ((DefPHolder*)Def)->getID();
166 }
167}
168
169static inline bool readBlock(const uchar *&Buf, const uchar *EndBuf,
170 unsigned &Type, unsigned &Size) {
171#if DEBUG_OUTPUT
172 bool Result = read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size);
173 cerr << "StartLoc = " << ((unsigned)Buf & 4095)
174 << " Type = " << Type << " Size = " << Size << endl;
175 return Result;
176#else
177 return read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size);
178#endif
179}
180
Chris Lattner3d3f2892001-07-28 17:50:18 +0000181
182// failure Template - This template function is used as a place to put
183// breakpoints in to debug failures of the bytecode parser.
184//
185template <typename X>
186static X failure(X Value) {
187 return Value;
188}
189
Chris Lattner00950542001-06-06 20:29:01 +0000190#endif