| OLD | NEW |
| 1 //===-- pnacl-llc.cpp - PNaCl-specific llc: pexe ---> nexe ---------------===// | 1 //===-- pnacl-llc.cpp - PNaCl-specific llc: pexe ---> nexe ---------------===// |
| 2 // | 2 // |
| 3 // The LLVM Compiler Infrastructure | 3 // The LLVM Compiler Infrastructure |
| 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 // pnacl-llc: the core of the PNaCl translator, compiling a pexe into a nexe. | 10 // pnacl-llc: the core of the PNaCl translator, compiling a pexe into a nexe. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 #include "llvm/CodeGen/LinkAllCodegenComponents.h" | 21 #include "llvm/CodeGen/LinkAllCodegenComponents.h" |
| 22 #include "llvm/IR/DataLayout.h" | 22 #include "llvm/IR/DataLayout.h" |
| 23 #include "llvm/IR/LLVMContext.h" | 23 #include "llvm/IR/LLVMContext.h" |
| 24 #include "llvm/IR/Module.h" | 24 #include "llvm/IR/Module.h" |
| 25 #include "llvm/IRReader/IRReader.h" | 25 #include "llvm/IRReader/IRReader.h" |
| 26 #include "llvm/MC/SubtargetFeature.h" | 26 #include "llvm/MC/SubtargetFeature.h" |
| 27 #include "llvm/Pass.h" | 27 #include "llvm/Pass.h" |
| 28 #include "llvm/PassManager.h" | 28 #include "llvm/PassManager.h" |
| 29 #include "llvm/Support/CommandLine.h" | 29 #include "llvm/Support/CommandLine.h" |
| 30 #include "llvm/Support/Debug.h" | 30 #include "llvm/Support/Debug.h" |
| 31 #include "llvm/Support/ErrorHandling.h" |
| 31 #include "llvm/Support/FormattedStream.h" | 32 #include "llvm/Support/FormattedStream.h" |
| 32 #include "llvm/Support/Host.h" | 33 #include "llvm/Support/Host.h" |
| 33 #include "llvm/Support/ManagedStatic.h" | 34 #include "llvm/Support/ManagedStatic.h" |
| 34 #include "llvm/Support/PrettyStackTrace.h" | 35 #include "llvm/Support/PrettyStackTrace.h" |
| 35 #include "llvm/Support/Signals.h" | 36 #include "llvm/Support/Signals.h" |
| 36 #include "llvm/Support/SourceMgr.h" | 37 #include "llvm/Support/SourceMgr.h" |
| 37 #include "llvm/Support/TargetRegistry.h" | 38 #include "llvm/Support/TargetRegistry.h" |
| 38 #include "llvm/Support/TargetSelect.h" | 39 #include "llvm/Support/TargetSelect.h" |
| 39 #include "llvm/Support/ToolOutputFile.h" | 40 #include "llvm/Support/ToolOutputFile.h" |
| 40 #include "llvm/Target/TargetLibraryInfo.h" | 41 #include "llvm/Target/TargetLibraryInfo.h" |
| 41 #include "llvm/Target/TargetMachine.h" | 42 #include "llvm/Target/TargetMachine.h" |
| 42 #include "llvm/Transforms/NaCl.h" | 43 #include "llvm/Transforms/NaCl.h" |
| 43 #include <memory> | 44 #include <memory> |
| 44 | 45 |
| 45 | 46 |
| 46 using namespace llvm; | 47 using namespace llvm; |
| 47 | 48 |
| 48 // NOTE: When __native_client__ is defined it means pnacl-llc is built as a | 49 // NOTE: When __native_client__ is defined it means pnacl-llc is built as a |
| 49 // sandboxed translator (from pnacl-llc.pexe to pnacl-llc.nexe). In this mode | 50 // sandboxed translator (from pnacl-llc.pexe to pnacl-llc.nexe). In this mode |
| 50 // it uses SRPC operations instead of direct OS intefaces. | 51 // it uses SRPC operations instead of direct OS intefaces. |
| 51 #if defined(__native_client__) | 52 #if defined(__native_client__) |
| 52 int srpc_main(int argc, char **argv); | 53 int srpc_main(int argc, char **argv); |
| 53 int getObjectFileFD(); | 54 int getObjectFileFD(); |
| 54 DataStreamer *getNaClBitcodeStreamer(); | 55 DataStreamer *getNaClBitcodeStreamer(); |
| 56 fatal_error_handler_t getSRPCErrorHandler(); |
| 55 #endif | 57 #endif |
| 56 | 58 |
| 57 cl::opt<NaClFileFormat> | 59 cl::opt<NaClFileFormat> |
| 58 InputFileFormat( | 60 InputFileFormat( |
| 59 "bitcode-format", | 61 "bitcode-format", |
| 60 cl::desc("Define format of input file:"), | 62 cl::desc("Define format of input file:"), |
| 61 cl::values( | 63 cl::values( |
| 62 clEnumValN(LLVMFormat, "llvm", "LLVM file (default)"), | 64 clEnumValN(LLVMFormat, "llvm", "LLVM file (default)"), |
| 63 clEnumValN(PNaClFormat, "pnacl", "PNaCl bitcode file"), | 65 clEnumValN(PNaClFormat, "pnacl", "PNaCl bitcode file"), |
| 64 clEnumValEnd), | 66 clEnumValEnd), |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 int llc_main(int argc, char **argv) { | 210 int llc_main(int argc, char **argv) { |
| 209 sys::PrintStackTraceOnErrorSignal(); | 211 sys::PrintStackTraceOnErrorSignal(); |
| 210 PrettyStackTraceProgram X(argc, argv); | 212 PrettyStackTraceProgram X(argc, argv); |
| 211 | 213 |
| 212 // Enable debug stream buffering. | 214 // Enable debug stream buffering. |
| 213 EnableDebugBuffering = true; | 215 EnableDebugBuffering = true; |
| 214 | 216 |
| 215 LLVMContext &Context = getGlobalContext(); | 217 LLVMContext &Context = getGlobalContext(); |
| 216 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. | 218 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 217 | 219 |
| 220 #if defined(__native_client__) |
| 221 install_fatal_error_handler(getSRPCErrorHandler(), NULL); |
| 222 #endif |
| 223 |
| 218 // Initialize targets first, so that --version shows registered targets. | 224 // Initialize targets first, so that --version shows registered targets. |
| 219 InitializeAllTargets(); | 225 InitializeAllTargets(); |
| 220 InitializeAllTargetMCs(); | 226 InitializeAllTargetMCs(); |
| 221 InitializeAllAsmPrinters(); | 227 InitializeAllAsmPrinters(); |
| 222 #if !defined(__native_client__) | 228 #if !defined(__native_client__) |
| 223 // Prune asm parsing from sandboxed translator. | 229 // Prune asm parsing from sandboxed translator. |
| 224 // Do not prune "AsmPrinters" because that includes | 230 // Do not prune "AsmPrinters" because that includes |
| 225 // the direct object emission. | 231 // the direct object emission. |
| 226 InitializeAllAsmParsers(); | 232 InitializeAllAsmParsers(); |
| 227 #endif | 233 #endif |
| (...skipping 17 matching lines...) Expand all Loading... |
| 245 #endif | 251 #endif |
| 246 | 252 |
| 247 cl::ParseCommandLineOptions(argc, argv, "pnacl-llc\n"); | 253 cl::ParseCommandLineOptions(argc, argv, "pnacl-llc\n"); |
| 248 | 254 |
| 249 return compileModule(argv[0], Context); | 255 return compileModule(argv[0], Context); |
| 250 } | 256 } |
| 251 | 257 |
| 252 static void CheckABIVerifyErrors(PNaClABIErrorReporter &Reporter, | 258 static void CheckABIVerifyErrors(PNaClABIErrorReporter &Reporter, |
| 253 const Twine &Name) { | 259 const Twine &Name) { |
| 254 if (PNaClABIVerify && Reporter.getErrorCount() > 0) { | 260 if (PNaClABIVerify && Reporter.getErrorCount() > 0) { |
| 255 errs() << (PNaClABIVerifyFatalErrors ? "ERROR: " : "WARNING: "); | 261 std::string errors; |
| 256 errs() << Name << " is not valid PNaCl bitcode:\n"; | 262 raw_string_ostream os(errors); |
| 257 Reporter.printErrors(errs()); | 263 os << (PNaClABIVerifyFatalErrors ? "ERROR: " : "WARNING: "); |
| 258 if (PNaClABIVerifyFatalErrors) | 264 os << Name << " is not valid PNaCl bitcode:\n"; |
| 259 exit(1); | 265 Reporter.printErrors(os); |
| 266 if (PNaClABIVerifyFatalErrors) { |
| 267 report_fatal_error(os.str()); |
| 268 } |
| 269 errs() << os.str(); |
| 260 } | 270 } |
| 261 Reporter.reset(); | 271 Reporter.reset(); |
| 262 } | 272 } |
| 263 | 273 |
| 264 static int compileModule(StringRef ProgramName, LLVMContext &Context) { | 274 static int compileModule(StringRef ProgramName, LLVMContext &Context) { |
| 265 // Load the module to be compiled... | 275 // Load the module to be compiled... |
| 266 SMDiagnostic Err; | 276 SMDiagnostic Err; |
| 267 std::auto_ptr<Module> M; | 277 std::auto_ptr<Module> M; |
| 268 Module *mod = 0; | 278 Module *mod = 0; |
| 269 Triple TheTriple; | 279 Triple TheTriple; |
| 270 | 280 |
| 271 PNaClABIErrorReporter ABIErrorReporter; | 281 PNaClABIErrorReporter ABIErrorReporter; |
| 272 | 282 |
| 273 #if defined(__native_client__) | 283 #if defined(__native_client__) |
| 274 if (LazyBitcode) { | 284 if (LazyBitcode) { |
| 275 std::string StrError; | 285 std::string StrError; |
| 286 std::string DisplayFilename("<PNaCl-translated pexe>"); |
| 276 M.reset(getNaClStreamedBitcodeModule( | 287 M.reset(getNaClStreamedBitcodeModule( |
| 277 std::string("<SRPC stream>"), | 288 DisplayFilename, |
| 278 getNaClBitcodeStreamer(), Context, &StrError)); | 289 getNaClBitcodeStreamer(), Context, &StrError)); |
| 279 if (!StrError.empty()) | 290 if (!StrError.empty()) |
| 280 Err = SMDiagnostic(InputFilename, SourceMgr::DK_Error, StrError); | 291 Err = SMDiagnostic(DisplayFilename, SourceMgr::DK_Error, StrError); |
| 281 } else { | 292 } else { |
| 282 llvm_unreachable("native client SRPC only supports streaming"); | 293 llvm_unreachable("native client SRPC only supports streaming"); |
| 283 } | 294 } |
| 284 #else | 295 #else |
| 285 M.reset(NaClParseIRFile(InputFilename, InputFileFormat, Err, Context)); | 296 M.reset(NaClParseIRFile(InputFilename, InputFileFormat, Err, Context)); |
| 286 #endif // __native_client__ | 297 #endif // __native_client__ |
| 287 | 298 |
| 288 mod = M.get(); | 299 mod = M.get(); |
| 289 if (mod == 0) { | 300 if (mod == 0) { |
| 301 #if defined(__native_client__) |
| 302 report_fatal_error(Err.getMessage()); |
| 303 #else |
| 304 // Err.print is prettier, so use it for the non-sandboxed translator. |
| 290 Err.print(ProgramName.data(), errs()); | 305 Err.print(ProgramName.data(), errs()); |
| 291 return 1; | 306 return 1; |
| 307 #endif |
| 292 } | 308 } |
| 293 | 309 |
| 294 if (PNaClABIVerify) { | 310 if (PNaClABIVerify) { |
| 295 // Verify the module (but not the functions yet) | 311 // Verify the module (but not the functions yet) |
| 296 ModulePass *VerifyPass = createPNaClABIVerifyModulePass(&ABIErrorReporter, | 312 ModulePass *VerifyPass = createPNaClABIVerifyModulePass(&ABIErrorReporter, |
| 297 LazyBitcode); | 313 LazyBitcode); |
| 298 VerifyPass->runOnModule(*mod); | 314 VerifyPass->runOnModule(*mod); |
| 299 CheckABIVerifyErrors(ABIErrorReporter, "Module"); | 315 CheckABIVerifyErrors(ABIErrorReporter, "Module"); |
| 300 } | 316 } |
| 301 | 317 |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 477 return 0; | 493 return 0; |
| 478 } | 494 } |
| 479 | 495 |
| 480 int main(int argc, char **argv) { | 496 int main(int argc, char **argv) { |
| 481 #if defined(__native_client__) | 497 #if defined(__native_client__) |
| 482 return srpc_main(argc, argv); | 498 return srpc_main(argc, argv); |
| 483 #else | 499 #else |
| 484 return llc_main(argc, argv); | 500 return llc_main(argc, argv); |
| 485 #endif // __native_client__ | 501 #endif // __native_client__ |
| 486 } | 502 } |
| OLD | NEW |