xref: /llvm-project/llvm/tools/llvm-as/llvm-as.cpp (revision 68c9977d7f66af7671094019891e6062b255ddaf)
1 //===------------------------------------------------------------------------===
2 // LLVM 'AS' UTILITY
3 //
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
8 //                       to the x.bc file.
9 //
10 //===------------------------------------------------------------------------===
11 
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"
17 #include <fstream>
18 #include <memory>
19 #include <iostream>
20 using std::cerr;
21 
22 cl::String InputFilename ("", "Parse <arg> file, compile to bytecode", 0, "-");
23 cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
24 cl::Flag   Force         ("f", "Overwrite output files", cl::NoFlags, false);
25 cl::Flag   DumpAsm       ("d", "Print assembly as parsed", cl::Hidden, false);
26 
27 int main(int argc, char **argv) {
28   cl::ParseCommandLineOptions(argc, argv, " llvm .ll -> .bc assembler\n");
29 
30   std::ostream *Out = 0;
31   try {
32     // Parse the file now...
33     std::auto_ptr<Module> M(ParseAssemblyFile(InputFilename));
34     if (M.get() == 0) {
35       cerr << "assembly didn't read correctly.\n";
36       return 1;
37     }
38 
39     if (DumpAsm) cerr << "Here's the assembly:\n" << M.get();
40 
41     if (OutputFilename != "") {   // Specified an output filename?
42       if (!Force && std::ifstream(OutputFilename.c_str())) {
43         // If force is not specified, make sure not to overwrite a file!
44         cerr << "Error opening '" << OutputFilename << "': File exists!\n"
45              << "Use -f command line argument to force output\n";
46         return 1;
47       }
48       Out = new std::ofstream(OutputFilename.c_str());
49     } else {
50       if (InputFilename == "-") {
51 	OutputFilename = "-";
52 	Out = &std::cout;
53       } else {
54 	std::string IFN = InputFilename;
55 	int Len = IFN.length();
56 	if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
57 	  // Source ends in .ll
58 	  OutputFilename = std::string(IFN.begin(), IFN.end()-3);
59         } else {
60 	  OutputFilename = IFN;   // Append a .bc to it
61 	}
62 	OutputFilename += ".bc";
63 
64         if (!Force && std::ifstream(OutputFilename.c_str())) {
65           // If force is not specified, make sure not to overwrite a file!
66           cerr << "Error opening '" << OutputFilename << "': File exists!\n"
67                << "Use -f command line argument to force output\n";
68           return 1;
69         }
70 
71 	Out = new std::ofstream(OutputFilename.c_str());
72         // Make sure that the Out file gets unlink'd from the disk if we get a
73         // SIGINT
74         RemoveFileOnSignal(OutputFilename);
75       }
76     }
77 
78     if (!Out->good()) {
79       cerr << "Error opening " << OutputFilename << "!\n";
80       return 1;
81     }
82 
83     WriteBytecodeToFile(M.get(), *Out);
84   } catch (const ParseException &E) {
85     cerr << E.getMessage() << std::endl;
86     return 1;
87   }
88 
89   if (Out != &std::cout) delete Out;
90   return 0;
91 }
92 
93