blob: 8cb274798dbe20662e688d830daa2e87f0101c00 [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
12#include <iostream.h>
13#include <fstream.h>
14#include <string>
15#include "llvm/Module.h"
16#include "llvm/Assembly/Parser.h"
17#include "llvm/Assembly/Writer.h"
18#include "llvm/Bytecode/Writer.h"
Chris Lattner57dbb3a2001-07-23 17:46:59 +000019#include "llvm/Support/CommandLine.h"
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, "-");
22cl::String OutputFilename("o", "Override output filename", 0, "");
23cl::Flag Force ("f", "Overwrite output files", 0, false);
24cl::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 Lattner8f367bd2001-07-23 02:35:57 +000032 Module *C = ParseAssemblyFile(InputFilename.getValue());
Chris Lattner00950542001-06-06 20:29:01 +000033 if (C == 0) {
34 cerr << "assembly didn't read correctly.\n";
35 return 1;
36 }
37
Chris Lattner8f367bd2001-07-23 02:35:57 +000038 if (DumpAsm.getValue())
Chris Lattner00950542001-06-06 20:29:01 +000039 cerr << "Here's the assembly:\n" << C;
Chris Lattner8f367bd2001-07-23 02:35:57 +000040
41 if (OutputFilename.getValue() != "") { // Specified an output filename?
42 Out = new ofstream(OutputFilename.getValue().c_str(),
43 (Force.getValue() ? 0 : ios::noreplace)|ios::out);
44 } else {
45 if (InputFilename.getValue() == "-") {
46 OutputFilename.setValue("-");
47 Out = &cout;
48 } else {
49 string IFN = InputFilename.getValue();
50 int Len = IFN.length();
51 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
52 // Source ends in .ll
53 OutputFilename.setValue(string(IFN.begin(), IFN.end()-3));
54 } else {
55 OutputFilename.setValue(IFN); // Append a .bc to it
56 }
57 OutputFilename.setValue(OutputFilename.getValue() + ".bc");
58 Out = new ofstream(OutputFilename.getValue().c_str(),
59 (Force.getValue() ? 0 : ios::noreplace)|ios::out);
60 }
Chris Lattner00950542001-06-06 20:29:01 +000061
Chris Lattner00950542001-06-06 20:29:01 +000062 if (!Out->good()) {
Chris Lattner8f367bd2001-07-23 02:35:57 +000063 cerr << "Error opening " << OutputFilename.getValue() << "!\n";
Chris Lattner00950542001-06-06 20:29:01 +000064 delete C;
65 return 1;
66 }
67 }
68
69 WriteBytecodeToFile(C, *Out);
70
71 delete C;
72 } catch (const ParseException &E) {
73 cerr << E.getMessage() << endl;
74 return 1;
75 }
76
77 if (Out != &cout) delete Out;
78 return 0;
79}
80