blob: 7a777a4bab4638d15c32f24aeaf3470c7260d718 [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
Anand Shukla63aaa112002-06-25 21:43:28 +000055 std::ostream *Out = 0;
Chris Lattner00950542001-06-06 20:29:01 +000056 try {
57 // Parse the file now...
Chris Lattner06272dc2002-04-07 22:34:19 +000058 std::auto_ptr<Module> M(ParseAssemblyFile(InputFilename));
59 if (M.get() == 0) {
Chris Lattner6c8103f2003-05-22 20:13:16 +000060 std::cerr << argv[0] << ": assembly didn't read correctly.\n";
Chris Lattner00950542001-06-06 20:29:01 +000061 return 1;
62 }
Chris Lattner1acbea12002-08-30 22:54:41 +000063
Chris Lattnerb3366bc2004-07-13 08:48:04 +000064 try {
65 if (!DisableVerify)
66 verifyModule(*M.get(), ThrowExceptionAction);
67 } catch (const std::string &Err) {
Chris Lattner6c8103f2003-05-22 20:13:16 +000068 std::cerr << argv[0]
69 << ": assembly parsed, but does not verify as correct!\n";
Chris Lattnerb3366bc2004-07-13 08:48:04 +000070 std::cerr << Err;
Chris Lattner1acbea12002-08-30 22:54:41 +000071 return 1;
72 }
Chris Lattner00950542001-06-06 20:29:01 +000073
Chris Lattner6c8103f2003-05-22 20:13:16 +000074 if (DumpAsm) std::cerr << "Here's the assembly:\n" << M.get();
Chris Lattner8f367bd2001-07-23 02:35:57 +000075
Chris Lattner1e78f362001-07-23 19:27:24 +000076 if (OutputFilename != "") { // Specified an output filename?
Chris Lattner3ce0ea82003-05-31 21:47:16 +000077 if (OutputFilename != "-") { // Not stdout?
78 if (!Force && std::ifstream(OutputFilename.c_str())) {
79 // If force is not specified, make sure not to overwrite a file!
80 std::cerr << argv[0] << ": error opening '" << OutputFilename
81 << "': file exists!\n"
82 << "Use -f command line argument to force output\n";
83 return 1;
84 }
Chris Lattnera8e750f2004-06-25 20:54:43 +000085 Out = new std::ofstream(OutputFilename.c_str(), std::ios_base::out |
86 std::ios_base::trunc | std::ios_base::binary);
Chris Lattner3ce0ea82003-05-31 21:47:16 +000087 } else { // Specified stdout
88 Out = &std::cout;
Chris Lattner697954c2002-01-20 22:54:45 +000089 }
Chris Lattner8f367bd2001-07-23 02:35:57 +000090 } else {
Chris Lattner1e78f362001-07-23 19:27:24 +000091 if (InputFilename == "-") {
92 OutputFilename = "-";
Anand Shukla63aaa112002-06-25 21:43:28 +000093 Out = &std::cout;
Chris Lattner8f367bd2001-07-23 02:35:57 +000094 } else {
Chris Lattner697954c2002-01-20 22:54:45 +000095 std::string IFN = InputFilename;
Chris Lattner8f367bd2001-07-23 02:35:57 +000096 int Len = IFN.length();
97 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
98 // Source ends in .ll
Chris Lattner697954c2002-01-20 22:54:45 +000099 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
Chris Lattner8f367bd2001-07-23 02:35:57 +0000100 } else {
Chris Lattner1e78f362001-07-23 19:27:24 +0000101 OutputFilename = IFN; // Append a .bc to it
Chris Lattner8f367bd2001-07-23 02:35:57 +0000102 }
Chris Lattner1e78f362001-07-23 19:27:24 +0000103 OutputFilename += ".bc";
Chris Lattner697954c2002-01-20 22:54:45 +0000104
Chris Lattner888912d2002-01-22 21:07:24 +0000105 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +0000106 // If force is not specified, make sure not to overwrite a file!
Chris Lattner6c8103f2003-05-22 20:13:16 +0000107 std::cerr << argv[0] << ": error opening '" << OutputFilename
108 << "': file exists!\n"
109 << "Use -f command line argument to force output\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000110 return 1;
111 }
112
Chris Lattnera8e750f2004-06-25 20:54:43 +0000113 Out = new std::ofstream(OutputFilename.c_str(), std::ios_base::out |
114 std::ios_base::trunc | std::ios_base::binary);
Misha Brukman452fea92003-10-10 17:56:49 +0000115 // Make sure that the Out file gets unlinked from the disk if we get a
Chris Lattner76d12292002-04-18 19:55:25 +0000116 // SIGINT
Reid Spencer227b6d02004-11-14 22:30:54 +0000117 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
Chris Lattner00950542001-06-06 20:29:01 +0000118 }
119 }
Chris Lattner697954c2002-01-20 22:54:45 +0000120
121 if (!Out->good()) {
Chris Lattner6c8103f2003-05-22 20:13:16 +0000122 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000123 return 1;
124 }
Chris Lattner00950542001-06-06 20:29:01 +0000125
Reid Spencercf0eb8d2004-11-07 05:37:27 +0000126 WriteBytecodeToFile(M.get(), *Out, !NoCompress);
Chris Lattner00950542001-06-06 20:29:01 +0000127 } catch (const ParseException &E) {
Chris Lattner6c8103f2003-05-22 20:13:16 +0000128 std::cerr << argv[0] << ": " << E.getMessage() << "\n";
Chris Lattner00950542001-06-06 20:29:01 +0000129 return 1;
130 }
131
Anand Shukla63aaa112002-06-25 21:43:28 +0000132 if (Out != &std::cout) delete Out;
Chris Lattner00950542001-06-06 20:29:01 +0000133 return 0;
134}
135