blob: ee664f1524986c36c03e86031cdf6a3899dca43c [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 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 Lattner1e78f362001-07-23 19:27:24 +000031 Module *C = ParseAssemblyFile(InputFilename);
Chris Lattner00950542001-06-06 20:29:01 +000032 if (C == 0) {
33 cerr << "assembly didn't read correctly.\n";
34 return 1;
35 }
36
Chris Lattner1e78f362001-07-23 19:27:24 +000037 if (DumpAsm)
Chris Lattner00950542001-06-06 20:29:01 +000038 cerr << "Here's the assembly:\n" << C;
Chris Lattner8f367bd2001-07-23 02:35:57 +000039
Chris Lattner1e78f362001-07-23 19:27:24 +000040 if (OutputFilename != "") { // Specified an output filename?
41 Out = new ofstream(OutputFilename.c_str(),
42 (Force ? 0 : ios::noreplace)|ios::out);
Chris Lattner8f367bd2001-07-23 02:35:57 +000043 } else {
Chris Lattner1e78f362001-07-23 19:27:24 +000044 if (InputFilename == "-") {
45 OutputFilename = "-";
Chris Lattner8f367bd2001-07-23 02:35:57 +000046 Out = &cout;
47 } else {
Chris Lattner1e78f362001-07-23 19:27:24 +000048 string IFN = InputFilename;
Chris Lattner8f367bd2001-07-23 02:35:57 +000049 int Len = IFN.length();
50 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
51 // Source ends in .ll
Chris Lattner1e78f362001-07-23 19:27:24 +000052 OutputFilename = string(IFN.begin(), IFN.end()-3);
Chris Lattner8f367bd2001-07-23 02:35:57 +000053 } else {
Chris Lattner1e78f362001-07-23 19:27:24 +000054 OutputFilename = IFN; // Append a .bc to it
Chris Lattner8f367bd2001-07-23 02:35:57 +000055 }
Chris Lattner1e78f362001-07-23 19:27:24 +000056 OutputFilename += ".bc";
57 Out = new ofstream(OutputFilename.c_str(),
58 (Force ? 0 : ios::noreplace)|ios::out);
Chris Lattner8f367bd2001-07-23 02:35:57 +000059 }
Chris Lattner00950542001-06-06 20:29:01 +000060
Chris Lattner00950542001-06-06 20:29:01 +000061 if (!Out->good()) {
Chris Lattner1e78f362001-07-23 19:27:24 +000062 cerr << "Error opening " << OutputFilename << "!\n";
Chris Lattner00950542001-06-06 20:29:01 +000063 delete C;
64 return 1;
65 }
66 }
67
68 WriteBytecodeToFile(C, *Out);
69
70 delete C;
71 } catch (const ParseException &E) {
72 cerr << E.getMessage() << endl;
73 return 1;
74 }
75
76 if (Out != &cout) delete Out;
77 return 0;
78}
79