blob: c9bd3b4e08c05a6d7bc5ebb71b66a1fdb40d5216 [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>
Anand Shukla63aaa112002-06-25 21:43:28 +000020using std::cerr;
Chris Lattnerc6a4bf12002-07-25 15:00:45 +000021using std::string;
Chris Lattner00950542001-06-06 20:29:01 +000022
Chris Lattner5ff62e92002-07-22 02:10:13 +000023static cl::opt<string>
24InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-"));
25
26static cl::opt<string>
27OutputFilename("o", cl::desc("Override output filename"),
28 cl::value_desc("filename"));
29
30static cl::opt<bool>
31Force("f", cl::desc("Overwrite output files"));
32
33static cl::opt<bool>
34DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden);
Chris Lattner00950542001-06-06 20:29:01 +000035
36int main(int argc, char **argv) {
Chris Lattner8f367bd2001-07-23 02:35:57 +000037 cl::ParseCommandLineOptions(argc, argv, " llvm .ll -> .bc assembler\n");
Chris Lattner00950542001-06-06 20:29:01 +000038
Anand Shukla63aaa112002-06-25 21:43:28 +000039 std::ostream *Out = 0;
Chris Lattner00950542001-06-06 20:29:01 +000040 try {
41 // Parse the file now...
Chris Lattner06272dc2002-04-07 22:34:19 +000042 std::auto_ptr<Module> M(ParseAssemblyFile(InputFilename));
43 if (M.get() == 0) {
Chris Lattnerd4c7f272002-07-30 21:43:25 +000044 cerr << argv[0] << ": assembly didn't read correctly.\n";
Chris Lattner00950542001-06-06 20:29:01 +000045 return 1;
46 }
Chris Lattner1acbea12002-08-30 22:54:41 +000047
48 if (verifyModule(*M.get())) {
49 cerr << argv[0] << ": assembly parsed, but does not verify as correct!\n";
50 return 1;
51 }
52
Chris Lattner00950542001-06-06 20:29:01 +000053
Chris Lattnerd43035e2002-04-28 05:13:45 +000054 if (DumpAsm) cerr << "Here's the assembly:\n" << M.get();
Chris Lattner8f367bd2001-07-23 02:35:57 +000055
Chris Lattner1e78f362001-07-23 19:27:24 +000056 if (OutputFilename != "") { // Specified an output filename?
Chris Lattner888912d2002-01-22 21:07:24 +000057 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +000058 // If force is not specified, make sure not to overwrite a file!
Chris Lattnerd4c7f272002-07-30 21:43:25 +000059 cerr << argv[0] << ": error opening '" << OutputFilename
60 << "': file exists!\n"
Chris Lattner697954c2002-01-20 22:54:45 +000061 << "Use -f command line argument to force output\n";
62 return 1;
63 }
64 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattner8f367bd2001-07-23 02:35:57 +000065 } else {
Chris Lattner1e78f362001-07-23 19:27:24 +000066 if (InputFilename == "-") {
67 OutputFilename = "-";
Anand Shukla63aaa112002-06-25 21:43:28 +000068 Out = &std::cout;
Chris Lattner8f367bd2001-07-23 02:35:57 +000069 } else {
Chris Lattner697954c2002-01-20 22:54:45 +000070 std::string IFN = InputFilename;
Chris Lattner8f367bd2001-07-23 02:35:57 +000071 int Len = IFN.length();
72 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
73 // Source ends in .ll
Chris Lattner697954c2002-01-20 22:54:45 +000074 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
Chris Lattner8f367bd2001-07-23 02:35:57 +000075 } else {
Chris Lattner1e78f362001-07-23 19:27:24 +000076 OutputFilename = IFN; // Append a .bc to it
Chris Lattner8f367bd2001-07-23 02:35:57 +000077 }
Chris Lattner1e78f362001-07-23 19:27:24 +000078 OutputFilename += ".bc";
Chris Lattner697954c2002-01-20 22:54:45 +000079
Chris Lattner888912d2002-01-22 21:07:24 +000080 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +000081 // If force is not specified, make sure not to overwrite a file!
Chris Lattnerd4c7f272002-07-30 21:43:25 +000082 cerr << argv[0] << ": error opening '" << OutputFilename
83 << "': file exists!\n"
Chris Lattner697954c2002-01-20 22:54:45 +000084 << "Use -f command line argument to force output\n";
85 return 1;
86 }
87
88 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattner76d12292002-04-18 19:55:25 +000089 // Make sure that the Out file gets unlink'd from the disk if we get a
90 // SIGINT
91 RemoveFileOnSignal(OutputFilename);
Chris Lattner00950542001-06-06 20:29:01 +000092 }
93 }
Chris Lattner697954c2002-01-20 22:54:45 +000094
95 if (!Out->good()) {
Chris Lattnerd4c7f272002-07-30 21:43:25 +000096 cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
Chris Lattner697954c2002-01-20 22:54:45 +000097 return 1;
98 }
Chris Lattner00950542001-06-06 20:29:01 +000099
Chris Lattner06272dc2002-04-07 22:34:19 +0000100 WriteBytecodeToFile(M.get(), *Out);
Chris Lattner00950542001-06-06 20:29:01 +0000101 } catch (const ParseException &E) {
Chris Lattnerd4c7f272002-07-30 21:43:25 +0000102 cerr << argv[0] << ": " << E.getMessage() << "\n";
Chris Lattner00950542001-06-06 20:29:01 +0000103 return 1;
104 }
105
Anand Shukla63aaa112002-06-25 21:43:28 +0000106 if (Out != &std::cout) delete Out;
Chris Lattner00950542001-06-06 20:29:01 +0000107 return 0;
108}
109