blob: dd2a08fcdbde69cee7a73c6b0df78e2e71faa3a4 [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
Chris Lattner52e20b02003-03-19 20:54:26 +000021#define BCR_TRACE(n, X) \
22 if (n < TRACE_LEVEL) std::cerr << std::string(n*2, ' ') << X
Chris Lattner1d670cc2001-09-07 16:37:43 +000023#else
24#define BCR_TRACE(n, X)
25#endif
26
Chris Lattner00950542001-06-06 20:29:01 +000027typedef unsigned char uchar;
28
29struct RawInst { // The raw fields out of the bytecode stream...
30 unsigned NumOperands;
31 unsigned Opcode;
32 const Type *Ty;
33 unsigned Arg1, Arg2;
34 union {
35 unsigned Arg3;
Chris Lattner697954c2002-01-20 22:54:45 +000036 std::vector<unsigned> *VarArgs; // Contains arg #3,4,5... if NumOperands > 3
Chris Lattner00950542001-06-06 20:29:01 +000037 };
38};
39
Chris Lattner1d670cc2001-09-07 16:37:43 +000040class BytecodeParser : public AbstractTypeUser {
Chris Lattner697954c2002-01-20 22:54:45 +000041 std::string Error; // Error message string goes here...
Chris Lattner74734132002-08-17 22:01:27 +000042 BytecodeParser(const BytecodeParser &); // DO NOT IMPLEMENT
43 void operator=(const BytecodeParser &); // DO NOT IMPLEMENT
Chris Lattner00950542001-06-06 20:29:01 +000044public:
45 BytecodeParser() {
46 // Define this in case we don't see a ModuleGlobalInfo block.
47 FirstDerivedTyID = Type::FirstDerivedTyID;
48 }
Chris Lattner52e20b02003-03-19 20:54:26 +000049 ~BytecodeParser() {
50 freeTable(Values);
51 freeTable(LateResolveValues);
52 freeTable(ModuleValues);
53 }
Chris Lattner00950542001-06-06 20:29:01 +000054
55 Module *ParseBytecode(const uchar *Buf, const uchar *EndBuf);
Chris Lattnerd6b65252001-10-24 01:15:12 +000056
Chris Lattner697954c2002-01-20 22:54:45 +000057 std::string getError() const { return Error; }
Chris Lattnerd6b65252001-10-24 01:15:12 +000058
Chris Lattnerb3afb1f2002-04-04 19:24:11 +000059 void dump() const {
Anand Shuklaeea60fc2002-06-25 20:44:04 +000060 std::cerr << "BytecodeParser instance!\n";
Chris Lattnerb3afb1f2002-04-04 19:24:11 +000061 }
62
Chris Lattner00950542001-06-06 20:29:01 +000063private: // All of this data is transient across calls to ParseBytecode
Chris Lattner52e20b02003-03-19 20:54:26 +000064 struct ValueList : public User {
65 ValueList() : User(Type::TypeTy, Value::TypeVal) {
66 }
67 ~ValueList() {}
68
69 // vector compatibility methods
70 unsigned size() const { return getNumOperands(); }
71 void push_back(Value *V) { Operands.push_back(Use(V, this)); }
72 Value *back() const { return Operands.back(); }
73 void pop_back() { Operands.pop_back(); }
74 bool empty() const { return Operands.empty(); }
75
76 virtual void print(std::ostream& OS) const {
77 OS << "Bytecode Reader UseHandle!";
78 }
79 };
80
Chris Lattner05950c32001-10-13 06:47:01 +000081 Module *TheModule; // Current Module being read into...
82
Chris Lattner036b8aa2003-03-06 17:55:45 +000083 // Information about the module, extracted from the bytecode revision number.
84 unsigned char RevisionNum; // The rev # itself
85 unsigned char FirstDerivedTyID; // First variable index to use for type
86 bool HasImplicitZeroInitializer; // Is entry 0 of every slot implicity zeros?
87 bool isBigEndian, hasLongPointers;// Information about the target compiled for
Chris Lattnere3869c82003-04-16 21:16:05 +000088 bool hasInternalMarkerOnly; // Only types of linkage are intern/external
Chris Lattner036b8aa2003-03-06 17:55:45 +000089
Chris Lattner52e20b02003-03-19 20:54:26 +000090 typedef std::vector<ValueList*> ValueTable;
Chris Lattner00950542001-06-06 20:29:01 +000091 ValueTable Values, LateResolveValues;
Chris Lattner52e20b02003-03-19 20:54:26 +000092 ValueTable ModuleValues;
Chris Lattner1d670cc2001-09-07 16:37:43 +000093
Chris Lattner74734132002-08-17 22:01:27 +000094 // GlobalRefs - This maintains a mapping between <Type, Slot #>'s and forward
95 // references to global values or constants. Such values may be referenced
96 // before they are defined, and if so, the temporary object that they
97 // represent is held here.
98 //
Chris Lattner52e20b02003-03-19 20:54:26 +000099 typedef std::map<std::pair<const Type *, unsigned>, Value*> GlobalRefsType;
Chris Lattner74734132002-08-17 22:01:27 +0000100 GlobalRefsType GlobalRefs;
Chris Lattner05950c32001-10-13 06:47:01 +0000101
Chris Lattner1d670cc2001-09-07 16:37:43 +0000102 // TypesLoaded - This vector mirrors the Values[TypeTyID] plane. It is used
103 // to deal with forward references to types.
104 //
Chris Lattner697954c2002-01-20 22:54:45 +0000105 typedef std::vector<PATypeHandle<Type> > TypeValuesListTy;
Chris Lattner1d670cc2001-09-07 16:37:43 +0000106 TypeValuesListTy ModuleTypeValues;
Chris Lattner6e5a0e42003-03-06 17:18:14 +0000107 TypeValuesListTy FunctionTypeValues;
Chris Lattner00950542001-06-06 20:29:01 +0000108
Chris Lattner52e20b02003-03-19 20:54:26 +0000109 // When the ModuleGlobalInfo section is read, we create a function object for
110 // each function in the module. When the function is loaded, this function is
111 // filled in.
Chris Lattner00950542001-06-06 20:29:01 +0000112 //
Chris Lattner52e20b02003-03-19 20:54:26 +0000113 std::vector<std::pair<Function*, unsigned> > FunctionSignatureList;
114
115 // Constant values are read in after global variables. Because of this, we
116 // must defer setting the initializers on global variables until after module
117 // level constants have been read. In the mean time, this list keeps track of
118 // what we must do.
119 //
120 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
Chris Lattner00950542001-06-06 20:29:01 +0000121
122private:
Chris Lattner52e20b02003-03-19 20:54:26 +0000123 void freeTable(ValueTable &Tab) {
124 while (!Tab.empty()) {
125 delete Tab.back();
126 Tab.pop_back();
127 }
128 }
129
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000130 bool ParseModule (const uchar * Buf, const uchar *End);
Chris Lattner036b8aa2003-03-06 17:55:45 +0000131 bool ParseVersionInfo (const uchar *&Buf, const uchar *End);
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000132 bool ParseModuleGlobalInfo(const uchar *&Buf, const uchar *End);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000133 bool ParseSymbolTable (const uchar *&Buf, const uchar *End, SymbolTable *);
Chris Lattner6e5a0e42003-03-06 17:18:14 +0000134 bool ParseFunction (const uchar *&Buf, const uchar *End);
Chris Lattner00950542001-06-06 20:29:01 +0000135 bool ParseBasicBlock (const uchar *&Buf, const uchar *End, BasicBlock *&);
Chris Lattner352eef72002-08-21 22:55:27 +0000136 bool ParseInstruction (const uchar *&Buf, const uchar *End, Instruction *&,
137 BasicBlock *BB /*HACK*/);
Chris Lattner00950542001-06-06 20:29:01 +0000138 bool ParseRawInst (const uchar *&Buf, const uchar *End, RawInst &);
139
Chris Lattner52e20b02003-03-19 20:54:26 +0000140 bool ParseGlobalTypes(const uchar *&Buf, const uchar *EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +0000141 bool ParseConstantPool(const uchar *&Buf, const uchar *EndBuf,
Chris Lattner1d670cc2001-09-07 16:37:43 +0000142 ValueTable &Tab, TypeValuesListTy &TypeTab);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000143 bool parseConstantValue(const uchar *&Buf, const uchar *End,
144 const Type *Ty, Constant *&V);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000145 bool parseTypeConstants(const uchar *&Buf, const uchar *EndBuf,
146 TypeValuesListTy &Tab, unsigned NumEntries);
147 const Type *parseTypeConstant(const uchar *&Buf, const uchar *EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +0000148
149 Value *getValue(const Type *Ty, unsigned num, bool Create = true);
150 const Type *getType(unsigned ID);
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000151 Constant *getConstantValue(const Type *Ty, unsigned num);
Chris Lattner00950542001-06-06 20:29:01 +0000152
Chris Lattner52e20b02003-03-19 20:54:26 +0000153 int insertValue(Value *V, ValueTable &Table); // -1 = Failure
154 void setValueTo(ValueTable &D, unsigned Slot, Value *V);
Chris Lattner00950542001-06-06 20:29:01 +0000155 bool postResolveValues(ValueTable &ValTab);
156
157 bool getTypeSlot(const Type *Ty, unsigned &Slot);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000158
Chris Lattner74734132002-08-17 22:01:27 +0000159 // resolve all references to the placeholder (if any) for the given value
160 void ResolveReferencesToValue(Value *Val, unsigned Slot);
161
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000162
Chris Lattner1d670cc2001-09-07 16:37:43 +0000163 // refineAbstractType - The callback method is invoked when one of the
164 // elements of TypeValues becomes more concrete...
165 //
166 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
Chris Lattner00950542001-06-06 20:29:01 +0000167};
168
169template<class SuperType>
170class PlaceholderDef : public SuperType {
171 unsigned ID;
Chris Lattner74734132002-08-17 22:01:27 +0000172 PlaceholderDef(); // DO NOT IMPLEMENT
173 void operator=(const PlaceholderDef &); // DO NOT IMPLEMENT
Chris Lattner00950542001-06-06 20:29:01 +0000174public:
175 PlaceholderDef(const Type *Ty, unsigned id) : SuperType(Ty), ID(id) {}
176 unsigned getID() { return ID; }
177};
178
179struct InstPlaceHolderHelper : public Instruction {
180 InstPlaceHolderHelper(const Type *Ty) : Instruction(Ty, UserOp1, "") {}
Chris Lattnera41f50d2001-07-07 19:24:15 +0000181 virtual const char *getOpcodeName() const { return "placeholder"; }
Chris Lattner00950542001-06-06 20:29:01 +0000182
183 virtual Instruction *clone() const { abort(); return 0; }
Chris Lattner00950542001-06-06 20:29:01 +0000184};
185
186struct BBPlaceHolderHelper : public BasicBlock {
187 BBPlaceHolderHelper(const Type *Ty) : BasicBlock() {
Chris Lattner35e309a2002-04-08 21:59:36 +0000188 assert(Ty == Type::LabelTy);
Chris Lattner00950542001-06-06 20:29:01 +0000189 }
190};
191
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000192struct ConstantPlaceHolderHelper : public Constant {
193 ConstantPlaceHolderHelper(const Type *Ty)
194 : Constant(Ty) {}
195 virtual bool isNullValue() const { return false; }
196};
197
Chris Lattner74734132002-08-17 22:01:27 +0000198typedef PlaceholderDef<InstPlaceHolderHelper> ValPHolder;
Chris Lattner00950542001-06-06 20:29:01 +0000199typedef PlaceholderDef<BBPlaceHolderHelper> BBPHolder;
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000200typedef PlaceholderDef<ConstantPlaceHolderHelper> ConstPHolder;
201
Chris Lattner00950542001-06-06 20:29:01 +0000202
Chris Lattner74734132002-08-17 22:01:27 +0000203static inline unsigned getValueIDNumberFromPlaceHolder(Value *Val) {
204 if (isa<Constant>(Val))
205 return ((ConstPHolder*)Val)->getID();
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000206
207 // else discriminate by type
Chris Lattner74734132002-08-17 22:01:27 +0000208 switch (Val->getType()->getPrimitiveID()) {
209 case Type::LabelTyID: return ((BBPHolder*)Val)->getID();
Chris Lattner74734132002-08-17 22:01:27 +0000210 default: return ((ValPHolder*)Val)->getID();
Chris Lattner00950542001-06-06 20:29:01 +0000211 }
212}
213
214static inline bool readBlock(const uchar *&Buf, const uchar *EndBuf,
215 unsigned &Type, unsigned &Size) {
216#if DEBUG_OUTPUT
217 bool Result = read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size);
Anand Shuklaeea60fc2002-06-25 20:44:04 +0000218 std::cerr << "StartLoc = " << ((unsigned)Buf & 4095)
Chris Lattner00950542001-06-06 20:29:01 +0000219 << " Type = " << Type << " Size = " << Size << endl;
220 return Result;
221#else
222 return read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size);
223#endif
224}
225
Chris Lattner00950542001-06-06 20:29:01 +0000226#endif