Add reg-imm variants
[llvm/msp430.git] / tools / llvm-as / llvm-as.cpp
blob79ece8f106f49708b256866ab6cfc73a7f2b12db
1 //===--- llvm-as.cpp - The low-level LLVM assembler -----------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // 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 bitcode to stdout
13 // llvm-as [options] x.ll - Read LLVM asm from the x.ll file, write bitcode
14 // to the x.bc file.
16 //===----------------------------------------------------------------------===//
18 #include "llvm/Module.h"
19 #include "llvm/Assembly/Parser.h"
20 #include "llvm/Analysis/Verifier.h"
21 #include "llvm/Bitcode/ReaderWriter.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/Support/ManagedStatic.h"
24 #include "llvm/Support/PrettyStackTrace.h"
25 #include "llvm/Support/Streams.h"
26 #include "llvm/Support/SystemUtils.h"
27 #include "llvm/Support/raw_ostream.h"
28 #include "llvm/System/Signals.h"
29 #include <fstream>
30 #include <iostream>
31 #include <memory>
32 using namespace llvm;
34 static cl::opt<std::string>
35 InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-"));
37 static cl::opt<std::string>
38 OutputFilename("o", cl::desc("Override output filename"),
39 cl::value_desc("filename"));
41 static cl::opt<bool>
42 Force("f", cl::desc("Overwrite output files"));
44 static cl::opt<bool>
45 DisableOutput("disable-output", cl::desc("Disable output"), cl::init(false));
47 static cl::opt<bool>
48 DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden);
50 static cl::opt<bool>
51 DisableVerify("disable-verify", cl::Hidden,
52 cl::desc("Do not run verifier on input LLVM (dangerous!)"));
54 int main(int argc, char **argv) {
55 // Print a stack trace if we signal out.
56 sys::PrintStackTraceOnErrorSignal();
57 PrettyStackTraceProgram X(argc, argv);
58 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
59 cl::ParseCommandLineOptions(argc, argv, "llvm .ll -> .bc assembler\n");
61 int exitCode = 0;
62 std::ostream *Out = 0;
63 try {
64 // Parse the file now...
65 ParseError Err;
66 std::auto_ptr<Module> M(ParseAssemblyFile(InputFilename, Err));
67 if (M.get() == 0) {
68 Err.PrintError(argv[0], errs());
69 return 1;
72 if (!DisableVerify) {
73 std::string Err;
74 if (verifyModule(*M.get(), ReturnStatusAction, &Err)) {
75 cerr << argv[0]
76 << ": assembly parsed, but does not verify as correct!\n";
77 cerr << Err;
78 return 1;
82 if (DumpAsm) cerr << "Here's the assembly:\n" << *M.get();
84 if (OutputFilename != "") { // Specified an output filename?
85 if (OutputFilename != "-") { // Not stdout?
86 if (!Force && std::ifstream(OutputFilename.c_str())) {
87 // If force is not specified, make sure not to overwrite a file!
88 cerr << argv[0] << ": error opening '" << OutputFilename
89 << "': file exists!\n"
90 << "Use -f command line argument to force output\n";
91 return 1;
93 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
94 std::ios::trunc | std::ios::binary);
95 } else { // Specified stdout
96 // FIXME: cout is not binary!
97 Out = &std::cout;
99 } else {
100 if (InputFilename == "-") {
101 OutputFilename = "-";
102 Out = &std::cout;
103 } else {
104 std::string IFN = InputFilename;
105 int Len = IFN.length();
106 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
107 // Source ends in .ll
108 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
109 } else {
110 OutputFilename = IFN; // Append a .bc to it
112 OutputFilename += ".bc";
114 if (!Force && std::ifstream(OutputFilename.c_str())) {
115 // If force is not specified, make sure not to overwrite a file!
116 cerr << argv[0] << ": error opening '" << OutputFilename
117 << "': file exists!\n"
118 << "Use -f command line argument to force output\n";
119 return 1;
122 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
123 std::ios::trunc | std::ios::binary);
124 // Make sure that the Out file gets unlinked from the disk if we get a
125 // SIGINT
126 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
130 if (!Out->good()) {
131 cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
132 return 1;
135 if (!DisableOutput)
136 if (Force || !CheckBitcodeOutputToConsole(Out,true))
137 WriteBitcodeToFile(M.get(), *Out);
138 } catch (const std::string& msg) {
139 cerr << argv[0] << ": " << msg << "\n";
140 exitCode = 1;
141 } catch (...) {
142 cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
143 exitCode = 1;
146 if (Out != &std::cout) delete Out;
147 return exitCode;