blob: fdaf13d13c1ff993b568661497675aaac98328f4 [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 Lattner7061dc52001-12-03 18:02:31 +000011#include "llvm/DerivedTypes.h"
Chris Lattner74734132002-08-17 22:01:27 +000012#include "llvm/Function.h"
Vikram S. Advec668b7c2002-07-14 23:05:09 +000013#include "llvm/Constant.h"
Chris Lattner00950542001-06-06 20:29:01 +000014#include <utility>
Chris Lattner74734132002-08-17 22:01:27 +000015#include <map>
Chris Lattner00950542001-06-06 20:29:01 +000016
Chris Lattner1d670cc2001-09-07 16:37:43 +000017// Enable to trace to figure out what the heck is going on when parsing fails
Chris Lattnerf6086082001-10-24 05:14:35 +000018#define TRACE_LEVEL 0
Chris Lattner1d670cc2001-09-07 16:37:43 +000019
20#if TRACE_LEVEL // ByteCodeReading_TRACEer
Anand Shuklaeea60fc2002-06-25 20:44:04 +000021#define BCR_TRACE(n, X) if (n < TRACE_LEVEL) std::cerr << std::string(n*2, ' ') << X
Chris Lattner1d670cc2001-09-07 16:37:43 +000022#else
23#define BCR_TRACE(n, X)
24#endif
25
Chris Lattner00950542001-06-06 20:29:01 +000026typedef unsigned char uchar;
27
28struct RawInst { // The raw fields out of the bytecode stream...
29 unsigned NumOperands;
30 unsigned Opcode;
31 const Type *Ty;
32 unsigned Arg1, Arg2;
33 union {
34 unsigned Arg3;
Chris Lattner697954c2002-01-20 22:54:45 +000035 std::vector<unsigned> *VarArgs; // Contains arg #3,4,5... if NumOperands > 3
Chris Lattner00950542001-06-06 20:29:01 +000036 };
37};
38
Chris Lattner1d670cc2001-09-07 16:37:43 +000039class BytecodeParser : public AbstractTypeUser {
Chris Lattner697954c2002-01-20 22:54:45 +000040 std::string Error; // Error message string goes here...
Chris Lattner74734132002-08-17 22:01:27 +000041 BytecodeParser(const BytecodeParser &); // DO NOT IMPLEMENT
42 void operator=(const BytecodeParser &); // DO NOT IMPLEMENT
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 {
Anand Shuklaeea60fc2002-06-25 20:44:04 +000054 std::cerr << "BytecodeParser instance!\n";
Chris Lattnerb3afb1f2002-04-04 19:24:11 +000055 }
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 Lattner74734132002-08-17 22:01:27 +000065 // GlobalRefs - This maintains a mapping between <Type, Slot #>'s and forward
66 // references to global values or constants. Such values may be referenced
67 // before they are defined, and if so, the temporary object that they
68 // represent is held here.
69 //
70 typedef std::map<std::pair<const Type *, unsigned>,
71 Value*> GlobalRefsType;
72 GlobalRefsType GlobalRefs;
Chris Lattner05950c32001-10-13 06:47:01 +000073
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
Chris Lattner74734132002-08-17 22:01:27 +000084 // When the ModuleGlobalInfo section is read, we load the type of each
85 // function and the 'ModuleValues' slot that it lands in. We then load a
86 // placeholder into its slot to reserve it. When the function is loaded, this
87 // placeholder is replaced.
Chris Lattner00950542001-06-06 20:29:01 +000088 //
Chris Lattner74734132002-08-17 22:01:27 +000089 std::vector<std::pair<const PointerType *, unsigned> > FunctionSignatureList;
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 Lattner74734132002-08-17 22:01:27 +0000116 // resolve all references to the placeholder (if any) for the given value
117 void ResolveReferencesToValue(Value *Val, unsigned Slot);
118
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000119
Chris Lattner1d670cc2001-09-07 16:37:43 +0000120 // refineAbstractType - The callback method is invoked when one of the
121 // elements of TypeValues becomes more concrete...
122 //
123 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
Chris Lattner00950542001-06-06 20:29:01 +0000124};
125
126template<class SuperType>
127class PlaceholderDef : public SuperType {
128 unsigned ID;
Chris Lattner74734132002-08-17 22:01:27 +0000129 PlaceholderDef(); // DO NOT IMPLEMENT
130 void operator=(const PlaceholderDef &); // DO NOT IMPLEMENT
Chris Lattner00950542001-06-06 20:29:01 +0000131public:
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 Lattner74734132002-08-17 22:01:27 +0000149struct FunctionPlaceHolderHelper : public Function {
150 FunctionPlaceHolderHelper(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
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000155struct ConstantPlaceHolderHelper : public Constant {
156 ConstantPlaceHolderHelper(const Type *Ty)
157 : Constant(Ty) {}
158 virtual bool isNullValue() const { return false; }
159};
160
Chris Lattner74734132002-08-17 22:01:27 +0000161typedef PlaceholderDef<InstPlaceHolderHelper> ValPHolder;
Chris Lattner00950542001-06-06 20:29:01 +0000162typedef PlaceholderDef<BBPlaceHolderHelper> BBPHolder;
Chris Lattner74734132002-08-17 22:01:27 +0000163typedef PlaceholderDef<FunctionPlaceHolderHelper> FunctionPHolder;
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000164typedef PlaceholderDef<ConstantPlaceHolderHelper> ConstPHolder;
165
Chris Lattner00950542001-06-06 20:29:01 +0000166
Chris Lattner74734132002-08-17 22:01:27 +0000167static inline unsigned getValueIDNumberFromPlaceHolder(Value *Val) {
168 if (isa<Constant>(Val))
169 return ((ConstPHolder*)Val)->getID();
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000170
171 // else discriminate by type
Chris Lattner74734132002-08-17 22:01:27 +0000172 switch (Val->getType()->getPrimitiveID()) {
173 case Type::LabelTyID: return ((BBPHolder*)Val)->getID();
174 case Type::FunctionTyID: return ((FunctionPHolder*)Val)->getID();
175 default: return ((ValPHolder*)Val)->getID();
Chris Lattner00950542001-06-06 20:29:01 +0000176 }
177}
178
179static inline bool readBlock(const uchar *&Buf, const uchar *EndBuf,
180 unsigned &Type, unsigned &Size) {
181#if DEBUG_OUTPUT
182 bool Result = read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size);
Anand Shuklaeea60fc2002-06-25 20:44:04 +0000183 std::cerr << "StartLoc = " << ((unsigned)Buf & 4095)
Chris Lattner00950542001-06-06 20:29:01 +0000184 << " Type = " << Type << " Size = " << Size << endl;
185 return Result;
186#else
187 return read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size);
188#endif
189}
190
Chris Lattner00950542001-06-06 20:29:01 +0000191#endif