blob: c75c6557648310544f9c546029478815495e8407 [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 Lattner8dc6ba92003-11-14 06:38:46 +000017#include "llvm/Constants.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
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;
40 unsigned FunctionSlot;
41};
42
Chris Lattner00413e32003-10-04 20:14:59 +000043class BytecodeParser : public ModuleProvider {
Chris Lattner74734132002-08-17 22:01:27 +000044 BytecodeParser(const BytecodeParser &); // DO NOT IMPLEMENT
45 void operator=(const BytecodeParser &); // DO NOT IMPLEMENT
Chris Lattner00950542001-06-06 20:29:01 +000046public:
Misha Brukman96f78772003-09-22 23:58:08 +000047 BytecodeParser() {
Chris Lattner00950542001-06-06 20:29:01 +000048 // Define this in case we don't see a ModuleGlobalInfo block.
49 FirstDerivedTyID = Type::FirstDerivedTyID;
50 }
Misha Brukman12c29d12003-09-22 23:38:23 +000051
Chris Lattner52e20b02003-03-19 20:54:26 +000052 ~BytecodeParser() {
Chris Lattnera2602f32003-05-22 18:26:48 +000053 freeState();
54 }
55 void freeState() {
Chris Lattner52e20b02003-03-19 20:54:26 +000056 freeTable(Values);
Chris Lattner52e20b02003-03-19 20:54:26 +000057 freeTable(ModuleValues);
58 }
Chris Lattner00950542001-06-06 20:29:01 +000059
Misha Brukman12c29d12003-09-22 23:38:23 +000060 Module* releaseModule() {
61 // Since we're losing control of this Module, we must hand it back complete
Chris Lattner927b1852003-10-09 20:22:47 +000062 Module *M = ModuleProvider::releaseModule();
Misha Brukman12c29d12003-09-22 23:38:23 +000063 freeState();
Chris Lattner927b1852003-10-09 20:22:47 +000064 return M;
Misha Brukman12c29d12003-09-22 23:38:23 +000065 }
Chris Lattnerd6b65252001-10-24 01:15:12 +000066
Misha Brukman12c29d12003-09-22 23:38:23 +000067 void ParseBytecode(const unsigned char *Buf, unsigned Length,
68 const std::string &ModuleID);
Chris Lattnerd6b65252001-10-24 01:15:12 +000069
Chris Lattnerb3afb1f2002-04-04 19:24:11 +000070 void dump() const {
Anand Shuklaeea60fc2002-06-25 20:44:04 +000071 std::cerr << "BytecodeParser instance!\n";
Chris Lattnerb3afb1f2002-04-04 19:24:11 +000072 }
73
Chris Lattner6e448022003-10-08 21:51:46 +000074private:
Chris Lattner52e20b02003-03-19 20:54:26 +000075 struct ValueList : public User {
Chris Lattner6e448022003-10-08 21:51:46 +000076 ValueList() : User(Type::TypeTy, Value::TypeVal) {}
Chris Lattner52e20b02003-03-19 20:54:26 +000077
78 // vector compatibility methods
79 unsigned size() const { return getNumOperands(); }
80 void push_back(Value *V) { Operands.push_back(Use(V, this)); }
81 Value *back() const { return Operands.back(); }
82 void pop_back() { Operands.pop_back(); }
83 bool empty() const { return Operands.empty(); }
84
85 virtual void print(std::ostream& OS) const {
86 OS << "Bytecode Reader UseHandle!";
87 }
88 };
89
Chris Lattner036b8aa2003-03-06 17:55:45 +000090 // Information about the module, extracted from the bytecode revision number.
91 unsigned char RevisionNum; // The rev # itself
92 unsigned char FirstDerivedTyID; // First variable index to use for type
Chris Lattnere3869c82003-04-16 21:16:05 +000093 bool hasInternalMarkerOnly; // Only types of linkage are intern/external
Chris Lattnercb7e2e22003-10-18 05:54:18 +000094 bool hasExtendedLinkageSpecs; // Supports more than 4 linkage types
95 bool hasOldStyleVarargs; // Has old version of varargs intrinsics?
96 bool hasVarArgCallPadding; // Bytecode has extra padding in vararg call
97
98 bool usesOldStyleVarargs; // Does this module USE old style varargs?
Chris Lattner036b8aa2003-03-06 17:55:45 +000099
Chris Lattner52e20b02003-03-19 20:54:26 +0000100 typedef std::vector<ValueList*> ValueTable;
Chris Lattner8eb10ce2003-10-09 06:05:40 +0000101 ValueTable Values;
Chris Lattner52e20b02003-03-19 20:54:26 +0000102 ValueTable ModuleValues;
Chris Lattner8eb10ce2003-10-09 06:05:40 +0000103 std::map<std::pair<unsigned,unsigned>, Value*> ForwardReferences;
Chris Lattner1d670cc2001-09-07 16:37:43 +0000104
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000105 std::vector<BasicBlock*> ParsedBasicBlocks;
106
Chris Lattner74734132002-08-17 22:01:27 +0000107 // GlobalRefs - This maintains a mapping between <Type, Slot #>'s and forward
108 // references to global values or constants. Such values may be referenced
109 // before they are defined, and if so, the temporary object that they
110 // represent is held here.
111 //
Chris Lattner52e20b02003-03-19 20:54:26 +0000112 typedef std::map<std::pair<const Type *, unsigned>, Value*> GlobalRefsType;
Chris Lattner74734132002-08-17 22:01:27 +0000113 GlobalRefsType GlobalRefs;
Chris Lattner05950c32001-10-13 06:47:01 +0000114
Chris Lattner1d670cc2001-09-07 16:37:43 +0000115 // TypesLoaded - This vector mirrors the Values[TypeTyID] plane. It is used
116 // to deal with forward references to types.
117 //
Chris Lattnerc7b6f032003-10-02 20:26:18 +0000118 typedef std::vector<PATypeHolder> TypeValuesListTy;
Chris Lattner1d670cc2001-09-07 16:37:43 +0000119 TypeValuesListTy ModuleTypeValues;
Chris Lattner6e5a0e42003-03-06 17:18:14 +0000120 TypeValuesListTy FunctionTypeValues;
Chris Lattner00950542001-06-06 20:29:01 +0000121
Chris Lattner52e20b02003-03-19 20:54:26 +0000122 // When the ModuleGlobalInfo section is read, we create a function object for
123 // each function in the module. When the function is loaded, this function is
124 // filled in.
Chris Lattner00950542001-06-06 20:29:01 +0000125 //
Chris Lattner52e20b02003-03-19 20:54:26 +0000126 std::vector<std::pair<Function*, unsigned> > FunctionSignatureList;
127
128 // Constant values are read in after global variables. Because of this, we
129 // must defer setting the initializers on global variables until after module
130 // level constants have been read. In the mean time, this list keeps track of
131 // what we must do.
132 //
133 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
Chris Lattner00950542001-06-06 20:29:01 +0000134
Misha Brukman12c29d12003-09-22 23:38:23 +0000135 // For lazy reading-in of functions, we need to save away several pieces of
136 // information about each function: its begin and end pointer in the buffer
137 // and its FunctionSlot.
138 //
139 std::map<Function*, LazyFunctionInfo*> LazyFunctionLoadMap;
140
Chris Lattner00950542001-06-06 20:29:01 +0000141private:
Chris Lattner52e20b02003-03-19 20:54:26 +0000142 void freeTable(ValueTable &Tab) {
143 while (!Tab.empty()) {
144 delete Tab.back();
145 Tab.pop_back();
146 }
147 }
148
Misha Brukman12c29d12003-09-22 23:38:23 +0000149public:
150 void ParseModule(const unsigned char * Buf, const unsigned char *End);
151 void materializeFunction(Function *F);
152
153private:
154 void ParseVersionInfo (const unsigned char *&Buf, const unsigned char *End);
155 void ParseModuleGlobalInfo(const unsigned char *&Buf, const unsigned char *E);
156 void ParseSymbolTable(const unsigned char *&Buf, const unsigned char *End,
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000157 SymbolTable *, Function *CurrentFunction);
Misha Brukman12c29d12003-09-22 23:38:23 +0000158 void ParseFunction(const unsigned char *&Buf, const unsigned char *End);
159 void ParseGlobalTypes(const unsigned char *&Buf, const unsigned char *EndBuf);
160
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000161 BasicBlock *ParseBasicBlock(const unsigned char *&Buf,
162 const unsigned char *End,
163 unsigned BlockNo);
Misha Brukman12c29d12003-09-22 23:38:23 +0000164
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000165 void ParseInstruction(const unsigned char *&Buf, const unsigned char *End,
166 std::vector<unsigned> &Args, BasicBlock *BB);
Chris Lattner00950542001-06-06 20:29:01 +0000167
Misha Brukman12c29d12003-09-22 23:38:23 +0000168 void ParseConstantPool(const unsigned char *&Buf, const unsigned char *EndBuf,
169 ValueTable &Tab, TypeValuesListTy &TypeTab);
Chris Lattner9e460f22003-10-04 20:00:03 +0000170 Constant *parseConstantValue(const unsigned char *&Buf,
171 const unsigned char *End,
172 const Type *Ty);
Misha Brukman12c29d12003-09-22 23:38:23 +0000173 void parseTypeConstants(const unsigned char *&Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000174 const unsigned char *EndBuf,
Misha Brukman12c29d12003-09-22 23:38:23 +0000175 TypeValuesListTy &Tab, unsigned NumEntries);
Chris Lattner12e64652003-05-22 18:08:30 +0000176 const Type *parseTypeConstant(const unsigned char *&Buf,
177 const unsigned char *EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +0000178
179 Value *getValue(const Type *Ty, unsigned num, bool Create = true);
Chris Lattner36392bc2003-10-08 21:18:57 +0000180 Value *getValue(unsigned TypeID, unsigned num, bool Create = true);
Chris Lattner00950542001-06-06 20:29:01 +0000181 const Type *getType(unsigned ID);
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000182 BasicBlock *getBasicBlock(unsigned ID);
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000183 Constant *getConstantValue(const Type *Ty, unsigned num);
Chris Lattner00950542001-06-06 20:29:01 +0000184
Chris Lattner927b1852003-10-09 20:22:47 +0000185 unsigned insertValue(Value *V, ValueTable &Table);
Chris Lattnerf0d92732003-10-13 14:34:59 +0000186 unsigned insertValue(Value *V, unsigned Type, ValueTable &Table);
Chris Lattner00950542001-06-06 20:29:01 +0000187
Chris Lattner9e460f22003-10-04 20:00:03 +0000188 unsigned getTypeSlot(const Type *Ty);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000189
Chris Lattner74734132002-08-17 22:01:27 +0000190 // resolve all references to the placeholder (if any) for the given value
191 void ResolveReferencesToValue(Value *Val, unsigned Slot);
Chris Lattner00950542001-06-06 20:29:01 +0000192};
193
194template<class SuperType>
195class PlaceholderDef : public SuperType {
196 unsigned ID;
Chris Lattner74734132002-08-17 22:01:27 +0000197 PlaceholderDef(); // DO NOT IMPLEMENT
198 void operator=(const PlaceholderDef &); // DO NOT IMPLEMENT
Chris Lattner00950542001-06-06 20:29:01 +0000199public:
200 PlaceholderDef(const Type *Ty, unsigned id) : SuperType(Ty), ID(id) {}
201 unsigned getID() { return ID; }
202};
203
Chris Lattner8dc6ba92003-11-14 06:38:46 +0000204struct ConstantPlaceHolderHelper : public ConstantExpr {
205 ConstantPlaceHolderHelper(const Type *Ty) : ConstantExpr(Instruction::UserOp1, Constant::getNullValue(Ty), Ty) {}
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000206};
207
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000208typedef PlaceholderDef<ConstantPlaceHolderHelper> ConstPHolder;
209
Misha Brukman12c29d12003-09-22 23:38:23 +0000210// Some common errors we find
211static const std::string Error_readvbr = "read_vbr(): error reading.";
212static const std::string Error_read = "read(): error reading.";
213static const std::string Error_inputdata = "input_data(): error reading.";
214static const std::string Error_DestSlot = "No destination slot found.";
Chris Lattner00950542001-06-06 20:29:01 +0000215
Misha Brukman12c29d12003-09-22 23:38:23 +0000216static inline void readBlock(const unsigned char *&Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000217 const unsigned char *EndBuf,
Misha Brukman12c29d12003-09-22 23:38:23 +0000218 unsigned &Type, unsigned &Size) {
Alkis Evlogimenos60048b82003-10-30 18:33:58 +0000219#ifdef DEBUG_OUTPUT
Chris Lattner00950542001-06-06 20:29:01 +0000220 bool Result = read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size);
Anand Shuklaeea60fc2002-06-25 20:44:04 +0000221 std::cerr << "StartLoc = " << ((unsigned)Buf & 4095)
Chris Lattner095be962003-11-05 19:53:32 +0000222 << " Type = " << Type << " Size = " << Size << "\n";
Misha Brukman12c29d12003-09-22 23:38:23 +0000223 if (Result) throw Error_read;
Chris Lattner00950542001-06-06 20:29:01 +0000224#else
Misha Brukman12c29d12003-09-22 23:38:23 +0000225 if (read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size)) throw Error_read;
Chris Lattner00950542001-06-06 20:29:01 +0000226#endif
227}
228
Brian Gaeked0fde302003-11-11 22:41:34 +0000229} // End llvm namespace
230
Chris Lattner00950542001-06-06 20:29:01 +0000231#endif