blob: c022519e58e5397179df04921bbf2ac8069acdf8 [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
Misha Brukman12c29d12003-09-22 23:38:23 +000010#include "llvm/Constant.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"
Misha Brukman12c29d12003-09-22 23:38:23 +000013#include "llvm/ModuleProvider.h"
14#include "llvm/Bytecode/Primitives.h"
Chris Lattner00950542001-06-06 20:29:01 +000015#include <utility>
Chris Lattner74734132002-08-17 22:01:27 +000016#include <map>
Misha Brukman12c29d12003-09-22 23:38:23 +000017#include <memory>
18class Module;
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 Lattnerd256ed82003-09-04 23:47:07 +000021//#define TRACE_LEVEL 10
Chris Lattner1d670cc2001-09-07 16:37:43 +000022
Misha Brukman12c29d12003-09-22 23:38:23 +000023#if TRACE_LEVEL // ByteCodeReading_TRACEr
Chris Lattner52e20b02003-03-19 20:54:26 +000024#define BCR_TRACE(n, X) \
25 if (n < TRACE_LEVEL) std::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 +000030struct RawInst { // The raw fields out of the bytecode stream...
31 unsigned NumOperands;
32 unsigned Opcode;
33 const Type *Ty;
34 unsigned Arg1, Arg2;
35 union {
36 unsigned Arg3;
Chris Lattner697954c2002-01-20 22:54:45 +000037 std::vector<unsigned> *VarArgs; // Contains arg #3,4,5... if NumOperands > 3
Chris Lattner00950542001-06-06 20:29:01 +000038 };
39};
40
Misha Brukman12c29d12003-09-22 23:38:23 +000041struct LazyFunctionInfo {
42 const unsigned char *Buf, *EndBuf;
43 unsigned FunctionSlot;
44};
45
Chris Lattner00413e32003-10-04 20:14:59 +000046class BytecodeParser : public ModuleProvider {
Chris Lattner74734132002-08-17 22:01:27 +000047 BytecodeParser(const BytecodeParser &); // DO NOT IMPLEMENT
48 void operator=(const BytecodeParser &); // DO NOT IMPLEMENT
Chris Lattner00950542001-06-06 20:29:01 +000049public:
Misha Brukman96f78772003-09-22 23:58:08 +000050 BytecodeParser() {
Chris Lattner00950542001-06-06 20:29:01 +000051 // Define this in case we don't see a ModuleGlobalInfo block.
52 FirstDerivedTyID = Type::FirstDerivedTyID;
53 }
Misha Brukman12c29d12003-09-22 23:38:23 +000054
Chris Lattner52e20b02003-03-19 20:54:26 +000055 ~BytecodeParser() {
Chris Lattnera2602f32003-05-22 18:26:48 +000056 freeState();
57 }
58 void freeState() {
Chris Lattner52e20b02003-03-19 20:54:26 +000059 freeTable(Values);
60 freeTable(LateResolveValues);
61 freeTable(ModuleValues);
62 }
Chris Lattner00950542001-06-06 20:29:01 +000063
Misha Brukman12c29d12003-09-22 23:38:23 +000064 Module* releaseModule() {
65 // Since we're losing control of this Module, we must hand it back complete
66 materializeModule();
67 freeState();
68 Module *tempM = TheModule;
69 TheModule = 0;
70 return tempM;
71 }
Chris Lattnerd6b65252001-10-24 01:15:12 +000072
Misha Brukman12c29d12003-09-22 23:38:23 +000073 void ParseBytecode(const unsigned char *Buf, unsigned Length,
74 const std::string &ModuleID);
Chris Lattnerd6b65252001-10-24 01:15:12 +000075
Chris Lattnerb3afb1f2002-04-04 19:24:11 +000076 void dump() const {
Anand Shuklaeea60fc2002-06-25 20:44:04 +000077 std::cerr << "BytecodeParser instance!\n";
Chris Lattnerb3afb1f2002-04-04 19:24:11 +000078 }
79
Chris Lattner00950542001-06-06 20:29:01 +000080private: // All of this data is transient across calls to ParseBytecode
Chris Lattner52e20b02003-03-19 20:54:26 +000081 struct ValueList : public User {
82 ValueList() : User(Type::TypeTy, Value::TypeVal) {
83 }
84 ~ValueList() {}
85
86 // vector compatibility methods
87 unsigned size() const { return getNumOperands(); }
88 void push_back(Value *V) { Operands.push_back(Use(V, this)); }
89 Value *back() const { return Operands.back(); }
90 void pop_back() { Operands.pop_back(); }
91 bool empty() const { return Operands.empty(); }
92
93 virtual void print(std::ostream& OS) const {
94 OS << "Bytecode Reader UseHandle!";
95 }
96 };
97
Chris Lattner036b8aa2003-03-06 17:55:45 +000098 // Information about the module, extracted from the bytecode revision number.
99 unsigned char RevisionNum; // The rev # itself
100 unsigned char FirstDerivedTyID; // First variable index to use for type
101 bool HasImplicitZeroInitializer; // Is entry 0 of every slot implicity zeros?
Chris Lattnere3869c82003-04-16 21:16:05 +0000102 bool hasInternalMarkerOnly; // Only types of linkage are intern/external
Chris Lattner036b8aa2003-03-06 17:55:45 +0000103
Chris Lattner52e20b02003-03-19 20:54:26 +0000104 typedef std::vector<ValueList*> ValueTable;
Chris Lattner00950542001-06-06 20:29:01 +0000105 ValueTable Values, LateResolveValues;
Chris Lattner52e20b02003-03-19 20:54:26 +0000106 ValueTable ModuleValues;
Chris Lattner1d670cc2001-09-07 16:37:43 +0000107
Chris Lattner74734132002-08-17 22:01:27 +0000108 // GlobalRefs - This maintains a mapping between <Type, Slot #>'s and forward
109 // references to global values or constants. Such values may be referenced
110 // before they are defined, and if so, the temporary object that they
111 // represent is held here.
112 //
Chris Lattner52e20b02003-03-19 20:54:26 +0000113 typedef std::map<std::pair<const Type *, unsigned>, Value*> GlobalRefsType;
Chris Lattner74734132002-08-17 22:01:27 +0000114 GlobalRefsType GlobalRefs;
Chris Lattner05950c32001-10-13 06:47:01 +0000115
Chris Lattner1d670cc2001-09-07 16:37:43 +0000116 // TypesLoaded - This vector mirrors the Values[TypeTyID] plane. It is used
117 // to deal with forward references to types.
118 //
Chris Lattnerc7b6f032003-10-02 20:26:18 +0000119 typedef std::vector<PATypeHolder> TypeValuesListTy;
Chris Lattner1d670cc2001-09-07 16:37:43 +0000120 TypeValuesListTy ModuleTypeValues;
Chris Lattner6e5a0e42003-03-06 17:18:14 +0000121 TypeValuesListTy FunctionTypeValues;
Chris Lattner00950542001-06-06 20:29:01 +0000122
Chris Lattner52e20b02003-03-19 20:54:26 +0000123 // When the ModuleGlobalInfo section is read, we create a function object for
124 // each function in the module. When the function is loaded, this function is
125 // filled in.
Chris Lattner00950542001-06-06 20:29:01 +0000126 //
Chris Lattner52e20b02003-03-19 20:54:26 +0000127 std::vector<std::pair<Function*, unsigned> > FunctionSignatureList;
128
129 // Constant values are read in after global variables. Because of this, we
130 // must defer setting the initializers on global variables until after module
131 // level constants have been read. In the mean time, this list keeps track of
132 // what we must do.
133 //
134 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
Chris Lattner00950542001-06-06 20:29:01 +0000135
Misha Brukman12c29d12003-09-22 23:38:23 +0000136 // For lazy reading-in of functions, we need to save away several pieces of
137 // information about each function: its begin and end pointer in the buffer
138 // and its FunctionSlot.
139 //
140 std::map<Function*, LazyFunctionInfo*> LazyFunctionLoadMap;
141
Chris Lattner00950542001-06-06 20:29:01 +0000142private:
Chris Lattner52e20b02003-03-19 20:54:26 +0000143 void freeTable(ValueTable &Tab) {
144 while (!Tab.empty()) {
145 delete Tab.back();
146 Tab.pop_back();
147 }
148 }
149
Misha Brukman12c29d12003-09-22 23:38:23 +0000150public:
151 void ParseModule(const unsigned char * Buf, const unsigned char *End);
152 void materializeFunction(Function *F);
153
154private:
155 void ParseVersionInfo (const unsigned char *&Buf, const unsigned char *End);
156 void ParseModuleGlobalInfo(const unsigned char *&Buf, const unsigned char *E);
157 void ParseSymbolTable(const unsigned char *&Buf, const unsigned char *End,
158 SymbolTable *);
159 void ParseFunction(const unsigned char *&Buf, const unsigned char *End);
160 void ParseGlobalTypes(const unsigned char *&Buf, const unsigned char *EndBuf);
161
162 std::auto_ptr<BasicBlock>
163 ParseBasicBlock(const unsigned char *&Buf, const unsigned char *End);
164
Chris Lattner12e64652003-05-22 18:08:30 +0000165 bool ParseInstruction (const unsigned char *&Buf, const unsigned char *End,
Chris Lattner09bd0252003-09-08 18:04:16 +0000166 Instruction *&);
Misha Brukmand554ebf2003-09-23 16:17:50 +0000167 std::auto_ptr<RawInst> ParseRawInst(const unsigned char *&Buf,
168 const unsigned char *End);
Chris Lattner00950542001-06-06 20:29:01 +0000169
Misha Brukman12c29d12003-09-22 23:38:23 +0000170 void ParseConstantPool(const unsigned char *&Buf, const unsigned char *EndBuf,
171 ValueTable &Tab, TypeValuesListTy &TypeTab);
Chris Lattner9e460f22003-10-04 20:00:03 +0000172 Constant *parseConstantValue(const unsigned char *&Buf,
173 const unsigned char *End,
174 const Type *Ty);
Misha Brukman12c29d12003-09-22 23:38:23 +0000175 void parseTypeConstants(const unsigned char *&Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000176 const unsigned char *EndBuf,
Misha Brukman12c29d12003-09-22 23:38:23 +0000177 TypeValuesListTy &Tab, unsigned NumEntries);
Chris Lattner12e64652003-05-22 18:08:30 +0000178 const Type *parseTypeConstant(const unsigned char *&Buf,
179 const unsigned char *EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +0000180
181 Value *getValue(const Type *Ty, unsigned num, bool Create = true);
Chris Lattner36392bc2003-10-08 21:18:57 +0000182 Value *getValue(unsigned TypeID, unsigned num, bool Create = true);
Chris Lattner00950542001-06-06 20:29:01 +0000183 const Type *getType(unsigned ID);
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000184 Constant *getConstantValue(const Type *Ty, unsigned num);
Chris Lattner00950542001-06-06 20:29:01 +0000185
Chris Lattner52e20b02003-03-19 20:54:26 +0000186 int insertValue(Value *V, ValueTable &Table); // -1 = Failure
187 void setValueTo(ValueTable &D, unsigned Slot, Value *V);
Misha Brukman12c29d12003-09-22 23:38:23 +0000188 void postResolveValues(ValueTable &ValTab);
Chris Lattner00950542001-06-06 20:29:01 +0000189
Chris Lattner9e460f22003-10-04 20:00:03 +0000190 unsigned getTypeSlot(const Type *Ty);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000191
Chris Lattner74734132002-08-17 22:01:27 +0000192 // resolve all references to the placeholder (if any) for the given value
193 void ResolveReferencesToValue(Value *Val, unsigned Slot);
Chris Lattner00950542001-06-06 20:29:01 +0000194};
195
196template<class SuperType>
197class PlaceholderDef : public SuperType {
198 unsigned ID;
Chris Lattner74734132002-08-17 22:01:27 +0000199 PlaceholderDef(); // DO NOT IMPLEMENT
200 void operator=(const PlaceholderDef &); // DO NOT IMPLEMENT
Chris Lattner00950542001-06-06 20:29:01 +0000201public:
202 PlaceholderDef(const Type *Ty, unsigned id) : SuperType(Ty), ID(id) {}
203 unsigned getID() { return ID; }
204};
205
206struct InstPlaceHolderHelper : public Instruction {
207 InstPlaceHolderHelper(const Type *Ty) : Instruction(Ty, UserOp1, "") {}
Chris Lattnera41f50d2001-07-07 19:24:15 +0000208 virtual const char *getOpcodeName() const { return "placeholder"; }
Chris Lattner00950542001-06-06 20:29:01 +0000209
210 virtual Instruction *clone() const { abort(); return 0; }
Chris Lattner00950542001-06-06 20:29:01 +0000211};
212
213struct BBPlaceHolderHelper : public BasicBlock {
214 BBPlaceHolderHelper(const Type *Ty) : BasicBlock() {
Chris Lattner35e309a2002-04-08 21:59:36 +0000215 assert(Ty == Type::LabelTy);
Chris Lattner00950542001-06-06 20:29:01 +0000216 }
217};
218
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000219struct ConstantPlaceHolderHelper : public Constant {
220 ConstantPlaceHolderHelper(const Type *Ty)
221 : Constant(Ty) {}
222 virtual bool isNullValue() const { return false; }
223};
224
Chris Lattner74734132002-08-17 22:01:27 +0000225typedef PlaceholderDef<InstPlaceHolderHelper> ValPHolder;
Chris Lattner00950542001-06-06 20:29:01 +0000226typedef PlaceholderDef<BBPlaceHolderHelper> BBPHolder;
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000227typedef PlaceholderDef<ConstantPlaceHolderHelper> ConstPHolder;
228
Misha Brukman12c29d12003-09-22 23:38:23 +0000229// Some common errors we find
230static const std::string Error_readvbr = "read_vbr(): error reading.";
231static const std::string Error_read = "read(): error reading.";
232static const std::string Error_inputdata = "input_data(): error reading.";
233static const std::string Error_DestSlot = "No destination slot found.";
Chris Lattner00950542001-06-06 20:29:01 +0000234
Chris Lattner74734132002-08-17 22:01:27 +0000235static inline unsigned getValueIDNumberFromPlaceHolder(Value *Val) {
236 if (isa<Constant>(Val))
237 return ((ConstPHolder*)Val)->getID();
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000238
239 // else discriminate by type
Chris Lattner74734132002-08-17 22:01:27 +0000240 switch (Val->getType()->getPrimitiveID()) {
241 case Type::LabelTyID: return ((BBPHolder*)Val)->getID();
Chris Lattner74734132002-08-17 22:01:27 +0000242 default: return ((ValPHolder*)Val)->getID();
Chris Lattner00950542001-06-06 20:29:01 +0000243 }
244}
245
Misha Brukman12c29d12003-09-22 23:38:23 +0000246static inline void readBlock(const unsigned char *&Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000247 const unsigned char *EndBuf,
Misha Brukman12c29d12003-09-22 23:38:23 +0000248 unsigned &Type, unsigned &Size) {
Chris Lattner00950542001-06-06 20:29:01 +0000249#if DEBUG_OUTPUT
250 bool Result = read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size);
Anand Shuklaeea60fc2002-06-25 20:44:04 +0000251 std::cerr << "StartLoc = " << ((unsigned)Buf & 4095)
Chris Lattner00950542001-06-06 20:29:01 +0000252 << " Type = " << Type << " Size = " << Size << endl;
Misha Brukman12c29d12003-09-22 23:38:23 +0000253 if (Result) throw Error_read;
Chris Lattner00950542001-06-06 20:29:01 +0000254#else
Misha Brukman12c29d12003-09-22 23:38:23 +0000255 if (read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size)) throw Error_read;
Chris Lattner00950542001-06-06 20:29:01 +0000256#endif
257}
258
Chris Lattner00950542001-06-06 20:29:01 +0000259#endif