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 //===------------------------------------------------------------------------===
15 #include "llvm/Module.h"
16 #include "llvm/Assembly/Parser.h"
17 #include "llvm/Assembly/Writer.h"
18 #include "llvm/Bytecode/Writer.h"
19 #include "llvm/Tools/CommandLine.h"
21 cl::String InputFilename ("", "Parse <arg> file, compile to bytecode", 0, "-");
22 cl::String OutputFilename("o", "Override output filename", 0, "");
23 cl::Flag Force ("f", "Overwrite output files", 0, 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 Module *C = ParseAssemblyFile(InputFilename.getValue());
34 cerr << "assembly didn't read correctly.\n";
38 if (DumpAsm.getValue())
39 cerr << "Here's the assembly:\n" << C;
41 if (OutputFilename.getValue() != "") { // Specified an output filename?
42 Out = new ofstream(OutputFilename.getValue().c_str(),
43 (Force.getValue() ? 0 : ios::noreplace)|ios::out);
45 if (InputFilename.getValue() == "-") {
46 OutputFilename.setValue("-");
49 string IFN = InputFilename.getValue();
50 int Len = IFN.length();
51 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
53 OutputFilename.setValue(string(IFN.begin(), IFN.end()-3));
55 OutputFilename.setValue(IFN); // Append a .bc to it
57 OutputFilename.setValue(OutputFilename.getValue() + ".bc");
58 Out = new ofstream(OutputFilename.getValue().c_str(),
59 (Force.getValue() ? 0 : ios::noreplace)|ios::out);
63 cerr << "Error opening " << OutputFilename.getValue() << "!\n";
69 WriteBytecodeToFile(C, *Out);
72 } catch (const ParseException &E) {
73 cerr << E.getMessage() << endl;
77 if (Out != &cout) delete Out;