blob: 428125954901189559bd87f2aef9aa1d6fe4c974 [file] [log] [blame]
Chris Lattnerbb4688a2009-06-18 23:05:21 +00001//===-- llvm-mc.cpp - Machine Code Hacking Driver -------------------------===//
Chris Lattnerf9f065e2009-06-18 23:04:45 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnerbb4688a2009-06-18 23:05:21 +000010// This utility is a simple driver that allows command line hacking on machine
11// code.
Chris Lattnerf9f065e2009-06-18 23:04:45 +000012//
13//===----------------------------------------------------------------------===//
14
Daniel Dunbar9fbb37e2010-07-18 18:31:33 +000015#include "llvm/MC/MCParser/AsmLexer.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000016#include "llvm/MC/MCParser/MCAsmLexer.h"
Evan Cheng78c10ee2011-07-25 23:24:55 +000017#include "llvm/MC/MCAsmBackend.h"
Chris Lattnercbc23f72009-06-24 00:52:40 +000018#include "llvm/MC/MCContext.h"
Daniel Dunbar4a0abd82009-08-27 00:51:57 +000019#include "llvm/MC/MCCodeEmitter.h"
Chris Lattner90edac02009-09-14 03:02:37 +000020#include "llvm/MC/MCInstPrinter.h"
Evan Cheng59ee62d2011-07-11 03:57:24 +000021#include "llvm/MC/MCInstrInfo.h"
Evan Chenge76a33b2011-07-20 05:58:47 +000022#include "llvm/MC/MCObjectFileInfo.h"
23#include "llvm/MC/MCRegisterInfo.h"
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000024#include "llvm/MC/MCSectionMachO.h"
Chris Lattnercbc23f72009-06-24 00:52:40 +000025#include "llvm/MC/MCStreamer.h"
Evan Chengffc0e732011-07-09 05:47:46 +000026#include "llvm/MC/MCSubtargetInfo.h"
Evan Cheng94b95502011-07-26 00:24:13 +000027#include "llvm/MC/MCTargetAsmParser.h"
Evan Chengab8be962011-06-29 01:14:12 +000028#include "llvm/MC/SubtargetFeature.h"
Chris Lattnercbc23f72009-06-24 00:52:40 +000029#include "llvm/ADT/OwningPtr.h"
Chris Lattnerf9f065e2009-06-18 23:04:45 +000030#include "llvm/Support/CommandLine.h"
Dan Gohmand5826a32010-08-20 01:07:01 +000031#include "llvm/Support/FileUtilities.h"
Daniel Dunbarc22e0b22009-08-14 03:48:55 +000032#include "llvm/Support/FormattedStream.h"
Chris Lattnerf9f065e2009-06-18 23:04:45 +000033#include "llvm/Support/ManagedStatic.h"
34#include "llvm/Support/MemoryBuffer.h"
35#include "llvm/Support/PrettyStackTrace.h"
Chris Lattnerb23677e2009-06-21 05:22:37 +000036#include "llvm/Support/SourceMgr.h"
Dan Gohmane4f1a9b2010-10-07 20:32:40 +000037#include "llvm/Support/ToolOutputFile.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000038#include "llvm/Support/Host.h"
39#include "llvm/Support/Signals.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000040#include "llvm/Support/TargetRegistry.h"
41#include "llvm/Support/TargetSelect.h"
Michael J. Spencer333fb042010-12-09 17:36:48 +000042#include "llvm/Support/system_error.h"
Chris Lattnera3dcfb12009-12-22 22:50:29 +000043#include "Disassembler.h"
Chris Lattnerf9f065e2009-06-18 23:04:45 +000044using namespace llvm;
45
46static cl::opt<std::string>
47InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
48
49static cl::opt<std::string>
50OutputFilename("o", cl::desc("Output filename"),
51 cl::value_desc("filename"));
52
Daniel Dunbarf2f6b0c2009-08-27 07:56:39 +000053static cl::opt<bool>
54ShowEncoding("show-encoding", cl::desc("Show instruction encodings"));
55
Daniel Dunbar9dee8e32010-02-03 18:18:30 +000056static cl::opt<bool>
57ShowInst("show-inst", cl::desc("Show internal instruction representation"));
58
Daniel Dunbar3c14ca42010-08-11 06:37:09 +000059static cl::opt<bool>
60ShowInstOperands("show-inst-operands",
61 cl::desc("Show instructions operands as parsed"));
62
Chris Lattnere895c612009-09-20 07:17:49 +000063static cl::opt<unsigned>
64OutputAsmVariant("output-asm-variant",
65 cl::desc("Syntax variant to use for output printing"));
66
Daniel Dunbarac2884a2010-03-25 22:49:09 +000067static cl::opt<bool>
68RelaxAll("mc-relax-all", cl::desc("Relax all fixups"));
69
Daniel Dunbarfdb5a862010-05-23 17:44:06 +000070static cl::opt<bool>
Rafael Espindola96aa78c2011-01-23 17:55:27 +000071NoExecStack("mc-no-exec-stack", cl::desc("File doesn't need an exec stack"));
72
73static cl::opt<bool>
Daniel Dunbarfdb5a862010-05-23 17:44:06 +000074EnableLogging("enable-api-logging", cl::desc("Enable MC API logging"));
75
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000076enum OutputFileType {
Daniel Dunbar2d9f5d12010-03-23 23:47:12 +000077 OFT_Null,
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000078 OFT_AssemblyFile,
79 OFT_ObjectFile
80};
81static cl::opt<OutputFileType>
82FileType("filetype", cl::init(OFT_AssemblyFile),
83 cl::desc("Choose an output file type:"),
84 cl::values(
85 clEnumValN(OFT_AssemblyFile, "asm",
86 "Emit an assembly ('.s') file"),
Daniel Dunbar2d9f5d12010-03-23 23:47:12 +000087 clEnumValN(OFT_Null, "null",
88 "Don't emit anything (for timing purposes)"),
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000089 clEnumValN(OFT_ObjectFile, "obj",
90 "Emit a native object ('.o') file"),
91 clEnumValEnd));
92
Chris Lattnerb23677e2009-06-21 05:22:37 +000093static cl::list<std::string>
94IncludeDirs("I", cl::desc("Directory of include files"),
95 cl::value_desc("directory"), cl::Prefix);
Chris Lattnerf9f065e2009-06-18 23:04:45 +000096
Daniel Dunbarb4b53e52009-07-17 22:38:58 +000097static cl::opt<std::string>
Daniel Dunbar181ab6a2010-03-13 02:20:38 +000098ArchName("arch", cl::desc("Target arch to assemble for, "
Bill Wendling916a94b2011-06-17 20:35:21 +000099 "see -version for available targets"));
Daniel Dunbar181ab6a2010-03-13 02:20:38 +0000100
101static cl::opt<std::string>
Nick Lewyckyed1f1682009-11-01 22:07:54 +0000102TripleName("triple", cl::desc("Target triple to assemble for, "
Daniel Dunbar181ab6a2010-03-13 02:20:38 +0000103 "see -version for available targets"));
Daniel Dunbarb4b53e52009-07-17 22:38:58 +0000104
Jim Grosbachd2f14252010-10-30 15:57:50 +0000105static cl::opt<std::string>
106MCPU("mcpu",
107 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
108 cl::value_desc("cpu-name"),
109 cl::init(""));
110
James Molloyb9505852011-09-07 17:24:38 +0000111static cl::list<std::string>
112MAttrs("mattr",
113 cl::CommaSeparated,
114 cl::desc("Target specific attributes (-mattr=help for details)"),
115 cl::value_desc("a1,+a2,-a3,..."));
116
Evan Cheng43966132011-07-19 06:37:02 +0000117static cl::opt<Reloc::Model>
118RelocModel("relocation-model",
119 cl::desc("Choose relocation model"),
120 cl::init(Reloc::Default),
121 cl::values(
122 clEnumValN(Reloc::Default, "default",
123 "Target default relocation model"),
124 clEnumValN(Reloc::Static, "static",
125 "Non-relocatable code"),
126 clEnumValN(Reloc::PIC_, "pic",
127 "Fully relocatable, position independent code"),
128 clEnumValN(Reloc::DynamicNoPIC, "dynamic-no-pic",
129 "Relocatable external references, non-relocatable code"),
130 clEnumValEnd));
131
Evan Cheng34ad6db2011-07-20 07:51:56 +0000132static cl::opt<llvm::CodeModel::Model>
133CMModel("code-model",
134 cl::desc("Choose code model"),
135 cl::init(CodeModel::Default),
136 cl::values(clEnumValN(CodeModel::Default, "default",
137 "Target default code model"),
138 clEnumValN(CodeModel::Small, "small",
139 "Small code model"),
140 clEnumValN(CodeModel::Kernel, "kernel",
141 "Kernel code model"),
142 clEnumValN(CodeModel::Medium, "medium",
143 "Medium code model"),
144 clEnumValN(CodeModel::Large, "large",
145 "Large code model"),
146 clEnumValEnd));
147
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000148static cl::opt<bool>
Bill Wendling916a94b2011-06-17 20:35:21 +0000149NoInitialTextSection("n", cl::desc("Don't assume assembly file starts "
150 "in the text section"));
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000151
Daniel Dunbarc6cf43d2011-03-28 22:49:15 +0000152static cl::opt<bool>
Bill Wendling916a94b2011-06-17 20:35:21 +0000153SaveTempLabels("L", cl::desc("Don't discard temporary labels"));
Daniel Dunbarc6cf43d2011-03-28 22:49:15 +0000154
Kevin Enderby613b7572011-11-01 22:27:22 +0000155static cl::opt<bool>
156GenDwarfForAssembly("g", cl::desc("Generate dwarf debugging info for assembly "
157 "source files"));
158
Chris Lattnerb23677e2009-06-21 05:22:37 +0000159enum ActionType {
Chris Lattner27aa7d22009-06-21 20:16:42 +0000160 AC_AsLex,
Sean Callananba847da2009-12-17 01:49:59 +0000161 AC_Assemble,
Sean Callanan668b1542010-04-12 19:43:00 +0000162 AC_Disassemble,
163 AC_EDisassemble
Chris Lattnerb23677e2009-06-21 05:22:37 +0000164};
Chris Lattnerf9f065e2009-06-18 23:04:45 +0000165
Chris Lattnerb23677e2009-06-21 05:22:37 +0000166static cl::opt<ActionType>
167Action(cl::desc("Action to perform:"),
Chris Lattner27aa7d22009-06-21 20:16:42 +0000168 cl::init(AC_Assemble),
169 cl::values(clEnumValN(AC_AsLex, "as-lex",
170 "Lex tokens from a .s file"),
171 clEnumValN(AC_Assemble, "assemble",
Chris Lattnerb23677e2009-06-21 05:22:37 +0000172 "Assemble a .s file (default)"),
Sean Callananba847da2009-12-17 01:49:59 +0000173 clEnumValN(AC_Disassemble, "disassemble",
174 "Disassemble strings of hex bytes"),
Sean Callanan668b1542010-04-12 19:43:00 +0000175 clEnumValN(AC_EDisassemble, "edis",
176 "Enhanced disassembly of strings of hex bytes"),
Chris Lattnerb23677e2009-06-21 05:22:37 +0000177 clEnumValEnd));
178
Kevin Enderby9823ca92009-09-04 21:45:34 +0000179static const Target *GetTarget(const char *ProgName) {
Daniel Dunbar181ab6a2010-03-13 02:20:38 +0000180 // Figure out the target triple.
181 if (TripleName.empty())
Sebastian Pop01738642011-11-01 21:32:20 +0000182 TripleName = sys::getDefaultTargetTriple();
Evan Chengd6dcf392011-07-26 19:02:16 +0000183 Triple TheTriple(Triple::normalize(TripleName));
184
185 const Target *TheTarget = 0;
Daniel Dunbar181ab6a2010-03-13 02:20:38 +0000186 if (!ArchName.empty()) {
Evan Chengd6dcf392011-07-26 19:02:16 +0000187 for (TargetRegistry::iterator it = TargetRegistry::begin(),
188 ie = TargetRegistry::end(); it != ie; ++it) {
189 if (ArchName == it->getName()) {
190 TheTarget = &*it;
191 break;
192 }
193 }
194
195 if (!TheTarget) {
196 errs() << ProgName << ": error: invalid target '" << ArchName << "'.\n";
197 return 0;
198 }
199
200 // Adjust the triple to match (if known), otherwise stick with the
201 // module/host triple.
202 Triple::ArchType Type = Triple::getArchTypeForLLVMName(ArchName);
203 if (Type != Triple::UnknownArch)
204 TheTriple.setArch(Type);
205 } else {
206 // Get the target specific parser.
207 std::string Error;
208 TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), Error);
209 if (TheTarget == 0) {
210 errs() << ProgName << ": error: unable to get target for '"
211 << TheTriple.getTriple()
212 << "', see --version and --triple.\n";
213 return 0;
214 }
Daniel Dunbar181ab6a2010-03-13 02:20:38 +0000215 }
216
Evan Chengd6dcf392011-07-26 19:02:16 +0000217 TripleName = TheTriple.getTriple();
218 return TheTarget;
Kevin Enderby9823ca92009-09-04 21:45:34 +0000219}
220
Dan Gohmand4c45432010-09-01 14:20:41 +0000221static tool_output_file *GetOutputStream() {
Dan Gohmand5826a32010-08-20 01:07:01 +0000222 if (OutputFilename == "")
223 OutputFilename = "-";
224
225 std::string Err;
226 tool_output_file *Out = new tool_output_file(OutputFilename.c_str(), Err,
227 raw_fd_ostream::F_Binary);
228 if (!Err.empty()) {
229 errs() << Err << '\n';
230 delete Out;
231 return 0;
232 }
Dan Gohmand4c45432010-09-01 14:20:41 +0000233
234 return Out;
Dan Gohmand5826a32010-08-20 01:07:01 +0000235}
236
Kevin Enderby94c2e852011-12-09 18:09:40 +0000237static std::string DwarfDebugFlags;
238static void setDwarfDebugFlags(int argc, char **argv) {
239 if (!getenv("RC_DEBUG_OPTIONS"))
240 return;
241 for (int i = 0; i < argc; i++) {
242 DwarfDebugFlags += argv[i];
243 if (i + 1 < argc)
244 DwarfDebugFlags += " ";
245 }
246}
247
Chris Lattner27aa7d22009-06-21 20:16:42 +0000248static int AsLexInput(const char *ProgName) {
Michael J. Spencer3ff95632010-12-16 03:29:14 +0000249 OwningPtr<MemoryBuffer> BufferPtr;
250 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename, BufferPtr)) {
Michael J. Spencer333fb042010-12-09 17:36:48 +0000251 errs() << ProgName << ": " << ec.message() << '\n';
Chris Lattnerf9f065e2009-06-18 23:04:45 +0000252 return 1;
253 }
Michael J. Spencer3ff95632010-12-16 03:29:14 +0000254 MemoryBuffer *Buffer = BufferPtr.take();
Chris Lattnerb23677e2009-06-21 05:22:37 +0000255
256 SourceMgr SrcMgr;
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000257
Chris Lattnerb23677e2009-06-21 05:22:37 +0000258 // Tell SrcMgr about this buffer, which is what TGParser will pick up.
259 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000260
Chris Lattnerb23677e2009-06-21 05:22:37 +0000261 // Record the location of the include directories so that the lexer can find
262 // it later.
263 SrcMgr.setIncludeDirs(IncludeDirs);
Chris Lattnera59e8772009-06-21 07:19:10 +0000264
Kevin Enderby9823ca92009-09-04 21:45:34 +0000265 const Target *TheTarget = GetTarget(ProgName);
266 if (!TheTarget)
267 return 1;
268
Evan Cheng1abf2cb2011-07-14 23:50:31 +0000269 llvm::OwningPtr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(TripleName));
Kevin Enderby9823ca92009-09-04 21:45:34 +0000270 assert(MAI && "Unable to create target asm info!");
271
Sean Callananfd0b0282010-01-21 00:19:58 +0000272 AsmLexer Lexer(*MAI);
Daniel Dunbar346cc612010-07-18 18:31:28 +0000273 Lexer.setBuffer(SrcMgr.getMemoryBuffer(0));
274
Dan Gohmand4c45432010-09-01 14:20:41 +0000275 OwningPtr<tool_output_file> Out(GetOutputStream());
Dan Gohmand5826a32010-08-20 01:07:01 +0000276 if (!Out)
277 return 1;
278
Chris Lattner27aa7d22009-06-21 20:16:42 +0000279 bool Error = false;
Daniel Dunbara3c924f2009-07-28 16:56:42 +0000280 while (Lexer.Lex().isNot(AsmToken::Eof)) {
Daniel Dunbar54b63692010-10-25 20:18:46 +0000281 AsmToken Tok = Lexer.getTok();
282
283 switch (Tok.getKind()) {
Chris Lattner27aa7d22009-06-21 20:16:42 +0000284 default:
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000285 SrcMgr.PrintMessage(Lexer.getLoc(), SourceMgr::DK_Warning,
286 "unknown token");
Chris Lattner27aa7d22009-06-21 20:16:42 +0000287 Error = true;
288 break;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000289 case AsmToken::Error:
Chris Lattner27aa7d22009-06-21 20:16:42 +0000290 Error = true; // error already printed.
Chris Lattner4651bca2009-06-21 19:21:25 +0000291 break;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000292 case AsmToken::Identifier:
Daniel Dunbar54b63692010-10-25 20:18:46 +0000293 Out->os() << "identifier: " << Lexer.getTok().getString();
Chris Lattnera59e8772009-06-21 07:19:10 +0000294 break;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000295 case AsmToken::Integer:
Daniel Dunbar54b63692010-10-25 20:18:46 +0000296 Out->os() << "int: " << Lexer.getTok().getString();
Chris Lattnera59e8772009-06-21 07:19:10 +0000297 break;
Daniel Dunbar54f0a622010-09-24 01:59:31 +0000298 case AsmToken::Real:
Daniel Dunbar54b63692010-10-25 20:18:46 +0000299 Out->os() << "real: " << Lexer.getTok().getString();
Daniel Dunbar54f0a622010-09-24 01:59:31 +0000300 break;
Daniel Dunbarf9698682010-09-16 00:42:35 +0000301 case AsmToken::Register:
Daniel Dunbar54b63692010-10-25 20:18:46 +0000302 Out->os() << "register: " << Lexer.getTok().getRegVal();
Daniel Dunbarf9698682010-09-16 00:42:35 +0000303 break;
304 case AsmToken::String:
Daniel Dunbar54b63692010-10-25 20:18:46 +0000305 Out->os() << "string: " << Lexer.getTok().getString();
Daniel Dunbarf9698682010-09-16 00:42:35 +0000306 break;
Daniel Dunbar165e8342009-07-01 06:56:54 +0000307
Daniel Dunbar54b63692010-10-25 20:18:46 +0000308 case AsmToken::Amp: Out->os() << "Amp"; break;
309 case AsmToken::AmpAmp: Out->os() << "AmpAmp"; break;
310 case AsmToken::At: Out->os() << "At"; break;
311 case AsmToken::Caret: Out->os() << "Caret"; break;
312 case AsmToken::Colon: Out->os() << "Colon"; break;
313 case AsmToken::Comma: Out->os() << "Comma"; break;
314 case AsmToken::Dollar: Out->os() << "Dollar"; break;
315 case AsmToken::Dot: Out->os() << "Dot"; break;
316 case AsmToken::EndOfStatement: Out->os() << "EndOfStatement"; break;
317 case AsmToken::Eof: Out->os() << "Eof"; break;
318 case AsmToken::Equal: Out->os() << "Equal"; break;
319 case AsmToken::EqualEqual: Out->os() << "EqualEqual"; break;
320 case AsmToken::Exclaim: Out->os() << "Exclaim"; break;
321 case AsmToken::ExclaimEqual: Out->os() << "ExclaimEqual"; break;
322 case AsmToken::Greater: Out->os() << "Greater"; break;
323 case AsmToken::GreaterEqual: Out->os() << "GreaterEqual"; break;
324 case AsmToken::GreaterGreater: Out->os() << "GreaterGreater"; break;
325 case AsmToken::Hash: Out->os() << "Hash"; break;
326 case AsmToken::LBrac: Out->os() << "LBrac"; break;
327 case AsmToken::LCurly: Out->os() << "LCurly"; break;
328 case AsmToken::LParen: Out->os() << "LParen"; break;
329 case AsmToken::Less: Out->os() << "Less"; break;
330 case AsmToken::LessEqual: Out->os() << "LessEqual"; break;
331 case AsmToken::LessGreater: Out->os() << "LessGreater"; break;
332 case AsmToken::LessLess: Out->os() << "LessLess"; break;
333 case AsmToken::Minus: Out->os() << "Minus"; break;
334 case AsmToken::Percent: Out->os() << "Percent"; break;
335 case AsmToken::Pipe: Out->os() << "Pipe"; break;
336 case AsmToken::PipePipe: Out->os() << "PipePipe"; break;
337 case AsmToken::Plus: Out->os() << "Plus"; break;
338 case AsmToken::RBrac: Out->os() << "RBrac"; break;
339 case AsmToken::RCurly: Out->os() << "RCurly"; break;
340 case AsmToken::RParen: Out->os() << "RParen"; break;
341 case AsmToken::Slash: Out->os() << "Slash"; break;
342 case AsmToken::Star: Out->os() << "Star"; break;
343 case AsmToken::Tilde: Out->os() << "Tilde"; break;
Chris Lattnera59e8772009-06-21 07:19:10 +0000344 }
Daniel Dunbar54b63692010-10-25 20:18:46 +0000345
346 // Print the token string.
347 Out->os() << " (\"";
348 Out->os().write_escaped(Tok.getString());
349 Out->os() << "\")\n";
Chris Lattnera59e8772009-06-21 07:19:10 +0000350 }
Dan Gohmand5826a32010-08-20 01:07:01 +0000351
352 // Keep output if no errors.
353 if (Error == 0) Out->keep();
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000354
Chris Lattner27aa7d22009-06-21 20:16:42 +0000355 return Error;
Chris Lattnerb23677e2009-06-21 05:22:37 +0000356}
357
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000358static int AssembleInput(const char *ProgName) {
Daniel Dunbarc22e0b22009-08-14 03:48:55 +0000359 const Target *TheTarget = GetTarget(ProgName);
360 if (!TheTarget)
361 return 1;
362
Michael J. Spencer3ff95632010-12-16 03:29:14 +0000363 OwningPtr<MemoryBuffer> BufferPtr;
364 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename, BufferPtr)) {
Michael J. Spencer333fb042010-12-09 17:36:48 +0000365 errs() << ProgName << ": " << ec.message() << '\n';
Chris Lattner27aa7d22009-06-21 20:16:42 +0000366 return 1;
367 }
Michael J. Spencer3ff95632010-12-16 03:29:14 +0000368 MemoryBuffer *Buffer = BufferPtr.take();
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000369
Chris Lattner27aa7d22009-06-21 20:16:42 +0000370 SourceMgr SrcMgr;
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000371
Daniel Dunbar12a8a442009-08-19 16:25:53 +0000372 // Tell SrcMgr about this buffer, which is what the parser will pick up.
Chris Lattner27aa7d22009-06-21 20:16:42 +0000373 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000374
Chris Lattner27aa7d22009-06-21 20:16:42 +0000375 // Record the location of the include directories so that the lexer can find
376 // it later.
377 SrcMgr.setIncludeDirs(IncludeDirs);
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000378
379
Evan Cheng1abf2cb2011-07-14 23:50:31 +0000380 llvm::OwningPtr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(TripleName));
Chris Lattnerc18409a2010-03-11 22:53:35 +0000381 assert(MAI && "Unable to create target asm info!");
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000382
Evan Cheng0e6a0522011-07-18 20:57:22 +0000383 llvm::OwningPtr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
384 assert(MRI && "Unable to create target register info!");
385
Evan Chenge76a33b2011-07-20 05:58:47 +0000386 // FIXME: This is not pretty. MCContext has a ptr to MCObjectFileInfo and
387 // MCObjectFileInfo needs a MCContext reference in order to initialize itself.
388 OwningPtr<MCObjectFileInfo> MOFI(new MCObjectFileInfo());
Evan Cheng203576a2011-07-20 19:50:42 +0000389 MCContext Ctx(*MAI, *MRI, MOFI.get());
390 MOFI->InitMCObjectFileInfo(TripleName, RelocModel, CMModel, Ctx);
Evan Chenge76a33b2011-07-20 05:58:47 +0000391
Daniel Dunbarc6cf43d2011-03-28 22:49:15 +0000392 if (SaveTempLabels)
393 Ctx.setAllowTemporaryLabels(false);
Rafael Espindola89b93722010-12-10 07:39:47 +0000394
Kevin Enderby613b7572011-11-01 22:27:22 +0000395 Ctx.setGenDwarfForAssembly(GenDwarfForAssembly);
Kevin Enderby94c2e852011-12-09 18:09:40 +0000396 if (!DwarfDebugFlags.empty())
397 Ctx.setDwarfDebugFlags(StringRef(DwarfDebugFlags));
Kevin Enderby613b7572011-11-01 22:27:22 +0000398
James Molloyb9505852011-09-07 17:24:38 +0000399 // Package up features to be passed to target/subtarget
400 std::string FeaturesStr;
401 if (MAttrs.size()) {
402 SubtargetFeatures Features;
403 for (unsigned i = 0; i != MAttrs.size(); ++i)
404 Features.AddFeature(MAttrs[i]);
405 FeaturesStr = Features.getString();
406 }
407
Dan Gohmand4c45432010-09-01 14:20:41 +0000408 OwningPtr<tool_output_file> Out(GetOutputStream());
Dan Gohmand5826a32010-08-20 01:07:01 +0000409 if (!Out)
410 return 1;
411
Dan Gohmand4c45432010-09-01 14:20:41 +0000412 formatted_raw_ostream FOS(Out->os());
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000413 OwningPtr<MCStreamer> Str;
Chris Lattnerf3ce0092009-08-17 04:23:44 +0000414
Evan Cheng59ee62d2011-07-11 03:57:24 +0000415 OwningPtr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo());
Evan Chengffc0e732011-07-09 05:47:46 +0000416 OwningPtr<MCSubtargetInfo>
417 STI(TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr));
418
Rafael Espindola96aa78c2011-01-23 17:55:27 +0000419 // FIXME: There is a bit of code duplication with addPassesToEmitFile.
Kevin Enderby9823ca92009-09-04 21:45:34 +0000420 if (FileType == OFT_AssemblyFile) {
Chris Lattner4c42a6d2010-03-19 05:48:53 +0000421 MCInstPrinter *IP =
James Molloyb9505852011-09-07 17:24:38 +0000422 TheTarget->createMCInstPrinter(OutputAsmVariant, *MAI, *STI);
Benjamin Kramer1abcd062010-07-29 17:48:06 +0000423 MCCodeEmitter *CE = 0;
Evan Cheng78c10ee2011-07-25 23:24:55 +0000424 MCAsmBackend *MAB = 0;
Daniel Dunbar745dacc2010-12-16 03:05:59 +0000425 if (ShowEncoding) {
Evan Cheng28c85a82011-07-26 00:42:34 +0000426 CE = TheTarget->createMCCodeEmitter(*MCII, *STI, Ctx);
Evan Cheng78c10ee2011-07-25 23:24:55 +0000427 MAB = TheTarget->createMCAsmBackend(TripleName);
Daniel Dunbar745dacc2010-12-16 03:05:59 +0000428 }
Rafael Espindola89b93722010-12-10 07:39:47 +0000429 Str.reset(TheTarget->createAsmStreamer(Ctx, FOS, /*asmverbose*/true,
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000430 /*useLoc*/ true,
Nick Lewycky44d798d2011-10-17 23:05:28 +0000431 /*useCFI*/ true,
432 /*useDwarfDirectory*/ true,
433 IP, CE, MAB, ShowInst));
Jim Grosbach57920512011-12-05 23:20:14 +0000434
Daniel Dunbar2d9f5d12010-03-23 23:47:12 +0000435 } else if (FileType == OFT_Null) {
436 Str.reset(createNullStreamer(Ctx));
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000437 } else {
438 assert(FileType == OFT_ObjectFile && "Invalid file type!");
Evan Cheng28c85a82011-07-26 00:42:34 +0000439 MCCodeEmitter *CE = TheTarget->createMCCodeEmitter(*MCII, *STI, Ctx);
Evan Cheng78c10ee2011-07-25 23:24:55 +0000440 MCAsmBackend *MAB = TheTarget->createMCAsmBackend(TripleName);
Evan Cheng28c85a82011-07-26 00:42:34 +0000441 Str.reset(TheTarget->createMCObjectStreamer(TripleName, Ctx, *MAB,
442 FOS, CE, RelaxAll,
443 NoExecStack));
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000444 }
Daniel Dunbara0d14262009-06-24 23:30:00 +0000445
Daniel Dunbarfdb5a862010-05-23 17:44:06 +0000446 if (EnableLogging) {
447 Str.reset(createLoggingStreamer(Str.take(), errs()));
448 }
449
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000450 OwningPtr<MCAsmParser> Parser(createMCAsmParser(SrcMgr, Ctx,
451 *Str.get(), *MAI));
Evan Cheng94b95502011-07-26 00:24:13 +0000452 OwningPtr<MCTargetAsmParser> TAP(TheTarget->createMCAsmParser(*STI, *Parser));
Daniel Dunbarc22e0b22009-08-14 03:48:55 +0000453 if (!TAP) {
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000454 errs() << ProgName
Daniel Dunbarc22e0b22009-08-14 03:48:55 +0000455 << ": error: this target does not support assembly parsing.\n";
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000456 return 1;
Daniel Dunbarc22e0b22009-08-14 03:48:55 +0000457 }
458
Daniel Dunbar3c14ca42010-08-11 06:37:09 +0000459 Parser->setShowParsedOperands(ShowInstOperands);
Daniel Dunbar9fbb37e2010-07-18 18:31:33 +0000460 Parser->setTargetParser(*TAP.get());
Daniel Dunbarbfc0f342009-08-11 04:34:48 +0000461
Daniel Dunbar9fbb37e2010-07-18 18:31:33 +0000462 int Res = Parser->Run(NoInitialTextSection);
Daniel Dunbarbfc0f342009-08-11 04:34:48 +0000463
Dan Gohmand5826a32010-08-20 01:07:01 +0000464 // Keep output if no errors.
465 if (Res == 0) Out->keep();
Daniel Dunbar4ca90652010-03-13 19:31:47 +0000466
Daniel Dunbarbfc0f342009-08-11 04:34:48 +0000467 return Res;
Sean Callananba847da2009-12-17 01:49:59 +0000468}
469
Sean Callanan668b1542010-04-12 19:43:00 +0000470static int DisassembleInput(const char *ProgName, bool Enhanced) {
Daniel Dunbar4d4358e2010-03-19 18:07:50 +0000471 const Target *TheTarget = GetTarget(ProgName);
472 if (!TheTarget)
Sean Callananba847da2009-12-17 01:49:59 +0000473 return 0;
Sean Callananba847da2009-12-17 01:49:59 +0000474
Michael J. Spencer3ff95632010-12-16 03:29:14 +0000475 OwningPtr<MemoryBuffer> Buffer;
476 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename, Buffer)) {
Michael J. Spencer333fb042010-12-09 17:36:48 +0000477 errs() << ProgName << ": " << ec.message() << '\n';
Sean Callananba847da2009-12-17 01:49:59 +0000478 return 1;
479 }
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000480
Dan Gohmand4c45432010-09-01 14:20:41 +0000481 OwningPtr<tool_output_file> Out(GetOutputStream());
Dan Gohmand5826a32010-08-20 01:07:01 +0000482 if (!Out)
483 return 1;
484
485 int Res;
Bill Wendlinga5c177e2011-03-21 04:13:46 +0000486 if (Enhanced) {
Michael J. Spencer3ff95632010-12-16 03:29:14 +0000487 Res =
488 Disassembler::disassembleEnhanced(TripleName, *Buffer.take(), Out->os());
Bill Wendlinga5c177e2011-03-21 04:13:46 +0000489 } else {
James Molloyb9505852011-09-07 17:24:38 +0000490 // Package up features to be passed to target/subtarget
491 std::string FeaturesStr;
492 if (MAttrs.size()) {
493 SubtargetFeatures Features;
494 for (unsigned i = 0; i != MAttrs.size(); ++i)
495 Features.AddFeature(MAttrs[i]);
496 FeaturesStr = Features.getString();
497 }
498
499 Res = Disassembler::disassemble(*TheTarget, TripleName, MCPU, FeaturesStr,
Michael J. Spencer3ff95632010-12-16 03:29:14 +0000500 *Buffer.take(), Out->os());
Bill Wendlinga5c177e2011-03-21 04:13:46 +0000501 }
Dan Gohmand5826a32010-08-20 01:07:01 +0000502
503 // Keep output if no errors.
504 if (Res == 0) Out->keep();
505
506 return Res;
Sean Callananba847da2009-12-17 01:49:59 +0000507}
Chris Lattner27aa7d22009-06-21 20:16:42 +0000508
Chris Lattnerb23677e2009-06-21 05:22:37 +0000509
510int main(int argc, char **argv) {
511 // Print a stack trace if we signal out.
512 sys::PrintStackTraceOnErrorSignal();
513 PrettyStackTraceProgram X(argc, argv);
514 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Daniel Dunbarb4b53e52009-07-17 22:38:58 +0000515
Daniel Dunbarc22e0b22009-08-14 03:48:55 +0000516 // Initialize targets and assembly printers/parsers.
Daniel Dunbarb4b53e52009-07-17 22:38:58 +0000517 llvm::InitializeAllTargetInfos();
Evan Chenge78085a2011-07-22 21:58:54 +0000518 llvm::InitializeAllTargetMCs();
Daniel Dunbarb4b53e52009-07-17 22:38:58 +0000519 llvm::InitializeAllAsmParsers();
Sean Callananba847da2009-12-17 01:49:59 +0000520 llvm::InitializeAllDisassemblers();
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000521
Chris Lattnerb23677e2009-06-21 05:22:37 +0000522 cl::ParseCommandLineOptions(argc, argv, "llvm machine code playground\n");
Duncan Sands75ebbce2010-08-28 01:30:02 +0000523 TripleName = Triple::normalize(TripleName);
Kevin Enderby94c2e852011-12-09 18:09:40 +0000524 setDwarfDebugFlags(argc, argv);
Chris Lattnerb23677e2009-06-21 05:22:37 +0000525
526 switch (Action) {
527 default:
Chris Lattner27aa7d22009-06-21 20:16:42 +0000528 case AC_AsLex:
529 return AsLexInput(argv[0]);
Chris Lattnerb23677e2009-06-21 05:22:37 +0000530 case AC_Assemble:
531 return AssembleInput(argv[0]);
Sean Callananba847da2009-12-17 01:49:59 +0000532 case AC_Disassemble:
Sean Callanan668b1542010-04-12 19:43:00 +0000533 return DisassembleInput(argv[0], false);
534 case AC_EDisassemble:
535 return DisassembleInput(argv[0], true);
Chris Lattnerb23677e2009-06-21 05:22:37 +0000536 }
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000537
Chris Lattnerf9f065e2009-06-18 23:04:45 +0000538 return 0;
539}