blob: 7bea711826e57c4b5ce0c8c3db87cb5b4058c05e [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"
16#include <fstream>
17#include <string>
Chris Lattner697954c2002-01-20 22:54:45 +000018#include <memory>
Chris Lattner00950542001-06-06 20:29:01 +000019
Chris Lattner8f367bd2001-07-23 02:35:57 +000020cl::String InputFilename ("", "Parse <arg> file, compile to bytecode", 0, "-");
Chris Lattner1e78f362001-07-23 19:27:24 +000021cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
22cl::Flag Force ("f", "Overwrite output files", cl::NoFlags, false);
Chris Lattner8f367bd2001-07-23 02:35:57 +000023cl::Flag DumpAsm ("d", "Print assembly as parsed", cl::Hidden, false);
Chris Lattner00950542001-06-06 20:29:01 +000024
25int main(int argc, char **argv) {
Chris Lattner8f367bd2001-07-23 02:35:57 +000026 cl::ParseCommandLineOptions(argc, argv, " llvm .ll -> .bc assembler\n");
Chris Lattner00950542001-06-06 20:29:01 +000027
Chris Lattner8f367bd2001-07-23 02:35:57 +000028 ostream *Out = 0;
Chris Lattner00950542001-06-06 20:29:01 +000029 try {
30 // Parse the file now...
Chris Lattner06272dc2002-04-07 22:34:19 +000031 std::auto_ptr<Module> M(ParseAssemblyFile(InputFilename));
32 if (M.get() == 0) {
Chris Lattner00950542001-06-06 20:29:01 +000033 cerr << "assembly didn't read correctly.\n";
34 return 1;
35 }
36
Chris Lattner06272dc2002-04-07 22:34:19 +000037 if (DumpAsm) {
38 cerr << "Here's the assembly:\n";
39 M.get()->dump();
40 }
Chris Lattner8f367bd2001-07-23 02:35:57 +000041
Chris Lattner1e78f362001-07-23 19:27:24 +000042 if (OutputFilename != "") { // Specified an output filename?
Chris Lattner888912d2002-01-22 21:07:24 +000043 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +000044 // If force is not specified, make sure not to overwrite a file!
45 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
46 << "Use -f command line argument to force output\n";
47 return 1;
48 }
49 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattner8f367bd2001-07-23 02:35:57 +000050 } else {
Chris Lattner1e78f362001-07-23 19:27:24 +000051 if (InputFilename == "-") {
52 OutputFilename = "-";
Chris Lattner8f367bd2001-07-23 02:35:57 +000053 Out = &cout;
54 } else {
Chris Lattner697954c2002-01-20 22:54:45 +000055 std::string IFN = InputFilename;
Chris Lattner8f367bd2001-07-23 02:35:57 +000056 int Len = IFN.length();
57 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
58 // Source ends in .ll
Chris Lattner697954c2002-01-20 22:54:45 +000059 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
Chris Lattner8f367bd2001-07-23 02:35:57 +000060 } else {
Chris Lattner1e78f362001-07-23 19:27:24 +000061 OutputFilename = IFN; // Append a .bc to it
Chris Lattner8f367bd2001-07-23 02:35:57 +000062 }
Chris Lattner1e78f362001-07-23 19:27:24 +000063 OutputFilename += ".bc";
Chris Lattner697954c2002-01-20 22:54:45 +000064
Chris Lattner888912d2002-01-22 21:07:24 +000065 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +000066 // If force is not specified, make sure not to overwrite a file!
67 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
68 << "Use -f command line argument to force output\n";
69 return 1;
70 }
71
72 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattner00950542001-06-06 20:29:01 +000073 }
74 }
Chris Lattner697954c2002-01-20 22:54:45 +000075
76 if (!Out->good()) {
77 cerr << "Error opening " << OutputFilename << "!\n";
78 return 1;
79 }
Chris Lattner00950542001-06-06 20:29:01 +000080
Chris Lattner06272dc2002-04-07 22:34:19 +000081 WriteBytecodeToFile(M.get(), *Out);
Chris Lattner00950542001-06-06 20:29:01 +000082 } catch (const ParseException &E) {
83 cerr << E.getMessage() << endl;
84 return 1;
85 }
86
87 if (Out != &cout) delete Out;
88 return 0;
89}
90