For PR797:

Remove exception throwing/handling from lib/Bytecode, and adjust its users
to compensate for changes in the interface.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@29875 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Reid Spencer
2006-08-25 17:43:11 +00:00
parent ac12322710
commit 0b5a504d10
10 changed files with 325 additions and 234 deletions

View File

@@ -29,48 +29,75 @@ namespace llvm {
// Forward declare the handler class // Forward declare the handler class
class BytecodeHandler; class BytecodeHandler;
/// getBytecodeModuleProvider - lazy function-at-a-time loading from a file /// This function returns a ModuleProvider that can be used to do lazy
/// /// function-at-a-time loading from a bytecode file.
/// @returns NULL on error
/// @returns ModuleProvider* if successful
/// @brief Get a ModuleProvide for a bytecode file.
ModuleProvider *getBytecodeModuleProvider( ModuleProvider *getBytecodeModuleProvider(
const std::string &Filename, ///< Name of file to be read const std::string &Filename, ///< Name of file to be read
BytecodeHandler* H = 0 ///< Optional handler for reader events std::string* ErrMsg, ///< Optional error message holder
BytecodeHandler* H = 0 ///< Optional handler for reader events
); );
/// getBytecodeBufferModuleProvider - lazy function-at-a-time loading from a /// This function returns a ModuleProvider that can be used to do lazy
/// buffer /// function function-at-a-time loading from a bytecode buffer.
/// /// @returns NULL on error
ModuleProvider *getBytecodeBufferModuleProvider(const unsigned char *Buffer, /// @returns ModuleProvider* if successful
unsigned BufferSize, /// @brief Get a ModuleProvider for a bytecode buffer.
const std::string &ModuleID="", ModuleProvider *getBytecodeBufferModuleProvider(
BytecodeHandler* H = 0); const unsigned char *Buffer, ///< Start of buffer to parse
unsigned BufferSize, ///< Size of the buffer
const std::string &ModuleID, ///< Name to give the module
std::string* ErrMsg, ///< Optional place to return an error message
BytecodeHandler* H ///< Optional handler for reader events
);
/// This is the main interface to bytecode parsing. It opens the file specified
/// by \p Filename and parses the entire file, returing the corresponding Module
/// object if successful.
/// @returns NULL on error
/// @returns the module corresponding to the bytecode file, if successful
/// @brief Parse the given bytecode file /// @brief Parse the given bytecode file
Module* ParseBytecodeFile(const std::string &Filename, Module* ParseBytecodeFile(
std::string *ErrorStr = 0); const std::string &Filename, ///< Name of file to parse
std::string *ErrMsg = 0 ///< Optional place to return an error message
);
/// Parses a bytecode buffer specified by \p Buffer and \p BufferSize.
/// @returns NULL on error
/// @returns the module corresponding to the bytecode buffer, if successful
/// @brief Parse a given bytecode buffer /// @brief Parse a given bytecode buffer
Module* ParseBytecodeBuffer(const unsigned char *Buffer, Module* ParseBytecodeBuffer(
unsigned BufferSize, const unsigned char *Buffer, ///< Start of buffer to parse
const std::string &ModuleID = "", unsigned BufferSize, ///< Size of the buffer
std::string *ErrorStr = 0); const std::string &ModuleID="", ///< Name to give the module
std::string *ErrMsg = 0 ///< Optional place to return an error message
);
/// This function will read only the necessary parts of a bytecode file in order /// This function will read only the necessary parts of a bytecode file in order
/// to determine the list of dependent libraries encoded within it. The \p /// to determine the list of dependent libraries encoded within it. The \p
/// deplibs parameter will contain a vector of strings of the bytecode module's /// deplibs parameter will contain a vector of strings of the bytecode module's
/// dependent libraries. /// dependent libraries.
/// @returns true on success, false otherwise /// @returns true on error, false otherwise
/// @brief Get the list of dependent libraries from a bytecode file. /// @brief Get the list of dependent libraries from a bytecode file.
bool GetBytecodeDependentLibraries(const std::string &fileName, bool GetBytecodeDependentLibraries(
Module::LibraryListType& deplibs); const std::string &fileName, ///< File name to read bytecode from
Module::LibraryListType& deplibs, ///< List of dependent libraries extracted
std::string* ErrMsg ///< Optional error message holder
);
/// This function will read only the necessary parts of a bytecode file in order /// This function will read only the necessary parts of a bytecode file in order
/// to obtain a list of externally visible global symbols that the bytecode /// to obtain a list of externally visible global symbols that the bytecode
/// module defines. This is used for archiving and linking when only the list /// module defines. This is used for archiving and linking when only the list
/// of symbols the module defines is needed. /// of symbols the module defines is needed.
/// @returns true on success, false otherwise /// @returns true on error, false otherwise
/// @brief Get a bytecode file's externally visibile defined global symbols. /// @brief Get a bytecode file's externally visibile defined global symbols.
bool GetBytecodeSymbols(const sys::Path& fileName, bool GetBytecodeSymbols(
std::vector<std::string>& syms); const sys::Path& fileName, ///< Filename to read bytecode from
std::vector<std::string>& syms, ///< Vector to return symbols in
std::string* ErrMsg ///< Optional error message holder
);
/// This function will read only the necessary parts of a bytecode buffer in /// This function will read only the necessary parts of a bytecode buffer in
/// order to obtain a list of externally visible global symbols that the /// order to obtain a list of externally visible global symbols that the
@@ -83,7 +110,8 @@ ModuleProvider* GetBytecodeSymbols(
const unsigned char*Buffer, ///< The buffer to be parsed const unsigned char*Buffer, ///< The buffer to be parsed
unsigned Length, ///< The length of \p Buffer unsigned Length, ///< The length of \p Buffer
const std::string& ModuleID, ///< An identifier for the module const std::string& ModuleID, ///< An identifier for the module
std::vector<std::string>& symbols ///< The symbols defined in the module std::vector<std::string>& symbols, ///< The symbols defined in the module
std::string* ErrMsg ///< Optional error message holder
); );
} // End llvm namespace } // End llvm namespace

