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/MCParser/AsmLexer.h"
16 #include "llvm/MC/MCParser/MCAsmLexer.h"
17 #include "llvm/MC/MCAsmBackend.h"
18 #include "llvm/MC/MCContext.h"
19 #include "llvm/MC/MCCodeEmitter.h"
20 #include "llvm/MC/MCInstPrinter.h"
21 #include "llvm/MC/MCInstrInfo.h"
22 #include "llvm/MC/MCObjectFileInfo.h"
23 #include "llvm/MC/MCRegisterInfo.h"
24 #include "llvm/MC/MCSectionMachO.h"
25 #include "llvm/MC/MCStreamer.h"
26 #include "llvm/MC/MCSubtargetInfo.h"
27 #include "llvm/MC/MCTargetAsmParser.h"
28 #include "llvm/MC/SubtargetFeature.h"
29 #include "llvm/ADT/OwningPtr.h"
30 #include "llvm/Support/CommandLine.h"
31 #include "llvm/Support/FileUtilities.h"
32 #include "llvm/Support/FormattedStream.h"
33 #include "llvm/Support/ManagedStatic.h"
34 #include "llvm/Support/MemoryBuffer.h"
35 #include "llvm/Support/PrettyStackTrace.h"
36 #include "llvm/Support/SourceMgr.h"
37 #include "llvm/Support/ToolOutputFile.h"
38 #include "llvm/Support/Host.h"
39 #include "llvm/Support/Signals.h"
40 #include "llvm/Support/TargetRegistry.h"
41 #include "llvm/Support/TargetSelect.h"
42 #include "llvm/Support/system_error.h"
43 #include "Disassembler.h"
46 static cl::opt<std::string>
47 InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
49 static cl::opt<std::string>
50 OutputFilename("o", cl::desc("Output filename"),
51 cl::value_desc("filename"));
54 ShowEncoding("show-encoding", cl::desc("Show instruction encodings"));
57 ShowInst("show-inst", cl::desc("Show internal instruction representation"));
60 ShowInstOperands("show-inst-operands",
61 cl::desc("Show instructions operands as parsed"));
63 static cl::opt<unsigned>
64 OutputAsmVariant("output-asm-variant",
65 cl::desc("Syntax variant to use for output printing"));
68 RelaxAll("mc-relax-all", cl::desc("Relax all fixups"));
71 NoExecStack("mc-no-exec-stack", cl::desc("File doesn't need an exec stack"));
78 static cl::opt<OutputFileType>
79 FileType("filetype", cl::init(OFT_AssemblyFile),
80 cl::desc("Choose an output file type:"),
82 clEnumValN(OFT_AssemblyFile, "asm",
83 "Emit an assembly ('.s') file"),
84 clEnumValN(OFT_Null, "null",
85 "Don't emit anything (for timing purposes)"),
86 clEnumValN(OFT_ObjectFile, "obj",
87 "Emit a native object ('.o') file"),
90 static cl::list<std::string>
91 IncludeDirs("I", cl::desc("Directory of include files"),
92 cl::value_desc("directory"), cl::Prefix);
94 static cl::opt<std::string>
95 ArchName("arch", cl::desc("Target arch to assemble for, "
96 "see -version for available targets"));
98 static cl::opt<std::string>
99 TripleName("triple", cl::desc("Target triple to assemble for, "
100 "see -version for available targets"));
102 static cl::opt<std::string>
104 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
105 cl::value_desc("cpu-name"),
108 static cl::list<std::string>
111 cl::desc("Target specific attributes (-mattr=help for details)"),
112 cl::value_desc("a1,+a2,-a3,..."));
114 static cl::opt<Reloc::Model>
115 RelocModel("relocation-model",
116 cl::desc("Choose relocation model"),
117 cl::init(Reloc::Default),
119 clEnumValN(Reloc::Default, "default",
120 "Target default relocation model"),
121 clEnumValN(Reloc::Static, "static",
122 "Non-relocatable code"),
123 clEnumValN(Reloc::PIC_, "pic",
124 "Fully relocatable, position independent code"),
125 clEnumValN(Reloc::DynamicNoPIC, "dynamic-no-pic",
126 "Relocatable external references, non-relocatable code"),
129 static cl::opt<llvm::CodeModel::Model>
130 CMModel("code-model",
131 cl::desc("Choose code model"),
132 cl::init(CodeModel::Default),
133 cl::values(clEnumValN(CodeModel::Default, "default",
134 "Target default code model"),
135 clEnumValN(CodeModel::Small, "small",
137 clEnumValN(CodeModel::Kernel, "kernel",
138 "Kernel code model"),
139 clEnumValN(CodeModel::Medium, "medium",
140 "Medium code model"),
141 clEnumValN(CodeModel::Large, "large",
146 NoInitialTextSection("n", cl::desc("Don't assume assembly file starts "
147 "in the text section"));
150 SaveTempLabels("L", cl::desc("Don't discard temporary labels"));
153 GenDwarfForAssembly("g", cl::desc("Generate dwarf debugging info for assembly "
163 static cl::opt<ActionType>
164 Action(cl::desc("Action to perform:"),
165 cl::init(AC_Assemble),
166 cl::values(clEnumValN(AC_AsLex, "as-lex",
167 "Lex tokens from a .s file"),
168 clEnumValN(AC_Assemble, "assemble",
169 "Assemble a .s file (default)"),
170 clEnumValN(AC_Disassemble, "disassemble",
171 "Disassemble strings of hex bytes"),
172 clEnumValN(AC_EDisassemble, "edis",
173 "Enhanced disassembly of strings of hex bytes"),
176 static const Target *GetTarget(const char *ProgName) {
177 // Figure out the target triple.
178 if (TripleName.empty())
179 TripleName = sys::getDefaultTargetTriple();
180 Triple TheTriple(Triple::normalize(TripleName));
182 const Target *TheTarget = 0;
183 if (!ArchName.empty()) {
184 for (TargetRegistry::iterator it = TargetRegistry::begin(),
185 ie = TargetRegistry::end(); it != ie; ++it) {
186 if (ArchName == it->getName()) {
193 errs() << ProgName << ": error: invalid target '" << ArchName << "'.\n";
197 // Adjust the triple to match (if known), otherwise stick with the
198 // module/host triple.
199 Triple::ArchType Type = Triple::getArchTypeForLLVMName(ArchName);
200 if (Type != Triple::UnknownArch)
201 TheTriple.setArch(Type);
203 // Get the target specific parser.
205 TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), Error);
206 if (TheTarget == 0) {
207 errs() << ProgName << ": error: unable to get target for '"
208 << TheTriple.getTriple()
209 << "', see --version and --triple.\n";
214 TripleName = TheTriple.getTriple();
218 static tool_output_file *GetOutputStream() {
219 if (OutputFilename == "")
220 OutputFilename = "-";
223 tool_output_file *Out = new tool_output_file(OutputFilename.c_str(), Err,
224 raw_fd_ostream::F_Binary);
226 errs() << Err << '\n';
234 static std::string DwarfDebugFlags;
235 static void setDwarfDebugFlags(int argc, char **argv) {
236 if (!getenv("RC_DEBUG_OPTIONS"))
238 for (int i = 0; i < argc; i++) {
239 DwarfDebugFlags += argv[i];
241 DwarfDebugFlags += " ";
245 static int AsLexInput(const char *ProgName) {
246 OwningPtr<MemoryBuffer> BufferPtr;
247 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename, BufferPtr)) {
248 errs() << ProgName << ": " << ec.message() << '\n';
251 MemoryBuffer *Buffer = BufferPtr.take();
255 // Tell SrcMgr about this buffer, which is what TGParser will pick up.
256 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
258 // Record the location of the include directories so that the lexer can find
260 SrcMgr.setIncludeDirs(IncludeDirs);
262 const Target *TheTarget = GetTarget(ProgName);
266 llvm::OwningPtr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(TripleName));
267 assert(MAI && "Unable to create target asm info!");
269 AsmLexer Lexer(*MAI);
270 Lexer.setBuffer(SrcMgr.getMemoryBuffer(0));
272 OwningPtr<tool_output_file> Out(GetOutputStream());
277 while (Lexer.Lex().isNot(AsmToken::Eof)) {
278 AsmToken Tok = Lexer.getTok();
280 switch (Tok.getKind()) {
282 SrcMgr.PrintMessage(Lexer.getLoc(), SourceMgr::DK_Warning,
286 case AsmToken::Error:
287 Error = true; // error already printed.
289 case AsmToken::Identifier:
290 Out->os() << "identifier: " << Lexer.getTok().getString();
292 case AsmToken::Integer:
293 Out->os() << "int: " << Lexer.getTok().getString();
296 Out->os() << "real: " << Lexer.getTok().getString();
298 case AsmToken::Register:
299 Out->os() << "register: " << Lexer.getTok().getRegVal();
301 case AsmToken::String:
302 Out->os() << "string: " << Lexer.getTok().getString();
305 case AsmToken::Amp: Out->os() << "Amp"; break;
306 case AsmToken::AmpAmp: Out->os() << "AmpAmp"; break;
307 case AsmToken::At: Out->os() << "At"; break;
308 case AsmToken::Caret: Out->os() << "Caret"; break;
309 case AsmToken::Colon: Out->os() << "Colon"; break;
310 case AsmToken::Comma: Out->os() << "Comma"; break;
311 case AsmToken::Dollar: Out->os() << "Dollar"; break;
312 case AsmToken::Dot: Out->os() << "Dot"; break;
313 case AsmToken::EndOfStatement: Out->os() << "EndOfStatement"; break;
314 case AsmToken::Eof: Out->os() << "Eof"; break;
315 case AsmToken::Equal: Out->os() << "Equal"; break;
316 case AsmToken::EqualEqual: Out->os() << "EqualEqual"; break;
317 case AsmToken::Exclaim: Out->os() << "Exclaim"; break;
318 case AsmToken::ExclaimEqual: Out->os() << "ExclaimEqual"; break;
319 case AsmToken::Greater: Out->os() << "Greater"; break;
320 case AsmToken::GreaterEqual: Out->os() << "GreaterEqual"; break;
321 case AsmToken::GreaterGreater: Out->os() << "GreaterGreater"; break;
322 case AsmToken::Hash: Out->os() << "Hash"; break;
323 case AsmToken::LBrac: Out->os() << "LBrac"; break;
324 case AsmToken::LCurly: Out->os() << "LCurly"; break;
325 case AsmToken::LParen: Out->os() << "LParen"; break;
326 case AsmToken::Less: Out->os() << "Less"; break;
327 case AsmToken::LessEqual: Out->os() << "LessEqual"; break;
328 case AsmToken::LessGreater: Out->os() << "LessGreater"; break;
329 case AsmToken::LessLess: Out->os() << "LessLess"; break;
330 case AsmToken::Minus: Out->os() << "Minus"; break;
331 case AsmToken::Percent: Out->os() << "Percent"; break;
332 case AsmToken::Pipe: Out->os() << "Pipe"; break;
333 case AsmToken::PipePipe: Out->os() << "PipePipe"; break;
334 case AsmToken::Plus: Out->os() << "Plus"; break;
335 case AsmToken::RBrac: Out->os() << "RBrac"; break;
336 case AsmToken::RCurly: Out->os() << "RCurly"; break;
337 case AsmToken::RParen: Out->os() << "RParen"; break;
338 case AsmToken::Slash: Out->os() << "Slash"; break;
339 case AsmToken::Star: Out->os() << "Star"; break;
340 case AsmToken::Tilde: Out->os() << "Tilde"; break;
343 // Print the token string.
345 Out->os().write_escaped(Tok.getString());
346 Out->os() << "\")\n";
349 // Keep output if no errors.
350 if (Error == 0) Out->keep();
355 static int AssembleInput(const char *ProgName) {
356 const Target *TheTarget = GetTarget(ProgName);
360 OwningPtr<MemoryBuffer> BufferPtr;
361 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename, BufferPtr)) {
362 errs() << ProgName << ": " << ec.message() << '\n';
365 MemoryBuffer *Buffer = BufferPtr.take();
369 // Tell SrcMgr about this buffer, which is what the parser will pick up.
370 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
372 // Record the location of the include directories so that the lexer can find
374 SrcMgr.setIncludeDirs(IncludeDirs);
377 llvm::OwningPtr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(TripleName));
378 assert(MAI && "Unable to create target asm info!");
380 llvm::OwningPtr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
381 assert(MRI && "Unable to create target register info!");
383 // FIXME: This is not pretty. MCContext has a ptr to MCObjectFileInfo and
384 // MCObjectFileInfo needs a MCContext reference in order to initialize itself.
385 OwningPtr<MCObjectFileInfo> MOFI(new MCObjectFileInfo());
386 MCContext Ctx(*MAI, *MRI, MOFI.get(), &SrcMgr);
387 MOFI->InitMCObjectFileInfo(TripleName, RelocModel, CMModel, Ctx);
390 Ctx.setAllowTemporaryLabels(false);
392 Ctx.setGenDwarfForAssembly(GenDwarfForAssembly);
393 if (!DwarfDebugFlags.empty())
394 Ctx.setDwarfDebugFlags(StringRef(DwarfDebugFlags));
396 // Package up features to be passed to target/subtarget
397 std::string FeaturesStr;
399 SubtargetFeatures Features;
400 for (unsigned i = 0; i != MAttrs.size(); ++i)
401 Features.AddFeature(MAttrs[i]);
402 FeaturesStr = Features.getString();
405 OwningPtr<tool_output_file> Out(GetOutputStream());
409 formatted_raw_ostream FOS(Out->os());
410 OwningPtr<MCStreamer> Str;
412 OwningPtr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo());
413 OwningPtr<MCSubtargetInfo>
414 STI(TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr));
416 // FIXME: There is a bit of code duplication with addPassesToEmitFile.
417 if (FileType == OFT_AssemblyFile) {
419 TheTarget->createMCInstPrinter(OutputAsmVariant, *MAI, *MRI, *STI);
420 MCCodeEmitter *CE = 0;
421 MCAsmBackend *MAB = 0;
423 CE = TheTarget->createMCCodeEmitter(*MCII, *STI, Ctx);
424 MAB = TheTarget->createMCAsmBackend(TripleName);
426 Str.reset(TheTarget->createAsmStreamer(Ctx, FOS, /*asmverbose*/true,
429 /*useDwarfDirectory*/ true,
430 IP, CE, MAB, ShowInst));
432 } else if (FileType == OFT_Null) {
433 Str.reset(createNullStreamer(Ctx));
435 assert(FileType == OFT_ObjectFile && "Invalid file type!");
436 MCCodeEmitter *CE = TheTarget->createMCCodeEmitter(*MCII, *STI, Ctx);
437 MCAsmBackend *MAB = TheTarget->createMCAsmBackend(TripleName);
438 Str.reset(TheTarget->createMCObjectStreamer(TripleName, Ctx, *MAB,
443 OwningPtr<MCAsmParser> Parser(createMCAsmParser(SrcMgr, Ctx,
445 OwningPtr<MCTargetAsmParser> TAP(TheTarget->createMCAsmParser(*STI, *Parser));
448 << ": error: this target does not support assembly parsing.\n";
452 Parser->setShowParsedOperands(ShowInstOperands);
453 Parser->setTargetParser(*TAP.get());
455 int Res = Parser->Run(NoInitialTextSection);
457 // Keep output if no errors.
458 if (Res == 0) Out->keep();
463 static int DisassembleInput(const char *ProgName, bool Enhanced) {
464 const Target *TheTarget = GetTarget(ProgName);
468 OwningPtr<MemoryBuffer> Buffer;
469 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename, Buffer)) {
470 errs() << ProgName << ": " << ec.message() << '\n';
474 OwningPtr<tool_output_file> Out(GetOutputStream());
481 Disassembler::disassembleEnhanced(TripleName, *Buffer.take(), Out->os());
483 // Package up features to be passed to target/subtarget
484 std::string FeaturesStr;
486 SubtargetFeatures Features;
487 for (unsigned i = 0; i != MAttrs.size(); ++i)
488 Features.AddFeature(MAttrs[i]);
489 FeaturesStr = Features.getString();
492 Res = Disassembler::disassemble(*TheTarget, TripleName, MCPU, FeaturesStr,
493 *Buffer.take(), Out->os());
496 // Keep output if no errors.
497 if (Res == 0) Out->keep();
503 int main(int argc, char **argv) {
504 // Print a stack trace if we signal out.
505 sys::PrintStackTraceOnErrorSignal();
506 PrettyStackTraceProgram X(argc, argv);
507 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
509 // Initialize targets and assembly printers/parsers.
510 llvm::InitializeAllTargetInfos();
511 llvm::InitializeAllTargetMCs();
512 llvm::InitializeAllAsmParsers();
513 llvm::InitializeAllDisassemblers();
515 cl::ParseCommandLineOptions(argc, argv, "llvm machine code playground\n");
516 TripleName = Triple::normalize(TripleName);
517 setDwarfDebugFlags(argc, argv);
521 return AsLexInput(argv[0]);
523 return AssembleInput(argv[0]);
525 return DisassembleInput(argv[0], false);
526 case AC_EDisassemble:
527 return DisassembleInput(argv[0], true);