diff options
-rw-r--r-- | tools/analyze/analyze.cpp | 96 | ||||
-rw-r--r-- | tools/bugpoint/bugpoint.cpp | 5 | ||||
-rw-r--r-- | tools/extract/extract.cpp | 89 | ||||
-rw-r--r-- | tools/gccas/gccas.cpp | 122 | ||||
-rw-r--r-- | tools/llc/llc.cpp | 193 | ||||
-rw-r--r-- | tools/lli/lli.cpp | 117 | ||||
-rw-r--r-- | tools/llvm-as/llvm-as.cpp | 11 | ||||
-rw-r--r-- | tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp | 88 | ||||
-rw-r--r-- | tools/llvm-db/llvm-db.cpp | 61 | ||||
-rw-r--r-- | tools/llvm-dis/llvm-dis.cpp | 135 | ||||
-rw-r--r-- | tools/llvm-extract/llvm-extract.cpp | 89 | ||||
-rw-r--r-- | tools/llvm-ld/llvm-ld.cpp | 241 | ||||
-rw-r--r-- | tools/llvm-link/llvm-link.cpp | 111 | ||||
-rw-r--r-- | tools/llvm-nm/llvm-nm.cpp | 35 | ||||
-rw-r--r-- | tools/llvm-prof/llvm-prof.cpp | 215 | ||||
-rw-r--r-- | tools/llvmc/llvmc.cpp | 1 | ||||
-rw-r--r-- | tools/opt/opt.cpp | 179 |
17 files changed, 948 insertions, 840 deletions
diff --git a/tools/analyze/analyze.cpp b/tools/analyze/analyze.cpp index 5a0c9508f6..0ac70687da 100644 --- a/tools/analyze/analyze.cpp +++ b/tools/analyze/analyze.cpp @@ -116,58 +116,66 @@ namespace { } int main(int argc, char **argv) { - cl::ParseCommandLineOptions(argc, argv, " llvm analysis printer tool\n"); - sys::PrintStackTraceOnErrorSignal(); - - Module *CurMod = 0; try { + cl::ParseCommandLineOptions(argc, argv, " llvm analysis printer tool\n"); + sys::PrintStackTraceOnErrorSignal(); + + Module *CurMod = 0; + try { #if 0 - TimeRegion RegionTimer(BytecodeLoadTimer); + TimeRegion RegionTimer(BytecodeLoadTimer); #endif - CurMod = ParseBytecodeFile(InputFilename); - if (!CurMod && !(CurMod = ParseAssemblyFile(InputFilename))){ - std::cerr << argv[0] << ": input file didn't read correctly.\n"; + CurMod = ParseBytecodeFile(InputFilename); + if (!CurMod && !(CurMod = ParseAssemblyFile(InputFilename))){ + std::cerr << argv[0] << ": input file didn't read correctly.\n"; + return 1; + } + } catch (const ParseException &E) { + std::cerr << argv[0] << ": " << E.getMessage() << "\n"; return 1; } - } catch (const ParseException &E) { - std::cerr << argv[0] << ": " << E.getMessage() << "\n"; - return 1; - } - // Create a PassManager to hold and optimize the collection of passes we are - // about to build... - // - PassManager Passes; + // Create a PassManager to hold and optimize the collection of passes we are + // about to build... + // + PassManager Passes; + + // Add an appropriate TargetData instance for this module... + Passes.add(new TargetData("analyze", CurMod)); + + // Make sure the input LLVM is well formed. + if (!NoVerify) + Passes.add(createVerifierPass()); + + // Create a new optimization pass for each one specified on the command line + for (unsigned i = 0; i < AnalysesList.size(); ++i) { + const PassInfo *Analysis = AnalysesList[i]; + + if (Analysis->getNormalCtor()) { + Pass *P = Analysis->getNormalCtor()(); + Passes.add(P); + + if (BasicBlockPass *BBP = dynamic_cast<BasicBlockPass*>(P)) + Passes.add(new BasicBlockPassPrinter(Analysis)); + else if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) + Passes.add(new FunctionPassPrinter(Analysis)); + else + Passes.add(new ModulePassPrinter(Analysis)); + + } else + std::cerr << argv[0] << ": cannot create pass: " + << Analysis->getPassName() << "\n"; + } - // Add an appropriate TargetData instance for this module... - Passes.add(new TargetData("analyze", CurMod)); + Passes.run(*CurMod); - // Make sure the input LLVM is well formed. - if (!NoVerify) - Passes.add(createVerifierPass()); + delete CurMod; + return 0; - // Create a new optimization pass for each one specified on the command line - for (unsigned i = 0; i < AnalysesList.size(); ++i) { - const PassInfo *Analysis = AnalysesList[i]; - - if (Analysis->getNormalCtor()) { - Pass *P = Analysis->getNormalCtor()(); - Passes.add(P); - - if (BasicBlockPass *BBP = dynamic_cast<BasicBlockPass*>(P)) - Passes.add(new BasicBlockPassPrinter(Analysis)); - else if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) - Passes.add(new FunctionPassPrinter(Analysis)); - else - Passes.add(new ModulePassPrinter(Analysis)); - - } else - std::cerr << argv[0] << ": cannot create pass: " - << Analysis->getPassName() << "\n"; + } catch (const std::string& msg) { + std::cerr << argv[0] << ": " << msg << "\n"; + } catch (...) { + std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n"; } - - Passes.run(*CurMod); - - delete CurMod; - return 0; + return 1; } diff --git a/tools/bugpoint/bugpoint.cpp b/tools/bugpoint/bugpoint.cpp index ff26a37693..9e5b4c4e87 100644 --- a/tools/bugpoint/bugpoint.cpp +++ b/tools/bugpoint/bugpoint.cpp @@ -51,10 +51,11 @@ int main(int argc, char **argv) { return D.run(); } catch (ToolExecutionError &TEE) { std::cerr << "Tool execution error: " << TEE.what() << '\n'; - return 1; + } catch (const std::string& msg) { + std::cerr << argv[0] << ": " << msg << "\n"; } catch (...) { std::cerr << "Whoops, an exception leaked out of bugpoint. " << "This is a bug in bugpoint!\n"; - return 1; } + return 1; } diff --git a/tools/extract/extract.cpp b/tools/extract/extract.cpp index 95822d0389..c7f942af27 100644 --- a/tools/extract/extract.cpp +++ b/tools/extract/extract.cpp @@ -45,53 +45,60 @@ ExtractFunc("func", cl::desc("Specify function to extract"), cl::init("main"), cl::value_desc("function")); int main(int argc, char **argv) { - cl::ParseCommandLineOptions(argc, argv, " llvm extractor\n"); - sys::PrintStackTraceOnErrorSignal(); + try { + cl::ParseCommandLineOptions(argc, argv, " llvm extractor\n"); + sys::PrintStackTraceOnErrorSignal(); - std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename)); - if (M.get() == 0) { - std::cerr << argv[0] << ": bytecode didn't read correctly.\n"; - return 1; - } + std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename)); + if (M.get() == 0) { + std::cerr << argv[0] << ": bytecode didn't read correctly.\n"; + return 1; + } - // Figure out which function we should extract - Function *F = M.get()->getNamedFunction(ExtractFunc); - if (F == 0) { - std::cerr << argv[0] << ": program doesn't contain function named '" - << ExtractFunc << "'!\n"; - return 1; - } + // Figure out which function we should extract + Function *F = M.get()->getNamedFunction(ExtractFunc); + if (F == 0) { + std::cerr << argv[0] << ": program doesn't contain function named '" + << ExtractFunc << "'!\n"; + return 1; + } - // In addition to deleting all other functions, we also want to spiff it up a - // little bit. Do this now. - // - PassManager Passes; - Passes.add(new TargetData("extract", M.get())); // Use correct TargetData - // Either isolate the function or delete it from the Module - Passes.add(createFunctionExtractionPass(F, DeleteFn)); - Passes.add(createGlobalDCEPass()); // Delete unreachable globals - Passes.add(createFunctionResolvingPass()); // Delete prototypes - Passes.add(createDeadTypeEliminationPass()); // Remove dead types... + // In addition to deleting all other functions, we also want to spiff it up a + // little bit. Do this now. + // + PassManager Passes; + Passes.add(new TargetData("extract", M.get())); // Use correct TargetData + // Either isolate the function or delete it from the Module + Passes.add(createFunctionExtractionPass(F, DeleteFn)); + Passes.add(createGlobalDCEPass()); // Delete unreachable globals + Passes.add(createFunctionResolvingPass()); // Delete prototypes + Passes.add(createDeadTypeEliminationPass()); // Remove dead types... - std::ostream *Out = 0; + std::ostream *Out = 0; - if (OutputFilename != "-") { // Not stdout? - if (!Force && std::ifstream(OutputFilename.c_str())) { - // If force is not specified, make sure not to overwrite a file! - std::cerr << argv[0] << ": error opening '" << OutputFilename - << "': file exists!\n" - << "Use -f command line argument to force output\n"; - return 1; + if (OutputFilename != "-") { // Not stdout? + if (!Force && std::ifstream(OutputFilename.c_str())) { + // If force is not specified, make sure not to overwrite a file! + std::cerr << argv[0] << ": error opening '" << OutputFilename + << "': file exists!\n" + << "Use -f command line argument to force output\n"; + return 1; + } + Out = new std::ofstream(OutputFilename.c_str()); + } else { // Specified stdout + Out = &std::cout; } - Out = new std::ofstream(OutputFilename.c_str()); - } else { // Specified stdout - Out = &std::cout; - } - Passes.add(new WriteBytecodePass(Out)); // Write bytecode to file... - Passes.run(*M.get()); + Passes.add(new WriteBytecodePass(Out)); // Write bytecode to file... + Passes.run(*M.get()); - if (Out != &std::cout) - delete Out; - return 0; + if (Out != &std::cout) + delete Out; + return 0; + } catch (const std::string& msg) { + std::cerr << argv[0] << ": " << msg << "\n"; + } catch (...) { + std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n"; + } + return 1; } diff --git a/tools/gccas/gccas.cpp b/tools/gccas/gccas.cpp index 813042cf50..c222c6e183 100644 --- a/tools/gccas/gccas.cpp +++ b/tools/gccas/gccas.cpp @@ -128,77 +128,85 @@ void AddConfiguredTransformationPasses(PassManager &PM) { int main(int argc, char **argv) { - cl::ParseCommandLineOptions(argc, argv, " llvm .s -> .o assembler for GCC\n"); - sys::PrintStackTraceOnErrorSignal(); - - std::auto_ptr<Module> M; try { - // Parse the file now... - M.reset(ParseAssemblyFile(InputFilename)); - } catch (const ParseException &E) { - std::cerr << argv[0] << ": " << E.getMessage() << "\n"; - return 1; - } + cl::ParseCommandLineOptions(argc, argv, + " llvm .s -> .o assembler for GCC\n"); + sys::PrintStackTraceOnErrorSignal(); + + std::auto_ptr<Module> M; + try { + // Parse the file now... + M.reset(ParseAssemblyFile(InputFilename)); + } catch (const ParseException &E) { + std::cerr << argv[0] << ": " << E.getMessage() << "\n"; + return 1; + } - if (M.get() == 0) { - std::cerr << argv[0] << ": assembly didn't read correctly.\n"; - return 1; - } + if (M.get() == 0) { + std::cerr << argv[0] << ": assembly didn't read correctly.\n"; + return 1; + } - std::ostream *Out = 0; - if (OutputFilename == "") { // Didn't specify an output filename? - if (InputFilename == "-") { - OutputFilename = "-"; - } else { - std::string IFN = InputFilename; - int Len = IFN.length(); - if (IFN[Len-2] == '.' && IFN[Len-1] == 's') { // Source ends in .s? - OutputFilename = std::string(IFN.begin(), IFN.end()-2); + std::ostream *Out = 0; + if (OutputFilename == "") { // Didn't specify an output filename? + if (InputFilename == "-") { + OutputFilename = "-"; } else { - OutputFilename = IFN; // Append a .o to it + std::string IFN = InputFilename; + int Len = IFN.length(); + if (IFN[Len-2] == '.' && IFN[Len-1] == 's') { // Source ends in .s? + OutputFilename = std::string(IFN.begin(), IFN.end()-2); + } else { + OutputFilename = IFN; // Append a .o to it + } + OutputFilename += ".o"; } - OutputFilename += ".o"; } - } - if (OutputFilename == "-") - Out = &std::cout; - else { - Out = new std::ofstream(OutputFilename.c_str(), std::ios::out); + if (OutputFilename == "-") + Out = &std::cout; + else { + Out = new std::ofstream(OutputFilename.c_str(), std::ios::out); - // Make sure that the Out file gets unlinked from the disk if we get a - // signal - sys::RemoveFileOnSignal(sys::Path(OutputFilename)); - } + // Make sure that the Out file gets unlinked from the disk if we get a + // signal + sys::RemoveFileOnSignal(sys::Path(OutputFilename)); + } - - if (!Out->good()) { - std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n"; - return 1; - } + + if (!Out->good()) { + std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n"; + return 1; + } - // In addition to just parsing the input from GCC, we also want to spiff it up - // a little bit. Do this now. - // - PassManager Passes; + // In addition to just parsing the input from GCC, we also want to spiff it up + // a little bit. Do this now. + // + PassManager Passes; - // Add an appropriate TargetData instance for this module... - Passes.add(new TargetData("gccas", M.get())); + // Add an appropriate TargetData instance for this module... + Passes.add(new TargetData("gccas", M.get())); - // Add all of the transformation passes to the pass manager to do the cleanup - // and optimization of the GCC output. - // - AddConfiguredTransformationPasses(Passes); + // Add all of the transformation passes to the pass manager to do the cleanup + // and optimization of the GCC output. + // + AddConfiguredTransformationPasses(Passes); - // Make sure everything is still good. - Passes.add(createVerifierPass()); + // Make sure everything is still good. + Passes.add(createVerifierPass()); - // Write bytecode to file... - Passes.add(new WriteBytecodePass(Out,false,!NoCompress)); + // Write bytecode to file... + Passes.add(new WriteBytecodePass(Out,false,!NoCompress)); - // Run our queue of passes all at once now, efficiently. - Passes.run(*M.get()); + // Run our queue of passes all at once now, efficiently. + Passes.run(*M.get()); - if (Out != &std::cout) delete Out; - return 0; + if (Out != &std::cout) delete Out; + return 0; + } catch (const std::string& msg) { + std::cerr << argv[0] << ": " << msg << "\n"; + } catch (...) { + std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n"; + } + return 1; } diff --git a/tools/llc/llc.cpp b/tools/llc/llc.cpp index ba624b2127..cbb2507f3f 100644 --- a/tools/llc/llc.cpp +++ b/tools/llc/llc.cpp @@ -63,110 +63,117 @@ GetFileNameRoot(const std::string &InputFilename) { // main - Entry point for the llc compiler. // int main(int argc, char **argv) { - cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n"); - sys::PrintStackTraceOnErrorSignal(); - - // Load the module to be compiled... - std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename)); - if (M.get() == 0) { - std::cerr << argv[0] << ": bytecode didn't read correctly.\n"; - return 1; - } - Module &mod = *M.get(); - - // Allocate target machine. First, check whether the user has - // explicitly specified an architecture to compile for. - TargetMachine* (*TargetMachineAllocator)(const Module&, - IntrinsicLowering *) = 0; - if (MArch == 0) { - std::string Err; - MArch = TargetMachineRegistry::getClosestStaticTargetForModule(mod, Err); - if (MArch == 0) { - std::cerr << argv[0] << ": error auto-selecting target for module '" - << Err << "'. Please use the -march option to explicitly " - << "pick a target.\n"; + try { + cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n"); + sys::PrintStackTraceOnErrorSignal(); + + // Load the module to be compiled... + std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename)); + if (M.get() == 0) { + std::cerr << argv[0] << ": bytecode didn't read correctly.\n"; return 1; } - } + Module &mod = *M.get(); - std::auto_ptr<TargetMachine> target(MArch->CtorFn(mod, 0)); - assert(target.get() && "Could not allocate target machine!"); - TargetMachine &Target = *target.get(); - const TargetData &TD = Target.getTargetData(); - - // Build up all of the passes that we want to do to the module... - PassManager Passes; - Passes.add(new TargetData("llc", TD.isLittleEndian(), TD.getPointerSize(), - TD.getPointerAlignment(), TD.getDoubleAlignment())); - - // Figure out where we are going to send the output... - std::ostream *Out = 0; - if (OutputFilename != "") { - if (OutputFilename != "-") { - // Specified an output filename? - if (!Force && std::ifstream(OutputFilename.c_str())) { - // If force is not specified, make sure not to overwrite a file! - std::cerr << argv[0] << ": error opening '" << OutputFilename - << "': file exists!\n" - << "Use -f command line argument to force output\n"; + // Allocate target machine. First, check whether the user has + // explicitly specified an architecture to compile for. + TargetMachine* (*TargetMachineAllocator)(const Module&, + IntrinsicLowering *) = 0; + if (MArch == 0) { + std::string Err; + MArch = TargetMachineRegistry::getClosestStaticTargetForModule(mod, Err); + if (MArch == 0) { + std::cerr << argv[0] << ": error auto-selecting target for module '" + << Err << "'. Please use the -march option to explicitly " + << "pick a target.\n"; return 1; } - Out = new std::ofstream(OutputFilename.c_str()); + } - // Make sure that the Out file gets unlinked from the disk if we get a - // SIGINT - sys::RemoveFileOnSignal(sys::Path(OutputFilename)); + std::auto_ptr<TargetMachine> target(MArch->CtorFn(mod, 0)); + assert(target.get() && "Could not allocate target machine!"); + TargetMachine &Target = *target.get(); + const TargetData &TD = Target.getTargetData(); + + // Build up all of the passes that we want to do to the module... + PassManager Passes; + Passes.add(new TargetData("llc", TD.isLittleEndian(), TD.getPointerSize(), + TD.getPointerAlignment(), TD.getDoubleAlignment())); + + // Figure out where we are going to send the output... + std::ostream *Out = 0; + if (OutputFilename != "") { + if (OutputFilename != "-") { + // Specified an output filename? + if (!Force && std::ifstream(OutputFilename.c_str())) { + // If force is not specified, make sure not to overwrite a file! + std::cerr << argv[0] << ": error opening '" << OutputFilename + << "': file exists!\n" + << "Use -f command line argument to force output\n"; + return 1; + } + Out = new std::ofstream(OutputFilename.c_str()); + + // Make sure that the Out file gets unlinked from the disk if we get a + // SIGINT + sys::RemoveFileOnSignal(sys::Path(OutputFilename)); + } else { + Out = &std::cout; + } } else { - Out = &std::cout; + if (InputFilename == "-") { + OutputFilename = "-"; + Out = &std::cout; + } else { + OutputFilename = GetFileNameRoot(InputFilename); + + if (MArch->Name[0] != 'c' || MArch->Name[1] != 0) // not CBE + OutputFilename += ".s"; + else + OutputFilename += ".cbe.c"; + + if (!Force && std::ifstream(OutputFilename.c_str())) { + // If force is not specified, make sure not to overwrite a file! + std::cerr << argv[0] << ": error opening '" << OutputFilename + << "': file exists!\n" + << "Use -f command line argument to force output\n"; + return 1; + } + + Out = new std::ofstream(OutputFilename.c_str()); + if (!Out->good()) { + std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n"; + delete Out; + return 1; + } + + // Make sure that the Out file gets unlinked from the disk if we get a + // SIGINT + sys::RemoveFileOnSignal(sys::Path(OutputFilename)); + } } - } else { - if (InputFilename == "-") { - OutputFilename = "-"; - Out = &std::cout; + + // Ask the target to add backend passes as necessary + if (Target.addPassesToEmitAssembly(Passes, *Out)) { + std::cerr << argv[0] << ": target '" << Target.getName() + << "' does not support static compilation!\n"; + if (Out != &std::cout) delete Out; + // And the Out file is empty and useless, so remove it now. + std::remove(OutputFilename.c_str()); + return 1; } else { - OutputFilename = GetFileNameRoot(InputFilename); - - if (MArch->Name[0] != 'c' || MArch->Name[1] != 0) // not CBE - OutputFilename += ".s"; - else - OutputFilename += ".cbe.c"; - - if (!Force && std::ifstream(OutputFilename.c_str())) { - // If force is not specified, make sure not to overwrite a file! - std::cerr << argv[0] << ": error opening '" << OutputFilename - << "': file exists!\n" - << "Use -f command line argument to force output\n"; - return 1; - } - - Out = new std::ofstream(OutputFilename.c_str()); - if (!Out->good()) { - std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n"; - delete Out; - return 1; - } - - // Make sure that the Out file gets unlinked from the disk if we get a - // SIGINT - sys::RemoveFileOnSignal(sys::Path(OutputFilename)); + // Run our queue of passes all at once now, efficiently. + Passes.run(*M.get()); } - } - // Ask the target to add backend passes as necessary - if (Target.addPassesToEmitAssembly(Passes, *Out)) { - std::cerr << argv[0] << ": target '" << Target.getName() - << "' does not support static compilation!\n"; + // Delete the ostream if it's not a stdout stream if (Out != &std::cout) delete Out; - // And the Out file is empty and useless, so remove it now. - std::remove(OutputFilename.c_str()); - return 1; - } else { - // Run our queue of passes all at once now, efficiently. - Passes.run(*M.get()); - } - // Delete the ostream if it's not a stdout stream - if (Out != &std::cout) delete Out; - - return 0; + return 0; + } catch (const std::string& msg) { + std::cerr << argv[0] << ": " << msg << "\n"; + } catch (...) { + std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n"; + } + return 1; } diff --git a/tools/lli/lli.cpp b/tools/lli/lli.cpp index eb8ac0126e..6d16ea5c80 100644 --- a/tools/lli/lli.cpp +++ b/tools/lli/lli.cpp @@ -47,62 +47,69 @@ namespace { // main Driver function // int main(int argc, char **argv, char * const *envp) { - cl::ParseCommandLineOptions(argc, argv, - " llvm interpreter & dynamic compiler\n"); - sys::PrintStackTraceOnErrorSignal(); - - // Load the bytecode... - std::string ErrorMsg; - ModuleProvider *MP = 0; try { - MP = getBytecodeModuleProvider(InputFile); - } catch (std::string &err) { - std::cerr << "Error loading program '" << InputFile << "': " << err << "\n"; - exit(1); - } - - ExecutionEngine *EE = ExecutionEngine::create(MP, ForceInterpreter); - assert(EE && "Couldn't create an ExecutionEngine, not even an interpreter?"); - - // If the user specifically requested an argv[0] to pass into the program, do - // it now. - if (!FakeArgv0.empty()) { - InputFile = FakeArgv0; - } else { - // Otherwise, if there is a .bc suffix on the executable strip it off, it - // might confuse the program. - if (InputFile.rfind(".bc") == InputFile.length() - 3) - InputFile.erase(InputFile.length() - 3); - } - - // Add the module's name to the start of the vector of arguments to main(). - InputArgv.insert(InputArgv.begin(), InputFile); - - // Call the main function from M as if its signature were: - // int main (int argc, char **argv, const char **envp) - // using the contents of Args to determine argc & argv, and the contents of - // EnvVars to determine envp. - // - Function *Fn = MP->getModule()->getMainFunction(); - if (!Fn) { - std::cerr << "'main' function not found in module.\n"; - return -1; + cl::ParseCommandLineOptions(argc, argv, + " llvm interpreter & dynamic compiler\n"); + sys::PrintStackTraceOnErrorSignal(); + + // Load the bytecode... + std::string ErrorMsg; + ModuleProvider *MP = 0; + try { + MP = getBytecodeModuleProvider(InputFile); + } catch (std::string &err) { + std::cerr << "Error loading program '" << InputFile << "': " << err << "\n"; + exit(1); + } + + ExecutionEngine *EE = ExecutionEngine::create(MP, ForceInterpreter); + assert(EE && "Couldn't create an ExecutionEngine, not even an interpreter?"); + + // If the user specifically requested an argv[0] to pass into the program, do + // it now. + if (!FakeArgv0.empty()) { + InputFile = FakeArgv0; + } else { + // Otherwise, if there is a .bc suffix on the executable strip it off, it + // might confuse the program. + if (InputFile.rfind(".bc") == InputFile.length() - 3) + InputFile.erase(InputFile.length() - 3); + } + + // Add the module's name to the start of the vector of arguments to main(). + InputArgv.insert(InputArgv.begin(), InputFile); + + // Call the main function from M as if its signature were: + // int main (int argc, char **argv, const char **envp) + // using the contents of Args to determine argc & argv, and the contents of + // EnvVars to determine envp. + // + Function *Fn = MP->getModule()->getMainFunction(); + if (!Fn) { + std::cerr << "'main' function not found in module.\n"; + return -1; + } + + // Run main... + int Result = EE->runFunctionAsMain(Fn, InputArgv, envp); + + // If the program didn't explicitly call exit, call exit now, for the program. + // This ensures that any atexit handlers get called correctly. + Function *Exit = MP->getModule()->getOrInsertFunction("exit", Type::VoidTy, + Type::IntTy, 0); + + std::vector<GenericValue> Args; + GenericValue ResultGV; + ResultGV.IntVal = Result; + Args.push_back(ResultGV); + EE->runFunction(Exit, Args); + + std::cerr << "ERROR: exit(" << Result << ") returned!\n"; + abort(); + } catch (const std::string& msg) { + std::cerr << argv[0] << ": " << msg << "\n"; + } catch (...) { + std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n"; } - - // Run main... - int Result = EE->runFunctionAsMain(Fn, InputArgv, envp); - - // If the program didn't explicitly call exit, call exit now, for the program. - // This ensures that any atexit handlers get called correctly. - Function *Exit = MP->getModule()->getOrInsertFunction("exit", Type::VoidTy, - Type::IntTy, 0); - - std::vector<GenericValue> Args; - GenericValue ResultGV; - ResultGV.IntVal = Result; - Args.push_back(ResultGV); - EE->runFunction(Exit, Args); - - std::cerr << "ERROR: exit(" << Result << ") returned!\n"; abort(); } diff --git a/tools/llvm-as/llvm-as.cpp b/tools/llvm-as/llvm-as.cpp index 7a777a4bab..804490a77e 100644 --- a/tools/llvm-as/llvm-as.cpp +++ b/tools/llvm-as/llvm-as.cpp @@ -52,6 +52,7 @@ int main(int argc, char **argv) { cl::ParseCommandLineOptions(argc, argv, " llvm .ll -> .bc assembler\n"); sys::PrintStackTraceOnErrorSignal(); + int exitCode = 0; std::ostream *Out = 0; try { // Parse the file now... @@ -126,10 +127,16 @@ int main(int argc, char **argv) { WriteBytecodeToFile(M.get(), *Out, !NoCompress); } catch (const ParseException &E) { std::cerr << argv[0] << ": " << E.getMessage() << "\n"; - return 1; + exitCode = 1; + } catch (const std::string& msg) { + std::cerr << argv[0] << ": " << msg << "\n"; + exitCode = 1; + } catch (...) { + std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n"; + exitCode = 1; } if (Out != &std::cout) delete Out; - return 0; + return exitCode; } diff --git a/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp b/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp index d4be138fe7..b79b6fe278 100644 --- a/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp +++ b/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp @@ -49,52 +49,58 @@ static cl::opt<bool> Dump ("dump", cl::desc("Dump low level bytecode trace" static cl::opt<bool> Verify ("verify", cl::desc("Progressively verify module")); int -main(int argc, char **argv) -{ - cl::ParseCommandLineOptions(argc, argv, - " llvm-bcanalyzer Analysis of ByteCode Dumper\n"); - - sys::PrintStackTraceOnErrorSignal(); - - std::ostream* Out = &std::cout; // Default to printing to stdout... - std::istream* In = &std::cin; // Default to reading stdin - std::string ErrorMessage; - BytecodeAnalysis bca; - - /// Determine what to generate - bca.detailedResults = !NoDetails; - bca.progressiveVerify = Verify; - - /// Analyze the bytecode file - Module* M = AnalyzeBytecodeFile(InputFilename, bca, &ErrorMessage, (Dump?Out:0)); - - // All that bcanalyzer does is write the gathered statistics to the output - PrintBytecodeAnalysis(bca,*Out); - - if ( M && Verify ) { - std::string verificationMsg; - try { - verifyModule( *M, ThrowExceptionAction ); - } catch (std::string& errmsg ) { - verificationMsg = errmsg; +main(int argc, char **argv) { + try { + cl::ParseCommandLineOptions(argc, argv, + " llvm-bcanalyzer Analysis of ByteCode Dumper\n"); + + sys::PrintStackTraceOnErrorSignal(); + + std::ostream* Out = &std::cout; // Default to printing to stdout... + std::istream* In = &std::cin; // Default to reading stdin + std::string ErrorMessage; + BytecodeAnalysis bca; + + /// Determine what to generate + bca.detailedResults = !NoDetails; + bca.progressiveVerify = Verify; + + /// Analyze the bytecode file + Module* M = AnalyzeBytecodeFile(InputFilename, bca, &ErrorMessage, (Dump?Out:0)); + + // All that bcanalyzer does is write the gathered statistics to the output + PrintBytecodeAnalysis(bca,*Out); + + if ( M && Verify ) { + std::string verificationMsg; + try { + verifyModule( *M, ThrowExceptionAction ); + } catch (std::string& errmsg ) { + verificationMsg = errmsg; + } + if ( verificationMsg.length() > 0 ) + std::cerr << "Final Verification Message: " << verificationMsg << "\n"; } - if ( verificationMsg.length() > 0 ) - std::cerr << "Final Verification Message: " << verificationMsg << "\n"; - } - // If there was an error, print it and stop. - if ( ErrorMessage.size() ) { - std::cerr << argv[0] << ": " << ErrorMessage << "\n"; - return 1; - } - + // If there was an error, print it and stop. + if ( ErrorMessage.size() ) { + std::cerr << argv[0] << ": " << ErrorMessage << "\n"; + return 1; + } + - if (Out != &std::cout) { - ((std::ofstream*)Out)->close(); - delete Out; + if (Out != &std::cout) { + ((std::ofstream*)Out)->close(); |