blob: 4ca59b99803b49e1d17099942d247bcab2b25f56 [file] [log] [blame]
Chris Lattner00950542001-06-06 20:29:01 +00001//===------------------------------------------------------------------------===
2// LLVM 'AS' UTILITY
3//
4// This utility may be invoked in the following manner:
5// as --help - Output information about command line switches
6// as [options] - Read LLVM assembly from stdin, write bytecode to stdout
7// as [options] x.ll - Read LLVM assembly from the x.ll file, write bytecode
8// to the x.bc file.
Anand Shukla63aaa112002-06-25 21:43:28 +00009//
Chris Lattner00950542001-06-06 20:29:01 +000010//===------------------------------------------------------------------------===
11
Chris Lattner00950542001-06-06 20:29:01 +000012#include "llvm/Module.h"
13#include "llvm/Assembly/Parser.h"
Chris Lattner00950542001-06-06 20:29:01 +000014#include "llvm/Bytecode/Writer.h"
Chris Lattner1acbea12002-08-30 22:54:41 +000015#include "llvm/Analysis/Verifier.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000016#include "Support/CommandLine.h"
Chris Lattner76d12292002-04-18 19:55:25 +000017#include "Support/Signals.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000018#include <fstream>
Chris Lattner697954c2002-01-20 22:54:45 +000019#include <memory>
Chris Lattner00950542001-06-06 20:29:01 +000020
Chris Lattner6c8103f2003-05-22 20:13:16 +000021static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000022InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-"));
23
Chris Lattner6c8103f2003-05-22 20:13:16 +000024static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000025OutputFilename("o", cl::desc("Override output filename"),
26 cl::value_desc("filename"));
27
28static cl::opt<bool>
29Force("f", cl::desc("Overwrite output files"));
30
31static cl::opt<bool>
32DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden);
Chris Lattner00950542001-06-06 20:29:01 +000033
Chris Lattner04aa29d2003-08-18 20:47:13 +000034static cl::opt<bool>
35DisableVerify("disable-verify", cl::Hidden,
36 cl::desc("Do not run verifier on input LLVM (dangerous!"));
37
Chris Lattner00950542001-06-06 20:29:01 +000038int main(int argc, char **argv) {
Chris Lattner8f367bd2001-07-23 02:35:57 +000039 cl::ParseCommandLineOptions(argc, argv, " llvm .ll -> .bc assembler\n");
Chris Lattner00950542001-06-06 20:29:01 +000040
Anand Shukla63aaa112002-06-25 21:43:28 +000041 std::ostream *Out = 0;
Chris Lattner00950542001-06-06 20:29:01 +000042 try {
43 // Parse the file now...
Chris Lattner06272dc2002-04-07 22:34:19 +000044 std::auto_ptr<Module> M(ParseAssemblyFile(InputFilename));
45 if (M.get() == 0) {
Chris Lattner6c8103f2003-05-22 20:13:16 +000046 std::cerr << argv[0] << ": assembly didn't read correctly.\n";
Chris Lattner00950542001-06-06 20:29:01 +000047 return 1;
48 }
Chris Lattner1acbea12002-08-30 22:54:41 +000049
Chris Lattner04aa29d2003-08-18 20:47:13 +000050 if (!DisableVerify && verifyModule(*M.get())) {
Chris Lattner6c8103f2003-05-22 20:13:16 +000051 std::cerr << argv[0]
52 << ": assembly parsed, but does not verify as correct!\n";
Chris Lattner1acbea12002-08-30 22:54:41 +000053 return 1;
54 }
Chris Lattner00950542001-06-06 20:29:01 +000055
Chris Lattner6c8103f2003-05-22 20:13:16 +000056 if (DumpAsm) std::cerr << "Here's the assembly:\n" << M.get();
Chris Lattner8f367bd2001-07-23 02:35:57 +000057
Chris Lattner1e78f362001-07-23 19:27:24 +000058 if (OutputFilename != "") { // Specified an output filename?
Chris Lattner3ce0ea82003-05-31 21:47:16 +000059 if (OutputFilename != "-") { // Not stdout?
60 if (!Force && std::ifstream(OutputFilename.c_str())) {
61 // If force is not specified, make sure not to overwrite a file!
62 std::cerr << argv[0] << ": error opening '" << OutputFilename
63 << "': file exists!\n"
64 << "Use -f command line argument to force output\n";
65 return 1;
66 }
67 Out = new std::ofstream(OutputFilename.c_str());
68 } else { // Specified stdout
69 Out = &std::cout;
Chris Lattner697954c2002-01-20 22:54:45 +000070 }
Chris Lattner8f367bd2001-07-23 02:35:57 +000071 } else {
Chris Lattner1e78f362001-07-23 19:27:24 +000072 if (InputFilename == "-") {
73 OutputFilename = "-";
Anand Shukla63aaa112002-06-25 21:43:28 +000074 Out = &std::cout;
Chris Lattner8f367bd2001-07-23 02:35:57 +000075 } else {
Chris Lattner697954c2002-01-20 22:54:45 +000076 std::string IFN = InputFilename;
Chris Lattner8f367bd2001-07-23 02:35:57 +000077 int Len = IFN.length();
78 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
79 // Source ends in .ll
Chris Lattner697954c2002-01-20 22:54:45 +000080 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
Chris Lattner8f367bd2001-07-23 02:35:57 +000081 } else {
Chris Lattner1e78f362001-07-23 19:27:24 +000082 OutputFilename = IFN; // Append a .bc to it
Chris Lattner8f367bd2001-07-23 02:35:57 +000083 }
Chris Lattner1e78f362001-07-23 19:27:24 +000084 OutputFilename += ".bc";
Chris Lattner697954c2002-01-20 22:54:45 +000085
Chris Lattner888912d2002-01-22 21:07:24 +000086 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +000087 // If force is not specified, make sure not to overwrite a file!
Chris Lattner6c8103f2003-05-22 20:13:16 +000088 std::cerr << argv[0] << ": error opening '" << OutputFilename
89 << "': file exists!\n"
90 << "Use -f command line argument to force output\n";
Chris Lattner697954c2002-01-20 22:54:45 +000091 return 1;
92 }
93
94 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattner76d12292002-04-18 19:55:25 +000095 // Make sure that the Out file gets unlink'd from the disk if we get a
96 // SIGINT
97 RemoveFileOnSignal(OutputFilename);
Chris Lattner00950542001-06-06 20:29:01 +000098 }
99 }
Chris Lattner697954c2002-01-20 22:54:45 +0000100
101 if (!Out->good()) {
Chris Lattner6c8103f2003-05-22 20:13:16 +0000102 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000103 return 1;
104 }
Chris Lattner00950542001-06-06 20:29:01 +0000105
Chris Lattner06272dc2002-04-07 22:34:19 +0000106 WriteBytecodeToFile(M.get(), *Out);
Chris Lattner00950542001-06-06 20:29:01 +0000107 } catch (const ParseException &E) {
Chris Lattner6c8103f2003-05-22 20:13:16 +0000108 std::cerr << argv[0] << ": " << E.getMessage() << "\n";
Chris Lattner00950542001-06-06 20:29:01 +0000109 return 1;
110 }
111
Anand Shukla63aaa112002-06-25 21:43:28 +0000112 if (Out != &std::cout) delete Out;
Chris Lattner00950542001-06-06 20:29:01 +0000113 return 0;
114}
115