blob: 36bf2f6f0e25de5df7d5f3688e636e2c059b9ab4 [file] [log] [blame]
Chris Lattnerd9113c72003-10-13 04:22:07 +00001//===-- ReaderInternals.h - Definitions internal to the reader --*- C++ -*-===//
John Criswell856ba762003-10-21 15:17:13 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This header file defines various stuff that is used by the bytecode reader.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef READER_INTERNALS_H
15#define READER_INTERNALS_H
16
Chris Lattner3446ae82004-01-10 19:00:15 +000017#include "ReaderPrimitives.h"
Chris Lattner8dc6ba92003-11-14 06:38:46 +000018#include "llvm/Constants.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000019#include "llvm/DerivedTypes.h"
Chris Lattner74734132002-08-17 22:01:27 +000020#include "llvm/Function.h"
Misha Brukman12c29d12003-09-22 23:38:23 +000021#include "llvm/ModuleProvider.h"
Chris Lattner00950542001-06-06 20:29:01 +000022#include <utility>
Chris Lattner74734132002-08-17 22:01:27 +000023#include <map>
Chris Lattner00950542001-06-06 20:29:01 +000024
Brian Gaeked0fde302003-11-11 22:41:34 +000025namespace llvm {
26
Chris Lattner1d670cc2001-09-07 16:37:43 +000027// Enable to trace to figure out what the heck is going on when parsing fails
Chris Lattnerd256ed82003-09-04 23:47:07 +000028//#define TRACE_LEVEL 10
Alkis Evlogimenos60048b82003-10-30 18:33:58 +000029//#define DEBUG_OUTPUT
Chris Lattner1d670cc2001-09-07 16:37:43 +000030
Misha Brukman12c29d12003-09-22 23:38:23 +000031#if TRACE_LEVEL // ByteCodeReading_TRACEr
Chris Lattner52e20b02003-03-19 20:54:26 +000032#define BCR_TRACE(n, X) \
33 if (n < TRACE_LEVEL) std::cerr << std::string(n*2, ' ') << X
Chris Lattner1d670cc2001-09-07 16:37:43 +000034#else
35#define BCR_TRACE(n, X)
36#endif
37
Misha Brukman12c29d12003-09-22 23:38:23 +000038struct LazyFunctionInfo {
39 const unsigned char *Buf, *EndBuf;
Chris Lattner29b789b2003-11-19 17:27:18 +000040 LazyFunctionInfo(const unsigned char *B = 0, const unsigned char *EB = 0)
41 : Buf(B), EndBuf(EB) {}
Misha Brukman12c29d12003-09-22 23:38:23 +000042};
43
Chris Lattner00413e32003-10-04 20:14:59 +000044class BytecodeParser : public ModuleProvider {
Chris Lattner74734132002-08-17 22:01:27 +000045 BytecodeParser(const BytecodeParser &); // DO NOT IMPLEMENT
46 void operator=(const BytecodeParser &); // DO NOT IMPLEMENT
Chris Lattner00950542001-06-06 20:29:01 +000047public:
Chris Lattner89e02532004-01-18 21:08:15 +000048 BytecodeParser() {}
Misha Brukman12c29d12003-09-22 23:38:23 +000049
Chris Lattner52e20b02003-03-19 20:54:26 +000050 ~BytecodeParser() {
Chris Lattnera2602f32003-05-22 18:26:48 +000051 freeState();
52 }
53 void freeState() {
Chris Lattner52e20b02003-03-19 20:54:26 +000054 freeTable(Values);
Chris Lattner52e20b02003-03-19 20:54:26 +000055 freeTable(ModuleValues);
56 }
Chris Lattner00950542001-06-06 20:29:01 +000057
Misha Brukmanafca90e2004-01-21 22:55:34 +000058 Module* materializeModule() {
59 while (! LazyFunctionLoadMap.empty()) {
60 std::map<Function*, LazyFunctionInfo>::iterator i =
61 LazyFunctionLoadMap.begin();
62 materializeFunction((*i).first);
63 }
64
65 return TheModule;
66 }
67
Misha Brukman12c29d12003-09-22 23:38:23 +000068 Module* releaseModule() {
69 // Since we're losing control of this Module, we must hand it back complete
Chris Lattner927b1852003-10-09 20:22:47 +000070 Module *M = ModuleProvider::releaseModule();
Misha Brukman12c29d12003-09-22 23:38:23 +000071 freeState();
Chris Lattner927b1852003-10-09 20:22:47 +000072 return M;
Misha Brukman12c29d12003-09-22 23:38:23 +000073 }
Chris Lattnerd6b65252001-10-24 01:15:12 +000074
Misha Brukman12c29d12003-09-22 23:38:23 +000075 void ParseBytecode(const unsigned char *Buf, unsigned Length,
76 const std::string &ModuleID);
Chris Lattnerd6b65252001-10-24 01:15:12 +000077
Chris Lattnerb3afb1f2002-04-04 19:24:11 +000078 void dump() const {
Anand Shuklaeea60fc2002-06-25 20:44:04 +000079 std::cerr << "BytecodeParser instance!\n";
Chris Lattnerb3afb1f2002-04-04 19:24:11 +000080 }
81
Chris Lattner6e448022003-10-08 21:51:46 +000082private:
Chris Lattner52e20b02003-03-19 20:54:26 +000083 struct ValueList : public User {
Chris Lattner6e448022003-10-08 21:51:46 +000084 ValueList() : User(Type::TypeTy, Value::TypeVal) {}
Chris Lattner52e20b02003-03-19 20:54:26 +000085
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
Chris Lattner036b8aa2003-03-06 17:55:45 +0000100
Chris Lattner80b97342004-01-17 23:25:43 +0000101 // Flags to distinguish LLVM 1.0 & 1.1 bytecode formats (revision #0)
102
103 // Revision #0 had an explicit alignment of data only for the ModuleGlobalInfo
104 // block. This was fixed to be like all other blocks in 1.2
Chris Lattner44d0eeb2004-01-15 17:55:01 +0000105 bool hasInconsistentModuleGlobalInfo;
106
Chris Lattner80b97342004-01-17 23:25:43 +0000107 // Revision #0 also explicitly encoded zero values for primitive types like
108 // int/sbyte/etc.
109 bool hasExplicitPrimitiveZeros;
110
Chris Lattner5fa428f2004-04-05 01:27:26 +0000111 // Flags to control features specific the LLVM 1.2 and before (revision #1)
112
113 // LLVM 1.2 and earlier required that getelementptr structure indices were
114 // ubyte constants and that sequential type indices were longs.
115 bool hasRestrictedGEPTypes;
116
117
Chris Lattner52e20b02003-03-19 20:54:26 +0000118 typedef std::vector<ValueList*> ValueTable;
Chris Lattner8eb10ce2003-10-09 06:05:40 +0000119 ValueTable Values;
Chris Lattner52e20b02003-03-19 20:54:26 +0000120 ValueTable ModuleValues;
Chris Lattner8eb10ce2003-10-09 06:05:40 +0000121 std::map<std::pair<unsigned,unsigned>, Value*> ForwardReferences;
Chris Lattner1d670cc2001-09-07 16:37:43 +0000122
Chris Lattner89e02532004-01-18 21:08:15 +0000123 /// CompactionTable - If a compaction table is active in the current function,
124 /// this is the mapping that it contains.
125 std::vector<std::vector<Value*> > CompactionTable;
126
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000127 std::vector<BasicBlock*> ParsedBasicBlocks;
128
Chris Lattner29b789b2003-11-19 17:27:18 +0000129 // ConstantFwdRefs - This maintains a mapping between <Type, Slot #>'s and
130 // forward references to constants. Such values may be referenced before they
131 // are defined, and if so, the temporary object that they represent is held
132 // here.
Chris Lattner74734132002-08-17 22:01:27 +0000133 //
Chris Lattner29b789b2003-11-19 17:27:18 +0000134 typedef std::map<std::pair<const Type*,unsigned>, Constant*> ConstantRefsType;
135 ConstantRefsType ConstantFwdRefs;
Chris Lattner05950c32001-10-13 06:47:01 +0000136
Chris Lattner1d670cc2001-09-07 16:37:43 +0000137 // TypesLoaded - This vector mirrors the Values[TypeTyID] plane. It is used
138 // to deal with forward references to types.
139 //
Chris Lattnerc7b6f032003-10-02 20:26:18 +0000140 typedef std::vector<PATypeHolder> TypeValuesListTy;
Chris Lattner1d670cc2001-09-07 16:37:43 +0000141 TypeValuesListTy ModuleTypeValues;
Chris Lattner6e5a0e42003-03-06 17:18:14 +0000142 TypeValuesListTy FunctionTypeValues;
Chris Lattner00950542001-06-06 20:29:01 +0000143
Chris Lattner52e20b02003-03-19 20:54:26 +0000144 // When the ModuleGlobalInfo section is read, we create a function object for
145 // each function in the module. When the function is loaded, this function is
146 // filled in.
Chris Lattner00950542001-06-06 20:29:01 +0000147 //
Chris Lattner29b789b2003-11-19 17:27:18 +0000148 std::vector<Function*> FunctionSignatureList;
Chris Lattner52e20b02003-03-19 20:54:26 +0000149
150 // Constant values are read in after global variables. Because of this, we
151 // must defer setting the initializers on global variables until after module
152 // level constants have been read. In the mean time, this list keeps track of
153 // what we must do.
154 //
155 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
Chris Lattner00950542001-06-06 20:29:01 +0000156
Misha Brukman12c29d12003-09-22 23:38:23 +0000157 // For lazy reading-in of functions, we need to save away several pieces of
158 // information about each function: its begin and end pointer in the buffer
159 // and its FunctionSlot.
160 //
Chris Lattner29b789b2003-11-19 17:27:18 +0000161 std::map<Function*, LazyFunctionInfo> LazyFunctionLoadMap;
Misha Brukman12c29d12003-09-22 23:38:23 +0000162
Chris Lattner00950542001-06-06 20:29:01 +0000163private:
Chris Lattner52e20b02003-03-19 20:54:26 +0000164 void freeTable(ValueTable &Tab) {
165 while (!Tab.empty()) {
166 delete Tab.back();
167 Tab.pop_back();
168 }
169 }
170
Chris Lattner89e02532004-01-18 21:08:15 +0000171 /// getGlobalTableType - This is just like getType, but when a compaction
172 /// table is in use, it is ignored. Also, no forward references or other
173 /// fancy features are supported.
174 const Type *getGlobalTableType(unsigned Slot) {
175 if (Slot < Type::FirstDerivedTyID) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000176 const Type *Ty = Type::getPrimitiveType((Type::TypeID)Slot);
Chris Lattner89e02532004-01-18 21:08:15 +0000177 assert(Ty && "Not a primitive type ID?");
178 return Ty;
179 }
180 Slot -= Type::FirstDerivedTyID;
181 if (Slot >= ModuleTypeValues.size())
182 throw std::string("Illegal compaction table type reference!");
183 return ModuleTypeValues[Slot];
184 }
185
186 unsigned getGlobalTableTypeSlot(const Type *Ty) {
187 if (Ty->isPrimitiveType())
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000188 return Ty->getTypeID();
Chris Lattner89e02532004-01-18 21:08:15 +0000189 TypeValuesListTy::iterator I = find(ModuleTypeValues.begin(),
190 ModuleTypeValues.end(), Ty);
191 if (I == ModuleTypeValues.end())
192 throw std::string("Didn't find type in ModuleTypeValues.");
193 return Type::FirstDerivedTyID + (&*I - &ModuleTypeValues[0]);
194 }
195
196 /// getGlobalTableValue - This is just like getValue, but when a compaction
197 /// table is in use, it is ignored. Also, no forward references or other
198 /// fancy features are supported.
199 Value *getGlobalTableValue(const Type *Ty, unsigned SlotNo) {
200 // FIXME: getTypeSlot is inefficient!
201 unsigned TyID = getGlobalTableTypeSlot(Ty);
202
203 if (TyID != Type::LabelTyID) {
204 if (SlotNo == 0)
205 return Constant::getNullValue(Ty);
206 --SlotNo;
207 }
208
209 if (TyID >= ModuleValues.size() || ModuleValues[TyID] == 0 ||
210 SlotNo >= ModuleValues[TyID]->getNumOperands()) {
211 std::cerr << TyID << ", " << SlotNo << ": " << ModuleValues.size() << ", "
212 << (void*)ModuleValues[TyID] << ", "
213 << ModuleValues[TyID]->getNumOperands() << "\n";
214 throw std::string("Corrupt compaction table entry!");
215 }
216 return ModuleValues[TyID]->getOperand(SlotNo);
217 }
218
Misha Brukman12c29d12003-09-22 23:38:23 +0000219public:
220 void ParseModule(const unsigned char * Buf, const unsigned char *End);
221 void materializeFunction(Function *F);
222
223private:
224 void ParseVersionInfo (const unsigned char *&Buf, const unsigned char *End);
225 void ParseModuleGlobalInfo(const unsigned char *&Buf, const unsigned char *E);
226 void ParseSymbolTable(const unsigned char *&Buf, const unsigned char *End,
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000227 SymbolTable *, Function *CurrentFunction);
Misha Brukman12c29d12003-09-22 23:38:23 +0000228 void ParseFunction(const unsigned char *&Buf, const unsigned char *End);
Chris Lattner89e02532004-01-18 21:08:15 +0000229 void ParseCompactionTable(const unsigned char *&Buf,const unsigned char *End);
Misha Brukman12c29d12003-09-22 23:38:23 +0000230 void ParseGlobalTypes(const unsigned char *&Buf, const unsigned char *EndBuf);
231
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000232 BasicBlock *ParseBasicBlock(const unsigned char *&Buf,
233 const unsigned char *End,
234 unsigned BlockNo);
Chris Lattner8d1dbd22003-12-01 07:05:31 +0000235 unsigned ParseInstructionList(Function *F, const unsigned char *&Buf,
236 const unsigned char *EndBuf);
237
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000238 void ParseInstruction(const unsigned char *&Buf, const unsigned char *End,
239 std::vector<unsigned> &Args, BasicBlock *BB);
Chris Lattner00950542001-06-06 20:29:01 +0000240
Misha Brukman12c29d12003-09-22 23:38:23 +0000241 void ParseConstantPool(const unsigned char *&Buf, const unsigned char *EndBuf,
242 ValueTable &Tab, TypeValuesListTy &TypeTab);
Chris Lattner9e460f22003-10-04 20:00:03 +0000243 Constant *parseConstantValue(const unsigned char *&Buf,
244 const unsigned char *End,
Chris Lattner29b789b2003-11-19 17:27:18 +0000245 unsigned TypeID);
Misha Brukman12c29d12003-09-22 23:38:23 +0000246 void parseTypeConstants(const unsigned char *&Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000247 const unsigned char *EndBuf,
Misha Brukman12c29d12003-09-22 23:38:23 +0000248 TypeValuesListTy &Tab, unsigned NumEntries);
Chris Lattner12e64652003-05-22 18:08:30 +0000249 const Type *parseTypeConstant(const unsigned char *&Buf,
250 const unsigned char *EndBuf);
Chris Lattner9e893e82004-01-14 23:35:21 +0000251 void parseStringConstants(const unsigned char *&Buf,
252 const unsigned char *EndBuf,
253 unsigned NumEntries, ValueTable &Tab);
Chris Lattner00950542001-06-06 20:29:01 +0000254
Chris Lattner36392bc2003-10-08 21:18:57 +0000255 Value *getValue(unsigned TypeID, unsigned num, bool Create = true);
Chris Lattner00950542001-06-06 20:29:01 +0000256 const Type *getType(unsigned ID);
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000257 BasicBlock *getBasicBlock(unsigned ID);
Chris Lattner1c3673b2003-11-19 06:01:12 +0000258 Constant *getConstantValue(unsigned TypeID, unsigned num);
259 Constant *getConstantValue(const Type *Ty, unsigned num) {
260 return getConstantValue(getTypeSlot(Ty), num);
261 }
Chris Lattner00950542001-06-06 20:29:01 +0000262
Chris Lattnerf0d92732003-10-13 14:34:59 +0000263 unsigned insertValue(Value *V, unsigned Type, ValueTable &Table);
Chris Lattner00950542001-06-06 20:29:01 +0000264
Chris Lattner9e460f22003-10-04 20:00:03 +0000265 unsigned getTypeSlot(const Type *Ty);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000266
Chris Lattner29b789b2003-11-19 17:27:18 +0000267 // resolve all references to the placeholder (if any) for the given constant
268 void ResolveReferencesToConstant(Constant *C, unsigned Slot);
Chris Lattner00950542001-06-06 20:29:01 +0000269};
270
271template<class SuperType>
272class PlaceholderDef : public SuperType {
273 unsigned ID;
Chris Lattner74734132002-08-17 22:01:27 +0000274 PlaceholderDef(); // DO NOT IMPLEMENT
275 void operator=(const PlaceholderDef &); // DO NOT IMPLEMENT
Chris Lattner00950542001-06-06 20:29:01 +0000276public:
277 PlaceholderDef(const Type *Ty, unsigned id) : SuperType(Ty), ID(id) {}
278 unsigned getID() { return ID; }
279};
280
Chris Lattner8dc6ba92003-11-14 06:38:46 +0000281struct ConstantPlaceHolderHelper : public ConstantExpr {
Chris Lattnerdb9546e2003-11-14 16:34:25 +0000282 ConstantPlaceHolderHelper(const Type *Ty)
283 : ConstantExpr(Instruction::UserOp1, Constant::getNullValue(Ty), Ty) {}
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000284};
285
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000286typedef PlaceholderDef<ConstantPlaceHolderHelper> ConstPHolder;
287
Misha Brukman12c29d12003-09-22 23:38:23 +0000288static inline void readBlock(const unsigned char *&Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000289 const unsigned char *EndBuf,
Misha Brukman12c29d12003-09-22 23:38:23 +0000290 unsigned &Type, unsigned &Size) {
Chris Lattner7969dc22004-01-15 06:13:09 +0000291 Type = read(Buf, EndBuf);
292 Size = read(Buf, EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +0000293}
294
Brian Gaeked0fde302003-11-11 22:41:34 +0000295} // End llvm namespace
296
Chris Lattner00950542001-06-06 20:29:01 +0000297#endif