blob: 935a770365dbb55ed70fe28ad4e7ba3392db609a [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
Chandler Carruthf010c462012-12-04 10:44:52 +000015#include "Disassembler.h"
16#include "llvm/ADT/OwningPtr.h"
Evan Cheng78c10ee2011-07-25 23:24:55 +000017#include "llvm/MC/MCAsmBackend.h"
Craig Topperfb22ede2012-04-15 22:00:22 +000018#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbar4a0abd82009-08-27 00:51:57 +000019#include "llvm/MC/MCCodeEmitter.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000020#include "llvm/MC/MCContext.h"
Chris Lattner90edac02009-09-14 03:02:37 +000021#include "llvm/MC/MCInstPrinter.h"
Evan Cheng59ee62d2011-07-11 03:57:24 +000022#include "llvm/MC/MCInstrInfo.h"
Evan Chenge76a33b2011-07-20 05:58:47 +000023#include "llvm/MC/MCObjectFileInfo.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000024#include "llvm/MC/MCParser/AsmLexer.h"
25#include "llvm/MC/MCParser/MCAsmLexer.h"
Evan Chenge76a33b2011-07-20 05:58:47 +000026#include "llvm/MC/MCRegisterInfo.h"
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000027#include "llvm/MC/MCSectionMachO.h"
Chris Lattnercbc23f72009-06-24 00:52:40 +000028#include "llvm/MC/MCStreamer.h"
Evan Chengffc0e732011-07-09 05:47:46 +000029#include "llvm/MC/MCSubtargetInfo.h"
Evan Cheng94b95502011-07-26 00:24:13 +000030#include "llvm/MC/MCTargetAsmParser.h"
Evan Chengab8be962011-06-29 01:14:12 +000031#include "llvm/MC/SubtargetFeature.h"
Chris Lattnerf9f065e2009-06-18 23:04:45 +000032#include "llvm/Support/CommandLine.h"
Dan Gohmand5826a32010-08-20 01:07:01 +000033#include "llvm/Support/FileUtilities.h"
Daniel Dunbarc22e0b22009-08-14 03:48:55 +000034#include "llvm/Support/FormattedStream.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000035#include "llvm/Support/Host.h"
Chris Lattnerf9f065e2009-06-18 23:04:45 +000036#include "llvm/Support/ManagedStatic.h"
37#include "llvm/Support/MemoryBuffer.h"
38#include "llvm/Support/PrettyStackTrace.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000039#include "llvm/Support/Signals.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000040#include "llvm/Support/SourceMgr.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000041#include "llvm/Support/TargetRegistry.h"
42#include "llvm/Support/TargetSelect.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000043#include "llvm/Support/ToolOutputFile.h"
Michael J. Spencer333fb042010-12-09 17:36:48 +000044#include "llvm/Support/system_error.h"
Chris Lattnerf9f065e2009-06-18 23:04:45 +000045using namespace llvm;
46
47static cl::opt<std::string>
48InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
49
50static cl::opt<std::string>
51OutputFilename("o", cl::desc("Output filename"),
52 cl::value_desc("filename"));
53
Daniel Dunbarf2f6b0c2009-08-27 07:56:39 +000054static cl::opt<bool>
55ShowEncoding("show-encoding", cl::desc("Show instruction encodings"));
56
Daniel Dunbar9dee8e32010-02-03 18:18:30 +000057static cl::opt<bool>
58ShowInst("show-inst", cl::desc("Show internal instruction representation"));
59
Daniel Dunbar3c14ca42010-08-11 06:37:09 +000060static cl::opt<bool>
61ShowInstOperands("show-inst-operands",
62 cl::desc("Show instructions operands as parsed"));
63
Chris Lattnere895c612009-09-20 07:17:49 +000064static cl::opt<unsigned>
65OutputAsmVariant("output-asm-variant",
66 cl::desc("Syntax variant to use for output printing"));
67
Daniel Dunbarac2884a2010-03-25 22:49:09 +000068static cl::opt<bool>
69RelaxAll("mc-relax-all", cl::desc("Relax all fixups"));
70
Daniel Dunbarfdb5a862010-05-23 17:44:06 +000071static cl::opt<bool>
Rafael Espindola985884c2012-11-23 17:37:34 +000072DisableCFI("disable-cfi", cl::desc("Do not use .cfi_* directives"));
73
74static cl::opt<bool>
Rafael Espindola96aa78c2011-01-23 17:55:27 +000075NoExecStack("mc-no-exec-stack", cl::desc("File doesn't need an exec stack"));
76
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000077enum OutputFileType {
Daniel Dunbar2d9f5d12010-03-23 23:47:12 +000078 OFT_Null,
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000079 OFT_AssemblyFile,
80 OFT_ObjectFile
81};
82static cl::opt<OutputFileType>
83FileType("filetype", cl::init(OFT_AssemblyFile),
84 cl::desc("Choose an output file type:"),
85 cl::values(
86 clEnumValN(OFT_AssemblyFile, "asm",
87 "Emit an assembly ('.s') file"),
Daniel Dunbar2d9f5d12010-03-23 23:47:12 +000088 clEnumValN(OFT_Null, "null",
89 "Don't emit anything (for timing purposes)"),
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000090 clEnumValN(OFT_ObjectFile, "obj",
91 "Emit a native object ('.o') file"),
92 clEnumValEnd));
93
Chris Lattnerb23677e2009-06-21 05:22:37 +000094static cl::list<std::string>
95IncludeDirs("I", cl::desc("Directory of include files"),
96 cl::value_desc("directory"), cl::Prefix);
Chris Lattnerf9f065e2009-06-18 23:04:45 +000097
Daniel Dunbarb4b53e52009-07-17 22:38:58 +000098static cl::opt<std::string>
Daniel Dunbar181ab6a2010-03-13 02:20:38 +000099ArchName("arch", cl::desc("Target arch to assemble for, "
Bill Wendling916a94b2011-06-17 20:35:21 +0000100 "see -version for available targets"));
Daniel Dunbar181ab6a2010-03-13 02:20:38 +0000101
102static cl::opt<std::string>
Nick Lewyckyed1f1682009-11-01 22:07:54 +0000103TripleName("triple", cl::desc("Target triple to assemble for, "
Daniel Dunbar181ab6a2010-03-13 02:20:38 +0000104 "see -version for available targets"));
Daniel Dunbarb4b53e52009-07-17 22:38:58 +0000105
Jim Grosbachd2f14252010-10-30 15:57:50 +0000106static cl::opt<std::string>
107MCPU("mcpu",
108 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
109 cl::value_desc("cpu-name"),
110 cl::init(""));
111
James Molloyb9505852011-09-07 17:24:38 +0000112static cl::list<std::string>
113MAttrs("mattr",
114 cl::CommaSeparated,
115 cl::desc("Target specific attributes (-mattr=help for details)"),
116 cl::value_desc("a1,+a2,-a3,..."));
117
Evan Cheng43966132011-07-19 06:37:02 +0000118static cl::opt<Reloc::Model>
119RelocModel("relocation-model",
120 cl::desc("Choose relocation model"),
121 cl::init(Reloc::Default),
122 cl::values(
123 clEnumValN(Reloc::Default, "default",
124 "Target default relocation model"),
125 clEnumValN(Reloc::Static, "static",
126 "Non-relocatable code"),
127 clEnumValN(Reloc::PIC_, "pic",
128 "Fully relocatable, position independent code"),
129 clEnumValN(Reloc::DynamicNoPIC, "dynamic-no-pic",
130 "Relocatable external references, non-relocatable code"),
131 clEnumValEnd));
132
Evan Cheng34ad6db2011-07-20 07:51:56 +0000133static cl::opt<llvm::CodeModel::Model>
134CMModel("code-model",
135 cl::desc("Choose code model"),
136 cl::init(CodeModel::Default),
137 cl::values(clEnumValN(CodeModel::Default, "default",
138 "Target default code model"),
139 clEnumValN(CodeModel::Small, "small",
140 "Small code model"),
141 clEnumValN(CodeModel::Kernel, "kernel",
142 "Kernel code model"),
143 clEnumValN(CodeModel::Medium, "medium",
144 "Medium code model"),
145 clEnumValN(CodeModel::Large, "large",
146 "Large code model"),
147 clEnumValEnd));
148
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000149static cl::opt<bool>
Bill Wendling916a94b2011-06-17 20:35:21 +0000150NoInitialTextSection("n", cl::desc("Don't assume assembly file starts "
151 "in the text section"));
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000152
Daniel Dunbarc6cf43d2011-03-28 22:49:15 +0000153static cl::opt<bool>
Bill Wendling916a94b2011-06-17 20:35:21 +0000154SaveTempLabels("L", cl::desc("Don't discard temporary labels"));
Daniel Dunbarc6cf43d2011-03-28 22:49:15 +0000155
Kevin Enderby613b7572011-11-01 22:27:22 +0000156static cl::opt<bool>
157GenDwarfForAssembly("g", cl::desc("Generate dwarf debugging info for assembly "
158 "source files"));
159
Chris Lattnerb23677e2009-06-21 05:22:37 +0000160enum ActionType {
Chris Lattner27aa7d22009-06-21 20:16:42 +0000161 AC_AsLex,
Sean Callananba847da2009-12-17 01:49:59 +0000162 AC_Assemble,
Sean Callanan668b1542010-04-12 19:43:00 +0000163 AC_Disassemble,
Kevin Enderby3ed03162012-10-22 22:31:46 +0000164 AC_EDisassemble,
165 AC_MDisassemble
Chris Lattnerb23677e2009-06-21 05:22:37 +0000166};
Chris Lattnerf9f065e2009-06-18 23:04:45 +0000167
Chris Lattnerb23677e2009-06-21 05:22:37 +0000168static cl::opt<ActionType>
169Action(cl::desc("Action to perform:"),
Chris Lattner27aa7d22009-06-21 20:16:42 +0000170 cl::init(AC_Assemble),
171 cl::values(clEnumValN(AC_AsLex, "as-lex",
172 "Lex tokens from a .s file"),
173 clEnumValN(AC_Assemble, "assemble",
Chris Lattnerb23677e2009-06-21 05:22:37 +0000174 "Assemble a .s file (default)"),
Sean Callananba847da2009-12-17 01:49:59 +0000175 clEnumValN(AC_Disassemble, "disassemble",
176 "Disassemble strings of hex bytes"),
Sean Callanan668b1542010-04-12 19:43:00 +0000177 clEnumValN(AC_EDisassemble, "edis",
178 "Enhanced disassembly of strings of hex bytes"),
Kevin Enderby3ed03162012-10-22 22:31:46 +0000179 clEnumValN(AC_MDisassemble, "mdis",
180 "Marked up disassembly of strings of hex bytes"),
Chris Lattnerb23677e2009-06-21 05:22:37 +0000181 clEnumValEnd));
182
Kevin Enderby9823ca92009-09-04 21:45:34 +0000183static const Target *GetTarget(const char *ProgName) {
Daniel Dunbar181ab6a2010-03-13 02:20:38 +0000184 // Figure out the target triple.
185 if (TripleName.empty())
Sebastian Pop01738642011-11-01 21:32:20 +0000186 TripleName = sys::getDefaultTargetTriple();
Evan Chengd6dcf392011-07-26 19:02:16 +0000187 Triple TheTriple(Triple::normalize(TripleName));
188
Kevin Enderby9ed9e5d2012-05-08 23:38:45 +0000189 // Get the target specific parser.
190 std::string Error;
191 const Target *TheTarget = TargetRegistry::lookupTarget(ArchName, TheTriple,
192 Error);
193 if (!TheTarget) {
194 errs() << ProgName << ": " << Error;
195 return 0;
Daniel Dunbar181ab6a2010-03-13 02:20:38 +0000196 }
197
Kevin Enderby9ed9e5d2012-05-08 23:38:45 +0000198 // Update the triple name and return the found target.
Evan Chengd6dcf392011-07-26 19:02:16 +0000199 TripleName = TheTriple.getTriple();
200 return TheTarget;
Kevin Enderby9823ca92009-09-04 21:45:34 +0000201}
202
Dan Gohmand4c45432010-09-01 14:20:41 +0000203static tool_output_file *GetOutputStream() {
Dan Gohmand5826a32010-08-20 01:07:01 +0000204 if (OutputFilename == "")
205 OutputFilename = "-";
206
207 std::string Err;
208 tool_output_file *Out = new tool_output_file(OutputFilename.c_str(), Err,
209 raw_fd_ostream::F_Binary);
210 if (!Err.empty()) {
211 errs() << Err << '\n';
212 delete Out;
213 return 0;
214 }
Dan Gohmand4c45432010-09-01 14:20:41 +0000215
216 return Out;
Dan Gohmand5826a32010-08-20 01:07:01 +0000217}
218
Kevin Enderby94c2e852011-12-09 18:09:40 +0000219static std::string DwarfDebugFlags;
220static void setDwarfDebugFlags(int argc, char **argv) {
221 if (!getenv("RC_DEBUG_OPTIONS"))
222 return;
223 for (int i = 0; i < argc; i++) {
224 DwarfDebugFlags += argv[i];
225 if (i + 1 < argc)
226 DwarfDebugFlags += " ";
227 }
228}
229
Richard Bartond0c478d2012-04-16 11:32:10 +0000230static int AsLexInput(SourceMgr &SrcMgr, MCAsmInfo &MAI, tool_output_file *Out) {
Chris Lattnerb23677e2009-06-21 05:22:37 +0000231
Richard Bartond0c478d2012-04-16 11:32:10 +0000232 AsmLexer Lexer(MAI);
Daniel Dunbar346cc612010-07-18 18:31:28 +0000233 Lexer.setBuffer(SrcMgr.getMemoryBuffer(0));
234
Chris Lattner27aa7d22009-06-21 20:16:42 +0000235 bool Error = false;
Daniel Dunbara3c924f2009-07-28 16:56:42 +0000236 while (Lexer.Lex().isNot(AsmToken::Eof)) {
Daniel Dunbar54b63692010-10-25 20:18:46 +0000237 AsmToken Tok = Lexer.getTok();
238
239 switch (Tok.getKind()) {
Chris Lattner27aa7d22009-06-21 20:16:42 +0000240 default:
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000241 SrcMgr.PrintMessage(Lexer.getLoc(), SourceMgr::DK_Warning,
242 "unknown token");
Chris Lattner27aa7d22009-06-21 20:16:42 +0000243 Error = true;
244 break;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000245 case AsmToken::Error:
Chris Lattner27aa7d22009-06-21 20:16:42 +0000246 Error = true; // error already printed.
Chris Lattner4651bca2009-06-21 19:21:25 +0000247 break;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000248 case AsmToken::Identifier:
Daniel Dunbar54b63692010-10-25 20:18:46 +0000249 Out->os() << "identifier: " << Lexer.getTok().getString();
Chris Lattnera59e8772009-06-21 07:19:10 +0000250 break;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000251 case AsmToken::Integer:
Daniel Dunbar54b63692010-10-25 20:18:46 +0000252 Out->os() << "int: " << Lexer.getTok().getString();
Chris Lattnera59e8772009-06-21 07:19:10 +0000253 break;
Daniel Dunbar54f0a622010-09-24 01:59:31 +0000254 case AsmToken::Real:
Daniel Dunbar54b63692010-10-25 20:18:46 +0000255 Out->os() << "real: " << Lexer.getTok().getString();
Daniel Dunbar54f0a622010-09-24 01:59:31 +0000256 break;
Daniel Dunbarf9698682010-09-16 00:42:35 +0000257 case AsmToken::Register:
Daniel Dunbar54b63692010-10-25 20:18:46 +0000258 Out->os() << "register: " << Lexer.getTok().getRegVal();
Daniel Dunbarf9698682010-09-16 00:42:35 +0000259 break;
260 case AsmToken::String:
Daniel Dunbar54b63692010-10-25 20:18:46 +0000261 Out->os() << "string: " << Lexer.getTok().getString();
Daniel Dunbarf9698682010-09-16 00:42:35 +0000262 break;
Daniel Dunbar165e8342009-07-01 06:56:54 +0000263
Daniel Dunbar54b63692010-10-25 20:18:46 +0000264 case AsmToken::Amp: Out->os() << "Amp"; break;
265 case AsmToken::AmpAmp: Out->os() << "AmpAmp"; break;
266 case AsmToken::At: Out->os() << "At"; break;
267 case AsmToken::Caret: Out->os() << "Caret"; break;
268 case AsmToken::Colon: Out->os() << "Colon"; break;
269 case AsmToken::Comma: Out->os() << "Comma"; break;
270 case AsmToken::Dollar: Out->os() << "Dollar"; break;
271 case AsmToken::Dot: Out->os() << "Dot"; break;
272 case AsmToken::EndOfStatement: Out->os() << "EndOfStatement"; break;
273 case AsmToken::Eof: Out->os() << "Eof"; break;
274 case AsmToken::Equal: Out->os() << "Equal"; break;
275 case AsmToken::EqualEqual: Out->os() << "EqualEqual"; break;
276 case AsmToken::Exclaim: Out->os() << "Exclaim"; break;
277 case AsmToken::ExclaimEqual: Out->os() << "ExclaimEqual"; break;
278 case AsmToken::Greater: Out->os() << "Greater"; break;
279 case AsmToken::GreaterEqual: Out->os() << "GreaterEqual"; break;
280 case AsmToken::GreaterGreater: Out->os() << "GreaterGreater"; break;
281 case AsmToken::Hash: Out->os() << "Hash"; break;
282 case AsmToken::LBrac: Out->os() << "LBrac"; break;
283 case AsmToken::LCurly: Out->os() << "LCurly"; break;
284 case AsmToken::LParen: Out->os() << "LParen"; break;
285 case AsmToken::Less: Out->os() << "Less"; break;
286 case AsmToken::LessEqual: Out->os() << "LessEqual"; break;
287 case AsmToken::LessGreater: Out->os() << "LessGreater"; break;
288 case AsmToken::LessLess: Out->os() << "LessLess"; break;
289 case AsmToken::Minus: Out->os() << "Minus"; break;
290 case AsmToken::Percent: Out->os() << "Percent"; break;
291 case AsmToken::Pipe: Out->os() << "Pipe"; break;
292 case AsmToken::PipePipe: Out->os() << "PipePipe"; break;
293 case AsmToken::Plus: Out->os() << "Plus"; break;
294 case AsmToken::RBrac: Out->os() << "RBrac"; break;
295 case AsmToken::RCurly: Out->os() << "RCurly"; break;
296 case AsmToken::RParen: Out->os() << "RParen"; break;
297 case AsmToken::Slash: Out->os() << "Slash"; break;
298 case AsmToken::Star: Out->os() << "Star"; break;
299 case AsmToken::Tilde: Out->os() << "Tilde"; break;
Chris Lattnera59e8772009-06-21 07:19:10 +0000300 }
Daniel Dunbar54b63692010-10-25 20:18:46 +0000301
302 // Print the token string.
303 Out->os() << " (\"";
304 Out->os().write_escaped(Tok.getString());
305 Out->os() << "\")\n";
Chris Lattnera59e8772009-06-21 07:19:10 +0000306 }
Dan Gohmand5826a32010-08-20 01:07:01 +0000307
Chris Lattner27aa7d22009-06-21 20:16:42 +0000308 return Error;
Chris Lattnerb23677e2009-06-21 05:22:37 +0000309}
310
Richard Bartond0c478d2012-04-16 11:32:10 +0000311static int AssembleInput(const char *ProgName, const Target *TheTarget,
312 SourceMgr &SrcMgr, MCContext &Ctx, MCStreamer &Str,
313 MCAsmInfo &MAI, MCSubtargetInfo &STI) {
314 OwningPtr<MCAsmParser> Parser(createMCAsmParser(SrcMgr, Ctx,
315 Str, MAI));
316 OwningPtr<MCTargetAsmParser> TAP(TheTarget->createMCAsmParser(STI, *Parser));
317 if (!TAP) {
318 errs() << ProgName
319 << ": error: this target does not support assembly parsing.\n";
320 return 1;
321 }
322
323 Parser->setShowParsedOperands(ShowInstOperands);
324 Parser->setTargetParser(*TAP.get());
325
326 int Res = Parser->Run(NoInitialTextSection);
327
328 return Res;
329}
330
331int main(int argc, char **argv) {
332 // Print a stack trace if we signal out.
333 sys::PrintStackTraceOnErrorSignal();
334 PrettyStackTraceProgram X(argc, argv);
335 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
336
337 // Initialize targets and assembly printers/parsers.
338 llvm::InitializeAllTargetInfos();
339 llvm::InitializeAllTargetMCs();
340 llvm::InitializeAllAsmParsers();
341 llvm::InitializeAllDisassemblers();
342
343 // Register the target printer for --version.
344 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
345
346 cl::ParseCommandLineOptions(argc, argv, "llvm machine code playground\n");
347 TripleName = Triple::normalize(TripleName);
348 setDwarfDebugFlags(argc, argv);
349
350 const char *ProgName = argv[0];
Daniel Dunbarc22e0b22009-08-14 03:48:55 +0000351 const Target *TheTarget = GetTarget(ProgName);
352 if (!TheTarget)
353 return 1;
354
Michael J. Spencer3ff95632010-12-16 03:29:14 +0000355 OwningPtr<MemoryBuffer> BufferPtr;
356 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename, BufferPtr)) {
Michael J. Spencer333fb042010-12-09 17:36:48 +0000357 errs() << ProgName << ": " << ec.message() << '\n';
Chris Lattner27aa7d22009-06-21 20:16:42 +0000358 return 1;
359 }
Michael J. Spencer3ff95632010-12-16 03:29:14 +0000360 MemoryBuffer *Buffer = BufferPtr.take();
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000361
Chris Lattner27aa7d22009-06-21 20:16:42 +0000362 SourceMgr SrcMgr;
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000363
Daniel Dunbar12a8a442009-08-19 16:25:53 +0000364 // Tell SrcMgr about this buffer, which is what the parser will pick up.
Chris Lattner27aa7d22009-06-21 20:16:42 +0000365 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000366
Chris Lattner27aa7d22009-06-21 20:16:42 +0000367 // Record the location of the include directories so that the lexer can find
368 // it later.
369 SrcMgr.setIncludeDirs(IncludeDirs);
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000370
371
Evan Cheng1abf2cb2011-07-14 23:50:31 +0000372 llvm::OwningPtr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(TripleName));
Chris Lattnerc18409a2010-03-11 22:53:35 +0000373 assert(MAI && "Unable to create target asm info!");
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000374
Evan Cheng0e6a0522011-07-18 20:57:22 +0000375 llvm::OwningPtr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
376 assert(MRI && "Unable to create target register info!");
377
Evan Chenge76a33b2011-07-20 05:58:47 +0000378 // FIXME: This is not pretty. MCContext has a ptr to MCObjectFileInfo and
379 // MCObjectFileInfo needs a MCContext reference in order to initialize itself.
380 OwningPtr<MCObjectFileInfo> MOFI(new MCObjectFileInfo());
Jim Grosbach93cd59a2012-01-26 23:20:07 +0000381 MCContext Ctx(*MAI, *MRI, MOFI.get(), &SrcMgr);
Evan Cheng203576a2011-07-20 19:50:42 +0000382 MOFI->InitMCObjectFileInfo(TripleName, RelocModel, CMModel, Ctx);
Evan Chenge76a33b2011-07-20 05:58:47 +0000383
Daniel Dunbarc6cf43d2011-03-28 22:49:15 +0000384 if (SaveTempLabels)
385 Ctx.setAllowTemporaryLabels(false);
Rafael Espindola89b93722010-12-10 07:39:47 +0000386
Kevin Enderby613b7572011-11-01 22:27:22 +0000387 Ctx.setGenDwarfForAssembly(GenDwarfForAssembly);
Kevin Enderby94c2e852011-12-09 18:09:40 +0000388 if (!DwarfDebugFlags.empty())
389 Ctx.setDwarfDebugFlags(StringRef(DwarfDebugFlags));
Kevin Enderby613b7572011-11-01 22:27:22 +0000390
James Molloyb9505852011-09-07 17:24:38 +0000391 // Package up features to be passed to target/subtarget
392 std::string FeaturesStr;
393 if (MAttrs.size()) {
394 SubtargetFeatures Features;
395 for (unsigned i = 0; i != MAttrs.size(); ++i)
396 Features.AddFeature(MAttrs[i]);
397 FeaturesStr = Features.getString();
398 }
399
Dan Gohmand4c45432010-09-01 14:20:41 +0000400 OwningPtr<tool_output_file> Out(GetOutputStream());
Dan Gohmand5826a32010-08-20 01:07:01 +0000401 if (!Out)
402 return 1;
403
Dan Gohmand4c45432010-09-01 14:20:41 +0000404 formatted_raw_ostream FOS(Out->os());
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000405 OwningPtr<MCStreamer> Str;
Chris Lattnerf3ce0092009-08-17 04:23:44 +0000406
Evan Cheng59ee62d2011-07-11 03:57:24 +0000407 OwningPtr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo());
Evan Chengffc0e732011-07-09 05:47:46 +0000408 OwningPtr<MCSubtargetInfo>
409 STI(TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr));
410
Kevin Enderby3ed03162012-10-22 22:31:46 +0000411 MCInstPrinter *IP;
Kevin Enderby9823ca92009-09-04 21:45:34 +0000412 if (FileType == OFT_AssemblyFile) {
Kevin Enderby3ed03162012-10-22 22:31:46 +0000413 IP =
Craig Topper17463b32012-04-02 06:09:36 +0000414 TheTarget->createMCInstPrinter(OutputAsmVariant, *MAI, *MCII, *MRI, *STI);
Benjamin Kramer1abcd062010-07-29 17:48:06 +0000415 MCCodeEmitter *CE = 0;
Evan Cheng78c10ee2011-07-25 23:24:55 +0000416 MCAsmBackend *MAB = 0;
Daniel Dunbar745dacc2010-12-16 03:05:59 +0000417 if (ShowEncoding) {
Jim Grosbach918f55f2012-05-15 17:35:52 +0000418 CE = TheTarget->createMCCodeEmitter(*MCII, *MRI, *STI, Ctx);
Roman Divacky536a88a2012-09-18 16:08:49 +0000419 MAB = TheTarget->createMCAsmBackend(TripleName, MCPU);
Daniel Dunbar745dacc2010-12-16 03:05:59 +0000420 }
Rafael Espindola985884c2012-11-23 17:37:34 +0000421 bool UseCFI = !DisableCFI;
Rafael Espindola89b93722010-12-10 07:39:47 +0000422 Str.reset(TheTarget->createAsmStreamer(Ctx, FOS, /*asmverbose*/true,
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000423 /*useLoc*/ true,
Rafael Espindola985884c2012-11-23 17:37:34 +0000424 UseCFI,
Nick Lewycky44d798d2011-10-17 23:05:28 +0000425 /*useDwarfDirectory*/ true,
426 IP, CE, MAB, ShowInst));
Jim Grosbach57920512011-12-05 23:20:14 +0000427
Daniel Dunbar2d9f5d12010-03-23 23:47:12 +0000428 } else if (FileType == OFT_Null) {
429 Str.reset(createNullStreamer(Ctx));
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000430 } else {
431 assert(FileType == OFT_ObjectFile && "Invalid file type!");
Jim Grosbach918f55f2012-05-15 17:35:52 +0000432 MCCodeEmitter *CE = TheTarget->createMCCodeEmitter(*MCII, *MRI, *STI, Ctx);
Roman Divacky536a88a2012-09-18 16:08:49 +0000433 MCAsmBackend *MAB = TheTarget->createMCAsmBackend(TripleName, MCPU);
Evan Cheng28c85a82011-07-26 00:42:34 +0000434 Str.reset(TheTarget->createMCObjectStreamer(TripleName, Ctx, *MAB,
435 FOS, CE, RelaxAll,
436 NoExecStack));
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000437 }
Daniel Dunbara0d14262009-06-24 23:30:00 +0000438
Richard Bartond0c478d2012-04-16 11:32:10 +0000439 int Res = 1;
Chris Lattnerb23677e2009-06-21 05:22:37 +0000440 switch (Action) {
Chris Lattner27aa7d22009-06-21 20:16:42 +0000441 case AC_AsLex:
Richard Bartond0c478d2012-04-16 11:32:10 +0000442 Res = AsLexInput(SrcMgr, *MAI, Out.get());
443 break;
Chris Lattnerb23677e2009-06-21 05:22:37 +0000444 case AC_Assemble:
Richard Bartond0c478d2012-04-16 11:32:10 +0000445 Res = AssembleInput(ProgName, TheTarget, SrcMgr, Ctx, *Str, *MAI, *STI);
446 break;
Kevin Enderby3ed03162012-10-22 22:31:46 +0000447 case AC_MDisassemble:
448 IP->setUseMarkup(1);
449 // Fall through to do disassembly.
Sean Callananba847da2009-12-17 01:49:59 +0000450 case AC_Disassemble:
Richard Bartond0c478d2012-04-16 11:32:10 +0000451 Res = Disassembler::disassemble(*TheTarget, TripleName, *STI, *Str,
452 *Buffer, SrcMgr, Out->os());
453 break;
Sean Callanan668b1542010-04-12 19:43:00 +0000454 case AC_EDisassemble:
Richard Bartond0c478d2012-04-16 11:32:10 +0000455 Res = Disassembler::disassembleEnhanced(TripleName, *Buffer, SrcMgr, Out->os());
456 break;
Chris Lattnerb23677e2009-06-21 05:22:37 +0000457 }
Richard Bartond0c478d2012-04-16 11:32:10 +0000458
459 // Keep output if no errors.
460 if (Res == 0) Out->keep();
461 return Res;
Chris Lattnerf9f065e2009-06-18 23:04:45 +0000462}