blob: 47cde2adc0e809d90c3e7fcfea26eb0b3053c179 [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"
11#include "llvm/SymTabValue.h"
Chris Lattnerc9aa7df2002-03-29 03:51:11 +000012#include "llvm/Function.h"
Chris Lattner221d6882002-02-12 21:07:25 +000013#include "llvm/BasicBlock.h"
Chris Lattner00950542001-06-06 20:29:01 +000014#include "llvm/Instruction.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000015#include "llvm/DerivedTypes.h"
Chris Lattner00950542001-06-06 20:29:01 +000016#include <map>
17#include <utility>
Chris Lattner3ff43872001-09-28 22:56:31 +000018#include <list>
Chris Lattner00950542001-06-06 20:29:01 +000019
Chris Lattner1d670cc2001-09-07 16:37:43 +000020// Enable to trace to figure out what the heck is going on when parsing fails
Chris Lattnerf6086082001-10-24 05:14:35 +000021#define TRACE_LEVEL 0
Chris Lattner1d670cc2001-09-07 16:37:43 +000022
23#if TRACE_LEVEL // ByteCodeReading_TRACEer
24#include "llvm/Assembly/Writer.h"
Chris Lattner697954c2002-01-20 22:54:45 +000025#define BCR_TRACE(n, X) if (n < TRACE_LEVEL) cerr << std::string(n*2, ' ') << X
Chris Lattner1d670cc2001-09-07 16:37:43 +000026#else
27#define BCR_TRACE(n, X)
28#endif
29
Chris Lattner00950542001-06-06 20:29:01 +000030typedef unsigned char uchar;
31
32struct RawInst { // The raw fields out of the bytecode stream...
33 unsigned NumOperands;
34 unsigned Opcode;
35 const Type *Ty;
36 unsigned Arg1, Arg2;
37 union {
38 unsigned Arg3;
Chris Lattner697954c2002-01-20 22:54:45 +000039 std::vector<unsigned> *VarArgs; // Contains arg #3,4,5... if NumOperands > 3
Chris Lattner00950542001-06-06 20:29:01 +000040 };
41};
42
Chris Lattner1d670cc2001-09-07 16:37:43 +000043class BytecodeParser : public AbstractTypeUser {
Chris Lattner697954c2002-01-20 22:54:45 +000044 std::string Error; // Error message string goes here...
Chris Lattner00950542001-06-06 20:29:01 +000045public:
46 BytecodeParser() {
47 // Define this in case we don't see a ModuleGlobalInfo block.
48 FirstDerivedTyID = Type::FirstDerivedTyID;
49 }
50
51 Module *ParseBytecode(const uchar *Buf, const uchar *EndBuf);
Chris Lattnerd6b65252001-10-24 01:15:12 +000052
Chris Lattner697954c2002-01-20 22:54:45 +000053 std::string getError() const { return Error; }
Chris Lattnerd6b65252001-10-24 01:15:12 +000054
Chris Lattnerb3afb1f2002-04-04 19:24:11 +000055 void dump() const {
56 cerr << "BytecodeParser instance!\n";
57 }
58
Chris Lattner00950542001-06-06 20:29:01 +000059private: // All of this data is transient across calls to ParseBytecode
Chris Lattner05950c32001-10-13 06:47:01 +000060 Module *TheModule; // Current Module being read into...
61
Chris Lattner697954c2002-01-20 22:54:45 +000062 typedef std::vector<Value *> ValueList;
63 typedef std::vector<ValueList> ValueTable;
Chris Lattner00950542001-06-06 20:29:01 +000064 ValueTable Values, LateResolveValues;
65 ValueTable ModuleValues, LateResolveModuleValues;
Chris Lattner1d670cc2001-09-07 16:37:43 +000066
Chris Lattner05950c32001-10-13 06:47:01 +000067 // GlobalRefs - This maintains a mapping between <Type, Slot #>'s and forward
68 // references to global values. Global values may be referenced before they
69 // are defined, and if so, the temporary object that they represent is held
70 // here.
71 //
Chris Lattner697954c2002-01-20 22:54:45 +000072 typedef std::map<std::pair<const PointerType *, unsigned>,
73 GlobalVariable*> GlobalRefsType;
Chris Lattner05950c32001-10-13 06:47:01 +000074 GlobalRefsType GlobalRefs;
75
Chris Lattner1d670cc2001-09-07 16:37:43 +000076 // TypesLoaded - This vector mirrors the Values[TypeTyID] plane. It is used
77 // to deal with forward references to types.
78 //
Chris Lattner697954c2002-01-20 22:54:45 +000079 typedef std::vector<PATypeHandle<Type> > TypeValuesListTy;
Chris Lattner1d670cc2001-09-07 16:37:43 +000080 TypeValuesListTy ModuleTypeValues;
81 TypeValuesListTy MethodTypeValues;
Chris Lattner00950542001-06-06 20:29:01 +000082
83 // Information read from the ModuleGlobalInfo section of the file...
84 unsigned FirstDerivedTyID;
85
86 // When the ModuleGlobalInfo section is read, we load the type of each method
87 // and the 'ModuleValues' slot that it lands in. We then load a placeholder
88 // into its slot to reserve it. When the method is loaded, this placeholder
89 // is replaced.
90 //
Chris Lattner697954c2002-01-20 22:54:45 +000091 std::list<std::pair<const PointerType *, unsigned> > MethodSignatureList;
Chris Lattner00950542001-06-06 20:29:01 +000092
93private:
Chris Lattner697954c2002-01-20 22:54:45 +000094 bool ParseModule (const uchar * Buf, const uchar *End, Module *&);
95 bool ParseModuleGlobalInfo(const uchar *&Buf, const uchar *End, Module *);
Chris Lattner1d670cc2001-09-07 16:37:43 +000096 bool ParseSymbolTable (const uchar *&Buf, const uchar *End, SymbolTable *);
97 bool ParseMethod (const uchar *&Buf, const uchar *End, Module *);
Chris Lattner00950542001-06-06 20:29:01 +000098 bool ParseBasicBlock (const uchar *&Buf, const uchar *End, BasicBlock *&);
99 bool ParseInstruction (const uchar *&Buf, const uchar *End, Instruction *&);
100 bool ParseRawInst (const uchar *&Buf, const uchar *End, RawInst &);
101
102 bool ParseConstantPool(const uchar *&Buf, const uchar *EndBuf,
Chris Lattner1d670cc2001-09-07 16:37:43 +0000103 ValueTable &Tab, TypeValuesListTy &TypeTab);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000104 bool parseConstantValue(const uchar *&Buf, const uchar *End,
105 const Type *Ty, Constant *&V);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000106 bool parseTypeConstants(const uchar *&Buf, const uchar *EndBuf,
107 TypeValuesListTy &Tab, unsigned NumEntries);
108 const Type *parseTypeConstant(const uchar *&Buf, const uchar *EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +0000109
110 Value *getValue(const Type *Ty, unsigned num, bool Create = true);
111 const Type *getType(unsigned ID);
112
Chris Lattner697954c2002-01-20 22:54:45 +0000113 int insertValue(Value *D, std::vector<ValueList> &D); // -1 = Failure
Chris Lattner00950542001-06-06 20:29:01 +0000114 bool postResolveValues(ValueTable &ValTab);
115
116 bool getTypeSlot(const Type *Ty, unsigned &Slot);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000117
Chris Lattner05950c32001-10-13 06:47:01 +0000118 // DeclareNewGlobalValue - Patch up forward references to global values in the
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000119 // form of ConstantPointerRefs.
Chris Lattner05950c32001-10-13 06:47:01 +0000120 //
121 void DeclareNewGlobalValue(GlobalValue *GV, unsigned Slot);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000122
123 // refineAbstractType - The callback method is invoked when one of the
124 // elements of TypeValues becomes more concrete...
125 //
126 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
Chris Lattner00950542001-06-06 20:29:01 +0000127};
128
129template<class SuperType>
130class PlaceholderDef : public SuperType {
131 unsigned ID;
132public:
133 PlaceholderDef(const Type *Ty, unsigned id) : SuperType(Ty), ID(id) {}
134 unsigned getID() { return ID; }
135};
136
137struct InstPlaceHolderHelper : public Instruction {
138 InstPlaceHolderHelper(const Type *Ty) : Instruction(Ty, UserOp1, "") {}
Chris Lattnera41f50d2001-07-07 19:24:15 +0000139 virtual const char *getOpcodeName() const { return "placeholder"; }
Chris Lattner00950542001-06-06 20:29:01 +0000140
141 virtual Instruction *clone() const { abort(); return 0; }
Chris Lattner00950542001-06-06 20:29:01 +0000142};
143
144struct BBPlaceHolderHelper : public BasicBlock {
145 BBPlaceHolderHelper(const Type *Ty) : BasicBlock() {
146 assert(Ty->isLabelType());
147 }
148};
149
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000150struct MethPlaceHolderHelper : public Function {
Chris Lattner00950542001-06-06 20:29:01 +0000151 MethPlaceHolderHelper(const Type *Ty)
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000152 : Function(cast<const FunctionType>(Ty), true) {
Chris Lattner00950542001-06-06 20:29:01 +0000153 }
154};
155
156typedef PlaceholderDef<InstPlaceHolderHelper> DefPHolder;
157typedef PlaceholderDef<BBPlaceHolderHelper> BBPHolder;
158typedef PlaceholderDef<MethPlaceHolderHelper> MethPHolder;
159
160static inline unsigned getValueIDNumberFromPlaceHolder(Value *Def) {
161 switch (Def->getType()->getPrimitiveID()) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000162 case Type::LabelTyID: return ((BBPHolder*)Def)->getID();
163 case Type::FunctionTyID: return ((MethPHolder*)Def)->getID();
164 default: return ((DefPHolder*)Def)->getID();
Chris Lattner00950542001-06-06 20:29:01 +0000165 }
166}
167
168static inline bool readBlock(const uchar *&Buf, const uchar *EndBuf,
169 unsigned &Type, unsigned &Size) {
170#if DEBUG_OUTPUT
171 bool Result = read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size);
172 cerr << "StartLoc = " << ((unsigned)Buf & 4095)
173 << " Type = " << Type << " Size = " << Size << endl;
174 return Result;
175#else
176 return read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size);
177#endif
178}
179
Chris Lattner3d3f2892001-07-28 17:50:18 +0000180
181// failure Template - This template function is used as a place to put
182// breakpoints in to debug failures of the bytecode parser.
183//
184template <typename X>
185static X failure(X Value) {
186 return Value;
187}
188
Chris Lattner00950542001-06-06 20:29:01 +0000189#endif