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"
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);
40 static cl::opt<ActionType>
41 Action(cl::desc("Action to perform:"),
42 cl::values(clEnumValN(AC_Assemble, "assemble",
43 "Assemble a .s file (default)"),
46 static int AssembleInput(const char *ProgName) {
47 std::string ErrorMessage;
48 MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename,
51 errs() << ProgName << ": ";
52 if (ErrorMessage.size())
53 errs() << ErrorMessage << "\n";
55 errs() << "input file didn't read correctly.\n";
61 // Tell SrcMgr about this buffer, which is what TGParser will pick up.
62 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
64 // Record the location of the include directories so that the lexer can find
66 SrcMgr.setIncludeDirs(IncludeDirs);
70 AsmLexer Lexer(SrcMgr);
72 asmtok::TokKind Tok = Lexer.Lex();
73 while (Tok != asmtok::Eof) {
75 default: Lexer.PrintError(Lexer.getLoc(), "driver: unknown token"); break;
77 Lexer.PrintError(Lexer.getLoc(), "error, bad token");
79 case asmtok::Identifier:
80 outs() << "identifier: " << Lexer.getCurStrVal() << '\n';
82 case asmtok::Register:
83 outs() << "register: " << Lexer.getCurStrVal() << '\n';
86 outs() << "string: " << Lexer.getCurStrVal() << '\n';
89 outs() << "int: " << Lexer.getCurIntVal() << '\n';
91 case asmtok::EndOfStatement: outs() << "EndOfStatement\n"; break;
92 case asmtok::Colon: outs() << "Colon\n"; break;
93 case asmtok::Plus: outs() << "Plus\n"; break;
94 case asmtok::Minus: outs() << "Minus\n"; break;
95 case asmtok::Slash: outs() << "Slash\n"; break;
96 case asmtok::LParen: outs() << "LParen\n"; break;
97 case asmtok::RParen: outs() << "RParen\n"; break;
98 case asmtok::Star: outs() << "Star\n"; break;
99 case asmtok::Comma: outs() << "Comma\n"; break;
100 case asmtok::Dollar: outs() << "Dollar\n"; break;
110 int main(int argc, char **argv) {
111 // Print a stack trace if we signal out.
112 sys::PrintStackTraceOnErrorSignal();
113 PrettyStackTraceProgram X(argc, argv);
114 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
115 cl::ParseCommandLineOptions(argc, argv, "llvm machine code playground\n");
120 return AssembleInput(argv[0]);