blob: b0c7abfff67da219c9b7f29c12ea1445f0f19234 [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 Lattner6e448022003-10-08 21:51:46 +000080private:
Chris Lattner52e20b02003-03-19 20:54:26 +000081 struct ValueList : public User {
Chris Lattner6e448022003-10-08 21:51:46 +000082 ValueList() : User(Type::TypeTy, Value::TypeVal) {}
Chris Lattner52e20b02003-03-19 20:54:26 +000083
84 // vector compatibility methods
85 unsigned size() const { return getNumOperands(); }
86 void push_back(Value *V) { Operands.push_back(Use(V, this)); }
87 Value *back() const { return Operands.back(); }
88 void pop_back() { Operands.pop_back(); }
89 bool empty() const { return Operands.empty(); }
90
91 virtual void print(std::ostream& OS) const {
92 OS << "Bytecode Reader UseHandle!";
93 }
94 };
95
Chris Lattner036b8aa2003-03-06 17:55:45 +000096 // Information about the module, extracted from the bytecode revision number.
97 unsigned char RevisionNum; // The rev # itself
98 unsigned char FirstDerivedTyID; // First variable index to use for type
99 bool HasImplicitZeroInitializer; // Is entry 0 of every slot implicity zeros?
Chris Lattnere3869c82003-04-16 21:16:05 +0000100 bool hasInternalMarkerOnly; // Only types of linkage are intern/external
Chris Lattner036b8aa2003-03-06 17:55:45 +0000101
Chris Lattner52e20b02003-03-19 20:54:26 +0000102 typedef std::vector<ValueList*> ValueTable;
Chris Lattner00950542001-06-06 20:29:01 +0000103 ValueTable Values, LateResolveValues;
Chris Lattner52e20b02003-03-19 20:54:26 +0000104 ValueTable ModuleValues;
Chris Lattner1d670cc2001-09-07 16:37:43 +0000105
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000106 std::vector<BasicBlock*> ParsedBasicBlocks;
107
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,
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000158 SymbolTable *, Function *CurrentFunction);
Misha Brukman12c29d12003-09-22 23:38:23 +0000159 void ParseFunction(const unsigned char *&Buf, const unsigned char *End);
160 void ParseGlobalTypes(const unsigned char *&Buf, const unsigned char *EndBuf);
161
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000162 BasicBlock *ParseBasicBlock(const unsigned char *&Buf,
163 const unsigned char *End,
164 unsigned BlockNo);
Misha Brukman12c29d12003-09-22 23:38:23 +0000165
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000166 bool ParseInstruction(const unsigned char *&Buf, const unsigned char *End,
167 Instruction *&);
Misha Brukmand554ebf2003-09-23 16:17:50 +0000168 std::auto_ptr<RawInst> ParseRawInst(const unsigned char *&Buf,
169 const unsigned char *End);
Chris Lattner00950542001-06-06 20:29:01 +0000170
Misha Brukman12c29d12003-09-22 23:38:23 +0000171 void ParseConstantPool(const unsigned char *&Buf, const unsigned char *EndBuf,
172 ValueTable &Tab, TypeValuesListTy &TypeTab);
Chris Lattner9e460f22003-10-04 20:00:03 +0000173 Constant *parseConstantValue(const unsigned char *&Buf,
174 const unsigned char *End,
175 const Type *Ty);
Misha Brukman12c29d12003-09-22 23:38:23 +0000176 void parseTypeConstants(const unsigned char *&Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000177 const unsigned char *EndBuf,
Misha Brukman12c29d12003-09-22 23:38:23 +0000178 TypeValuesListTy &Tab, unsigned NumEntries);
Chris Lattner12e64652003-05-22 18:08:30 +0000179 const Type *parseTypeConstant(const unsigned char *&Buf,
180 const unsigned char *EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +0000181
182 Value *getValue(const Type *Ty, unsigned num, bool Create = true);
Chris Lattner36392bc2003-10-08 21:18:57 +0000183 Value *getValue(unsigned TypeID, unsigned num, bool Create = true);
Chris Lattner00950542001-06-06 20:29:01 +0000184 const Type *getType(unsigned ID);
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000185 BasicBlock *getBasicBlock(unsigned ID);
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000186 Constant *getConstantValue(const Type *Ty, unsigned num);
Chris Lattner00950542001-06-06 20:29:01 +0000187
Chris Lattner52e20b02003-03-19 20:54:26 +0000188 int insertValue(Value *V, ValueTable &Table); // -1 = Failure
189 void setValueTo(ValueTable &D, unsigned Slot, Value *V);
Chris Lattner00950542001-06-06 20:29:01 +0000190
Chris Lattner9e460f22003-10-04 20:00:03 +0000191 unsigned getTypeSlot(const Type *Ty);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000192
Chris Lattner74734132002-08-17 22:01:27 +0000193 // resolve all references to the placeholder (if any) for the given value
194 void ResolveReferencesToValue(Value *Val, unsigned Slot);
Chris Lattner00950542001-06-06 20:29:01 +0000195};
196
197template<class SuperType>
198class PlaceholderDef : public SuperType {
199 unsigned ID;
Chris Lattner74734132002-08-17 22:01:27 +0000200 PlaceholderDef(); // DO NOT IMPLEMENT
201 void operator=(const PlaceholderDef &); // DO NOT IMPLEMENT
Chris Lattner00950542001-06-06 20:29:01 +0000202public:
203 PlaceholderDef(const Type *Ty, unsigned id) : SuperType(Ty), ID(id) {}
204 unsigned getID() { return ID; }
205};
206
207struct InstPlaceHolderHelper : public Instruction {
208 InstPlaceHolderHelper(const Type *Ty) : Instruction(Ty, UserOp1, "") {}
Chris Lattnera41f50d2001-07-07 19:24:15 +0000209 virtual const char *getOpcodeName() const { return "placeholder"; }
Chris Lattner00950542001-06-06 20:29:01 +0000210
211 virtual Instruction *clone() const { abort(); return 0; }
Chris Lattner00950542001-06-06 20:29:01 +0000212};
213
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000214struct ConstantPlaceHolderHelper : public Constant {
215 ConstantPlaceHolderHelper(const Type *Ty)
216 : Constant(Ty) {}
217 virtual bool isNullValue() const { return false; }
218};
219
Chris Lattner74734132002-08-17 22:01:27 +0000220typedef PlaceholderDef<InstPlaceHolderHelper> ValPHolder;
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000221typedef PlaceholderDef<ConstantPlaceHolderHelper> ConstPHolder;
222
Misha Brukman12c29d12003-09-22 23:38:23 +0000223// Some common errors we find
224static const std::string Error_readvbr = "read_vbr(): error reading.";
225static const std::string Error_read = "read(): error reading.";
226static const std::string Error_inputdata = "input_data(): error reading.";
227static const std::string Error_DestSlot = "No destination slot found.";
Chris Lattner00950542001-06-06 20:29:01 +0000228
Chris Lattner74734132002-08-17 22:01:27 +0000229static inline unsigned getValueIDNumberFromPlaceHolder(Value *Val) {
230 if (isa<Constant>(Val))
231 return ((ConstPHolder*)Val)->getID();
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000232 return ((ValPHolder*)Val)->getID();
Chris Lattner00950542001-06-06 20:29:01 +0000233}
234
Misha Brukman12c29d12003-09-22 23:38:23 +0000235static inline void readBlock(const unsigned char *&Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000236 const unsigned char *EndBuf,
Misha Brukman12c29d12003-09-22 23:38:23 +0000237 unsigned &Type, unsigned &Size) {
Chris Lattner00950542001-06-06 20:29:01 +0000238#if DEBUG_OUTPUT
239 bool Result = read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size);
Anand Shuklaeea60fc2002-06-25 20:44:04 +0000240 std::cerr << "StartLoc = " << ((unsigned)Buf & 4095)
Chris Lattner00950542001-06-06 20:29:01 +0000241 << " Type = " << Type << " Size = " << Size << endl;
Misha Brukman12c29d12003-09-22 23:38:23 +0000242 if (Result) throw Error_read;
Chris Lattner00950542001-06-06 20:29:01 +0000243#else
Misha Brukman12c29d12003-09-22 23:38:23 +0000244 if (read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size)) throw Error_read;
Chris Lattner00950542001-06-06 20:29:01 +0000245#endif
246}
247
Chris Lattner00950542001-06-06 20:29:01 +0000248#endif