blob: 3a8dbb8c2a9bb3de3484c232ccc0cccc517239ae [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"
14#include <map>
15#include <utility>
Chris Lattner3ff43872001-09-28 22:56:31 +000016#include <list>
Chris Lattner00950542001-06-06 20:29:01 +000017
Chris Lattner1d670cc2001-09-07 16:37:43 +000018// Enable to trace to figure out what the heck is going on when parsing fails
Chris Lattnerd6b65252001-10-24 01:15:12 +000019#define TRACE_LEVEL 10
Chris Lattner1d670cc2001-09-07 16:37:43 +000020
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
24#else
25#define BCR_TRACE(n, X)
26#endif
27
Chris Lattner00950542001-06-06 20:29:01 +000028class BasicBlock;
29class Method;
30class Module;
31class Type;
Chris Lattner05950c32001-10-13 06:47:01 +000032class PointerType;
Chris Lattner00950542001-06-06 20:29:01 +000033
34typedef unsigned char uchar;
35
36struct RawInst { // The raw fields out of the bytecode stream...
37 unsigned NumOperands;
38 unsigned Opcode;
39 const Type *Ty;
40 unsigned Arg1, Arg2;
41 union {
42 unsigned Arg3;
43 vector<unsigned> *VarArgs; // Contains arg #3,4,5... if NumOperands > 3
44 };
45};
46
Chris Lattner1d670cc2001-09-07 16:37:43 +000047class BytecodeParser : public AbstractTypeUser {
Chris Lattnerd6b65252001-10-24 01:15:12 +000048 string Error; // Error message string goes here...
Chris Lattner00950542001-06-06 20:29:01 +000049public:
50 BytecodeParser() {
51 // Define this in case we don't see a ModuleGlobalInfo block.
52 FirstDerivedTyID = Type::FirstDerivedTyID;
53 }
54
55 Module *ParseBytecode(const uchar *Buf, const uchar *EndBuf);
Chris Lattnerd6b65252001-10-24 01:15:12 +000056
57 string getError() const { return Error; }
58
Chris Lattner00950542001-06-06 20:29:01 +000059private: // All of this data is transient across calls to ParseBytecode
Chris Lattner05950c32001-10-13 06:47:01 +000060 Module *TheModule; // Current Module being read into...
61
Chris Lattner00950542001-06-06 20:29:01 +000062 typedef vector<Value *> ValueList;
63 typedef vector<ValueList> ValueTable;
Chris Lattner00950542001-06-06 20:29:01 +000064 ValueTable Values, LateResolveValues;
65 ValueTable ModuleValues, LateResolveModuleValues;
Chris Lattner1d670cc2001-09-07 16:37:43 +000066
Chris Lattner05950c32001-10-13 06:47:01 +000067 // GlobalRefs - This maintains a mapping between <Type, Slot #>'s and forward
68 // references to global values. Global values may be referenced before they
69 // are defined, and if so, the temporary object that they represent is held
70 // here.
71 //
72 typedef map<pair<const PointerType *, unsigned>, GlobalVariable*>
73 GlobalRefsType;
74 GlobalRefsType GlobalRefs;
75
Chris Lattner1d670cc2001-09-07 16:37:43 +000076 // TypesLoaded - This vector mirrors the Values[TypeTyID] plane. It is used
77 // to deal with forward references to types.
78 //
79 typedef vector<PATypeHandle<Type> > TypeValuesListTy;
80 TypeValuesListTy ModuleTypeValues;
81 TypeValuesListTy MethodTypeValues;
Chris Lattner00950542001-06-06 20:29:01 +000082
83 // Information read from the ModuleGlobalInfo section of the file...
84 unsigned FirstDerivedTyID;
85
86 // When the ModuleGlobalInfo section is read, we load the type of each method
87 // and the 'ModuleValues' slot that it lands in. We then load a placeholder
88 // into its slot to reserve it. When the method is loaded, this placeholder
89 // is replaced.
90 //
Chris Lattneref9c23f2001-10-03 14:53:21 +000091 list<pair<const PointerType *, unsigned> > MethodSignatureList;
Chris Lattner00950542001-06-06 20:29:01 +000092
93private:
94 bool ParseModule (const uchar * Buf, const uchar *End, Module *&);
95 bool ParseModuleGlobalInfo (const uchar *&Buf, const uchar *End, Module *);
Chris Lattner1d670cc2001-09-07 16:37:43 +000096 bool ParseSymbolTable (const uchar *&Buf, const uchar *End, SymbolTable *);
97 bool ParseMethod (const uchar *&Buf, const uchar *End, Module *);
Chris Lattner00950542001-06-06 20:29:01 +000098 bool ParseBasicBlock (const uchar *&Buf, const uchar *End, BasicBlock *&);
99 bool ParseInstruction (const uchar *&Buf, const uchar *End, Instruction *&);
100 bool ParseRawInst (const uchar *&Buf, const uchar *End, RawInst &);
101
102 bool ParseConstantPool(const uchar *&Buf, const uchar *EndBuf,
Chris Lattner1d670cc2001-09-07 16:37:43 +0000103 ValueTable &Tab, TypeValuesListTy &TypeTab);
Chris Lattner00950542001-06-06 20:29:01 +0000104 bool parseConstPoolValue(const uchar *&Buf, const uchar *End,
105 const Type *Ty, ConstPoolVal *&V);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000106 bool parseTypeConstants(const uchar *&Buf, const uchar *EndBuf,
107 TypeValuesListTy &Tab, unsigned NumEntries);
108 const Type *parseTypeConstant(const uchar *&Buf, const uchar *EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +0000109
110 Value *getValue(const Type *Ty, unsigned num, bool Create = true);
111 const Type *getType(unsigned ID);
112
Chris Lattner05950c32001-10-13 06:47:01 +0000113 int insertValue(Value *D, vector<ValueList> &D); // -1 = Failure
Chris Lattner00950542001-06-06 20:29:01 +0000114 bool postResolveValues(ValueTable &ValTab);
115
116 bool getTypeSlot(const Type *Ty, unsigned &Slot);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000117
Chris Lattner05950c32001-10-13 06:47:01 +0000118 // DeclareNewGlobalValue - Patch up forward references to global values in the
Chris Lattnerc18545d2001-10-15 13:21:42 +0000119 // form of ConstPoolPointerRefs.
Chris Lattner05950c32001-10-13 06:47:01 +0000120 //
121 void DeclareNewGlobalValue(GlobalValue *GV, unsigned Slot);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000122
123 // refineAbstractType - The callback method is invoked when one of the
124 // elements of TypeValues becomes more concrete...
125 //
126 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
Chris Lattner00950542001-06-06 20:29:01 +0000127};
128
129template<class SuperType>
130class PlaceholderDef : public SuperType {
131 unsigned ID;
132public:
133 PlaceholderDef(const Type *Ty, unsigned id) : SuperType(Ty), ID(id) {}
134 unsigned getID() { return ID; }
135};
136
137struct InstPlaceHolderHelper : public Instruction {
138 InstPlaceHolderHelper(const Type *Ty) : Instruction(Ty, UserOp1, "") {}
Chris Lattnera41f50d2001-07-07 19:24:15 +0000139 virtual const char *getOpcodeName() const { return "placeholder"; }
Chris Lattner00950542001-06-06 20:29:01 +0000140
141 virtual Instruction *clone() const { abort(); return 0; }
Chris Lattner00950542001-06-06 20:29:01 +0000142};
143
144struct BBPlaceHolderHelper : public BasicBlock {
145 BBPlaceHolderHelper(const Type *Ty) : BasicBlock() {
146 assert(Ty->isLabelType());
147 }
148};
149
150struct MethPlaceHolderHelper : public Method {
151 MethPlaceHolderHelper(const Type *Ty)
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000152 : Method(cast<const MethodType>(Ty)) {
Chris Lattner00950542001-06-06 20:29:01 +0000153 }
154};
155
156typedef PlaceholderDef<InstPlaceHolderHelper> DefPHolder;
157typedef PlaceholderDef<BBPlaceHolderHelper> BBPHolder;
158typedef PlaceholderDef<MethPlaceHolderHelper> MethPHolder;
159
160static inline unsigned getValueIDNumberFromPlaceHolder(Value *Def) {
161 switch (Def->getType()->getPrimitiveID()) {
162 case Type::LabelTyID: return ((BBPHolder*)Def)->getID();
163 case Type::MethodTyID: return ((MethPHolder*)Def)->getID();
164 default: return ((DefPHolder*)Def)->getID();
165 }
166}
167
168static inline bool readBlock(const uchar *&Buf, const uchar *EndBuf,
169 unsigned &Type, unsigned &Size) {
170#if DEBUG_OUTPUT
171 bool Result = read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size);
172 cerr << "StartLoc = " << ((unsigned)Buf & 4095)
173 << " Type = " << Type << " Size = " << Size << endl;
174 return Result;
175#else
176 return read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size);
177#endif
178}
179
Chris Lattner3d3f2892001-07-28 17:50:18 +0000180
181// failure Template - This template function is used as a place to put
182// breakpoints in to debug failures of the bytecode parser.
183//
184template <typename X>
185static X failure(X Value) {
186 return Value;
187}
188
Chris Lattner00950542001-06-06 20:29:01 +0000189#endif