blob: 4b66b78efb72dd7db887f896988b652449e04130 [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
Brian Gaeked0fde302003-11-11 22:41:34 +000027using namespace llvm;
28
Chris Lattner6c8103f2003-05-22 20:13:16 +000029static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000030InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-"));
31
Chris Lattner6c8103f2003-05-22 20:13:16 +000032static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000033OutputFilename("o", cl::desc("Override output filename"),
34 cl::value_desc("filename"));
35
36static cl::opt<bool>
37Force("f", cl::desc("Overwrite output files"));
38
39static cl::opt<bool>
40DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden);
Chris Lattner00950542001-06-06 20:29:01 +000041
Chris Lattner04aa29d2003-08-18 20:47:13 +000042static cl::opt<bool>
43DisableVerify("disable-verify", cl::Hidden,
44 cl::desc("Do not run verifier on input LLVM (dangerous!"));
45
Chris Lattner00950542001-06-06 20:29:01 +000046int main(int argc, char **argv) {
Chris Lattner8f367bd2001-07-23 02:35:57 +000047 cl::ParseCommandLineOptions(argc, argv, " llvm .ll -> .bc assembler\n");
Chris Lattnerf73b4ca2004-02-19 20:32:12 +000048 PrintStackTraceOnErrorSignal();
Chris Lattner00950542001-06-06 20:29:01 +000049
Anand Shukla63aaa112002-06-25 21:43:28 +000050 std::ostream *Out = 0;
Chris Lattner00950542001-06-06 20:29:01 +000051 try {
52 // Parse the file now...
Chris Lattner06272dc2002-04-07 22:34:19 +000053 std::auto_ptr<Module> M(ParseAssemblyFile(InputFilename));
54 if (M.get() == 0) {
Chris Lattner6c8103f2003-05-22 20:13:16 +000055 std::cerr << argv[0] << ": assembly didn't read correctly.\n";
Chris Lattner00950542001-06-06 20:29:01 +000056 return 1;
57 }
Chris Lattner1acbea12002-08-30 22:54:41 +000058
Chris Lattner04aa29d2003-08-18 20:47:13 +000059 if (!DisableVerify && verifyModule(*M.get())) {
Chris Lattner6c8103f2003-05-22 20:13:16 +000060 std::cerr << argv[0]
61 << ": assembly parsed, but does not verify as correct!\n";
Chris Lattner1acbea12002-08-30 22:54:41 +000062 return 1;
63 }
Chris Lattner00950542001-06-06 20:29:01 +000064
Chris Lattner6c8103f2003-05-22 20:13:16 +000065 if (DumpAsm) std::cerr << "Here's the assembly:\n" << M.get();
Chris Lattner8f367bd2001-07-23 02:35:57 +000066
Chris Lattner1e78f362001-07-23 19:27:24 +000067 if (OutputFilename != "") { // Specified an output filename?
Chris Lattner3ce0ea82003-05-31 21:47:16 +000068 if (OutputFilename != "-") { // Not stdout?
69 if (!Force && std::ifstream(OutputFilename.c_str())) {
70 // If force is not specified, make sure not to overwrite a file!
71 std::cerr << argv[0] << ": error opening '" << OutputFilename
72 << "': file exists!\n"
73 << "Use -f command line argument to force output\n";
74 return 1;
75 }
76 Out = new std::ofstream(OutputFilename.c_str());
77 } else { // Specified stdout
78 Out = &std::cout;
Chris Lattner697954c2002-01-20 22:54:45 +000079 }
Chris Lattner8f367bd2001-07-23 02:35:57 +000080 } else {
Chris Lattner1e78f362001-07-23 19:27:24 +000081 if (InputFilename == "-") {
82 OutputFilename = "-";
Anand Shukla63aaa112002-06-25 21:43:28 +000083 Out = &std::cout;
Chris Lattner8f367bd2001-07-23 02:35:57 +000084 } else {
Chris Lattner697954c2002-01-20 22:54:45 +000085 std::string IFN = InputFilename;
Chris Lattner8f367bd2001-07-23 02:35:57 +000086 int Len = IFN.length();
87 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
88 // Source ends in .ll
Chris Lattner697954c2002-01-20 22:54:45 +000089 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
Chris Lattner8f367bd2001-07-23 02:35:57 +000090 } else {
Chris Lattner1e78f362001-07-23 19:27:24 +000091 OutputFilename = IFN; // Append a .bc to it
Chris Lattner8f367bd2001-07-23 02:35:57 +000092 }
Chris Lattner1e78f362001-07-23 19:27:24 +000093 OutputFilename += ".bc";
Chris Lattner697954c2002-01-20 22:54:45 +000094
Chris Lattner888912d2002-01-22 21:07:24 +000095 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +000096 // If force is not specified, make sure not to overwrite a file!
Chris Lattner6c8103f2003-05-22 20:13:16 +000097 std::cerr << argv[0] << ": error opening '" << OutputFilename
98 << "': file exists!\n"
99 << "Use -f command line argument to force output\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000100 return 1;
101 }
102
103 Out = new std::ofstream(OutputFilename.c_str());
Misha Brukman452fea92003-10-10 17:56:49 +0000104 // Make sure that the Out file gets unlinked from the disk if we get a
Chris Lattner76d12292002-04-18 19:55:25 +0000105 // SIGINT
106 RemoveFileOnSignal(OutputFilename);
Chris Lattner00950542001-06-06 20:29:01 +0000107 }
108 }
Chris Lattner697954c2002-01-20 22:54:45 +0000109
110 if (!Out->good()) {
Chris Lattner6c8103f2003-05-22 20:13:16 +0000111 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000112 return 1;
113 }
Chris Lattner00950542001-06-06 20:29:01 +0000114
Chris Lattner06272dc2002-04-07 22:34:19 +0000115 WriteBytecodeToFile(M.get(), *Out);
Chris Lattner00950542001-06-06 20:29:01 +0000116 } catch (const ParseException &E) {
Chris Lattner6c8103f2003-05-22 20:13:16 +0000117 std::cerr << argv[0] << ": " << E.getMessage() << "\n";
Chris Lattner00950542001-06-06 20:29:01 +0000118 return 1;
119 }
120
Anand Shukla63aaa112002-06-25 21:43:28 +0000121 if (Out != &std::cout) delete Out;
Chris Lattner00950542001-06-06 20:29:01 +0000122 return 0;
123}
124