blob: 0905466780ccba33a31eb30f0780c66a06d5f1d2 [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"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000022#include "Support/CommandLine.h"
Chris Lattner76d12292002-04-18 19:55:25 +000023#include "Support/Signals.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000024#include <fstream>
Chris Lattner697954c2002-01-20 22:54:45 +000025#include <memory>
Chris Lattner00950542001-06-06 20:29:01 +000026
Chris Lattner6c8103f2003-05-22 20:13:16 +000027static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000028InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-"));
29
Chris Lattner6c8103f2003-05-22 20:13:16 +000030static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000031OutputFilename("o", cl::desc("Override output filename"),
32 cl::value_desc("filename"));
33
34static cl::opt<bool>
35Force("f", cl::desc("Overwrite output files"));
36
37static cl::opt<bool>
38DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden);
Chris Lattner00950542001-06-06 20:29:01 +000039
Chris Lattner04aa29d2003-08-18 20:47:13 +000040static cl::opt<bool>
41DisableVerify("disable-verify", cl::Hidden,
42 cl::desc("Do not run verifier on input LLVM (dangerous!"));
43
Chris Lattner00950542001-06-06 20:29:01 +000044int main(int argc, char **argv) {
Chris Lattner8f367bd2001-07-23 02:35:57 +000045 cl::ParseCommandLineOptions(argc, argv, " llvm .ll -> .bc assembler\n");
Chris Lattner00950542001-06-06 20:29:01 +000046
Anand Shukla63aaa112002-06-25 21:43:28 +000047 std::ostream *Out = 0;
Chris Lattner00950542001-06-06 20:29:01 +000048 try {
49 // Parse the file now...
Chris Lattner06272dc2002-04-07 22:34:19 +000050 std::auto_ptr<Module> M(ParseAssemblyFile(InputFilename));
51 if (M.get() == 0) {
Chris Lattner6c8103f2003-05-22 20:13:16 +000052 std::cerr << argv[0] << ": assembly didn't read correctly.\n";
Chris Lattner00950542001-06-06 20:29:01 +000053 return 1;
54 }
Chris Lattner1acbea12002-08-30 22:54:41 +000055
Chris Lattner04aa29d2003-08-18 20:47:13 +000056 if (!DisableVerify && verifyModule(*M.get())) {
Chris Lattner6c8103f2003-05-22 20:13:16 +000057 std::cerr << argv[0]
58 << ": assembly parsed, but does not verify as correct!\n";
Chris Lattner1acbea12002-08-30 22:54:41 +000059 return 1;
60 }
Chris Lattner00950542001-06-06 20:29:01 +000061
Chris Lattner6c8103f2003-05-22 20:13:16 +000062 if (DumpAsm) std::cerr << "Here's the assembly:\n" << M.get();
Chris Lattner8f367bd2001-07-23 02:35:57 +000063
Chris Lattner1e78f362001-07-23 19:27:24 +000064 if (OutputFilename != "") { // Specified an output filename?
Chris Lattner3ce0ea82003-05-31 21:47:16 +000065 if (OutputFilename != "-") { // Not stdout?
66 if (!Force && std::ifstream(OutputFilename.c_str())) {
67 // If force is not specified, make sure not to overwrite a file!
68 std::cerr << argv[0] << ": error opening '" << OutputFilename
69 << "': file exists!\n"
70 << "Use -f command line argument to force output\n";
71 return 1;
72 }
73 Out = new std::ofstream(OutputFilename.c_str());
74 } else { // Specified stdout
75 Out = &std::cout;
Chris Lattner697954c2002-01-20 22:54:45 +000076 }
Chris Lattner8f367bd2001-07-23 02:35:57 +000077 } else {
Chris Lattner1e78f362001-07-23 19:27:24 +000078 if (InputFilename == "-") {
79 OutputFilename = "-";
Anand Shukla63aaa112002-06-25 21:43:28 +000080 Out = &std::cout;
Chris Lattner8f367bd2001-07-23 02:35:57 +000081 } else {
Chris Lattner697954c2002-01-20 22:54:45 +000082 std::string IFN = InputFilename;
Chris Lattner8f367bd2001-07-23 02:35:57 +000083 int Len = IFN.length();
84 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
85 // Source ends in .ll
Chris Lattner697954c2002-01-20 22:54:45 +000086 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
Chris Lattner8f367bd2001-07-23 02:35:57 +000087 } else {
Chris Lattner1e78f362001-07-23 19:27:24 +000088 OutputFilename = IFN; // Append a .bc to it
Chris Lattner8f367bd2001-07-23 02:35:57 +000089 }
Chris Lattner1e78f362001-07-23 19:27:24 +000090 OutputFilename += ".bc";
Chris Lattner697954c2002-01-20 22:54:45 +000091
Chris Lattner888912d2002-01-22 21:07:24 +000092 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +000093 // If force is not specified, make sure not to overwrite a file!
Chris Lattner6c8103f2003-05-22 20:13:16 +000094 std::cerr << argv[0] << ": error opening '" << OutputFilename
95 << "': file exists!\n"
96 << "Use -f command line argument to force output\n";
Chris Lattner697954c2002-01-20 22:54:45 +000097 return 1;
98 }
99
100 Out = new std::ofstream(OutputFilename.c_str());
Misha Brukman452fea92003-10-10 17:56:49 +0000101 // Make sure that the Out file gets unlinked from the disk if we get a
Chris Lattner76d12292002-04-18 19:55:25 +0000102 // SIGINT
103 RemoveFileOnSignal(OutputFilename);
Chris Lattner00950542001-06-06 20:29:01 +0000104 }
105 }
Chris Lattner697954c2002-01-20 22:54:45 +0000106
107 if (!Out->good()) {
Chris Lattner6c8103f2003-05-22 20:13:16 +0000108 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000109 return 1;
110 }
Chris Lattner00950542001-06-06 20:29:01 +0000111
Chris Lattner06272dc2002-04-07 22:34:19 +0000112 WriteBytecodeToFile(M.get(), *Out);
Chris Lattner00950542001-06-06 20:29:01 +0000113 } catch (const ParseException &E) {
Chris Lattner6c8103f2003-05-22 20:13:16 +0000114 std::cerr << argv[0] << ": " << E.getMessage() << "\n";
Chris Lattner00950542001-06-06 20:29:01 +0000115 return 1;
116 }
117
Anand Shukla63aaa112002-06-25 21:43:28 +0000118 if (Out != &std::cout) delete Out;
Chris Lattner00950542001-06-06 20:29:01 +0000119 return 0;
120}
121