2003-10-13 04:22:07 +00:00
|
|
|
//===-- ReaderInternals.h - Definitions internal to the reader --*- C++ -*-===//
|
2003-10-21 15:17:13 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-06 20:29:01 +00:00
|
|
|
//
|
|
|
|
// This header file defines various stuff that is used by the bytecode reader.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef READER_INTERNALS_H
|
|
|
|
#define READER_INTERNALS_H
|
|
|
|
|
2004-01-10 19:00:15 +00:00
|
|
|
#include "ReaderPrimitives.h"
|
2003-11-14 06:38:46 +00:00
|
|
|
#include "llvm/Constants.h"
|
2001-12-03 18:02:31 +00:00
|
|
|
#include "llvm/DerivedTypes.h"
|
2002-08-17 22:01:27 +00:00
|
|
|
#include "llvm/Function.h"
|
2003-09-22 23:38:23 +00:00
|
|
|
#include "llvm/ModuleProvider.h"
|
2001-06-06 20:29:01 +00:00
|
|
|
#include <utility>
|
2002-08-17 22:01:27 +00:00
|
|
|
#include <map>
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
namespace llvm {
|
|
|
|
|
2001-09-07 16:37:43 +00:00
|
|
|
// Enable to trace to figure out what the heck is going on when parsing fails
|
2003-09-04 23:47:07 +00:00
|
|
|
//#define TRACE_LEVEL 10
|
2003-10-30 18:33:58 +00:00
|
|
|
//#define DEBUG_OUTPUT
|
2001-09-07 16:37:43 +00:00
|
|
|
|
2003-09-22 23:38:23 +00:00
|
|
|
#if TRACE_LEVEL // ByteCodeReading_TRACEr
|
2003-03-19 20:54:26 +00:00
|
|
|
#define BCR_TRACE(n, X) \
|
|
|
|
if (n < TRACE_LEVEL) std::cerr << std::string(n*2, ' ') << X
|
2001-09-07 16:37:43 +00:00
|
|
|
#else
|
|
|
|
#define BCR_TRACE(n, X)
|
|
|
|
#endif
|
|
|
|
|
2003-09-22 23:38:23 +00:00
|
|
|
struct LazyFunctionInfo {
|
|
|
|
const unsigned char *Buf, *EndBuf;
|
2003-11-19 17:27:18 +00:00
|
|
|
LazyFunctionInfo(const unsigned char *B = 0, const unsigned char *EB = 0)
|
|
|
|
: Buf(B), EndBuf(EB) {}
|
2003-09-22 23:38:23 +00:00
|
|
|
};
|
|
|
|
|
2003-10-04 20:14:59 +00:00
|
|
|
class BytecodeParser : public ModuleProvider {
|
2002-08-17 22:01:27 +00:00
|
|
|
BytecodeParser(const BytecodeParser &); // DO NOT IMPLEMENT
|
|
|
|
void operator=(const BytecodeParser &); // DO NOT IMPLEMENT
|
2001-06-06 20:29:01 +00:00
|
|
|
public:
|
2004-01-18 21:08:15 +00:00
|
|
|
BytecodeParser() {}
|
2003-09-22 23:38:23 +00:00
|
|
|
|
2003-03-19 20:54:26 +00:00
|
|
|
~BytecodeParser() {
|
2003-05-22 18:26:48 +00:00
|
|
|
freeState();
|
|
|
|
}
|
|
|
|
void freeState() {
|
2003-03-19 20:54:26 +00:00
|
|
|
freeTable(Values);
|
|
|
|
freeTable(ModuleValues);
|
|
|
|
}
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2004-01-21 22:55:34 +00:00
|
|
|
Module* materializeModule() {
|
|
|
|
while (! LazyFunctionLoadMap.empty()) {
|
|
|
|
std::map<Function*, LazyFunctionInfo>::iterator i =
|
|
|
|
LazyFunctionLoadMap.begin();
|
|
|
|
materializeFunction((*i).first);
|
|
|
|
}
|
|
|
|
|
|
|
|
return TheModule;
|
|
|
|
}
|
|
|
|
|
2003-09-22 23:38:23 +00:00
|
|
|
Module* releaseModule() {
|
|
|
|
// Since we're losing control of this Module, we must hand it back complete
|
2003-10-09 20:22:47 +00:00
|
|
|
Module *M = ModuleProvider::releaseModule();
|
2003-09-22 23:38:23 +00:00
|
|
|
freeState();
|
2003-10-09 20:22:47 +00:00
|
|
|
return M;
|
2003-09-22 23:38:23 +00:00
|
|
|
}
|
2001-10-24 01:15:12 +00:00
|
|
|
|
2003-09-22 23:38:23 +00:00
|
|
|
void ParseBytecode(const unsigned char *Buf, unsigned Length,
|
|
|
|
const std::string &ModuleID);
|
2001-10-24 01:15:12 +00:00
|
|
|
|
2002-04-04 19:24:11 +00:00
|
|
|
void dump() const {
|
2002-06-25 20:44:04 +00:00
|
|
|
std::cerr << "BytecodeParser instance!\n";
|
2002-04-04 19:24:11 +00:00
|
|
|
}
|
|
|
|
|
2003-10-08 21:51:46 +00:00
|
|
|
private:
|
2003-03-19 20:54:26 +00:00
|
|
|
struct ValueList : public User {
|
2003-10-08 21:51:46 +00:00
|
|
|
ValueList() : User(Type::TypeTy, Value::TypeVal) {}
|
2003-03-19 20:54:26 +00:00
|
|
|
|
|
|
|
// vector compatibility methods
|
|
|
|
unsigned size() const { return getNumOperands(); }
|
|
|
|
void push_back(Value *V) { Operands.push_back(Use(V, this)); }
|
|
|
|
Value *back() const { return Operands.back(); }
|
|
|
|
void pop_back() { Operands.pop_back(); }
|
|
|
|
bool empty() const { return Operands.empty(); }
|
|
|
|
|
|
|
|
virtual void print(std::ostream& OS) const {
|
|
|
|
OS << "Bytecode Reader UseHandle!";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2003-03-06 17:55:45 +00:00
|
|
|
// Information about the module, extracted from the bytecode revision number.
|
|
|
|
unsigned char RevisionNum; // The rev # itself
|
2003-10-18 05:54:18 +00:00
|
|
|
bool hasExtendedLinkageSpecs; // Supports more than 4 linkage types
|
|
|
|
bool hasOldStyleVarargs; // Has old version of varargs intrinsics?
|
|
|
|
bool hasVarArgCallPadding; // Bytecode has extra padding in vararg call
|
|
|
|
|
|
|
|
bool usesOldStyleVarargs; // Does this module USE old style varargs?
|
2003-03-06 17:55:45 +00:00
|
|
|
|
2004-01-17 23:25:43 +00:00
|
|
|
// Flags to distinguish LLVM 1.0 & 1.1 bytecode formats (revision #0)
|
|
|
|
|
|
|
|
// Revision #0 had an explicit alignment of data only for the ModuleGlobalInfo
|
|
|
|
// block. This was fixed to be like all other blocks in 1.2
|
2004-01-15 17:55:01 +00:00
|
|
|
bool hasInconsistentModuleGlobalInfo;
|
|
|
|
|
2004-01-17 23:25:43 +00:00
|
|
|
// Revision #0 also explicitly encoded zero values for primitive types like
|
|
|
|
// int/sbyte/etc.
|
|
|
|
bool hasExplicitPrimitiveZeros;
|
|
|
|
|
2003-03-19 20:54:26 +00:00
|
|
|
typedef std::vector<ValueList*> ValueTable;
|
2003-10-09 06:05:40 +00:00
|
|
|
ValueTable Values;
|
2003-03-19 20:54:26 +00:00
|
|
|
ValueTable ModuleValues;
|
2003-10-09 06:05:40 +00:00
|
|
|
std::map<std::pair<unsigned,unsigned>, Value*> ForwardReferences;
|
2001-09-07 16:37:43 +00:00
|
|
|
|
2004-01-18 21:08:15 +00:00
|
|
|
/// CompactionTable - If a compaction table is active in the current function,
|
|
|
|
/// this is the mapping that it contains.
|
|
|
|
std::vector<std::vector<Value*> > CompactionTable;
|
|
|
|
|
2003-10-08 22:52:54 +00:00
|
|
|
std::vector<BasicBlock*> ParsedBasicBlocks;
|
|
|
|
|
2003-11-19 17:27:18 +00:00
|
|
|
// ConstantFwdRefs - This maintains a mapping between <Type, Slot #>'s and
|
|
|
|
// forward references to constants. Such values may be referenced before they
|
|
|
|
// are defined, and if so, the temporary object that they represent is held
|
|
|
|
// here.
|
2002-08-17 22:01:27 +00:00
|
|
|
//
|
2003-11-19 17:27:18 +00:00
|
|
|
typedef std::map<std::pair<const Type*,unsigned>, Constant*> ConstantRefsType;
|
|
|
|
ConstantRefsType ConstantFwdRefs;
|
2001-10-13 06:47:01 +00:00
|
|
|
|
2001-09-07 16:37:43 +00:00
|
|
|
// TypesLoaded - This vector mirrors the Values[TypeTyID] plane. It is used
|
|
|
|
// to deal with forward references to types.
|
|
|
|
//
|
2003-10-02 20:26:18 +00:00
|
|
|
typedef std::vector<PATypeHolder> TypeValuesListTy;
|
2001-09-07 16:37:43 +00:00
|
|
|
TypeValuesListTy ModuleTypeValues;
|
2003-03-06 17:18:14 +00:00
|
|
|
TypeValuesListTy FunctionTypeValues;
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2003-03-19 20:54:26 +00:00
|
|
|
// When the ModuleGlobalInfo section is read, we create a function object for
|
|
|
|
// each function in the module. When the function is loaded, this function is
|
|
|
|
// filled in.
|
|
|
|
//
|
2003-11-19 17:27:18 +00:00
|
|
|
std::vector<Function*> FunctionSignatureList;
|
2003-03-19 20:54:26 +00:00
|
|
|
|
|
|
|
// Constant values are read in after global variables. Because of this, we
|
|
|
|
// must defer setting the initializers on global variables until after module
|
|
|
|
// level constants have been read. In the mean time, this list keeps track of
|
|
|
|
// what we must do.
|
2001-06-06 20:29:01 +00:00
|
|
|
//
|
2003-03-19 20:54:26 +00:00
|
|
|
std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2003-09-22 23:38:23 +00:00
|
|
|
// For lazy reading-in of functions, we need to save away several pieces of
|
|
|
|
// information about each function: its begin and end pointer in the buffer
|
|
|
|
// and its FunctionSlot.
|
|
|
|
//
|
2003-11-19 17:27:18 +00:00
|
|
|
std::map<Function*, LazyFunctionInfo> LazyFunctionLoadMap;
|
2003-09-22 23:38:23 +00:00
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
private:
|
2003-03-19 20:54:26 +00:00
|
|
|
void freeTable(ValueTable &Tab) {
|
|
|
|
while (!Tab.empty()) {
|
|
|
|
delete Tab.back();
|
|
|
|
Tab.pop_back();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-18 21:08:15 +00:00
|
|
|
/// getGlobalTableType - This is just like getType, but when a compaction
|
|
|
|
/// table is in use, it is ignored. Also, no forward references or other
|
|
|
|
/// fancy features are supported.
|
|
|
|
const Type *getGlobalTableType(unsigned Slot) {
|
|
|
|
if (Slot < Type::FirstDerivedTyID) {
|
|
|
|
const Type *Ty = Type::getPrimitiveType((Type::PrimitiveID)Slot);
|
|
|
|
assert(Ty && "Not a primitive type ID?");
|
|
|
|
return Ty;
|
|
|
|
}
|
|
|
|
Slot -= Type::FirstDerivedTyID;
|
|
|
|
if (Slot >= ModuleTypeValues.size())
|
|
|
|
throw std::string("Illegal compaction table type reference!");
|
|
|
|
return ModuleTypeValues[Slot];
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned getGlobalTableTypeSlot(const Type *Ty) {
|
|
|
|
if (Ty->isPrimitiveType())
|
|
|
|
return Ty->getPrimitiveID();
|
|
|
|
TypeValuesListTy::iterator I = find(ModuleTypeValues.begin(),
|
|
|
|
ModuleTypeValues.end(), Ty);
|
|
|
|
if (I == ModuleTypeValues.end())
|
|
|
|
throw std::string("Didn't find type in ModuleTypeValues.");
|
|
|
|
return Type::FirstDerivedTyID + (&*I - &ModuleTypeValues[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getGlobalTableValue - This is just like getValue, but when a compaction
|
|
|
|
/// table is in use, it is ignored. Also, no forward references or other
|
|
|
|
/// fancy features are supported.
|
|
|
|
Value *getGlobalTableValue(const Type *Ty, unsigned SlotNo) {
|
|
|
|
// FIXME: getTypeSlot is inefficient!
|
|
|
|
unsigned TyID = getGlobalTableTypeSlot(Ty);
|
|
|
|
|
|
|
|
if (TyID != Type::LabelTyID) {
|
|
|
|
if (SlotNo == 0)
|
|
|
|
return Constant::getNullValue(Ty);
|
|
|
|
--SlotNo;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (TyID >= ModuleValues.size() || ModuleValues[TyID] == 0 ||
|
|
|
|
SlotNo >= ModuleValues[TyID]->getNumOperands()) {
|
|
|
|
std::cerr << TyID << ", " << SlotNo << ": " << ModuleValues.size() << ", "
|
|
|
|
<< (void*)ModuleValues[TyID] << ", "
|
|
|
|
<< ModuleValues[TyID]->getNumOperands() << "\n";
|
|
|
|
throw std::string("Corrupt compaction table entry!");
|
|
|
|
}
|
|
|
|
return ModuleValues[TyID]->getOperand(SlotNo);
|
|
|
|
}
|
|
|
|
|
2003-09-22 23:38:23 +00:00
|
|
|
public:
|
|
|
|
void ParseModule(const unsigned char * Buf, const unsigned char *End);
|
|
|
|
void materializeFunction(Function *F);
|
|
|
|
|
|
|
|
private:
|
|
|
|
void ParseVersionInfo (const unsigned char *&Buf, const unsigned char *End);
|
|
|
|
void ParseModuleGlobalInfo(const unsigned char *&Buf, const unsigned char *E);
|
|
|
|
void ParseSymbolTable(const unsigned char *&Buf, const unsigned char *End,
|
2003-10-08 22:52:54 +00:00
|
|
|
SymbolTable *, Function *CurrentFunction);
|
2003-09-22 23:38:23 +00:00
|
|
|
void ParseFunction(const unsigned char *&Buf, const unsigned char *End);
|
2004-01-18 21:08:15 +00:00
|
|
|
void ParseCompactionTable(const unsigned char *&Buf,const unsigned char *End);
|
2003-09-22 23:38:23 +00:00
|
|
|
void ParseGlobalTypes(const unsigned char *&Buf, const unsigned char *EndBuf);
|
|
|
|
|
2003-10-08 22:52:54 +00:00
|
|
|
BasicBlock *ParseBasicBlock(const unsigned char *&Buf,
|
|
|
|
const unsigned char *End,
|
|
|
|
unsigned BlockNo);
|
2003-12-01 07:05:31 +00:00
|
|
|
unsigned ParseInstructionList(Function *F, const unsigned char *&Buf,
|
|
|
|
const unsigned char *EndBuf);
|
|
|
|
|
2003-10-18 05:54:18 +00:00
|
|
|
void ParseInstruction(const unsigned char *&Buf, const unsigned char *End,
|
|
|
|
std::vector<unsigned> &Args, BasicBlock *BB);
|
2003-05-22 18:08:30 +00:00
|
|
|
|
2003-09-22 23:38:23 +00:00
|
|
|
void ParseConstantPool(const unsigned char *&Buf, const unsigned char *EndBuf,
|
|
|
|
ValueTable &Tab, TypeValuesListTy &TypeTab);
|
2003-10-04 20:00:03 +00:00
|
|
|
Constant *parseConstantValue(const unsigned char *&Buf,
|
|
|
|
const unsigned char *End,
|
2003-11-19 17:27:18 +00:00
|
|
|
unsigned TypeID);
|
2003-09-22 23:38:23 +00:00
|
|
|
void parseTypeConstants(const unsigned char *&Buf,
|
2003-05-22 18:08:30 +00:00
|
|
|
const unsigned char *EndBuf,
|
2003-09-22 23:38:23 +00:00
|
|
|
TypeValuesListTy &Tab, unsigned NumEntries);
|
2003-05-22 18:08:30 +00:00
|
|
|
const Type *parseTypeConstant(const unsigned char *&Buf,
|
|
|
|
const unsigned char *EndBuf);
|
2004-01-14 23:35:21 +00:00
|
|
|
void parseStringConstants(const unsigned char *&Buf,
|
|
|
|
const unsigned char *EndBuf,
|
|
|
|
unsigned NumEntries, ValueTable &Tab);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2003-10-08 21:18:57 +00:00
|
|
|
Value *getValue(unsigned TypeID, unsigned num, bool Create = true);
|
2001-06-06 20:29:01 +00:00
|
|
|
const Type *getType(unsigned ID);
|
2003-10-08 22:52:54 +00:00
|
|
|
BasicBlock *getBasicBlock(unsigned ID);
|
2003-11-19 06:01:12 +00:00
|
|
|
Constant *getConstantValue(unsigned TypeID, unsigned num);
|
|
|
|
Constant *getConstantValue(const Type *Ty, unsigned num) {
|
|
|
|
return getConstantValue(getTypeSlot(Ty), num);
|
|
|
|
}
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2003-10-13 14:34:59 +00:00
|
|
|
unsigned insertValue(Value *V, unsigned Type, ValueTable &Table);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2003-10-04 20:00:03 +00:00
|
|
|
unsigned getTypeSlot(const Type *Ty);
|
2001-09-07 16:37:43 +00:00
|
|
|
|
2003-11-19 17:27:18 +00:00
|
|
|
// resolve all references to the placeholder (if any) for the given constant
|
|
|
|
void ResolveReferencesToConstant(Constant *C, unsigned Slot);
|
2001-06-06 20:29:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<class SuperType>
|
|
|
|
class PlaceholderDef : public SuperType {
|
|
|
|
unsigned ID;
|
2002-08-17 22:01:27 +00:00
|
|
|
PlaceholderDef(); // DO NOT IMPLEMENT
|
|
|
|
void operator=(const PlaceholderDef &); // DO NOT IMPLEMENT
|
2001-06-06 20:29:01 +00:00
|
|
|
public:
|
|
|
|
PlaceholderDef(const Type *Ty, unsigned id) : SuperType(Ty), ID(id) {}
|
|
|
|
unsigned getID() { return ID; }
|
|
|
|
};
|
|
|
|
|
2003-11-14 06:38:46 +00:00
|
|
|
struct ConstantPlaceHolderHelper : public ConstantExpr {
|
2003-11-14 16:34:25 +00:00
|
|
|
ConstantPlaceHolderHelper(const Type *Ty)
|
|
|
|
: ConstantExpr(Instruction::UserOp1, Constant::getNullValue(Ty), Ty) {}
|
2002-07-14 23:05:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef PlaceholderDef<ConstantPlaceHolderHelper> ConstPHolder;
|
|
|
|
|
2003-09-22 23:38:23 +00:00
|
|
|
static inline void readBlock(const unsigned char *&Buf,
|
2003-05-22 18:08:30 +00:00
|
|
|
const unsigned char *EndBuf,
|
2003-09-22 23:38:23 +00:00
|
|
|
unsigned &Type, unsigned &Size) {
|
2004-01-15 06:13:09 +00:00
|
|
|
Type = read(Buf, EndBuf);
|
|
|
|
Size = read(Buf, EndBuf);
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
} // End llvm namespace
|
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
#endif
|