blob: a7463b532ebe222ff42a15c16e2a683a83768d2a [file] [log] [blame]
Chris Lattner10cf8fa2003-10-20 17:52:11 +00001//===--- llvm-as.cpp - The low-level LLVM assembler -----------------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell7c0e0222003-10-20 17:47:21 +00003// 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.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
John Criswell7c0e0222003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
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.
Misha Brukman3da94ae2005-04-22 00:00:37 +000015//
Chris Lattner6d1727c2006-05-29 18:52:52 +000016//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +000017
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"
Chris Lattner27bb3af2007-04-22 06:28:58 +000022#include "llvm/Bitcode/ReaderWriter.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000023#include "llvm/Support/CommandLine.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000024#include "llvm/Support/ManagedStatic.h"
Bill Wendling68fe61d2006-11-29 00:19:40 +000025#include "llvm/Support/Streams.h"
Reid Spencerb687c0c2005-01-02 00:08:46 +000026#include "llvm/Support/SystemUtils.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000027#include "llvm/System/Signals.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000028#include <fstream>
Reid Spencer86f42bd2004-07-04 12:20:55 +000029#include <iostream>
Chris Lattner697954c2002-01-20 22:54:45 +000030#include <memory>
Brian Gaeked0fde302003-11-11 22:41:34 +000031using namespace llvm;
32
Misha Brukman3da94ae2005-04-22 00:00:37 +000033static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000034InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-"));
35
Chris Lattner6c8103f2003-05-22 20:13:16 +000036static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000037OutputFilename("o", cl::desc("Override output filename"),
38 cl::value_desc("filename"));
39
40static cl::opt<bool>
41Force("f", cl::desc("Overwrite output files"));
42
43static cl::opt<bool>
44DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden);
Chris Lattner00950542001-06-06 20:29:01 +000045
Misha Brukman3da94ae2005-04-22 00:00:37 +000046static cl::opt<bool>
Chris Lattner17be6792007-01-21 06:34:18 +000047NoCompress("disable-compression", cl::init(true),
Jeff Cohen7109ce82005-01-01 22:10:32 +000048 cl::desc("Don't compress the generated bytecode"));
Reid Spencer17f52c52004-11-06 23:17:23 +000049
Chris Lattner04aa29d2003-08-18 20:47:13 +000050static cl::opt<bool>
51DisableVerify("disable-verify", cl::Hidden,
Reid Spencer0d886162004-07-11 23:20:54 +000052 cl::desc("Do not run verifier on input LLVM (dangerous!)"));
Chris Lattner04aa29d2003-08-18 20:47:13 +000053
Chris Lattner27bb3af2007-04-22 06:28:58 +000054static cl::opt<bool>
55EnableBitcode("bitcode", cl::desc("Emit bitcode"));
56
57
Chris Lattner00950542001-06-06 20:29:01 +000058int main(int argc, char **argv) {
Chris Lattnerc30598b2006-12-06 01:18:01 +000059 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
Chris Lattner8f367bd2001-07-23 02:35:57 +000060 cl::ParseCommandLineOptions(argc, argv, " llvm .ll -> .bc assembler\n");
Reid Spencer9de7b332004-08-29 19:28:55 +000061 sys::PrintStackTraceOnErrorSignal();
Chris Lattner00950542001-06-06 20:29:01 +000062
Reid Spencer1ef8bda2004-12-30 05:36:08 +000063 int exitCode = 0;
Anand Shukla63aaa112002-06-25 21:43:28 +000064 std::ostream *Out = 0;
Chris Lattner00950542001-06-06 20:29:01 +000065 try {
66 // Parse the file now...
Reid Spencer61c83e02006-08-18 08:43:06 +000067 ParseError Err;
68 std::auto_ptr<Module> M(ParseAssemblyFile(InputFilename,&Err));
Chris Lattner06272dc2002-04-07 22:34:19 +000069 if (M.get() == 0) {
Bill Wendlinge8156192006-12-07 01:30:32 +000070 cerr << argv[0] << ": " << Err.getMessage() << "\n";
Chris Lattner00950542001-06-06 20:29:01 +000071 return 1;
72 }
Chris Lattner1acbea12002-08-30 22:54:41 +000073
Chris Lattner05ac92c2006-07-06 18:02:27 +000074 if (!DisableVerify) {
75 std::string Err;
76 if (verifyModule(*M.get(), ReturnStatusAction, &Err)) {
Bill Wendlinge8156192006-12-07 01:30:32 +000077 cerr << argv[0]
78 << ": assembly parsed, but does not verify as correct!\n";
79 cerr << Err;
Chris Lattner05ac92c2006-07-06 18:02:27 +000080 return 1;
81 }
Chris Lattner1acbea12002-08-30 22:54:41 +000082 }
Misha Brukman3da94ae2005-04-22 00:00:37 +000083
Bill Wendlinge8156192006-12-07 01:30:32 +000084 if (DumpAsm) cerr << "Here's the assembly:\n" << *M.get();
Chris Lattner8f367bd2001-07-23 02:35:57 +000085
Chris Lattner1e78f362001-07-23 19:27:24 +000086 if (OutputFilename != "") { // Specified an output filename?
Chris Lattner3ce0ea82003-05-31 21:47:16 +000087 if (OutputFilename != "-") { // Not stdout?
88 if (!Force && std::ifstream(OutputFilename.c_str())) {
89 // If force is not specified, make sure not to overwrite a file!
Bill Wendlinge8156192006-12-07 01:30:32 +000090 cerr << argv[0] << ": error opening '" << OutputFilename
91 << "': file exists!\n"
92 << "Use -f command line argument to force output\n";
Chris Lattner3ce0ea82003-05-31 21:47:16 +000093 return 1;
94 }
Misha Brukman3da94ae2005-04-22 00:00:37 +000095 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
Jeff Cohen5fb6ed42005-01-22 17:36:17 +000096 std::ios::trunc | std::ios::binary);
Chris Lattner3ce0ea82003-05-31 21:47:16 +000097 } else { // Specified stdout
Jeff Cohen5fb6ed42005-01-22 17:36:17 +000098 // FIXME: cout is not binary!
99 Out = &std::cout;
Chris Lattner697954c2002-01-20 22:54:45 +0000100 }
Chris Lattner8f367bd2001-07-23 02:35:57 +0000101 } else {
Chris Lattner1e78f362001-07-23 19:27:24 +0000102 if (InputFilename == "-") {
Jeff Cohen5fb6ed42005-01-22 17:36:17 +0000103 OutputFilename = "-";
104 Out = &std::cout;
Chris Lattner8f367bd2001-07-23 02:35:57 +0000105 } else {
Jeff Cohen5fb6ed42005-01-22 17:36:17 +0000106 std::string IFN = InputFilename;
107 int Len = IFN.length();
108 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
109 // Source ends in .ll
110 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
Chris Lattner8f367bd2001-07-23 02:35:57 +0000111 } else {
Jeff Cohen5fb6ed42005-01-22 17:36:17 +0000112 OutputFilename = IFN; // Append a .bc to it
113 }
114 OutputFilename += ".bc";
Chris Lattner697954c2002-01-20 22:54:45 +0000115
Chris Lattner888912d2002-01-22 21:07:24 +0000116 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +0000117 // If force is not specified, make sure not to overwrite a file!
Bill Wendlinge8156192006-12-07 01:30:32 +0000118 cerr << argv[0] << ": error opening '" << OutputFilename
119 << "': file exists!\n"
120 << "Use -f command line argument to force output\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000121 return 1;
122 }
123
Misha Brukman3da94ae2005-04-22 00:00:37 +0000124 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
Jeff Cohen5fb6ed42005-01-22 17:36:17 +0000125 std::ios::trunc | std::ios::binary);
Misha Brukman452fea92003-10-10 17:56:49 +0000126 // Make sure that the Out file gets unlinked from the disk if we get a
Chris Lattner76d12292002-04-18 19:55:25 +0000127 // SIGINT
Reid Spencer227b6d02004-11-14 22:30:54 +0000128 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
Chris Lattner00950542001-06-06 20:29:01 +0000129 }
130 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000131
Chris Lattner697954c2002-01-20 22:54:45 +0000132 if (!Out->good()) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000133 cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000134 return 1;
135 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000136
Reid Spencerb687c0c2005-01-02 00:08:46 +0000137 if (Force || !CheckBytecodeOutputToConsole(Out,true)) {
Chris Lattner27bb3af2007-04-22 06:28:58 +0000138 if (EnableBitcode) {
139 WriteBitcodeToFile(M.get(), *Out);
140 } else {
141 OStream L(*Out);
142 WriteBytecodeToFile(M.get(), L, !NoCompress);
143 }
Reid Spencerb687c0c2005-01-02 00:08:46 +0000144 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000145 } catch (const std::string& msg) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000146 cerr << argv[0] << ": " << msg << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000147 exitCode = 1;
148 } catch (...) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000149 cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000150 exitCode = 1;
Chris Lattner00950542001-06-06 20:29:01 +0000151 }
152
Anand Shukla63aaa112002-06-25 21:43:28 +0000153 if (Out != &std::cout) delete Out;
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000154 return exitCode;
Chris Lattner00950542001-06-06 20:29:01 +0000155}
156