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"
20 cl::String InputFilename ("", "Parse <arg> file, compile to bytecode", 0, "-");
21 cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
22 cl::Flag Force ("f", "Overwrite output files", cl::NoFlags, false);
23 cl::Flag DumpAsm ("d", "Print assembly as parsed", cl::Hidden, false);
25 int main(int argc, char **argv) {
26 cl::ParseCommandLineOptions(argc, argv, " llvm .ll -> .bc assembler\n");
30 // Parse the file now...
31 std::auto_ptr<Module> M(ParseAssemblyFile(InputFilename));
33 cerr << "assembly didn't read correctly.\n";
38 cerr << "Here's the assembly:\n";
42 if (OutputFilename != "") { // Specified an output filename?
43 if (!Force && std::ifstream(OutputFilename.c_str())) {
44 // If force is not specified, make sure not to overwrite a file!
45 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
46 << "Use -f command line argument to force output\n";
49 Out = new std::ofstream(OutputFilename.c_str());
51 if (InputFilename == "-") {
55 std::string IFN = InputFilename;
56 int Len = IFN.length();
57 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
59 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
61 OutputFilename = IFN; // Append a .bc to it
63 OutputFilename += ".bc";
65 if (!Force && std::ifstream(OutputFilename.c_str())) {
66 // If force is not specified, make sure not to overwrite a file!
67 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
68 << "Use -f command line argument to force output\n";
72 Out = new std::ofstream(OutputFilename.c_str());
77 cerr << "Error opening " << OutputFilename << "!\n";
81 WriteBytecodeToFile(M.get(), *Out);
82 } catch (const ParseException &E) {
83 cerr << E.getMessage() << endl;
87 if (Out != &cout) delete Out;