blob: 8eb138a16784c68097127c9daddb0f640a1b1827 [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
Misha Brukman12c29d12003-09-22 23:38:23 +000017#include "llvm/Constant.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000018#include "llvm/DerivedTypes.h"
Chris Lattner74734132002-08-17 22:01:27 +000019#include "llvm/Function.h"
Misha Brukman12c29d12003-09-22 23:38:23 +000020#include "llvm/ModuleProvider.h"
21#include "llvm/Bytecode/Primitives.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
Chris Lattner1d670cc2001-09-07 16:37:43 +000025// Enable to trace to figure out what the heck is going on when parsing fails
Chris Lattnerd256ed82003-09-04 23:47:07 +000026//#define TRACE_LEVEL 10
Chris Lattner1d670cc2001-09-07 16:37:43 +000027
Misha Brukman12c29d12003-09-22 23:38:23 +000028#if TRACE_LEVEL // ByteCodeReading_TRACEr
Chris Lattner52e20b02003-03-19 20:54:26 +000029#define BCR_TRACE(n, X) \
30 if (n < TRACE_LEVEL) std::cerr << std::string(n*2, ' ') << X
Chris Lattner1d670cc2001-09-07 16:37:43 +000031#else
32#define BCR_TRACE(n, X)
33#endif
34
Misha Brukman12c29d12003-09-22 23:38:23 +000035struct LazyFunctionInfo {
36 const unsigned char *Buf, *EndBuf;
37 unsigned FunctionSlot;
38};
39
Chris Lattner00413e32003-10-04 20:14:59 +000040class BytecodeParser : public ModuleProvider {
Chris Lattner74734132002-08-17 22:01:27 +000041 BytecodeParser(const BytecodeParser &); // DO NOT IMPLEMENT
42 void operator=(const BytecodeParser &); // DO NOT IMPLEMENT
Chris Lattner00950542001-06-06 20:29:01 +000043public:
Misha Brukman96f78772003-09-22 23:58:08 +000044 BytecodeParser() {
Chris Lattner00950542001-06-06 20:29:01 +000045 // Define this in case we don't see a ModuleGlobalInfo block.
46 FirstDerivedTyID = Type::FirstDerivedTyID;
47 }
Misha Brukman12c29d12003-09-22 23:38:23 +000048
Chris Lattner52e20b02003-03-19 20:54:26 +000049 ~BytecodeParser() {
Chris Lattnera2602f32003-05-22 18:26:48 +000050 freeState();
51 }
52 void freeState() {
Chris Lattner52e20b02003-03-19 20:54:26 +000053 freeTable(Values);
Chris Lattner52e20b02003-03-19 20:54:26 +000054 freeTable(ModuleValues);
55 }
Chris Lattner00950542001-06-06 20:29:01 +000056
Misha Brukman12c29d12003-09-22 23:38:23 +000057 Module* releaseModule() {
58 // Since we're losing control of this Module, we must hand it back complete
Chris Lattner927b1852003-10-09 20:22:47 +000059 Module *M = ModuleProvider::releaseModule();
Misha Brukman12c29d12003-09-22 23:38:23 +000060 freeState();
Chris Lattner927b1852003-10-09 20:22:47 +000061 return M;
Misha Brukman12c29d12003-09-22 23:38:23 +000062 }
Chris Lattnerd6b65252001-10-24 01:15:12 +000063
Misha Brukman12c29d12003-09-22 23:38:23 +000064 void ParseBytecode(const unsigned char *Buf, unsigned Length,
65 const std::string &ModuleID);
Chris Lattnerd6b65252001-10-24 01:15:12 +000066
Chris Lattnerb3afb1f2002-04-04 19:24:11 +000067 void dump() const {
Anand Shuklaeea60fc2002-06-25 20:44:04 +000068 std::cerr << "BytecodeParser instance!\n";
Chris Lattnerb3afb1f2002-04-04 19:24:11 +000069 }
70
Chris Lattner6e448022003-10-08 21:51:46 +000071private:
Chris Lattner52e20b02003-03-19 20:54:26 +000072 struct ValueList : public User {
Chris Lattner6e448022003-10-08 21:51:46 +000073 ValueList() : User(Type::TypeTy, Value::TypeVal) {}
Chris Lattner52e20b02003-03-19 20:54:26 +000074
75 // vector compatibility methods
76 unsigned size() const { return getNumOperands(); }
77 void push_back(Value *V) { Operands.push_back(Use(V, this)); }
78 Value *back() const { return Operands.back(); }
79 void pop_back() { Operands.pop_back(); }
80 bool empty() const { return Operands.empty(); }
81
82 virtual void print(std::ostream& OS) const {
83 OS << "Bytecode Reader UseHandle!";
84 }
85 };
86
Chris Lattner036b8aa2003-03-06 17:55:45 +000087 // Information about the module, extracted from the bytecode revision number.
88 unsigned char RevisionNum; // The rev # itself
89 unsigned char FirstDerivedTyID; // First variable index to use for type
Chris Lattnere3869c82003-04-16 21:16:05 +000090 bool hasInternalMarkerOnly; // Only types of linkage are intern/external
Chris Lattnercb7e2e22003-10-18 05:54:18 +000091 bool hasExtendedLinkageSpecs; // Supports more than 4 linkage types
92 bool hasOldStyleVarargs; // Has old version of varargs intrinsics?
93 bool hasVarArgCallPadding; // Bytecode has extra padding in vararg call
94
95 bool usesOldStyleVarargs; // Does this module USE old style varargs?
Chris Lattner036b8aa2003-03-06 17:55:45 +000096
Chris Lattner52e20b02003-03-19 20:54:26 +000097 typedef std::vector<ValueList*> ValueTable;
Chris Lattner8eb10ce2003-10-09 06:05:40 +000098 ValueTable Values;
Chris Lattner52e20b02003-03-19 20:54:26 +000099 ValueTable ModuleValues;
Chris Lattner8eb10ce2003-10-09 06:05:40 +0000100 std::map<std::pair<unsigned,unsigned>, Value*> ForwardReferences;
Chris Lattner1d670cc2001-09-07 16:37:43 +0000101
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000102 std::vector<BasicBlock*> ParsedBasicBlocks;
103
Chris Lattner74734132002-08-17 22:01:27 +0000104 // GlobalRefs - This maintains a mapping between <Type, Slot #>'s and forward
105 // references to global values or constants. Such values may be referenced
106 // before they are defined, and if so, the temporary object that they
107 // represent is held here.
108 //
Chris Lattner52e20b02003-03-19 20:54:26 +0000109 typedef std::map<std::pair<const Type *, unsigned>, Value*> GlobalRefsType;
Chris Lattner74734132002-08-17 22:01:27 +0000110 GlobalRefsType GlobalRefs;
Chris Lattner05950c32001-10-13 06:47:01 +0000111
Chris Lattner1d670cc2001-09-07 16:37:43 +0000112 // TypesLoaded - This vector mirrors the Values[TypeTyID] plane. It is used
113 // to deal with forward references to types.
114 //
Chris Lattnerc7b6f032003-10-02 20:26:18 +0000115 typedef std::vector<PATypeHolder> TypeValuesListTy;
Chris Lattner1d670cc2001-09-07 16:37:43 +0000116 TypeValuesListTy ModuleTypeValues;
Chris Lattner6e5a0e42003-03-06 17:18:14 +0000117 TypeValuesListTy FunctionTypeValues;
Chris Lattner00950542001-06-06 20:29:01 +0000118
Chris Lattner52e20b02003-03-19 20:54:26 +0000119 // When the ModuleGlobalInfo section is read, we create a function object for
120 // each function in the module. When the function is loaded, this function is
121 // filled in.
Chris Lattner00950542001-06-06 20:29:01 +0000122 //
Chris Lattner52e20b02003-03-19 20:54:26 +0000123 std::vector<std::pair<Function*, unsigned> > FunctionSignatureList;
124
125 // Constant values are read in after global variables. Because of this, we
126 // must defer setting the initializers on global variables until after module
127 // level constants have been read. In the mean time, this list keeps track of
128 // what we must do.
129 //
130 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
Chris Lattner00950542001-06-06 20:29:01 +0000131
Misha Brukman12c29d12003-09-22 23:38:23 +0000132 // For lazy reading-in of functions, we need to save away several pieces of
133 // information about each function: its begin and end pointer in the buffer
134 // and its FunctionSlot.
135 //
136 std::map<Function*, LazyFunctionInfo*> LazyFunctionLoadMap;
137
Chris Lattner00950542001-06-06 20:29:01 +0000138private:
Chris Lattner52e20b02003-03-19 20:54:26 +0000139 void freeTable(ValueTable &Tab) {
140 while (!Tab.empty()) {
141 delete Tab.back();
142 Tab.pop_back();
143 }
144 }
145
Misha Brukman12c29d12003-09-22 23:38:23 +0000146public:
147 void ParseModule(const unsigned char * Buf, const unsigned char *End);
148 void materializeFunction(Function *F);
149
150private:
151 void ParseVersionInfo (const unsigned char *&Buf, const unsigned char *End);
152 void ParseModuleGlobalInfo(const unsigned char *&Buf, const unsigned char *E);
153 void ParseSymbolTable(const unsigned char *&Buf, const unsigned char *End,
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000154 SymbolTable *, Function *CurrentFunction);
Misha Brukman12c29d12003-09-22 23:38:23 +0000155 void ParseFunction(const unsigned char *&Buf, const unsigned char *End);
156 void ParseGlobalTypes(const unsigned char *&Buf, const unsigned char *EndBuf);
157
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000158 BasicBlock *ParseBasicBlock(const unsigned char *&Buf,
159 const unsigned char *End,
160 unsigned BlockNo);
Misha Brukman12c29d12003-09-22 23:38:23 +0000161
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000162 void ParseInstruction(const unsigned char *&Buf, const unsigned char *End,
163 std::vector<unsigned> &Args, BasicBlock *BB);
Chris Lattner00950542001-06-06 20:29:01 +0000164
Misha Brukman12c29d12003-09-22 23:38:23 +0000165 void ParseConstantPool(const unsigned char *&Buf, const unsigned char *EndBuf,
166 ValueTable &Tab, TypeValuesListTy &TypeTab);
Chris Lattner9e460f22003-10-04 20:00:03 +0000167 Constant *parseConstantValue(const unsigned char *&Buf,
168 const unsigned char *End,
169 const Type *Ty);
Misha Brukman12c29d12003-09-22 23:38:23 +0000170 void parseTypeConstants(const unsigned char *&Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000171 const unsigned char *EndBuf,
Misha Brukman12c29d12003-09-22 23:38:23 +0000172 TypeValuesListTy &Tab, unsigned NumEntries);
Chris Lattner12e64652003-05-22 18:08:30 +0000173 const Type *parseTypeConstant(const unsigned char *&Buf,
174 const unsigned char *EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +0000175
176 Value *getValue(const Type *Ty, unsigned num, bool Create = true);
Chris Lattner36392bc2003-10-08 21:18:57 +0000177 Value *getValue(unsigned TypeID, unsigned num, bool Create = true);
Chris Lattner00950542001-06-06 20:29:01 +0000178 const Type *getType(unsigned ID);
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000179 BasicBlock *getBasicBlock(unsigned ID);
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000180 Constant *getConstantValue(const Type *Ty, unsigned num);
Chris Lattner00950542001-06-06 20:29:01 +0000181
Chris Lattner927b1852003-10-09 20:22:47 +0000182 unsigned insertValue(Value *V, ValueTable &Table);
Chris Lattnerf0d92732003-10-13 14:34:59 +0000183 unsigned insertValue(Value *V, unsigned Type, ValueTable &Table);
Chris Lattner00950542001-06-06 20:29:01 +0000184
Chris Lattner9e460f22003-10-04 20:00:03 +0000185 unsigned getTypeSlot(const Type *Ty);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000186
Chris Lattner74734132002-08-17 22:01:27 +0000187 // resolve all references to the placeholder (if any) for the given value
188 void ResolveReferencesToValue(Value *Val, unsigned Slot);
Chris Lattner00950542001-06-06 20:29:01 +0000189};
190
191template<class SuperType>
192class PlaceholderDef : public SuperType {
193 unsigned ID;
Chris Lattner74734132002-08-17 22:01:27 +0000194 PlaceholderDef(); // DO NOT IMPLEMENT
195 void operator=(const PlaceholderDef &); // DO NOT IMPLEMENT
Chris Lattner00950542001-06-06 20:29:01 +0000196public:
197 PlaceholderDef(const Type *Ty, unsigned id) : SuperType(Ty), ID(id) {}
198 unsigned getID() { return ID; }
199};
200
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000201struct ConstantPlaceHolderHelper : public Constant {
202 ConstantPlaceHolderHelper(const Type *Ty)
203 : Constant(Ty) {}
204 virtual bool isNullValue() const { return false; }
205};
206
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000207typedef PlaceholderDef<ConstantPlaceHolderHelper> ConstPHolder;
208
Misha Brukman12c29d12003-09-22 23:38:23 +0000209// Some common errors we find
210static const std::string Error_readvbr = "read_vbr(): error reading.";
211static const std::string Error_read = "read(): error reading.";
212static const std::string Error_inputdata = "input_data(): error reading.";
213static const std::string Error_DestSlot = "No destination slot found.";
Chris Lattner00950542001-06-06 20:29:01 +0000214
Misha Brukman12c29d12003-09-22 23:38:23 +0000215static inline void readBlock(const unsigned char *&Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000216 const unsigned char *EndBuf,
Misha Brukman12c29d12003-09-22 23:38:23 +0000217 unsigned &Type, unsigned &Size) {
Chris Lattner00950542001-06-06 20:29:01 +0000218#if DEBUG_OUTPUT
219 bool Result = read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size);
Anand Shuklaeea60fc2002-06-25 20:44:04 +0000220 std::cerr << "StartLoc = " << ((unsigned)Buf & 4095)
Chris Lattner00950542001-06-06 20:29:01 +0000221 << " Type = " << Type << " Size = " << Size << endl;
Misha Brukman12c29d12003-09-22 23:38:23 +0000222 if (Result) throw Error_read;
Chris Lattner00950542001-06-06 20:29:01 +0000223#else
Misha Brukman12c29d12003-09-22 23:38:23 +0000224 if (read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size)) throw Error_read;
Chris Lattner00950542001-06-06 20:29:01 +0000225#endif
226}
227
Chris Lattner00950542001-06-06 20:29:01 +0000228#endif