Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(569)

Side by Side Diff: src/IceCompileServer.cpp

Issue 1190413004: Allow pnacl-sz to be compiled to textual bitcode records. (Closed) Base URL: https://chromium.googlesource.com/native_client/pnacl-subzero.git@master
Patch Set: Fix nits. Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« Makefile.standalone ('K') | « Makefile.standalone ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 //===- subzero/src/IceCompileServer.cpp - Compile server ------------------===// 1 //===- subzero/src/IceCompileServer.cpp - Compile server ------------------===//
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 // This file defines the basic commandline-based compile server. 10 // This file defines the basic commandline-based compile server.
11 // 11 //
12 //===----------------------------------------------------------------------===// 12 //===----------------------------------------------------------------------===//
13 13
14 #include <fstream> 14 #include <fstream>
15 #include <iostream> 15 #include <iostream>
16 #include <thread> 16 #include <thread>
17 17
18 // Include code to handle converting textual bitcode records to binary (for
19 // INPUT_IS_TEXTUAL_BITCODE).
20 #include "llvm/Bitcode/NaCl/NaClBitcodeMungeUtils.h"
21
18 #include "llvm/Support/FileSystem.h" 22 #include "llvm/Support/FileSystem.h"
19 #include "llvm/Support/raw_os_ostream.h" 23 #include "llvm/Support/raw_os_ostream.h"
20 #include "llvm/Support/Signals.h" 24 #include "llvm/Support/Signals.h"
21 #include "llvm/Support/SourceMgr.h" 25 #include "llvm/Support/SourceMgr.h"
22 #include "llvm/Support/StreamingMemoryObject.h" 26 #include "llvm/Support/StreamingMemoryObject.h"
23 27
24 #include "IceClFlags.h" 28 #include "IceClFlags.h"
25 #include "IceClFlagsExtra.h" 29 #include "IceClFlagsExtra.h"
26 #include "IceCompileServer.h" 30 #include "IceCompileServer.h"
27 #include "IceELFStreamer.h" 31 #include "IceELFStreamer.h"
28 #include "IceGlobalContext.h" 32 #include "IceGlobalContext.h"
29 33
30 namespace Ice { 34 namespace Ice {
31 35
32 namespace { 36 namespace {
33 37
38 static_assert(
39 !(INPUT_IS_TEXTUAL_BITCODE && PNACL_BROWSER_TRANSLATOR),
40 "Can not define INPUT_IS_TEXTUAL_BITCODE when building browswer translator");
41
42 // Define a SmallVector backed buffer as a data stream, so that it
43 // can hold the generated binary version of the textual bitcode in the
44 // input file.
45 class TextDataStreamer : public llvm::DataStreamer {
46 public:
47 TextDataStreamer() = default;
48 ~TextDataStreamer() final = default;
49 static TextDataStreamer *create(const IceString &Filename, std::string *Err);
50 size_t GetBytes(unsigned char *Buf, size_t Len) final;
51 private:
52 llvm::SmallVector<char, 1024> BitcodeBuffer;
53 size_t Cursor = 0;
54 };
55
56 TextDataStreamer *TextDataStreamer::create(const IceString &Filename,
57 std::string *Err) {
58 TextDataStreamer *Streamer = new TextDataStreamer();
59 llvm::raw_string_ostream ErrStrm(*Err);
60 if (std::error_code EC = llvm::readNaClRecordTextAndBuildBitcode(
61 Filename, Streamer->BitcodeBuffer, &ErrStrm)) {
62 ErrStrm << "Error: " << EC.message() << "\n";
63 ErrStrm.flush();
64 delete Streamer;
65 return nullptr;
66 }
67 ErrStrm.flush();
68 return Streamer;
69 }
70
71 size_t TextDataStreamer::GetBytes(unsigned char *Buf, size_t Len) {
72 if (Cursor >= BitcodeBuffer.size())
73 return 0;
74 size_t Remaining = BitcodeBuffer.size();
75 Len = std::min(Len, Remaining);
76 for (size_t i = 0; i < Len; ++i)
77 Buf[i] = BitcodeBuffer[Cursor + i];
78 Cursor += Len;
79 return Len;
80 }
81
34 std::unique_ptr<Ostream> makeStream(const IceString &Filename, 82 std::unique_ptr<Ostream> makeStream(const IceString &Filename,
35 std::error_code &EC) { 83 std::error_code &EC) {
36 if (Filename == "-") { 84 if (Filename == "-") {
37 return std::unique_ptr<Ostream>(new llvm::raw_os_ostream(std::cout)); 85 return std::unique_ptr<Ostream>(new llvm::raw_os_ostream(std::cout));
38 } else { 86 } else {
39 return std::unique_ptr<Ostream>( 87 return std::unique_ptr<Ostream>(
40 new llvm::raw_fd_ostream(Filename, EC, llvm::sys::fs::F_None)); 88 new llvm::raw_fd_ostream(Filename, EC, llvm::sys::fs::F_None));
41 } 89 }
42 } 90 }
43 91
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 *Ls << "Failed to open output file: " << ExtraFlags.getOutputFilename() 141 *Ls << "Failed to open output file: " << ExtraFlags.getOutputFilename()
94 << ":\n" << EC.message() << "\n"; 142 << ":\n" << EC.message() << "\n";
95 return transferErrorCode(getReturnValue(ExtraFlags, Ice::EC_Args)); 143 return transferErrorCode(getReturnValue(ExtraFlags, Ice::EC_Args));
96 } 144 }
97 Os->SetUnbuffered(); 145 Os->SetUnbuffered();
98 } break; 146 } break;
99 } 147 }
100 148
101 IceString StrError; 149 IceString StrError;
102 std::unique_ptr<llvm::DataStreamer> InputStream( 150 std::unique_ptr<llvm::DataStreamer> InputStream(
103 llvm::getDataFileStreamer(ExtraFlags.getIRFilename(), &StrError)); 151 INPUT_IS_TEXTUAL_BITCODE
152 ? TextDataStreamer::create(ExtraFlags.getIRFilename(), &StrError)
153 : llvm::getDataFileStreamer(ExtraFlags.getIRFilename(), &StrError)
154 );
104 if (!StrError.empty() || !InputStream) { 155 if (!StrError.empty() || !InputStream) {
105 llvm::SMDiagnostic Err(ExtraFlags.getIRFilename(), 156 llvm::SMDiagnostic Err(ExtraFlags.getIRFilename(),
106 llvm::SourceMgr::DK_Error, StrError); 157 llvm::SourceMgr::DK_Error, StrError);
107 Err.print(ExtraFlags.getAppName().c_str(), *Ls); 158 Err.print(ExtraFlags.getAppName().c_str(), *Ls);
108 return transferErrorCode(getReturnValue(ExtraFlags, Ice::EC_Bitcode)); 159 return transferErrorCode(getReturnValue(ExtraFlags, Ice::EC_Bitcode));
109 } 160 }
110 161
111 Ctx.reset( 162 Ctx.reset(
112 new GlobalContext(Ls.get(), Os.get(), Ls.get(), ELFStr.get(), Flags)); 163 new GlobalContext(Ls.get(), Os.get(), Ls.get(), ELFStr.get(), Flags));
113 if (Ctx->getFlags().getNumTranslationThreads() != 0) { 164 if (Ctx->getFlags().getNumTranslationThreads() != 0) {
114 std::thread CompileThread([this, &ExtraFlags, &InputStream]() { 165 std::thread CompileThread([this, &ExtraFlags, &InputStream]() {
115 Ctx->initParserThread(); 166 Ctx->initParserThread();
116 getCompiler().run(ExtraFlags, *Ctx.get(), std::move(InputStream)); 167 getCompiler().run(ExtraFlags, *Ctx.get(), std::move(InputStream));
117 }); 168 });
118 CompileThread.join(); 169 CompileThread.join();
119 } else { 170 } else {
120 getCompiler().run(ExtraFlags, *Ctx.get(), std::move(InputStream)); 171 getCompiler().run(ExtraFlags, *Ctx.get(), std::move(InputStream));
121 } 172 }
122 transferErrorCode(getReturnValue( 173 transferErrorCode(getReturnValue(
123 ExtraFlags, static_cast<ErrorCodes>(Ctx->getErrorStatus()->value()))); 174 ExtraFlags, static_cast<ErrorCodes>(Ctx->getErrorStatus()->value())));
124 } 175 }
125 176
126 } // end of namespace Ice 177 } // end of namespace Ice
OLDNEW
« Makefile.standalone ('K') | « Makefile.standalone ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698