xref: /llvm-project/llvm/tools/llvm-as/llvm-as.cpp (revision 09344dcf97da052fa6493e08459121968df451df)
1 //===------------------------------------------------------------------------===
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // LLVM 'AS' UTILITY
11 //
12 //  This utility may be invoked in the following manner:
13 //   llvm-as --help         - Output information about command line switches
14 //   llvm-as [options]      - Read LLVM asm from stdin, write bytecode to stdout
15 //   llvm-as [options] x.ll - Read LLVM asm from the x.ll file, write bytecode
16 //                            to the x.bc file.
17 //
18 //===------------------------------------------------------------------------===
19 
20 #include "llvm/Module.h"
21 #include "llvm/Assembly/Parser.h"
22 #include "llvm/Bytecode/Writer.h"
23 #include "llvm/Analysis/Verifier.h"
24 #include "Support/CommandLine.h"
25 #include "Support/Signals.h"
26 #include <fstream>
27 #include <memory>
28 
29 static cl::opt<std::string>
30 InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-"));
31 
32 static cl::opt<std::string>
33 OutputFilename("o", cl::desc("Override output filename"),
34                cl::value_desc("filename"));
35 
36 static cl::opt<bool>
37 Force("f", cl::desc("Overwrite output files"));
38 
39 static cl::opt<bool>
40 DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden);
41 
42 static cl::opt<bool>
43 DisableVerify("disable-verify", cl::Hidden,
44               cl::desc("Do not run verifier on input LLVM (dangerous!"));
45 
46 int main(int argc, char **argv) {
47   cl::ParseCommandLineOptions(argc, argv, " llvm .ll -> .bc assembler\n");
48 
49   std::ostream *Out = 0;
50   try {
51     // Parse the file now...
52     std::auto_ptr<Module> M(ParseAssemblyFile(InputFilename));
53     if (M.get() == 0) {
54       std::cerr << argv[0] << ": assembly didn't read correctly.\n";
55       return 1;
56     }
57 
58     if (!DisableVerify && verifyModule(*M.get())) {
59       std::cerr << argv[0]
60                 << ": assembly parsed, but does not verify as correct!\n";
61       return 1;
62     }
63 
64     if (DumpAsm) std::cerr << "Here's the assembly:\n" << M.get();
65 
66     if (OutputFilename != "") {   // Specified an output filename?
67       if (OutputFilename != "-") {  // Not stdout?
68         if (!Force && std::ifstream(OutputFilename.c_str())) {
69           // If force is not specified, make sure not to overwrite a file!
70           std::cerr << argv[0] << ": error opening '" << OutputFilename
71                     << "': file exists!\n"
72                     << "Use -f command line argument to force output\n";
73           return 1;
74         }
75         Out = new std::ofstream(OutputFilename.c_str());
76       } else {                      // Specified stdout
77 	Out = &std::cout;
78       }
79     } else {
80       if (InputFilename == "-") {
81 	OutputFilename = "-";
82 	Out = &std::cout;
83       } else {
84 	std::string IFN = InputFilename;
85 	int Len = IFN.length();
86 	if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
87 	  // Source ends in .ll
88 	  OutputFilename = std::string(IFN.begin(), IFN.end()-3);
89         } else {
90 	  OutputFilename = IFN;   // Append a .bc to it
91 	}
92 	OutputFilename += ".bc";
93 
94         if (!Force && std::ifstream(OutputFilename.c_str())) {
95           // If force is not specified, make sure not to overwrite a file!
96           std::cerr << argv[0] << ": error opening '" << OutputFilename
97                     << "': file exists!\n"
98                     << "Use -f command line argument to force output\n";
99           return 1;
100         }
101 
102 	Out = new std::ofstream(OutputFilename.c_str());
103         // Make sure that the Out file gets unlinked from the disk if we get a
104         // SIGINT
105         RemoveFileOnSignal(OutputFilename);
106       }
107     }
108 
109     if (!Out->good()) {
110       std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
111       return 1;
112     }
113 
114     WriteBytecodeToFile(M.get(), *Out);
115   } catch (const ParseException &E) {
116     std::cerr << argv[0] << ": " << E.getMessage() << "\n";
117     return 1;
118   }
119 
120   if (Out != &std::cout) delete Out;
121   return 0;
122 }
123 
124