blob: 3e8a9eaf31a6b56230b3f1c766b3e0402de79239 [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.
9//
10//===------------------------------------------------------------------------===
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>
18#include <string>
Chris Lattner697954c2002-01-20 22:54:45 +000019#include <memory>
Chris Lattner00950542001-06-06 20:29:01 +000020
Chris Lattner8f367bd2001-07-23 02:35:57 +000021cl::String InputFilename ("", "Parse <arg> file, compile to bytecode", 0, "-");
Chris Lattner1e78f362001-07-23 19:27:24 +000022cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
23cl::Flag Force ("f", "Overwrite output files", cl::NoFlags, false);
Chris Lattner8f367bd2001-07-23 02:35:57 +000024cl::Flag DumpAsm ("d", "Print assembly as parsed", cl::Hidden, false);
Chris Lattner00950542001-06-06 20:29:01 +000025
26int main(int argc, char **argv) {
Chris Lattner8f367bd2001-07-23 02:35:57 +000027 cl::ParseCommandLineOptions(argc, argv, " llvm .ll -> .bc assembler\n");
Chris Lattner00950542001-06-06 20:29:01 +000028
Chris Lattner8f367bd2001-07-23 02:35:57 +000029 ostream *Out = 0;
Chris Lattner00950542001-06-06 20:29:01 +000030 try {
31 // Parse the file now...
Chris Lattner06272dc2002-04-07 22:34:19 +000032 std::auto_ptr<Module> M(ParseAssemblyFile(InputFilename));
33 if (M.get() == 0) {
Chris Lattner00950542001-06-06 20:29:01 +000034 cerr << "assembly didn't read correctly.\n";
35 return 1;
36 }
37
Chris Lattner06272dc2002-04-07 22:34:19 +000038 if (DumpAsm) {
39 cerr << "Here's the assembly:\n";
40 M.get()->dump();
41 }
Chris Lattner8f367bd2001-07-23 02:35:57 +000042
Chris Lattner1e78f362001-07-23 19:27:24 +000043 if (OutputFilename != "") { // Specified an output filename?
Chris Lattner888912d2002-01-22 21:07:24 +000044 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +000045 // If force is not specified, make sure not to overwrite a file!
46 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
47 << "Use -f command line argument to force output\n";
48 return 1;
49 }
50 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattner8f367bd2001-07-23 02:35:57 +000051 } else {
Chris Lattner1e78f362001-07-23 19:27:24 +000052 if (InputFilename == "-") {
53 OutputFilename = "-";
Chris Lattner8f367bd2001-07-23 02:35:57 +000054 Out = &cout;
55 } else {
Chris Lattner697954c2002-01-20 22:54:45 +000056 std::string IFN = InputFilename;
Chris Lattner8f367bd2001-07-23 02:35:57 +000057 int Len = IFN.length();
58 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
59 // Source ends in .ll
Chris Lattner697954c2002-01-20 22:54:45 +000060 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
Chris Lattner8f367bd2001-07-23 02:35:57 +000061 } else {
Chris Lattner1e78f362001-07-23 19:27:24 +000062 OutputFilename = IFN; // Append a .bc to it
Chris Lattner8f367bd2001-07-23 02:35:57 +000063 }
Chris Lattner1e78f362001-07-23 19:27:24 +000064 OutputFilename += ".bc";
Chris Lattner697954c2002-01-20 22:54:45 +000065
Chris Lattner888912d2002-01-22 21:07:24 +000066 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +000067 // If force is not specified, make sure not to overwrite a file!
68 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
69 << "Use -f command line argument to force output\n";
70 return 1;
71 }
72
73 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattner76d12292002-04-18 19:55:25 +000074 // Make sure that the Out file gets unlink'd from the disk if we get a
75 // SIGINT
76 RemoveFileOnSignal(OutputFilename);
Chris Lattner00950542001-06-06 20:29:01 +000077 }
78 }
Chris Lattner697954c2002-01-20 22:54:45 +000079
80 if (!Out->good()) {
81 cerr << "Error opening " << OutputFilename << "!\n";
82 return 1;
83 }
Chris Lattner00950542001-06-06 20:29:01 +000084
Chris Lattner06272dc2002-04-07 22:34:19 +000085 WriteBytecodeToFile(M.get(), *Out);
Chris Lattner00950542001-06-06 20:29:01 +000086 } catch (const ParseException &E) {
87 cerr << E.getMessage() << endl;
88 return 1;
89 }
90
91 if (Out != &cout) delete Out;
92 return 0;
93}
94