blob: a5b3b7acfd66b1b2d444d602549d8f5d7bf30832 [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 +000030class BasicBlock;
31class Method;
32class Module;
33class Type;
Chris Lattner05950c32001-10-13 06:47:01 +000034class PointerType;
Chris Lattner00950542001-06-06 20:29:01 +000035
36typedef unsigned char uchar;
37
38struct RawInst { // The raw fields out of the bytecode stream...
39 unsigned NumOperands;
40 unsigned Opcode;
41 const Type *Ty;
42 unsigned Arg1, Arg2;
43 union {
44 unsigned Arg3;
Chris Lattner697954c2002-01-20 22:54:45 +000045 std::vector<unsigned> *VarArgs; // Contains arg #3,4,5... if NumOperands > 3
Chris Lattner00950542001-06-06 20:29:01 +000046 };
47};
48
Chris Lattner1d670cc2001-09-07 16:37:43 +000049class BytecodeParser : public AbstractTypeUser {
Chris Lattner697954c2002-01-20 22:54:45 +000050 std::string Error; // Error message string goes here...
Chris Lattner00950542001-06-06 20:29:01 +000051public:
52 BytecodeParser() {
53 // Define this in case we don't see a ModuleGlobalInfo block.
54 FirstDerivedTyID = Type::FirstDerivedTyID;
55 }
56
57 Module *ParseBytecode(const uchar *Buf, const uchar *EndBuf);
Chris Lattnerd6b65252001-10-24 01:15:12 +000058
Chris Lattner697954c2002-01-20 22:54:45 +000059 std::string getError() const { return Error; }
Chris Lattnerd6b65252001-10-24 01:15:12 +000060
Chris Lattner00950542001-06-06 20:29:01 +000061private: // All of this data is transient across calls to ParseBytecode
Chris Lattner05950c32001-10-13 06:47:01 +000062 Module *TheModule; // Current Module being read into...
63
Chris Lattner697954c2002-01-20 22:54:45 +000064 typedef std::vector<Value *> ValueList;
65 typedef std::vector<ValueList> ValueTable;
Chris Lattner00950542001-06-06 20:29:01 +000066 ValueTable Values, LateResolveValues;
67 ValueTable ModuleValues, LateResolveModuleValues;
Chris Lattner1d670cc2001-09-07 16:37:43 +000068
Chris Lattner05950c32001-10-13 06:47:01 +000069 // GlobalRefs - This maintains a mapping between <Type, Slot #>'s and forward
70 // references to global values. Global values may be referenced before they
71 // are defined, and if so, the temporary object that they represent is held
72 // here.
73 //
Chris Lattner697954c2002-01-20 22:54:45 +000074 typedef std::map<std::pair<const PointerType *, unsigned>,
75 GlobalVariable*> GlobalRefsType;
Chris Lattner05950c32001-10-13 06:47:01 +000076 GlobalRefsType GlobalRefs;
77
Chris Lattner1d670cc2001-09-07 16:37:43 +000078 // TypesLoaded - This vector mirrors the Values[TypeTyID] plane. It is used
79 // to deal with forward references to types.
80 //
Chris Lattner697954c2002-01-20 22:54:45 +000081 typedef std::vector<PATypeHandle<Type> > TypeValuesListTy;
Chris Lattner1d670cc2001-09-07 16:37:43 +000082 TypeValuesListTy ModuleTypeValues;
83 TypeValuesListTy MethodTypeValues;
Chris Lattner00950542001-06-06 20:29:01 +000084
85 // Information read from the ModuleGlobalInfo section of the file...
86 unsigned FirstDerivedTyID;
87
88 // When the ModuleGlobalInfo section is read, we load the type of each method
89 // and the 'ModuleValues' slot that it lands in. We then load a placeholder
90 // into its slot to reserve it. When the method is loaded, this placeholder
91 // is replaced.
92 //
Chris Lattner697954c2002-01-20 22:54:45 +000093 std::list<std::pair<const PointerType *, unsigned> > MethodSignatureList;
Chris Lattner00950542001-06-06 20:29:01 +000094
95private:
Chris Lattner697954c2002-01-20 22:54:45 +000096 bool ParseModule (const uchar * Buf, const uchar *End, Module *&);
97 bool ParseModuleGlobalInfo(const uchar *&Buf, const uchar *End, Module *);
Chris Lattner1d670cc2001-09-07 16:37:43 +000098 bool ParseSymbolTable (const uchar *&Buf, const uchar *End, SymbolTable *);
99 bool ParseMethod (const uchar *&Buf, const uchar *End, Module *);
Chris Lattner00950542001-06-06 20:29:01 +0000100 bool ParseBasicBlock (const uchar *&Buf, const uchar *End, BasicBlock *&);
101 bool ParseInstruction (const uchar *&Buf, const uchar *End, Instruction *&);
102 bool ParseRawInst (const uchar *&Buf, const uchar *End, RawInst &);
103
104 bool ParseConstantPool(const uchar *&Buf, const uchar *EndBuf,
Chris Lattner1d670cc2001-09-07 16:37:43 +0000105 ValueTable &Tab, TypeValuesListTy &TypeTab);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000106 bool parseConstantValue(const uchar *&Buf, const uchar *End,
107 const Type *Ty, Constant *&V);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000108 bool parseTypeConstants(const uchar *&Buf, const uchar *EndBuf,
109 TypeValuesListTy &Tab, unsigned NumEntries);
110 const Type *parseTypeConstant(const uchar *&Buf, const uchar *EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +0000111
112 Value *getValue(const Type *Ty, unsigned num, bool Create = true);
113 const Type *getType(unsigned ID);
114
Chris Lattner697954c2002-01-20 22:54:45 +0000115 int insertValue(Value *D, std::vector<ValueList> &D); // -1 = Failure
Chris Lattner00950542001-06-06 20:29:01 +0000116 bool postResolveValues(ValueTable &ValTab);
117
118 bool getTypeSlot(const Type *Ty, unsigned &Slot);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000119
Chris Lattner05950c32001-10-13 06:47:01 +0000120 // DeclareNewGlobalValue - Patch up forward references to global values in the
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000121 // form of ConstantPointerRefs.
Chris Lattner05950c32001-10-13 06:47:01 +0000122 //
123 void DeclareNewGlobalValue(GlobalValue *GV, unsigned Slot);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000124
125 // refineAbstractType - The callback method is invoked when one of the
126 // elements of TypeValues becomes more concrete...
127 //
128 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
Chris Lattner00950542001-06-06 20:29:01 +0000129};
130
131template<class SuperType>
132class PlaceholderDef : public SuperType {
133 unsigned ID;
134public:
135 PlaceholderDef(const Type *Ty, unsigned id) : SuperType(Ty), ID(id) {}
136 unsigned getID() { return ID; }
137};
138
139struct InstPlaceHolderHelper : public Instruction {
140 InstPlaceHolderHelper(const Type *Ty) : Instruction(Ty, UserOp1, "") {}
Chris Lattnera41f50d2001-07-07 19:24:15 +0000141 virtual const char *getOpcodeName() const { return "placeholder"; }
Chris Lattner00950542001-06-06 20:29:01 +0000142
143 virtual Instruction *clone() const { abort(); return 0; }
Chris Lattner00950542001-06-06 20:29:01 +0000144};
145
146struct BBPlaceHolderHelper : public BasicBlock {
147 BBPlaceHolderHelper(const Type *Ty) : BasicBlock() {
148 assert(Ty->isLabelType());
149 }
150};
151
152struct MethPlaceHolderHelper : public Method {
153 MethPlaceHolderHelper(const Type *Ty)
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000154 : Method(cast<const MethodType>(Ty), true) {
Chris Lattner00950542001-06-06 20:29:01 +0000155 }
156};
157
158typedef PlaceholderDef<InstPlaceHolderHelper> DefPHolder;
159typedef PlaceholderDef<BBPlaceHolderHelper> BBPHolder;
160typedef PlaceholderDef<MethPlaceHolderHelper> MethPHolder;
161
162static inline unsigned getValueIDNumberFromPlaceHolder(Value *Def) {
163 switch (Def->getType()->getPrimitiveID()) {
164 case Type::LabelTyID: return ((BBPHolder*)Def)->getID();
165 case Type::MethodTyID: return ((MethPHolder*)Def)->getID();
166 default: return ((DefPHolder*)Def)->getID();
167 }
168}
169
170static inline bool readBlock(const uchar *&Buf, const uchar *EndBuf,
171 unsigned &Type, unsigned &Size) {
172#if DEBUG_OUTPUT
173 bool Result = read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size);
174 cerr << "StartLoc = " << ((unsigned)Buf & 4095)
175 << " Type = " << Type << " Size = " << Size << endl;
176 return Result;
177#else
178 return read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size);
179#endif
180}
181
Chris Lattner3d3f2892001-07-28 17:50:18 +0000182
183// failure Template - This template function is used as a place to put
184// breakpoints in to debug failures of the bytecode parser.
185//
186template <typename X>
187static X failure(X Value) {
188 return Value;
189}
190
Chris Lattner00950542001-06-06 20:29:01 +0000191#endif