- // In addition to just parsing the input from GCC, we also want to spiff it up
- // a little bit. Do this now.
- //
- PassManager Passes;
- Passes.add(createFunctionExtractionPass(F)); // Extract the function
- Passes.add(createGlobalDCEPass()); // Delete unreachable globals
- Passes.add(createFunctionResolvingPass()); // Delete prototypes
- Passes.add(createDeadTypeEliminationPass()); // Remove dead types...
- Passes.add(new WriteBytecodePass(&std::cout)); // Write bytecode to file...
-
- Passes.run(*M.get());
- return 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;
+ }
+ std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
+ std::ios::binary;
+ Out = new std::ofstream(OutputFilename.c_str(), io_mode);
+ } else { // Specified stdout
+ // FIXME: cout is not binary!
+ Out = &std::cout;
+ }
+
+ Passes.add(new WriteBytecodePass(Out)); // Write bytecode to file...
+ Passes.run(*M.get());
+
+ 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;