Commit 620ad732 by Jim Stichnoth

Subzero: Fix asm (non-ELF) output files.

In an earlier version of Subzero, the text output stream object was stack-allocated within main. A later refactoring moved its allocation into a helper function, but it was still being stack-allocated, which was bad when the helper function returned. This change allocates the object via "new", which fixes that problem, but reveals another problem: the raw_ostream object for some reason doesn't finish writing everything to disk and yielding a truncated output file. This is solved in the style of the ELF streamer, by using raw_fd_ostream instead. BUG= none R=kschimpf@google.com Review URL: https://codereview.chromium.org/1111603003
parent 2f67b929
...@@ -75,9 +75,7 @@ public: ...@@ -75,9 +75,7 @@ public:
ELFStream.reset(nullptr); ELFStream.reset(nullptr);
} }
StringStream &getErrorStream() { StringStream &getErrorStream() { return *ErrorStream; }
return *ErrorStream;
}
private: private:
class StringStream { class StringStream {
...@@ -85,6 +83,7 @@ private: ...@@ -85,6 +83,7 @@ private:
StringStream() : StrBuf(Buffer) {} StringStream() : StrBuf(Buffer) {}
const IceString &getContents() { return StrBuf.str(); } const IceString &getContents() { return StrBuf.str(); }
Ostream &getStream() { return StrBuf; } Ostream &getStream() { return StrBuf; }
private: private:
std::string Buffer; std::string Buffer;
llvm::raw_string_ostream StrBuf; llvm::raw_string_ostream StrBuf;
......
...@@ -30,13 +30,13 @@ namespace Ice { ...@@ -30,13 +30,13 @@ namespace Ice {
namespace { namespace {
std::unique_ptr<Ostream> getStream(const IceString &Filename) { std::unique_ptr<Ostream> makeStream(const IceString &Filename,
std::ofstream Ofs; std::error_code &EC) {
if (Filename != "-") { if (Filename == "-") {
Ofs.open(Filename.c_str(), std::ofstream::out);
return std::unique_ptr<Ostream>(new llvm::raw_os_ostream(Ofs));
} else {
return std::unique_ptr<Ostream>(new llvm::raw_os_ostream(std::cout)); return std::unique_ptr<Ostream>(new llvm::raw_os_ostream(std::cout));
} else {
return std::unique_ptr<Ostream>(
new llvm::raw_fd_ostream(Filename, EC, llvm::sys::fs::F_None));
} }
} }
...@@ -55,7 +55,11 @@ void CLCompileServer::run() { ...@@ -55,7 +55,11 @@ void CLCompileServer::run() {
ClFlags::getParsedClFlags(Flags); ClFlags::getParsedClFlags(Flags);
ClFlags::getParsedClFlagsExtra(ExtraFlags); ClFlags::getParsedClFlagsExtra(ExtraFlags);
std::unique_ptr<Ostream> Ls = getStream(ExtraFlags.getLogFilename()); std::error_code EC;
std::unique_ptr<Ostream> Ls = makeStream(ExtraFlags.getLogFilename(), EC);
if (EC) {
llvm::report_fatal_error("Unable to open log file");
}
Ls->SetUnbuffered(); Ls->SetUnbuffered();
std::unique_ptr<Ostream> Os; std::unique_ptr<Ostream> Os;
std::unique_ptr<ELFStreamer> ELFStr; std::unique_ptr<ELFStreamer> ELFStr;
...@@ -65,7 +69,6 @@ void CLCompileServer::run() { ...@@ -65,7 +69,6 @@ void CLCompileServer::run() {
*Ls << "Error: writing binary ELF to stdout is unsupported\n"; *Ls << "Error: writing binary ELF to stdout is unsupported\n";
return transferErrorCode(getReturnValue(ExtraFlags, Ice::EC_Args)); return transferErrorCode(getReturnValue(ExtraFlags, Ice::EC_Args));
} }
std::error_code EC;
std::unique_ptr<llvm::raw_fd_ostream> FdOs(new llvm::raw_fd_ostream( std::unique_ptr<llvm::raw_fd_ostream> FdOs(new llvm::raw_fd_ostream(
ExtraFlags.getOutputFilename(), EC, llvm::sys::fs::F_None)); ExtraFlags.getOutputFilename(), EC, llvm::sys::fs::F_None));
if (EC) { if (EC) {
...@@ -81,7 +84,12 @@ void CLCompileServer::run() { ...@@ -81,7 +84,12 @@ void CLCompileServer::run() {
} break; } break;
case FT_Asm: case FT_Asm:
case FT_Iasm: { case FT_Iasm: {
Os = getStream(ExtraFlags.getOutputFilename()); Os = makeStream(ExtraFlags.getOutputFilename(), EC);
if (EC) {
*Ls << "Failed to open output file: " << ExtraFlags.getOutputFilename()
<< ":\n" << EC.message() << "\n";
return transferErrorCode(getReturnValue(ExtraFlags, Ice::EC_Args));
}
Os->SetUnbuffered(); Os->SetUnbuffered();
} break; } break;
} }
...@@ -96,8 +104,8 @@ void CLCompileServer::run() { ...@@ -96,8 +104,8 @@ void CLCompileServer::run() {
return transferErrorCode(getReturnValue(ExtraFlags, Ice::EC_Bitcode)); return transferErrorCode(getReturnValue(ExtraFlags, Ice::EC_Bitcode));
} }
Ctx.reset(new GlobalContext(Ls.get(), Os.get(), Ls.get(), ELFStr.get(), Ctx.reset(
Flags)); new GlobalContext(Ls.get(), Os.get(), Ls.get(), ELFStr.get(), Flags));
if (Ctx->getFlags().getNumTranslationThreads() != 0) { if (Ctx->getFlags().getNumTranslationThreads() != 0) {
std::thread CompileThread([this, &ExtraFlags, &InputStream]() { std::thread CompileThread([this, &ExtraFlags, &InputStream]() {
Ctx->initParserThread(); Ctx->initParserThread();
......
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