blob: ef1602b6a72c7ee7e84ad67fe9c736838da101bf [file] [log] [blame]
Chris Lattner22feb172003-10-20 17:52:11 +00001//===--- llvm-as.cpp - The low-level LLVM assembler -----------------------===//
John Criswell09344dc2003-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 Lattner2f7c9632001-06-06 20:29:01 +00009//
10// This utility may be invoked in the following manner:
Misha Brukman98930272003-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 Shukla68c99772002-06-25 21:43:28 +000015//
Chris Lattner2f7c9632001-06-06 20:29:01 +000016//===------------------------------------------------------------------------===
17
Chris Lattner2f7c9632001-06-06 20:29:01 +000018#include "llvm/Module.h"
19#include "llvm/Assembly/Parser.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000020#include "llvm/Bytecode/Writer.h"
Chris Lattner4a84ad572002-08-30 22:54:41 +000021#include "llvm/Analysis/Verifier.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000022#include "llvm/Support/CommandLine.h"
Reid Spencer742af2f2005-01-02 00:08:46 +000023#include "llvm/Support/SystemUtils.h"
Chris Lattner278f5152004-05-27 05:41:36 +000024#include "llvm/System/Signals.h"
Chris Lattner5de22042001-11-27 00:03:19 +000025#include <fstream>
Reid Spencerf0ebb252004-07-04 12:20:55 +000026#include <iostream>
Chris Lattner7f74a562002-01-20 22:54:45 +000027#include <memory>
Chris Lattner2f7c9632001-06-06 20:29:01 +000028
Brian Gaeke960707c2003-11-11 22:41:34 +000029using namespace llvm;
30
Chris Lattner02a16832003-05-22 20:13:16 +000031static cl::opt<std::string>
Chris Lattnerf5cad152002-07-22 02:10:13 +000032InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-"));
33
Chris Lattner02a16832003-05-22 20:13:16 +000034static cl::opt<std::string>
Chris Lattnerf5cad152002-07-22 02:10:13 +000035OutputFilename("o", cl::desc("Override output filename"),
36 cl::value_desc("filename"));
37
38static cl::opt<bool>
39Force("f", cl::desc("Overwrite output files"));
40
41static cl::opt<bool>
42DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden);
Chris Lattner2f7c9632001-06-06 20:29:01 +000043
Reid Spencerb2d0fa02004-11-14 22:30:54 +000044static cl::opt<bool>
45NoCompress("disable-compression", cl::init(false),
Jeff Cohenca7d19e22005-01-01 22:10:32 +000046 cl::desc("Don't compress the generated bytecode"));
Reid Spencer2e492042004-11-06 23:17:23 +000047
Chris Lattner016ccdf2003-08-18 20:47:13 +000048static cl::opt<bool>
49DisableVerify("disable-verify", cl::Hidden,
Reid Spencera9e83602004-07-11 23:20:54 +000050 cl::desc("Do not run verifier on input LLVM (dangerous!)"));
Chris Lattner016ccdf2003-08-18 20:47:13 +000051
Chris Lattner2f7c9632001-06-06 20:29:01 +000052int main(int argc, char **argv) {
Chris Lattner0af24642001-07-23 02:35:57 +000053 cl::ParseCommandLineOptions(argc, argv, " llvm .ll -> .bc assembler\n");
Reid Spencere3263ec2004-08-29 19:28:55 +000054 sys::PrintStackTraceOnErrorSignal();
Chris Lattner2f7c9632001-06-06 20:29:01 +000055
Reid Spencer996ec722004-12-30 05:36:08 +000056 int exitCode = 0;
Anand Shukla68c99772002-06-25 21:43:28 +000057 std::ostream *Out = 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +000058 try {
59 // Parse the file now...
Chris Lattner3c707192002-04-07 22:34:19 +000060 std::auto_ptr<Module> M(ParseAssemblyFile(InputFilename));
61 if (M.get() == 0) {
Chris Lattner02a16832003-05-22 20:13:16 +000062 std::cerr << argv[0] << ": assembly didn't read correctly.\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +000063 return 1;
64 }
Chris Lattner4a84ad572002-08-30 22:54:41 +000065
Chris Lattnerab1aeda2004-07-13 08:48:04 +000066 try {
67 if (!DisableVerify)
68 verifyModule(*M.get(), ThrowExceptionAction);
69 } catch (const std::string &Err) {
Chris Lattner02a16832003-05-22 20:13:16 +000070 std::cerr << argv[0]
71 << ": assembly parsed, but does not verify as correct!\n";
Chris Lattnerab1aeda2004-07-13 08:48:04 +000072 std::cerr << Err;
Chris Lattner4a84ad572002-08-30 22:54:41 +000073 return 1;
74 }
Chris Lattner2f7c9632001-06-06 20:29:01 +000075
Chris Lattner02a16832003-05-22 20:13:16 +000076 if (DumpAsm) std::cerr << "Here's the assembly:\n" << M.get();
Chris Lattner0af24642001-07-23 02:35:57 +000077
Chris Lattnerab0cc402001-07-23 19:27:24 +000078 if (OutputFilename != "") { // Specified an output filename?
Chris Lattner306f8b42003-05-31 21:47:16 +000079 if (OutputFilename != "-") { // Not stdout?
80 if (!Force && std::ifstream(OutputFilename.c_str())) {
81 // If force is not specified, make sure not to overwrite a file!
82 std::cerr << argv[0] << ": error opening '" << OutputFilename
83 << "': file exists!\n"
84 << "Use -f command line argument to force output\n";
85 return 1;
86 }
Chris Lattner10247b12004-06-25 20:54:43 +000087 Out = new std::ofstream(OutputFilename.c_str(), std::ios_base::out |
88 std::ios_base::trunc | std::ios_base::binary);
Chris Lattner306f8b42003-05-31 21:47:16 +000089 } else { // Specified stdout
90 Out = &std::cout;
Chris Lattner7f74a562002-01-20 22:54:45 +000091 }
Chris Lattner0af24642001-07-23 02:35:57 +000092 } else {
Chris Lattnerab0cc402001-07-23 19:27:24 +000093 if (InputFilename == "-") {
94 OutputFilename = "-";
Anand Shukla68c99772002-06-25 21:43:28 +000095 Out = &std::cout;
Chris Lattner0af24642001-07-23 02:35:57 +000096 } else {
Chris Lattner7f74a562002-01-20 22:54:45 +000097 std::string IFN = InputFilename;
Chris Lattner0af24642001-07-23 02:35:57 +000098 int Len = IFN.length();
99 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
100 // Source ends in .ll
Chris Lattner7f74a562002-01-20 22:54:45 +0000101 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
Chris Lattner0af24642001-07-23 02:35:57 +0000102 } else {
Chris Lattnerab0cc402001-07-23 19:27:24 +0000103 OutputFilename = IFN; // Append a .bc to it
Chris Lattner0af24642001-07-23 02:35:57 +0000104 }
Chris Lattnerab0cc402001-07-23 19:27:24 +0000105 OutputFilename += ".bc";
Chris Lattner7f74a562002-01-20 22:54:45 +0000106
Chris Lattner0e11e542002-01-22 21:07:24 +0000107 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner7f74a562002-01-20 22:54:45 +0000108 // If force is not specified, make sure not to overwrite a file!
Chris Lattner02a16832003-05-22 20:13:16 +0000109 std::cerr << argv[0] << ": error opening '" << OutputFilename
110 << "': file exists!\n"
111 << "Use -f command line argument to force output\n";
Chris Lattner7f74a562002-01-20 22:54:45 +0000112 return 1;
113 }
114
Chris Lattner10247b12004-06-25 20:54:43 +0000115 Out = new std::ofstream(OutputFilename.c_str(), std::ios_base::out |
116 std::ios_base::trunc | std::ios_base::binary);
Misha Brukmand6769742003-10-10 17:56:49 +0000117 // Make sure that the Out file gets unlinked from the disk if we get a
Chris Lattnerc065ad82002-04-18 19:55:25 +0000118 // SIGINT
Reid Spencerb2d0fa02004-11-14 22:30:54 +0000119 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000120 }
121 }
Chris Lattner7f74a562002-01-20 22:54:45 +0000122
123 if (!Out->good()) {
Chris Lattner02a16832003-05-22 20:13:16 +0000124 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
Chris Lattner7f74a562002-01-20 22:54:45 +0000125 return 1;
126 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000127
Reid Spencer742af2f2005-01-02 00:08:46 +0000128 if (Force || !CheckBytecodeOutputToConsole(Out,true)) {
129 WriteBytecodeToFile(M.get(), *Out, !NoCompress);
130 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000131 } catch (const ParseException &E) {
Chris Lattner02a16832003-05-22 20:13:16 +0000132 std::cerr << argv[0] << ": " << E.getMessage() << "\n";
Reid Spencer996ec722004-12-30 05:36:08 +0000133 exitCode = 1;
134 } catch (const std::string& msg) {
135 std::cerr << argv[0] << ": " << msg << "\n";
136 exitCode = 1;
137 } catch (...) {
138 std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
139 exitCode = 1;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000140 }
141
Anand Shukla68c99772002-06-25 21:43:28 +0000142 if (Out != &std::cout) delete Out;
Reid Spencer996ec722004-12-30 05:36:08 +0000143 return exitCode;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000144}
145