blob: d6a934da077b687bddce54c7d8bfd9f9bd06c1a5 [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:
Misha Brukman96f78772003-09-22 23:58:08 +000048 BytecodeParser() {
Chris Lattner00950542001-06-06 20:29:01 +000049 // Define this in case we don't see a ModuleGlobalInfo block.
50 FirstDerivedTyID = Type::FirstDerivedTyID;
51 }
Misha Brukman12c29d12003-09-22 23:38:23 +000052
Chris Lattner52e20b02003-03-19 20:54:26 +000053 ~BytecodeParser() {
Chris Lattnera2602f32003-05-22 18:26:48 +000054 freeState();
55 }
56 void freeState() {
Chris Lattner52e20b02003-03-19 20:54:26 +000057 freeTable(Values);
Chris Lattner52e20b02003-03-19 20:54:26 +000058 freeTable(ModuleValues);
59 }
Chris Lattner00950542001-06-06 20:29:01 +000060
Misha Brukman12c29d12003-09-22 23:38:23 +000061 Module* releaseModule() {
62 // Since we're losing control of this Module, we must hand it back complete
Chris Lattner927b1852003-10-09 20:22:47 +000063 Module *M = ModuleProvider::releaseModule();
Misha Brukman12c29d12003-09-22 23:38:23 +000064 freeState();
Chris Lattner927b1852003-10-09 20:22:47 +000065 return M;
Misha Brukman12c29d12003-09-22 23:38:23 +000066 }
Chris Lattnerd6b65252001-10-24 01:15:12 +000067
Misha Brukman12c29d12003-09-22 23:38:23 +000068 void ParseBytecode(const unsigned char *Buf, unsigned Length,
69 const std::string &ModuleID);
Chris Lattnerd6b65252001-10-24 01:15:12 +000070
Chris Lattnerb3afb1f2002-04-04 19:24:11 +000071 void dump() const {
Anand Shuklaeea60fc2002-06-25 20:44:04 +000072 std::cerr << "BytecodeParser instance!\n";
Chris Lattnerb3afb1f2002-04-04 19:24:11 +000073 }
74
Chris Lattner6e448022003-10-08 21:51:46 +000075private:
Chris Lattner52e20b02003-03-19 20:54:26 +000076 struct ValueList : public User {
Chris Lattner6e448022003-10-08 21:51:46 +000077 ValueList() : User(Type::TypeTy, Value::TypeVal) {}
Chris Lattner52e20b02003-03-19 20:54:26 +000078
79 // vector compatibility methods
80 unsigned size() const { return getNumOperands(); }
81 void push_back(Value *V) { Operands.push_back(Use(V, this)); }
82 Value *back() const { return Operands.back(); }
83 void pop_back() { Operands.pop_back(); }
84 bool empty() const { return Operands.empty(); }
85
86 virtual void print(std::ostream& OS) const {
87 OS << "Bytecode Reader UseHandle!";
88 }
89 };
90
Chris Lattner036b8aa2003-03-06 17:55:45 +000091 // Information about the module, extracted from the bytecode revision number.
92 unsigned char RevisionNum; // The rev # itself
93 unsigned char FirstDerivedTyID; // First variable index to use for type
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 Lattner44d0eeb2004-01-15 17:55:01 +0000100 // LLVM 1.0 & 1.1 had an explicit alignment of data only for the
101 // ModuleGlobalInfo block. This was fixed to be like all other blocks in 1.2
102 bool hasInconsistentModuleGlobalInfo;
103
Chris Lattner52e20b02003-03-19 20:54:26 +0000104 typedef std::vector<ValueList*> ValueTable;
Chris Lattner8eb10ce2003-10-09 06:05:40 +0000105 ValueTable Values;
Chris Lattner52e20b02003-03-19 20:54:26 +0000106 ValueTable ModuleValues;
Chris Lattner8eb10ce2003-10-09 06:05:40 +0000107 std::map<std::pair<unsigned,unsigned>, Value*> ForwardReferences;
Chris Lattner1d670cc2001-09-07 16:37:43 +0000108
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000109 std::vector<BasicBlock*> ParsedBasicBlocks;
110
Chris Lattner29b789b2003-11-19 17:27:18 +0000111 // ConstantFwdRefs - This maintains a mapping between <Type, Slot #>'s and
112 // forward references to constants. Such values may be referenced before they
113 // are defined, and if so, the temporary object that they represent is held
114 // here.
Chris Lattner74734132002-08-17 22:01:27 +0000115 //
Chris Lattner29b789b2003-11-19 17:27:18 +0000116 typedef std::map<std::pair<const Type*,unsigned>, Constant*> ConstantRefsType;
117 ConstantRefsType ConstantFwdRefs;
Chris Lattner05950c32001-10-13 06:47:01 +0000118
Chris Lattner1d670cc2001-09-07 16:37:43 +0000119 // TypesLoaded - This vector mirrors the Values[TypeTyID] plane. It is used
120 // to deal with forward references to types.
121 //
Chris Lattnerc7b6f032003-10-02 20:26:18 +0000122 typedef std::vector<PATypeHolder> TypeValuesListTy;
Chris Lattner1d670cc2001-09-07 16:37:43 +0000123 TypeValuesListTy ModuleTypeValues;
Chris Lattner6e5a0e42003-03-06 17:18:14 +0000124 TypeValuesListTy FunctionTypeValues;
Chris Lattner00950542001-06-06 20:29:01 +0000125
Chris Lattner52e20b02003-03-19 20:54:26 +0000126 // When the ModuleGlobalInfo section is read, we create a function object for
127 // each function in the module. When the function is loaded, this function is
128 // filled in.
Chris Lattner00950542001-06-06 20:29:01 +0000129 //
Chris Lattner29b789b2003-11-19 17:27:18 +0000130 std::vector<Function*> FunctionSignatureList;
Chris Lattner52e20b02003-03-19 20:54:26 +0000131
132 // Constant values are read in after global variables. Because of this, we
133 // must defer setting the initializers on global variables until after module
134 // level constants have been read. In the mean time, this list keeps track of
135 // what we must do.
136 //
137 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
Chris Lattner00950542001-06-06 20:29:01 +0000138
Misha Brukman12c29d12003-09-22 23:38:23 +0000139 // For lazy reading-in of functions, we need to save away several pieces of
140 // information about each function: its begin and end pointer in the buffer
141 // and its FunctionSlot.
142 //
Chris Lattner29b789b2003-11-19 17:27:18 +0000143 std::map<Function*, LazyFunctionInfo> LazyFunctionLoadMap;
Misha Brukman12c29d12003-09-22 23:38:23 +0000144
Chris Lattner00950542001-06-06 20:29:01 +0000145private:
Chris Lattner52e20b02003-03-19 20:54:26 +0000146 void freeTable(ValueTable &Tab) {
147 while (!Tab.empty()) {
148 delete Tab.back();
149 Tab.pop_back();
150 }
151 }
152
Misha Brukman12c29d12003-09-22 23:38:23 +0000153public:
154 void ParseModule(const unsigned char * Buf, const unsigned char *End);
155 void materializeFunction(Function *F);
156
157private:
158 void ParseVersionInfo (const unsigned char *&Buf, const unsigned char *End);
159 void ParseModuleGlobalInfo(const unsigned char *&Buf, const unsigned char *E);
160 void ParseSymbolTable(const unsigned char *&Buf, const unsigned char *End,
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000161 SymbolTable *, Function *CurrentFunction);
Misha Brukman12c29d12003-09-22 23:38:23 +0000162 void ParseFunction(const unsigned char *&Buf, const unsigned char *End);
163 void ParseGlobalTypes(const unsigned char *&Buf, const unsigned char *EndBuf);
164
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000165 BasicBlock *ParseBasicBlock(const unsigned char *&Buf,
166 const unsigned char *End,
167 unsigned BlockNo);
Chris Lattner8d1dbd22003-12-01 07:05:31 +0000168 unsigned ParseInstructionList(Function *F, const unsigned char *&Buf,
169 const unsigned char *EndBuf);
170
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000171 void ParseInstruction(const unsigned char *&Buf, const unsigned char *End,
172 std::vector<unsigned> &Args, BasicBlock *BB);
Chris Lattner00950542001-06-06 20:29:01 +0000173
Misha Brukman12c29d12003-09-22 23:38:23 +0000174 void ParseConstantPool(const unsigned char *&Buf, const unsigned char *EndBuf,
175 ValueTable &Tab, TypeValuesListTy &TypeTab);
Chris Lattner9e460f22003-10-04 20:00:03 +0000176 Constant *parseConstantValue(const unsigned char *&Buf,
177 const unsigned char *End,
Chris Lattner29b789b2003-11-19 17:27:18 +0000178 unsigned TypeID);
Misha Brukman12c29d12003-09-22 23:38:23 +0000179 void parseTypeConstants(const unsigned char *&Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000180 const unsigned char *EndBuf,
Misha Brukman12c29d12003-09-22 23:38:23 +0000181 TypeValuesListTy &Tab, unsigned NumEntries);
Chris Lattner12e64652003-05-22 18:08:30 +0000182 const Type *parseTypeConstant(const unsigned char *&Buf,
183 const unsigned char *EndBuf);
Chris Lattner9e893e82004-01-14 23:35:21 +0000184 void parseStringConstants(const unsigned char *&Buf,
185 const unsigned char *EndBuf,
186 unsigned NumEntries, ValueTable &Tab);
Chris Lattner00950542001-06-06 20:29:01 +0000187
Chris Lattner36392bc2003-10-08 21:18:57 +0000188 Value *getValue(unsigned TypeID, unsigned num, bool Create = true);
Chris Lattner00950542001-06-06 20:29:01 +0000189 const Type *getType(unsigned ID);
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000190 BasicBlock *getBasicBlock(unsigned ID);
Chris Lattner1c3673b2003-11-19 06:01:12 +0000191 Constant *getConstantValue(unsigned TypeID, unsigned num);
192 Constant *getConstantValue(const Type *Ty, unsigned num) {
193 return getConstantValue(getTypeSlot(Ty), num);
194 }
Chris Lattner00950542001-06-06 20:29:01 +0000195
Chris Lattnerf0d92732003-10-13 14:34:59 +0000196 unsigned insertValue(Value *V, unsigned Type, ValueTable &Table);
Chris Lattner00950542001-06-06 20:29:01 +0000197
Chris Lattner9e460f22003-10-04 20:00:03 +0000198 unsigned getTypeSlot(const Type *Ty);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000199
Chris Lattner29b789b2003-11-19 17:27:18 +0000200 // resolve all references to the placeholder (if any) for the given constant
201 void ResolveReferencesToConstant(Constant *C, unsigned Slot);
Chris Lattner00950542001-06-06 20:29:01 +0000202};
203
204template<class SuperType>
205class PlaceholderDef : public SuperType {
206 unsigned ID;
Chris Lattner74734132002-08-17 22:01:27 +0000207 PlaceholderDef(); // DO NOT IMPLEMENT
208 void operator=(const PlaceholderDef &); // DO NOT IMPLEMENT
Chris Lattner00950542001-06-06 20:29:01 +0000209public:
210 PlaceholderDef(const Type *Ty, unsigned id) : SuperType(Ty), ID(id) {}
211 unsigned getID() { return ID; }
212};
213
Chris Lattner8dc6ba92003-11-14 06:38:46 +0000214struct ConstantPlaceHolderHelper : public ConstantExpr {
Chris Lattnerdb9546e2003-11-14 16:34:25 +0000215 ConstantPlaceHolderHelper(const Type *Ty)
216 : ConstantExpr(Instruction::UserOp1, Constant::getNullValue(Ty), Ty) {}
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000217};
218
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000219typedef PlaceholderDef<ConstantPlaceHolderHelper> ConstPHolder;
220
Misha Brukman12c29d12003-09-22 23:38:23 +0000221static inline void readBlock(const unsigned char *&Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000222 const unsigned char *EndBuf,
Misha Brukman12c29d12003-09-22 23:38:23 +0000223 unsigned &Type, unsigned &Size) {
Chris Lattner7969dc22004-01-15 06:13:09 +0000224 Type = read(Buf, EndBuf);
225 Size = read(Buf, EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +0000226}
227
Brian Gaeked0fde302003-11-11 22:41:34 +0000228} // End llvm namespace
229
Chris Lattner00950542001-06-06 20:29:01 +0000230#endif