blob: 3e76f35dc62c925f5a22bc0805cf71fc1e3569ad [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 Espindolab3b60572012-11-23 17:37:34 +000067DisableCFI("disable-cfi", cl::desc("Do not use .cfi_* directives"));
68
69static cl::opt<bool>
Rafael Espindolab3eca9b2011-01-23 17:55:27 +000070NoExecStack("mc-no-exec-stack", cl::desc("File doesn't need an exec stack"));
71
Daniel Dunbar3016db32009-08-21 09:11:24 +000072enum OutputFileType {
Daniel Dunbarb09b8902010-03-23 23:47:12 +000073 OFT_Null,
Daniel Dunbar3016db32009-08-21 09:11:24 +000074 OFT_AssemblyFile,
75 OFT_ObjectFile
76};
77static cl::opt<OutputFileType>
78FileType("filetype", cl::init(OFT_AssemblyFile),
79 cl::desc("Choose an output file type:"),
80 cl::values(
81 clEnumValN(OFT_AssemblyFile, "asm",
82 "Emit an assembly ('.s') file"),
Daniel Dunbarb09b8902010-03-23 23:47:12 +000083 clEnumValN(OFT_Null, "null",
84 "Don't emit anything (for timing purposes)"),
Daniel Dunbar3016db32009-08-21 09:11:24 +000085 clEnumValN(OFT_ObjectFile, "obj",
86 "Emit a native object ('.o') file"),
87 clEnumValEnd));
88
Chris Lattnerd70e15b42009-06-21 05:22:37 +000089static cl::list<std::string>
90IncludeDirs("I", cl::desc("Directory of include files"),
91 cl::value_desc("directory"), cl::Prefix);
Chris Lattner8dd8a522009-06-18 23:04:45 +000092
Daniel Dunbar8c6bad22009-07-17 22:38:58 +000093static cl::opt<std::string>
Daniel Dunbar46892112010-03-13 02:20:38 +000094ArchName("arch", cl::desc("Target arch to assemble for, "
Bill Wendlinge3031142011-06-17 20:35:21 +000095 "see -version for available targets"));
Daniel Dunbar46892112010-03-13 02:20:38 +000096
97static cl::opt<std::string>
Nick Lewyckyb2449092009-11-01 22:07:54 +000098TripleName("triple", cl::desc("Target triple to assemble for, "
Daniel Dunbar46892112010-03-13 02:20:38 +000099 "see -version for available targets"));
Daniel Dunbar8c6bad22009-07-17 22:38:58 +0000100
Jim Grosbach685b7732010-10-30 15:57:50 +0000101static cl::opt<std::string>
102MCPU("mcpu",
103 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
104 cl::value_desc("cpu-name"),
105 cl::init(""));
106
James Molloy4c493e82011-09-07 17:24:38 +0000107static cl::list<std::string>
108MAttrs("mattr",
109 cl::CommaSeparated,
110 cl::desc("Target specific attributes (-mattr=help for details)"),
111 cl::value_desc("a1,+a2,-a3,..."));
112
Evan Cheng2129f592011-07-19 06:37:02 +0000113static cl::opt<Reloc::Model>
114RelocModel("relocation-model",
115 cl::desc("Choose relocation model"),
116 cl::init(Reloc::Default),
117 cl::values(
118 clEnumValN(Reloc::Default, "default",
119 "Target default relocation model"),
120 clEnumValN(Reloc::Static, "static",
121 "Non-relocatable code"),
122 clEnumValN(Reloc::PIC_, "pic",
123 "Fully relocatable, position independent code"),
124 clEnumValN(Reloc::DynamicNoPIC, "dynamic-no-pic",
125 "Relocatable external references, non-relocatable code"),
126 clEnumValEnd));
127
Evan Chengefd9b422011-07-20 07:51:56 +0000128static cl::opt<llvm::CodeModel::Model>
129CMModel("code-model",
130 cl::desc("Choose code model"),
131 cl::init(CodeModel::Default),
132 cl::values(clEnumValN(CodeModel::Default, "default",
133 "Target default code model"),
134 clEnumValN(CodeModel::Small, "small",
135 "Small code model"),
136 clEnumValN(CodeModel::Kernel, "kernel",
137 "Kernel code model"),
138 clEnumValN(CodeModel::Medium, "medium",
139 "Medium code model"),
140 clEnumValN(CodeModel::Large, "large",
141 "Large code model"),
142 clEnumValEnd));
143
Daniel Dunbar322fec62010-03-13 02:20:57 +0000144static cl::opt<bool>
Bill Wendlinge3031142011-06-17 20:35:21 +0000145NoInitialTextSection("n", cl::desc("Don't assume assembly file starts "
146 "in the text section"));
Daniel Dunbar322fec62010-03-13 02:20:57 +0000147
Daniel Dunbar4ee0d032011-03-28 22:49:15 +0000148static cl::opt<bool>
Bill Wendlinge3031142011-06-17 20:35:21 +0000149SaveTempLabels("L", cl::desc("Don't discard temporary labels"));
Daniel Dunbar4ee0d032011-03-28 22:49:15 +0000150
Kevin Enderby6469fc22011-11-01 22:27:22 +0000151static cl::opt<bool>
152GenDwarfForAssembly("g", cl::desc("Generate dwarf debugging info for assembly "
153 "source files"));
154
Chandler Carruth10700aad2012-12-17 21:32:42 +0000155static cl::opt<std::string>
156DebugCompilationDir("fdebug-compilation-dir",
157 cl::desc("Specifies the debug info's compilation dir"));
158
Eric Christopher906da232012-12-18 00:31:01 +0000159static cl::opt<std::string>
160MainFileName("main-file-name",
161 cl::desc("Specifies the name we should consider the input file"));
162
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000163enum ActionType {
Chris Lattnerb0133452009-06-21 20:16:42 +0000164 AC_AsLex,
Sean Callanan7e645502009-12-17 01:49:59 +0000165 AC_Assemble,
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000166 AC_Disassemble,
Kevin Enderby168ffb32012-12-05 18:13:19 +0000167 AC_MDisassemble,
168 AC_HDisassemble
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000169};
Chris Lattner8dd8a522009-06-18 23:04:45 +0000170
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000171static cl::opt<ActionType>
172Action(cl::desc("Action to perform:"),
Chris Lattnerb0133452009-06-21 20:16:42 +0000173 cl::init(AC_Assemble),
174 cl::values(clEnumValN(AC_AsLex, "as-lex",
175 "Lex tokens from a .s file"),
176 clEnumValN(AC_Assemble, "assemble",
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000177 "Assemble a .s file (default)"),
Sean Callanan7e645502009-12-17 01:49:59 +0000178 clEnumValN(AC_Disassemble, "disassemble",
179 "Disassemble strings of hex bytes"),
Kevin Enderby62183c42012-10-22 22:31:46 +0000180 clEnumValN(AC_MDisassemble, "mdis",
181 "Marked up disassembly of strings of hex bytes"),
Kevin Enderby168ffb32012-12-05 18:13:19 +0000182 clEnumValN(AC_HDisassemble, "hdis",
183 "Disassemble strings of hex bytes printing "
184 "immediates as hex"),
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000185 clEnumValEnd));
186
Kevin Enderbyf92f9902009-09-04 21:45:34 +0000187static const Target *GetTarget(const char *ProgName) {
Daniel Dunbar46892112010-03-13 02:20:38 +0000188 // Figure out the target triple.
189 if (TripleName.empty())
Sebastian Pop94441fb2011-11-01 21:32:20 +0000190 TripleName = sys::getDefaultTargetTriple();
Evan Chenge64f0e52011-07-26 19:02:16 +0000191 Triple TheTriple(Triple::normalize(TripleName));
192
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000193 // Get the target specific parser.
194 std::string Error;
195 const Target *TheTarget = TargetRegistry::lookupTarget(ArchName, TheTriple,
196 Error);
197 if (!TheTarget) {
198 errs() << ProgName << ": " << Error;
199 return 0;
Daniel Dunbar46892112010-03-13 02:20:38 +0000200 }
201
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000202 // Update the triple name and return the found target.
Evan Chenge64f0e52011-07-26 19:02:16 +0000203 TripleName = TheTriple.getTriple();
204 return TheTarget;
Kevin Enderbyf92f9902009-09-04 21:45:34 +0000205}
206
Dan Gohmana2233f22010-09-01 14:20:41 +0000207static tool_output_file *GetOutputStream() {
Dan Gohman268b0f42010-08-20 01:07:01 +0000208 if (OutputFilename == "")
209 OutputFilename = "-";
210
211 std::string Err;
Rafael Espindola6d354812013-07-16 19:44:17 +0000212 tool_output_file *Out =
Rafael Espindola90c7f1c2014-02-24 18:20:12 +0000213 new tool_output_file(OutputFilename.c_str(), Err, sys::fs::F_None);
Dan Gohman268b0f42010-08-20 01:07:01 +0000214 if (!Err.empty()) {
215 errs() << Err << '\n';
216 delete Out;
217 return 0;
218 }
Dan Gohmana2233f22010-09-01 14:20:41 +0000219
220 return Out;
Dan Gohman268b0f42010-08-20 01:07:01 +0000221}
222
Kevin Enderbye7739d42011-12-09 18:09:40 +0000223static std::string DwarfDebugFlags;
224static void setDwarfDebugFlags(int argc, char **argv) {
225 if (!getenv("RC_DEBUG_OPTIONS"))
226 return;
227 for (int i = 0; i < argc; i++) {
228 DwarfDebugFlags += argv[i];
229 if (i + 1 < argc)
230 DwarfDebugFlags += " ";
231 }
232}
233
Kevin Enderbye82ada62013-01-16 17:46:23 +0000234static std::string DwarfDebugProducer;
235static void setDwarfDebugProducer(void) {
236 if(!getenv("DEBUG_PRODUCER"))
237 return;
238 DwarfDebugProducer += getenv("DEBUG_PRODUCER");
239}
240
Richard Bartondef81b92012-04-16 11:32:10 +0000241static int AsLexInput(SourceMgr &SrcMgr, MCAsmInfo &MAI, tool_output_file *Out) {
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000242
Richard Bartondef81b92012-04-16 11:32:10 +0000243 AsmLexer Lexer(MAI);
Daniel Dunbar7a85f9c2010-07-18 18:31:28 +0000244 Lexer.setBuffer(SrcMgr.getMemoryBuffer(0));
245
Chris Lattnerb0133452009-06-21 20:16:42 +0000246 bool Error = false;
Daniel Dunbarbc798162009-07-28 16:56:42 +0000247 while (Lexer.Lex().isNot(AsmToken::Eof)) {
Daniel Dunbar1601f552010-10-25 20:18:46 +0000248 AsmToken Tok = Lexer.getTok();
249
250 switch (Tok.getKind()) {
Chris Lattnerb0133452009-06-21 20:16:42 +0000251 default:
Chris Lattner03b80a42011-10-16 05:43:57 +0000252 SrcMgr.PrintMessage(Lexer.getLoc(), SourceMgr::DK_Warning,
253 "unknown token");
Chris Lattnerb0133452009-06-21 20:16:42 +0000254 Error = true;
255 break;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000256 case AsmToken::Error:
Chris Lattnerb0133452009-06-21 20:16:42 +0000257 Error = true; // error already printed.
Chris Lattnerd0765612009-06-21 19:21:25 +0000258 break;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000259 case AsmToken::Identifier:
Daniel Dunbar1601f552010-10-25 20:18:46 +0000260 Out->os() << "identifier: " << Lexer.getTok().getString();
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +0000261 break;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000262 case AsmToken::Integer:
Daniel Dunbar1601f552010-10-25 20:18:46 +0000263 Out->os() << "int: " << Lexer.getTok().getString();
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +0000264 break;
Daniel Dunbar3068a932010-09-24 01:59:31 +0000265 case AsmToken::Real:
Daniel Dunbar1601f552010-10-25 20:18:46 +0000266 Out->os() << "real: " << Lexer.getTok().getString();
Daniel Dunbar3068a932010-09-24 01:59:31 +0000267 break;
Daniel Dunbar25c2c622010-09-16 00:42:35 +0000268 case AsmToken::String:
Daniel Dunbar1601f552010-10-25 20:18:46 +0000269 Out->os() << "string: " << Lexer.getTok().getString();
Daniel Dunbar25c2c622010-09-16 00:42:35 +0000270 break;
Daniel Dunbar9c4809a2009-07-01 06:56:54 +0000271
Daniel Dunbar1601f552010-10-25 20:18:46 +0000272 case AsmToken::Amp: Out->os() << "Amp"; break;
273 case AsmToken::AmpAmp: Out->os() << "AmpAmp"; break;
274 case AsmToken::At: Out->os() << "At"; break;
275 case AsmToken::Caret: Out->os() << "Caret"; break;
276 case AsmToken::Colon: Out->os() << "Colon"; break;
277 case AsmToken::Comma: Out->os() << "Comma"; break;
278 case AsmToken::Dollar: Out->os() << "Dollar"; break;
279 case AsmToken::Dot: Out->os() << "Dot"; break;
280 case AsmToken::EndOfStatement: Out->os() << "EndOfStatement"; break;
281 case AsmToken::Eof: Out->os() << "Eof"; break;
282 case AsmToken::Equal: Out->os() << "Equal"; break;
283 case AsmToken::EqualEqual: Out->os() << "EqualEqual"; break;
284 case AsmToken::Exclaim: Out->os() << "Exclaim"; break;
285 case AsmToken::ExclaimEqual: Out->os() << "ExclaimEqual"; break;
286 case AsmToken::Greater: Out->os() << "Greater"; break;
287 case AsmToken::GreaterEqual: Out->os() << "GreaterEqual"; break;
288 case AsmToken::GreaterGreater: Out->os() << "GreaterGreater"; break;
289 case AsmToken::Hash: Out->os() << "Hash"; break;
290 case AsmToken::LBrac: Out->os() << "LBrac"; break;
291 case AsmToken::LCurly: Out->os() << "LCurly"; break;
292 case AsmToken::LParen: Out->os() << "LParen"; break;
293 case AsmToken::Less: Out->os() << "Less"; break;
294 case AsmToken::LessEqual: Out->os() << "LessEqual"; break;
295 case AsmToken::LessGreater: Out->os() << "LessGreater"; break;
296 case AsmToken::LessLess: Out->os() << "LessLess"; break;
297 case AsmToken::Minus: Out->os() << "Minus"; break;
298 case AsmToken::Percent: Out->os() << "Percent"; break;
299 case AsmToken::Pipe: Out->os() << "Pipe"; break;
300 case AsmToken::PipePipe: Out->os() << "PipePipe"; break;
301 case AsmToken::Plus: Out->os() << "Plus"; break;
302 case AsmToken::RBrac: Out->os() << "RBrac"; break;
303 case AsmToken::RCurly: Out->os() << "RCurly"; break;
304 case AsmToken::RParen: Out->os() << "RParen"; break;
305 case AsmToken::Slash: Out->os() << "Slash"; break;
306 case AsmToken::Star: Out->os() << "Star"; break;
307 case AsmToken::Tilde: Out->os() << "Tilde"; break;
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +0000308 }
Daniel Dunbar1601f552010-10-25 20:18:46 +0000309
310 // Print the token string.
311 Out->os() << " (\"";
312 Out->os().write_escaped(Tok.getString());
313 Out->os() << "\")\n";
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +0000314 }
Dan Gohman268b0f42010-08-20 01:07:01 +0000315
Chris Lattnerb0133452009-06-21 20:16:42 +0000316 return Error;
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000317}
318
NAKAMURA Takumi89b8c172014-01-22 03:12:43 +0000319static int AssembleInput(const char *ProgName, const Target *TheTarget,
Richard Bartondef81b92012-04-16 11:32:10 +0000320 SourceMgr &SrcMgr, MCContext &Ctx, MCStreamer &Str,
Joey Gouly0e76fa72013-09-12 10:28:05 +0000321 MCAsmInfo &MAI, MCSubtargetInfo &STI, MCInstrInfo &MCII) {
Ahmed Charles56440fd2014-03-06 05:51:42 +0000322 std::unique_ptr<MCAsmParser> Parser(createMCAsmParser(SrcMgr, Ctx, Str, MAI));
323 std::unique_ptr<MCTargetAsmParser> TAP(
324 TheTarget->createMCAsmParser(STI, *Parser, MCII));
Richard Bartondef81b92012-04-16 11:32:10 +0000325 if (!TAP) {
326 errs() << ProgName
327 << ": error: this target does not support assembly parsing.\n";
328 return 1;
329 }
330
331 Parser->setShowParsedOperands(ShowInstOperands);
332 Parser->setTargetParser(*TAP.get());
333
334 int Res = Parser->Run(NoInitialTextSection);
335
336 return Res;
337}
338
339int main(int argc, char **argv) {
340 // Print a stack trace if we signal out.
341 sys::PrintStackTraceOnErrorSignal();
342 PrettyStackTraceProgram X(argc, argv);
343 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
344
345 // Initialize targets and assembly printers/parsers.
346 llvm::InitializeAllTargetInfos();
347 llvm::InitializeAllTargetMCs();
348 llvm::InitializeAllAsmParsers();
349 llvm::InitializeAllDisassemblers();
350
351 // Register the target printer for --version.
352 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
353
354 cl::ParseCommandLineOptions(argc, argv, "llvm machine code playground\n");
355 TripleName = Triple::normalize(TripleName);
356 setDwarfDebugFlags(argc, argv);
357
Kevin Enderbye82ada62013-01-16 17:46:23 +0000358 setDwarfDebugProducer();
359
Richard Bartondef81b92012-04-16 11:32:10 +0000360 const char *ProgName = argv[0];
Daniel Dunbar80d484e2009-08-14 03:48:55 +0000361 const Target *TheTarget = GetTarget(ProgName);
362 if (!TheTarget)
363 return 1;
364
Ahmed Charles56440fd2014-03-06 05:51:42 +0000365 std::unique_ptr<MemoryBuffer> BufferPtr;
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +0000366 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename, BufferPtr)) {
Michael J. Spencer7b6fef82010-12-09 17:36:48 +0000367 errs() << ProgName << ": " << ec.message() << '\n';
Chris Lattnerb0133452009-06-21 20:16:42 +0000368 return 1;
369 }
Ahmed Charles96c9d952014-03-05 10:19:29 +0000370 MemoryBuffer *Buffer = BufferPtr.release();
Jim Grosbach112a2de2011-05-09 20:05:25 +0000371
Chris Lattnerb0133452009-06-21 20:16:42 +0000372 SourceMgr SrcMgr;
Jim Grosbach112a2de2011-05-09 20:05:25 +0000373
Daniel Dunbar5c947db2009-08-19 16:25:53 +0000374 // Tell SrcMgr about this buffer, which is what the parser will pick up.
Chris Lattnerb0133452009-06-21 20:16:42 +0000375 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
Jim Grosbach112a2de2011-05-09 20:05:25 +0000376
Chris Lattnerb0133452009-06-21 20:16:42 +0000377 // Record the location of the include directories so that the lexer can find
378 // it later.
379 SrcMgr.setIncludeDirs(IncludeDirs);
Jim Grosbach112a2de2011-05-09 20:05:25 +0000380
Ahmed Charles56440fd2014-03-06 05:51:42 +0000381 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
Evan Chengd60fa58b2011-07-18 20:57:22 +0000382 assert(MRI && "Unable to create target register info!");
383
Ahmed Charles56440fd2014-03-06 05:51:42 +0000384 std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName));
Rafael Espindola227144c2013-05-13 01:16:13 +0000385 assert(MAI && "Unable to create target asm info!");
386
Evan Cheng76792992011-07-20 05:58:47 +0000387 // FIXME: This is not pretty. MCContext has a ptr to MCObjectFileInfo and
388 // MCObjectFileInfo needs a MCContext reference in order to initialize itself.
Ahmed Charles56440fd2014-03-06 05:51:42 +0000389 std::unique_ptr<MCObjectFileInfo> MOFI(new MCObjectFileInfo());
Bill Wendlingbc07a892013-06-18 07:20:20 +0000390 MCContext Ctx(MAI.get(), MRI.get(), MOFI.get(), &SrcMgr);
Evan Chengbbf3b0d2011-07-20 19:50:42 +0000391 MOFI->InitMCObjectFileInfo(TripleName, RelocModel, CMModel, Ctx);
Evan Cheng76792992011-07-20 05:58:47 +0000392
Daniel Dunbar4ee0d032011-03-28 22:49:15 +0000393 if (SaveTempLabels)
394 Ctx.setAllowTemporaryLabels(false);
Rafael Espindola0a017a62010-12-10 07:39:47 +0000395
Kevin Enderby6469fc22011-11-01 22:27:22 +0000396 Ctx.setGenDwarfForAssembly(GenDwarfForAssembly);
Chandler Carruth10700aad2012-12-17 21:32:42 +0000397 if (!DwarfDebugFlags.empty())
Kevin Enderbye7739d42011-12-09 18:09:40 +0000398 Ctx.setDwarfDebugFlags(StringRef(DwarfDebugFlags));
Kevin Enderbye82ada62013-01-16 17:46:23 +0000399 if (!DwarfDebugProducer.empty())
400 Ctx.setDwarfDebugProducer(StringRef(DwarfDebugProducer));
Chandler Carruth10700aad2012-12-17 21:32:42 +0000401 if (!DebugCompilationDir.empty())
402 Ctx.setCompilationDir(DebugCompilationDir);
Eric Christopher906da232012-12-18 00:31:01 +0000403 if (!MainFileName.empty())
404 Ctx.setMainFileName(MainFileName);
Kevin Enderby6469fc22011-11-01 22:27:22 +0000405
James Molloy4c493e82011-09-07 17:24:38 +0000406 // Package up features to be passed to target/subtarget
407 std::string FeaturesStr;
408 if (MAttrs.size()) {
409 SubtargetFeatures Features;
410 for (unsigned i = 0; i != MAttrs.size(); ++i)
411 Features.AddFeature(MAttrs[i]);
412 FeaturesStr = Features.getString();
413 }
414
Ahmed Charles56440fd2014-03-06 05:51:42 +0000415 std::unique_ptr<tool_output_file> Out(GetOutputStream());
Dan Gohman268b0f42010-08-20 01:07:01 +0000416 if (!Out)
417 return 1;
418
Dan Gohmana2233f22010-09-01 14:20:41 +0000419 formatted_raw_ostream FOS(Out->os());
Ahmed Charles56440fd2014-03-06 05:51:42 +0000420 std::unique_ptr<MCStreamer> Str;
Chris Lattnera61e93d2009-08-17 04:23:44 +0000421
Ahmed Charles56440fd2014-03-06 05:51:42 +0000422 std::unique_ptr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo());
423 std::unique_ptr<MCSubtargetInfo> STI(
424 TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr));
Evan Cheng91111d22011-07-09 05:47:46 +0000425
Eli Bendersky6f5d70e2013-02-26 23:04:17 +0000426 MCInstPrinter *IP = NULL;
Kevin Enderbyf92f9902009-09-04 21:45:34 +0000427 if (FileType == OFT_AssemblyFile) {
Kevin Enderby62183c42012-10-22 22:31:46 +0000428 IP =
Craig Topper54bfde72012-04-02 06:09:36 +0000429 TheTarget->createMCInstPrinter(OutputAsmVariant, *MAI, *MCII, *MRI, *STI);
Benjamin Kramera3e0ddb2010-07-29 17:48:06 +0000430 MCCodeEmitter *CE = 0;
Evan Cheng5928e692011-07-25 23:24:55 +0000431 MCAsmBackend *MAB = 0;
Daniel Dunbarecd0c8a2010-12-16 03:05:59 +0000432 if (ShowEncoding) {
Jim Grosbachc3b04272012-05-15 17:35:52 +0000433 CE = TheTarget->createMCCodeEmitter(*MCII, *MRI, *STI, Ctx);
Bill Wendling58e2d3d2013-09-09 02:37:14 +0000434 MAB = TheTarget->createMCAsmBackend(*MRI, TripleName, MCPU);
Daniel Dunbarecd0c8a2010-12-16 03:05:59 +0000435 }
Rafael Espindolab3b60572012-11-23 17:37:34 +0000436 bool UseCFI = !DisableCFI;
Rafael Espindolab4eec1d2014-02-05 18:00:21 +0000437 Str.reset(TheTarget->createAsmStreamer(
438 Ctx, FOS, /*asmverbose*/ true, UseCFI,
439 /*useDwarfDirectory*/ true, IP, CE, MAB, ShowInst));
Jim Grosbach78309c42011-12-05 23:20:14 +0000440
Daniel Dunbarb09b8902010-03-23 23:47:12 +0000441 } else if (FileType == OFT_Null) {
442 Str.reset(createNullStreamer(Ctx));
Daniel Dunbar3016db32009-08-21 09:11:24 +0000443 } else {
444 assert(FileType == OFT_ObjectFile && "Invalid file type!");
Jim Grosbachc3b04272012-05-15 17:35:52 +0000445 MCCodeEmitter *CE = TheTarget->createMCCodeEmitter(*MCII, *MRI, *STI, Ctx);
Bill Wendling58e2d3d2013-09-09 02:37:14 +0000446 MCAsmBackend *MAB = TheTarget->createMCAsmBackend(*MRI, TripleName, MCPU);
Evan Cheng3a792252011-07-26 00:42:34 +0000447 Str.reset(TheTarget->createMCObjectStreamer(TripleName, Ctx, *MAB,
Rafael Espindolae41383f2014-01-26 06:38:58 +0000448 FOS, CE, *STI, RelaxAll,
Evan Cheng3a792252011-07-26 00:42:34 +0000449 NoExecStack));
Daniel Dunbar3016db32009-08-21 09:11:24 +0000450 }
Daniel Dunbara10e5192009-06-24 23:30:00 +0000451
Richard Bartondef81b92012-04-16 11:32:10 +0000452 int Res = 1;
Kevin Enderby168ffb32012-12-05 18:13:19 +0000453 bool disassemble = false;
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000454 switch (Action) {
Chris Lattnerb0133452009-06-21 20:16:42 +0000455 case AC_AsLex:
Richard Bartondef81b92012-04-16 11:32:10 +0000456 Res = AsLexInput(SrcMgr, *MAI, Out.get());
457 break;
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000458 case AC_Assemble:
Joey Gouly0e76fa72013-09-12 10:28:05 +0000459 Res = AssembleInput(ProgName, TheTarget, SrcMgr, Ctx, *Str, *MAI, *STI, *MCII);
Richard Bartondef81b92012-04-16 11:32:10 +0000460 break;
Kevin Enderby62183c42012-10-22 22:31:46 +0000461 case AC_MDisassemble:
Eli Bendersky6f5d70e2013-02-26 23:04:17 +0000462 assert(IP && "Expected assembly output");
Kevin Enderby62183c42012-10-22 22:31:46 +0000463 IP->setUseMarkup(1);
Kevin Enderby168ffb32012-12-05 18:13:19 +0000464 disassemble = true;
465 break;
466 case AC_HDisassemble:
Eli Bendersky6f5d70e2013-02-26 23:04:17 +0000467 assert(IP && "Expected assembly output");
Kevin Enderby168ffb32012-12-05 18:13:19 +0000468 IP->setPrintImmHex(1);
469 disassemble = true;
470 break;
Sean Callanan7e645502009-12-17 01:49:59 +0000471 case AC_Disassemble:
Kevin Enderby168ffb32012-12-05 18:13:19 +0000472 disassemble = true;
Richard Bartondef81b92012-04-16 11:32:10 +0000473 break;
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000474 }
Kevin Enderby168ffb32012-12-05 18:13:19 +0000475 if (disassemble)
476 Res = Disassembler::disassemble(*TheTarget, TripleName, *STI, *Str,
477 *Buffer, SrcMgr, Out->os());
Richard Bartondef81b92012-04-16 11:32:10 +0000478
479 // Keep output if no errors.
480 if (Res == 0) Out->keep();
481 return Res;
Chris Lattner8dd8a522009-06-18 23:04:45 +0000482}