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 "llvm/Analysis/Verifier.h"
16 #include "Support/CommandLine.h"
17 #include "Support/Signals.h"
23 static cl::opt<string>
24 InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-"));
26 static cl::opt<string>
27 OutputFilename("o", cl::desc("Override output filename"),
28 cl::value_desc("filename"));
31 Force("f", cl::desc("Overwrite output files"));
34 DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden);
36 int main(int argc, char **argv) {
37 cl::ParseCommandLineOptions(argc, argv, " llvm .ll -> .bc assembler\n");
39 std::ostream *Out = 0;
41 // Parse the file now...
42 std::auto_ptr<Module> M(ParseAssemblyFile(InputFilename));
44 cerr << argv[0] << ": assembly didn't read correctly.\n";
48 if (verifyModule(*M.get())) {
49 cerr << argv[0] << ": assembly parsed, but does not verify as correct!\n";
54 if (DumpAsm) cerr << "Here's the assembly:\n" << M.get();
56 if (OutputFilename != "") { // Specified an output filename?
57 if (!Force && std::ifstream(OutputFilename.c_str())) {
58 // If force is not specified, make sure not to overwrite a file!
59 cerr << argv[0] << ": error opening '" << OutputFilename
60 << "': file exists!\n"
61 << "Use -f command line argument to force output\n";
64 Out = new std::ofstream(OutputFilename.c_str());
66 if (InputFilename == "-") {
70 std::string IFN = InputFilename;
71 int Len = IFN.length();
72 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
74 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
76 OutputFilename = IFN; // Append a .bc to it
78 OutputFilename += ".bc";
80 if (!Force && std::ifstream(OutputFilename.c_str())) {
81 // If force is not specified, make sure not to overwrite a file!
82 cerr << argv[0] << ": error opening '" << OutputFilename
83 << "': file exists!\n"
84 << "Use -f command line argument to force output\n";
88 Out = new std::ofstream(OutputFilename.c_str());
89 // Make sure that the Out file gets unlink'd from the disk if we get a
91 RemoveFileOnSignal(OutputFilename);
96 cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
100 WriteBytecodeToFile(M.get(), *Out);
101 } catch (const ParseException &E) {
102 cerr << argv[0] << ": " << E.getMessage() << "\n";
106 if (Out != &std::cout) delete Out;