blob: 36a482e908cd2431d4048731818b7b3e04a8561d [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"
Craig Topperfb22ede2012-04-15 22:00:22 +000018#include "llvm/MC/MCAsmInfo.h"
Chris Lattnercbc23f72009-06-24 00:52:40 +000019#include "llvm/MC/MCContext.h"
Daniel Dunbar4a0abd82009-08-27 00:51:57 +000020#include "llvm/MC/MCCodeEmitter.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"
24#include "llvm/MC/MCRegisterInfo.h"
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000025#include "llvm/MC/MCSectionMachO.h"
Chris Lattnercbc23f72009-06-24 00:52:40 +000026#include "llvm/MC/MCStreamer.h"
Evan Chengffc0e732011-07-09 05:47:46 +000027#include "llvm/MC/MCSubtargetInfo.h"
Evan Cheng94b95502011-07-26 00:24:13 +000028#include "llvm/MC/MCTargetAsmParser.h"
Evan Chengab8be962011-06-29 01:14:12 +000029#include "llvm/MC/SubtargetFeature.h"
Chris Lattnercbc23f72009-06-24 00:52:40 +000030#include "llvm/ADT/OwningPtr.h"
Chris Lattnerf9f065e2009-06-18 23:04:45 +000031#include "llvm/Support/CommandLine.h"
Dan Gohmand5826a32010-08-20 01:07:01 +000032#include "llvm/Support/FileUtilities.h"
Daniel Dunbarc22e0b22009-08-14 03:48:55 +000033#include "llvm/Support/FormattedStream.h"
Chris Lattnerf9f065e2009-06-18 23:04:45 +000034#include "llvm/Support/ManagedStatic.h"
35#include "llvm/Support/MemoryBuffer.h"
36#include "llvm/Support/PrettyStackTrace.h"
Chris Lattnerb23677e2009-06-21 05:22:37 +000037#include "llvm/Support/SourceMgr.h"
Dan Gohmane4f1a9b2010-10-07 20:32:40 +000038#include "llvm/Support/ToolOutputFile.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000039#include "llvm/Support/Host.h"
40#include "llvm/Support/Signals.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000041#include "llvm/Support/TargetRegistry.h"
42#include "llvm/Support/TargetSelect.h"
Michael J. Spencer333fb042010-12-09 17:36:48 +000043#include "llvm/Support/system_error.h"
Chris Lattnera3dcfb12009-12-22 22:50:29 +000044#include "Disassembler.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 Espindola96aa78c2011-01-23 17:55:27 +000072NoExecStack("mc-no-exec-stack", cl::desc("File doesn't need an exec stack"));
73
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000074enum OutputFileType {
Daniel Dunbar2d9f5d12010-03-23 23:47:12 +000075 OFT_Null,
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000076 OFT_AssemblyFile,
77 OFT_ObjectFile
78};
79static cl::opt<OutputFileType>
80FileType("filetype", cl::init(OFT_AssemblyFile),
81 cl::desc("Choose an output file type:"),
82 cl::values(
83 clEnumValN(OFT_AssemblyFile, "asm",
84 "Emit an assembly ('.s') file"),
Daniel Dunbar2d9f5d12010-03-23 23:47:12 +000085 clEnumValN(OFT_Null, "null",
86 "Don't emit anything (for timing purposes)"),
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000087 clEnumValN(OFT_ObjectFile, "obj",
88 "Emit a native object ('.o') file"),
89 clEnumValEnd));
90
Chris Lattnerb23677e2009-06-21 05:22:37 +000091static cl::list<std::string>
92IncludeDirs("I", cl::desc("Directory of include files"),
93 cl::value_desc("directory"), cl::Prefix);
Chris Lattnerf9f065e2009-06-18 23:04:45 +000094
Daniel Dunbarb4b53e52009-07-17 22:38:58 +000095static cl::opt<std::string>
Daniel Dunbar181ab6a2010-03-13 02:20:38 +000096ArchName("arch", cl::desc("Target arch to assemble for, "
Bill Wendling916a94b2011-06-17 20:35:21 +000097 "see -version for available targets"));
Daniel Dunbar181ab6a2010-03-13 02:20:38 +000098
99static cl::opt<std::string>
Nick Lewyckyed1f1682009-11-01 22:07:54 +0000100TripleName("triple", cl::desc("Target triple to assemble for, "
Daniel Dunbar181ab6a2010-03-13 02:20:38 +0000101 "see -version for available targets"));
Daniel Dunbarb4b53e52009-07-17 22:38:58 +0000102
Jim Grosbachd2f14252010-10-30 15:57:50 +0000103static cl::opt<std::string>
104MCPU("mcpu",
105 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
106 cl::value_desc("cpu-name"),
107 cl::init(""));
108
James Molloyb9505852011-09-07 17:24:38 +0000109static cl::list<std::string>
110MAttrs("mattr",
111 cl::CommaSeparated,
112 cl::desc("Target specific attributes (-mattr=help for details)"),
113 cl::value_desc("a1,+a2,-a3,..."));
114
Evan Cheng43966132011-07-19 06:37:02 +0000115static cl::opt<Reloc::Model>
116RelocModel("relocation-model",
117 cl::desc("Choose relocation model"),
118 cl::init(Reloc::Default),
119 cl::values(
120 clEnumValN(Reloc::Default, "default",
121 "Target default relocation model"),
122 clEnumValN(Reloc::Static, "static",
123 "Non-relocatable code"),
124 clEnumValN(Reloc::PIC_, "pic",
125 "Fully relocatable, position independent code"),
126 clEnumValN(Reloc::DynamicNoPIC, "dynamic-no-pic",
127 "Relocatable external references, non-relocatable code"),
128 clEnumValEnd));
129
Evan Cheng34ad6db2011-07-20 07:51:56 +0000130static cl::opt<llvm::CodeModel::Model>
131CMModel("code-model",
132 cl::desc("Choose code model"),
133 cl::init(CodeModel::Default),
134 cl::values(clEnumValN(CodeModel::Default, "default",
135 "Target default code model"),
136 clEnumValN(CodeModel::Small, "small",
137 "Small code model"),
138 clEnumValN(CodeModel::Kernel, "kernel",
139 "Kernel code model"),
140 clEnumValN(CodeModel::Medium, "medium",
141 "Medium code model"),
142 clEnumValN(CodeModel::Large, "large",
143 "Large code model"),
144 clEnumValEnd));
145
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000146static cl::opt<bool>
Bill Wendling916a94b2011-06-17 20:35:21 +0000147NoInitialTextSection("n", cl::desc("Don't assume assembly file starts "
148 "in the text section"));
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000149
Daniel Dunbarc6cf43d2011-03-28 22:49:15 +0000150static cl::opt<bool>
Bill Wendling916a94b2011-06-17 20:35:21 +0000151SaveTempLabels("L", cl::desc("Don't discard temporary labels"));
Daniel Dunbarc6cf43d2011-03-28 22:49:15 +0000152
Kevin Enderby613b7572011-11-01 22:27:22 +0000153static cl::opt<bool>
154GenDwarfForAssembly("g", cl::desc("Generate dwarf debugging info for assembly "
155 "source files"));
156
Chris Lattnerb23677e2009-06-21 05:22:37 +0000157enum ActionType {
Chris Lattner27aa7d22009-06-21 20:16:42 +0000158 AC_AsLex,
Sean Callananba847da2009-12-17 01:49:59 +0000159 AC_Assemble,
Sean Callanan668b1542010-04-12 19:43:00 +0000160 AC_Disassemble,
161 AC_EDisassemble
Chris Lattnerb23677e2009-06-21 05:22:37 +0000162};
Chris Lattnerf9f065e2009-06-18 23:04:45 +0000163
Chris Lattnerb23677e2009-06-21 05:22:37 +0000164static cl::opt<ActionType>
165Action(cl::desc("Action to perform:"),
Chris Lattner27aa7d22009-06-21 20:16:42 +0000166 cl::init(AC_Assemble),
167 cl::values(clEnumValN(AC_AsLex, "as-lex",
168 "Lex tokens from a .s file"),
169 clEnumValN(AC_Assemble, "assemble",
Chris Lattnerb23677e2009-06-21 05:22:37 +0000170 "Assemble a .s file (default)"),
Sean Callananba847da2009-12-17 01:49:59 +0000171 clEnumValN(AC_Disassemble, "disassemble",
172 "Disassemble strings of hex bytes"),
Sean Callanan668b1542010-04-12 19:43:00 +0000173 clEnumValN(AC_EDisassemble, "edis",
174 "Enhanced disassembly of strings of hex bytes"),
Chris Lattnerb23677e2009-06-21 05:22:37 +0000175 clEnumValEnd));
176
Kevin Enderby9823ca92009-09-04 21:45:34 +0000177static const Target *GetTarget(const char *ProgName) {
Daniel Dunbar181ab6a2010-03-13 02:20:38 +0000178 // Figure out the target triple.
179 if (TripleName.empty())
Sebastian Pop01738642011-11-01 21:32:20 +0000180 TripleName = sys::getDefaultTargetTriple();
Evan Chengd6dcf392011-07-26 19:02:16 +0000181 Triple TheTriple(Triple::normalize(TripleName));
182
183 const Target *TheTarget = 0;
Daniel Dunbar181ab6a2010-03-13 02:20:38 +0000184 if (!ArchName.empty()) {
Evan Chengd6dcf392011-07-26 19:02:16 +0000185 for (TargetRegistry::iterator it = TargetRegistry::begin(),
186 ie = TargetRegistry::end(); it != ie; ++it) {
187 if (ArchName == it->getName()) {
188 TheTarget = &*it;
189 break;
190 }
191 }
192
193 if (!TheTarget) {
194 errs() << ProgName << ": error: invalid target '" << ArchName << "'.\n";
195 return 0;
196 }
197
198 // Adjust the triple to match (if known), otherwise stick with the
199 // module/host triple.
200 Triple::ArchType Type = Triple::getArchTypeForLLVMName(ArchName);
201 if (Type != Triple::UnknownArch)
202 TheTriple.setArch(Type);
203 } else {
204 // Get the target specific parser.
205 std::string Error;
206 TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), Error);
207 if (TheTarget == 0) {
208 errs() << ProgName << ": error: unable to get target for '"
209 << TheTriple.getTriple()
210 << "', see --version and --triple.\n";
211 return 0;
212 }
Daniel Dunbar181ab6a2010-03-13 02:20:38 +0000213 }
214
Evan Chengd6dcf392011-07-26 19:02:16 +0000215 TripleName = TheTriple.getTriple();
216 return TheTarget;
Kevin Enderby9823ca92009-09-04 21:45:34 +0000217}
218
Dan Gohmand4c45432010-09-01 14:20:41 +0000219static tool_output_file *GetOutputStream() {
Dan Gohmand5826a32010-08-20 01:07:01 +0000220 if (OutputFilename == "")
221 OutputFilename = "-";
222
223 std::string Err;
224 tool_output_file *Out = new tool_output_file(OutputFilename.c_str(), Err,
225 raw_fd_ostream::F_Binary);
226 if (!Err.empty()) {
227 errs() << Err << '\n';
228 delete Out;
229 return 0;
230 }
Dan Gohmand4c45432010-09-01 14:20:41 +0000231
232 return Out;
Dan Gohmand5826a32010-08-20 01:07:01 +0000233}
234
Kevin Enderby94c2e852011-12-09 18:09:40 +0000235static std::string DwarfDebugFlags;
236static void setDwarfDebugFlags(int argc, char **argv) {
237 if (!getenv("RC_DEBUG_OPTIONS"))
238 return;
239 for (int i = 0; i < argc; i++) {
240 DwarfDebugFlags += argv[i];
241 if (i + 1 < argc)
242 DwarfDebugFlags += " ";
243 }
244}
245
Richard Bartond0c478d2012-04-16 11:32:10 +0000246static int AsLexInput(SourceMgr &SrcMgr, MCAsmInfo &MAI, tool_output_file *Out) {
Chris Lattnerb23677e2009-06-21 05:22:37 +0000247
Richard Bartond0c478d2012-04-16 11:32:10 +0000248 AsmLexer Lexer(MAI);
Daniel Dunbar346cc612010-07-18 18:31:28 +0000249 Lexer.setBuffer(SrcMgr.getMemoryBuffer(0));
250
Chris Lattner27aa7d22009-06-21 20:16:42 +0000251 bool Error = false;
Daniel Dunbara3c924f2009-07-28 16:56:42 +0000252 while (Lexer.Lex().isNot(AsmToken::Eof)) {
Daniel Dunbar54b63692010-10-25 20:18:46 +0000253 AsmToken Tok = Lexer.getTok();
254
255 switch (Tok.getKind()) {
Chris Lattner27aa7d22009-06-21 20:16:42 +0000256 default:
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000257 SrcMgr.PrintMessage(Lexer.getLoc(), SourceMgr::DK_Warning,
258 "unknown token");
Chris Lattner27aa7d22009-06-21 20:16:42 +0000259 Error = true;
260 break;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000261 case AsmToken::Error:
Chris Lattner27aa7d22009-06-21 20:16:42 +0000262 Error = true; // error already printed.
Chris Lattner4651bca2009-06-21 19:21:25 +0000263 break;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000264 case AsmToken::Identifier:
Daniel Dunbar54b63692010-10-25 20:18:46 +0000265 Out->os() << "identifier: " << Lexer.getTok().getString();
Chris Lattnera59e8772009-06-21 07:19:10 +0000266 break;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000267 case AsmToken::Integer:
Daniel Dunbar54b63692010-10-25 20:18:46 +0000268 Out->os() << "int: " << Lexer.getTok().getString();
Chris Lattnera59e8772009-06-21 07:19:10 +0000269 break;
Daniel Dunbar54f0a622010-09-24 01:59:31 +0000270 case AsmToken::Real:
Daniel Dunbar54b63692010-10-25 20:18:46 +0000271 Out->os() << "real: " << Lexer.getTok().getString();
Daniel Dunbar54f0a622010-09-24 01:59:31 +0000272 break;
Daniel Dunbarf9698682010-09-16 00:42:35 +0000273 case AsmToken::Register:
Daniel Dunbar54b63692010-10-25 20:18:46 +0000274 Out->os() << "register: " << Lexer.getTok().getRegVal();
Daniel Dunbarf9698682010-09-16 00:42:35 +0000275 break;
276 case AsmToken::String:
Daniel Dunbar54b63692010-10-25 20:18:46 +0000277 Out->os() << "string: " << Lexer.getTok().getString();
Daniel Dunbarf9698682010-09-16 00:42:35 +0000278 break;
Daniel Dunbar165e8342009-07-01 06:56:54 +0000279
Daniel Dunbar54b63692010-10-25 20:18:46 +0000280 case AsmToken::Amp: Out->os() << "Amp"; break;
281 case AsmToken::AmpAmp: Out->os() << "AmpAmp"; break;
282 case AsmToken::At: Out->os() << "At"; break;
283 case AsmToken::Caret: Out->os() << "Caret"; break;
284 case AsmToken::Colon: Out->os() << "Colon"; break;
285 case AsmToken::Comma: Out->os() << "Comma"; break;
286 case AsmToken::Dollar: Out->os() << "Dollar"; break;
287 case AsmToken::Dot: Out->os() << "Dot"; break;
288 case AsmToken::EndOfStatement: Out->os() << "EndOfStatement"; break;
289 case AsmToken::Eof: Out->os() << "Eof"; break;
290 case AsmToken::Equal: Out->os() << "Equal"; break;
291 case AsmToken::EqualEqual: Out->os() << "EqualEqual"; break;
292 case AsmToken::Exclaim: Out->os() << "Exclaim"; break;
293 case AsmToken::ExclaimEqual: Out->os() << "ExclaimEqual"; break;
294 case AsmToken::Greater: Out->os() << "Greater"; break;
295 case AsmToken::GreaterEqual: Out->os() << "GreaterEqual"; break;
296 case AsmToken::GreaterGreater: Out->os() << "GreaterGreater"; break;
297 case AsmToken::Hash: Out->os() << "Hash"; break;
298 case AsmToken::LBrac: Out->os() << "LBrac"; break;
299 case AsmToken::LCurly: Out->os() << "LCurly"; break;
300 case AsmToken::LParen: Out->os() << "LParen"; break;
301 case AsmToken::Less: Out->os() << "Less"; break;
302 case AsmToken::LessEqual: Out->os() << "LessEqual"; break;
303 case AsmToken::LessGreater: Out->os() << "LessGreater"; break;
304 case AsmToken::LessLess: Out->os() << "LessLess"; break;
305 case AsmToken::Minus: Out->os() << "Minus"; break;
306 case AsmToken::Percent: Out->os() << "Percent"; break;
307 case AsmToken::Pipe: Out->os() << "Pipe"; break;
308 case AsmToken::PipePipe: Out->os() << "PipePipe"; break;
309 case AsmToken::Plus: Out->os() << "Plus"; break;
310 case AsmToken::RBrac: Out->os() << "RBrac"; break;
311 case AsmToken::RCurly: Out->os() << "RCurly"; break;
312 case AsmToken::RParen: Out->os() << "RParen"; break;
313 case AsmToken::Slash: Out->os() << "Slash"; break;
314 case AsmToken::Star: Out->os() << "Star"; break;
315 case AsmToken::Tilde: Out->os() << "Tilde"; break;
Chris Lattnera59e8772009-06-21 07:19:10 +0000316 }
Daniel Dunbar54b63692010-10-25 20:18:46 +0000317
318 // Print the token string.
319 Out->os() << " (\"";
320 Out->os().write_escaped(Tok.getString());
321 Out->os() << "\")\n";
Chris Lattnera59e8772009-06-21 07:19:10 +0000322 }
Dan Gohmand5826a32010-08-20 01:07:01 +0000323
Chris Lattner27aa7d22009-06-21 20:16:42 +0000324 return Error;
Chris Lattnerb23677e2009-06-21 05:22:37 +0000325}
326
Richard Bartond0c478d2012-04-16 11:32:10 +0000327static int AssembleInput(const char *ProgName, const Target *TheTarget,
328 SourceMgr &SrcMgr, MCContext &Ctx, MCStreamer &Str,
329 MCAsmInfo &MAI, MCSubtargetInfo &STI) {
330 OwningPtr<MCAsmParser> Parser(createMCAsmParser(SrcMgr, Ctx,
331 Str, MAI));
332 OwningPtr<MCTargetAsmParser> TAP(TheTarget->createMCAsmParser(STI, *Parser));
333 if (!TAP) {
334 errs() << ProgName
335 << ": error: this target does not support assembly parsing.\n";
336 return 1;
337 }
338
339 Parser->setShowParsedOperands(ShowInstOperands);
340 Parser->setTargetParser(*TAP.get());
341
342 int Res = Parser->Run(NoInitialTextSection);
343
344 return Res;
345}
346
347int main(int argc, char **argv) {
348 // Print a stack trace if we signal out.
349 sys::PrintStackTraceOnErrorSignal();
350 PrettyStackTraceProgram X(argc, argv);
351 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
352
353 // Initialize targets and assembly printers/parsers.
354 llvm::InitializeAllTargetInfos();
355 llvm::InitializeAllTargetMCs();
356 llvm::InitializeAllAsmParsers();
357 llvm::InitializeAllDisassemblers();
358
359 // Register the target printer for --version.
360 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
361
362 cl::ParseCommandLineOptions(argc, argv, "llvm machine code playground\n");
363 TripleName = Triple::normalize(TripleName);
364 setDwarfDebugFlags(argc, argv);
365
366 const char *ProgName = argv[0];
Daniel Dunbarc22e0b22009-08-14 03:48:55 +0000367 const Target *TheTarget = GetTarget(ProgName);
368 if (!TheTarget)
369 return 1;
370
Michael J. Spencer3ff95632010-12-16 03:29:14 +0000371 OwningPtr<MemoryBuffer> BufferPtr;
372 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename, BufferPtr)) {
Michael J. Spencer333fb042010-12-09 17:36:48 +0000373 errs() << ProgName << ": " << ec.message() << '\n';
Chris Lattner27aa7d22009-06-21 20:16:42 +0000374 return 1;
375 }
Michael J. Spencer3ff95632010-12-16 03:29:14 +0000376 MemoryBuffer *Buffer = BufferPtr.take();
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000377
Chris Lattner27aa7d22009-06-21 20:16:42 +0000378 SourceMgr SrcMgr;
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000379
Daniel Dunbar12a8a442009-08-19 16:25:53 +0000380 // Tell SrcMgr about this buffer, which is what the parser will pick up.
Chris Lattner27aa7d22009-06-21 20:16:42 +0000381 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000382
Chris Lattner27aa7d22009-06-21 20:16:42 +0000383 // Record the location of the include directories so that the lexer can find
384 // it later.
385 SrcMgr.setIncludeDirs(IncludeDirs);
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000386
387
Evan Cheng1abf2cb2011-07-14 23:50:31 +0000388 llvm::OwningPtr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(TripleName));
Chris Lattnerc18409a2010-03-11 22:53:35 +0000389 assert(MAI && "Unable to create target asm info!");
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000390
Evan Cheng0e6a0522011-07-18 20:57:22 +0000391 llvm::OwningPtr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
392 assert(MRI && "Unable to create target register info!");
393
Evan Chenge76a33b2011-07-20 05:58:47 +0000394 // FIXME: This is not pretty. MCContext has a ptr to MCObjectFileInfo and
395 // MCObjectFileInfo needs a MCContext reference in order to initialize itself.
396 OwningPtr<MCObjectFileInfo> MOFI(new MCObjectFileInfo());
Jim Grosbach93cd59a2012-01-26 23:20:07 +0000397 MCContext Ctx(*MAI, *MRI, MOFI.get(), &SrcMgr);
Evan Cheng203576a2011-07-20 19:50:42 +0000398 MOFI->InitMCObjectFileInfo(TripleName, RelocModel, CMModel, Ctx);
Evan Chenge76a33b2011-07-20 05:58:47 +0000399
Daniel Dunbarc6cf43d2011-03-28 22:49:15 +0000400 if (SaveTempLabels)
401 Ctx.setAllowTemporaryLabels(false);
Rafael Espindola89b93722010-12-10 07:39:47 +0000402
Kevin Enderby613b7572011-11-01 22:27:22 +0000403 Ctx.setGenDwarfForAssembly(GenDwarfForAssembly);
Kevin Enderby94c2e852011-12-09 18:09:40 +0000404 if (!DwarfDebugFlags.empty())
405 Ctx.setDwarfDebugFlags(StringRef(DwarfDebugFlags));
Kevin Enderby613b7572011-11-01 22:27:22 +0000406
James Molloyb9505852011-09-07 17:24:38 +0000407 // Package up features to be passed to target/subtarget
408 std::string FeaturesStr;
409 if (MAttrs.size()) {
410 SubtargetFeatures Features;
411 for (unsigned i = 0; i != MAttrs.size(); ++i)
412 Features.AddFeature(MAttrs[i]);
413 FeaturesStr = Features.getString();
414 }
415
Dan Gohmand4c45432010-09-01 14:20:41 +0000416 OwningPtr<tool_output_file> Out(GetOutputStream());
Dan Gohmand5826a32010-08-20 01:07:01 +0000417 if (!Out)
418 return 1;
419
Dan Gohmand4c45432010-09-01 14:20:41 +0000420 formatted_raw_ostream FOS(Out->os());
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000421 OwningPtr<MCStreamer> Str;
Chris Lattnerf3ce0092009-08-17 04:23:44 +0000422
Evan Cheng59ee62d2011-07-11 03:57:24 +0000423 OwningPtr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo());
Evan Chengffc0e732011-07-09 05:47:46 +0000424 OwningPtr<MCSubtargetInfo>
425 STI(TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr));
426
Kevin Enderby9823ca92009-09-04 21:45:34 +0000427 if (FileType == OFT_AssemblyFile) {
Chris Lattner4c42a6d2010-03-19 05:48:53 +0000428 MCInstPrinter *IP =
Craig Topper17463b32012-04-02 06:09:36 +0000429 TheTarget->createMCInstPrinter(OutputAsmVariant, *MAI, *MCII, *MRI, *STI);
Benjamin Kramer1abcd062010-07-29 17:48:06 +0000430 MCCodeEmitter *CE = 0;
Evan Cheng78c10ee2011-07-25 23:24:55 +0000431 MCAsmBackend *MAB = 0;
Daniel Dunbar745dacc2010-12-16 03:05:59 +0000432 if (ShowEncoding) {
Evan Cheng28c85a82011-07-26 00:42:34 +0000433 CE = TheTarget->createMCCodeEmitter(*MCII, *STI, Ctx);
Evan Cheng78c10ee2011-07-25 23:24:55 +0000434 MAB = TheTarget->createMCAsmBackend(TripleName);
Daniel Dunbar745dacc2010-12-16 03:05:59 +0000435 }
Rafael Espindola89b93722010-12-10 07:39:47 +0000436 Str.reset(TheTarget->createAsmStreamer(Ctx, FOS, /*asmverbose*/true,
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000437 /*useLoc*/ true,
Nick Lewycky44d798d2011-10-17 23:05:28 +0000438 /*useCFI*/ true,
439 /*useDwarfDirectory*/ true,
440 IP, CE, MAB, ShowInst));
Jim Grosbach57920512011-12-05 23:20:14 +0000441
Daniel Dunbar2d9f5d12010-03-23 23:47:12 +0000442 } else if (FileType == OFT_Null) {
443 Str.reset(createNullStreamer(Ctx));
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000444 } else {
445 assert(FileType == OFT_ObjectFile && "Invalid file type!");
Evan Cheng28c85a82011-07-26 00:42:34 +0000446 MCCodeEmitter *CE = TheTarget->createMCCodeEmitter(*MCII, *STI, Ctx);
Evan Cheng78c10ee2011-07-25 23:24:55 +0000447 MCAsmBackend *MAB = TheTarget->createMCAsmBackend(TripleName);
Evan Cheng28c85a82011-07-26 00:42:34 +0000448 Str.reset(TheTarget->createMCObjectStreamer(TripleName, Ctx, *MAB,
449 FOS, CE, RelaxAll,
450 NoExecStack));
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000451 }
Daniel Dunbara0d14262009-06-24 23:30:00 +0000452
Richard Bartond0c478d2012-04-16 11:32:10 +0000453 int Res = 1;
Chris Lattnerb23677e2009-06-21 05:22:37 +0000454 switch (Action) {
Chris Lattner27aa7d22009-06-21 20:16:42 +0000455 case AC_AsLex:
Richard Bartond0c478d2012-04-16 11:32:10 +0000456 Res = AsLexInput(SrcMgr, *MAI, Out.get());
457 break;
Chris Lattnerb23677e2009-06-21 05:22:37 +0000458 case AC_Assemble:
Richard Bartond0c478d2012-04-16 11:32:10 +0000459 Res = AssembleInput(ProgName, TheTarget, SrcMgr, Ctx, *Str, *MAI, *STI);
460 break;
Sean Callananba847da2009-12-17 01:49:59 +0000461 case AC_Disassemble:
Richard Bartond0c478d2012-04-16 11:32:10 +0000462 Res = Disassembler::disassemble(*TheTarget, TripleName, *STI, *Str,
463 *Buffer, SrcMgr, Out->os());
464 break;
Sean Callanan668b1542010-04-12 19:43:00 +0000465 case AC_EDisassemble:
Richard Bartond0c478d2012-04-16 11:32:10 +0000466 Res = Disassembler::disassembleEnhanced(TripleName, *Buffer, SrcMgr, Out->os());
467 break;
Chris Lattnerb23677e2009-06-21 05:22:37 +0000468 }
Richard Bartond0c478d2012-04-16 11:32:10 +0000469
470 // Keep output if no errors.
471 if (Res == 0) Out->keep();
472 return Res;
Chris Lattnerf9f065e2009-06-18 23:04:45 +0000473}