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