blob: 804490a77ea67d50aba66ad064a99af87bf487aa [file] [log] [blame]
Chris Lattner10cf8fa2003-10-20 17:52:11 +00001//===--- llvm-as.cpp - The low-level LLVM assembler -----------------------===//
John Criswell7c0e0222003-10-20 17:47:21 +00002//
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//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This utility may be invoked in the following manner:
Misha Brukmancbb62dd2003-08-28 21:32:57 +000011// 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.
Anand Shukla63aaa112002-06-25 21:43:28 +000015//
Chris Lattner00950542001-06-06 20:29:01 +000016//===------------------------------------------------------------------------===
17
Chris Lattner00950542001-06-06 20:29:01 +000018#include "llvm/Module.h"
19#include "llvm/Assembly/Parser.h"
Chris Lattner00950542001-06-06 20:29:01 +000020#include "llvm/Bytecode/Writer.h"
Chris Lattner1acbea12002-08-30 22:54:41 +000021#include "llvm/Analysis/Verifier.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000022#include "llvm/Support/CommandLine.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000023#include "llvm/System/Signals.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000024#include <fstream>
Reid Spencer86f42bd2004-07-04 12:20:55 +000025#include <iostream>
Chris Lattner697954c2002-01-20 22:54:45 +000026#include <memory>
Chris Lattner00950542001-06-06 20:29:01 +000027
Brian Gaeked0fde302003-11-11 22:41:34 +000028using namespace llvm;
29
Chris Lattner6c8103f2003-05-22 20:13:16 +000030static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000031InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-"));
32
Chris Lattner6c8103f2003-05-22 20:13:16 +000033static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000034OutputFilename("o", cl::desc("Override output filename"),
35 cl::value_desc("filename"));
36
37static cl::opt<bool>
38Force("f", cl::desc("Overwrite output files"));
39
40static cl::opt<bool>
41DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden);
Chris Lattner00950542001-06-06 20:29:01 +000042
Reid Spencer227b6d02004-11-14 22:30:54 +000043static cl::opt<bool>
44NoCompress("disable-compression", cl::init(false),
45 cl::desc("Don't ompress the generated bytecode"));
Reid Spencer17f52c52004-11-06 23:17:23 +000046
Chris Lattner04aa29d2003-08-18 20:47:13 +000047static cl::opt<bool>
48DisableVerify("disable-verify", cl::Hidden,
Reid Spencer0d886162004-07-11 23:20:54 +000049 cl::desc("Do not run verifier on input LLVM (dangerous!)"));
Chris Lattner04aa29d2003-08-18 20:47:13 +000050
Chris Lattner00950542001-06-06 20:29:01 +000051int main(int argc, char **argv) {
Chris Lattner8f367bd2001-07-23 02:35:57 +000052 cl::ParseCommandLineOptions(argc, argv, " llvm .ll -> .bc assembler\n");
Reid Spencer9de7b332004-08-29 19:28:55 +000053 sys::PrintStackTraceOnErrorSignal();
Chris Lattner00950542001-06-06 20:29:01 +000054
Reid Spencer1ef8bda2004-12-30 05:36:08 +000055 int exitCode = 0;
Anand Shukla63aaa112002-06-25 21:43:28 +000056 std::ostream *Out = 0;
Chris Lattner00950542001-06-06 20:29:01 +000057 try {
58 // Parse the file now...
Chris Lattner06272dc2002-04-07 22:34:19 +000059 std::auto_ptr<Module> M(ParseAssemblyFile(InputFilename));
60 if (M.get() == 0) {
Chris Lattner6c8103f2003-05-22 20:13:16 +000061 std::cerr << argv[0] << ": assembly didn't read correctly.\n";
Chris Lattner00950542001-06-06 20:29:01 +000062 return 1;
63 }
Chris Lattner1acbea12002-08-30 22:54:41 +000064
Chris Lattnerb3366bc2004-07-13 08:48:04 +000065 try {
66 if (!DisableVerify)
67 verifyModule(*M.get(), ThrowExceptionAction);
68 } catch (const std::string &Err) {
Chris Lattner6c8103f2003-05-22 20:13:16 +000069 std::cerr << argv[0]
70 << ": assembly parsed, but does not verify as correct!\n";
Chris Lattnerb3366bc2004-07-13 08:48:04 +000071 std::cerr << Err;
Chris Lattner1acbea12002-08-30 22:54:41 +000072 return 1;
73 }
Chris Lattner00950542001-06-06 20:29:01 +000074
Chris Lattner6c8103f2003-05-22 20:13:16 +000075 if (DumpAsm) std::cerr << "Here's the assembly:\n" << M.get();
Chris Lattner8f367bd2001-07-23 02:35:57 +000076
Chris Lattner1e78f362001-07-23 19:27:24 +000077 if (OutputFilename != "") { // Specified an output filename?
Chris Lattner3ce0ea82003-05-31 21:47:16 +000078 if (OutputFilename != "-") { // Not stdout?
79 if (!Force && std::ifstream(OutputFilename.c_str())) {
80 // If force is not specified, make sure not to overwrite a file!
81 std::cerr << argv[0] << ": error opening '" << OutputFilename
82 << "': file exists!\n"
83 << "Use -f command line argument to force output\n";
84 return 1;
85 }
Chris Lattnera8e750f2004-06-25 20:54:43 +000086 Out = new std::ofstream(OutputFilename.c_str(), std::ios_base::out |
87 std::ios_base::trunc | std::ios_base::binary);
Chris Lattner3ce0ea82003-05-31 21:47:16 +000088 } else { // Specified stdout
89 Out = &std::cout;
Chris Lattner697954c2002-01-20 22:54:45 +000090 }
Chris Lattner8f367bd2001-07-23 02:35:57 +000091 } else {
Chris Lattner1e78f362001-07-23 19:27:24 +000092 if (InputFilename == "-") {
93 OutputFilename = "-";
Anand Shukla63aaa112002-06-25 21:43:28 +000094 Out = &std::cout;
Chris Lattner8f367bd2001-07-23 02:35:57 +000095 } else {
Chris Lattner697954c2002-01-20 22:54:45 +000096 std::string IFN = InputFilename;
Chris Lattner8f367bd2001-07-23 02:35:57 +000097 int Len = IFN.length();
98 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
99 // Source ends in .ll
Chris Lattner697954c2002-01-20 22:54:45 +0000100 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
Chris Lattner8f367bd2001-07-23 02:35:57 +0000101 } else {
Chris Lattner1e78f362001-07-23 19:27:24 +0000102 OutputFilename = IFN; // Append a .bc to it
Chris Lattner8f367bd2001-07-23 02:35:57 +0000103 }
Chris Lattner1e78f362001-07-23 19:27:24 +0000104 OutputFilename += ".bc";
Chris Lattner697954c2002-01-20 22:54:45 +0000105
Chris Lattner888912d2002-01-22 21:07:24 +0000106 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +0000107 // If force is not specified, make sure not to overwrite a file!
Chris Lattner6c8103f2003-05-22 20:13:16 +0000108 std::cerr << argv[0] << ": error opening '" << OutputFilename
109 << "': file exists!\n"
110 << "Use -f command line argument to force output\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000111 return 1;
112 }
113
Chris Lattnera8e750f2004-06-25 20:54:43 +0000114 Out = new std::ofstream(OutputFilename.c_str(), std::ios_base::out |
115 std::ios_base::trunc | std::ios_base::binary);
Misha Brukman452fea92003-10-10 17:56:49 +0000116 // Make sure that the Out file gets unlinked from the disk if we get a
Chris Lattner76d12292002-04-18 19:55:25 +0000117 // SIGINT
Reid Spencer227b6d02004-11-14 22:30:54 +0000118 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
Chris Lattner00950542001-06-06 20:29:01 +0000119 }
120 }
Chris Lattner697954c2002-01-20 22:54:45 +0000121
122 if (!Out->good()) {
Chris Lattner6c8103f2003-05-22 20:13:16 +0000123 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000124 return 1;
125 }
Chris Lattner00950542001-06-06 20:29:01 +0000126
Reid Spencercf0eb8d2004-11-07 05:37:27 +0000127 WriteBytecodeToFile(M.get(), *Out, !NoCompress);
Chris Lattner00950542001-06-06 20:29:01 +0000128 } catch (const ParseException &E) {
Chris Lattner6c8103f2003-05-22 20:13:16 +0000129 std::cerr << argv[0] << ": " << E.getMessage() << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000130 exitCode = 1;
131 } catch (const std::string& msg) {
132 std::cerr << argv[0] << ": " << msg << "\n";
133 exitCode = 1;
134 } catch (...) {
135 std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
136 exitCode = 1;
Chris Lattner00950542001-06-06 20:29:01 +0000137 }
138
Anand Shukla63aaa112002-06-25 21:43:28 +0000139 if (Out != &std::cout) delete Out;
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000140 return exitCode;
Chris Lattner00950542001-06-06 20:29:01 +0000141}
142