blob: c66dd598b9bb8f4f4cab59f367136ad4571be2dd [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 Lattner00950542001-06-06 20:29:01 +000020
Chris Lattner5ff62e92002-07-22 02:10:13 +000021static cl::opt<string>
22InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-"));
23
24static cl::opt<string>
25OutputFilename("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
34int main(int argc, char **argv) {
Chris Lattner8f367bd2001-07-23 02:35:57 +000035 cl::ParseCommandLineOptions(argc, argv, " llvm .ll -> .bc assembler\n");
Chris Lattner00950542001-06-06 20:29:01 +000036
Anand Shukla63aaa112002-06-25 21:43:28 +000037 std::ostream *Out = 0;
Chris Lattner00950542001-06-06 20:29:01 +000038 try {
39 // Parse the file now...
Chris Lattner06272dc2002-04-07 22:34:19 +000040 std::auto_ptr<Module> M(ParseAssemblyFile(InputFilename));
41 if (M.get() == 0) {
Chris Lattner00950542001-06-06 20:29:01 +000042 cerr << "assembly didn't read correctly.\n";
43 return 1;
44 }
45
Chris Lattnerd43035e2002-04-28 05:13:45 +000046 if (DumpAsm) cerr << "Here's the assembly:\n" << M.get();
Chris Lattner8f367bd2001-07-23 02:35:57 +000047
Chris Lattner1e78f362001-07-23 19:27:24 +000048 if (OutputFilename != "") { // Specified an output filename?
Chris Lattner888912d2002-01-22 21:07:24 +000049 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +000050 // If force is not specified, make sure not to overwrite a file!
51 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
52 << "Use -f command line argument to force output\n";
53 return 1;
54 }
55 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattner8f367bd2001-07-23 02:35:57 +000056 } else {
Chris Lattner1e78f362001-07-23 19:27:24 +000057 if (InputFilename == "-") {
58 OutputFilename = "-";
Anand Shukla63aaa112002-06-25 21:43:28 +000059 Out = &std::cout;
Chris Lattner8f367bd2001-07-23 02:35:57 +000060 } else {
Chris Lattner697954c2002-01-20 22:54:45 +000061 std::string IFN = InputFilename;
Chris Lattner8f367bd2001-07-23 02:35:57 +000062 int Len = IFN.length();
63 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
64 // Source ends in .ll
Chris Lattner697954c2002-01-20 22:54:45 +000065 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
Chris Lattner8f367bd2001-07-23 02:35:57 +000066 } else {
Chris Lattner1e78f362001-07-23 19:27:24 +000067 OutputFilename = IFN; // Append a .bc to it
Chris Lattner8f367bd2001-07-23 02:35:57 +000068 }
Chris Lattner1e78f362001-07-23 19:27:24 +000069 OutputFilename += ".bc";
Chris Lattner697954c2002-01-20 22:54:45 +000070
Chris Lattner888912d2002-01-22 21:07:24 +000071 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +000072 // If force is not specified, make sure not to overwrite a file!
73 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
74 << "Use -f command line argument to force output\n";
75 return 1;
76 }
77
78 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattner76d12292002-04-18 19:55:25 +000079 // Make sure that the Out file gets unlink'd from the disk if we get a
80 // SIGINT
81 RemoveFileOnSignal(OutputFilename);
Chris Lattner00950542001-06-06 20:29:01 +000082 }
83 }
Chris Lattner697954c2002-01-20 22:54:45 +000084
85 if (!Out->good()) {
86 cerr << "Error opening " << OutputFilename << "!\n";
87 return 1;
88 }
Chris Lattner00950542001-06-06 20:29:01 +000089
Chris Lattner06272dc2002-04-07 22:34:19 +000090 WriteBytecodeToFile(M.get(), *Out);
Chris Lattner00950542001-06-06 20:29:01 +000091 } catch (const ParseException &E) {
Anand Shukla63aaa112002-06-25 21:43:28 +000092 cerr << E.getMessage() << std::endl;
Chris Lattner00950542001-06-06 20:29:01 +000093 return 1;
94 }
95
Anand Shukla63aaa112002-06-25 21:43:28 +000096 if (Out != &std::cout) delete Out;
Chris Lattner00950542001-06-06 20:29:01 +000097 return 0;
98}
99