1 //===-- llvm-mc.cpp - Machine Code Hacking Driver -------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This utility is a simple driver that allows command line hacking on machine
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Support/CommandLine.h"
16 #include "llvm/Support/ManagedStatic.h"
17 #include "llvm/Support/MemoryBuffer.h"
18 #include "llvm/Support/PrettyStackTrace.h"
19 #include "llvm/Support/SourceMgr.h"
20 #include "llvm/Support/raw_ostream.h"
21 #include "llvm/System/Signals.h"
22 #include "AsmParser.h"
25 static cl::opt<std::string>
26 InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
28 static cl::opt<std::string>
29 OutputFilename("o", cl::desc("Output filename"),
30 cl::value_desc("filename"));
32 static cl::list<std::string>
33 IncludeDirs("I", cl::desc("Directory of include files"),
34 cl::value_desc("directory"), cl::Prefix);
41 static cl::opt<ActionType>
42 Action(cl::desc("Action to perform:"),
43 cl::init(AC_Assemble),
44 cl::values(clEnumValN(AC_AsLex, "as-lex",
45 "Lex tokens from a .s file"),
46 clEnumValN(AC_Assemble, "assemble",
47 "Assemble a .s file (default)"),
50 static int AsLexInput(const char *ProgName) {
51 std::string ErrorMessage;
52 MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename,
55 errs() << ProgName << ": ";
56 if (ErrorMessage.size())
57 errs() << ErrorMessage << "\n";
59 errs() << "input file didn't read correctly.\n";
65 // Tell SrcMgr about this buffer, which is what TGParser will pick up.
66 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
68 // Record the location of the include directories so that the lexer can find
70 SrcMgr.setIncludeDirs(IncludeDirs);
72 AsmLexer Lexer(SrcMgr);
76 asmtok::TokKind Tok = Lexer.Lex();
77 while (Tok != asmtok::Eof) {
80 Lexer.PrintMessage(Lexer.getLoc(), "driver: unknown token");
84 Error = true; // error already printed.
86 case asmtok::Identifier:
87 outs() << "identifier: " << Lexer.getCurStrVal() << '\n';
89 case asmtok::Register:
90 outs() << "register: " << Lexer.getCurStrVal() << '\n';
93 outs() << "string: " << Lexer.getCurStrVal() << '\n';
96 outs() << "int: " << Lexer.getCurIntVal() << '\n';
98 case asmtok::EndOfStatement: outs() << "EndOfStatement\n"; break;
99 case asmtok::Colon: outs() << "Colon\n"; break;
100 case asmtok::Plus: outs() << "Plus\n"; break;
101 case asmtok::Minus: outs() << "Minus\n"; break;
102 case asmtok::Tilde: outs() << "Tilde\n"; break;
103 case asmtok::Slash: outs() << "Slash\n"; break;
104 case asmtok::LParen: outs() << "LParen\n"; break;
105 case asmtok::RParen: outs() << "RParen\n"; break;
106 case asmtok::Star: outs() << "Star\n"; break;
107 case asmtok::Comma: outs() << "Comma\n"; break;
108 case asmtok::Dollar: outs() << "Dollar\n"; break;
117 static int AssembleInput(const char *ProgName) {
118 std::string ErrorMessage;
119 MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename,
122 errs() << ProgName << ": ";
123 if (ErrorMessage.size())
124 errs() << ErrorMessage << "\n";
126 errs() << "input file didn't read correctly.\n";
132 // Tell SrcMgr about this buffer, which is what TGParser will pick up.
133 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
135 // Record the location of the include directories so that the lexer can find
137 SrcMgr.setIncludeDirs(IncludeDirs);
139 AsmParser Parser(SrcMgr);
144 int main(int argc, char **argv) {
145 // Print a stack trace if we signal out.
146 sys::PrintStackTraceOnErrorSignal();
147 PrettyStackTraceProgram X(argc, argv);
148 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
149 cl::ParseCommandLineOptions(argc, argv, "llvm machine code playground\n");
154 return AsLexInput(argv[0]);
156 return AssembleInput(argv[0]);