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/MC/MCContext.h"
16 #include "llvm/MC/MCStreamer.h"
17 #include "llvm/ADT/OwningPtr.h"
18 #include "llvm/Support/CommandLine.h"
19 #include "llvm/Support/ManagedStatic.h"
20 #include "llvm/Support/MemoryBuffer.h"
21 #include "llvm/Support/PrettyStackTrace.h"
22 #include "llvm/Support/SourceMgr.h"
23 #include "llvm/Support/raw_ostream.h"
24 #include "llvm/System/Signals.h"
25 #include "AsmParser.h"
28 static cl::opt<std::string>
29 InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
31 static cl::opt<std::string>
32 OutputFilename("o", cl::desc("Output filename"),
33 cl::value_desc("filename"));
35 static cl::list<std::string>
36 IncludeDirs("I", cl::desc("Directory of include files"),
37 cl::value_desc("directory"), cl::Prefix);
44 static cl::opt<ActionType>
45 Action(cl::desc("Action to perform:"),
46 cl::init(AC_Assemble),
47 cl::values(clEnumValN(AC_AsLex, "as-lex",
48 "Lex tokens from a .s file"),
49 clEnumValN(AC_Assemble, "assemble",
50 "Assemble a .s file (default)"),
53 static int AsLexInput(const char *ProgName) {
54 std::string ErrorMessage;
55 MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename,
58 errs() << ProgName << ": ";
59 if (ErrorMessage.size())
60 errs() << ErrorMessage << "\n";
62 errs() << "input file didn't read correctly.\n";
68 // Tell SrcMgr about this buffer, which is what TGParser will pick up.
69 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
71 // Record the location of the include directories so that the lexer can find
73 SrcMgr.setIncludeDirs(IncludeDirs);
75 AsmLexer Lexer(SrcMgr);
79 asmtok::TokKind Tok = Lexer.Lex();
80 while (Tok != asmtok::Eof) {
83 Lexer.PrintMessage(Lexer.getLoc(), "unknown token", "warning");
87 Error = true; // error already printed.
89 case asmtok::Identifier:
90 outs() << "identifier: " << Lexer.getCurStrVal() << '\n';
92 case asmtok::Register:
93 outs() << "register: " << Lexer.getCurStrVal() << '\n';
96 outs() << "string: " << Lexer.getCurStrVal() << '\n';
99 outs() << "int: " << Lexer.getCurIntVal() << '\n';
102 case asmtok::Amp: outs() << "Amp\n"; break;
103 case asmtok::AmpAmp: outs() << "AmpAmp\n"; break;
104 case asmtok::Caret: outs() << "Caret\n"; break;
105 case asmtok::Colon: outs() << "Colon\n"; break;
106 case asmtok::Comma: outs() << "Comma\n"; break;
107 case asmtok::Dollar: outs() << "Dollar\n"; break;
108 case asmtok::EndOfStatement: outs() << "EndOfStatement\n"; break;
109 case asmtok::Eof: outs() << "Eof\n"; break;
110 case asmtok::Equal: outs() << "Equal\n"; break;
111 case asmtok::EqualEqual: outs() << "EqualEqual\n"; break;
112 case asmtok::Exclaim: outs() << "Exclaim\n"; break;
113 case asmtok::ExclaimEqual: outs() << "ExclaimEqual\n"; break;
114 case asmtok::Greater: outs() << "Greater\n"; break;
115 case asmtok::GreaterEqual: outs() << "GreaterEqual\n"; break;
116 case asmtok::GreaterGreater: outs() << "GreaterGreater\n"; break;
117 case asmtok::LParen: outs() << "LParen\n"; break;
118 case asmtok::Less: outs() << "Less\n"; break;
119 case asmtok::LessEqual: outs() << "LessEqual\n"; break;
120 case asmtok::LessGreater: outs() << "LessGreater\n"; break;
121 case asmtok::LessLess: outs() << "LessLess\n"; break;
122 case asmtok::Minus: outs() << "Minus\n"; break;
123 case asmtok::Percent: outs() << "Percent\n"; break;
124 case asmtok::Pipe: outs() << "Pipe\n"; break;
125 case asmtok::PipePipe: outs() << "PipePipe\n"; break;
126 case asmtok::Plus: outs() << "Plus\n"; break;
127 case asmtok::RParen: outs() << "RParen\n"; break;
128 case asmtok::Slash: outs() << "Slash\n"; break;
129 case asmtok::Star: outs() << "Star\n"; break;
130 case asmtok::Tilde: outs() << "Tilde\n"; break;
139 static int AssembleInput(const char *ProgName) {
140 std::string ErrorMessage;
141 MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename,
144 errs() << ProgName << ": ";
145 if (ErrorMessage.size())
146 errs() << ErrorMessage << "\n";
148 errs() << "input file didn't read correctly.\n";
154 // Tell SrcMgr about this buffer, which is what TGParser will pick up.
155 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
157 // Record the location of the include directories so that the lexer can find
159 SrcMgr.setIncludeDirs(IncludeDirs);
162 OwningPtr<MCStreamer> Str(createAsmStreamer(Ctx, outs()));
164 // FIXME: Target hook & command line option for initial section.
165 Str.get()->SwitchSection(Ctx.GetSection("__TEXT,__text,regular,pure_instructions"));
167 AsmParser Parser(SrcMgr, Ctx, *Str.get());
172 int main(int argc, char **argv) {
173 // Print a stack trace if we signal out.
174 sys::PrintStackTraceOnErrorSignal();
175 PrettyStackTraceProgram X(argc, argv);
176 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
177 cl::ParseCommandLineOptions(argc, argv, "llvm machine code playground\n");
182 return AsLexInput(argv[0]);
184 return AssembleInput(argv[0]);