blob: a4b4a844f67818bb19291130f0c9435dfeae0ae2 [file] [log] [blame]
Chris Lattnerc7ab9532009-06-18 23:05:21 +00001//===-- llvm-mc.cpp - Machine Code Hacking Driver -------------------------===//
Chris Lattner8dd8a522009-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 Lattnerc7ab9532009-06-18 23:05:21 +000010// This utility is a simple driver that allows command line hacking on machine
11// code.
Chris Lattner8dd8a522009-06-18 23:04:45 +000012//
13//===----------------------------------------------------------------------===//
14
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000015#include "Disassembler.h"
Evan Cheng5928e692011-07-25 23:24:55 +000016#include "llvm/MC/MCAsmBackend.h"
Craig Toppera6e377f2012-04-15 22:00:22 +000017#include "llvm/MC/MCAsmInfo.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000018#include "llvm/MC/MCContext.h"
Chris Lattner11b2fc92009-09-14 03:02:37 +000019#include "llvm/MC/MCInstPrinter.h"
Evan Chengc5e6d2f2011-07-11 03:57:24 +000020#include "llvm/MC/MCInstrInfo.h"
Evan Cheng76792992011-07-20 05:58:47 +000021#include "llvm/MC/MCObjectFileInfo.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000022#include "llvm/MC/MCParser/AsmLexer.h"
Evan Cheng76792992011-07-20 05:58:47 +000023#include "llvm/MC/MCRegisterInfo.h"
Chris Lattner6c203912009-08-10 18:15:01 +000024#include "llvm/MC/MCSectionMachO.h"
Chris Lattner92ffdd12009-06-24 00:52:40 +000025#include "llvm/MC/MCStreamer.h"
Evan Cheng91111d22011-07-09 05:47:46 +000026#include "llvm/MC/MCSubtargetInfo.h"
Evan Cheng11424442011-07-26 00:24:13 +000027#include "llvm/MC/MCTargetAsmParser.h"
Chris Lattner8dd8a522009-06-18 23:04:45 +000028#include "llvm/Support/CommandLine.h"
Dan Gohman268b0f42010-08-20 01:07:01 +000029#include "llvm/Support/FileUtilities.h"
Daniel Dunbar80d484e2009-08-14 03:48:55 +000030#include "llvm/Support/FormattedStream.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000031#include "llvm/Support/Host.h"
Chris Lattner8dd8a522009-06-18 23:04:45 +000032#include "llvm/Support/ManagedStatic.h"
33#include "llvm/Support/MemoryBuffer.h"
34#include "llvm/Support/PrettyStackTrace.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000035#include "llvm/Support/Signals.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000036#include "llvm/Support/SourceMgr.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000037#include "llvm/Support/TargetRegistry.h"
38#include "llvm/Support/TargetSelect.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000039#include "llvm/Support/ToolOutputFile.h"
Chris Lattner8dd8a522009-06-18 23:04:45 +000040using namespace llvm;
41
42static cl::opt<std::string>
43InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
44
45static cl::opt<std::string>
46OutputFilename("o", cl::desc("Output filename"),
47 cl::value_desc("filename"));
48
Daniel Dunbard18dd1d2009-08-27 07:56:39 +000049static cl::opt<bool>
50ShowEncoding("show-encoding", cl::desc("Show instruction encodings"));
51
Daniel Dunbare3ee3322010-02-03 18:18:30 +000052static cl::opt<bool>
53ShowInst("show-inst", cl::desc("Show internal instruction representation"));
54
Daniel Dunbar2eca0252010-08-11 06:37:09 +000055static cl::opt<bool>
56ShowInstOperands("show-inst-operands",
57 cl::desc("Show instructions operands as parsed"));
58
Chris Lattner44790342009-09-20 07:17:49 +000059static cl::opt<unsigned>
60OutputAsmVariant("output-asm-variant",
61 cl::desc("Syntax variant to use for output printing"));
62
Daniel Dunbard821f4a2010-03-25 22:49:09 +000063static cl::opt<bool>
64RelaxAll("mc-relax-all", cl::desc("Relax all fixups"));
65
Daniel Dunbar3ff1a062010-05-23 17:44:06 +000066static cl::opt<bool>
Rafael Espindolab3eca9b2011-01-23 17:55:27 +000067NoExecStack("mc-no-exec-stack", cl::desc("File doesn't need an exec stack"));
68
Daniel Dunbar3016db32009-08-21 09:11:24 +000069enum OutputFileType {
Daniel Dunbarb09b8902010-03-23 23:47:12 +000070 OFT_Null,
Daniel Dunbar3016db32009-08-21 09:11:24 +000071 OFT_AssemblyFile,
72 OFT_ObjectFile
73};
74static cl::opt<OutputFileType>
75FileType("filetype", cl::init(OFT_AssemblyFile),
76 cl::desc("Choose an output file type:"),
77 cl::values(
78 clEnumValN(OFT_AssemblyFile, "asm",
79 "Emit an assembly ('.s') file"),
Daniel Dunbarb09b8902010-03-23 23:47:12 +000080 clEnumValN(OFT_Null, "null",
81 "Don't emit anything (for timing purposes)"),
Daniel Dunbar3016db32009-08-21 09:11:24 +000082 clEnumValN(OFT_ObjectFile, "obj",
83 "Emit a native object ('.o') file"),
84 clEnumValEnd));
85
Chris Lattnerd70e15b42009-06-21 05:22:37 +000086static cl::list<std::string>
87IncludeDirs("I", cl::desc("Directory of include files"),
88 cl::value_desc("directory"), cl::Prefix);
Chris Lattner8dd8a522009-06-18 23:04:45 +000089
Daniel Dunbar8c6bad22009-07-17 22:38:58 +000090static cl::opt<std::string>
Daniel Dunbar46892112010-03-13 02:20:38 +000091ArchName("arch", cl::desc("Target arch to assemble for, "
Bill Wendlinge3031142011-06-17 20:35:21 +000092 "see -version for available targets"));
Daniel Dunbar46892112010-03-13 02:20:38 +000093
94static cl::opt<std::string>
Nick Lewyckyb2449092009-11-01 22:07:54 +000095TripleName("triple", cl::desc("Target triple to assemble for, "
Daniel Dunbar46892112010-03-13 02:20:38 +000096 "see -version for available targets"));
Daniel Dunbar8c6bad22009-07-17 22:38:58 +000097
Jim Grosbach685b7732010-10-30 15:57:50 +000098static cl::opt<std::string>
99MCPU("mcpu",
100 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
101 cl::value_desc("cpu-name"),
102 cl::init(""));
103
James Molloy4c493e82011-09-07 17:24:38 +0000104static cl::list<std::string>
105MAttrs("mattr",
106 cl::CommaSeparated,
107 cl::desc("Target specific attributes (-mattr=help for details)"),
108 cl::value_desc("a1,+a2,-a3,..."));
109
Evan Cheng2129f592011-07-19 06:37:02 +0000110static cl::opt<Reloc::Model>
111RelocModel("relocation-model",
112 cl::desc("Choose relocation model"),
113 cl::init(Reloc::Default),
114 cl::values(
115 clEnumValN(Reloc::Default, "default",
116 "Target default relocation model"),
117 clEnumValN(Reloc::Static, "static",
118 "Non-relocatable code"),
119 clEnumValN(Reloc::PIC_, "pic",
120 "Fully relocatable, position independent code"),
121 clEnumValN(Reloc::DynamicNoPIC, "dynamic-no-pic",
122 "Relocatable external references, non-relocatable code"),
123 clEnumValEnd));
124
Evan Chengefd9b422011-07-20 07:51:56 +0000125static cl::opt<llvm::CodeModel::Model>
126CMModel("code-model",
127 cl::desc("Choose code model"),
128 cl::init(CodeModel::Default),
129 cl::values(clEnumValN(CodeModel::Default, "default",
130 "Target default code model"),
131 clEnumValN(CodeModel::Small, "small",
132 "Small code model"),
133 clEnumValN(CodeModel::Kernel, "kernel",
134 "Kernel code model"),
135 clEnumValN(CodeModel::Medium, "medium",
136 "Medium code model"),
137 clEnumValN(CodeModel::Large, "large",
138 "Large code model"),
139 clEnumValEnd));
140
Daniel Dunbar322fec62010-03-13 02:20:57 +0000141static cl::opt<bool>
Bill Wendlinge3031142011-06-17 20:35:21 +0000142NoInitialTextSection("n", cl::desc("Don't assume assembly file starts "
143 "in the text section"));
Daniel Dunbar322fec62010-03-13 02:20:57 +0000144
Daniel Dunbar4ee0d032011-03-28 22:49:15 +0000145static cl::opt<bool>
Bill Wendlinge3031142011-06-17 20:35:21 +0000146SaveTempLabels("L", cl::desc("Don't discard temporary labels"));
Daniel Dunbar4ee0d032011-03-28 22:49:15 +0000147
Kevin Enderby6469fc22011-11-01 22:27:22 +0000148static cl::opt<bool>
149GenDwarfForAssembly("g", cl::desc("Generate dwarf debugging info for assembly "
150 "source files"));
151
Chandler Carruth10700aad2012-12-17 21:32:42 +0000152static cl::opt<std::string>
153DebugCompilationDir("fdebug-compilation-dir",
154 cl::desc("Specifies the debug info's compilation dir"));
155
Eric Christopher906da232012-12-18 00:31:01 +0000156static cl::opt<std::string>
157MainFileName("main-file-name",
158 cl::desc("Specifies the name we should consider the input file"));
159
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000160enum ActionType {
Chris Lattnerb0133452009-06-21 20:16:42 +0000161 AC_AsLex,
Sean Callanan7e645502009-12-17 01:49:59 +0000162 AC_Assemble,
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000163 AC_Disassemble,
Kevin Enderby168ffb32012-12-05 18:13:19 +0000164 AC_MDisassemble,
165 AC_HDisassemble
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000166};
Chris Lattner8dd8a522009-06-18 23:04:45 +0000167
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000168static cl::opt<ActionType>
169Action(cl::desc("Action to perform:"),
Chris Lattnerb0133452009-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 Lattnerd70e15b42009-06-21 05:22:37 +0000174 "Assemble a .s file (default)"),
Sean Callanan7e645502009-12-17 01:49:59 +0000175 clEnumValN(AC_Disassemble, "disassemble",
176 "Disassemble strings of hex bytes"),
Kevin Enderby62183c42012-10-22 22:31:46 +0000177 clEnumValN(AC_MDisassemble, "mdis",
178 "Marked up disassembly of strings of hex bytes"),
Kevin Enderby168ffb32012-12-05 18:13:19 +0000179 clEnumValN(AC_HDisassemble, "hdis",
180 "Disassemble strings of hex bytes printing "
181 "immediates as hex"),
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000182 clEnumValEnd));
183
Kevin Enderbyf92f9902009-09-04 21:45:34 +0000184static const Target *GetTarget(const char *ProgName) {
Daniel Dunbar46892112010-03-13 02:20:38 +0000185 // Figure out the target triple.
186 if (TripleName.empty())
Sebastian Pop94441fb2011-11-01 21:32:20 +0000187 TripleName = sys::getDefaultTargetTriple();
Evan Chenge64f0e52011-07-26 19:02:16 +0000188 Triple TheTriple(Triple::normalize(TripleName));
189
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000190 // Get the target specific parser.
191 std::string Error;
192 const Target *TheTarget = TargetRegistry::lookupTarget(ArchName, TheTriple,
193 Error);
194 if (!TheTarget) {
195 errs() << ProgName << ": " << Error;
196 return 0;
Daniel Dunbar46892112010-03-13 02:20:38 +0000197 }
198
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000199 // Update the triple name and return the found target.
Evan Chenge64f0e52011-07-26 19:02:16 +0000200 TripleName = TheTriple.getTriple();
201 return TheTarget;
Kevin Enderbyf92f9902009-09-04 21:45:34 +0000202}
203
Dan Gohmana2233f22010-09-01 14:20:41 +0000204static tool_output_file *GetOutputStream() {
Dan Gohman268b0f42010-08-20 01:07:01 +0000205 if (OutputFilename == "")
206 OutputFilename = "-";
207
208 std::string Err;
Rafael Espindola6d354812013-07-16 19:44:17 +0000209 tool_output_file *Out =
Rafael Espindola90c7f1c2014-02-24 18:20:12 +0000210 new tool_output_file(OutputFilename.c_str(), Err, sys::fs::F_None);
Dan Gohman268b0f42010-08-20 01:07:01 +0000211 if (!Err.empty()) {
212 errs() << Err << '\n';
213 delete Out;
214 return 0;
215 }
Dan Gohmana2233f22010-09-01 14:20:41 +0000216
217 return Out;
Dan Gohman268b0f42010-08-20 01:07:01 +0000218}
219
Kevin Enderbye7739d42011-12-09 18:09:40 +0000220static std::string DwarfDebugFlags;
221static void setDwarfDebugFlags(int argc, char **argv) {
222 if (!getenv("RC_DEBUG_OPTIONS"))
223 return;
224 for (int i = 0; i < argc; i++) {
225 DwarfDebugFlags += argv[i];
226 if (i + 1 < argc)
227 DwarfDebugFlags += " ";
228 }
229}
230
Kevin Enderbye82ada62013-01-16 17:46:23 +0000231static std::string DwarfDebugProducer;
232static void setDwarfDebugProducer(void) {
233 if(!getenv("DEBUG_PRODUCER"))
234 return;
235 DwarfDebugProducer += getenv("DEBUG_PRODUCER");
236}
237
Richard Bartondef81b92012-04-16 11:32:10 +0000238static int AsLexInput(SourceMgr &SrcMgr, MCAsmInfo &MAI, tool_output_file *Out) {
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000239
Richard Bartondef81b92012-04-16 11:32:10 +0000240 AsmLexer Lexer(MAI);
Daniel Dunbar7a85f9c2010-07-18 18:31:28 +0000241 Lexer.setBuffer(SrcMgr.getMemoryBuffer(0));
242
Chris Lattnerb0133452009-06-21 20:16:42 +0000243 bool Error = false;
Daniel Dunbarbc798162009-07-28 16:56:42 +0000244 while (Lexer.Lex().isNot(AsmToken::Eof)) {
Daniel Dunbar1601f552010-10-25 20:18:46 +0000245 AsmToken Tok = Lexer.getTok();
246
247 switch (Tok.getKind()) {
Chris Lattnerb0133452009-06-21 20:16:42 +0000248 default:
Chris Lattner03b80a42011-10-16 05:43:57 +0000249 SrcMgr.PrintMessage(Lexer.getLoc(), SourceMgr::DK_Warning,
250 "unknown token");
Chris Lattnerb0133452009-06-21 20:16:42 +0000251 Error = true;
252 break;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000253 case AsmToken::Error:
Chris Lattnerb0133452009-06-21 20:16:42 +0000254 Error = true; // error already printed.
Chris Lattnerd0765612009-06-21 19:21:25 +0000255 break;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000256 case AsmToken::Identifier:
Daniel Dunbar1601f552010-10-25 20:18:46 +0000257 Out->os() << "identifier: " << Lexer.getTok().getString();
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +0000258 break;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000259 case AsmToken::Integer:
Daniel Dunbar1601f552010-10-25 20:18:46 +0000260 Out->os() << "int: " << Lexer.getTok().getString();
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +0000261 break;
Daniel Dunbar3068a932010-09-24 01:59:31 +0000262 case AsmToken::Real:
Daniel Dunbar1601f552010-10-25 20:18:46 +0000263 Out->os() << "real: " << Lexer.getTok().getString();
Daniel Dunbar3068a932010-09-24 01:59:31 +0000264 break;
Daniel Dunbar25c2c622010-09-16 00:42:35 +0000265 case AsmToken::String:
Daniel Dunbar1601f552010-10-25 20:18:46 +0000266 Out->os() << "string: " << Lexer.getTok().getString();
Daniel Dunbar25c2c622010-09-16 00:42:35 +0000267 break;
Daniel Dunbar9c4809a2009-07-01 06:56:54 +0000268
Daniel Dunbar1601f552010-10-25 20:18:46 +0000269 case AsmToken::Amp: Out->os() << "Amp"; break;
270 case AsmToken::AmpAmp: Out->os() << "AmpAmp"; break;
271 case AsmToken::At: Out->os() << "At"; break;
272 case AsmToken::Caret: Out->os() << "Caret"; break;
273 case AsmToken::Colon: Out->os() << "Colon"; break;
274 case AsmToken::Comma: Out->os() << "Comma"; break;
275 case AsmToken::Dollar: Out->os() << "Dollar"; break;
276 case AsmToken::Dot: Out->os() << "Dot"; break;
277 case AsmToken::EndOfStatement: Out->os() << "EndOfStatement"; break;
278 case AsmToken::Eof: Out->os() << "Eof"; break;
279 case AsmToken::Equal: Out->os() << "Equal"; break;
280 case AsmToken::EqualEqual: Out->os() << "EqualEqual"; break;
281 case AsmToken::Exclaim: Out->os() << "Exclaim"; break;
282 case AsmToken::ExclaimEqual: Out->os() << "ExclaimEqual"; break;
283 case AsmToken::Greater: Out->os() << "Greater"; break;
284 case AsmToken::GreaterEqual: Out->os() << "GreaterEqual"; break;
285 case AsmToken::GreaterGreater: Out->os() << "GreaterGreater"; break;
286 case AsmToken::Hash: Out->os() << "Hash"; break;
287 case AsmToken::LBrac: Out->os() << "LBrac"; break;
288 case AsmToken::LCurly: Out->os() << "LCurly"; break;
289 case AsmToken::LParen: Out->os() << "LParen"; break;
290 case AsmToken::Less: Out->os() << "Less"; break;
291 case AsmToken::LessEqual: Out->os() << "LessEqual"; break;
292 case AsmToken::LessGreater: Out->os() << "LessGreater"; break;
293 case AsmToken::LessLess: Out->os() << "LessLess"; break;
294 case AsmToken::Minus: Out->os() << "Minus"; break;
295 case AsmToken::Percent: Out->os() << "Percent"; break;
296 case AsmToken::Pipe: Out->os() << "Pipe"; break;
297 case AsmToken::PipePipe: Out->os() << "PipePipe"; break;
298 case AsmToken::Plus: Out->os() << "Plus"; break;
299 case AsmToken::RBrac: Out->os() << "RBrac"; break;
300 case AsmToken::RCurly: Out->os() << "RCurly"; break;
301 case AsmToken::RParen: Out->os() << "RParen"; break;
302 case AsmToken::Slash: Out->os() << "Slash"; break;
303 case AsmToken::Star: Out->os() << "Star"; break;
304 case AsmToken::Tilde: Out->os() << "Tilde"; break;
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +0000305 }
Daniel Dunbar1601f552010-10-25 20:18:46 +0000306
307 // Print the token string.
308 Out->os() << " (\"";
309 Out->os().write_escaped(Tok.getString());
310 Out->os() << "\")\n";
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +0000311 }
Dan Gohman268b0f42010-08-20 01:07:01 +0000312
Chris Lattnerb0133452009-06-21 20:16:42 +0000313 return Error;
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000314}
315
NAKAMURA Takumi89b8c172014-01-22 03:12:43 +0000316static int AssembleInput(const char *ProgName, const Target *TheTarget,
Richard Bartondef81b92012-04-16 11:32:10 +0000317 SourceMgr &SrcMgr, MCContext &Ctx, MCStreamer &Str,
Joey Gouly0e76fa72013-09-12 10:28:05 +0000318 MCAsmInfo &MAI, MCSubtargetInfo &STI, MCInstrInfo &MCII) {
Ahmed Charles56440fd2014-03-06 05:51:42 +0000319 std::unique_ptr<MCAsmParser> Parser(createMCAsmParser(SrcMgr, Ctx, Str, MAI));
320 std::unique_ptr<MCTargetAsmParser> TAP(
321 TheTarget->createMCAsmParser(STI, *Parser, MCII));
Richard Bartondef81b92012-04-16 11:32:10 +0000322 if (!TAP) {
323 errs() << ProgName
324 << ": error: this target does not support assembly parsing.\n";
325 return 1;
326 }
327
328 Parser->setShowParsedOperands(ShowInstOperands);
329 Parser->setTargetParser(*TAP.get());
330
331 int Res = Parser->Run(NoInitialTextSection);
332
333 return Res;
334}
335
336int main(int argc, char **argv) {
337 // Print a stack trace if we signal out.
338 sys::PrintStackTraceOnErrorSignal();
339 PrettyStackTraceProgram X(argc, argv);
340 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
341
342 // Initialize targets and assembly printers/parsers.
343 llvm::InitializeAllTargetInfos();
344 llvm::InitializeAllTargetMCs();
345 llvm::InitializeAllAsmParsers();
346 llvm::InitializeAllDisassemblers();
347
348 // Register the target printer for --version.
349 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
350
351 cl::ParseCommandLineOptions(argc, argv, "llvm machine code playground\n");
352 TripleName = Triple::normalize(TripleName);
353 setDwarfDebugFlags(argc, argv);
354
Kevin Enderbye82ada62013-01-16 17:46:23 +0000355 setDwarfDebugProducer();
356
Richard Bartondef81b92012-04-16 11:32:10 +0000357 const char *ProgName = argv[0];
Daniel Dunbar80d484e2009-08-14 03:48:55 +0000358 const Target *TheTarget = GetTarget(ProgName);
359 if (!TheTarget)
360 return 1;
361
Ahmed Charles56440fd2014-03-06 05:51:42 +0000362 std::unique_ptr<MemoryBuffer> BufferPtr;
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +0000363 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename, BufferPtr)) {
Michael J. Spencer7b6fef82010-12-09 17:36:48 +0000364 errs() << ProgName << ": " << ec.message() << '\n';
Chris Lattnerb0133452009-06-21 20:16:42 +0000365 return 1;
366 }
Ahmed Charles96c9d952014-03-05 10:19:29 +0000367 MemoryBuffer *Buffer = BufferPtr.release();
Jim Grosbach112a2de2011-05-09 20:05:25 +0000368
Chris Lattnerb0133452009-06-21 20:16:42 +0000369 SourceMgr SrcMgr;
Jim Grosbach112a2de2011-05-09 20:05:25 +0000370
Daniel Dunbar5c947db2009-08-19 16:25:53 +0000371 // Tell SrcMgr about this buffer, which is what the parser will pick up.
Chris Lattnerb0133452009-06-21 20:16:42 +0000372 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
Jim Grosbach112a2de2011-05-09 20:05:25 +0000373
Chris Lattnerb0133452009-06-21 20:16:42 +0000374 // Record the location of the include directories so that the lexer can find
375 // it later.
376 SrcMgr.setIncludeDirs(IncludeDirs);
Jim Grosbach112a2de2011-05-09 20:05:25 +0000377
Ahmed Charles56440fd2014-03-06 05:51:42 +0000378 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
Evan Chengd60fa58b2011-07-18 20:57:22 +0000379 assert(MRI && "Unable to create target register info!");
380
Ahmed Charles56440fd2014-03-06 05:51:42 +0000381 std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName));
Rafael Espindola227144c2013-05-13 01:16:13 +0000382 assert(MAI && "Unable to create target asm info!");
383
Evan Cheng76792992011-07-20 05:58:47 +0000384 // FIXME: This is not pretty. MCContext has a ptr to MCObjectFileInfo and
385 // MCObjectFileInfo needs a MCContext reference in order to initialize itself.
Ahmed Charles56440fd2014-03-06 05:51:42 +0000386 std::unique_ptr<MCObjectFileInfo> MOFI(new MCObjectFileInfo());
Bill Wendlingbc07a892013-06-18 07:20:20 +0000387 MCContext Ctx(MAI.get(), MRI.get(), MOFI.get(), &SrcMgr);
Evan Chengbbf3b0d2011-07-20 19:50:42 +0000388 MOFI->InitMCObjectFileInfo(TripleName, RelocModel, CMModel, Ctx);
Evan Cheng76792992011-07-20 05:58:47 +0000389
Daniel Dunbar4ee0d032011-03-28 22:49:15 +0000390 if (SaveTempLabels)
391 Ctx.setAllowTemporaryLabels(false);
Rafael Espindola0a017a62010-12-10 07:39:47 +0000392
Kevin Enderby6469fc22011-11-01 22:27:22 +0000393 Ctx.setGenDwarfForAssembly(GenDwarfForAssembly);
Chandler Carruth10700aad2012-12-17 21:32:42 +0000394 if (!DwarfDebugFlags.empty())
Kevin Enderbye7739d42011-12-09 18:09:40 +0000395 Ctx.setDwarfDebugFlags(StringRef(DwarfDebugFlags));
Kevin Enderbye82ada62013-01-16 17:46:23 +0000396 if (!DwarfDebugProducer.empty())
397 Ctx.setDwarfDebugProducer(StringRef(DwarfDebugProducer));
Chandler Carruth10700aad2012-12-17 21:32:42 +0000398 if (!DebugCompilationDir.empty())
399 Ctx.setCompilationDir(DebugCompilationDir);
Eric Christopher906da232012-12-18 00:31:01 +0000400 if (!MainFileName.empty())
401 Ctx.setMainFileName(MainFileName);
Kevin Enderby6469fc22011-11-01 22:27:22 +0000402
James Molloy4c493e82011-09-07 17:24:38 +0000403 // Package up features to be passed to target/subtarget
404 std::string FeaturesStr;
405 if (MAttrs.size()) {
406 SubtargetFeatures Features;
407 for (unsigned i = 0; i != MAttrs.size(); ++i)
408 Features.AddFeature(MAttrs[i]);
409 FeaturesStr = Features.getString();
410 }
411
Ahmed Charles56440fd2014-03-06 05:51:42 +0000412 std::unique_ptr<tool_output_file> Out(GetOutputStream());
Dan Gohman268b0f42010-08-20 01:07:01 +0000413 if (!Out)
414 return 1;
415
Dan Gohmana2233f22010-09-01 14:20:41 +0000416 formatted_raw_ostream FOS(Out->os());
Ahmed Charles56440fd2014-03-06 05:51:42 +0000417 std::unique_ptr<MCStreamer> Str;
Chris Lattnera61e93d2009-08-17 04:23:44 +0000418
Ahmed Charles56440fd2014-03-06 05:51:42 +0000419 std::unique_ptr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo());
420 std::unique_ptr<MCSubtargetInfo> STI(
421 TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr));
Evan Cheng91111d22011-07-09 05:47:46 +0000422
Eli Bendersky6f5d70e2013-02-26 23:04:17 +0000423 MCInstPrinter *IP = NULL;
Kevin Enderbyf92f9902009-09-04 21:45:34 +0000424 if (FileType == OFT_AssemblyFile) {
Kevin Enderby62183c42012-10-22 22:31:46 +0000425 IP =
Craig Topper54bfde72012-04-02 06:09:36 +0000426 TheTarget->createMCInstPrinter(OutputAsmVariant, *MAI, *MCII, *MRI, *STI);
Benjamin Kramera3e0ddb2010-07-29 17:48:06 +0000427 MCCodeEmitter *CE = 0;
Evan Cheng5928e692011-07-25 23:24:55 +0000428 MCAsmBackend *MAB = 0;
Daniel Dunbarecd0c8a2010-12-16 03:05:59 +0000429 if (ShowEncoding) {
Jim Grosbachc3b04272012-05-15 17:35:52 +0000430 CE = TheTarget->createMCCodeEmitter(*MCII, *MRI, *STI, Ctx);
Bill Wendling58e2d3d2013-09-09 02:37:14 +0000431 MAB = TheTarget->createMCAsmBackend(*MRI, TripleName, MCPU);
Daniel Dunbarecd0c8a2010-12-16 03:05:59 +0000432 }
Rafael Espindola52845cf2014-03-20 21:48:20 +0000433 Str.reset(TheTarget->createAsmStreamer(Ctx, FOS, /*asmverbose*/ true,
434 /*UseCFI*/ true,
435 /*useDwarfDirectory*/
436 true, IP, CE, MAB, ShowInst));
Jim Grosbach78309c42011-12-05 23:20:14 +0000437
Daniel Dunbarb09b8902010-03-23 23:47:12 +0000438 } else if (FileType == OFT_Null) {
439 Str.reset(createNullStreamer(Ctx));
Daniel Dunbar3016db32009-08-21 09:11:24 +0000440 } else {
441 assert(FileType == OFT_ObjectFile && "Invalid file type!");
Jim Grosbachc3b04272012-05-15 17:35:52 +0000442 MCCodeEmitter *CE = TheTarget->createMCCodeEmitter(*MCII, *MRI, *STI, Ctx);
Bill Wendling58e2d3d2013-09-09 02:37:14 +0000443 MCAsmBackend *MAB = TheTarget->createMCAsmBackend(*MRI, TripleName, MCPU);
Evan Cheng3a792252011-07-26 00:42:34 +0000444 Str.reset(TheTarget->createMCObjectStreamer(TripleName, Ctx, *MAB,
Rafael Espindolae41383f2014-01-26 06:38:58 +0000445 FOS, CE, *STI, RelaxAll,
Evan Cheng3a792252011-07-26 00:42:34 +0000446 NoExecStack));
Daniel Dunbar3016db32009-08-21 09:11:24 +0000447 }
Daniel Dunbara10e5192009-06-24 23:30:00 +0000448
Richard Bartondef81b92012-04-16 11:32:10 +0000449 int Res = 1;
Kevin Enderby168ffb32012-12-05 18:13:19 +0000450 bool disassemble = false;
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000451 switch (Action) {
Chris Lattnerb0133452009-06-21 20:16:42 +0000452 case AC_AsLex:
Richard Bartondef81b92012-04-16 11:32:10 +0000453 Res = AsLexInput(SrcMgr, *MAI, Out.get());
454 break;
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000455 case AC_Assemble:
Joey Gouly0e76fa72013-09-12 10:28:05 +0000456 Res = AssembleInput(ProgName, TheTarget, SrcMgr, Ctx, *Str, *MAI, *STI, *MCII);
Richard Bartondef81b92012-04-16 11:32:10 +0000457 break;
Kevin Enderby62183c42012-10-22 22:31:46 +0000458 case AC_MDisassemble:
Eli Bendersky6f5d70e2013-02-26 23:04:17 +0000459 assert(IP && "Expected assembly output");
Kevin Enderby62183c42012-10-22 22:31:46 +0000460 IP->setUseMarkup(1);
Kevin Enderby168ffb32012-12-05 18:13:19 +0000461 disassemble = true;
462 break;
463 case AC_HDisassemble:
Eli Bendersky6f5d70e2013-02-26 23:04:17 +0000464 assert(IP && "Expected assembly output");
Kevin Enderby168ffb32012-12-05 18:13:19 +0000465 IP->setPrintImmHex(1);
466 disassemble = true;
467 break;
Sean Callanan7e645502009-12-17 01:49:59 +0000468 case AC_Disassemble:
Kevin Enderby168ffb32012-12-05 18:13:19 +0000469 disassemble = true;
Richard Bartondef81b92012-04-16 11:32:10 +0000470 break;
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000471 }
Kevin Enderby168ffb32012-12-05 18:13:19 +0000472 if (disassemble)
473 Res = Disassembler::disassemble(*TheTarget, TripleName, *STI, *Str,
474 *Buffer, SrcMgr, Out->os());
Richard Bartondef81b92012-04-16 11:32:10 +0000475
476 // Keep output if no errors.
477 if (Res == 0) Out->keep();
478 return Res;
Chris Lattner8dd8a522009-06-18 23:04:45 +0000479}