blob: 54cda44a6b49da07981354cfe3a1e9005dbbc0ed [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"
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
24#include "llvm/Assembly/Writer.h"
Chris Lattner697954c2002-01-20 22:54:45 +000025#define BCR_TRACE(n, X) if (n < TRACE_LEVEL) cerr << std::string(n*2, ' ') << X
Chris Lattner1d670cc2001-09-07 16:37:43 +000026#else
27#define BCR_TRACE(n, X)
28#endif
29
Chris Lattner00950542001-06-06 20:29:01 +000030typedef unsigned char uchar;
31
32struct RawInst { // The raw fields out of the bytecode stream...
33 unsigned NumOperands;
34 unsigned Opcode;
35 const Type *Ty;
36 unsigned Arg1, Arg2;
37 union {
38 unsigned Arg3;
Chris Lattner697954c2002-01-20 22:54:45 +000039 std::vector<unsigned> *VarArgs; // Contains arg #3,4,5... if NumOperands > 3
Chris Lattner00950542001-06-06 20:29:01 +000040 };
41};
42
Chris Lattner1d670cc2001-09-07 16:37:43 +000043class BytecodeParser : public AbstractTypeUser {
Chris Lattner697954c2002-01-20 22:54:45 +000044 std::string Error; // Error message string goes here...
Chris Lattner00950542001-06-06 20:29:01 +000045public:
46 BytecodeParser() {
47 // Define this in case we don't see a ModuleGlobalInfo block.
48 FirstDerivedTyID = Type::FirstDerivedTyID;
49 }
50
51 Module *ParseBytecode(const uchar *Buf, const uchar *EndBuf);
Chris Lattnerd6b65252001-10-24 01:15:12 +000052
Chris Lattner697954c2002-01-20 22:54:45 +000053 std::string getError() const { return Error; }
Chris Lattnerd6b65252001-10-24 01:15:12 +000054
Chris Lattner00950542001-06-06 20:29:01 +000055private: // 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 Lattner697954c2002-01-20 22:54:45 +000058 typedef std::vector<Value *> ValueList;
59 typedef std::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 //
Chris Lattner697954c2002-01-20 22:54:45 +000068 typedef std::map<std::pair<const PointerType *, unsigned>,
69 GlobalVariable*> GlobalRefsType;
Chris Lattner05950c32001-10-13 06:47:01 +000070 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 //
Chris Lattner697954c2002-01-20 22:54:45 +000075 typedef std::vector<PATypeHandle<Type> > TypeValuesListTy;
Chris Lattner1d670cc2001-09-07 16:37:43 +000076 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 Lattner697954c2002-01-20 22:54:45 +000087 std::list<std::pair<const PointerType *, unsigned> > MethodSignatureList;
Chris Lattner00950542001-06-06 20:29:01 +000088
89private:
Chris Lattner697954c2002-01-20 22:54:45 +000090 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 Lattnere9bb2df2001-12-03 22:26:30 +0000100 bool parseConstantValue(const uchar *&Buf, const uchar *End,
101 const Type *Ty, Constant *&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 Lattner697954c2002-01-20 22:54:45 +0000109 int insertValue(Value *D, std::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 Lattnere9bb2df2001-12-03 22:26:30 +0000115 // form of ConstantPointerRefs.
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 Lattnerd23b1d32001-11-26 18:56:10 +0000148 : Method(cast<const MethodType>(Ty), true) {
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