blob: 9044c0f6c5867e8f73671e3fe71f894f1c33fe6e [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 Lattnerd256ed82003-09-04 23:47:07 +000018//#define TRACE_LEVEL 10
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 +000027struct RawInst { // The raw fields out of the bytecode stream...
28 unsigned NumOperands;
29 unsigned Opcode;
30 const Type *Ty;
31 unsigned Arg1, Arg2;
32 union {
33 unsigned Arg3;
Chris Lattner697954c2002-01-20 22:54:45 +000034 std::vector<unsigned> *VarArgs; // Contains arg #3,4,5... if NumOperands > 3
Chris Lattner00950542001-06-06 20:29:01 +000035 };
36};
37
Chris Lattner1d670cc2001-09-07 16:37:43 +000038class BytecodeParser : public AbstractTypeUser {
Chris Lattner697954c2002-01-20 22:54:45 +000039 std::string Error; // Error message string goes here...
Chris Lattner74734132002-08-17 22:01:27 +000040 BytecodeParser(const BytecodeParser &); // DO NOT IMPLEMENT
41 void operator=(const BytecodeParser &); // DO NOT IMPLEMENT
Chris Lattner00950542001-06-06 20:29:01 +000042public:
43 BytecodeParser() {
44 // Define this in case we don't see a ModuleGlobalInfo block.
45 FirstDerivedTyID = Type::FirstDerivedTyID;
46 }
Chris Lattner52e20b02003-03-19 20:54:26 +000047 ~BytecodeParser() {
Chris Lattnera2602f32003-05-22 18:26:48 +000048 freeState();
49 }
50 void freeState() {
Chris Lattner52e20b02003-03-19 20:54:26 +000051 freeTable(Values);
52 freeTable(LateResolveValues);
53 freeTable(ModuleValues);
54 }
Chris Lattner00950542001-06-06 20:29:01 +000055
Chris Lattner12e64652003-05-22 18:08:30 +000056 Module *ParseBytecode(const unsigned char *Buf, const unsigned char *EndBuf,
Chris Lattner75f20532003-04-22 18:02:52 +000057 const std::string &ModuleID);
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 Lattnerb3afb1f2002-04-04 19:24:11 +000061 void dump() const {
Anand Shuklaeea60fc2002-06-25 20:44:04 +000062 std::cerr << "BytecodeParser instance!\n";
Chris Lattnerb3afb1f2002-04-04 19:24:11 +000063 }
64
Chris Lattner00950542001-06-06 20:29:01 +000065private: // All of this data is transient across calls to ParseBytecode
Chris Lattner52e20b02003-03-19 20:54:26 +000066 struct ValueList : public User {
67 ValueList() : User(Type::TypeTy, Value::TypeVal) {
68 }
69 ~ValueList() {}
70
71 // vector compatibility methods
72 unsigned size() const { return getNumOperands(); }
73 void push_back(Value *V) { Operands.push_back(Use(V, this)); }
74 Value *back() const { return Operands.back(); }
75 void pop_back() { Operands.pop_back(); }
76 bool empty() const { return Operands.empty(); }
77
78 virtual void print(std::ostream& OS) const {
79 OS << "Bytecode Reader UseHandle!";
80 }
81 };
82
Chris Lattner05950c32001-10-13 06:47:01 +000083 Module *TheModule; // Current Module being read into...
84
Chris Lattner036b8aa2003-03-06 17:55:45 +000085 // Information about the module, extracted from the bytecode revision number.
86 unsigned char RevisionNum; // The rev # itself
87 unsigned char FirstDerivedTyID; // First variable index to use for type
88 bool HasImplicitZeroInitializer; // Is entry 0 of every slot implicity zeros?
Chris Lattnere3869c82003-04-16 21:16:05 +000089 bool hasInternalMarkerOnly; // Only types of linkage are intern/external
Chris Lattner036b8aa2003-03-06 17:55:45 +000090
Chris Lattner52e20b02003-03-19 20:54:26 +000091 typedef std::vector<ValueList*> ValueTable;
Chris Lattner00950542001-06-06 20:29:01 +000092 ValueTable Values, LateResolveValues;
Chris Lattner52e20b02003-03-19 20:54:26 +000093 ValueTable ModuleValues;
Chris Lattner1d670cc2001-09-07 16:37:43 +000094
Chris Lattner74734132002-08-17 22:01:27 +000095 // GlobalRefs - This maintains a mapping between <Type, Slot #>'s and forward
96 // references to global values or constants. Such values may be referenced
97 // before they are defined, and if so, the temporary object that they
98 // represent is held here.
99 //
Chris Lattner52e20b02003-03-19 20:54:26 +0000100 typedef std::map<std::pair<const Type *, unsigned>, Value*> GlobalRefsType;
Chris Lattner74734132002-08-17 22:01:27 +0000101 GlobalRefsType GlobalRefs;
Chris Lattner05950c32001-10-13 06:47:01 +0000102
Chris Lattner1d670cc2001-09-07 16:37:43 +0000103 // TypesLoaded - This vector mirrors the Values[TypeTyID] plane. It is used
104 // to deal with forward references to types.
105 //
Chris Lattner893f0252003-06-18 19:22:36 +0000106 typedef std::vector<PATypeHandle> TypeValuesListTy;
Chris Lattner1d670cc2001-09-07 16:37:43 +0000107 TypeValuesListTy ModuleTypeValues;
Chris Lattner6e5a0e42003-03-06 17:18:14 +0000108 TypeValuesListTy FunctionTypeValues;
Chris Lattner00950542001-06-06 20:29:01 +0000109
Chris Lattner52e20b02003-03-19 20:54:26 +0000110 // When the ModuleGlobalInfo section is read, we create a function object for
111 // each function in the module. When the function is loaded, this function is
112 // filled in.
Chris Lattner00950542001-06-06 20:29:01 +0000113 //
Chris Lattner52e20b02003-03-19 20:54:26 +0000114 std::vector<std::pair<Function*, unsigned> > FunctionSignatureList;
115
116 // Constant values are read in after global variables. Because of this, we
117 // must defer setting the initializers on global variables until after module
118 // level constants have been read. In the mean time, this list keeps track of
119 // what we must do.
120 //
121 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
Chris Lattner00950542001-06-06 20:29:01 +0000122
123private:
Chris Lattner52e20b02003-03-19 20:54:26 +0000124 void freeTable(ValueTable &Tab) {
125 while (!Tab.empty()) {
126 delete Tab.back();
127 Tab.pop_back();
128 }
129 }
130
Chris Lattner12e64652003-05-22 18:08:30 +0000131 bool ParseModule (const unsigned char * Buf, const unsigned char *End);
132 bool ParseVersionInfo (const unsigned char *&Buf, const unsigned char *End);
133 bool ParseModuleGlobalInfo(const unsigned char *&Buf, const unsigned char *E);
134 bool ParseSymbolTable (const unsigned char *&Buf, const unsigned char *End,
135 SymbolTable *);
136 bool ParseFunction (const unsigned char *&Buf, const unsigned char *End);
137 bool ParseBasicBlock (const unsigned char *&Buf, const unsigned char *End,
138 BasicBlock *&);
139 bool ParseInstruction (const unsigned char *&Buf, const unsigned char *End,
140 Instruction *&, BasicBlock *BB /*HACK*/);
141 bool ParseRawInst (const unsigned char *&Buf, const unsigned char *End,
142 RawInst &);
Chris Lattner00950542001-06-06 20:29:01 +0000143
Chris Lattner12e64652003-05-22 18:08:30 +0000144 bool ParseGlobalTypes(const unsigned char *&Buf, const unsigned char *EndBuf);
145 bool ParseConstantPool(const unsigned char *&Buf, const unsigned char *EndBuf,
Chris Lattner1d670cc2001-09-07 16:37:43 +0000146 ValueTable &Tab, TypeValuesListTy &TypeTab);
Chris Lattner12e64652003-05-22 18:08:30 +0000147 bool parseConstantValue(const unsigned char *&Buf, const unsigned char *End,
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000148 const Type *Ty, Constant *&V);
Chris Lattner12e64652003-05-22 18:08:30 +0000149 bool parseTypeConstants(const unsigned char *&Buf,
150 const unsigned char *EndBuf,
Chris Lattner1d670cc2001-09-07 16:37:43 +0000151 TypeValuesListTy &Tab, unsigned NumEntries);
Chris Lattner12e64652003-05-22 18:08:30 +0000152 const Type *parseTypeConstant(const unsigned char *&Buf,
153 const unsigned char *EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +0000154
155 Value *getValue(const Type *Ty, unsigned num, bool Create = true);
156 const Type *getType(unsigned ID);
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000157 Constant *getConstantValue(const Type *Ty, unsigned num);
Chris Lattner00950542001-06-06 20:29:01 +0000158
Chris Lattner52e20b02003-03-19 20:54:26 +0000159 int insertValue(Value *V, ValueTable &Table); // -1 = Failure
160 void setValueTo(ValueTable &D, unsigned Slot, Value *V);
Chris Lattner00950542001-06-06 20:29:01 +0000161 bool postResolveValues(ValueTable &ValTab);
162
163 bool getTypeSlot(const Type *Ty, unsigned &Slot);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000164
Chris Lattner74734132002-08-17 22:01:27 +0000165 // resolve all references to the placeholder (if any) for the given value
166 void ResolveReferencesToValue(Value *Val, unsigned Slot);
167
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000168
Chris Lattner1d670cc2001-09-07 16:37:43 +0000169 // refineAbstractType - The callback method is invoked when one of the
170 // elements of TypeValues becomes more concrete...
171 //
172 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
Chris Lattner00950542001-06-06 20:29:01 +0000173};
174
175template<class SuperType>
176class PlaceholderDef : public SuperType {
177 unsigned ID;
Chris Lattner74734132002-08-17 22:01:27 +0000178 PlaceholderDef(); // DO NOT IMPLEMENT
179 void operator=(const PlaceholderDef &); // DO NOT IMPLEMENT
Chris Lattner00950542001-06-06 20:29:01 +0000180public:
181 PlaceholderDef(const Type *Ty, unsigned id) : SuperType(Ty), ID(id) {}
182 unsigned getID() { return ID; }
183};
184
185struct InstPlaceHolderHelper : public Instruction {
186 InstPlaceHolderHelper(const Type *Ty) : Instruction(Ty, UserOp1, "") {}
Chris Lattnera41f50d2001-07-07 19:24:15 +0000187 virtual const char *getOpcodeName() const { return "placeholder"; }
Chris Lattner00950542001-06-06 20:29:01 +0000188
189 virtual Instruction *clone() const { abort(); return 0; }
Chris Lattner00950542001-06-06 20:29:01 +0000190};
191
192struct BBPlaceHolderHelper : public BasicBlock {
193 BBPlaceHolderHelper(const Type *Ty) : BasicBlock() {
Chris Lattner35e309a2002-04-08 21:59:36 +0000194 assert(Ty == Type::LabelTy);
Chris Lattner00950542001-06-06 20:29:01 +0000195 }
196};
197
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000198struct ConstantPlaceHolderHelper : public Constant {
199 ConstantPlaceHolderHelper(const Type *Ty)
200 : Constant(Ty) {}
201 virtual bool isNullValue() const { return false; }
202};
203
Chris Lattner74734132002-08-17 22:01:27 +0000204typedef PlaceholderDef<InstPlaceHolderHelper> ValPHolder;
Chris Lattner00950542001-06-06 20:29:01 +0000205typedef PlaceholderDef<BBPlaceHolderHelper> BBPHolder;
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000206typedef PlaceholderDef<ConstantPlaceHolderHelper> ConstPHolder;
207
Chris Lattner00950542001-06-06 20:29:01 +0000208
Chris Lattner74734132002-08-17 22:01:27 +0000209static inline unsigned getValueIDNumberFromPlaceHolder(Value *Val) {
210 if (isa<Constant>(Val))
211 return ((ConstPHolder*)Val)->getID();
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000212
213 // else discriminate by type
Chris Lattner74734132002-08-17 22:01:27 +0000214 switch (Val->getType()->getPrimitiveID()) {
215 case Type::LabelTyID: return ((BBPHolder*)Val)->getID();
Chris Lattner74734132002-08-17 22:01:27 +0000216 default: return ((ValPHolder*)Val)->getID();
Chris Lattner00950542001-06-06 20:29:01 +0000217 }
218}
219
Chris Lattner12e64652003-05-22 18:08:30 +0000220static inline bool readBlock(const unsigned char *&Buf,
221 const unsigned char *EndBuf,
Chris Lattner00950542001-06-06 20:29:01 +0000222 unsigned &Type, unsigned &Size) {
223#if DEBUG_OUTPUT
224 bool Result = read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size);
Anand Shuklaeea60fc2002-06-25 20:44:04 +0000225 std::cerr << "StartLoc = " << ((unsigned)Buf & 4095)
Chris Lattner00950542001-06-06 20:29:01 +0000226 << " Type = " << Type << " Size = " << Size << endl;
227 return Result;
228#else
229 return read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size);
230#endif
231}
232
Chris Lattner00950542001-06-06 20:29:01 +0000233#endif