blob: 75cba07ba8e475ed5ee7351a2bb374a7cdf8fed4 [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"
Chris Lattnerc9aa7df2002-03-29 03:51:11 +000012#include "llvm/Function.h"
Chris Lattner221d6882002-02-12 21:07:25 +000013#include "llvm/BasicBlock.h"
Chris Lattner00950542001-06-06 20:29:01 +000014#include "llvm/Instruction.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000015#include "llvm/DerivedTypes.h"
Chris Lattner00950542001-06-06 20:29:01 +000016#include <map>
17#include <utility>
Chris Lattner3ff43872001-09-28 22:56:31 +000018#include <list>
Chris Lattner00950542001-06-06 20:29:01 +000019
Chris Lattner1d670cc2001-09-07 16:37:43 +000020// Enable to trace to figure out what the heck is going on when parsing fails
Chris Lattnerf6086082001-10-24 05:14:35 +000021#define TRACE_LEVEL 0
Chris Lattner1d670cc2001-09-07 16:37:43 +000022
23#if TRACE_LEVEL // ByteCodeReading_TRACEer
Chris Lattner697954c2002-01-20 22:54:45 +000024#define BCR_TRACE(n, X) if (n < TRACE_LEVEL) cerr << std::string(n*2, ' ') << X
Chris Lattner1d670cc2001-09-07 16:37:43 +000025#else
26#define BCR_TRACE(n, X)
27#endif
28
Chris Lattner00950542001-06-06 20:29:01 +000029typedef unsigned char uchar;
30
31struct RawInst { // The raw fields out of the bytecode stream...
32 unsigned NumOperands;
33 unsigned Opcode;
34 const Type *Ty;
35 unsigned Arg1, Arg2;
36 union {
37 unsigned Arg3;
Chris Lattner697954c2002-01-20 22:54:45 +000038 std::vector<unsigned> *VarArgs; // Contains arg #3,4,5... if NumOperands > 3
Chris Lattner00950542001-06-06 20:29:01 +000039 };
40};
41
Chris Lattner1d670cc2001-09-07 16:37:43 +000042class BytecodeParser : public AbstractTypeUser {
Chris Lattner697954c2002-01-20 22:54:45 +000043 std::string Error; // Error message string goes here...
Chris Lattner00950542001-06-06 20:29:01 +000044public:
45 BytecodeParser() {
46 // Define this in case we don't see a ModuleGlobalInfo block.
47 FirstDerivedTyID = Type::FirstDerivedTyID;
48 }
49
50 Module *ParseBytecode(const uchar *Buf, const uchar *EndBuf);
Chris Lattnerd6b65252001-10-24 01:15:12 +000051
Chris Lattner697954c2002-01-20 22:54:45 +000052 std::string getError() const { return Error; }
Chris Lattnerd6b65252001-10-24 01:15:12 +000053
Chris Lattnerb3afb1f2002-04-04 19:24:11 +000054 void dump() const {
55 cerr << "BytecodeParser instance!\n";
56 }
57
Chris Lattner00950542001-06-06 20:29:01 +000058private: // All of this data is transient across calls to ParseBytecode
Chris Lattner05950c32001-10-13 06:47:01 +000059 Module *TheModule; // Current Module being read into...
60
Chris Lattner697954c2002-01-20 22:54:45 +000061 typedef std::vector<Value *> ValueList;
62 typedef std::vector<ValueList> ValueTable;
Chris Lattner00950542001-06-06 20:29:01 +000063 ValueTable Values, LateResolveValues;
64 ValueTable ModuleValues, LateResolveModuleValues;
Chris Lattner1d670cc2001-09-07 16:37:43 +000065
Chris Lattner05950c32001-10-13 06:47:01 +000066 // GlobalRefs - This maintains a mapping between <Type, Slot #>'s and forward
67 // references to global values. Global values may be referenced before they
68 // are defined, and if so, the temporary object that they represent is held
69 // here.
70 //
Chris Lattner697954c2002-01-20 22:54:45 +000071 typedef std::map<std::pair<const PointerType *, unsigned>,
72 GlobalVariable*> GlobalRefsType;
Chris Lattner05950c32001-10-13 06:47:01 +000073 GlobalRefsType GlobalRefs;
74
Chris Lattner1d670cc2001-09-07 16:37:43 +000075 // TypesLoaded - This vector mirrors the Values[TypeTyID] plane. It is used
76 // to deal with forward references to types.
77 //
Chris Lattner697954c2002-01-20 22:54:45 +000078 typedef std::vector<PATypeHandle<Type> > TypeValuesListTy;
Chris Lattner1d670cc2001-09-07 16:37:43 +000079 TypeValuesListTy ModuleTypeValues;
80 TypeValuesListTy MethodTypeValues;
Chris Lattner00950542001-06-06 20:29:01 +000081
82 // Information read from the ModuleGlobalInfo section of the file...
83 unsigned FirstDerivedTyID;
84
85 // When the ModuleGlobalInfo section is read, we load the type of each method
86 // and the 'ModuleValues' slot that it lands in. We then load a placeholder
87 // into its slot to reserve it. When the method is loaded, this placeholder
88 // is replaced.
89 //
Chris Lattner697954c2002-01-20 22:54:45 +000090 std::list<std::pair<const PointerType *, unsigned> > MethodSignatureList;
Chris Lattner00950542001-06-06 20:29:01 +000091
92private:
Chris Lattner697954c2002-01-20 22:54:45 +000093 bool ParseModule (const uchar * Buf, const uchar *End, Module *&);
94 bool ParseModuleGlobalInfo(const uchar *&Buf, const uchar *End, Module *);
Chris Lattner1d670cc2001-09-07 16:37:43 +000095 bool ParseSymbolTable (const uchar *&Buf, const uchar *End, SymbolTable *);
96 bool ParseMethod (const uchar *&Buf, const uchar *End, Module *);
Chris Lattner00950542001-06-06 20:29:01 +000097 bool ParseBasicBlock (const uchar *&Buf, const uchar *End, BasicBlock *&);
98 bool ParseInstruction (const uchar *&Buf, const uchar *End, Instruction *&);
99 bool ParseRawInst (const uchar *&Buf, const uchar *End, RawInst &);
100
101 bool ParseConstantPool(const uchar *&Buf, const uchar *EndBuf,
Chris Lattner1d670cc2001-09-07 16:37:43 +0000102 ValueTable &Tab, TypeValuesListTy &TypeTab);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000103 bool parseConstantValue(const uchar *&Buf, const uchar *End,
104 const Type *Ty, Constant *&V);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000105 bool parseTypeConstants(const uchar *&Buf, const uchar *EndBuf,
106 TypeValuesListTy &Tab, unsigned NumEntries);
107 const Type *parseTypeConstant(const uchar *&Buf, const uchar *EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +0000108
109 Value *getValue(const Type *Ty, unsigned num, bool Create = true);
110 const Type *getType(unsigned ID);
111
Chris Lattner697954c2002-01-20 22:54:45 +0000112 int insertValue(Value *D, std::vector<ValueList> &D); // -1 = Failure
Chris Lattner00950542001-06-06 20:29:01 +0000113 bool postResolveValues(ValueTable &ValTab);
114
115 bool getTypeSlot(const Type *Ty, unsigned &Slot);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000116
Chris Lattner05950c32001-10-13 06:47:01 +0000117 // DeclareNewGlobalValue - Patch up forward references to global values in the
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000118 // form of ConstantPointerRefs.
Chris Lattner05950c32001-10-13 06:47:01 +0000119 //
120 void DeclareNewGlobalValue(GlobalValue *GV, unsigned Slot);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000121
122 // refineAbstractType - The callback method is invoked when one of the
123 // elements of TypeValues becomes more concrete...
124 //
125 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
Chris Lattner00950542001-06-06 20:29:01 +0000126};
127
128template<class SuperType>
129class PlaceholderDef : public SuperType {
130 unsigned ID;
131public:
132 PlaceholderDef(const Type *Ty, unsigned id) : SuperType(Ty), ID(id) {}
133 unsigned getID() { return ID; }
134};
135
136struct InstPlaceHolderHelper : public Instruction {
137 InstPlaceHolderHelper(const Type *Ty) : Instruction(Ty, UserOp1, "") {}
Chris Lattnera41f50d2001-07-07 19:24:15 +0000138 virtual const char *getOpcodeName() const { return "placeholder"; }
Chris Lattner00950542001-06-06 20:29:01 +0000139
140 virtual Instruction *clone() const { abort(); return 0; }
Chris Lattner00950542001-06-06 20:29:01 +0000141};
142
143struct BBPlaceHolderHelper : public BasicBlock {
144 BBPlaceHolderHelper(const Type *Ty) : BasicBlock() {
Chris Lattner35e309a2002-04-08 21:59:36 +0000145 assert(Ty == Type::LabelTy);
Chris Lattner00950542001-06-06 20:29:01 +0000146 }
147};
148
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000149struct MethPlaceHolderHelper : public Function {
Chris Lattner00950542001-06-06 20:29:01 +0000150 MethPlaceHolderHelper(const Type *Ty)
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000151 : Function(cast<const FunctionType>(Ty), true) {
Chris Lattner00950542001-06-06 20:29:01 +0000152 }
153};
154
155typedef PlaceholderDef<InstPlaceHolderHelper> DefPHolder;
156typedef PlaceholderDef<BBPlaceHolderHelper> BBPHolder;
157typedef PlaceholderDef<MethPlaceHolderHelper> MethPHolder;
158
159static inline unsigned getValueIDNumberFromPlaceHolder(Value *Def) {
160 switch (Def->getType()->getPrimitiveID()) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000161 case Type::LabelTyID: return ((BBPHolder*)Def)->getID();
162 case Type::FunctionTyID: return ((MethPHolder*)Def)->getID();
163 default: return ((DefPHolder*)Def)->getID();
Chris Lattner00950542001-06-06 20:29:01 +0000164 }
165}
166
167static inline bool readBlock(const uchar *&Buf, const uchar *EndBuf,
168 unsigned &Type, unsigned &Size) {
169#if DEBUG_OUTPUT
170 bool Result = read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size);
171 cerr << "StartLoc = " << ((unsigned)Buf & 4095)
172 << " Type = " << Type << " Size = " << Size << endl;
173 return Result;
174#else
175 return read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size);
176#endif
177}
178
Chris Lattner3d3f2892001-07-28 17:50:18 +0000179
180// failure Template - This template function is used as a place to put
181// breakpoints in to debug failures of the bytecode parser.
182//
183template <typename X>
184static X failure(X Value) {
185 return Value;
186}
187
Chris Lattner00950542001-06-06 20:29:01 +0000188#endif