blob: e00d4e28becc2d19c410474ecd38aba472176242 [file] [log] [blame]
Chris Lattner00950542001-06-06 20:29:01 +00001//===------------------------------------------------------------------------===
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//===----------------------------------------------------------------------===//
9//
Chris Lattner00950542001-06-06 20:29:01 +000010// LLVM 'AS' UTILITY
11//
12// This utility may be invoked in the following manner:
Misha Brukmancbb62dd2003-08-28 21:32:57 +000013// llvm-as --help - Output information about command line switches
14// llvm-as [options] - Read LLVM asm from stdin, write bytecode to stdout
15// llvm-as [options] x.ll - Read LLVM asm from the x.ll file, write bytecode
16// to the x.bc file.
Anand Shukla63aaa112002-06-25 21:43:28 +000017//
Chris Lattner00950542001-06-06 20:29:01 +000018//===------------------------------------------------------------------------===
19
Chris Lattner00950542001-06-06 20:29:01 +000020#include "llvm/Module.h"
21#include "llvm/Assembly/Parser.h"
Chris Lattner00950542001-06-06 20:29:01 +000022#include "llvm/Bytecode/Writer.h"
Chris Lattner1acbea12002-08-30 22:54:41 +000023#include "llvm/Analysis/Verifier.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000024#include "Support/CommandLine.h"
Chris Lattner76d12292002-04-18 19:55:25 +000025#include "Support/Signals.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000026#include <fstream>
Chris Lattner697954c2002-01-20 22:54:45 +000027#include <memory>
Chris Lattner00950542001-06-06 20:29:01 +000028
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 Lattner00950542001-06-06 20:29:01 +000048
Anand Shukla63aaa112002-06-25 21:43:28 +000049 std::ostream *Out = 0;
Chris Lattner00950542001-06-06 20:29:01 +000050 try {
51 // Parse the file now...
Chris Lattner06272dc2002-04-07 22:34:19 +000052 std::auto_ptr<Module> M(ParseAssemblyFile(InputFilename));
53 if (M.get() == 0) {
Chris Lattner6c8103f2003-05-22 20:13:16 +000054 std::cerr << argv[0] << ": assembly didn't read correctly.\n";
Chris Lattner00950542001-06-06 20:29:01 +000055 return 1;
56 }
Chris Lattner1acbea12002-08-30 22:54:41 +000057
Chris Lattner04aa29d2003-08-18 20:47:13 +000058 if (!DisableVerify && verifyModule(*M.get())) {
Chris Lattner6c8103f2003-05-22 20:13:16 +000059 std::cerr << argv[0]
60 << ": assembly parsed, but does not verify as correct!\n";
Chris Lattner1acbea12002-08-30 22:54:41 +000061 return 1;
62 }
Chris Lattner00950542001-06-06 20:29:01 +000063
Chris Lattner6c8103f2003-05-22 20:13:16 +000064 if (DumpAsm) std::cerr << "Here's the assembly:\n" << M.get();
Chris Lattner8f367bd2001-07-23 02:35:57 +000065
Chris Lattner1e78f362001-07-23 19:27:24 +000066 if (OutputFilename != "") { // Specified an output filename?
Chris Lattner3ce0ea82003-05-31 21:47:16 +000067 if (OutputFilename != "-") { // Not stdout?
68 if (!Force && std::ifstream(OutputFilename.c_str())) {
69 // If force is not specified, make sure not to overwrite a file!
70 std::cerr << argv[0] << ": error opening '" << OutputFilename
71 << "': file exists!\n"
72 << "Use -f command line argument to force output\n";
73 return 1;
74 }
75 Out = new std::ofstream(OutputFilename.c_str());
76 } else { // Specified stdout
77 Out = &std::cout;
Chris Lattner697954c2002-01-20 22:54:45 +000078 }
Chris Lattner8f367bd2001-07-23 02:35:57 +000079 } else {
Chris Lattner1e78f362001-07-23 19:27:24 +000080 if (InputFilename == "-") {
81 OutputFilename = "-";
Anand Shukla63aaa112002-06-25 21:43:28 +000082 Out = &std::cout;
Chris Lattner8f367bd2001-07-23 02:35:57 +000083 } else {
Chris Lattner697954c2002-01-20 22:54:45 +000084 std::string IFN = InputFilename;
Chris Lattner8f367bd2001-07-23 02:35:57 +000085 int Len = IFN.length();
86 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
87 // Source ends in .ll
Chris Lattner697954c2002-01-20 22:54:45 +000088 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
Chris Lattner8f367bd2001-07-23 02:35:57 +000089 } else {
Chris Lattner1e78f362001-07-23 19:27:24 +000090 OutputFilename = IFN; // Append a .bc to it
Chris Lattner8f367bd2001-07-23 02:35:57 +000091 }
Chris Lattner1e78f362001-07-23 19:27:24 +000092 OutputFilename += ".bc";
Chris Lattner697954c2002-01-20 22:54:45 +000093
Chris Lattner888912d2002-01-22 21:07:24 +000094 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +000095 // If force is not specified, make sure not to overwrite a file!
Chris Lattner6c8103f2003-05-22 20:13:16 +000096 std::cerr << argv[0] << ": error opening '" << OutputFilename
97 << "': file exists!\n"
98 << "Use -f command line argument to force output\n";
Chris Lattner697954c2002-01-20 22:54:45 +000099 return 1;
100 }
101
102 Out = new std::ofstream(OutputFilename.c_str());
Misha Brukman452fea92003-10-10 17:56:49 +0000103 // Make sure that the Out file gets unlinked from the disk if we get a
Chris Lattner76d12292002-04-18 19:55:25 +0000104 // SIGINT
105 RemoveFileOnSignal(OutputFilename);
Chris Lattner00950542001-06-06 20:29:01 +0000106 }
107 }
Chris Lattner697954c2002-01-20 22:54:45 +0000108
109 if (!Out->good()) {
Chris Lattner6c8103f2003-05-22 20:13:16 +0000110 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000111 return 1;
112 }
Chris Lattner00950542001-06-06 20:29:01 +0000113
Chris Lattner06272dc2002-04-07 22:34:19 +0000114 WriteBytecodeToFile(M.get(), *Out);
Chris Lattner00950542001-06-06 20:29:01 +0000115 } catch (const ParseException &E) {
Chris Lattner6c8103f2003-05-22 20:13:16 +0000116 std::cerr << argv[0] << ": " << E.getMessage() << "\n";
Chris Lattner00950542001-06-06 20:29:01 +0000117 return 1;
118 }
119
Anand Shukla63aaa112002-06-25 21:43:28 +0000120 if (Out != &std::cout) delete Out;
Chris Lattner00950542001-06-06 20:29:01 +0000121 return 0;
122}
123