blob: 4a21962b162595c7b8d7d9fd70bfeac40cb389f4 [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"
Chris Lattner00950542001-06-06 20:29:01 +000015#include <map>
16#include <utility>
Chris Lattner3ff43872001-09-28 22:56:31 +000017#include <list>
Chris Lattner00950542001-06-06 20:29:01 +000018
Chris Lattner1d670cc2001-09-07 16:37:43 +000019// Enable to trace to figure out what the heck is going on when parsing fails
Chris Lattnerf6086082001-10-24 05:14:35 +000020#define TRACE_LEVEL 0
Chris Lattner1d670cc2001-09-07 16:37:43 +000021
22#if TRACE_LEVEL // ByteCodeReading_TRACEer
Chris Lattner697954c2002-01-20 22:54:45 +000023#define BCR_TRACE(n, X) if (n < TRACE_LEVEL) cerr << std::string(n*2, ' ') << X
Chris Lattner1d670cc2001-09-07 16:37:43 +000024#else
25#define BCR_TRACE(n, X)
26#endif
27
Chris Lattner00950542001-06-06 20:29:01 +000028typedef unsigned char uchar;
29
30struct RawInst { // The raw fields out of the bytecode stream...
31 unsigned NumOperands;
32 unsigned Opcode;
33 const Type *Ty;
34 unsigned Arg1, Arg2;
35 union {
36 unsigned Arg3;
Chris Lattner697954c2002-01-20 22:54:45 +000037 std::vector<unsigned> *VarArgs; // Contains arg #3,4,5... if NumOperands > 3
Chris Lattner00950542001-06-06 20:29:01 +000038 };
39};
40
Chris Lattner1d670cc2001-09-07 16:37:43 +000041class BytecodeParser : public AbstractTypeUser {
Chris Lattner697954c2002-01-20 22:54:45 +000042 std::string Error; // Error message string goes here...
Chris Lattner00950542001-06-06 20:29:01 +000043public:
44 BytecodeParser() {
45 // Define this in case we don't see a ModuleGlobalInfo block.
46 FirstDerivedTyID = Type::FirstDerivedTyID;
47 }
48
49 Module *ParseBytecode(const uchar *Buf, const uchar *EndBuf);
Chris Lattnerd6b65252001-10-24 01:15:12 +000050
Chris Lattner697954c2002-01-20 22:54:45 +000051 std::string getError() const { return Error; }
Chris Lattnerd6b65252001-10-24 01:15:12 +000052
Chris Lattnerb3afb1f2002-04-04 19:24:11 +000053 void dump() const {
54 cerr << "BytecodeParser instance!\n";
55 }
56
Chris Lattner00950542001-06-06 20:29:01 +000057private: // All of this data is transient across calls to ParseBytecode
Chris Lattner05950c32001-10-13 06:47:01 +000058 Module *TheModule; // Current Module being read into...
59
Chris Lattner697954c2002-01-20 22:54:45 +000060 typedef std::vector<Value *> ValueList;
61 typedef std::vector<ValueList> ValueTable;
Chris Lattner00950542001-06-06 20:29:01 +000062 ValueTable Values, LateResolveValues;
63 ValueTable ModuleValues, LateResolveModuleValues;
Chris Lattner1d670cc2001-09-07 16:37:43 +000064
Chris Lattner05950c32001-10-13 06:47:01 +000065 // GlobalRefs - This maintains a mapping between <Type, Slot #>'s and forward
66 // references to global values. Global values may be referenced before they
67 // are defined, and if so, the temporary object that they represent is held
68 // here.
69 //
Chris Lattner697954c2002-01-20 22:54:45 +000070 typedef std::map<std::pair<const PointerType *, unsigned>,
71 GlobalVariable*> GlobalRefsType;
Chris Lattner05950c32001-10-13 06:47:01 +000072 GlobalRefsType GlobalRefs;
73
Chris Lattner1d670cc2001-09-07 16:37:43 +000074 // TypesLoaded - This vector mirrors the Values[TypeTyID] plane. It is used
75 // to deal with forward references to types.
76 //
Chris Lattner697954c2002-01-20 22:54:45 +000077 typedef std::vector<PATypeHandle<Type> > TypeValuesListTy;
Chris Lattner1d670cc2001-09-07 16:37:43 +000078 TypeValuesListTy ModuleTypeValues;
79 TypeValuesListTy MethodTypeValues;
Chris Lattner00950542001-06-06 20:29:01 +000080
81 // Information read from the ModuleGlobalInfo section of the file...
82 unsigned FirstDerivedTyID;
83
84 // When the ModuleGlobalInfo section is read, we load the type of each method
85 // and the 'ModuleValues' slot that it lands in. We then load a placeholder
86 // into its slot to reserve it. When the method is loaded, this placeholder
87 // is replaced.
88 //
Chris Lattner697954c2002-01-20 22:54:45 +000089 std::list<std::pair<const PointerType *, unsigned> > MethodSignatureList;
Chris Lattner00950542001-06-06 20:29:01 +000090
91private:
Chris Lattner697954c2002-01-20 22:54:45 +000092 bool ParseModule (const uchar * Buf, const uchar *End, Module *&);
93 bool ParseModuleGlobalInfo(const uchar *&Buf, const uchar *End, Module *);
Chris Lattner1d670cc2001-09-07 16:37:43 +000094 bool ParseSymbolTable (const uchar *&Buf, const uchar *End, SymbolTable *);
95 bool ParseMethod (const uchar *&Buf, const uchar *End, Module *);
Chris Lattner00950542001-06-06 20:29:01 +000096 bool ParseBasicBlock (const uchar *&Buf, const uchar *End, BasicBlock *&);
97 bool ParseInstruction (const uchar *&Buf, const uchar *End, Instruction *&);
98 bool ParseRawInst (const uchar *&Buf, const uchar *End, RawInst &);
99
100 bool ParseConstantPool(const uchar *&Buf, const uchar *EndBuf,
Chris Lattner1d670cc2001-09-07 16:37:43 +0000101 ValueTable &Tab, TypeValuesListTy &TypeTab);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000102 bool parseConstantValue(const uchar *&Buf, const uchar *End,
103 const Type *Ty, Constant *&V);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000104 bool parseTypeConstants(const uchar *&Buf, const uchar *EndBuf,
105 TypeValuesListTy &Tab, unsigned NumEntries);
106 const Type *parseTypeConstant(const uchar *&Buf, const uchar *EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +0000107
108 Value *getValue(const Type *Ty, unsigned num, bool Create = true);
109 const Type *getType(unsigned ID);
110
Chris Lattner697954c2002-01-20 22:54:45 +0000111 int insertValue(Value *D, std::vector<ValueList> &D); // -1 = Failure
Chris Lattner00950542001-06-06 20:29:01 +0000112 bool postResolveValues(ValueTable &ValTab);
113
114 bool getTypeSlot(const Type *Ty, unsigned &Slot);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000115
Chris Lattner05950c32001-10-13 06:47:01 +0000116 // DeclareNewGlobalValue - Patch up forward references to global values in the
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000117 // form of ConstantPointerRefs.
Chris Lattner05950c32001-10-13 06:47:01 +0000118 //
119 void DeclareNewGlobalValue(GlobalValue *GV, unsigned Slot);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000120
121 // refineAbstractType - The callback method is invoked when one of the
122 // elements of TypeValues becomes more concrete...
123 //
124 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
Chris Lattner00950542001-06-06 20:29:01 +0000125};
126
127template<class SuperType>
128class PlaceholderDef : public SuperType {
129 unsigned ID;
130public:
131 PlaceholderDef(const Type *Ty, unsigned id) : SuperType(Ty), ID(id) {}
132 unsigned getID() { return ID; }
133};
134
135struct InstPlaceHolderHelper : public Instruction {
136 InstPlaceHolderHelper(const Type *Ty) : Instruction(Ty, UserOp1, "") {}
Chris Lattnera41f50d2001-07-07 19:24:15 +0000137 virtual const char *getOpcodeName() const { return "placeholder"; }
Chris Lattner00950542001-06-06 20:29:01 +0000138
139 virtual Instruction *clone() const { abort(); return 0; }
Chris Lattner00950542001-06-06 20:29:01 +0000140};
141
142struct BBPlaceHolderHelper : public BasicBlock {
143 BBPlaceHolderHelper(const Type *Ty) : BasicBlock() {
Chris Lattner35e309a2002-04-08 21:59:36 +0000144 assert(Ty == Type::LabelTy);
Chris Lattner00950542001-06-06 20:29:01 +0000145 }
146};
147
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000148struct MethPlaceHolderHelper : public Function {
Chris Lattner00950542001-06-06 20:29:01 +0000149 MethPlaceHolderHelper(const Type *Ty)
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000150 : Function(cast<const FunctionType>(Ty), true) {
Chris Lattner00950542001-06-06 20:29:01 +0000151 }
152};
153
154typedef PlaceholderDef<InstPlaceHolderHelper> DefPHolder;
155typedef PlaceholderDef<BBPlaceHolderHelper> BBPHolder;
156typedef PlaceholderDef<MethPlaceHolderHelper> MethPHolder;
157
158static inline unsigned getValueIDNumberFromPlaceHolder(Value *Def) {
159 switch (Def->getType()->getPrimitiveID()) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000160 case Type::LabelTyID: return ((BBPHolder*)Def)->getID();
161 case Type::FunctionTyID: return ((MethPHolder*)Def)->getID();
162 default: return ((DefPHolder*)Def)->getID();
Chris Lattner00950542001-06-06 20:29:01 +0000163 }
164}
165
166static inline bool readBlock(const uchar *&Buf, const uchar *EndBuf,
167 unsigned &Type, unsigned &Size) {
168#if DEBUG_OUTPUT
169 bool Result = read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size);
170 cerr << "StartLoc = " << ((unsigned)Buf & 4095)
171 << " Type = " << Type << " Size = " << Size << endl;
172 return Result;
173#else
174 return read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size);
175#endif
176}
177
Chris Lattner3d3f2892001-07-28 17:50:18 +0000178
179// failure Template - This template function is used as a place to put
180// breakpoints in to debug failures of the bytecode parser.
181//
182template <typename X>
183static X failure(X Value) {
184 return Value;
185}
186
Chris Lattner00950542001-06-06 20:29:01 +0000187#endif