blob: 79c5a0d9a63d630e84866ef340964875c8b9e3d5 [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
Alkis Evlogimenos60048b82003-10-30 18:33:58 +000027//#define DEBUG_OUTPUT
Chris Lattner1d670cc2001-09-07 16:37:43 +000028
Misha Brukman12c29d12003-09-22 23:38:23 +000029#if TRACE_LEVEL // ByteCodeReading_TRACEr
Chris Lattner52e20b02003-03-19 20:54:26 +000030#define BCR_TRACE(n, X) \
31 if (n < TRACE_LEVEL) std::cerr << std::string(n*2, ' ') << X
Chris Lattner1d670cc2001-09-07 16:37:43 +000032#else
33#define BCR_TRACE(n, X)
34#endif
35
Misha Brukman12c29d12003-09-22 23:38:23 +000036struct LazyFunctionInfo {
37 const unsigned char *Buf, *EndBuf;
38 unsigned FunctionSlot;
39};
40
Chris Lattner00413e32003-10-04 20:14:59 +000041class BytecodeParser : public ModuleProvider {
Chris Lattner74734132002-08-17 22:01:27 +000042 BytecodeParser(const BytecodeParser &); // DO NOT IMPLEMENT
43 void operator=(const BytecodeParser &); // DO NOT IMPLEMENT
Chris Lattner00950542001-06-06 20:29:01 +000044public:
Misha Brukman96f78772003-09-22 23:58:08 +000045 BytecodeParser() {
Chris Lattner00950542001-06-06 20:29:01 +000046 // Define this in case we don't see a ModuleGlobalInfo block.
47 FirstDerivedTyID = Type::FirstDerivedTyID;
48 }
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 Brukman12c29d12003-09-22 23:38:23 +000058 Module* releaseModule() {
59 // Since we're losing control of this Module, we must hand it back complete
Chris Lattner927b1852003-10-09 20:22:47 +000060 Module *M = ModuleProvider::releaseModule();
Misha Brukman12c29d12003-09-22 23:38:23 +000061 freeState();
Chris Lattner927b1852003-10-09 20:22:47 +000062 return M;
Misha Brukman12c29d12003-09-22 23:38:23 +000063 }
Chris Lattnerd6b65252001-10-24 01:15:12 +000064
Misha Brukman12c29d12003-09-22 23:38:23 +000065 void ParseBytecode(const unsigned char *Buf, unsigned Length,
66 const std::string &ModuleID);
Chris Lattnerd6b65252001-10-24 01:15:12 +000067
Chris Lattnerb3afb1f2002-04-04 19:24:11 +000068 void dump() const {
Anand Shuklaeea60fc2002-06-25 20:44:04 +000069 std::cerr << "BytecodeParser instance!\n";
Chris Lattnerb3afb1f2002-04-04 19:24:11 +000070 }
71
Chris Lattner6e448022003-10-08 21:51:46 +000072private:
Chris Lattner52e20b02003-03-19 20:54:26 +000073 struct ValueList : public User {
Chris Lattner6e448022003-10-08 21:51:46 +000074 ValueList() : User(Type::TypeTy, Value::TypeVal) {}
Chris Lattner52e20b02003-03-19 20:54:26 +000075
76 // vector compatibility methods
77 unsigned size() const { return getNumOperands(); }
78 void push_back(Value *V) { Operands.push_back(Use(V, this)); }
79 Value *back() const { return Operands.back(); }
80 void pop_back() { Operands.pop_back(); }
81 bool empty() const { return Operands.empty(); }
82
83 virtual void print(std::ostream& OS) const {
84 OS << "Bytecode Reader UseHandle!";
85 }
86 };
87
Chris Lattner036b8aa2003-03-06 17:55:45 +000088 // Information about the module, extracted from the bytecode revision number.
89 unsigned char RevisionNum; // The rev # itself
90 unsigned char FirstDerivedTyID; // First variable index to use for type
Chris Lattnere3869c82003-04-16 21:16:05 +000091 bool hasInternalMarkerOnly; // Only types of linkage are intern/external
Chris Lattnercb7e2e22003-10-18 05:54:18 +000092 bool hasExtendedLinkageSpecs; // Supports more than 4 linkage types
93 bool hasOldStyleVarargs; // Has old version of varargs intrinsics?
94 bool hasVarArgCallPadding; // Bytecode has extra padding in vararg call
95
96 bool usesOldStyleVarargs; // Does this module USE old style varargs?
Chris Lattner036b8aa2003-03-06 17:55:45 +000097
Chris Lattner52e20b02003-03-19 20:54:26 +000098 typedef std::vector<ValueList*> ValueTable;
Chris Lattner8eb10ce2003-10-09 06:05:40 +000099 ValueTable Values;
Chris Lattner52e20b02003-03-19 20:54:26 +0000100 ValueTable ModuleValues;
Chris Lattner8eb10ce2003-10-09 06:05:40 +0000101 std::map<std::pair<unsigned,unsigned>, Value*> ForwardReferences;
Chris Lattner1d670cc2001-09-07 16:37:43 +0000102
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000103 std::vector<BasicBlock*> ParsedBasicBlocks;
104
Chris Lattner74734132002-08-17 22:01:27 +0000105 // GlobalRefs - This maintains a mapping between <Type, Slot #>'s and forward
106 // references to global values or constants. Such values may be referenced
107 // before they are defined, and if so, the temporary object that they
108 // represent is held here.
109 //
Chris Lattner52e20b02003-03-19 20:54:26 +0000110 typedef std::map<std::pair<const Type *, unsigned>, Value*> GlobalRefsType;
Chris Lattner74734132002-08-17 22:01:27 +0000111 GlobalRefsType GlobalRefs;
Chris Lattner05950c32001-10-13 06:47:01 +0000112
Chris Lattner1d670cc2001-09-07 16:37:43 +0000113 // TypesLoaded - This vector mirrors the Values[TypeTyID] plane. It is used
114 // to deal with forward references to types.
115 //
Chris Lattnerc7b6f032003-10-02 20:26:18 +0000116 typedef std::vector<PATypeHolder> TypeValuesListTy;
Chris Lattner1d670cc2001-09-07 16:37:43 +0000117 TypeValuesListTy ModuleTypeValues;
Chris Lattner6e5a0e42003-03-06 17:18:14 +0000118 TypeValuesListTy FunctionTypeValues;
Chris Lattner00950542001-06-06 20:29:01 +0000119
Chris Lattner52e20b02003-03-19 20:54:26 +0000120 // When the ModuleGlobalInfo section is read, we create a function object for
121 // each function in the module. When the function is loaded, this function is
122 // filled in.
Chris Lattner00950542001-06-06 20:29:01 +0000123 //
Chris Lattner52e20b02003-03-19 20:54:26 +0000124 std::vector<std::pair<Function*, unsigned> > FunctionSignatureList;
125
126 // Constant values are read in after global variables. Because of this, we
127 // must defer setting the initializers on global variables until after module
128 // level constants have been read. In the mean time, this list keeps track of
129 // what we must do.
130 //
131 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
Chris Lattner00950542001-06-06 20:29:01 +0000132
Misha Brukman12c29d12003-09-22 23:38:23 +0000133 // For lazy reading-in of functions, we need to save away several pieces of
134 // information about each function: its begin and end pointer in the buffer
135 // and its FunctionSlot.
136 //
137 std::map<Function*, LazyFunctionInfo*> LazyFunctionLoadMap;
138
Chris Lattner00950542001-06-06 20:29:01 +0000139private:
Chris Lattner52e20b02003-03-19 20:54:26 +0000140 void freeTable(ValueTable &Tab) {
141 while (!Tab.empty()) {
142 delete Tab.back();
143 Tab.pop_back();
144 }
145 }
146
Misha Brukman12c29d12003-09-22 23:38:23 +0000147public:
148 void ParseModule(const unsigned char * Buf, const unsigned char *End);
149 void materializeFunction(Function *F);
150
151private:
152 void ParseVersionInfo (const unsigned char *&Buf, const unsigned char *End);
153 void ParseModuleGlobalInfo(const unsigned char *&Buf, const unsigned char *E);
154 void ParseSymbolTable(const unsigned char *&Buf, const unsigned char *End,
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000155 SymbolTable *, Function *CurrentFunction);
Misha Brukman12c29d12003-09-22 23:38:23 +0000156 void ParseFunction(const unsigned char *&Buf, const unsigned char *End);
157 void ParseGlobalTypes(const unsigned char *&Buf, const unsigned char *EndBuf);
158
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000159 BasicBlock *ParseBasicBlock(const unsigned char *&Buf,
160 const unsigned char *End,
161 unsigned BlockNo);
Misha Brukman12c29d12003-09-22 23:38:23 +0000162
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000163 void ParseInstruction(const unsigned char *&Buf, const unsigned char *End,
164 std::vector<unsigned> &Args, BasicBlock *BB);
Chris Lattner00950542001-06-06 20:29:01 +0000165
Misha Brukman12c29d12003-09-22 23:38:23 +0000166 void ParseConstantPool(const unsigned char *&Buf, const unsigned char *EndBuf,
167 ValueTable &Tab, TypeValuesListTy &TypeTab);
Chris Lattner9e460f22003-10-04 20:00:03 +0000168 Constant *parseConstantValue(const unsigned char *&Buf,
169 const unsigned char *End,
170 const Type *Ty);
Misha Brukman12c29d12003-09-22 23:38:23 +0000171 void parseTypeConstants(const unsigned char *&Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000172 const unsigned char *EndBuf,
Misha Brukman12c29d12003-09-22 23:38:23 +0000173 TypeValuesListTy &Tab, unsigned NumEntries);
Chris Lattner12e64652003-05-22 18:08:30 +0000174 const Type *parseTypeConstant(const unsigned char *&Buf,
175 const unsigned char *EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +0000176
177 Value *getValue(const Type *Ty, unsigned num, bool Create = true);
Chris Lattner36392bc2003-10-08 21:18:57 +0000178 Value *getValue(unsigned TypeID, unsigned num, bool Create = true);
Chris Lattner00950542001-06-06 20:29:01 +0000179 const Type *getType(unsigned ID);
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000180 BasicBlock *getBasicBlock(unsigned ID);
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000181 Constant *getConstantValue(const Type *Ty, unsigned num);
Chris Lattner00950542001-06-06 20:29:01 +0000182
Chris Lattner927b1852003-10-09 20:22:47 +0000183 unsigned insertValue(Value *V, ValueTable &Table);
Chris Lattnerf0d92732003-10-13 14:34:59 +0000184 unsigned insertValue(Value *V, unsigned Type, ValueTable &Table);
Chris Lattner00950542001-06-06 20:29:01 +0000185
Chris Lattner9e460f22003-10-04 20:00:03 +0000186 unsigned getTypeSlot(const Type *Ty);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000187
Chris Lattner74734132002-08-17 22:01:27 +0000188 // resolve all references to the placeholder (if any) for the given value
189 void ResolveReferencesToValue(Value *Val, unsigned Slot);
Chris Lattner00950542001-06-06 20:29:01 +0000190};
191
192template<class SuperType>
193class PlaceholderDef : public SuperType {
194 unsigned ID;
Chris Lattner74734132002-08-17 22:01:27 +0000195 PlaceholderDef(); // DO NOT IMPLEMENT
196 void operator=(const PlaceholderDef &); // DO NOT IMPLEMENT
Chris Lattner00950542001-06-06 20:29:01 +0000197public:
198 PlaceholderDef(const Type *Ty, unsigned id) : SuperType(Ty), ID(id) {}
199 unsigned getID() { return ID; }
200};
201
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000202struct ConstantPlaceHolderHelper : public Constant {
203 ConstantPlaceHolderHelper(const Type *Ty)
204 : Constant(Ty) {}
205 virtual bool isNullValue() const { return false; }
206};
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)
Alkis Evlogimenos60048b82003-10-30 18:33:58 +0000222 << " Type = " << Type << " Size = " << Size << std::endl;
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
Chris Lattner00950542001-06-06 20:29:01 +0000229#endif