Replacing std::iostreams with llvm iostreams. Some of these changes involve
[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/Support/Streams.h"
24 #include "llvm/Support/SystemUtils.h"
25 #include "llvm/System/Signals.h"
26 #include <fstream>
27 #include <iostream>
28 #include <memory>
29 using namespace llvm;
30
31 static cl::opt<std::string>
32 InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-"));
33
34 static cl::opt<std::string>
35 OutputFilename("o", cl::desc("Override output filename"),
36                cl::value_desc("filename"));
37
38 static cl::opt<bool>
39 Force("f", cl::desc("Overwrite output files"));
40
41 static cl::opt<bool>
42 DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden);
43
44 static cl::opt<bool>
45 NoCompress("disable-compression", cl::init(false),
46            cl::desc("Don't compress the generated bytecode"));
47
48 static cl::opt<bool>
49 DisableVerify("disable-verify", cl::Hidden,
50               cl::desc("Do not run verifier on input LLVM (dangerous!)"));
51
52 int main(int argc, char **argv) {
53   cl::ParseCommandLineOptions(argc, argv, " llvm .ll -> .bc assembler\n");
54   sys::PrintStackTraceOnErrorSignal();
55
56   int exitCode = 0;
57   std::ostream *Out = 0;
58   try {
59     // Parse the file now...
60     ParseError Err;
61     std::auto_ptr<Module> M(ParseAssemblyFile(InputFilename,&Err));
62     if (M.get() == 0) {
63       llvm_cerr << argv[0] << ": " << Err.getMessage() << "\n"; 
64       return 1;
65     }
66
67     if (!DisableVerify) {
68       std::string Err;
69       if (verifyModule(*M.get(), ReturnStatusAction, &Err)) {
70         llvm_cerr << argv[0]
71                   << ": assembly parsed, but does not verify as correct!\n";
72         llvm_cerr << Err;
73         return 1;
74       } 
75     }
76
77     if (DumpAsm) llvm_cerr << "Here's the assembly:\n" << *M.get();
78
79     if (OutputFilename != "") {   // Specified an output filename?
80       if (OutputFilename != "-") {  // Not stdout?
81         if (!Force && std::ifstream(OutputFilename.c_str())) {
82           // If force is not specified, make sure not to overwrite a file!
83           llvm_cerr << argv[0] << ": error opening '" << OutputFilename
84                     << "': file exists!\n"
85                     << "Use -f command line argument to force output\n";
86           return 1;
87         }
88         Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
89                                 std::ios::trunc | std::ios::binary);
90       } else {                      // Specified stdout
91         // FIXME: cout is not binary!
92         Out = &std::cout;
93       }
94     } else {
95       if (InputFilename == "-") {
96         OutputFilename = "-";
97         Out = &std::cout;
98       } else {
99         std::string IFN = InputFilename;
100         int Len = IFN.length();
101         if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
102           // Source ends in .ll
103           OutputFilename = std::string(IFN.begin(), IFN.end()-3);
104         } else {
105           OutputFilename = IFN;   // Append a .bc to it
106         }
107         OutputFilename += ".bc";
108
109         if (!Force && std::ifstream(OutputFilename.c_str())) {
110           // If force is not specified, make sure not to overwrite a file!
111           llvm_cerr << argv[0] << ": error opening '" << OutputFilename
112                     << "': file exists!\n"
113                     << "Use -f command line argument to force output\n";
114           return 1;
115         }
116
117         Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
118                                 std::ios::trunc | std::ios::binary);
119         // Make sure that the Out file gets unlinked from the disk if we get a
120         // SIGINT
121         sys::RemoveFileOnSignal(sys::Path(OutputFilename));
122       }
123     }
124
125     if (!Out->good()) {
126       llvm_cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
127       return 1;
128     }
129
130     if (Force || !CheckBytecodeOutputToConsole(Out,true)) {
131       llvm_ostream L(*Out);
132       WriteBytecodeToFile(M.get(), L, !NoCompress);
133     }
134   } catch (const std::string& msg) {
135     llvm_cerr << argv[0] << ": " << msg << "\n";
136     exitCode = 1;
137   } catch (...) {
138     llvm_cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
139     exitCode = 1;
140   }
141
142   if (Out != &std::cout) delete Out;
143   return exitCode;
144 }
145