Write .bc files to binary ostreams. This shouldn't change anything on unix,
[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 "Support/CommandLine.h"
23 #include "llvm/System/Signals.h"
24 #include <fstream>
25 #include <memory>
26
27 using namespace llvm;
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   PrintStackTraceOnErrorSignal();
49
50   std::ostream *Out = 0;
51   try {
52     // Parse the file now...
53     std::auto_ptr<Module> M(ParseAssemblyFile(InputFilename));
54     if (M.get() == 0) {
55       std::cerr << argv[0] << ": assembly didn't read correctly.\n";
56       return 1;
57     }
58
59     if (!DisableVerify && verifyModule(*M.get())) {
60       std::cerr << argv[0]
61                 << ": assembly parsed, but does not verify as correct!\n";
62       return 1;
63     }
64   
65     if (DumpAsm) std::cerr << "Here's the assembly:\n" << M.get();
66
67     if (OutputFilename != "") {   // Specified an output filename?
68       if (OutputFilename != "-") {  // Not stdout?
69         if (!Force && std::ifstream(OutputFilename.c_str())) {
70           // If force is not specified, make sure not to overwrite a file!
71           std::cerr << argv[0] << ": error opening '" << OutputFilename
72                     << "': file exists!\n"
73                     << "Use -f command line argument to force output\n";
74           return 1;
75         }
76         Out = new std::ofstream(OutputFilename.c_str(), std::ios_base::out | 
77                                 std::ios_base::trunc | std::ios_base::binary);
78       } else {                      // Specified stdout
79         Out = &std::cout;       
80       }
81     } else {
82       if (InputFilename == "-") {
83         OutputFilename = "-";
84         Out = &std::cout;
85       } else {
86         std::string IFN = InputFilename;
87         int Len = IFN.length();
88         if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
89           // Source ends in .ll
90           OutputFilename = std::string(IFN.begin(), IFN.end()-3);
91         } else {
92           OutputFilename = IFN;   // Append a .bc to it
93         }
94         OutputFilename += ".bc";
95
96         if (!Force && std::ifstream(OutputFilename.c_str())) {
97           // If force is not specified, make sure not to overwrite a file!
98           std::cerr << argv[0] << ": error opening '" << OutputFilename
99                     << "': file exists!\n"
100                     << "Use -f command line argument to force output\n";
101           return 1;
102         }
103
104         Out = new std::ofstream(OutputFilename.c_str(), std::ios_base::out | 
105                                 std::ios_base::trunc | std::ios_base::binary);
106         // Make sure that the Out file gets unlinked from the disk if we get a
107         // SIGINT
108         RemoveFileOnSignal(OutputFilename);
109       }
110     }
111   
112     if (!Out->good()) {
113       std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
114       return 1;
115     }
116    
117     WriteBytecodeToFile(M.get(), *Out);
118   } catch (const ParseException &E) {
119     std::cerr << argv[0] << ": " << E.getMessage() << "\n";
120     return 1;
121   }
122
123   if (Out != &std::cout) delete Out;
124   return 0;
125 }
126