blob: 0fbd1e6cd419a1d415a2998919315fac8537543f [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"
14#include "llvm/Assembly/Writer.h"
15#include "llvm/Bytecode/Writer.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000016#include "Support/CommandLine.h"
17#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 Lattner697954c2002-01-20 22:54:45 +000032 std::auto_ptr<Module> C(ParseAssemblyFile(InputFilename));
33 if (C.get() == 0) {
Chris Lattner00950542001-06-06 20:29:01 +000034 cerr << "assembly didn't read correctly.\n";
35 return 1;
36 }
37
Chris Lattner1e78f362001-07-23 19:27:24 +000038 if (DumpAsm)
Chris Lattner697954c2002-01-20 22:54:45 +000039 cerr << "Here's the assembly:\n" << C.get();
Chris Lattner8f367bd2001-07-23 02:35:57 +000040
Chris Lattner1e78f362001-07-23 19:27:24 +000041 if (OutputFilename != "") { // Specified an output filename?
Chris Lattner697954c2002-01-20 22:54:45 +000042 if (!Force && !std::ifstream(OutputFilename.c_str())) {
43 // 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 = "-";
Chris Lattner8f367bd2001-07-23 02:35:57 +000052 Out = &cout;
53 } 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
64 if (!Force && !std::ifstream(OutputFilename.c_str())) {
65 // 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 Lattner00950542001-06-06 20:29:01 +000072 }
73 }
Chris Lattner697954c2002-01-20 22:54:45 +000074
75 if (!Out->good()) {
76 cerr << "Error opening " << OutputFilename << "!\n";
77 return 1;
78 }
Chris Lattner00950542001-06-06 20:29:01 +000079
Chris Lattner697954c2002-01-20 22:54:45 +000080 WriteBytecodeToFile(C.get(), *Out);
Chris Lattner00950542001-06-06 20:29:01 +000081 } catch (const ParseException &E) {
82 cerr << E.getMessage() << endl;
83 return 1;
84 }
85
86 if (Out != &cout) delete Out;
87 return 0;
88}
89