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/Assembly/Writer.h"
15 #include "llvm/Bytecode/Writer.h"
16 #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 Module *C = ParseAssemblyFile(InputFilename);
33 cerr << "assembly didn't read correctly.\n";
38 cerr << "Here's the assembly:\n" << C;
40 if (OutputFilename != "") { // Specified an output filename?
41 Out = new ofstream(OutputFilename.c_str(),
42 (Force ? 0 : ios::noreplace)|ios::out);
44 if (InputFilename == "-") {
48 string IFN = InputFilename;
49 int Len = IFN.length();
50 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
52 OutputFilename = string(IFN.begin(), IFN.end()-3);
54 OutputFilename = IFN; // Append a .bc to it
56 OutputFilename += ".bc";
57 Out = new ofstream(OutputFilename.c_str(),
58 (Force ? 0 : ios::noreplace)|ios::out);
62 cerr << "Error opening " << OutputFilename << "!\n";
68 WriteBytecodeToFile(C, *Out);
71 } catch (const ParseException &E) {
72 cerr << E.getMessage() << endl;
76 if (Out != &cout) delete Out;