blob: 1d9903dcdeb686bd95e3f18be3dea64919cf542d [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
19#define TRACE_LEVEL 0
20
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 Lattner00950542001-06-06 20:29:01 +000048public:
49 BytecodeParser() {
50 // Define this in case we don't see a ModuleGlobalInfo block.
51 FirstDerivedTyID = Type::FirstDerivedTyID;
52 }
53
54 Module *ParseBytecode(const uchar *Buf, const uchar *EndBuf);
55private: // All of this data is transient across calls to ParseBytecode
Chris Lattner05950c32001-10-13 06:47:01 +000056 Module *TheModule; // Current Module being read into...
57
Chris Lattner00950542001-06-06 20:29:01 +000058 typedef vector<Value *> ValueList;
59 typedef vector<ValueList> ValueTable;
Chris Lattner00950542001-06-06 20:29:01 +000060 ValueTable Values, LateResolveValues;
61 ValueTable ModuleValues, LateResolveModuleValues;
Chris Lattner1d670cc2001-09-07 16:37:43 +000062
Chris Lattner05950c32001-10-13 06:47:01 +000063 // 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
66 // here.
67 //
68 typedef map<pair<const PointerType *, unsigned>, GlobalVariable*>
69 GlobalRefsType;
70 GlobalRefsType GlobalRefs;
71
Chris Lattner1d670cc2001-09-07 16:37:43 +000072 // TypesLoaded - This vector mirrors the Values[TypeTyID] plane. It is used
73 // to deal with forward references to types.
74 //
75 typedef vector<PATypeHandle<Type> > TypeValuesListTy;
76 TypeValuesListTy ModuleTypeValues;
77 TypeValuesListTy MethodTypeValues;
Chris Lattner00950542001-06-06 20:29:01 +000078
79 // Information read from the ModuleGlobalInfo section of the file...
80 unsigned FirstDerivedTyID;
81
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
85 // is replaced.
86 //
Chris Lattneref9c23f2001-10-03 14:53:21 +000087 list<pair<const PointerType *, unsigned> > MethodSignatureList;
Chris Lattner00950542001-06-06 20:29:01 +000088
89private:
90 bool ParseModule (const uchar * Buf, const uchar *End, Module *&);
91 bool ParseModuleGlobalInfo (const uchar *&Buf, const uchar *End, Module *);
Chris Lattner1d670cc2001-09-07 16:37:43 +000092 bool ParseSymbolTable (const uchar *&Buf, const uchar *End, SymbolTable *);
93 bool ParseMethod (const uchar *&Buf, const uchar *End, Module *);
Chris Lattner00950542001-06-06 20:29:01 +000094 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 &);
97
98 bool ParseConstantPool(const uchar *&Buf, const uchar *EndBuf,
Chris Lattner1d670cc2001-09-07 16:37:43 +000099 ValueTable &Tab, TypeValuesListTy &TypeTab);
Chris Lattner00950542001-06-06 20:29:01 +0000100 bool parseConstPoolValue(const uchar *&Buf, const uchar *End,
101 const Type *Ty, ConstPoolVal *&V);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000102 bool parseTypeConstants(const uchar *&Buf, const uchar *EndBuf,
103 TypeValuesListTy &Tab, unsigned NumEntries);
104 const Type *parseTypeConstant(const uchar *&Buf, const uchar *EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +0000105
106 Value *getValue(const Type *Ty, unsigned num, bool Create = true);
107 const Type *getType(unsigned ID);
108
Chris Lattner05950c32001-10-13 06:47:01 +0000109 int insertValue(Value *D, vector<ValueList> &D); // -1 = Failure
Chris Lattner00950542001-06-06 20:29:01 +0000110 bool postResolveValues(ValueTable &ValTab);
111
112 bool getTypeSlot(const Type *Ty, unsigned &Slot);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000113
Chris Lattner05950c32001-10-13 06:47:01 +0000114 // DeclareNewGlobalValue - Patch up forward references to global values in the
Chris Lattnerc18545d2001-10-15 13:21:42 +0000115 // form of ConstPoolPointerRefs.
Chris Lattner05950c32001-10-13 06:47:01 +0000116 //
117 void DeclareNewGlobalValue(GlobalValue *GV, unsigned Slot);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000118
119 // refineAbstractType - The callback method is invoked when one of the
120 // elements of TypeValues becomes more concrete...
121 //
122 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
Chris Lattner00950542001-06-06 20:29:01 +0000123};
124
125template<class SuperType>
126class PlaceholderDef : public SuperType {
127 unsigned ID;
128public:
129 PlaceholderDef(const Type *Ty, unsigned id) : SuperType(Ty), ID(id) {}
130 unsigned getID() { return ID; }
131};
132
133struct InstPlaceHolderHelper : public Instruction {
134 InstPlaceHolderHelper(const Type *Ty) : Instruction(Ty, UserOp1, "") {}
Chris Lattnera41f50d2001-07-07 19:24:15 +0000135 virtual const char *getOpcodeName() const { return "placeholder"; }
Chris Lattner00950542001-06-06 20:29:01 +0000136
137 virtual Instruction *clone() const { abort(); return 0; }
Chris Lattner00950542001-06-06 20:29:01 +0000138};
139
140struct BBPlaceHolderHelper : public BasicBlock {
141 BBPlaceHolderHelper(const Type *Ty) : BasicBlock() {
142 assert(Ty->isLabelType());
143 }
144};
145
146struct MethPlaceHolderHelper : public Method {
147 MethPlaceHolderHelper(const Type *Ty)
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000148 : Method(cast<const MethodType>(Ty)) {
Chris Lattner00950542001-06-06 20:29:01 +0000149 }
150};
151
152typedef PlaceholderDef<InstPlaceHolderHelper> DefPHolder;
153typedef PlaceholderDef<BBPlaceHolderHelper> BBPHolder;
154typedef PlaceholderDef<MethPlaceHolderHelper> MethPHolder;
155
156static 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();
161 }
162}
163
164static inline bool readBlock(const uchar *&Buf, const uchar *EndBuf,
165 unsigned &Type, unsigned &Size) {
166#if DEBUG_OUTPUT
167 bool Result = read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size);
168 cerr << "StartLoc = " << ((unsigned)Buf & 4095)
169 << " Type = " << Type << " Size = " << Size << endl;
170 return Result;
171#else
172 return read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size);
173#endif
174}
175
Chris Lattner3d3f2892001-07-28 17:50:18 +0000176
177// failure Template - This template function is used as a place to put
178// breakpoints in to debug failures of the bytecode parser.
179//
180template <typename X>
181static X failure(X Value) {
182 return Value;
183}
184
Chris Lattner00950542001-06-06 20:29:01 +0000185#endif