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"
21 cl::String InputFilename ("", "Parse <arg> file, compile to bytecode", 0, "-");
22 cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
23 cl::Flag Force ("f", "Overwrite output files", cl::NoFlags, false);
24 cl::Flag DumpAsm ("d", "Print assembly as parsed", cl::Hidden, false);
26 int main(int argc, char **argv) {
27 cl::ParseCommandLineOptions(argc, argv, " llvm .ll -> .bc assembler\n");
31 // Parse the file now...
32 std::auto_ptr<Module> M(ParseAssemblyFile(InputFilename));
34 cerr << "assembly didn't read correctly.\n";
39 cerr << "Here's the assembly:\n";
43 if (OutputFilename != "") { // Specified an output filename?
44 if (!Force && std::ifstream(OutputFilename.c_str())) {
45 // If force is not specified, make sure not to overwrite a file!
46 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
47 << "Use -f command line argument to force output\n";
50 Out = new std::ofstream(OutputFilename.c_str());
52 if (InputFilename == "-") {
56 std::string IFN = InputFilename;
57 int Len = IFN.length();
58 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
60 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
62 OutputFilename = IFN; // Append a .bc to it
64 OutputFilename += ".bc";
66 if (!Force && std::ifstream(OutputFilename.c_str())) {
67 // If force is not specified, make sure not to overwrite a file!
68 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
69 << "Use -f command line argument to force output\n";
73 Out = new std::ofstream(OutputFilename.c_str());
74 // Make sure that the Out file gets unlink'd from the disk if we get a
76 RemoveFileOnSignal(OutputFilename);
81 cerr << "Error opening " << OutputFilename << "!\n";
85 WriteBytecodeToFile(M.get(), *Out);
86 } catch (const ParseException &E) {
87 cerr << E.getMessage() << endl;
91 if (Out != &cout) delete Out;