Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 //===- subzero/src/IceCompiler.cpp - Driver for bitcode translation -------===// | 1 //===- subzero/src/IceCompiler.cpp - Driver for bitcode translation -------===// |
| 2 // | 2 // |
| 3 // The Subzero Code Generator | 3 // The Subzero Code Generator |
| 4 // | 4 // |
| 5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
| 6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
| 7 // | 7 // |
| 8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
| 9 /// | 9 /// |
| 10 /// \file | 10 /// \file |
| 11 /// \brief Defines a driver for translating PNaCl bitcode into native code. | 11 /// \brief Defines a driver for translating PNaCl bitcode into native code. |
| 12 /// | 12 /// |
| 13 /// The driver can either directly parse the binary bitcode file, or use LLVM | 13 /// The driver can either directly parse the binary bitcode file, or use LLVM |
| 14 /// routines to parse a textual bitcode file into LLVM IR and then convert LLVM | 14 /// routines to parse a textual bitcode file into LLVM IR and then convert LLVM |
| 15 /// IR into ICE. In either case, the high-level ICE is then compiled down to | 15 /// IR into ICE. In either case, the high-level ICE is then compiled down to |
| 16 /// native code, as either an ELF object file or a textual asm file. | 16 /// native code, as either an ELF object file or a textual asm file. |
| 17 /// | 17 /// |
| 18 //===----------------------------------------------------------------------===// | 18 //===----------------------------------------------------------------------===// |
| 19 | 19 |
| 20 #include <regex> | |
| 21 | |
| 20 #include "IceCompiler.h" | 22 #include "IceCompiler.h" |
| 21 | 23 |
| 22 #include "IceBuildDefs.h" | 24 #include "IceBuildDefs.h" |
| 23 #include "IceCfg.h" | 25 #include "IceCfg.h" |
| 24 #include "IceClFlags.h" | 26 #include "IceClFlags.h" |
| 25 #include "IceClFlagsExtra.h" | 27 #include "IceClFlagsExtra.h" |
| 26 #include "IceConverter.h" | 28 #include "IceConverter.h" |
| 27 #include "IceELFObjectWriter.h" | 29 #include "IceELFObjectWriter.h" |
| 28 #include "PNaClTranslator.h" | 30 #include "PNaClTranslator.h" |
| 29 | 31 |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 48 } ConditionalBuildAttributes[] = { | 50 } ConditionalBuildAttributes[] = { |
| 49 {"dump", BuildDefs::dump()}, | 51 {"dump", BuildDefs::dump()}, |
| 50 {"llvm_cl", BuildDefs::llvmCl()}, | 52 {"llvm_cl", BuildDefs::llvmCl()}, |
| 51 {"llvm_ir", BuildDefs::llvmIr()}, | 53 {"llvm_ir", BuildDefs::llvmIr()}, |
| 52 {"llvm_ir_as_input", BuildDefs::llvmIrAsInput()}, | 54 {"llvm_ir_as_input", BuildDefs::llvmIrAsInput()}, |
| 53 {"minimal_build", BuildDefs::minimal()}, | 55 {"minimal_build", BuildDefs::minimal()}, |
| 54 {"browser_mode", BuildDefs::browser()}}; | 56 {"browser_mode", BuildDefs::browser()}}; |
| 55 | 57 |
| 56 /// Dumps values of build attributes to Stream if Stream is non-null. | 58 /// Dumps values of build attributes to Stream if Stream is non-null. |
| 57 void dumpBuildAttributes(Ostream *Stream) { | 59 void dumpBuildAttributes(Ostream *Stream) { |
| 58 if (Stream == nullptr) | 60 if (Stream == nullptr) |
|
Jim Stichnoth
2015/12/25 15:31:01
The change to how this is called is nice. As a re
rkotlerimgtec
2015/12/26 23:29:42
Done.
| |
| 59 return; | 61 return; |
| 60 // List the supported targets. | 62 // List the supported targets. |
| 61 #define SUBZERO_TARGET(TARGET) *Stream << "target_" #TARGET << "\n"; | 63 #define SUBZERO_TARGET(TARGET) *Stream << "target_" #TARGET << "\n"; |
| 62 #include "llvm/Config/SZTargets.def" | 64 #include "llvm/Config/SZTargets.def" |
| 63 const char *Prefix[2] = {"no", "allow"}; | 65 const char *Prefix[2] = {"no", "allow"}; |
| 64 for (size_t i = 0; i < llvm::array_lengthof(ConditionalBuildAttributes); | 66 for (size_t i = 0; i < llvm::array_lengthof(ConditionalBuildAttributes); |
| 65 ++i) { | 67 ++i) { |
| 66 const auto &A = ConditionalBuildAttributes[i]; | 68 const auto &A = ConditionalBuildAttributes[i]; |
| 67 *Stream << Prefix[A.FlagValue] << "_" << A.FlagName << "\n"; | 69 *Stream << Prefix[A.FlagValue] << "_" << A.FlagName << "\n"; |
| 68 } | 70 } |
| 69 } | 71 } |
| 72 bool llvmIRInput(const IceString &Filename) { | |
| 73 return BuildDefs::llvmIrAsInput() && | |
| 74 std::regex_match(Filename, std::regex(".*\\.ll")); | |
| 75 } | |
| 70 | 76 |
| 71 } // end of anonymous namespace | 77 } // end of anonymous namespace |
| 72 | 78 |
| 73 void Compiler::run(const Ice::ClFlagsExtra &ExtraFlags, GlobalContext &Ctx, | 79 void Compiler::run(const Ice::ClFlagsExtra &ExtraFlags, GlobalContext &Ctx, |
| 74 std::unique_ptr<llvm::DataStreamer> &&InputStream) { | 80 std::unique_ptr<llvm::DataStreamer> &&InputStream) { |
| 75 dumpBuildAttributes(ExtraFlags.getGenerateBuildAtts() ? &Ctx.getStrDump() | 81 if (ExtraFlags.getGenerateBuildAtts()) { |
| 76 : nullptr); | 82 dumpBuildAttributes(&Ctx.getStrDump()); |
| 77 if (ExtraFlags.getGenerateBuildAtts()) | 83 Ctx.getErrorStatus()->assign(EC_None); |
| 78 return Ctx.getErrorStatus()->assign(EC_None); | 84 return; |
| 79 | 85 } |
| 80 // The Minimal build (specifically, when dump()/emit() are not implemented) | 86 // The Minimal build (specifically, when dump()/emit() are not implemented) |
| 81 // allows only --filetype=obj. Check here to avoid cryptic error messages | 87 // allows only --filetype=obj. Check here to avoid cryptic error messages |
| 82 // downstream. | 88 // downstream. |
| 83 if (!BuildDefs::dump() && Ctx.getFlags().getOutFileType() != FT_Elf) { | 89 if (!BuildDefs::dump() && Ctx.getFlags().getOutFileType() != FT_Elf) { |
| 84 // TODO(stichnot): Access the actual command-line argument via | 90 // TODO(stichnot): Access the actual command-line argument via |
| 85 // llvm::Option.ArgStr and .ValueStr . | 91 // llvm::Option.ArgStr and .ValueStr . |
| 86 Ctx.getStrError() | 92 Ctx.getStrError() |
| 87 << "Error: only --filetype=obj is supported in this build.\n"; | 93 << "Error: only --filetype=obj is supported in this build.\n"; |
| 88 return Ctx.getErrorStatus()->assign(EC_Args); | 94 Ctx.getErrorStatus()->assign(EC_Args); |
| 95 return; | |
| 89 } | 96 } |
| 90 | 97 |
| 91 // Force -build-on-read=0 for .ll files. | |
| 92 const std::string LLSuffix = ".ll"; | |
| 93 const IceString &IRFilename = ExtraFlags.getIRFilename(); | |
| 94 bool BuildOnRead = ExtraFlags.getBuildOnRead(); | |
| 95 if (BuildDefs::llvmIrAsInput() && IRFilename.length() >= LLSuffix.length() && | |
| 96 IRFilename.compare(IRFilename.length() - LLSuffix.length(), | |
| 97 LLSuffix.length(), LLSuffix) == 0) | |
| 98 BuildOnRead = false; | |
| 99 | |
| 100 TimerMarker T(Ice::TimerStack::TT_szmain, &Ctx); | 98 TimerMarker T(Ice::TimerStack::TT_szmain, &Ctx); |
| 101 | 99 |
| 102 Ctx.emitFileHeader(); | 100 Ctx.emitFileHeader(); |
| 103 Ctx.startWorkerThreads(); | 101 Ctx.startWorkerThreads(); |
| 104 | 102 |
| 105 std::unique_ptr<Translator> Translator; | 103 std::unique_ptr<Translator> Translator; |
| 104 const IceString &IRFilename = ExtraFlags.getIRFilename(); | |
| 105 bool BuildOnRead = ExtraFlags.getBuildOnRead() && !llvmIRInput(IRFilename); | |
| 106 if (BuildOnRead) { | 106 if (BuildOnRead) { |
| 107 std::unique_ptr<PNaClTranslator> PTranslator(new PNaClTranslator(&Ctx)); | 107 std::unique_ptr<PNaClTranslator> PTranslator(new PNaClTranslator(&Ctx)); |
| 108 std::unique_ptr<llvm::StreamingMemoryObject> MemObj( | 108 std::unique_ptr<llvm::StreamingMemoryObject> MemObj( |
| 109 new llvm::StreamingMemoryObjectImpl(InputStream.release())); | 109 new llvm::StreamingMemoryObjectImpl(InputStream.release())); |
| 110 PTranslator->translate(IRFilename, std::move(MemObj)); | 110 PTranslator->translate(IRFilename, std::move(MemObj)); |
| 111 Translator.reset(PTranslator.release()); | 111 Translator.reset(PTranslator.release()); |
| 112 } else if (BuildDefs::llvmIr()) { | 112 } else if (BuildDefs::llvmIr()) { |
| 113 if (BuildDefs::browser()) { | 113 if (BuildDefs::browser()) { |
| 114 Ctx.getStrError() | 114 Ctx.getStrError() |
| 115 << "non BuildOnRead is not supported w/ PNACL_BROWSER_TRANSLATOR\n"; | 115 << "non BuildOnRead is not supported w/ PNACL_BROWSER_TRANSLATOR\n"; |
| 116 return Ctx.getErrorStatus()->assign(EC_Args); | 116 Ctx.getErrorStatus()->assign(EC_Args); |
| 117 return; | |
| 117 } | 118 } |
| 118 // Parse the input LLVM IR file into a module. | 119 // Parse the input LLVM IR file into a module. |
| 119 llvm::SMDiagnostic Err; | 120 llvm::SMDiagnostic Err; |
| 120 TimerMarker T1(Ice::TimerStack::TT_parse, &Ctx); | 121 TimerMarker T1(Ice::TimerStack::TT_parse, &Ctx); |
| 121 llvm::DiagnosticHandlerFunction DiagnosticHandler = | 122 llvm::DiagnosticHandlerFunction DiagnosticHandler = |
| 122 ExtraFlags.getLLVMVerboseErrors() | 123 ExtraFlags.getLLVMVerboseErrors() |
| 123 ? redirectNaClDiagnosticToStream(llvm::errs()) | 124 ? redirectNaClDiagnosticToStream(llvm::errs()) |
| 124 : nullptr; | 125 : nullptr; |
| 125 std::unique_ptr<llvm::Module> Mod = | 126 std::unique_ptr<llvm::Module> Mod = |
| 126 NaClParseIRFile(IRFilename, ExtraFlags.getInputFileFormat(), Err, | 127 NaClParseIRFile(IRFilename, ExtraFlags.getInputFileFormat(), Err, |
| 127 llvm::getGlobalContext(), DiagnosticHandler); | 128 llvm::getGlobalContext(), DiagnosticHandler); |
| 128 if (!Mod) { | 129 if (!Mod) { |
| 129 Err.print(ExtraFlags.getAppName().c_str(), llvm::errs()); | 130 Err.print(ExtraFlags.getAppName().c_str(), llvm::errs()); |
| 130 return Ctx.getErrorStatus()->assign(EC_Bitcode); | 131 Ctx.getErrorStatus()->assign(EC_Bitcode); |
| 132 return; | |
| 131 } | 133 } |
| 132 | 134 |
| 133 std::unique_ptr<Converter> Converter(new class Converter(Mod.get(), &Ctx)); | 135 std::unique_ptr<Converter> Converter(new class Converter(Mod.get(), &Ctx)); |
| 134 Converter->convertToIce(); | 136 Converter->convertToIce(); |
| 135 Translator.reset(Converter.release()); | 137 Translator.reset(Converter.release()); |
| 136 } else { | 138 } else { |
| 137 Ctx.getStrError() << "Error: Build doesn't allow LLVM IR, " | 139 Ctx.getStrError() << "Error: Build doesn't allow LLVM IR, " |
| 138 << "--build-on-read=0 not allowed\n"; | 140 << "--build-on-read=0 not allowed\n"; |
| 139 return Ctx.getErrorStatus()->assign(EC_Args); | 141 Ctx.getErrorStatus()->assign(EC_Args); |
| 142 return; | |
| 140 } | 143 } |
| 141 | 144 |
| 142 Ctx.waitForWorkerThreads(); | 145 Ctx.waitForWorkerThreads(); |
| 143 if (Translator->getErrorStatus()) { | 146 if (Translator->getErrorStatus()) { |
| 144 Ctx.getErrorStatus()->assign(Translator->getErrorStatus().value()); | 147 Ctx.getErrorStatus()->assign(Translator->getErrorStatus().value()); |
| 145 } else { | 148 } else { |
| 146 Ctx.lowerGlobals("last"); | 149 Ctx.lowerGlobals("last"); |
| 147 Ctx.lowerProfileData(); | 150 Ctx.lowerProfileData(); |
| 148 Ctx.lowerConstants(); | 151 Ctx.lowerConstants(); |
| 149 Ctx.lowerJumpTables(); | 152 Ctx.lowerJumpTables(); |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 160 | 163 |
| 161 if (Ctx.getFlags().getTimeEachFunction()) { | 164 if (Ctx.getFlags().getTimeEachFunction()) { |
| 162 constexpr bool DumpCumulative = false; | 165 constexpr bool DumpCumulative = false; |
| 163 Ctx.dumpTimers(GlobalContext::TSK_Funcs, DumpCumulative); | 166 Ctx.dumpTimers(GlobalContext::TSK_Funcs, DumpCumulative); |
| 164 } | 167 } |
| 165 constexpr bool FinalStats = true; | 168 constexpr bool FinalStats = true; |
| 166 Ctx.dumpStats("_FINAL_", FinalStats); | 169 Ctx.dumpStats("_FINAL_", FinalStats); |
| 167 } | 170 } |
| 168 | 171 |
| 169 } // end of namespace Ice | 172 } // end of namespace Ice |
| OLD | NEW |