1 //===--- llvm-as.cpp - The low-level LLVM assembler -----------------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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
16 //===------------------------------------------------------------------------===
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"
30 static cl::opt<std::string>
31 InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-"));
33 static cl::opt<std::string>
34 OutputFilename("o", cl::desc("Override output filename"),
35 cl::value_desc("filename"));
38 Force("f", cl::desc("Overwrite output files"));
41 DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden);
44 NoCompress("disable-compression", cl::init(false),
45 cl::desc("Don't ompress the generated bytecode"));
48 DisableVerify("disable-verify", cl::Hidden,
49 cl::desc("Do not run verifier on input LLVM (dangerous!)"));
51 int main(int argc, char **argv) {
52 cl::ParseCommandLineOptions(argc, argv, " llvm .ll -> .bc assembler\n");
53 sys::PrintStackTraceOnErrorSignal();
55 std::ostream *Out = 0;
57 // Parse the file now...
58 std::auto_ptr<Module> M(ParseAssemblyFile(InputFilename));
60 std::cerr << argv[0] << ": assembly didn't read correctly.\n";
66 verifyModule(*M.get(), ThrowExceptionAction);
67 } catch (const std::string &Err) {
69 << ": assembly parsed, but does not verify as correct!\n";
74 if (DumpAsm) std::cerr << "Here's the assembly:\n" << M.get();
76 if (OutputFilename != "") { // Specified an output filename?
77 if (OutputFilename != "-") { // Not stdout?
78 if (!Force && std::ifstream(OutputFilename.c_str())) {
79 // If force is not specified, make sure not to overwrite a file!
80 std::cerr << argv[0] << ": error opening '" << OutputFilename
81 << "': file exists!\n"
82 << "Use -f command line argument to force output\n";
85 Out = new std::ofstream(OutputFilename.c_str(), std::ios_base::out |
86 std::ios_base::trunc | std::ios_base::binary);
87 } else { // Specified stdout
91 if (InputFilename == "-") {
95 std::string IFN = InputFilename;
96 int Len = IFN.length();
97 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
99 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
101 OutputFilename = IFN; // Append a .bc to it
103 OutputFilename += ".bc";
105 if (!Force && std::ifstream(OutputFilename.c_str())) {
106 // If force is not specified, make sure not to overwrite a file!
107 std::cerr << argv[0] << ": error opening '" << OutputFilename
108 << "': file exists!\n"
109 << "Use -f command line argument to force output\n";
113 Out = new std::ofstream(OutputFilename.c_str(), std::ios_base::out |
114 std::ios_base::trunc | std::ios_base::binary);
115 // Make sure that the Out file gets unlinked from the disk if we get a
117 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
122 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
126 WriteBytecodeToFile(M.get(), *Out, !NoCompress);
127 } catch (const ParseException &E) {
128 std::cerr << argv[0] << ": " << E.getMessage() << "\n";
132 if (Out != &std::cout) delete Out;