1 //===------------------------------------------------------------------------===
4 // This utility may be invoked in the following manner:
5 // as --help - Output information about command line switches
6 // as [options] - Read LLVM assembly from stdin, write bytecode to stdout
7 // as [options] x.ll - Read LLVM assembly from the x.ll file, write bytecode
10 //===------------------------------------------------------------------------===
12 #include "llvm/Module.h"
13 #include "llvm/Assembly/Parser.h"
14 #include "llvm/Bytecode/Writer.h"
15 #include "Support/CommandLine.h"
16 #include "Support/Signals.h"
22 static cl::opt<string>
23 InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-"));
25 static cl::opt<string>
26 OutputFilename("o", cl::desc("Override output filename"),
27 cl::value_desc("filename"));
30 Force("f", cl::desc("Overwrite output files"));
33 DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden);
35 int main(int argc, char **argv) {
36 cl::ParseCommandLineOptions(argc, argv, " llvm .ll -> .bc assembler\n");
38 std::ostream *Out = 0;
40 // Parse the file now...
41 std::auto_ptr<Module> M(ParseAssemblyFile(InputFilename));
43 cerr << argv[0] << ": assembly didn't read correctly.\n";
47 if (DumpAsm) cerr << "Here's the assembly:\n" << M.get();
49 if (OutputFilename != "") { // Specified an output filename?
50 if (!Force && std::ifstream(OutputFilename.c_str())) {
51 // If force is not specified, make sure not to overwrite a file!
52 cerr << argv[0] << ": error opening '" << OutputFilename
53 << "': file exists!\n"
54 << "Use -f command line argument to force output\n";
57 Out = new std::ofstream(OutputFilename.c_str());
59 if (InputFilename == "-") {
63 std::string IFN = InputFilename;
64 int Len = IFN.length();
65 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
67 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
69 OutputFilename = IFN; // Append a .bc to it
71 OutputFilename += ".bc";
73 if (!Force && std::ifstream(OutputFilename.c_str())) {
74 // If force is not specified, make sure not to overwrite a file!
75 cerr << argv[0] << ": error opening '" << OutputFilename
76 << "': file exists!\n"
77 << "Use -f command line argument to force output\n";
81 Out = new std::ofstream(OutputFilename.c_str());
82 // Make sure that the Out file gets unlink'd from the disk if we get a
84 RemoveFileOnSignal(OutputFilename);
89 cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
93 WriteBytecodeToFile(M.get(), *Out);
94 } catch (const ParseException &E) {
95 cerr << argv[0] << ": " << E.getMessage() << "\n";
99 if (Out != &std::cout) delete Out;