From 0b5a504d105514178c80b886321221fbe5ac1131 Mon Sep 17 00:00:00 2001 From: Reid Spencer Date: Fri, 25 Aug 2006 17:43:11 +0000 Subject: 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 --- include/llvm/Bytecode/Reader.h | 76 +++++++---- include/llvm/Constants.h | 194 ++++++++++++++++----------- lib/Archive/ArchiveReader.cpp | 10 +- lib/Archive/ArchiveWriter.cpp | 5 +- lib/Bytecode/Archive/ArchiveReader.cpp | 10 +- lib/Bytecode/Archive/ArchiveWriter.cpp | 5 +- lib/Bytecode/Reader/ReaderWrappers.cpp | 237 +++++++++++++++++++-------------- lib/Debugger/Debugger.cpp | 10 +- tools/lli/lli.cpp | 7 +- tools/llvmc/CompilerDriver.cpp | 5 +- 10 files changed, 325 insertions(+), 234 deletions(-) diff --git a/include/llvm/Bytecode/Reader.h b/include/llvm/Bytecode/Reader.h index c691ed05444..b69932ba013 100644 --- a/include/llvm/Bytecode/Reader.h +++ b/include/llvm/Bytecode/Reader.h @@ -29,48 +29,75 @@ namespace llvm { // Forward declare the handler class 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( - const std::string &Filename, ///< Name of file to be read - BytecodeHandler* H = 0 ///< Optional handler for reader events + const std::string &Filename, ///< Name of file to be read + std::string* ErrMsg, ///< Optional error message holder + BytecodeHandler* H = 0 ///< Optional handler for reader events ); -/// getBytecodeBufferModuleProvider - lazy function-at-a-time loading from a -/// buffer -/// -ModuleProvider *getBytecodeBufferModuleProvider(const unsigned char *Buffer, - unsigned BufferSize, - const std::string &ModuleID="", - BytecodeHandler* H = 0); +/// This function returns a ModuleProvider that can be used to do lazy +/// function function-at-a-time loading from a bytecode buffer. +/// @returns NULL on error +/// @returns ModuleProvider* if successful +/// @brief Get a ModuleProvider for a bytecode buffer. +ModuleProvider *getBytecodeBufferModuleProvider( + 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 -Module* ParseBytecodeFile(const std::string &Filename, - std::string *ErrorStr = 0); +Module* ParseBytecodeFile( + 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 -Module* ParseBytecodeBuffer(const unsigned char *Buffer, - unsigned BufferSize, - const std::string &ModuleID = "", - std::string *ErrorStr = 0); +Module* ParseBytecodeBuffer( + 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 = 0 ///< Optional place to return an error message +); /// 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 /// deplibs parameter will contain a vector of strings of the bytecode module's /// 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. -bool GetBytecodeDependentLibraries(const std::string &fileName, - Module::LibraryListType& deplibs); +bool GetBytecodeDependentLibraries( + 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 /// 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 /// 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. -bool GetBytecodeSymbols(const sys::Path& fileName, - std::vector& syms); +bool GetBytecodeSymbols( + const sys::Path& fileName, ///< Filename to read bytecode from + std::vector& 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 /// 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 unsigned Length, ///< The length of \p Buffer const std::string& ModuleID, ///< An identifier for the module - std::vector& symbols ///< The symbols defined in the module + std::vector& symbols, ///< The symbols defined in the module + std::string* ErrMsg ///< Optional error message holder ); } // End llvm namespace diff --git a/include/llvm/Constants.h b/include/llvm/Constants.h index ef06c33ced9..e0530290d12 100644 --- a/include/llvm/Constants.h +++ b/include/llvm/Constants.h @@ -7,13 +7,13 @@ // //===----------------------------------------------------------------------===// // -// This file contains the declarations for the subclasses of Constant, which -// represent the different flavors of constant values that live in LLVM. Note -// that Constants are immutable (once created they never change) and are fully -// shared by structural equivalence. This means that two structurally -// 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 -// worry about the lifetime of the objects. +/// @file This file contains the declarations for the subclasses of Constant, +/// which represent the different flavors of constant values that live in LLVM. +/// Note that Constants are immutable (once created they never change) and are +/// fully shared by structural equivalence. This means that two structurally +/// 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 +/// worry about the lifetime of the objects. // //===----------------------------------------------------------------------===// @@ -36,10 +36,9 @@ template struct ConvertConstantType; //===----------------------------------------------------------------------===// -/// ConstantIntegral - Shared superclass of boolean and integer constants. -/// -/// This class just defines some common interfaces to be implemented. -/// +/// This is the shared superclass of boolean and integer constants. This class +/// just defines some common interfaces to be implemented by the subclasses. +/// @brief An abstract class for integer constants. class ConstantIntegral : public Constant { protected: union { @@ -49,52 +48,66 @@ protected: ConstantIntegral(const Type *Ty, ValueTy VT, uint64_t V); public: - /// getRawValue - return the underlying value of this constant as a 64-bit - /// unsigned integer value. - /// + /// @brief Return the raw value of the constant as a 64-bit integer value. inline uint64_t getRawValue() const { return Val.Unsigned; } - /// getZExtValue - Return the constant zero extended as appropriate for this - /// type. + /// Return the constant as a 64-bit unsigned integer value after it + /// has been zero extended as appropriate for the type of this constant. + /// @brief Return the zero extended value. inline uint64_t getZExtValue() const { unsigned Size = getType()->getPrimitiveSizeInBits(); return Val.Unsigned & (~uint64_t(0UL) >> (64-Size)); } - /// getSExtValue - Return the constant sign extended as appropriate for this - /// type. + /// Return the constant as a 64-bit integer value after it has been sign + /// sign extended as appropriate for the type of this constant. + /// @brief REturn the sign extended value. inline int64_t getSExtValue() const { unsigned Size = getType()->getPrimitiveSizeInBits(); return (Val.Signed << (64-Size)) >> (64-Size); } - /// isNullValue - Return true if this is the value that would be returned by - /// getNullValue. - /// + /// This function is implemented by subclasses and will return true iff this + /// 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; - /// isMaxValue - Return true if this is the largest value that may be - /// represented by this type. - /// + /// This function is implemented by sublcasses and will return true iff this + /// 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; - /// isMinValue - Return true if this is the smallest value that may be - /// represented by this type. - /// + /// This function is implemented by subclasses and will return true iff this + /// 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; - /// 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; - /// Static constructor to get the maximum/minimum/allones constant of - /// specified (integral) type... - /// + /// @returns the largest value for an integer constant of the given type + /// @brief Get the maximal value 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); + + /// @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); - /// 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 bool classof(const Value *V) { 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 { ConstantBool(bool V); 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; } + + /// 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); } - /// 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; } - /// 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(getRawValue()); } - /// isNullValue - Return true if this is the value that would be returned by - /// getNullValue. - /// + /// @see ConstantIntegral for details + /// @brief Implement overrides virtual bool isNullValue() const { return this == False; } virtual bool isMaxValue() const { return this == True; } virtual bool isMinValue() const { return this == False; } 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 bool classof(const Value *V) { return V->getValueType() == ConstantBoolVal; @@ -140,37 +161,36 @@ public: //===----------------------------------------------------------------------===// -/// ConstantInt - Superclass of ConstantSInt & ConstantUInt, to make dealing -/// with integral constants easier. -/// +/// This is the abstract superclass of ConstantSInt & ConstantUInt, to make +/// dealing with integral constants easier when sign is irrelevant. +/// @brief Abstract clas for constant integers. class ConstantInt : public ConstantIntegral { protected: ConstantInt(const ConstantInt &); // DO NOT IMPLEMENT ConstantInt(const Type *Ty, ValueTy VT, uint64_t V); public: - /// equalsInt - Provide a helper method that can be used to determine if the - /// constant contained within is equal to a constant. This only works for - /// very small values, because this is all that can be represented with all - /// types. - /// + /// A helper method that can be used to determine if the constant contained + /// within is equal to a constant. This only works for very small values, + /// because this is all that can be represented with all types. + /// @brief Determine if this constant's value is same as an unsigned char. bool equalsInt(unsigned char V) const { assert(V <= 127 && "equalsInt: Can only be used with very small positive constants!"); return Val.Unsigned == V; } - /// ConstantInt::get static method: return a ConstantInt with the specified - /// value. as above, we work only with very small values here. - /// + /// Return a ConstantInt with the specified value for the specified type. + /// 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); - /// isNullValue - Return true if this is the value that would be returned by - /// getNullValue. + /// @returns true if this is the null integer value. + /// @see ConstantIntegral for details + /// @brief Implement override. 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 bool classof(const Value *V) { 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 { ConstantSInt(const ConstantSInt &); // DO NOT IMPLEMENT friend struct ConstantCreator; @@ -189,23 +210,35 @@ class ConstantSInt : public ConstantInt { protected: ConstantSInt(const Type *Ty, int64_t V); public: - /// get() - Static factory methods - Return objects of the specified value - /// - static ConstantSInt *get(const Type *Ty, int64_t V); - - /// isValueValidForType - return true if Ty is big enough to represent V. - /// + /// This static factory methods returns objects of the specified value. Note + /// that repeated calls with the same operands return the same object. + /// @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. + ); + + /// 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); - /// 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; } + /// @returns true iff this constant's bits are all set to true. + /// @see ConstantIntegral + /// @brief Override implementation virtual bool isAllOnesValue() const { return getValue() == -1; } - /// isMaxValue - Return true if this is the largest value that may be - /// represented by this type. - /// + /// @returns true iff this is the largest value that may be represented + /// by this type. + /// @see ConstantIntegeral + /// @brief Override implementation virtual bool isMaxValue() const { int64_t V = getValue(); if (V < 0) return false; // Be careful about wrap-around on 'long's @@ -213,9 +246,10 @@ public: return !isValueValidForType(getType(), V) || V < 0; } - /// isMinValue - Return true if this is the smallest value that may be - /// represented by this type. - /// + /// @returns true if this is the smallest value that may be represented by + /// this type. + /// @see ConstantIntegral + /// @brief Override implementation virtual bool isMinValue() const { int64_t V = getValue(); if (V > 0) return false; // Be careful about wrap-around on 'long's @@ -223,8 +257,7 @@ public: 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 bool classof(const Value *V) { 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 { ConstantUInt(const ConstantUInt &); // DO NOT IMPLEMENT friend struct ConstantCreator; diff --git a/lib/Archive/ArchiveReader.cpp b/lib/Archive/ArchiveReader.cpp index 38aa07243c7..e4769cac888 100644 --- a/lib/Archive/ArchiveReader.cpp +++ b/lib/Archive/ArchiveReader.cpp @@ -475,14 +475,16 @@ Archive::findModuleDefiningSymbol(const std::string& symbol, const char* modptr = base + fileOffset; ArchiveMember* mbr = parseMemberHeader(modptr, base + mapfile->size(),ErrMsg); if (!mbr) - return false; + return 0; // Now, load the bytecode module to get the ModuleProvider std::string FullMemberName = archPath.toString() + "(" + mbr->getPath().toString() + ")"; ModuleProvider* mp = getBytecodeBufferModuleProvider( (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))); @@ -523,7 +525,7 @@ Archive::findModulesDefiningSymbols(std::set& symbols, std::string FullMemberName = archPath.toString() + "(" + mbr->getPath().toString() + ")"; ModuleProvider* MP = GetBytecodeSymbols((const unsigned char*)At, - mbr->getSize(), FullMemberName, symbols); + mbr->getSize(), FullMemberName, symbols, error); if (MP) { // Insert the module's symbols into the symbol table @@ -537,7 +539,7 @@ Archive::findModulesDefiningSymbols(std::set& symbols, } else { if (error) *error = "Can't parse bytecode member: " + - mbr->getPath().toString(); + mbr->getPath().toString() + ": " + *error; delete mbr; return false; } diff --git a/lib/Archive/ArchiveWriter.cpp b/lib/Archive/ArchiveWriter.cpp index 3746dbff8a1..096a9993a81 100644 --- a/lib/Archive/ArchiveWriter.cpp +++ b/lib/Archive/ArchiveWriter.cpp @@ -223,7 +223,7 @@ Archive::writeMember( member.getPath().toString() + ")"; ModuleProvider* MP = GetBytecodeSymbols( - (const unsigned char*)data,fSize,FullMemberName, symbols); + (const unsigned char*)data,fSize,FullMemberName, symbols, ErrMsg); // If the bytecode parsed successfully if ( MP ) { @@ -247,7 +247,8 @@ Archive::writeMember( delete mFile; } if (ErrMsg) - *ErrMsg = "Can't parse bytecode member: " + member.getPath().toString(); + *ErrMsg = "Can't parse bytecode member: " + member.getPath().toString() + + ": " + *ErrMsg; return true; } } diff --git a/lib/Bytecode/Archive/ArchiveReader.cpp b/lib/Bytecode/Archive/ArchiveReader.cpp index 38aa07243c7..e4769cac888 100644 --- a/lib/Bytecode/Archive/ArchiveReader.cpp +++ b/lib/Bytecode/Archive/ArchiveReader.cpp @@ -475,14 +475,16 @@ Archive::findModuleDefiningSymbol(const std::string& symbol, const char* modptr = base + fileOffset; ArchiveMember* mbr = parseMemberHeader(modptr, base + mapfile->size(),ErrMsg); if (!mbr) - return false; + return 0; // Now, load the bytecode module to get the ModuleProvider std::string FullMemberName = archPath.toString() + "(" + mbr->getPath().toString() + ")"; ModuleProvider* mp = getBytecodeBufferModuleProvider( (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))); @@ -523,7 +525,7 @@ Archive::findModulesDefiningSymbols(std::set& symbols, std::string FullMemberName = archPath.toString() + "(" + mbr->getPath().toString() + ")"; ModuleProvider* MP = GetBytecodeSymbols((const unsigned char*)At, - mbr->getSize(), FullMemberName, symbols); + mbr->getSize(), FullMemberName, symbols, error); if (MP) { // Insert the module's symbols into the symbol table @@ -537,7 +539,7 @@ Archive::findModulesDefiningSymbols(std::set& symbols, } else { if (error) *error = "Can't parse bytecode member: " + - mbr->getPath().toString(); + mbr->getPath().toString() + ": " + *error; delete mbr; return false; } diff --git a/lib/Bytecode/Archive/ArchiveWriter.cpp b/lib/Bytecode/Archive/ArchiveWriter.cpp index 3746dbff8a1..096a9993a81 100644 --- a/lib/Bytecode/Archive/ArchiveWriter.cpp +++ b/lib/Bytecode/Archive/ArchiveWriter.cpp @@ -223,7 +223,7 @@ Archive::writeMember( member.getPath().toString() + ")"; ModuleProvider* MP = GetBytecodeSymbols( - (const unsigned char*)data,fSize,FullMemberName, symbols); + (const unsigned char*)data,fSize,FullMemberName, symbols, ErrMsg); // If the bytecode parsed successfully if ( MP ) { @@ -247,7 +247,8 @@ Archive::writeMember( delete mFile; } if (ErrMsg) - *ErrMsg = "Can't parse bytecode member: " + member.getPath().toString(); + *ErrMsg = "Can't parse bytecode member: " + member.getPath().toString() + + ": " + *ErrMsg; return true; } } diff --git a/lib/Bytecode/Reader/ReaderWrappers.cpp b/lib/Bytecode/Reader/ReaderWrappers.cpp index 17671593925..019d7bef529 100644 --- a/lib/Bytecode/Reader/ReaderWrappers.cpp +++ b/lib/Bytecode/Reader/ReaderWrappers.cpp @@ -35,6 +35,7 @@ namespace { /// class BytecodeFileReader : public BytecodeReader { private: + std::string fileName; sys::MappedFile mapFile; BytecodeFileReader(const BytecodeFileReader&); // Do not implement @@ -42,23 +43,30 @@ namespace { public: BytecodeFileReader(const std::string &Filename, llvm::BytecodeHandler* H=0); + bool read(std::string* ErrMsg); }; } BytecodeFileReader::BytecodeFileReader(const std::string &Filename, llvm::BytecodeHandler* H ) : BytecodeReader(H) + , fileName(Filename) , mapFile() { - std::string ErrMsg; - if (mapFile.open(sys::Path(Filename), sys::MappedFile::READ_ACCESS, &ErrMsg)) - throw ErrMsg; - if (!mapFile.map(&ErrMsg)) - throw ErrMsg; +} + +bool BytecodeFileReader::read(std::string* ErrMsg) { + if (mapFile.open(sys::Path(fileName), sys::MappedFile::READ_ACCESS, ErrMsg)) + return true; + if (!mapFile.map(ErrMsg)) { + mapFile.close(); + return true; + } unsigned char* buffer = reinterpret_cast(mapFile.base()); - if (ParseBytecode(buffer, mapFile.size(), Filename, &ErrMsg)) { - throw ErrMsg; + if (ParseBytecode(buffer, mapFile.size(), fileName, ErrMsg)) { + return true; } + return false; } //===----------------------------------------------------------------------===// @@ -71,6 +79,9 @@ namespace { class BytecodeBufferReader : public BytecodeReader { private: const unsigned char *Buffer; + const unsigned char *Buf; + unsigned Length; + std::string ModuleID; bool MustDelete; BytecodeBufferReader(const BytecodeBufferReader&); // Do not implement @@ -82,15 +93,30 @@ namespace { llvm::BytecodeHandler* Handler = 0); ~BytecodeBufferReader(); + bool read(std::string* ErrMsg); + }; } -BytecodeBufferReader::BytecodeBufferReader(const unsigned char *Buf, - unsigned Length, - const std::string &ModuleID, - llvm::BytecodeHandler* H ) +BytecodeBufferReader::BytecodeBufferReader(const unsigned char *buf, + unsigned len, + const std::string &modID, + llvm::BytecodeHandler* 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... const unsigned char *ParseBegin = 0; if (reinterpret_cast(Buf) & 3) { @@ -104,15 +130,11 @@ BytecodeBufferReader::BytecodeBufferReader(const unsigned char *Buf, ParseBegin = Buffer = Buf; MustDelete = false; } - std::string ErrMsg; - if (ParseBytecode(ParseBegin, Length, ModuleID, &ErrMsg)) { + if (ParseBytecode(ParseBegin, Length, ModuleID, ErrMsg)) { if (MustDelete) delete [] Buffer; - throw ErrMsg; + return true; } -} - -BytecodeBufferReader::~BytecodeBufferReader() { - if (MustDelete) delete [] Buffer; + return false; } //===----------------------------------------------------------------------===// @@ -132,11 +154,17 @@ namespace { public: BytecodeStdinReader( llvm::BytecodeHandler* H = 0 ); + bool read(std::string* ErrMsg); }; } BytecodeStdinReader::BytecodeStdinReader( BytecodeHandler* H ) : BytecodeReader(H) +{ +} + +bool +BytecodeStdinReader::read(std::string* ErrMsg) { sys::Program::ChangeStdinToBinary(); char Buffer[4096*4]; @@ -150,14 +178,16 @@ BytecodeStdinReader::BytecodeStdinReader( BytecodeHandler* H ) FileData.insert(FileData.end(), Buffer, Buffer+BlockSize); } - if (FileData.empty()) - throw std::string("Standard Input empty!"); + if (FileData.empty()) { + if (ErrMsg) + *ErrMsg = "Standard Input is empty!"; + return true; + } FileBuf = &FileData[0]; - std::string ErrMsg; - if (ParseBytecode(FileBuf, FileData.size(), "", &ErrMsg)) { - throw ErrMsg; - } + if (ParseBytecode(FileBuf, FileData.size(), "", ErrMsg)) + return true; + return false; } //===----------------------------------------------------------------------===// @@ -270,66 +300,71 @@ ModuleProvider* llvm::getBytecodeBufferModuleProvider(const unsigned char *Buffer, unsigned Length, const std::string &ModuleID, - BytecodeHandler* H ) { - return CheckVarargs( - new BytecodeBufferReader(Buffer, Length, ModuleID, H)); + std::string* ErrMsg, + BytecodeHandler* H) { + BytecodeBufferReader* rdr = + new BytecodeBufferReader(Buffer, Length, ModuleID, H); + if (rdr->read(ErrMsg)) + return 0; + return CheckVarargs(rdr); } /// ParseBytecodeBuffer - Parse a given bytecode buffer /// Module *llvm::ParseBytecodeBuffer(const unsigned char *Buffer, unsigned Length, const std::string &ModuleID, - std::string *ErrorStr){ - try { - std::auto_ptr - AMP(getBytecodeBufferModuleProvider(Buffer, Length, ModuleID)); - return AMP->releaseModule(); - } catch (std::string &err) { - if (ErrorStr) *ErrorStr = err; + std::string *ErrMsg){ + ModuleProvider* MP = + getBytecodeBufferModuleProvider(Buffer, Length, ModuleID, ErrMsg, 0); + if (!MP) return 0; - } + return MP->releaseModule(); } /// getBytecodeModuleProvider - lazy function-at-a-time loading from a file /// -ModuleProvider *llvm::getBytecodeModuleProvider(const std::string &Filename, - BytecodeHandler* H) { - if (Filename != std::string("-")) // Read from a file... - return CheckVarargs(new BytecodeFileReader(Filename,H)); - else // Read from stdin - return CheckVarargs(new BytecodeStdinReader(H)); +ModuleProvider * +llvm::getBytecodeModuleProvider(const std::string &Filename, + std::string* ErrMsg, + BytecodeHandler* H) { + // Read from a file + 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 /// Module *llvm::ParseBytecodeFile(const std::string &Filename, - std::string *ErrorStr) { - try { - std::auto_ptr AMP(getBytecodeModuleProvider(Filename)); - return AMP->releaseModule(); - } catch (std::string &err) { - if (ErrorStr) *ErrorStr = err; + std::string *ErrMsg) { + ModuleProvider* MP = getBytecodeModuleProvider(Filename, ErrMsg); + if (!MP) return 0; - } + return MP->releaseModule(); } // AnalyzeBytecodeFile - analyze one file Module* llvm::AnalyzeBytecodeFile( const std::string &Filename, ///< File to analyze BytecodeAnalysis& bca, ///< Statistical output - std::string *ErrorStr, ///< Error output + std::string *ErrMsg, ///< Error output std::ostream* output ///< Dump output ) { - try { - BytecodeHandler* analyzerHandler =createBytecodeAnalyzerHandler(bca,output); - std::auto_ptr AMP( - getBytecodeModuleProvider(Filename,analyzerHandler)); - return AMP->releaseModule(); - } catch (std::string &err) { - if (ErrorStr) *ErrorStr = err; + BytecodeHandler* AH = createBytecodeAnalyzerHandler(bca,output); + ModuleProvider* MP = getBytecodeModuleProvider(Filename, ErrMsg, AH); + if (!MP) return 0; - } + return MP->releaseModule(); } // AnalyzeBytecodeBuffer - analyze a buffer @@ -338,34 +373,30 @@ Module* llvm::AnalyzeBytecodeBuffer( unsigned Length, ///< Size of the bytecode buffer const std::string& ModuleID, ///< Identifier for the module 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 ) { - try { - BytecodeHandler* hdlr = createBytecodeAnalyzerHandler(bca, output); - std::auto_ptr - AMP(getBytecodeBufferModuleProvider(Buffer, Length, ModuleID, hdlr)); - return AMP->releaseModule(); - } catch (std::string &err) { - if (ErrorStr) *ErrorStr = err; + BytecodeHandler* hdlr = createBytecodeAnalyzerHandler(bca, output); + ModuleProvider* MP = + getBytecodeBufferModuleProvider(Buffer, Length, ModuleID, ErrMsg, hdlr); + if (!MP) return 0; - } + return MP->releaseModule(); } bool llvm::GetBytecodeDependentLibraries(const std::string &fname, - Module::LibraryListType& deplibs) { - try { - std::auto_ptr AMP( getBytecodeModuleProvider(fname)); - Module* M = AMP->releaseModule(); - - deplibs = M->getLibraries(); - delete M; - return true; - } catch (...) { + Module::LibraryListType& deplibs, + std::string* ErrMsg) { + ModuleProvider* MP = getBytecodeModuleProvider(fname, ErrMsg); + if (!MP) { deplibs.clear(); - return false; + return true; } + Module* M = MP->releaseModule(); + deplibs = M->getLibraries(); + delete M; + return false; } static void getSymbols(Module*M, std::vector& symbols) { @@ -384,13 +415,16 @@ static void getSymbols(Module*M, std::vector& symbols) { // Get just the externally visible defined symbols from the bytecode bool llvm::GetBytecodeSymbols(const sys::Path& fName, - std::vector& symbols) { - std::auto_ptr AMP( - getBytecodeModuleProvider(fName.toString())); + std::vector& symbols, + std::string* ErrMsg) { + ModuleProvider* MP = getBytecodeModuleProvider(fName.toString(), ErrMsg); + if (!MP) + return true; // Get the module from the provider - Module* M = AMP->materializeModule(); - if (M == 0) return false; + Module* M = MP->materializeModule(); + if (M == 0) + return true; // Get the symbols getSymbols(M, symbols); @@ -402,29 +436,26 @@ bool llvm::GetBytecodeSymbols(const sys::Path& fName, ModuleProvider* llvm::GetBytecodeSymbols(const unsigned char*Buffer, unsigned Length, const std::string& ModuleID, - std::vector& symbols) { - - ModuleProvider* MP = 0; - try { - // Get the module provider - 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; + std::vector& symbols, + std::string* ErrMsg) { + // Get the module provider + ModuleProvider* MP = + getBytecodeBufferModuleProvider(Buffer, Length, ModuleID, ErrMsg, 0); + if (!MP) + return 0; - } catch (...) { - // We delete only the ModuleProvider here because its destructor will - // also delete the Module (we used materializeModule not releaseModule). + // Get the module from the provider + Module* M = MP->materializeModule(); + if (M == 0) { 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; } diff --git a/lib/Debugger/Debugger.cpp b/lib/Debugger/Debugger.cpp index a0505bda578..613a19d2f3f 100644 --- a/lib/Debugger/Debugger.cpp +++ b/lib/Debugger/Debugger.cpp @@ -45,15 +45,7 @@ std::string Debugger::getProgramPath() const { static Module * getMaterializedModuleProvider(const std::string &Filename) { - try { - std::auto_ptr Result(getBytecodeModuleProvider(Filename)); - if (!Result.get()) return 0; - - Result->materializeModule(); - return Result.release()->releaseModule(); - } catch (...) { - return 0; - } + return ParseBytecodeFile(Filename); } /// loadProgram - If a program is currently loaded, unload it. Then search diff --git a/tools/lli/lli.cpp b/tools/lli/lli.cpp index d8f2b16e57c..be9f999a546 100644 --- a/tools/lli/lli.cpp +++ b/tools/lli/lli.cpp @@ -59,11 +59,10 @@ int main(int argc, char **argv, char * const *envp) { // Load the bytecode... std::string ErrorMsg; ModuleProvider *MP = 0; - try { - MP = getBytecodeModuleProvider(InputFile); - } catch (std::string &err) { + MP = getBytecodeModuleProvider(InputFile, &ErrorMsg); + if (!MP) { std::cerr << "Error loading program '" << InputFile << "': " - << err << "\n"; + << ErrorMsg << "\n"; exit(1); } diff --git a/tools/llvmc/CompilerDriver.cpp b/tools/llvmc/CompilerDriver.cpp index a6aff4eb30a..46dbd89fc95 100644 --- a/tools/llvmc/CompilerDriver.cpp +++ b/tools/llvmc/CompilerDriver.cpp @@ -576,7 +576,7 @@ private: if (fullpath.isBytecodeFile()) { // Process the dependent libraries recursively Module::LibraryListType modlibs; - if (GetBytecodeDependentLibraries(fullpath.toString(),modlibs)) { + if (GetBytecodeDependentLibraries(fullpath.toString(),modlibs,&err)) { // Traverse the dependent libraries list Module::lib_iterator LI = modlibs.begin(); Module::lib_iterator LE = modlibs.end(); @@ -598,7 +598,8 @@ private: "The dependent libraries could not be extracted from '") + fullpath.toString(); return false; - } + } else + return false; } return true; } -- cgit v1.2.3