1 //===------------------------------------------------------------------------===
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 //===----------------------------------------------------------------------===//
12 // This utility may be invoked in the following manner:
13 // llvm-as --help - Output information about command line switches
14 // llvm-as [options] - Read LLVM asm from stdin, write bytecode to stdout
15 // llvm-as [options] x.ll - Read LLVM asm from the x.ll file, write bytecode
18 //===------------------------------------------------------------------------===
20 #include "llvm/Module.h"
21 #include "llvm/Assembly/Parser.h"
22 #include "llvm/Bytecode/Writer.h"
23 #include "llvm/Analysis/Verifier.h"
24 #include "Support/CommandLine.h"
25 #include "Support/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");
49 std::ostream *Out = 0;
51 // Parse the file now...
52 std::auto_ptr<Module> M(ParseAssemblyFile(InputFilename));
54 std::cerr << argv[0] << ": assembly didn't read correctly.\n";
58 if (!DisableVerify && verifyModule(*M.get())) {
60 << ": assembly parsed, but does not verify as correct!\n";
64 if (DumpAsm) std::cerr << "Here's the assembly:\n" << M.get();
66 if (OutputFilename != "") { // Specified an output filename?
67 if (OutputFilename != "-") { // Not stdout?
68 if (!Force && std::ifstream(OutputFilename.c_str())) {
69 // If force is not specified, make sure not to overwrite a file!
70 std::cerr << argv[0] << ": error opening '" << OutputFilename
71 << "': file exists!\n"
72 << "Use -f command line argument to force output\n";
75 Out = new std::ofstream(OutputFilename.c_str());
76 } else { // Specified stdout
80 if (InputFilename == "-") {
84 std::string IFN = InputFilename;
85 int Len = IFN.length();
86 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
88 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
90 OutputFilename = IFN; // Append a .bc to it
92 OutputFilename += ".bc";
94 if (!Force && std::ifstream(OutputFilename.c_str())) {
95 // If force is not specified, make sure not to overwrite a file!
96 std::cerr << argv[0] << ": error opening '" << OutputFilename
97 << "': file exists!\n"
98 << "Use -f command line argument to force output\n";
102 Out = new std::ofstream(OutputFilename.c_str());
103 // Make sure that the Out file gets unlinked from the disk if we get a
105 RemoveFileOnSignal(OutputFilename);
110 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
114 WriteBytecodeToFile(M.get(), *Out);
115 } catch (const ParseException &E) {
116 std::cerr << argv[0] << ": " << E.getMessage() << "\n";
120 if (Out != &std::cout) delete Out;