For PR351:
[oota-llvm.git] / tools / llvm-as / llvm-as.cpp
1 //===--- llvm-as.cpp - The low-level LLVM assembler -----------------------===//
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 //  This utility may be invoked in the following manner:
11 //   llvm-as --help         - Output information about command line switches
12 //   llvm-as [options]      - Read LLVM asm from stdin, write bytecode to stdout
13 //   llvm-as [options] x.ll - Read LLVM asm from the x.ll file, write bytecode
14 //                            to the x.bc file.
15 // 
16 //===------------------------------------------------------------------------===
17
18 #include "llvm/Module.h"
19 #include "llvm/Assembly/Parser.h"
20 #include "llvm/Bytecode/Writer.h"
21 #include "llvm/Analysis/Verifier.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/System/Signals.h"
24 #include <fstream>
25 #include <iostream>
26 #include <memory>
27
28 using namespace llvm;
29
30 static cl::opt<std::string> 
31 InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-"));
32
33 static cl::opt<std::string>
34 OutputFilename("o", cl::desc("Override output filename"),
35                cl::value_desc("filename"));
36
37 static cl::opt<bool>
38 Force("f", cl::desc("Overwrite output files"));
39
40 static cl::opt<bool>
41 DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden);
42
43 static cl::opt<bool> 
44 NoCompress("disable-compression", cl::init(false),
45            cl::desc("Don't ompress the generated bytecode"));
46
47 static cl::opt<bool>
48 DisableVerify("disable-verify", cl::Hidden,
49               cl::desc("Do not run verifier on input LLVM (dangerous!)"));
50
51 int main(int argc, char **argv) {
52   cl::ParseCommandLineOptions(argc, argv, " llvm .ll -> .bc assembler\n");
53   sys::PrintStackTraceOnErrorSignal();
54
55   int exitCode = 0;
56   std::ostream *Out = 0;
57   try {
58     // Parse the file now...
59     std::auto_ptr<Module> M(ParseAssemblyFile(InputFilename));
60     if (M.get() == 0) {
61       std::cerr << argv[0] << ": assembly didn't read correctly.\n";
62       return 1;
63     }
64
65     try {
66       if (!DisableVerify)
67         verifyModule(*M.get(), ThrowExceptionAction);
68     } catch (const std::string &Err) {
69       std::cerr << argv[0]
70                 << ": assembly parsed, but does not verify as correct!\n";
71       std::cerr << Err;
72       return 1;
73     }
74   
75     if (DumpAsm) std::cerr << "Here's the assembly:\n" << M.get();
76
77     if (OutputFilename != "") {   // Specified an output filename?
78       if (OutputFilename != "-") {  // Not stdout?
79         if (!Force && std::ifstream(OutputFilename.c_str())) {
80           // If force is not specified, make sure not to overwrite a file!
81           std::cerr << argv[0] << ": error opening '" << OutputFilename
82                     << "': file exists!\n"
83                     << "Use -f command line argument to force output\n";
84           return 1;
85         }
86         Out = new std::ofstream(OutputFilename.c_str(), std::ios_base::out | 
87                                 std::ios_base::trunc | std::ios_base::binary);
88       } else {                      // Specified stdout
89         Out = &std::cout;       
90       }
91     } else {
92       if (InputFilename == "-") {
93         OutputFilename = "-";
94         Out = &std::cout;
95       } else {
96         std::string IFN = InputFilename;
97         int Len = IFN.length();
98         if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
99           // Source ends in .ll
100           OutputFilename = std::string(IFN.begin(), IFN.end()-3);
101         } else {
102           OutputFilename = IFN;   // Append a .bc to it
103         }
104         OutputFilename += ".bc";
105
106         if (!Force && std::ifstream(OutputFilename.c_str())) {
107           // If force is not specified, make sure not to overwrite a file!
108           std::cerr << argv[0] << ": error opening '" << OutputFilename
109                     << "': file exists!\n"
110                     << "Use -f command line argument to force output\n";
111           return 1;
112         }
113
114         Out = new std::ofstream(OutputFilename.c_str(), std::ios_base::out | 
115                                 std::ios_base::trunc | std::ios_base::binary);
116         // Make sure that the Out file gets unlinked from the disk if we get a
117         // SIGINT
118         sys::RemoveFileOnSignal(sys::Path(OutputFilename));
119       }
120     }
121   
122     if (!Out->good()) {
123       std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
124       return 1;
125     }
126    
127     WriteBytecodeToFile(M.get(), *Out, !NoCompress);
128   } catch (const ParseException &E) {
129     std::cerr << argv[0] << ": " << E.getMessage() << "\n";
130     exitCode = 1;
131   } catch (const std::string& msg) {
132     std::cerr << argv[0] << ": " << msg << "\n";
133     exitCode = 1;
134   } catch (...) {
135     std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
136     exitCode = 1;
137   }
138
139   if (Out != &std::cout) delete Out;
140   return exitCode;
141 }
142