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 "llvm/Target/TargetRegistry.h"
26 #include "llvm/Target/TargetSelect.h"
27 #include "AsmParser.h"
30 static cl::opt<std::string>
31 InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
33 static cl::opt<std::string>
34 OutputFilename("o", cl::desc("Output filename"),
35 cl::value_desc("filename"));
37 static cl::list<std::string>
38 IncludeDirs("I", cl::desc("Directory of include files"),
39 cl::value_desc("directory"), cl::Prefix);
41 static cl::opt<std::string>
42 Triple("triple", cl::desc("Target triple to assemble for,"
43 "see -version for available targets"),
44 cl::init(LLVM_HOSTTRIPLE));
51 static cl::opt<ActionType>
52 Action(cl::desc("Action to perform:"),
53 cl::init(AC_Assemble),
54 cl::values(clEnumValN(AC_AsLex, "as-lex",
55 "Lex tokens from a .s file"),
56 clEnumValN(AC_Assemble, "assemble",
57 "Assemble a .s file (default)"),
60 static int AsLexInput(const char *ProgName) {
61 std::string ErrorMessage;
62 MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename,
65 errs() << ProgName << ": ";
66 if (ErrorMessage.size())
67 errs() << ErrorMessage << "\n";
69 errs() << "input file didn't read correctly.\n";
75 // Tell SrcMgr about this buffer, which is what TGParser will pick up.
76 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
78 // Record the location of the include directories so that the lexer can find
80 SrcMgr.setIncludeDirs(IncludeDirs);
82 AsmLexer Lexer(SrcMgr);
86 asmtok::TokKind Tok = Lexer.Lex();
87 while (Tok != asmtok::Eof) {
90 Lexer.PrintMessage(Lexer.getLoc(), "unknown token", "warning");
94 Error = true; // error already printed.
96 case asmtok::Identifier:
97 outs() << "identifier: " << Lexer.getCurStrVal() << '\n';
99 case asmtok::Register:
100 outs() << "register: " << Lexer.getCurStrVal() << '\n';
103 outs() << "string: " << Lexer.getCurStrVal() << '\n';
106 outs() << "int: " << Lexer.getCurIntVal() << '\n';
109 case asmtok::Amp: outs() << "Amp\n"; break;
110 case asmtok::AmpAmp: outs() << "AmpAmp\n"; break;
111 case asmtok::Caret: outs() << "Caret\n"; break;
112 case asmtok::Colon: outs() << "Colon\n"; break;
113 case asmtok::Comma: outs() << "Comma\n"; break;
114 case asmtok::Dollar: outs() << "Dollar\n"; break;
115 case asmtok::EndOfStatement: outs() << "EndOfStatement\n"; break;
116 case asmtok::Eof: outs() << "Eof\n"; break;
117 case asmtok::Equal: outs() << "Equal\n"; break;
118 case asmtok::EqualEqual: outs() << "EqualEqual\n"; break;
119 case asmtok::Exclaim: outs() << "Exclaim\n"; break;
120 case asmtok::ExclaimEqual: outs() << "ExclaimEqual\n"; break;
121 case asmtok::Greater: outs() << "Greater\n"; break;
122 case asmtok::GreaterEqual: outs() << "GreaterEqual\n"; break;
123 case asmtok::GreaterGreater: outs() << "GreaterGreater\n"; break;
124 case asmtok::LParen: outs() << "LParen\n"; break;
125 case asmtok::Less: outs() << "Less\n"; break;
126 case asmtok::LessEqual: outs() << "LessEqual\n"; break;
127 case asmtok::LessGreater: outs() << "LessGreater\n"; break;
128 case asmtok::LessLess: outs() << "LessLess\n"; break;
129 case asmtok::Minus: outs() << "Minus\n"; break;
130 case asmtok::Percent: outs() << "Percent\n"; break;
131 case asmtok::Pipe: outs() << "Pipe\n"; break;
132 case asmtok::PipePipe: outs() << "PipePipe\n"; break;
133 case asmtok::Plus: outs() << "Plus\n"; break;
134 case asmtok::RParen: outs() << "RParen\n"; break;
135 case asmtok::Slash: outs() << "Slash\n"; break;
136 case asmtok::Star: outs() << "Star\n"; break;
137 case asmtok::Tilde: outs() << "Tilde\n"; break;
146 static int AssembleInput(const char *ProgName) {
147 // Get the target specific parser.
149 const Target *TheTarget =
150 TargetRegistry::getClosestStaticTargetForTriple(Triple, Error);
151 if (TheTarget == 0) {
152 errs() << ProgName << ": error: unable to get target for '" << Triple
153 << "', see --version and --triple.\n";
157 TargetAsmParser *TAP = TheTarget->createAsmParser();
160 << ": error: this target does not support assembly parsing.\n";
164 std::string ErrorMessage;
165 MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename,
168 errs() << ProgName << ": ";
169 if (ErrorMessage.size())
170 errs() << ErrorMessage << "\n";
172 errs() << "input file didn't read correctly.\n";
178 // Tell SrcMgr about this buffer, which is what TGParser will pick up.
179 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
181 // Record the location of the include directories so that the lexer can find
183 SrcMgr.setIncludeDirs(IncludeDirs);
186 OwningPtr<MCStreamer> Str(createAsmStreamer(Ctx, outs()));
188 // FIXME: Target hook & command line option for initial section.
189 Str.get()->SwitchSection(Ctx.GetSection("__TEXT,__text,"
190 "regular,pure_instructions"));
192 AsmParser Parser(SrcMgr, Ctx, *Str.get(), *TAP);
197 int main(int argc, char **argv) {
198 // Print a stack trace if we signal out.
199 sys::PrintStackTraceOnErrorSignal();
200 PrettyStackTraceProgram X(argc, argv);
201 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
203 // Initialize targets and assembly parsers.
204 llvm::InitializeAllTargetInfos();
205 llvm::InitializeAllAsmParsers();
207 cl::ParseCommandLineOptions(argc, argv, "llvm machine code playground\n");
212 return AsLexInput(argv[0]);
214 return AssembleInput(argv[0]);