blob: 36f845687838cd1fafa77b588b30244157dc8ebb [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 Lattnercee8f9a2001-11-27 00:03:19 +000015#include "Support/CommandLine.h"
Chris Lattner76d12292002-04-18 19:55:25 +000016#include "Support/Signals.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000017#include <fstream>
Chris Lattner697954c2002-01-20 22:54:45 +000018#include <memory>
Anand Shukla63aaa112002-06-25 21:43:28 +000019using std::cerr;
Chris Lattnerc6a4bf12002-07-25 15:00:45 +000020using std::string;
Chris Lattner00950542001-06-06 20:29:01 +000021
Chris Lattner5ff62e92002-07-22 02:10:13 +000022static cl::opt<string>
23InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-"));
24
25static cl::opt<string>
26OutputFilename("o", cl::desc("Override output filename"),
27 cl::value_desc("filename"));
28
29static cl::opt<bool>
30Force("f", cl::desc("Overwrite output files"));
31
32static cl::opt<bool>
33DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden);
Chris Lattner00950542001-06-06 20:29:01 +000034
35int main(int argc, char **argv) {
Chris Lattner8f367bd2001-07-23 02:35:57 +000036 cl::ParseCommandLineOptions(argc, argv, " llvm .ll -> .bc assembler\n");
Chris Lattner00950542001-06-06 20:29:01 +000037
Anand Shukla63aaa112002-06-25 21:43:28 +000038 std::ostream *Out = 0;
Chris Lattner00950542001-06-06 20:29:01 +000039 try {
40 // Parse the file now...
Chris Lattner06272dc2002-04-07 22:34:19 +000041 std::auto_ptr<Module> M(ParseAssemblyFile(InputFilename));
42 if (M.get() == 0) {
Chris Lattner00950542001-06-06 20:29:01 +000043 cerr << "assembly didn't read correctly.\n";
44 return 1;
45 }
46
Chris Lattnerd43035e2002-04-28 05:13:45 +000047 if (DumpAsm) cerr << "Here's the assembly:\n" << M.get();
Chris Lattner8f367bd2001-07-23 02:35:57 +000048
Chris Lattner1e78f362001-07-23 19:27:24 +000049 if (OutputFilename != "") { // Specified an output filename?
Chris Lattner888912d2002-01-22 21:07:24 +000050 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +000051 // If force is not specified, make sure not to overwrite a file!
52 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
53 << "Use -f command line argument to force output\n";
54 return 1;
55 }
56 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattner8f367bd2001-07-23 02:35:57 +000057 } else {
Chris Lattner1e78f362001-07-23 19:27:24 +000058 if (InputFilename == "-") {
59 OutputFilename = "-";
Anand Shukla63aaa112002-06-25 21:43:28 +000060 Out = &std::cout;
Chris Lattner8f367bd2001-07-23 02:35:57 +000061 } else {
Chris Lattner697954c2002-01-20 22:54:45 +000062 std::string IFN = InputFilename;
Chris Lattner8f367bd2001-07-23 02:35:57 +000063 int Len = IFN.length();
64 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
65 // Source ends in .ll
Chris Lattner697954c2002-01-20 22:54:45 +000066 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
Chris Lattner8f367bd2001-07-23 02:35:57 +000067 } else {
Chris Lattner1e78f362001-07-23 19:27:24 +000068 OutputFilename = IFN; // Append a .bc to it
Chris Lattner8f367bd2001-07-23 02:35:57 +000069 }
Chris Lattner1e78f362001-07-23 19:27:24 +000070 OutputFilename += ".bc";
Chris Lattner697954c2002-01-20 22:54:45 +000071
Chris Lattner888912d2002-01-22 21:07:24 +000072 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +000073 // If force is not specified, make sure not to overwrite a file!
74 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
75 << "Use -f command line argument to force output\n";
76 return 1;
77 }
78
79 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattner76d12292002-04-18 19:55:25 +000080 // Make sure that the Out file gets unlink'd from the disk if we get a
81 // SIGINT
82 RemoveFileOnSignal(OutputFilename);
Chris Lattner00950542001-06-06 20:29:01 +000083 }
84 }
Chris Lattner697954c2002-01-20 22:54:45 +000085
86 if (!Out->good()) {
87 cerr << "Error opening " << OutputFilename << "!\n";
88 return 1;
89 }
Chris Lattner00950542001-06-06 20:29:01 +000090
Chris Lattner06272dc2002-04-07 22:34:19 +000091 WriteBytecodeToFile(M.get(), *Out);
Chris Lattner00950542001-06-06 20:29:01 +000092 } catch (const ParseException &E) {
Anand Shukla63aaa112002-06-25 21:43:28 +000093 cerr << E.getMessage() << std::endl;
Chris Lattner00950542001-06-06 20:29:01 +000094 return 1;
95 }
96
Anand Shukla63aaa112002-06-25 21:43:28 +000097 if (Out != &std::cout) delete Out;
Chris Lattner00950542001-06-06 20:29:01 +000098 return 0;
99}
100