blob: 7f921d7913d2ce58746aa0e78de95e0be4a12fd3 [file] [log] [blame]
Chris Lattnerd9113c72003-10-13 04:22:07 +00001//===-- ReaderInternals.h - Definitions internal to the reader --*- C++ -*-===//
Chris Lattner00950542001-06-06 20:29:01 +00002//
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>
Chris Lattner00950542001-06-06 20:29:01 +000017
Chris Lattner1d670cc2001-09-07 16:37:43 +000018// Enable to trace to figure out what the heck is going on when parsing fails
Chris Lattnerd256ed82003-09-04 23:47:07 +000019//#define TRACE_LEVEL 10
Chris Lattner1d670cc2001-09-07 16:37:43 +000020
Misha Brukman12c29d12003-09-22 23:38:23 +000021#if TRACE_LEVEL // ByteCodeReading_TRACEr
Chris Lattner52e20b02003-03-19 20:54:26 +000022#define BCR_TRACE(n, X) \
23 if (n < TRACE_LEVEL) std::cerr << std::string(n*2, ' ') << X
Chris Lattner1d670cc2001-09-07 16:37:43 +000024#else
25#define BCR_TRACE(n, X)
26#endif
27
Misha Brukman12c29d12003-09-22 23:38:23 +000028struct LazyFunctionInfo {
29 const unsigned char *Buf, *EndBuf;
30 unsigned FunctionSlot;
31};
32
Chris Lattner00413e32003-10-04 20:14:59 +000033class BytecodeParser : public ModuleProvider {
Chris Lattner74734132002-08-17 22:01:27 +000034 BytecodeParser(const BytecodeParser &); // DO NOT IMPLEMENT
35 void operator=(const BytecodeParser &); // DO NOT IMPLEMENT
Chris Lattner00950542001-06-06 20:29:01 +000036public:
Misha Brukman96f78772003-09-22 23:58:08 +000037 BytecodeParser() {
Chris Lattner00950542001-06-06 20:29:01 +000038 // Define this in case we don't see a ModuleGlobalInfo block.
39 FirstDerivedTyID = Type::FirstDerivedTyID;
40 }
Misha Brukman12c29d12003-09-22 23:38:23 +000041
Chris Lattner52e20b02003-03-19 20:54:26 +000042 ~BytecodeParser() {
Chris Lattnera2602f32003-05-22 18:26:48 +000043 freeState();
44 }
45 void freeState() {
Chris Lattner52e20b02003-03-19 20:54:26 +000046 freeTable(Values);
Chris Lattner52e20b02003-03-19 20:54:26 +000047 freeTable(ModuleValues);
48 }
Chris Lattner00950542001-06-06 20:29:01 +000049
Misha Brukman12c29d12003-09-22 23:38:23 +000050 Module* releaseModule() {
51 // Since we're losing control of this Module, we must hand it back complete
Chris Lattner927b1852003-10-09 20:22:47 +000052 Module *M = ModuleProvider::releaseModule();
Misha Brukman12c29d12003-09-22 23:38:23 +000053 freeState();
Chris Lattner927b1852003-10-09 20:22:47 +000054 return M;
Misha Brukman12c29d12003-09-22 23:38:23 +000055 }
Chris Lattnerd6b65252001-10-24 01:15:12 +000056
Misha Brukman12c29d12003-09-22 23:38:23 +000057 void ParseBytecode(const unsigned char *Buf, unsigned Length,
58 const std::string &ModuleID);
Chris Lattnerd6b65252001-10-24 01:15:12 +000059
Chris Lattnerb3afb1f2002-04-04 19:24:11 +000060 void dump() const {
Anand Shuklaeea60fc2002-06-25 20:44:04 +000061 std::cerr << "BytecodeParser instance!\n";
Chris Lattnerb3afb1f2002-04-04 19:24:11 +000062 }
63
Chris Lattner6e448022003-10-08 21:51:46 +000064private:
Chris Lattner52e20b02003-03-19 20:54:26 +000065 struct ValueList : public User {
Chris Lattner6e448022003-10-08 21:51:46 +000066 ValueList() : User(Type::TypeTy, Value::TypeVal) {}
Chris Lattner52e20b02003-03-19 20:54:26 +000067
68 // vector compatibility methods
69 unsigned size() const { return getNumOperands(); }
70 void push_back(Value *V) { Operands.push_back(Use(V, this)); }
71 Value *back() const { return Operands.back(); }
72 void pop_back() { Operands.pop_back(); }
73 bool empty() const { return Operands.empty(); }
74
75 virtual void print(std::ostream& OS) const {
76 OS << "Bytecode Reader UseHandle!";
77 }
78 };
79
Chris Lattner036b8aa2003-03-06 17:55:45 +000080 // Information about the module, extracted from the bytecode revision number.
81 unsigned char RevisionNum; // The rev # itself
82 unsigned char FirstDerivedTyID; // First variable index to use for type
83 bool HasImplicitZeroInitializer; // Is entry 0 of every slot implicity zeros?
Chris Lattnere3869c82003-04-16 21:16:05 +000084 bool hasInternalMarkerOnly; // Only types of linkage are intern/external
Chris Lattner036b8aa2003-03-06 17:55:45 +000085
Chris Lattner52e20b02003-03-19 20:54:26 +000086 typedef std::vector<ValueList*> ValueTable;
Chris Lattner8eb10ce2003-10-09 06:05:40 +000087 ValueTable Values;
Chris Lattner52e20b02003-03-19 20:54:26 +000088 ValueTable ModuleValues;
Chris Lattner8eb10ce2003-10-09 06:05:40 +000089 std::map<std::pair<unsigned,unsigned>, Value*> ForwardReferences;
Chris Lattner1d670cc2001-09-07 16:37:43 +000090
Chris Lattner4ee8ef22003-10-08 22:52:54 +000091 std::vector<BasicBlock*> ParsedBasicBlocks;
92
Chris Lattner74734132002-08-17 22:01:27 +000093 // GlobalRefs - This maintains a mapping between <Type, Slot #>'s and forward
94 // references to global values or constants. Such values may be referenced
95 // before they are defined, and if so, the temporary object that they
96 // represent is held here.
97 //
Chris Lattner52e20b02003-03-19 20:54:26 +000098 typedef std::map<std::pair<const Type *, unsigned>, Value*> GlobalRefsType;
Chris Lattner74734132002-08-17 22:01:27 +000099 GlobalRefsType GlobalRefs;
Chris Lattner05950c32001-10-13 06:47:01 +0000100
Chris Lattner1d670cc2001-09-07 16:37:43 +0000101 // TypesLoaded - This vector mirrors the Values[TypeTyID] plane. It is used
102 // to deal with forward references to types.
103 //
Chris Lattnerc7b6f032003-10-02 20:26:18 +0000104 typedef std::vector<PATypeHolder> TypeValuesListTy;
Chris Lattner1d670cc2001-09-07 16:37:43 +0000105 TypeValuesListTy ModuleTypeValues;
Chris Lattner6e5a0e42003-03-06 17:18:14 +0000106 TypeValuesListTy FunctionTypeValues;
Chris Lattner00950542001-06-06 20:29:01 +0000107
Chris Lattner52e20b02003-03-19 20:54:26 +0000108 // When the ModuleGlobalInfo section is read, we create a function object for
109 // each function in the module. When the function is loaded, this function is
110 // filled in.
Chris Lattner00950542001-06-06 20:29:01 +0000111 //
Chris Lattner52e20b02003-03-19 20:54:26 +0000112 std::vector<std::pair<Function*, unsigned> > FunctionSignatureList;
113
114 // Constant values are read in after global variables. Because of this, we
115 // must defer setting the initializers on global variables until after module
116 // level constants have been read. In the mean time, this list keeps track of
117 // what we must do.
118 //
119 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
Chris Lattner00950542001-06-06 20:29:01 +0000120
Misha Brukman12c29d12003-09-22 23:38:23 +0000121 // For lazy reading-in of functions, we need to save away several pieces of
122 // information about each function: its begin and end pointer in the buffer
123 // and its FunctionSlot.
124 //
125 std::map<Function*, LazyFunctionInfo*> LazyFunctionLoadMap;
126
Chris Lattner00950542001-06-06 20:29:01 +0000127private:
Chris Lattner52e20b02003-03-19 20:54:26 +0000128 void freeTable(ValueTable &Tab) {
129 while (!Tab.empty()) {
130 delete Tab.back();
131 Tab.pop_back();
132 }
133 }
134
Misha Brukman12c29d12003-09-22 23:38:23 +0000135public:
136 void ParseModule(const unsigned char * Buf, const unsigned char *End);
137 void materializeFunction(Function *F);
138
139private:
140 void ParseVersionInfo (const unsigned char *&Buf, const unsigned char *End);
141 void ParseModuleGlobalInfo(const unsigned char *&Buf, const unsigned char *E);
142 void ParseSymbolTable(const unsigned char *&Buf, const unsigned char *End,
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000143 SymbolTable *, Function *CurrentFunction);
Misha Brukman12c29d12003-09-22 23:38:23 +0000144 void ParseFunction(const unsigned char *&Buf, const unsigned char *End);
145 void ParseGlobalTypes(const unsigned char *&Buf, const unsigned char *EndBuf);
146
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000147 BasicBlock *ParseBasicBlock(const unsigned char *&Buf,
148 const unsigned char *End,
149 unsigned BlockNo);
Misha Brukman12c29d12003-09-22 23:38:23 +0000150
Chris Lattner3483f542003-10-09 18:25:19 +0000151 Instruction *ParseInstruction(const unsigned char *&Buf,
Chris Lattner6fcf5032003-10-09 20:45:42 +0000152 const unsigned char *End,
153 std::vector<unsigned> &Args);
Chris Lattner00950542001-06-06 20:29:01 +0000154
Misha Brukman12c29d12003-09-22 23:38:23 +0000155 void ParseConstantPool(const unsigned char *&Buf, const unsigned char *EndBuf,
156 ValueTable &Tab, TypeValuesListTy &TypeTab);
Chris Lattner9e460f22003-10-04 20:00:03 +0000157 Constant *parseConstantValue(const unsigned char *&Buf,
158 const unsigned char *End,
159 const Type *Ty);
Misha Brukman12c29d12003-09-22 23:38:23 +0000160 void parseTypeConstants(const unsigned char *&Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000161 const unsigned char *EndBuf,
Misha Brukman12c29d12003-09-22 23:38:23 +0000162 TypeValuesListTy &Tab, unsigned NumEntries);
Chris Lattner12e64652003-05-22 18:08:30 +0000163 const Type *parseTypeConstant(const unsigned char *&Buf,
164 const unsigned char *EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +0000165
166 Value *getValue(const Type *Ty, unsigned num, bool Create = true);
Chris Lattner36392bc2003-10-08 21:18:57 +0000167 Value *getValue(unsigned TypeID, unsigned num, bool Create = true);
Chris Lattner00950542001-06-06 20:29:01 +0000168 const Type *getType(unsigned ID);
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000169 BasicBlock *getBasicBlock(unsigned ID);
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000170 Constant *getConstantValue(const Type *Ty, unsigned num);
Chris Lattner00950542001-06-06 20:29:01 +0000171
Chris Lattner927b1852003-10-09 20:22:47 +0000172 unsigned insertValue(Value *V, ValueTable &Table);
Chris Lattnerf0d92732003-10-13 14:34:59 +0000173 unsigned insertValue(Value *V, unsigned Type, ValueTable &Table);
Chris Lattner00950542001-06-06 20:29:01 +0000174
Chris Lattner9e460f22003-10-04 20:00:03 +0000175 unsigned getTypeSlot(const Type *Ty);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000176
Chris Lattner74734132002-08-17 22:01:27 +0000177 // resolve all references to the placeholder (if any) for the given value
178 void ResolveReferencesToValue(Value *Val, unsigned Slot);
Chris Lattner00950542001-06-06 20:29:01 +0000179};
180
181template<class SuperType>
182class PlaceholderDef : public SuperType {
183 unsigned ID;
Chris Lattner74734132002-08-17 22:01:27 +0000184 PlaceholderDef(); // DO NOT IMPLEMENT
185 void operator=(const PlaceholderDef &); // DO NOT IMPLEMENT
Chris Lattner00950542001-06-06 20:29:01 +0000186public:
187 PlaceholderDef(const Type *Ty, unsigned id) : SuperType(Ty), ID(id) {}
188 unsigned getID() { return ID; }
189};
190
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000191struct ConstantPlaceHolderHelper : public Constant {
192 ConstantPlaceHolderHelper(const Type *Ty)
193 : Constant(Ty) {}
194 virtual bool isNullValue() const { return false; }
195};
196
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000197typedef PlaceholderDef<ConstantPlaceHolderHelper> ConstPHolder;
198
Misha Brukman12c29d12003-09-22 23:38:23 +0000199// Some common errors we find
200static const std::string Error_readvbr = "read_vbr(): error reading.";
201static const std::string Error_read = "read(): error reading.";
202static const std::string Error_inputdata = "input_data(): error reading.";
203static const std::string Error_DestSlot = "No destination slot found.";
Chris Lattner00950542001-06-06 20:29:01 +0000204
Misha Brukman12c29d12003-09-22 23:38:23 +0000205static inline void readBlock(const unsigned char *&Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000206 const unsigned char *EndBuf,
Misha Brukman12c29d12003-09-22 23:38:23 +0000207 unsigned &Type, unsigned &Size) {
Chris Lattner00950542001-06-06 20:29:01 +0000208#if DEBUG_OUTPUT
209 bool Result = read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size);
Anand Shuklaeea60fc2002-06-25 20:44:04 +0000210 std::cerr << "StartLoc = " << ((unsigned)Buf & 4095)
Chris Lattner00950542001-06-06 20:29:01 +0000211 << " Type = " << Type << " Size = " << Size << endl;
Misha Brukman12c29d12003-09-22 23:38:23 +0000212 if (Result) throw Error_read;
Chris Lattner00950542001-06-06 20:29:01 +0000213#else
Misha Brukman12c29d12003-09-22 23:38:23 +0000214 if (read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size)) throw Error_read;
Chris Lattner00950542001-06-06 20:29:01 +0000215#endif
216}
217
Chris Lattner00950542001-06-06 20:29:01 +0000218#endif