blob: 4b1974e01963b4270a33a7724f0975e6b128d2fc [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"
Chris Lattnerc9aa7df2002-03-29 03:51:11 +000011#include "llvm/Function.h"
Chris Lattner221d6882002-02-12 21:07:25 +000012#include "llvm/BasicBlock.h"
Chris Lattner00950542001-06-06 20:29:01 +000013#include "llvm/Instruction.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000014#include "llvm/DerivedTypes.h"
Vikram S. Advec668b7c2002-07-14 23:05:09 +000015#include "llvm/Constant.h"
16#include "Support/NonCopyable.h"
Chris Lattner00950542001-06-06 20:29:01 +000017#include <map>
18#include <utility>
Chris Lattner3ff43872001-09-28 22:56:31 +000019#include <list>
Anand Shuklaeea60fc2002-06-25 20:44:04 +000020#include <iostream>
Chris Lattner00950542001-06-06 20:29:01 +000021
Chris Lattner1d670cc2001-09-07 16:37:43 +000022// Enable to trace to figure out what the heck is going on when parsing fails
Chris Lattnerf6086082001-10-24 05:14:35 +000023#define TRACE_LEVEL 0
Chris Lattner1d670cc2001-09-07 16:37:43 +000024
25#if TRACE_LEVEL // ByteCodeReading_TRACEer
Anand Shuklaeea60fc2002-06-25 20:44:04 +000026#define BCR_TRACE(n, X) if (n < TRACE_LEVEL) std::cerr << std::string(n*2, ' ') << X
Chris Lattner1d670cc2001-09-07 16:37:43 +000027#else
28#define BCR_TRACE(n, X)
29#endif
30
Chris Lattner00950542001-06-06 20:29:01 +000031typedef unsigned char uchar;
32
33struct RawInst { // The raw fields out of the bytecode stream...
34 unsigned NumOperands;
35 unsigned Opcode;
36 const Type *Ty;
37 unsigned Arg1, Arg2;
38 union {
39 unsigned Arg3;
Chris Lattner697954c2002-01-20 22:54:45 +000040 std::vector<unsigned> *VarArgs; // Contains arg #3,4,5... if NumOperands > 3
Chris Lattner00950542001-06-06 20:29:01 +000041 };
42};
43
Vikram S. Advec668b7c2002-07-14 23:05:09 +000044
45class ConstantFwdRefs: public NonCopyable {
46 Module* TheModule;
47
48 // GlobalRefs - This maintains a mapping between <Type, Slot #>'s and forward
49 // references to global values or constants. Such values may be referenced
50 // before they are defined, and if so, the temporary object that they
51 // represent is held here.
52 //
53 typedef std::map<std::pair<const Type *, unsigned>,
54 Value*> GlobalRefsType;
55 GlobalRefsType GlobalRefs;
56
57 Value* find (const Type* Ty, unsigned Slot);
58 void insert (const Type* Ty, unsigned Slot, Value* V);
59 void erase (const Type* Ty, unsigned Slot);
60
61public:
62 // sets the current module pointer: needed to insert placeholder globals
63 void VisitingModule (Module* M) { TheModule = M; }
64
65 // get a forward reference to a global or a constant
66 GlobalValue* GetFwdRefToGlobal (const PointerType* PT, unsigned Slot);
67 Constant* GetFwdRefToConstant (const Type* Ty, unsigned Slot);
68
69 // resolve all references to the placeholder (if any) for the given value
70 void ResolveRefsToValue (Value* val, unsigned Slot);
71};
72
73
Chris Lattner1d670cc2001-09-07 16:37:43 +000074class BytecodeParser : public AbstractTypeUser {
Chris Lattner697954c2002-01-20 22:54:45 +000075 std::string Error; // Error message string goes here...
Chris Lattner00950542001-06-06 20:29:01 +000076public:
77 BytecodeParser() {
78 // Define this in case we don't see a ModuleGlobalInfo block.
79 FirstDerivedTyID = Type::FirstDerivedTyID;
80 }
81
82 Module *ParseBytecode(const uchar *Buf, const uchar *EndBuf);
Chris Lattnerd6b65252001-10-24 01:15:12 +000083
Chris Lattner697954c2002-01-20 22:54:45 +000084 std::string getError() const { return Error; }
Chris Lattnerd6b65252001-10-24 01:15:12 +000085
Chris Lattnerb3afb1f2002-04-04 19:24:11 +000086 void dump() const {
Anand Shuklaeea60fc2002-06-25 20:44:04 +000087 std::cerr << "BytecodeParser instance!\n";
Chris Lattnerb3afb1f2002-04-04 19:24:11 +000088 }
89
Chris Lattner00950542001-06-06 20:29:01 +000090private: // All of this data is transient across calls to ParseBytecode
Chris Lattner05950c32001-10-13 06:47:01 +000091 Module *TheModule; // Current Module being read into...
92
Chris Lattner697954c2002-01-20 22:54:45 +000093 typedef std::vector<Value *> ValueList;
94 typedef std::vector<ValueList> ValueTable;
Chris Lattner00950542001-06-06 20:29:01 +000095 ValueTable Values, LateResolveValues;
96 ValueTable ModuleValues, LateResolveModuleValues;
Chris Lattner1d670cc2001-09-07 16:37:43 +000097
Vikram S. Advec668b7c2002-07-14 23:05:09 +000098 // fwdRefs - This manages forward references to global values.
99 ConstantFwdRefs fwdRefs;
Chris Lattner05950c32001-10-13 06:47:01 +0000100
Chris Lattner1d670cc2001-09-07 16:37:43 +0000101 // TypesLoaded - This vector mirrors the Values[TypeTyID] plane. It is used
102 // to deal with forward references to types.
103 //
Chris Lattner697954c2002-01-20 22:54:45 +0000104 typedef std::vector<PATypeHandle<Type> > TypeValuesListTy;
Chris Lattner1d670cc2001-09-07 16:37:43 +0000105 TypeValuesListTy ModuleTypeValues;
106 TypeValuesListTy MethodTypeValues;
Chris Lattner00950542001-06-06 20:29:01 +0000107
108 // Information read from the ModuleGlobalInfo section of the file...
109 unsigned FirstDerivedTyID;
110
111 // When the ModuleGlobalInfo section is read, we load the type of each method
112 // and the 'ModuleValues' slot that it lands in. We then load a placeholder
113 // into its slot to reserve it. When the method is loaded, this placeholder
114 // is replaced.
115 //
Chris Lattner697954c2002-01-20 22:54:45 +0000116 std::list<std::pair<const PointerType *, unsigned> > MethodSignatureList;
Chris Lattner00950542001-06-06 20:29:01 +0000117
118private:
Chris Lattner697954c2002-01-20 22:54:45 +0000119 bool ParseModule (const uchar * Buf, const uchar *End, Module *&);
120 bool ParseModuleGlobalInfo(const uchar *&Buf, const uchar *End, Module *);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000121 bool ParseSymbolTable (const uchar *&Buf, const uchar *End, SymbolTable *);
122 bool ParseMethod (const uchar *&Buf, const uchar *End, Module *);
Chris Lattner00950542001-06-06 20:29:01 +0000123 bool ParseBasicBlock (const uchar *&Buf, const uchar *End, BasicBlock *&);
124 bool ParseInstruction (const uchar *&Buf, const uchar *End, Instruction *&);
125 bool ParseRawInst (const uchar *&Buf, const uchar *End, RawInst &);
126
127 bool ParseConstantPool(const uchar *&Buf, const uchar *EndBuf,
Chris Lattner1d670cc2001-09-07 16:37:43 +0000128 ValueTable &Tab, TypeValuesListTy &TypeTab);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000129 bool parseConstantValue(const uchar *&Buf, const uchar *End,
130 const Type *Ty, Constant *&V);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000131 bool parseTypeConstants(const uchar *&Buf, const uchar *EndBuf,
132 TypeValuesListTy &Tab, unsigned NumEntries);
133 const Type *parseTypeConstant(const uchar *&Buf, const uchar *EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +0000134
135 Value *getValue(const Type *Ty, unsigned num, bool Create = true);
136 const Type *getType(unsigned ID);
137
Chris Lattner697954c2002-01-20 22:54:45 +0000138 int insertValue(Value *D, std::vector<ValueList> &D); // -1 = Failure
Chris Lattner00950542001-06-06 20:29:01 +0000139 bool postResolveValues(ValueTable &ValTab);
140
141 bool getTypeSlot(const Type *Ty, unsigned &Slot);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000142
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000143 // resolveRefsToGlobal -- resolve forward references to a global
144 // resolveRefsToConstant -- resolve forward references to a constant
145 //
146 void resolveRefsToGlobal(GlobalValue* GV, unsigned Slot);
147 void resolveRefsToConstant(Constant* C, unsigned Slot);
148
Chris Lattner1d670cc2001-09-07 16:37:43 +0000149 // refineAbstractType - The callback method is invoked when one of the
150 // elements of TypeValues becomes more concrete...
151 //
152 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
Chris Lattner00950542001-06-06 20:29:01 +0000153};
154
155template<class SuperType>
156class PlaceholderDef : public SuperType {
157 unsigned ID;
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000158 PlaceholderDef(); // do not implement
Chris Lattner00950542001-06-06 20:29:01 +0000159public:
160 PlaceholderDef(const Type *Ty, unsigned id) : SuperType(Ty), ID(id) {}
161 unsigned getID() { return ID; }
162};
163
164struct InstPlaceHolderHelper : public Instruction {
165 InstPlaceHolderHelper(const Type *Ty) : Instruction(Ty, UserOp1, "") {}
Chris Lattnera41f50d2001-07-07 19:24:15 +0000166 virtual const char *getOpcodeName() const { return "placeholder"; }
Chris Lattner00950542001-06-06 20:29:01 +0000167
168 virtual Instruction *clone() const { abort(); return 0; }
Chris Lattner00950542001-06-06 20:29:01 +0000169};
170
171struct BBPlaceHolderHelper : public BasicBlock {
172 BBPlaceHolderHelper(const Type *Ty) : BasicBlock() {
Chris Lattner35e309a2002-04-08 21:59:36 +0000173 assert(Ty == Type::LabelTy);
Chris Lattner00950542001-06-06 20:29:01 +0000174 }
175};
176
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000177struct MethPlaceHolderHelper : public Function {
Chris Lattner00950542001-06-06 20:29:01 +0000178 MethPlaceHolderHelper(const Type *Ty)
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000179 : Function(cast<const FunctionType>(Ty), true) {
Chris Lattner00950542001-06-06 20:29:01 +0000180 }
181};
182
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000183struct ConstantPlaceHolderHelper : public Constant {
184 ConstantPlaceHolderHelper(const Type *Ty)
185 : Constant(Ty) {}
186 virtual bool isNullValue() const { return false; }
187};
188
Chris Lattner00950542001-06-06 20:29:01 +0000189typedef PlaceholderDef<InstPlaceHolderHelper> DefPHolder;
190typedef PlaceholderDef<BBPlaceHolderHelper> BBPHolder;
191typedef PlaceholderDef<MethPlaceHolderHelper> MethPHolder;
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000192typedef PlaceholderDef<ConstantPlaceHolderHelper> ConstPHolder;
193
Chris Lattner00950542001-06-06 20:29:01 +0000194
195static inline unsigned getValueIDNumberFromPlaceHolder(Value *Def) {
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000196 if (isa<Constant>(Def))
197 return ((ConstPHolder*)Def)->getID();
198
199 // else discriminate by type
Chris Lattner00950542001-06-06 20:29:01 +0000200 switch (Def->getType()->getPrimitiveID()) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000201 case Type::LabelTyID: return ((BBPHolder*)Def)->getID();
202 case Type::FunctionTyID: return ((MethPHolder*)Def)->getID();
203 default: return ((DefPHolder*)Def)->getID();
Chris Lattner00950542001-06-06 20:29:01 +0000204 }
205}
206
207static inline bool readBlock(const uchar *&Buf, const uchar *EndBuf,
208 unsigned &Type, unsigned &Size) {
209#if DEBUG_OUTPUT
210 bool Result = read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size);
Anand Shuklaeea60fc2002-06-25 20:44:04 +0000211 std::cerr << "StartLoc = " << ((unsigned)Buf & 4095)
Chris Lattner00950542001-06-06 20:29:01 +0000212 << " Type = " << Type << " Size = " << Size << endl;
213 return Result;
214#else
215 return read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size);
216#endif
217}
218
Chris Lattner3d3f2892001-07-28 17:50:18 +0000219
220// failure Template - This template function is used as a place to put
221// breakpoints in to debug failures of the bytecode parser.
222//
223template <typename X>
224static X failure(X Value) {
225 return Value;
226}
227
Chris Lattner00950542001-06-06 20:29:01 +0000228#endif