blob: 4ee329661ffb2c456a226dc68ae139ccf0e2467d [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 +000019#include <iostream>
20using std::cerr;
Chris Lattner00950542001-06-06 20:29:01 +000021
Chris Lattner8f367bd2001-07-23 02:35:57 +000022cl::String InputFilename ("", "Parse <arg> file, compile to bytecode", 0, "-");
Chris Lattner1e78f362001-07-23 19:27:24 +000023cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
24cl::Flag Force ("f", "Overwrite output files", cl::NoFlags, false);
Chris Lattner8f367bd2001-07-23 02:35:57 +000025cl::Flag DumpAsm ("d", "Print assembly as parsed", cl::Hidden, false);
Chris Lattner00950542001-06-06 20:29:01 +000026
27int main(int argc, char **argv) {
Chris Lattner8f367bd2001-07-23 02:35:57 +000028 cl::ParseCommandLineOptions(argc, argv, " llvm .ll -> .bc assembler\n");
Chris Lattner00950542001-06-06 20:29:01 +000029
Anand Shukla63aaa112002-06-25 21:43:28 +000030 std::ostream *Out = 0;
Chris Lattner00950542001-06-06 20:29:01 +000031 try {
32 // Parse the file now...
Chris Lattner06272dc2002-04-07 22:34:19 +000033 std::auto_ptr<Module> M(ParseAssemblyFile(InputFilename));
34 if (M.get() == 0) {
Chris Lattner00950542001-06-06 20:29:01 +000035 cerr << "assembly didn't read correctly.\n";
36 return 1;
37 }
38
Chris Lattnerd43035e2002-04-28 05:13:45 +000039 if (DumpAsm) cerr << "Here's the assembly:\n" << M.get();
Chris Lattner8f367bd2001-07-23 02:35:57 +000040
Chris Lattner1e78f362001-07-23 19:27:24 +000041 if (OutputFilename != "") { // Specified an output filename?
Chris Lattner888912d2002-01-22 21:07:24 +000042 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +000043 // If force is not specified, make sure not to overwrite a file!
44 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
45 << "Use -f command line argument to force output\n";
46 return 1;
47 }
48 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattner8f367bd2001-07-23 02:35:57 +000049 } else {
Chris Lattner1e78f362001-07-23 19:27:24 +000050 if (InputFilename == "-") {
51 OutputFilename = "-";
Anand Shukla63aaa112002-06-25 21:43:28 +000052 Out = &std::cout;
Chris Lattner8f367bd2001-07-23 02:35:57 +000053 } else {
Chris Lattner697954c2002-01-20 22:54:45 +000054 std::string IFN = InputFilename;
Chris Lattner8f367bd2001-07-23 02:35:57 +000055 int Len = IFN.length();
56 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
57 // Source ends in .ll
Chris Lattner697954c2002-01-20 22:54:45 +000058 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
Chris Lattner8f367bd2001-07-23 02:35:57 +000059 } else {
Chris Lattner1e78f362001-07-23 19:27:24 +000060 OutputFilename = IFN; // Append a .bc to it
Chris Lattner8f367bd2001-07-23 02:35:57 +000061 }
Chris Lattner1e78f362001-07-23 19:27:24 +000062 OutputFilename += ".bc";
Chris Lattner697954c2002-01-20 22:54:45 +000063
Chris Lattner888912d2002-01-22 21:07:24 +000064 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +000065 // If force is not specified, make sure not to overwrite a file!
66 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
67 << "Use -f command line argument to force output\n";
68 return 1;
69 }
70
71 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattner76d12292002-04-18 19:55:25 +000072 // Make sure that the Out file gets unlink'd from the disk if we get a
73 // SIGINT
74 RemoveFileOnSignal(OutputFilename);
Chris Lattner00950542001-06-06 20:29:01 +000075 }
76 }
Chris Lattner697954c2002-01-20 22:54:45 +000077
78 if (!Out->good()) {
79 cerr << "Error opening " << OutputFilename << "!\n";
80 return 1;
81 }
Chris Lattner00950542001-06-06 20:29:01 +000082
Chris Lattner06272dc2002-04-07 22:34:19 +000083 WriteBytecodeToFile(M.get(), *Out);
Chris Lattner00950542001-06-06 20:29:01 +000084 } catch (const ParseException &E) {
Anand Shukla63aaa112002-06-25 21:43:28 +000085 cerr << E.getMessage() << std::endl;
Chris Lattner00950542001-06-06 20:29:01 +000086 return 1;
87 }
88
Anand Shukla63aaa112002-06-25 21:43:28 +000089 if (Out != &std::cout) delete Out;
Chris Lattner00950542001-06-06 20:29:01 +000090 return 0;
91}
92