OLD | NEW |
(Empty) | |
| 1 //===- subzero/src/IceCompileServer.cpp - Compile server ------------------===// |
| 2 // |
| 3 // The Subzero Code Generator |
| 4 // |
| 5 // This file is distributed under the University of Illinois Open Source |
| 6 // License. See LICENSE.TXT for details. |
| 7 // |
| 8 //===----------------------------------------------------------------------===// |
| 9 // |
| 10 // This file defines the basic commandline-based compile server. |
| 11 // |
| 12 //===----------------------------------------------------------------------===// |
| 13 |
| 14 #include <fstream> |
| 15 #include <iostream> |
| 16 #include <thread> |
| 17 |
| 18 #include "llvm/Support/FileSystem.h" |
| 19 #include "llvm/Support/raw_os_ostream.h" |
| 20 #include "llvm/Support/SourceMgr.h" |
| 21 #include "llvm/Support/StreamingMemoryObject.h" |
| 22 |
| 23 #include "IceClFlags.h" |
| 24 #include "IceClFlagsExtra.h" |
| 25 #include "IceCompileServer.h" |
| 26 #include "IceELFStreamer.h" |
| 27 #include "IceGlobalContext.h" |
| 28 |
| 29 namespace Ice { |
| 30 |
| 31 namespace { |
| 32 |
| 33 std::unique_ptr<Ostream> getStream(const IceString &Filename) { |
| 34 std::ofstream Ofs; |
| 35 if (Filename != "-") { |
| 36 Ofs.open(Filename.c_str(), std::ofstream::out); |
| 37 return std::unique_ptr<Ostream>(new llvm::raw_os_ostream(Ofs)); |
| 38 } else { |
| 39 return std::unique_ptr<Ostream>(new llvm::raw_os_ostream(std::cout)); |
| 40 } |
| 41 } |
| 42 |
| 43 ErrorCodes getReturnValue(const Ice::ClFlagsExtra &Flags, ErrorCodes Val) { |
| 44 if (Flags.getAlwaysExitSuccess()) |
| 45 return EC_None; |
| 46 return Val; |
| 47 } |
| 48 |
| 49 } // end of anonymous namespace |
| 50 |
| 51 void CLCompileServer::run() { |
| 52 ClFlags::parseFlags(argc, argv); |
| 53 ClFlags Flags; |
| 54 ClFlagsExtra ExtraFlags; |
| 55 ClFlags::getParsedClFlags(Flags); |
| 56 ClFlags::getParsedClFlagsExtra(ExtraFlags); |
| 57 |
| 58 std::unique_ptr<Ostream> Ls = getStream(ExtraFlags.getLogFilename()); |
| 59 Ls->SetUnbuffered(); |
| 60 std::unique_ptr<Ostream> Os; |
| 61 std::unique_ptr<ELFStreamer> ELFStr; |
| 62 switch (Flags.getOutFileType()) { |
| 63 case FT_Elf: { |
| 64 if (ExtraFlags.getOutputFilename() == "-") { |
| 65 *Ls << "Error: writing binary ELF to stdout is unsupported\n"; |
| 66 return transferErrorCode(getReturnValue(ExtraFlags, Ice::EC_Args)); |
| 67 } |
| 68 std::error_code EC; |
| 69 std::unique_ptr<llvm::raw_fd_ostream> FdOs(new llvm::raw_fd_ostream( |
| 70 ExtraFlags.getOutputFilename(), EC, llvm::sys::fs::F_None)); |
| 71 if (EC) { |
| 72 *Ls << "Failed to open output file: " << ExtraFlags.getOutputFilename() |
| 73 << ":\n" << EC.message() << "\n"; |
| 74 return transferErrorCode(getReturnValue(ExtraFlags, Ice::EC_Args)); |
| 75 } |
| 76 ELFStr.reset(new ELFStreamer(*FdOs.get())); |
| 77 Os.reset(FdOs.release()); |
| 78 // NaCl sets st_blksize to 0, and LLVM uses that to pick the |
| 79 // default preferred buffer size. Set to something non-zero. |
| 80 Os->SetBufferSize(1 << 14); |
| 81 } break; |
| 82 case FT_Asm: |
| 83 case FT_Iasm: { |
| 84 Os = getStream(ExtraFlags.getOutputFilename()); |
| 85 Os->SetUnbuffered(); |
| 86 } break; |
| 87 } |
| 88 |
| 89 IceString StrError; |
| 90 std::unique_ptr<llvm::DataStreamer> InputStream( |
| 91 llvm::getDataFileStreamer(ExtraFlags.getIRFilename(), &StrError)); |
| 92 if (!StrError.empty() || !InputStream) { |
| 93 llvm::SMDiagnostic Err(ExtraFlags.getIRFilename(), |
| 94 llvm::SourceMgr::DK_Error, StrError); |
| 95 Err.print(ExtraFlags.getAppName().c_str(), *Ls); |
| 96 return transferErrorCode(getReturnValue(ExtraFlags, Ice::EC_Bitcode)); |
| 97 } |
| 98 |
| 99 Ctx.reset(new GlobalContext(Ls.get(), Os.get(), ELFStr.get(), Flags)); |
| 100 if (Ctx->getFlags().getNumTranslationThreads() != 0) { |
| 101 std::thread CompileThread([this, &ExtraFlags, &InputStream]() { |
| 102 Ctx->initParserThread(); |
| 103 getCompiler().run(ExtraFlags, *Ctx.get(), std::move(InputStream)); |
| 104 }); |
| 105 CompileThread.join(); |
| 106 } else { |
| 107 getCompiler().run(ExtraFlags, *Ctx.get(), std::move(InputStream)); |
| 108 } |
| 109 transferErrorCode(getReturnValue( |
| 110 ExtraFlags, static_cast<ErrorCodes>(Ctx->getErrorStatus()->value()))); |
| 111 } |
| 112 |
| 113 } // end of namespace Ice |
OLD | NEW |