blob: 09a21d71e4bd59f6a547d6cb1295cd369df8bf10 [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 Lattner036b8aa2003-03-06 17:55:45 +000060 // Information about the module, extracted from the bytecode revision number.
61 unsigned char RevisionNum; // The rev # itself
62 unsigned char FirstDerivedTyID; // First variable index to use for type
63 bool HasImplicitZeroInitializer; // Is entry 0 of every slot implicity zeros?
64 bool isBigEndian, hasLongPointers;// Information about the target compiled for
65
Chris Lattner697954c2002-01-20 22:54:45 +000066 typedef std::vector<Value *> ValueList;
67 typedef std::vector<ValueList> ValueTable;
Chris Lattner00950542001-06-06 20:29:01 +000068 ValueTable Values, LateResolveValues;
69 ValueTable ModuleValues, LateResolveModuleValues;
Chris Lattner1d670cc2001-09-07 16:37:43 +000070
Chris Lattner74734132002-08-17 22:01:27 +000071 // GlobalRefs - This maintains a mapping between <Type, Slot #>'s and forward
72 // references to global values or constants. Such values may be referenced
73 // before they are defined, and if so, the temporary object that they
74 // represent is held here.
75 //
76 typedef std::map<std::pair<const Type *, unsigned>,
77 Value*> GlobalRefsType;
78 GlobalRefsType GlobalRefs;
Chris Lattner05950c32001-10-13 06:47:01 +000079
Chris Lattner1d670cc2001-09-07 16:37:43 +000080 // TypesLoaded - This vector mirrors the Values[TypeTyID] plane. It is used
81 // to deal with forward references to types.
82 //
Chris Lattner697954c2002-01-20 22:54:45 +000083 typedef std::vector<PATypeHandle<Type> > TypeValuesListTy;
Chris Lattner1d670cc2001-09-07 16:37:43 +000084 TypeValuesListTy ModuleTypeValues;
Chris Lattner6e5a0e42003-03-06 17:18:14 +000085 TypeValuesListTy FunctionTypeValues;
Chris Lattner00950542001-06-06 20:29:01 +000086
Chris Lattner74734132002-08-17 22:01:27 +000087 // When the ModuleGlobalInfo section is read, we load the type of each
88 // function and the 'ModuleValues' slot that it lands in. We then load a
89 // placeholder into its slot to reserve it. When the function is loaded, this
90 // placeholder is replaced.
Chris Lattner00950542001-06-06 20:29:01 +000091 //
Chris Lattner74734132002-08-17 22:01:27 +000092 std::vector<std::pair<const PointerType *, unsigned> > FunctionSignatureList;
Chris Lattner00950542001-06-06 20:29:01 +000093
94private:
Chris Lattner2a7b6ba2003-03-06 17:15:19 +000095 bool ParseModule (const uchar * Buf, const uchar *End);
Chris Lattner036b8aa2003-03-06 17:55:45 +000096 bool ParseVersionInfo (const uchar *&Buf, const uchar *End);
Chris Lattner2a7b6ba2003-03-06 17:15:19 +000097 bool ParseModuleGlobalInfo(const uchar *&Buf, const uchar *End);
Chris Lattner1d670cc2001-09-07 16:37:43 +000098 bool ParseSymbolTable (const uchar *&Buf, const uchar *End, SymbolTable *);
Chris Lattner6e5a0e42003-03-06 17:18:14 +000099 bool ParseFunction (const uchar *&Buf, const uchar *End);
Chris Lattner00950542001-06-06 20:29:01 +0000100 bool ParseBasicBlock (const uchar *&Buf, const uchar *End, BasicBlock *&);
Chris Lattner352eef72002-08-21 22:55:27 +0000101 bool ParseInstruction (const uchar *&Buf, const uchar *End, Instruction *&,
102 BasicBlock *BB /*HACK*/);
Chris Lattner00950542001-06-06 20:29:01 +0000103 bool ParseRawInst (const uchar *&Buf, const uchar *End, RawInst &);
104
105 bool ParseConstantPool(const uchar *&Buf, const uchar *EndBuf,
Chris Lattner1d670cc2001-09-07 16:37:43 +0000106 ValueTable &Tab, TypeValuesListTy &TypeTab);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000107 bool parseConstantValue(const uchar *&Buf, const uchar *End,
108 const Type *Ty, Constant *&V);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000109 bool parseTypeConstants(const uchar *&Buf, const uchar *EndBuf,
110 TypeValuesListTy &Tab, unsigned NumEntries);
111 const Type *parseTypeConstant(const uchar *&Buf, const uchar *EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +0000112
113 Value *getValue(const Type *Ty, unsigned num, bool Create = true);
114 const Type *getType(unsigned ID);
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000115 Constant *getConstantValue(const Type *Ty, unsigned num);
Chris Lattner00950542001-06-06 20:29:01 +0000116
Chris Lattner697954c2002-01-20 22:54:45 +0000117 int insertValue(Value *D, std::vector<ValueList> &D); // -1 = Failure
Chris Lattner00950542001-06-06 20:29:01 +0000118 bool postResolveValues(ValueTable &ValTab);
119
120 bool getTypeSlot(const Type *Ty, unsigned &Slot);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000121
Chris Lattner74734132002-08-17 22:01:27 +0000122 // resolve all references to the placeholder (if any) for the given value
123 void ResolveReferencesToValue(Value *Val, unsigned Slot);
124
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000125
Chris Lattner1d670cc2001-09-07 16:37:43 +0000126 // refineAbstractType - The callback method is invoked when one of the
127 // elements of TypeValues becomes more concrete...
128 //
129 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
Chris Lattner00950542001-06-06 20:29:01 +0000130};
131
132template<class SuperType>
133class PlaceholderDef : public SuperType {
134 unsigned ID;
Chris Lattner74734132002-08-17 22:01:27 +0000135 PlaceholderDef(); // DO NOT IMPLEMENT
136 void operator=(const PlaceholderDef &); // DO NOT IMPLEMENT
Chris Lattner00950542001-06-06 20:29:01 +0000137public:
138 PlaceholderDef(const Type *Ty, unsigned id) : SuperType(Ty), ID(id) {}
139 unsigned getID() { return ID; }
140};
141
142struct InstPlaceHolderHelper : public Instruction {
143 InstPlaceHolderHelper(const Type *Ty) : Instruction(Ty, UserOp1, "") {}
Chris Lattnera41f50d2001-07-07 19:24:15 +0000144 virtual const char *getOpcodeName() const { return "placeholder"; }
Chris Lattner00950542001-06-06 20:29:01 +0000145
146 virtual Instruction *clone() const { abort(); return 0; }
Chris Lattner00950542001-06-06 20:29:01 +0000147};
148
149struct BBPlaceHolderHelper : public BasicBlock {
150 BBPlaceHolderHelper(const Type *Ty) : BasicBlock() {
Chris Lattner35e309a2002-04-08 21:59:36 +0000151 assert(Ty == Type::LabelTy);
Chris Lattner00950542001-06-06 20:29:01 +0000152 }
153};
154
Chris Lattner74734132002-08-17 22:01:27 +0000155struct FunctionPlaceHolderHelper : public Function {
156 FunctionPlaceHolderHelper(const Type *Ty)
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000157 : Function(cast<const FunctionType>(Ty), true) {
Chris Lattner00950542001-06-06 20:29:01 +0000158 }
159};
160
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000161struct ConstantPlaceHolderHelper : public Constant {
162 ConstantPlaceHolderHelper(const Type *Ty)
163 : Constant(Ty) {}
164 virtual bool isNullValue() const { return false; }
165};
166
Chris Lattner74734132002-08-17 22:01:27 +0000167typedef PlaceholderDef<InstPlaceHolderHelper> ValPHolder;
Chris Lattner00950542001-06-06 20:29:01 +0000168typedef PlaceholderDef<BBPlaceHolderHelper> BBPHolder;
Chris Lattner74734132002-08-17 22:01:27 +0000169typedef PlaceholderDef<FunctionPlaceHolderHelper> FunctionPHolder;
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000170typedef PlaceholderDef<ConstantPlaceHolderHelper> ConstPHolder;
171
Chris Lattner00950542001-06-06 20:29:01 +0000172
Chris Lattner74734132002-08-17 22:01:27 +0000173static inline unsigned getValueIDNumberFromPlaceHolder(Value *Val) {
174 if (isa<Constant>(Val))
175 return ((ConstPHolder*)Val)->getID();
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000176
177 // else discriminate by type
Chris Lattner74734132002-08-17 22:01:27 +0000178 switch (Val->getType()->getPrimitiveID()) {
179 case Type::LabelTyID: return ((BBPHolder*)Val)->getID();
180 case Type::FunctionTyID: return ((FunctionPHolder*)Val)->getID();
181 default: return ((ValPHolder*)Val)->getID();
Chris Lattner00950542001-06-06 20:29:01 +0000182 }
183}
184
185static inline bool readBlock(const uchar *&Buf, const uchar *EndBuf,
186 unsigned &Type, unsigned &Size) {
187#if DEBUG_OUTPUT
188 bool Result = read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size);
Anand Shuklaeea60fc2002-06-25 20:44:04 +0000189 std::cerr << "StartLoc = " << ((unsigned)Buf & 4095)
Chris Lattner00950542001-06-06 20:29:01 +0000190 << " Type = " << Type << " Size = " << Size << endl;
191 return Result;
192#else
193 return read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size);
194#endif
195}
196
Chris Lattner00950542001-06-06 20:29:01 +0000197#endif