blob: 003ae4d4f898516b8635c27bd3b817b97870fc16 [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 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
Chris Lattner04aa29d2003-08-18 20:47:13 +000043static cl::opt<bool>
44DisableVerify("disable-verify", cl::Hidden,
Reid Spencer0d886162004-07-11 23:20:54 +000045 cl::desc("Do not run verifier on input LLVM (dangerous!)"));
Chris Lattner04aa29d2003-08-18 20:47:13 +000046
Chris Lattner00950542001-06-06 20:29:01 +000047int main(int argc, char **argv) {
Chris Lattner8f367bd2001-07-23 02:35:57 +000048 cl::ParseCommandLineOptions(argc, argv, " llvm .ll -> .bc assembler\n");
Chris Lattnerf73b4ca2004-02-19 20:32:12 +000049 PrintStackTraceOnErrorSignal();
Chris Lattner00950542001-06-06 20:29:01 +000050
Anand Shukla63aaa112002-06-25 21:43:28 +000051 std::ostream *Out = 0;
Chris Lattner00950542001-06-06 20:29:01 +000052 try {
53 // Parse the file now...
Chris Lattner06272dc2002-04-07 22:34:19 +000054 std::auto_ptr<Module> M(ParseAssemblyFile(InputFilename));
55 if (M.get() == 0) {
Chris Lattner6c8103f2003-05-22 20:13:16 +000056 std::cerr << argv[0] << ": assembly didn't read correctly.\n";
Chris Lattner00950542001-06-06 20:29:01 +000057 return 1;
58 }
Chris Lattner1acbea12002-08-30 22:54:41 +000059
Chris Lattner04aa29d2003-08-18 20:47:13 +000060 if (!DisableVerify && verifyModule(*M.get())) {
Chris Lattner6c8103f2003-05-22 20:13:16 +000061 std::cerr << argv[0]
62 << ": assembly parsed, but does not verify as correct!\n";
Chris Lattner1acbea12002-08-30 22:54:41 +000063 return 1;
64 }
Chris Lattner00950542001-06-06 20:29:01 +000065
Chris Lattner6c8103f2003-05-22 20:13:16 +000066 if (DumpAsm) std::cerr << "Here's the assembly:\n" << M.get();
Chris Lattner8f367bd2001-07-23 02:35:57 +000067
Chris Lattner1e78f362001-07-23 19:27:24 +000068 if (OutputFilename != "") { // Specified an output filename?
Chris Lattner3ce0ea82003-05-31 21:47:16 +000069 if (OutputFilename != "-") { // Not stdout?
70 if (!Force && std::ifstream(OutputFilename.c_str())) {
71 // If force is not specified, make sure not to overwrite a file!
72 std::cerr << argv[0] << ": error opening '" << OutputFilename
73 << "': file exists!\n"
74 << "Use -f command line argument to force output\n";
75 return 1;
76 }
Chris Lattnera8e750f2004-06-25 20:54:43 +000077 Out = new std::ofstream(OutputFilename.c_str(), std::ios_base::out |
78 std::ios_base::trunc | std::ios_base::binary);
Chris Lattner3ce0ea82003-05-31 21:47:16 +000079 } else { // Specified stdout
80 Out = &std::cout;
Chris Lattner697954c2002-01-20 22:54:45 +000081 }
Chris Lattner8f367bd2001-07-23 02:35:57 +000082 } else {
Chris Lattner1e78f362001-07-23 19:27:24 +000083 if (InputFilename == "-") {
84 OutputFilename = "-";
Anand Shukla63aaa112002-06-25 21:43:28 +000085 Out = &std::cout;
Chris Lattner8f367bd2001-07-23 02:35:57 +000086 } else {
Chris Lattner697954c2002-01-20 22:54:45 +000087 std::string IFN = InputFilename;
Chris Lattner8f367bd2001-07-23 02:35:57 +000088 int Len = IFN.length();
89 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
90 // Source ends in .ll
Chris Lattner697954c2002-01-20 22:54:45 +000091 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
Chris Lattner8f367bd2001-07-23 02:35:57 +000092 } else {
Chris Lattner1e78f362001-07-23 19:27:24 +000093 OutputFilename = IFN; // Append a .bc to it
Chris Lattner8f367bd2001-07-23 02:35:57 +000094 }
Chris Lattner1e78f362001-07-23 19:27:24 +000095 OutputFilename += ".bc";
Chris Lattner697954c2002-01-20 22:54:45 +000096
Chris Lattner888912d2002-01-22 21:07:24 +000097 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +000098 // If force is not specified, make sure not to overwrite a file!
Chris Lattner6c8103f2003-05-22 20:13:16 +000099 std::cerr << argv[0] << ": error opening '" << OutputFilename
100 << "': file exists!\n"
101 << "Use -f command line argument to force output\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000102 return 1;
103 }
104
Chris Lattnera8e750f2004-06-25 20:54:43 +0000105 Out = new std::ofstream(OutputFilename.c_str(), std::ios_base::out |
106 std::ios_base::trunc | std::ios_base::binary);
Misha Brukman452fea92003-10-10 17:56:49 +0000107 // Make sure that the Out file gets unlinked from the disk if we get a
Chris Lattner76d12292002-04-18 19:55:25 +0000108 // SIGINT
109 RemoveFileOnSignal(OutputFilename);
Chris Lattner00950542001-06-06 20:29:01 +0000110 }
111 }
Chris Lattner697954c2002-01-20 22:54:45 +0000112
113 if (!Out->good()) {
Chris Lattner6c8103f2003-05-22 20:13:16 +0000114 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000115 return 1;
116 }
Chris Lattner00950542001-06-06 20:29:01 +0000117
Chris Lattner06272dc2002-04-07 22:34:19 +0000118 WriteBytecodeToFile(M.get(), *Out);
Chris Lattner00950542001-06-06 20:29:01 +0000119 } catch (const ParseException &E) {
Chris Lattner6c8103f2003-05-22 20:13:16 +0000120 std::cerr << argv[0] << ": " << E.getMessage() << "\n";
Chris Lattner00950542001-06-06 20:29:01 +0000121 return 1;
122 }
123
Anand Shukla63aaa112002-06-25 21:43:28 +0000124 if (Out != &std::cout) delete Out;
Chris Lattner00950542001-06-06 20:29:01 +0000125 return 0;
126}
127