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 "Support/CommandLine.h"
23 #include "llvm/System/Signals.h"
29 static cl::opt<std::string>
30 InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-"));
32 static cl::opt<std::string>
33 OutputFilename("o", cl::desc("Override output filename"),
34 cl::value_desc("filename"));
37 Force("f", cl::desc("Overwrite output files"));
40 DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden);
43 DisableVerify("disable-verify", cl::Hidden,
44 cl::desc("Do not run verifier on input LLVM (dangerous!"));
46 int main(int argc, char **argv) {
47 cl::ParseCommandLineOptions(argc, argv, " llvm .ll -> .bc assembler\n");
48 PrintStackTraceOnErrorSignal();
50 std::ostream *Out = 0;
52 // Parse the file now...
53 std::auto_ptr<Module> M(ParseAssemblyFile(InputFilename));
55 std::cerr << argv[0] << ": assembly didn't read correctly.\n";
59 if (!DisableVerify && verifyModule(*M.get())) {
61 << ": assembly parsed, but does not verify as correct!\n";
65 if (DumpAsm) std::cerr << "Here's the assembly:\n" << M.get();
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";
76 Out = new std::ofstream(OutputFilename.c_str());
77 } else { // Specified stdout
81 if (InputFilename == "-") {
85 std::string IFN = InputFilename;
86 int Len = IFN.length();
87 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
89 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
91 OutputFilename = IFN; // Append a .bc to it
93 OutputFilename += ".bc";
95 if (!Force && std::ifstream(OutputFilename.c_str())) {
96 // If force is not specified, make sure not to overwrite a file!
97 std::cerr << argv[0] << ": error opening '" << OutputFilename
98 << "': file exists!\n"
99 << "Use -f command line argument to force output\n";
103 Out = new std::ofstream(OutputFilename.c_str());
104 // Make sure that the Out file gets unlinked from the disk if we get a
106 RemoveFileOnSignal(OutputFilename);
111 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
115 WriteBytecodeToFile(M.get(), *Out);
116 } catch (const ParseException &E) {
117 std::cerr << argv[0] << ": " << E.getMessage() << "\n";
121 if (Out != &std::cout) delete Out;