View File

@@ -7,13 +7,13 @@
// //
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
// //
// This file contains the declarations for the subclasses of Constant, which /// @file This file contains the declarations for the subclasses of Constant,
// represent the different flavors of constant values that live in LLVM. Note /// which represent the different flavors of constant values that live in LLVM.
// that Constants are immutable (once created they never change) and are fully /// Note that Constants are immutable (once created they never change) and are
// shared by structural equivalence. This means that two structurally /// fully shared by structural equivalence. This means that two structurally
// equivalent constants will always have the same address. Constant's are /// equivalent constants will always have the same address. Constant's are
// created on demand as needed and never deleted: thus clients don't have to /// created on demand as needed and never deleted: thus clients don't have to
// worry about the lifetime of the objects. /// worry about the lifetime of the objects.
// //
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
@@ -36,10 +36,9 @@ template<class ConstantClass, class TypeClass>
struct ConvertConstantType; struct ConvertConstantType;
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
/// ConstantIntegral - Shared superclass of boolean and integer constants. /// This is the shared superclass of boolean and integer constants. This class
/// /// just defines some common interfaces to be implemented by the subclasses.
/// This class just defines some common interfaces to be implemented. /// @brief An abstract class for integer constants.
///
class ConstantIntegral : public Constant { class ConstantIntegral : public Constant {
protected: protected:
union { union {
@@ -49,52 +48,66 @@ protected:
ConstantIntegral(const Type *Ty, ValueTy VT, uint64_t V); ConstantIntegral(const Type *Ty, ValueTy VT, uint64_t V);
public: public:
/// getRawValue - return the underlying value of this constant as a 64-bit /// @brief Return the raw value of the constant as a 64-bit integer value.
/// unsigned integer value.
///
inline uint64_t getRawValue() const { return Val.Unsigned; } inline uint64_t getRawValue() const { return Val.Unsigned; }
/// getZExtValue - Return the constant zero extended as appropriate for this /// Return the constant as a 64-bit unsigned integer value after it
/// type. /// has been zero extended as appropriate for the type of this constant.
/// @brief Return the zero extended value.
inline uint64_t getZExtValue() const { inline uint64_t getZExtValue() const {
unsigned Size = getType()->getPrimitiveSizeInBits(); unsigned Size = getType()->getPrimitiveSizeInBits();
return Val.Unsigned & (~uint64_t(0UL) >> (64-Size)); return Val.Unsigned & (~uint64_t(0UL) >> (64-Size));
} }
/// getSExtValue - Return the constant sign extended as appropriate for this /// Return the constant as a 64-bit integer value after it has been sign
/// type. /// sign extended as appropriate for the type of this constant.
/// @brief REturn the sign extended value.
inline int64_t getSExtValue() const { inline int64_t getSExtValue() const {
unsigned Size = getType()->getPrimitiveSizeInBits(); unsigned Size = getType()->getPrimitiveSizeInBits();
return (Val.Signed << (64-Size)) >> (64-Size); return (Val.Signed << (64-Size)) >> (64-Size);
} }
/// isNullValue - Return true if this is the value that would be returned by /// This function is implemented by subclasses and will return true iff this
/// getNullValue. /// constant represents the the "null" value that would be returned by the
/// /// getNullValue method.
/// @returns true if the constant's value is 0.
/// @brief Determine if the value is null.
virtual bool isNullValue() const = 0; virtual bool isNullValue() const = 0;
/// isMaxValue - Return true if this is the largest value that may be /// This function is implemented by sublcasses and will return true iff this
/// represented by this type. /// constant represents the the largest value that may be represented by this
/// /// constant's type.
/// @returns true if the constant's value is maximal.
/// @brief Determine if the value is maximal.
virtual bool isMaxValue() const = 0; virtual bool isMaxValue() const = 0;
/// isMinValue - Return true if this is the smallest value that may be /// This function is implemented by subclasses and will return true iff this
/// represented by this type. /// constant represents the smallest value that may be represented by this
/// /// constant's type.
/// @returns true if the constant's value is minimal
/// @brief Determine if the value is minimal.
virtual bool isMinValue() const = 0; virtual bool isMinValue() const = 0;
/// isAllOnesValue - Return true if every bit in this constant is set to true. /// This function is implemented by subclasses and will return true iff every
/// /// bit in this constant is set to true.
/// @returns true if all bits of the constant are ones.
/// @brief Determine if the value is all ones.
virtual bool isAllOnesValue() const = 0; virtual bool isAllOnesValue() const = 0;
/// Static constructor to get the maximum/minimum/allones constant of /// @returns the largest value for an integer constant of the given type
/// specified (integral) type... /// @brief Get the maximal value
///
static ConstantIntegral *getMaxValue(const Type *Ty); static ConstantIntegral *getMaxValue(const Type *Ty);
/// @returns the smallest value for an integer constant of the given type
/// @brief Get the minimal value
static ConstantIntegral *getMinValue(const Type *Ty); static ConstantIntegral *getMinValue(const Type *Ty);
/// @returns the value for an integer constant of the given type that has all
/// its bits set to true.
/// @brief Get the all ones value
static ConstantIntegral *getAllOnesValue(const Type *Ty); static ConstantIntegral *getAllOnesValue(const Type *Ty);
/// Methods for support type inquiry through isa, cast, and dyn_cast: /// Methods to support type inquiry through isa, cast, and dyn_cast:
static inline bool classof(const ConstantIntegral *) { return true; } static inline bool classof(const ConstantIntegral *) { return true; }
static bool classof(const Value *V) { static bool classof(const Value *V) {
return V->getValueType() == ConstantBoolVal || return V->getValueType() == ConstantBoolVal ||
@@ -105,33 +118,41 @@ public:
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
/// ConstantBool - Boolean Values /// This concrete class represents constant values of type BoolTy. There are
/// /// only two instances of this class constructed: the True and False static
/// members. The constructor is hidden to ensure this invariant.
/// @brief Constant Boolean class
class ConstantBool : public ConstantIntegral { class ConstantBool : public ConstantIntegral {
ConstantBool(bool V); ConstantBool(bool V);
public: public:
static ConstantBool *True, *False; // The True & False values static ConstantBool *True, *False; ///< The True & False values
/// get() - Static factory methods - Return objects of the specified value /// This method is provided mostly for compatibility with the other
/// ConstantIntegral subclasses.
/// @brief Static factory method for getting a ConstantBool instance.
static ConstantBool *get(bool Value) { return Value ? True : False; } static ConstantBool *get(bool Value) { return Value ? True : False; }
/// This method is provided mostly for compatibility with the other
/// ConstantIntegral subclasses.
/// @brief Static factory method for getting a ConstantBool instance.
static ConstantBool *get(const Type *Ty, bool Value) { return get(Value); } static ConstantBool *get(const Type *Ty, bool Value) { return get(Value); }
/// inverted - Return the opposite value of the current value. /// Returns the opposite value of this ConstantBool value.
/// @brief Get inverse value.
inline ConstantBool *inverted() const { return (this==True) ? False : True; } inline ConstantBool *inverted() const { return (this==True) ? False : True; }
/// getValue - return the boolean value of this constant. /// @returns the value of this ConstantBool
/// /// @brief return the boolean value of this constant.
inline bool getValue() const { return static_cast<bool>(getRawValue()); } inline bool getValue() const { return static_cast<bool>(getRawValue()); }
/// isNullValue - Return true if this is the value that would be returned by /// @see ConstantIntegral for details
/// getNullValue. /// @brief Implement overrides
///
virtual bool isNullValue() const { return this == False; } virtual bool isNullValue() const { return this == False; }
virtual bool isMaxValue() const { return this == True; } virtual bool isMaxValue() const { return this == True; }
virtual bool isMinValue() const { return this == False; } virtual bool isMinValue() const { return this == False; }
virtual bool isAllOnesValue() const { return this == True; } virtual bool isAllOnesValue() const { return this == True; }
/// Methods for support type inquiry through isa, cast, and dyn_cast: /// @brief Methods to support type inquiry through isa, cast, and dyn_cast:
static inline bool classof(const ConstantBool *) { return true; } static inline bool classof(const ConstantBool *) { return true; }
static bool classof(const Value *V) { static bool classof(const Value *V) {
return V->getValueType() == ConstantBoolVal; return V->getValueType() == ConstantBoolVal;
@@ -140,37 +161,36 @@ public:
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
/// ConstantInt - Superclass of ConstantSInt & ConstantUInt, to make dealing /// This is the abstract superclass of ConstantSInt & ConstantUInt, to make
/// with integral constants easier. /// dealing with integral constants easier when sign is irrelevant.
/// /// @brief Abstract clas for constant integers.
class ConstantInt : public ConstantIntegral { class ConstantInt : public ConstantIntegral {
protected: protected:
ConstantInt(const ConstantInt &); // DO NOT IMPLEMENT ConstantInt(const ConstantInt &); // DO NOT IMPLEMENT
ConstantInt(const Type *Ty, ValueTy VT, uint64_t V); ConstantInt(const Type *Ty, ValueTy VT, uint64_t V);
public: public:
/// equalsInt - Provide a helper method that can be used to determine if the /// A helper method that can be used to determine if the constant contained
/// constant contained within is equal to a constant. This only works for /// within is equal to a constant. This only works for very small values,
/// very small values, because this is all that can be represented with all /// because this is all that can be represented with all types.
/// types. /// @brief Determine if this constant's value is same as an unsigned char.
///
bool equalsInt(unsigned char V) const { bool equalsInt(unsigned char V) const {
assert(V <= 127 && assert(V <= 127 &&
"equalsInt: Can only be used with very small positive constants!"); "equalsInt: Can only be used with very small positive constants!");
return Val.Unsigned == V; return Val.Unsigned == V;
} }
/// ConstantInt::get static method: return a ConstantInt with the specified /// Return a ConstantInt with the specified value for the specified type.
/// value. as above, we work only with very small values here. /// This only works for very small values, because this is all that can be
/// /// represented with all types integer types.
/// @brief Get a ConstantInt for a specific value.
static ConstantInt *get(const Type *Ty, unsigned char V); static ConstantInt *get(const Type *Ty, unsigned char V);
/// isNullValue - Return true if this is the value that would be returned by /// @returns true if this is the null integer value.
/// getNullValue. /// @see ConstantIntegral for details
/// @brief Implement override.
virtual bool isNullValue() const { return Val.Unsigned == 0; } virtual bool isNullValue() const { return Val.Unsigned == 0; }
virtual bool isMaxValue() const = 0;
virtual bool isMinValue() const = 0;
/// Methods for support type inquiry through isa, cast, and dyn_cast: /// @brief Methods to support type inquiry through isa, cast, and dyn_cast.
static inline bool classof(const ConstantInt *) { return true; } static inline bool classof(const ConstantInt *) { return true; }
static bool classof(const Value *V) { static bool classof(const Value *V) {
return V->getValueType() == ConstantSIntVal || return V->getValueType() == ConstantSIntVal ||
@@ -180,8 +200,9 @@ public:
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
/// ConstantSInt - Signed Integer Values [sbyte, short, int, long] /// A concrete class to represent constant signed integer values for the types
/// /// sbyte, short, int, and long.
/// @brief Constant Signed Integer Class.
class ConstantSInt : public ConstantInt { class ConstantSInt : public ConstantInt {
ConstantSInt(const ConstantSInt &); // DO NOT IMPLEMENT ConstantSInt(const ConstantSInt &); // DO NOT IMPLEMENT
friend struct ConstantCreator<ConstantSInt, Type, int64_t>; friend struct ConstantCreator<ConstantSInt, Type, int64_t>;
@@ -189,23 +210,35 @@ class ConstantSInt : public ConstantInt {
protected: protected:
ConstantSInt(const Type *Ty, int64_t V); ConstantSInt(const Type *Ty, int64_t V);
public: public:
/// get() - Static factory methods - Return objects of the specified value /// This static factory methods returns objects of the specified value. Note
/// /// that repeated calls with the same operands return the same object.
static ConstantSInt *get(const Type *Ty, int64_t V); /// @returns A ConstantSInt instant for the type and value requested.
/// @brief Get a signed integer constant.
static ConstantSInt *get(
const Type *Ty, ///< The type of constant (SByteTy, IntTy, ShortTy, LongTy)
int64_t V ///< The value for the constant integer.
);
/// isValueValidForType - return true if Ty is big enough to represent V. /// This static method returns true if the type Ty is big enough to
/// /// represent the value V. This can be used to avoid having the get method
/// assert when V is larger than Ty can represent.
/// @returns true if V is a valid value for type Ty
/// @brief Determine if the value is in range for the given type.
static bool isValueValidForType(const Type *Ty, int64_t V); static bool isValueValidForType(const Type *Ty, int64_t V);
/// getValue - return the underlying value of this constant. /// @returns the underlying value of this constant.
/// /// @brief Get the constant value.
inline int64_t getValue() const { return Val.Signed; } inline int64_t getValue() const { return Val.Signed; }
/// @returns true iff this constant's bits are all set to true.
/// @see ConstantIntegral
/// @brief Override implementation
virtual bool isAllOnesValue() const { return getValue() == -1; } virtual bool isAllOnesValue() const { return getValue() == -1; }
/// isMaxValue - Return true if this is the largest value that may be /// @returns true iff this is the largest value that may be represented
/// represented by this type. /// by this type.
/// /// @see ConstantIntegeral
/// @brief Override implementation
virtual bool isMaxValue() const { virtual bool isMaxValue() const {
int64_t V = getValue(); int64_t V = getValue();
if (V < 0) return false; // Be careful about wrap-around on 'long's if (V < 0) return false; // Be careful about wrap-around on 'long's
@@ -213,9 +246,10 @@ public:
return !isValueValidForType(getType(), V) || V < 0; return !isValueValidForType(getType(), V) || V < 0;
} }
/// isMinValue - Return true if this is the smallest value that may be /// @returns true if this is the smallest value that may be represented by
/// represented by this type. /// this type.
/// /// @see ConstantIntegral
/// @brief Override implementation
virtual bool isMinValue() const { virtual bool isMinValue() const {
int64_t V = getValue(); int64_t V = getValue();
if (V > 0) return false; // Be careful about wrap-around on 'long's if (V > 0) return false; // Be careful about wrap-around on 'long's
@@ -223,8 +257,7 @@ public:
return !isValueValidForType(getType(), V) || V > 0; return !isValueValidForType(getType(), V) || V > 0;
} }
/// Methods for support type inquiry through isa, cast, and dyn_cast: /// @brief Methods to support type inquiry through isa, cast, and dyn_cast:
///
static inline bool classof(const ConstantSInt *) { return true; } static inline bool classof(const ConstantSInt *) { return true; }
static bool classof(const Value *V) { static bool classof(const Value *V) {
return V->getValueType() == ConstantSIntVal; return V->getValueType() == ConstantSIntVal;
@@ -232,8 +265,9 @@ public:
}; };
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
/// ConstantUInt - Unsigned Integer Values [ubyte, ushort, uint, ulong] /// A concrete class that represents constant unsigned integer values of type
/// /// Type::UByteTy, Type::UShortTy, Type::UIntTy, or Type::ULongTy.
/// @brief Constant Unsigned Integer Class
class ConstantUInt : public ConstantInt { class ConstantUInt : public ConstantInt {
ConstantUInt(const ConstantUInt &); // DO NOT IMPLEMENT ConstantUInt(const ConstantUInt &); // DO NOT IMPLEMENT
friend struct ConstantCreator<ConstantUInt, Type, uint64_t>; friend struct ConstantCreator<ConstantUInt, Type, uint64_t>;

View File

@@ -475,14 +475,16 @@ Archive::findModuleDefiningSymbol(const std::string& symbol,
const char* modptr = base + fileOffset; const char* modptr = base + fileOffset;
ArchiveMember* mbr = parseMemberHeader(modptr, base + mapfile->size(),ErrMsg); ArchiveMember* mbr = parseMemberHeader(modptr, base + mapfile->size(),ErrMsg);
if (!mbr) if (!mbr)
return false; return 0;
// Now, load the bytecode module to get the ModuleProvider // Now, load the bytecode module to get the ModuleProvider
std::string FullMemberName = archPath.toString() + "(" + std::string FullMemberName = archPath.toString() + "(" +
mbr->getPath().toString() + ")"; mbr->getPath().toString() + ")";
ModuleProvider* mp = getBytecodeBufferModuleProvider( ModuleProvider* mp = getBytecodeBufferModuleProvider(
(const unsigned char*) mbr->getData(), mbr->getSize(), (const unsigned char*) mbr->getData(), mbr->getSize(),
FullMemberName, 0); FullMemberName, ErrMsg, 0);
if (!mp)
return 0;
modules.insert(std::make_pair(fileOffset, std::make_pair(mp, mbr))); modules.insert(std::make_pair(fileOffset, std::make_pair(mp, mbr)));
@@ -523,7 +525,7 @@ Archive::findModulesDefiningSymbols(std::set<std::string>& symbols,
std::string FullMemberName = archPath.toString() + "(" + std::string FullMemberName = archPath.toString() + "(" +
mbr->getPath().toString() + ")"; mbr->getPath().toString() + ")";
ModuleProvider* MP = GetBytecodeSymbols((const unsigned char*)At, ModuleProvider* MP = GetBytecodeSymbols((const unsigned char*)At,
mbr->getSize(), FullMemberName, symbols); mbr->getSize(), FullMemberName, symbols, error);
if (MP) { if (MP) {
// Insert the module's symbols into the symbol table // Insert the module's symbols into the symbol table
@@ -537,7 +539,7 @@ Archive::findModulesDefiningSymbols(std::set<std::string>& symbols,
} else { } else {
if (error) if (error)
*error = "Can't parse bytecode member: " + *error = "Can't parse bytecode member: " +
mbr->getPath().toString(); mbr->getPath().toString() + ": " + *error;
delete mbr; delete mbr;
return false; return false;
} }

View File

@@ -223,7 +223,7 @@ Archive::writeMember(
member.getPath().toString() member.getPath().toString()
+ ")"; + ")";
ModuleProvider* MP = GetBytecodeSymbols( ModuleProvider* MP = GetBytecodeSymbols(
(const unsigned char*)data,fSize,FullMemberName, symbols); (const unsigned char*)data,fSize,FullMemberName, symbols, ErrMsg);
// If the bytecode parsed successfully // If the bytecode parsed successfully
if ( MP ) { if ( MP ) {
@@ -247,7 +247,8 @@ Archive::writeMember(
delete mFile; delete mFile;
} }
if (ErrMsg) if (ErrMsg)
*ErrMsg = "Can't parse bytecode member: " + member.getPath().toString(); *ErrMsg = "Can't parse bytecode member: " + member.getPath().toString()
+ ": " + *ErrMsg;
return true; return true;
} }
} }

View File

@@ -475,14 +475,16 @@ Archive::findModuleDefiningSymbol(const std::string& symbol,
const char* modptr = base + fileOffset; const char* modptr = base + fileOffset;
ArchiveMember* mbr = parseMemberHeader(modptr, base + mapfile->size(),ErrMsg); ArchiveMember* mbr = parseMemberHeader(modptr, base + mapfile->size(),ErrMsg);
if (!mbr) if (!mbr)
return false; return 0;
// Now, load the bytecode module to get the ModuleProvider // Now, load the bytecode module to get the ModuleProvider
std::string FullMemberName = archPath.toString() + "(" + std::string FullMemberName = archPath.toString() + "(" +
mbr->getPath().toString() + ")"; mbr->getPath().toString() + ")";
ModuleProvider* mp = getBytecodeBufferModuleProvider( ModuleProvider* mp = getBytecodeBufferModuleProvider(
(const unsigned char*) mbr->getData(), mbr->getSize(), (const unsigned char*) mbr->getData(), mbr->getSize(),
FullMemberName, 0); FullMemberName, ErrMsg, 0);
if (!mp)
return 0;
modules.insert(std::make_pair(fileOffset, std::make_pair(mp, mbr))); modules.insert(std::make_pair(fileOffset, std::make_pair(mp, mbr)));
@@ -523,7 +525,7 @@ Archive::findModulesDefiningSymbols(std::set<std::string>& symbols,
std::string FullMemberName = archPath.toString() + "(" + std::string FullMemberName = archPath.toString() + "(" +
mbr->getPath().toString() + ")"; mbr->getPath().toString() + ")";
ModuleProvider* MP = GetBytecodeSymbols((const unsigned char*)At, ModuleProvider* MP = GetBytecodeSymbols((const unsigned char*)At,
mbr->getSize(), FullMemberName, symbols); mbr->getSize(), FullMemberName, symbols, error);
if (MP) { if (MP) {
// Insert the module's symbols into the symbol table // Insert the module's symbols into the symbol table
@@ -537,7 +539,7 @@ Archive::findModulesDefiningSymbols(std::set<std::string>& symbols,
} else { } else {
if (error) if (error)
*error = "Can't parse bytecode member: " + *error = "Can't parse bytecode member: " +
mbr->getPath().toString(); mbr->getPath().toString() + ": " + *error;
delete mbr; delete mbr;
return false; return false;
} }

View File

@@ -223,7 +223,7 @@ Archive::writeMember(
member.getPath().toString() member.getPath().toString()
+ ")"; + ")";
ModuleProvider* MP = GetBytecodeSymbols( ModuleProvider* MP = GetBytecodeSymbols(
(const unsigned char*)data,fSize,FullMemberName, symbols); (const unsigned char*)data,fSize,FullMemberName, symbols, ErrMsg);
// If the bytecode parsed successfully // If the bytecode parsed successfully
if ( MP ) { if ( MP ) {
@@ -247,7 +247,8 @@ Archive::writeMember(
delete mFile; delete mFile;
} }
if (ErrMsg) if (ErrMsg)
*ErrMsg = "Can't parse bytecode member: " + member.getPath().toString(); *ErrMsg = "Can't parse bytecode member: " + member.getPath().toString()
+ ": " + *ErrMsg;
return true; return true;
} }
} }

View File

@@ -35,6 +35,7 @@ namespace {
/// ///
class BytecodeFileReader : public BytecodeReader { class BytecodeFileReader : public BytecodeReader {
private: private:
std::string fileName;
sys::MappedFile mapFile; sys::MappedFile mapFile;
BytecodeFileReader(const BytecodeFileReader&); // Do not implement BytecodeFileReader(const BytecodeFileReader&); // Do not implement
@@ -42,23 +43,30 @@ namespace {
public: public:
BytecodeFileReader(const std::string &Filename, llvm::BytecodeHandler* H=0); BytecodeFileReader(const std::string &Filename, llvm::BytecodeHandler* H=0);
bool read(std::string* ErrMsg);
}; };
} }
BytecodeFileReader::BytecodeFileReader(const std::string &Filename, BytecodeFileReader::BytecodeFileReader(const std::string &Filename,
llvm::BytecodeHandler* H ) llvm::BytecodeHandler* H )
: BytecodeReader(H) : BytecodeReader(H)
, fileName(Filename)
, mapFile() , mapFile()
{ {
std::string ErrMsg; }
if (mapFile.open(sys::Path(Filename), sys::MappedFile::READ_ACCESS, &ErrMsg))
throw ErrMsg; bool BytecodeFileReader::read(std::string* ErrMsg) {
if (!mapFile.map(&ErrMsg)) if (mapFile.open(sys::Path(fileName), sys::MappedFile::READ_ACCESS, ErrMsg))
throw ErrMsg; return true;
unsigned char* buffer = reinterpret_cast<unsigned char*>(mapFile.base()); if (!mapFile.map(ErrMsg)) {
if (ParseBytecode(buffer, mapFile.size(), Filename, &ErrMsg)) { mapFile.close();
throw ErrMsg; return true;
} }
unsigned char* buffer = reinterpret_cast<unsigned char*>(mapFile.base());
if (ParseBytecode(buffer, mapFile.size(), fileName, ErrMsg)) {
return true;
}
return false;
} }
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
@@ -71,6 +79,9 @@ namespace {
class BytecodeBufferReader : public BytecodeReader { class BytecodeBufferReader : public BytecodeReader {
private: private:
const unsigned char *Buffer; const unsigned char *Buffer;
const unsigned char *Buf;
unsigned Length;
std::string ModuleID;
bool MustDelete; bool MustDelete;
BytecodeBufferReader(const BytecodeBufferReader&); // Do not implement BytecodeBufferReader(const BytecodeBufferReader&); // Do not implement
@@ -82,15 +93,30 @@ namespace {
llvm::BytecodeHandler* Handler = 0); llvm::BytecodeHandler* Handler = 0);
~BytecodeBufferReader(); ~BytecodeBufferReader();
bool read(std::string* ErrMsg);
}; };
} }
BytecodeBufferReader::BytecodeBufferReader(const unsigned char *Buf, BytecodeBufferReader::BytecodeBufferReader(const unsigned char *buf,
unsigned Length, unsigned len,
const std::string &ModuleID, const std::string &modID,
llvm::BytecodeHandler* H ) llvm::BytecodeHandler* H)
: BytecodeReader(H) : BytecodeReader(H)
, Buffer(0)
, Buf(buf)
, Length(len)
, ModuleID(modID)
, MustDelete(false)
{ {
}
BytecodeBufferReader::~BytecodeBufferReader() {
if (MustDelete) delete [] Buffer;
}
bool
BytecodeBufferReader::read(std::string* ErrMsg) {
// If not aligned, allocate a new buffer to hold the bytecode... // If not aligned, allocate a new buffer to hold the bytecode...
const unsigned char *ParseBegin = 0; const unsigned char *ParseBegin = 0;
if (reinterpret_cast<uint64_t>(Buf) & 3) { if (reinterpret_cast<uint64_t>(Buf) & 3) {
@@ -104,15 +130,11 @@ BytecodeBufferReader::BytecodeBufferReader(const unsigned char *Buf,
ParseBegin = Buffer = Buf; ParseBegin = Buffer = Buf;
MustDelete = false; MustDelete = false;
} }
std::string ErrMsg; if (ParseBytecode(ParseBegin, Length, ModuleID, ErrMsg)) {
if (ParseBytecode(ParseBegin, Length, ModuleID, &ErrMsg)) {
if (MustDelete) delete [] Buffer; if (MustDelete) delete [] Buffer;
throw ErrMsg; return true;
} }
} return false;
BytecodeBufferReader::~BytecodeBufferReader() {
if (MustDelete) delete [] Buffer;
} }
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
@@ -132,11 +154,17 @@ namespace {
public: public:
BytecodeStdinReader( llvm::BytecodeHandler* H = 0 ); BytecodeStdinReader( llvm::BytecodeHandler* H = 0 );
bool read(std::string* ErrMsg);
}; };
} }
BytecodeStdinReader::BytecodeStdinReader( BytecodeHandler* H ) BytecodeStdinReader::BytecodeStdinReader( BytecodeHandler* H )
: BytecodeReader(H) : BytecodeReader(H)
{
}
bool
BytecodeStdinReader::read(std::string* ErrMsg)
{ {
sys::Program::ChangeStdinToBinary(); sys::Program::ChangeStdinToBinary();
char Buffer[4096*4]; char Buffer[4096*4];
@@ -150,14 +178,16 @@ BytecodeStdinReader::BytecodeStdinReader( BytecodeHandler* H )
FileData.insert(FileData.end(), Buffer, Buffer+BlockSize); FileData.insert(FileData.end(), Buffer, Buffer+BlockSize);
} }
if (FileData.empty()) if (FileData.empty()) {
throw std::string("Standard Input empty!"); if (ErrMsg)
*ErrMsg = "Standard Input is empty!";
return true;
}
FileBuf = &FileData[0]; FileBuf = &FileData[0];
std::string ErrMsg; if (ParseBytecode(FileBuf, FileData.size(), "<stdin>", ErrMsg))
if (ParseBytecode(FileBuf, FileData.size(), "<stdin>", &ErrMsg)) { return true;
throw ErrMsg; return false;
}
} }
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
@@ -270,66 +300,71 @@ ModuleProvider*
llvm::getBytecodeBufferModuleProvider(const unsigned char *Buffer, llvm::getBytecodeBufferModuleProvider(const unsigned char *Buffer,
unsigned Length, unsigned Length,
const std::string &ModuleID, const std::string &ModuleID,
BytecodeHandler* H ) { std::string* ErrMsg,
return CheckVarargs( BytecodeHandler* H) {
new BytecodeBufferReader(Buffer, Length, ModuleID, H)); BytecodeBufferReader* rdr =
new BytecodeBufferReader(Buffer, Length, ModuleID, H);
if (rdr->read(ErrMsg))
return 0;
return CheckVarargs(rdr);
} }
/// ParseBytecodeBuffer - Parse a given bytecode buffer /// ParseBytecodeBuffer - Parse a given bytecode buffer
/// ///
Module *llvm::ParseBytecodeBuffer(const unsigned char *Buffer, unsigned Length, Module *llvm::ParseBytecodeBuffer(const unsigned char *Buffer, unsigned Length,
const std::string &ModuleID, const std::string &ModuleID,
std::string *ErrorStr){ std::string *ErrMsg){
try { ModuleProvider* MP =
std::auto_ptr<ModuleProvider> getBytecodeBufferModuleProvider(Buffer, Length, ModuleID, ErrMsg, 0);
AMP(getBytecodeBufferModuleProvider(Buffer, Length, ModuleID)); if (!MP)
return AMP->releaseModule();
} catch (std::string &err) {
if (ErrorStr) *ErrorStr = err;
return 0; return 0;
} return MP->releaseModule();
} }
/// getBytecodeModuleProvider - lazy function-at-a-time loading from a file /// getBytecodeModuleProvider - lazy function-at-a-time loading from a file
/// ///
ModuleProvider *llvm::getBytecodeModuleProvider(const std::string &Filename, ModuleProvider *
BytecodeHandler* H) { llvm::getBytecodeModuleProvider(const std::string &Filename,
if (Filename != std::string("-")) // Read from a file... std::string* ErrMsg,
return CheckVarargs(new BytecodeFileReader(Filename,H)); BytecodeHandler* H) {
else // Read from stdin // Read from a file
return CheckVarargs(new BytecodeStdinReader(H)); if (Filename != std::string("-")) {
BytecodeFileReader* rdr = new BytecodeFileReader(Filename, H);
if (rdr->read(ErrMsg))
return 0;
return CheckVarargs(rdr);
}
// Read from stdin
BytecodeStdinReader* rdr = new BytecodeStdinReader(H);
if (rdr->read(ErrMsg))
return 0;
return CheckVarargs(rdr);
} }
/// ParseBytecodeFile - Parse the given bytecode file /// ParseBytecodeFile - Parse the given bytecode file
/// ///
Module *llvm::ParseBytecodeFile(const std::string &Filename, Module *llvm::ParseBytecodeFile(const std::string &Filename,
std::string *ErrorStr) { std::string *ErrMsg) {
try { ModuleProvider* MP = getBytecodeModuleProvider(Filename, ErrMsg);
std::auto_ptr<ModuleProvider> AMP(getBytecodeModuleProvider(Filename)); if (!MP)
return AMP->releaseModule();
} catch (std::string &err) {
if (ErrorStr) *ErrorStr = err;
return 0; return 0;
} return MP->releaseModule();
} }
// AnalyzeBytecodeFile - analyze one file // AnalyzeBytecodeFile - analyze one file
Module* llvm::AnalyzeBytecodeFile( Module* llvm::AnalyzeBytecodeFile(
const std::string &Filename, ///< File to analyze const std::string &Filename, ///< File to analyze
BytecodeAnalysis& bca, ///< Statistical output BytecodeAnalysis& bca, ///< Statistical output
std::string *ErrorStr, ///< Error output std::string *ErrMsg, ///< Error output
std::ostream* output ///< Dump output std::ostream* output ///< Dump output
) )
{ {
try { BytecodeHandler* AH = createBytecodeAnalyzerHandler(bca,output);
BytecodeHandler* analyzerHandler =createBytecodeAnalyzerHandler(bca,output); ModuleProvider* MP = getBytecodeModuleProvider(Filename, ErrMsg, AH);
std::auto_ptr<ModuleProvider> AMP( if (!MP)
getBytecodeModuleProvider(Filename,analyzerHandler));
return AMP->releaseModule();
} catch (std::string &err) {
if (ErrorStr) *ErrorStr = err;
return 0; return 0;
} return MP->releaseModule();
} }
// AnalyzeBytecodeBuffer - analyze a buffer // AnalyzeBytecodeBuffer - analyze a buffer
@@ -338,34 +373,30 @@ Module* llvm::AnalyzeBytecodeBuffer(
unsigned Length, ///< Size of the bytecode buffer unsigned Length, ///< Size of the bytecode buffer
const std::string& ModuleID, ///< Identifier for the module const std::string& ModuleID, ///< Identifier for the module
BytecodeAnalysis& bca, ///< The results of the analysis BytecodeAnalysis& bca, ///< The results of the analysis
std::string* ErrorStr, ///< Errors, if any. std::string* ErrMsg, ///< Errors, if any.
std::ostream* output ///< Dump output, if any std::ostream* output ///< Dump output, if any
) )
{ {
try { BytecodeHandler* hdlr = createBytecodeAnalyzerHandler(bca, output);
BytecodeHandler* hdlr = createBytecodeAnalyzerHandler(bca, output); ModuleProvider* MP =
std::auto_ptr<ModuleProvider> getBytecodeBufferModuleProvider(Buffer, Length, ModuleID, ErrMsg, hdlr);
AMP(getBytecodeBufferModuleProvider(Buffer, Length, ModuleID, hdlr)); if (!MP)
return AMP->releaseModule();
} catch (std::string &err) {
if (ErrorStr) *ErrorStr = err;
return 0; return 0;
} return MP->releaseModule();
} }
bool llvm::GetBytecodeDependentLibraries(const std::string &fname, bool llvm::GetBytecodeDependentLibraries(const std::string &fname,
Module::LibraryListType& deplibs) { Module::LibraryListType& deplibs,
try { std::string* ErrMsg) {
std::auto_ptr<ModuleProvider> AMP( getBytecodeModuleProvider(fname)); ModuleProvider* MP = getBytecodeModuleProvider(fname, ErrMsg);
Module* M = AMP->releaseModule(); if (!MP) {
deplibs = M->getLibraries();
delete M;
return true;
} catch (...) {
deplibs.clear(); deplibs.clear();
return false; return true;
} }
Module* M = MP->releaseModule();
deplibs = M->getLibraries();
delete M;
return false;
} }
static void getSymbols(Module*M, std::vector<std::string>& symbols) { static void getSymbols(Module*M, std::vector<std::string>& symbols) {
@@ -384,13 +415,16 @@ static void getSymbols(Module*M, std::vector<std::string>& symbols) {
// Get just the externally visible defined symbols from the bytecode // Get just the externally visible defined symbols from the bytecode
bool llvm::GetBytecodeSymbols(const sys::Path& fName, bool llvm::GetBytecodeSymbols(const sys::Path& fName,
std::vector<std::string>& symbols) { std::vector<std::string>& symbols,
std::auto_ptr<ModuleProvider> AMP( std::string* ErrMsg) {
getBytecodeModuleProvider(fName.toString())); ModuleProvider* MP = getBytecodeModuleProvider(fName.toString(), ErrMsg);
if (!MP)
return true;
// Get the module from the provider // Get the module from the provider
Module* M = AMP->materializeModule(); Module* M = MP->materializeModule();
if (M == 0) return false; if (M == 0)
return true;
// Get the symbols // Get the symbols
getSymbols(M, symbols); getSymbols(M, symbols);
@@ -402,29 +436,26 @@ bool llvm::GetBytecodeSymbols(const sys::Path& fName,
ModuleProvider* ModuleProvider*
llvm::GetBytecodeSymbols(const unsigned char*Buffer, unsigned Length, llvm::GetBytecodeSymbols(const unsigned char*Buffer, unsigned Length,
const std::string& ModuleID, const std::string& ModuleID,
std::vector<std::string>& symbols) { std::vector<std::string>& symbols,
std::string* ErrMsg) {
// Get the module provider
ModuleProvider* MP =
getBytecodeBufferModuleProvider(Buffer, Length, ModuleID, ErrMsg, 0);
if (!MP)
return 0;
ModuleProvider* MP = 0; // Get the module from the provider
try { Module* M = MP->materializeModule();
// Get the module provider if (M == 0) {
MP = getBytecodeBufferModuleProvider(Buffer, Length, ModuleID);
// Get the module from the provider
Module* M = MP->materializeModule();
if (M == 0) return 0;
// Get the symbols
getSymbols(M, symbols);
// Done with the module. Note that ModuleProvider will delete the
// Module when it is deleted. Also note that its the caller's responsibility
// to delete the ModuleProvider.
return MP;
} catch (...) {
// We delete only the ModuleProvider here because its destructor will
// also delete the Module (we used materializeModule not releaseModule).
delete MP; delete MP;
return 0;
} }
return 0;
// Get the symbols
getSymbols(M, symbols);
// Done with the module. Note that ModuleProvider will delete the
// Module when it is deleted. Also note that its the caller's responsibility
// to delete the ModuleProvider.
return MP;
} }

View File

@@ -45,15 +45,7 @@ std::string Debugger::getProgramPath() const {
static Module * static Module *
getMaterializedModuleProvider(const std::string &Filename) { getMaterializedModuleProvider(const std::string &Filename) {
try { return ParseBytecodeFile(Filename);
std::auto_ptr<ModuleProvider> Result(getBytecodeModuleProvider(Filename));
if (!Result.get()) return 0;
Result->materializeModule();
return Result.release()->releaseModule();
} catch (...) {
return 0;
}
} }
/// loadProgram - If a program is currently loaded, unload it. Then search /// loadProgram - If a program is currently loaded, unload it. Then search

View File

@@ -59,11 +59,10 @@ int main(int argc, char **argv, char * const *envp) {
// Load the bytecode... // Load the bytecode...
std::string ErrorMsg; std::string ErrorMsg;
ModuleProvider *MP = 0; ModuleProvider *MP = 0;
try { MP = getBytecodeModuleProvider(InputFile, &ErrorMsg);
MP = getBytecodeModuleProvider(InputFile); if (!MP) {
} catch (std::string &err) {
std::cerr << "Error loading program '" << InputFile << "': " std::cerr << "Error loading program '" << InputFile << "': "
<< err << "\n"; << ErrorMsg << "\n";
exit(1); exit(1);
} }

View File

@@ -576,7 +576,7 @@ private:
if (fullpath.isBytecodeFile()) { if (fullpath.isBytecodeFile()) {
// Process the dependent libraries recursively // Process the dependent libraries recursively
Module::LibraryListType modlibs; Module::LibraryListType modlibs;
if (GetBytecodeDependentLibraries(fullpath.toString(),modlibs)) { if (GetBytecodeDependentLibraries(fullpath.toString(),modlibs,&err)) {
// Traverse the dependent libraries list // Traverse the dependent libraries list
Module::lib_iterator LI = modlibs.begin(); Module::lib_iterator LI = modlibs.begin();
Module::lib_iterator LE = modlibs.end(); Module::lib_iterator LE = modlibs.end();
@@ -598,7 +598,8 @@ private:
"The dependent libraries could not be extracted from '") + "The dependent libraries could not be extracted from '") +
fullpath.toString(); fullpath.toString();
return false; return false;
} } else
return false;
} }
return true; return true;
} }