Commit c1f07ea7 by Jan Voung

Subzero: Switch file reading to be based on a DataStreamer and MemoryObject.

This makes it compatible with the current browser interface, which pushes bytes to a DataStreamer. In the browser-integration mode, there will be a push-based DataStreamer (vs a file-based one). BUG= https://code.google.com/p/nativeclient/issues/detail?id=4091 R=dschuff@chromium.org, kschimpf@google.com, stichnot@chromium.org Review URL: https://codereview.chromium.org/982403002
parent 68a06338
...@@ -2992,43 +2992,27 @@ bool TopLevelParser::ParseBlock(unsigned BlockID) { ...@@ -2992,43 +2992,27 @@ bool TopLevelParser::ParseBlock(unsigned BlockID) {
namespace Ice { namespace Ice {
void PNaClTranslator::translate(const std::string &IRFilename) {
ErrorOr<std::unique_ptr<MemoryBuffer>> ErrOrFile =
MemoryBuffer::getFileOrSTDIN(IRFilename);
if (std::error_code EC = ErrOrFile.getError()) {
errs() << "Error reading '" << IRFilename << "': " << EC.message() << "\n";
ErrorStatus.assign(EC.value());
return;
}
std::unique_ptr<MemoryBuffer> MemBuf(ErrOrFile.get().release());
translateBuffer(IRFilename, MemBuf.get());
}
void PNaClTranslator::translateBuffer(const std::string &IRFilename, void PNaClTranslator::translateBuffer(const std::string &IRFilename,
MemoryBuffer *MemBuf) { MemoryBuffer *MemBuf) {
if (MemBuf->getBufferSize() % 4 != 0) { std::unique_ptr<MemoryObject> MemObj(getNonStreamedMemoryObject(
errs() << IRFilename reinterpret_cast<const unsigned char *>(MemBuf->getBufferStart()),
<< ": Bitcode stream should be a multiple of 4 bytes in length.\n"; reinterpret_cast<const unsigned char *>(MemBuf->getBufferEnd())));
ErrorStatus.assign(EC_Bitcode); translate(IRFilename, std::move(MemObj));
return; }
}
const unsigned char *BufPtr = (const unsigned char *)MemBuf->getBufferStart(); void PNaClTranslator::translate(const std::string &IRFilename,
const unsigned char *HeaderPtr = BufPtr; std::unique_ptr<MemoryObject> &&MemObj) {
const unsigned char *EndBufPtr = BufPtr + MemBuf->getBufferSize();
// Read header and verify it is good. // Read header and verify it is good.
NaClBitcodeHeader Header; NaClBitcodeHeader Header;
if (Header.Read(HeaderPtr, EndBufPtr) || !Header.IsSupported()) { if (Header.Read(MemObj.get()) || !Header.IsSupported()) {
errs() << "Invalid PNaCl bitcode header.\n"; errs() << "Invalid PNaCl bitcode header.\n";
ErrorStatus.assign(EC_Bitcode); ErrorStatus.assign(EC_Bitcode);
return; return;
} }
// Create a bitstream reader to read the bitcode file. // Create a bitstream reader to read the bitcode file.
NaClBitstreamReader InputStreamFile(BufPtr, EndBufPtr, NaClBitstreamReader InputStreamFile(MemObj.release(), Header.getHeaderSize());
Header.getHeaderSize());
NaClBitstreamCursor InputStream(InputStreamFile); NaClBitstreamCursor InputStream(InputStreamFile);
TopLevelParser Parser(*this, InputStream, ErrorStatus); TopLevelParser Parser(*this, InputStream, ErrorStatus);
...@@ -3047,6 +3031,12 @@ void PNaClTranslator::translateBuffer(const std::string &IRFilename, ...@@ -3047,6 +3031,12 @@ void PNaClTranslator::translateBuffer(const std::string &IRFilename,
<< "\n"; << "\n";
ErrorStatus.assign(EC_Bitcode); ErrorStatus.assign(EC_Bitcode);
} }
if (InputStreamFile.getBitcodeBytes().getExtent() % 4 != 0) {
errs() << IRFilename
<< ": Bitcode stream should be a multiple of 4 bytes in length.\n";
ErrorStatus.assign(EC_Bitcode);
return;
}
} }
} // end of namespace Ice } // end of namespace Ice
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
namespace llvm { namespace llvm {
class MemoryBuffer; class MemoryBuffer;
class MemoryObject;
} // end of namespace llvm } // end of namespace llvm
namespace Ice { namespace Ice {
...@@ -35,8 +36,9 @@ public: ...@@ -35,8 +36,9 @@ public:
// Reads the PNaCl bitcode file and translates to ICE, which is then // Reads the PNaCl bitcode file and translates to ICE, which is then
// converted to machine code. Sets ErrorStatus to 1 if any errors // converted to machine code. Sets ErrorStatus to 1 if any errors
// occurred. // occurred. Takes ownership of the MemoryObject.
void translate(const std::string &IRFilename); void translate(const std::string &IRFilename,
std::unique_ptr<llvm::MemoryObject> &&MemoryObject);
// Reads MemBuf, assuming it is the PNaCl bitcode contents of IRFilename. // Reads MemBuf, assuming it is the PNaCl bitcode contents of IRFilename.
void translateBuffer(const std::string &IRFilename, void translateBuffer(const std::string &IRFilename,
......
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
#include "llvm/Support/FileSystem.h" #include "llvm/Support/FileSystem.h"
#include "llvm/Support/raw_os_ostream.h" #include "llvm/Support/raw_os_ostream.h"
#include "llvm/Support/SourceMgr.h" #include "llvm/Support/SourceMgr.h"
#include "llvm/Support/StreamingMemoryObject.h"
#include "IceCfg.h" #include "IceCfg.h"
#include "IceClFlags.h" #include "IceClFlags.h"
...@@ -379,7 +380,17 @@ int main(int argc, char **argv) { ...@@ -379,7 +380,17 @@ int main(int argc, char **argv) {
if (BuildOnRead) { if (BuildOnRead) {
std::unique_ptr<Ice::PNaClTranslator> PTranslator( std::unique_ptr<Ice::PNaClTranslator> PTranslator(
new Ice::PNaClTranslator(&Ctx)); new Ice::PNaClTranslator(&Ctx));
PTranslator->translate(IRFilename); std::string StrError;
std::unique_ptr<DataStreamer> FileStreamer(
getDataFileStreamer(IRFilename, &StrError));
if (!StrError.empty() || !FileStreamer) {
SMDiagnostic Err(IRFilename, SourceMgr::DK_Error, StrError);
Err.print(argv[0], errs());
return GetReturnValue(Ice::EC_Bitcode);
}
std::unique_ptr<StreamingMemoryObject> MemObj(
new StreamingMemoryObjectImpl(FileStreamer.release()));
PTranslator->translate(IRFilename, std::move(MemObj));
Translator.reset(PTranslator.release()); Translator.reset(PTranslator.release());
} else if (ALLOW_LLVM_IR) { } else if (ALLOW_LLVM_IR) {
// Parse the input LLVM IR file into a module. // Parse the input LLVM IR file into a module.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment