2001-06-06 22:29:01 +02:00
|
|
|
//===-- ReaderInternals.h - Definitions internal to the reader ---*- C++ -*--=//
|
|
|
|
//
|
|
|
|
// This header file defines various stuff that is used by the bytecode reader.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef READER_INTERNALS_H
|
|
|
|
#define READER_INTERNALS_H
|
|
|
|
|
|
|
|
#include "llvm/Bytecode/Primitives.h"
|
2001-12-03 19:02:31 +01:00
|
|
|
#include "llvm/DerivedTypes.h"
|
2002-08-18 00:01:27 +02:00
|
|
|
#include "llvm/Function.h"
|
2002-07-15 01:05:09 +02:00
|
|
|
#include "llvm/Constant.h"
|
2001-06-06 22:29:01 +02:00
|
|
|
#include <utility>
|
2002-08-18 00:01:27 +02:00
|
|
|
#include <map>
|
2001-06-06 22:29:01 +02:00
|
|
|
|
2001-09-07 18:37:43 +02:00
|
|
|
// Enable to trace to figure out what the heck is going on when parsing fails
|
2001-10-24 07:14:35 +02:00
|
|
|
#define TRACE_LEVEL 0
|
2001-09-07 18:37:43 +02:00
|
|
|
|
|
|
|
#if TRACE_LEVEL // ByteCodeReading_TRACEer
|
2003-03-19 21:54:26 +01:00
|
|
|
#define BCR_TRACE(n, X) \
|
|
|
|
if (n < TRACE_LEVEL) std::cerr << std::string(n*2, ' ') << X
|
2001-09-07 18:37:43 +02:00
|
|
|
#else
|
|
|
|
#define BCR_TRACE(n, X)
|
|
|
|
#endif
|
|
|
|
|
2001-06-06 22:29:01 +02:00
|
|
|
typedef unsigned char uchar;
|
|
|
|
|
|
|
|
struct RawInst { // The raw fields out of the bytecode stream...
|
|
|
|
unsigned NumOperands;
|
|
|
|
unsigned Opcode;
|
|
|
|
const Type *Ty;
|
|
|
|
unsigned Arg1, Arg2;
|
|
|
|
union {
|
|
|
|
unsigned Arg3;
|
2002-01-20 23:54:45 +01:00
|
|
|
std::vector<unsigned> *VarArgs; // Contains arg #3,4,5... if NumOperands > 3
|
2001-06-06 22:29:01 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2001-09-07 18:37:43 +02:00
|
|
|
class BytecodeParser : public AbstractTypeUser {
|
2002-01-20 23:54:45 +01:00
|
|
|
std::string Error; // Error message string goes here...
|
2002-08-18 00:01:27 +02:00
|
|
|
BytecodeParser(const BytecodeParser &); // DO NOT IMPLEMENT
|
|
|
|
void operator=(const BytecodeParser &); // DO NOT IMPLEMENT
|
2001-06-06 22:29:01 +02:00
|
|
|
public:
|
|
|
|
BytecodeParser() {
|
|
|
|
// Define this in case we don't see a ModuleGlobalInfo block.
|
|
|
|
FirstDerivedTyID = Type::FirstDerivedTyID;
|
|
|
|
}
|
2003-03-19 21:54:26 +01:00
|
|
|
~BytecodeParser() {
|
|
|
|
freeTable(Values);
|
|
|
|
freeTable(LateResolveValues);
|
|
|
|
freeTable(ModuleValues);
|
|
|
|
}
|
2001-06-06 22:29:01 +02:00
|
|
|
|
2003-04-22 20:02:52 +02:00
|
|
|
Module *ParseBytecode(const uchar *Buf, const uchar *EndBuf,
|
|
|
|
const std::string &ModuleID);
|
2001-10-24 03:15:12 +02:00
|
|
|
|
2002-01-20 23:54:45 +01:00
|
|
|
std::string getError() const { return Error; }
|
2001-10-24 03:15:12 +02:00
|
|
|
|
2002-04-04 21:24:11 +02:00
|
|
|
void dump() const {
|
2002-06-25 22:44:04 +02:00
|
|
|
std::cerr << "BytecodeParser instance!\n";
|
2002-04-04 21:24:11 +02:00
|
|
|
}
|
|
|
|
|
2001-06-06 22:29:01 +02:00
|
|
|
private: // All of this data is transient across calls to ParseBytecode
|
2003-03-19 21:54:26 +01:00
|
|
|
struct ValueList : public User {
|
|
|
|
ValueList() : User(Type::TypeTy, Value::TypeVal) {
|
|
|
|
}
|
|
|
|
~ValueList() {}
|
|
|
|
|
|
|
|
// 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!";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2001-10-13 08:47:01 +02:00
|
|
|
Module *TheModule; // Current Module being read into...
|
|
|
|
|
2003-03-06 18:55:45 +01:00
|
|
|
// Information about the module, extracted from the bytecode revision number.
|
|
|
|
unsigned char RevisionNum; // The rev # itself
|
|
|
|
unsigned char FirstDerivedTyID; // First variable index to use for type
|
|
|
|
bool HasImplicitZeroInitializer; // Is entry 0 of every slot implicity zeros?
|
|
|
|
bool isBigEndian, hasLongPointers;// Information about the target compiled for
|
2003-04-16 23:16:05 +02:00
|
|
|
bool hasInternalMarkerOnly; // Only types of linkage are intern/external
|
2003-03-06 18:55:45 +01:00
|
|
|
|
2003-03-19 21:54:26 +01:00
|
|
|
typedef std::vector<ValueList*> ValueTable;
|
2001-06-06 22:29:01 +02:00
|
|
|
ValueTable Values, LateResolveValues;
|
2003-03-19 21:54:26 +01:00
|
|
|
ValueTable ModuleValues;
|
2001-09-07 18:37:43 +02:00
|
|
|
|
2002-08-18 00:01:27 +02:00
|
|
|
// GlobalRefs - This maintains a mapping between <Type, Slot #>'s and forward
|
|
|
|
// references to global values or constants. Such values may be referenced
|
|
|
|
// before they are defined, and if so, the temporary object that they
|
|
|
|
// represent is held here.
|
|
|
|
//
|
2003-03-19 21:54:26 +01:00
|
|
|
typedef std::map<std::pair<const Type *, unsigned>, Value*> GlobalRefsType;
|
2002-08-18 00:01:27 +02:00
|
|
|
GlobalRefsType GlobalRefs;
|
2001-10-13 08:47:01 +02:00
|
|
|
|
2001-09-07 18:37:43 +02:00
|
|
|
// TypesLoaded - This vector mirrors the Values[TypeTyID] plane. It is used
|
|
|
|
// to deal with forward references to types.
|
|
|
|
//
|
2002-01-20 23:54:45 +01:00
|
|
|
typedef std::vector<PATypeHandle<Type> > TypeValuesListTy;
|
2001-09-07 18:37:43 +02:00
|
|
|
TypeValuesListTy ModuleTypeValues;
|
2003-03-06 18:18:14 +01:00
|
|
|
TypeValuesListTy FunctionTypeValues;
|
2001-06-06 22:29:01 +02:00
|
|
|
|
2003-03-19 21:54:26 +01: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.
|
|
|
|
//
|
|
|
|
std::vector<std::pair<Function*, unsigned> > FunctionSignatureList;
|
|
|
|
|
|
|
|
// 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 22:29:01 +02:00
|
|
|
//
|
2003-03-19 21:54:26 +01:00
|
|
|
std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
|
2001-06-06 22:29:01 +02:00
|
|
|
|
|
|
|
private:
|
2003-03-19 21:54:26 +01:00
|
|
|
void freeTable(ValueTable &Tab) {
|
|
|
|
while (!Tab.empty()) {
|
|
|
|
delete Tab.back();
|
|
|
|
Tab.pop_back();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-03-06 18:15:19 +01:00
|
|
|
bool ParseModule (const uchar * Buf, const uchar *End);
|
2003-03-06 18:55:45 +01:00
|
|
|
bool ParseVersionInfo (const uchar *&Buf, const uchar *End);
|
2003-03-06 18:15:19 +01:00
|
|
|
bool ParseModuleGlobalInfo(const uchar *&Buf, const uchar *End);
|
2001-09-07 18:37:43 +02:00
|
|
|
bool ParseSymbolTable (const uchar *&Buf, const uchar *End, SymbolTable *);
|
2003-03-06 18:18:14 +01:00
|
|
|
bool ParseFunction (const uchar *&Buf, const uchar *End);
|
2001-06-06 22:29:01 +02:00
|
|
|
bool ParseBasicBlock (const uchar *&Buf, const uchar *End, BasicBlock *&);
|
2002-08-22 00:55:27 +02:00
|
|
|
bool ParseInstruction (const uchar *&Buf, const uchar *End, Instruction *&,
|
|
|
|
BasicBlock *BB /*HACK*/);
|
2001-06-06 22:29:01 +02:00
|
|
|
bool ParseRawInst (const uchar *&Buf, const uchar *End, RawInst &);
|
|
|
|
|
2003-03-19 21:54:26 +01:00
|
|
|
bool ParseGlobalTypes(const uchar *&Buf, const uchar *EndBuf);
|
2001-06-06 22:29:01 +02:00
|
|
|
bool ParseConstantPool(const uchar *&Buf, const uchar *EndBuf,
|
2001-09-07 18:37:43 +02:00
|
|
|
ValueTable &Tab, TypeValuesListTy &TypeTab);
|
2001-12-03 23:26:30 +01:00
|
|
|
bool parseConstantValue(const uchar *&Buf, const uchar *End,
|
|
|
|
const Type *Ty, Constant *&V);
|
2001-09-07 18:37:43 +02:00
|
|
|
bool parseTypeConstants(const uchar *&Buf, const uchar *EndBuf,
|
|
|
|
TypeValuesListTy &Tab, unsigned NumEntries);
|
|
|
|
const Type *parseTypeConstant(const uchar *&Buf, const uchar *EndBuf);
|
2001-06-06 22:29:01 +02:00
|
|
|
|
|
|
|
Value *getValue(const Type *Ty, unsigned num, bool Create = true);
|
|
|
|
const Type *getType(unsigned ID);
|
2002-10-14 05:33:02 +02:00
|
|
|
Constant *getConstantValue(const Type *Ty, unsigned num);
|
2001-06-06 22:29:01 +02:00
|
|
|
|
2003-03-19 21:54:26 +01:00
|
|
|
int insertValue(Value *V, ValueTable &Table); // -1 = Failure
|
|
|
|
void setValueTo(ValueTable &D, unsigned Slot, Value *V);
|
2001-06-06 22:29:01 +02:00
|
|
|
bool postResolveValues(ValueTable &ValTab);
|
|
|
|
|
|
|
|
bool getTypeSlot(const Type *Ty, unsigned &Slot);
|
2001-09-07 18:37:43 +02:00
|
|
|
|
2002-08-18 00:01:27 +02:00
|
|
|
// resolve all references to the placeholder (if any) for the given value
|
|
|
|
void ResolveReferencesToValue(Value *Val, unsigned Slot);
|
|
|
|
|
2002-07-15 01:05:09 +02:00
|
|
|
|
2001-09-07 18:37:43 +02:00
|
|
|
// refineAbstractType - The callback method is invoked when one of the
|
|
|
|
// elements of TypeValues becomes more concrete...
|
|
|
|
//
|
|
|
|
virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
|
2001-06-06 22:29:01 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
template<class SuperType>
|
|
|
|
class PlaceholderDef : public SuperType {
|
|
|
|
unsigned ID;
|
2002-08-18 00:01:27 +02:00
|
|
|
PlaceholderDef(); // DO NOT IMPLEMENT
|
|
|
|
void operator=(const PlaceholderDef &); // DO NOT IMPLEMENT
|
2001-06-06 22:29:01 +02:00
|
|
|
public:
|
|
|
|
PlaceholderDef(const Type *Ty, unsigned id) : SuperType(Ty), ID(id) {}
|
|
|
|
unsigned getID() { return ID; }
|
|
|
|
};
|
|
|
|
|
|
|
|
struct InstPlaceHolderHelper : public Instruction {
|
|
|
|
InstPlaceHolderHelper(const Type *Ty) : Instruction(Ty, UserOp1, "") {}
|
2001-07-07 21:24:15 +02:00
|
|
|
virtual const char *getOpcodeName() const { return "placeholder"; }
|
2001-06-06 22:29:01 +02:00
|
|
|
|
|
|
|
virtual Instruction *clone() const { abort(); return 0; }
|
|
|
|
};
|
|
|
|
|
|
|
|
struct BBPlaceHolderHelper : public BasicBlock {
|
|
|
|
BBPlaceHolderHelper(const Type *Ty) : BasicBlock() {
|
2002-04-08 23:59:36 +02:00
|
|
|
assert(Ty == Type::LabelTy);
|
2001-06-06 22:29:01 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2002-07-15 01:05:09 +02:00
|
|
|
struct ConstantPlaceHolderHelper : public Constant {
|
|
|
|
ConstantPlaceHolderHelper(const Type *Ty)
|
|
|
|
: Constant(Ty) {}
|
|
|
|
virtual bool isNullValue() const { return false; }
|
|
|
|
};
|
|
|
|
|
2002-08-18 00:01:27 +02:00
|
|
|
typedef PlaceholderDef<InstPlaceHolderHelper> ValPHolder;
|
2001-06-06 22:29:01 +02:00
|
|
|
typedef PlaceholderDef<BBPlaceHolderHelper> BBPHolder;
|
2002-07-15 01:05:09 +02:00
|
|
|
typedef PlaceholderDef<ConstantPlaceHolderHelper> ConstPHolder;
|
|
|
|
|
2001-06-06 22:29:01 +02:00
|
|
|
|
2002-08-18 00:01:27 +02:00
|
|
|
static inline unsigned getValueIDNumberFromPlaceHolder(Value *Val) {
|
|
|
|
if (isa<Constant>(Val))
|
|
|
|
return ((ConstPHolder*)Val)->getID();
|
2002-07-15 01:05:09 +02:00
|
|
|
|
|
|
|
// else discriminate by type
|
2002-08-18 00:01:27 +02:00
|
|
|
switch (Val->getType()->getPrimitiveID()) {
|
|
|
|
case Type::LabelTyID: return ((BBPHolder*)Val)->getID();
|
|
|
|
default: return ((ValPHolder*)Val)->getID();
|
2001-06-06 22:29:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool readBlock(const uchar *&Buf, const uchar *EndBuf,
|
|
|
|
unsigned &Type, unsigned &Size) {
|
|
|
|
#if DEBUG_OUTPUT
|
|
|
|
bool Result = read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size);
|
2002-06-25 22:44:04 +02:00
|
|
|
std::cerr << "StartLoc = " << ((unsigned)Buf & 4095)
|
2001-06-06 22:29:01 +02:00
|
|
|
<< " Type = " << Type << " Size = " << Size << endl;
|
|
|
|
return Result;
|
|
|
|
#else
|
|
|
|
return read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|