blob: 91bacc034587a3baec6d9424d8dcf81d567246a3 [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"
Evgeniy Stepanov0a951b72014-04-23 11:16:03 +000028#include "llvm/MC/MCTargetOptionsCommandFlags.h"
Chris Lattner8dd8a522009-06-18 23:04:45 +000029#include "llvm/Support/CommandLine.h"
David Blaikie9c3857c2014-03-28 21:00:25 +000030#include "llvm/Support/Compression.h"
Dan Gohman268b0f42010-08-20 01:07:01 +000031#include "llvm/Support/FileUtilities.h"
Daniel Dunbar80d484e2009-08-14 03:48:55 +000032#include "llvm/Support/FormattedStream.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000033#include "llvm/Support/Host.h"
Chris Lattner8dd8a522009-06-18 23:04:45 +000034#include "llvm/Support/ManagedStatic.h"
35#include "llvm/Support/MemoryBuffer.h"
36#include "llvm/Support/PrettyStackTrace.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000037#include "llvm/Support/Signals.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000038#include "llvm/Support/SourceMgr.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000039#include "llvm/Support/TargetRegistry.h"
40#include "llvm/Support/TargetSelect.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000041#include "llvm/Support/ToolOutputFile.h"
Chris Lattner8dd8a522009-06-18 23:04:45 +000042using namespace llvm;
43
44static cl::opt<std::string>
45InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
46
47static cl::opt<std::string>
48OutputFilename("o", cl::desc("Output filename"),
49 cl::value_desc("filename"));
50
Daniel Dunbard18dd1d2009-08-27 07:56:39 +000051static cl::opt<bool>
52ShowEncoding("show-encoding", cl::desc("Show instruction encodings"));
53
Daniel Dunbare3ee3322010-02-03 18:18:30 +000054static cl::opt<bool>
Eric Christopher23c6d1f2014-06-19 06:22:01 +000055CompressDebugSections("compress-debug-sections",
56 cl::desc("Compress DWARF debug sections"));
David Blaikie7400a972014-03-27 20:45:58 +000057
58static cl::opt<bool>
Daniel Dunbare3ee3322010-02-03 18:18:30 +000059ShowInst("show-inst", cl::desc("Show internal instruction representation"));
60
Daniel Dunbar2eca0252010-08-11 06:37:09 +000061static cl::opt<bool>
62ShowInstOperands("show-inst-operands",
63 cl::desc("Show instructions operands as parsed"));
64
Chris Lattner44790342009-09-20 07:17:49 +000065static cl::opt<unsigned>
66OutputAsmVariant("output-asm-variant",
67 cl::desc("Syntax variant to use for output printing"));
68
Jim Grosbach3fdf7cf2014-06-11 20:26:40 +000069static cl::opt<bool>
70PrintImmHex("print-imm-hex", cl::init(false),
71 cl::desc("Prefer hex format for immediate values"));
72
Daniel Dunbar3016db32009-08-21 09:11:24 +000073enum OutputFileType {
Daniel Dunbarb09b8902010-03-23 23:47:12 +000074 OFT_Null,
Daniel Dunbar3016db32009-08-21 09:11:24 +000075 OFT_AssemblyFile,
76 OFT_ObjectFile
77};
78static cl::opt<OutputFileType>
79FileType("filetype", cl::init(OFT_AssemblyFile),
80 cl::desc("Choose an output file type:"),
81 cl::values(
82 clEnumValN(OFT_AssemblyFile, "asm",
83 "Emit an assembly ('.s') file"),
Daniel Dunbarb09b8902010-03-23 23:47:12 +000084 clEnumValN(OFT_Null, "null",
85 "Don't emit anything (for timing purposes)"),
Daniel Dunbar3016db32009-08-21 09:11:24 +000086 clEnumValN(OFT_ObjectFile, "obj",
87 "Emit a native object ('.o') file"),
88 clEnumValEnd));
89
Chris Lattnerd70e15b42009-06-21 05:22:37 +000090static cl::list<std::string>
91IncludeDirs("I", cl::desc("Directory of include files"),
92 cl::value_desc("directory"), cl::Prefix);
Chris Lattner8dd8a522009-06-18 23:04:45 +000093
Daniel Dunbar8c6bad22009-07-17 22:38:58 +000094static cl::opt<std::string>
Daniel Dunbar46892112010-03-13 02:20:38 +000095ArchName("arch", cl::desc("Target arch to assemble for, "
Bill Wendlinge3031142011-06-17 20:35:21 +000096 "see -version for available targets"));
Daniel Dunbar46892112010-03-13 02:20:38 +000097
98static cl::opt<std::string>
Nick Lewyckyb2449092009-11-01 22:07:54 +000099TripleName("triple", cl::desc("Target triple to assemble for, "
Daniel Dunbar46892112010-03-13 02:20:38 +0000100 "see -version for available targets"));
Daniel Dunbar8c6bad22009-07-17 22:38:58 +0000101
Jim Grosbach685b7732010-10-30 15:57:50 +0000102static cl::opt<std::string>
103MCPU("mcpu",
104 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
105 cl::value_desc("cpu-name"),
106 cl::init(""));
107
James Molloy4c493e82011-09-07 17:24:38 +0000108static cl::list<std::string>
109MAttrs("mattr",
110 cl::CommaSeparated,
111 cl::desc("Target specific attributes (-mattr=help for details)"),
112 cl::value_desc("a1,+a2,-a3,..."));
113
Evan Cheng2129f592011-07-19 06:37:02 +0000114static cl::opt<Reloc::Model>
115RelocModel("relocation-model",
116 cl::desc("Choose relocation model"),
117 cl::init(Reloc::Default),
118 cl::values(
119 clEnumValN(Reloc::Default, "default",
120 "Target default relocation model"),
121 clEnumValN(Reloc::Static, "static",
122 "Non-relocatable code"),
123 clEnumValN(Reloc::PIC_, "pic",
124 "Fully relocatable, position independent code"),
125 clEnumValN(Reloc::DynamicNoPIC, "dynamic-no-pic",
126 "Relocatable external references, non-relocatable code"),
127 clEnumValEnd));
128
Evan Chengefd9b422011-07-20 07:51:56 +0000129static cl::opt<llvm::CodeModel::Model>
130CMModel("code-model",
131 cl::desc("Choose code model"),
132 cl::init(CodeModel::Default),
133 cl::values(clEnumValN(CodeModel::Default, "default",
134 "Target default code model"),
135 clEnumValN(CodeModel::Small, "small",
136 "Small code model"),
137 clEnumValN(CodeModel::Kernel, "kernel",
138 "Kernel code model"),
139 clEnumValN(CodeModel::Medium, "medium",
140 "Medium code model"),
141 clEnumValN(CodeModel::Large, "large",
142 "Large code model"),
143 clEnumValEnd));
144
Daniel Dunbar322fec62010-03-13 02:20:57 +0000145static cl::opt<bool>
Bill Wendlinge3031142011-06-17 20:35:21 +0000146NoInitialTextSection("n", cl::desc("Don't assume assembly file starts "
147 "in the text section"));
Daniel Dunbar322fec62010-03-13 02:20:57 +0000148
Daniel Dunbar4ee0d032011-03-28 22:49:15 +0000149static cl::opt<bool>
Kevin Enderby6469fc22011-11-01 22:27:22 +0000150GenDwarfForAssembly("g", cl::desc("Generate dwarf debugging info for assembly "
151 "source files"));
152
Chandler Carruth10700aad2012-12-17 21:32:42 +0000153static cl::opt<std::string>
154DebugCompilationDir("fdebug-compilation-dir",
155 cl::desc("Specifies the debug info's compilation dir"));
156
Eric Christopher906da232012-12-18 00:31:01 +0000157static cl::opt<std::string>
158MainFileName("main-file-name",
159 cl::desc("Specifies the name we should consider the input file"));
160
Eric Christopherdc3e9c72014-05-21 00:20:01 +0000161static cl::opt<bool> SaveTempLabels("save-temp-labels",
162 cl::desc("Don't discard temporary labels"));
163
Eric Christopher472cee32014-05-21 21:05:09 +0000164static cl::opt<bool> NoExecStack("no-exec-stack",
165 cl::desc("File doesn't need an exec stack"));
166
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000167enum ActionType {
Chris Lattnerb0133452009-06-21 20:16:42 +0000168 AC_AsLex,
Sean Callanan7e645502009-12-17 01:49:59 +0000169 AC_Assemble,
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000170 AC_Disassemble,
Kevin Enderby168ffb32012-12-05 18:13:19 +0000171 AC_MDisassemble,
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000172};
Chris Lattner8dd8a522009-06-18 23:04:45 +0000173
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000174static cl::opt<ActionType>
175Action(cl::desc("Action to perform:"),
Chris Lattnerb0133452009-06-21 20:16:42 +0000176 cl::init(AC_Assemble),
177 cl::values(clEnumValN(AC_AsLex, "as-lex",
178 "Lex tokens from a .s file"),
179 clEnumValN(AC_Assemble, "assemble",
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000180 "Assemble a .s file (default)"),
Sean Callanan7e645502009-12-17 01:49:59 +0000181 clEnumValN(AC_Disassemble, "disassemble",
182 "Disassemble strings of hex bytes"),
Kevin Enderby62183c42012-10-22 22:31:46 +0000183 clEnumValN(AC_MDisassemble, "mdis",
184 "Marked up disassembly of strings of hex bytes"),
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;
Craig Toppere6cb63e2014-04-25 04:24:47 +0000199 return nullptr;
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
Craig Topper8c8ee4d2014-12-12 07:52:16 +0000207static std::unique_ptr<tool_output_file> GetOutputStream() {
Dan Gohman268b0f42010-08-20 01:07:01 +0000208 if (OutputFilename == "")
209 OutputFilename = "-";
210
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000211 std::error_code EC;
Craig Topper8c8ee4d2014-12-12 07:52:16 +0000212 auto Out = llvm::make_unique<tool_output_file>(OutputFilename, EC,
213 sys::fs::F_None);
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000214 if (EC) {
215 errs() << EC.message() << '\n';
Craig Toppere6cb63e2014-04-25 04:24:47 +0000216 return nullptr;
Dan Gohman268b0f42010-08-20 01:07:01 +0000217 }
Dan Gohmana2233f22010-09-01 14:20:41 +0000218
219 return Out;
Dan Gohman268b0f42010-08-20 01:07:01 +0000220}
221
Kevin Enderbye7739d42011-12-09 18:09:40 +0000222static std::string DwarfDebugFlags;
223static void setDwarfDebugFlags(int argc, char **argv) {
224 if (!getenv("RC_DEBUG_OPTIONS"))
225 return;
226 for (int i = 0; i < argc; i++) {
227 DwarfDebugFlags += argv[i];
228 if (i + 1 < argc)
229 DwarfDebugFlags += " ";
230 }
231}
232
Kevin Enderbye82ada62013-01-16 17:46:23 +0000233static std::string DwarfDebugProducer;
234static void setDwarfDebugProducer(void) {
235 if(!getenv("DEBUG_PRODUCER"))
236 return;
237 DwarfDebugProducer += getenv("DEBUG_PRODUCER");
238}
239
Eric Christopher23c6d1f2014-06-19 06:22:01 +0000240static int AsLexInput(SourceMgr &SrcMgr, MCAsmInfo &MAI,
Craig Topper4e05fc32014-12-12 07:52:19 +0000241 raw_ostream &OS) {
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000242
Richard Bartondef81b92012-04-16 11:32:10 +0000243 AsmLexer Lexer(MAI);
Rafael Espindola8026bd02014-07-06 14:17:29 +0000244 Lexer.setBuffer(SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID())->getBuffer());
Daniel Dunbar7a85f9c2010-07-18 18:31:28 +0000245
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:
Craig Topper4e05fc32014-12-12 07:52:19 +0000260 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:
Craig Topper4e05fc32014-12-12 07:52:19 +0000263 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:
Craig Topper4e05fc32014-12-12 07:52:19 +0000266 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:
Craig Topper4e05fc32014-12-12 07:52:19 +0000269 OS << "string: " << Lexer.getTok().getString();
Daniel Dunbar25c2c622010-09-16 00:42:35 +0000270 break;
Daniel Dunbar9c4809a2009-07-01 06:56:54 +0000271
Craig Topper4e05fc32014-12-12 07:52:19 +0000272 case AsmToken::Amp: OS << "Amp"; break;
273 case AsmToken::AmpAmp: OS << "AmpAmp"; break;
274 case AsmToken::At: OS << "At"; break;
275 case AsmToken::Caret: OS << "Caret"; break;
276 case AsmToken::Colon: OS << "Colon"; break;
277 case AsmToken::Comma: OS << "Comma"; break;
278 case AsmToken::Dollar: OS << "Dollar"; break;
279 case AsmToken::Dot: OS << "Dot"; break;
280 case AsmToken::EndOfStatement: OS << "EndOfStatement"; break;
281 case AsmToken::Eof: OS << "Eof"; break;
282 case AsmToken::Equal: OS << "Equal"; break;
283 case AsmToken::EqualEqual: OS << "EqualEqual"; break;
284 case AsmToken::Exclaim: OS << "Exclaim"; break;
285 case AsmToken::ExclaimEqual: OS << "ExclaimEqual"; break;
286 case AsmToken::Greater: OS << "Greater"; break;
287 case AsmToken::GreaterEqual: OS << "GreaterEqual"; break;
288 case AsmToken::GreaterGreater: OS << "GreaterGreater"; break;
289 case AsmToken::Hash: OS << "Hash"; break;
290 case AsmToken::LBrac: OS << "LBrac"; break;
291 case AsmToken::LCurly: OS << "LCurly"; break;
292 case AsmToken::LParen: OS << "LParen"; break;
293 case AsmToken::Less: OS << "Less"; break;
294 case AsmToken::LessEqual: OS << "LessEqual"; break;
295 case AsmToken::LessGreater: OS << "LessGreater"; break;
296 case AsmToken::LessLess: OS << "LessLess"; break;
297 case AsmToken::Minus: OS << "Minus"; break;
298 case AsmToken::Percent: OS << "Percent"; break;
299 case AsmToken::Pipe: OS << "Pipe"; break;
300 case AsmToken::PipePipe: OS << "PipePipe"; break;
301 case AsmToken::Plus: OS << "Plus"; break;
302 case AsmToken::RBrac: OS << "RBrac"; break;
303 case AsmToken::RCurly: OS << "RCurly"; break;
304 case AsmToken::RParen: OS << "RParen"; break;
305 case AsmToken::Slash: OS << "Slash"; break;
306 case AsmToken::Star: OS << "Star"; break;
307 case AsmToken::Tilde: OS << "Tilde"; break;
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +0000308 }
Daniel Dunbar1601f552010-10-25 20:18:46 +0000309
310 // Print the token string.
Craig Topper4e05fc32014-12-12 07:52:19 +0000311 OS << " (\"";
312 OS.write_escaped(Tok.getString());
313 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,
Eric Christopher23c6d1f2014-06-19 06:22:01 +0000321 MCAsmInfo &MAI, MCSubtargetInfo &STI,
Eric Christopher4c5bff32014-06-19 06:22:08 +0000322 MCInstrInfo &MCII, MCTargetOptions &MCOptions) {
Evgeniy Stepanov0a951b72014-04-23 11:16:03 +0000323 std::unique_ptr<MCAsmParser> Parser(
324 createMCAsmParser(SrcMgr, Ctx, Str, MAI));
Ahmed Charles56440fd2014-03-06 05:51:42 +0000325 std::unique_ptr<MCTargetAsmParser> TAP(
Eric Christopher4c5bff32014-06-19 06:22:08 +0000326 TheTarget->createMCAsmParser(STI, *Parser, MCII, MCOptions));
327
Richard Bartondef81b92012-04-16 11:32:10 +0000328 if (!TAP) {
329 errs() << ProgName
330 << ": error: this target does not support assembly parsing.\n";
331 return 1;
332 }
333
334 Parser->setShowParsedOperands(ShowInstOperands);
Craig Topper4e05fc32014-12-12 07:52:19 +0000335 Parser->setTargetParser(*TAP);
Richard Bartondef81b92012-04-16 11:32:10 +0000336
337 int Res = Parser->Run(NoInitialTextSection);
338
339 return Res;
340}
341
342int main(int argc, char **argv) {
343 // Print a stack trace if we signal out.
344 sys::PrintStackTraceOnErrorSignal();
345 PrettyStackTraceProgram X(argc, argv);
346 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
347
348 // Initialize targets and assembly printers/parsers.
349 llvm::InitializeAllTargetInfos();
350 llvm::InitializeAllTargetMCs();
351 llvm::InitializeAllAsmParsers();
352 llvm::InitializeAllDisassemblers();
353
354 // Register the target printer for --version.
355 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
356
357 cl::ParseCommandLineOptions(argc, argv, "llvm machine code playground\n");
Eric Christopher4c5bff32014-06-19 06:22:08 +0000358 MCTargetOptions MCOptions = InitMCTargetOptionsFromFlags();
Richard Bartondef81b92012-04-16 11:32:10 +0000359 TripleName = Triple::normalize(TripleName);
360 setDwarfDebugFlags(argc, argv);
361
Kevin Enderbye82ada62013-01-16 17:46:23 +0000362 setDwarfDebugProducer();
363
Richard Bartondef81b92012-04-16 11:32:10 +0000364 const char *ProgName = argv[0];
Daniel Dunbar80d484e2009-08-14 03:48:55 +0000365 const Target *TheTarget = GetTarget(ProgName);
366 if (!TheTarget)
367 return 1;
368
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000369 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferPtr =
370 MemoryBuffer::getFileOrSTDIN(InputFilename);
371 if (std::error_code EC = BufferPtr.getError()) {
372 errs() << ProgName << ": " << EC.message() << '\n';
Chris Lattnerb0133452009-06-21 20:16:42 +0000373 return 1;
374 }
David Blaikie1961f142014-08-21 20:44:56 +0000375 MemoryBuffer *Buffer = BufferPtr->get();
Jim Grosbach112a2de2011-05-09 20:05:25 +0000376
Chris Lattnerb0133452009-06-21 20:16:42 +0000377 SourceMgr SrcMgr;
Jim Grosbach112a2de2011-05-09 20:05:25 +0000378
Daniel Dunbar5c947db2009-08-19 16:25:53 +0000379 // Tell SrcMgr about this buffer, which is what the parser will pick up.
David Blaikie1961f142014-08-21 20:44:56 +0000380 SrcMgr.AddNewSourceBuffer(std::move(*BufferPtr), SMLoc());
Jim Grosbach112a2de2011-05-09 20:05:25 +0000381
Chris Lattnerb0133452009-06-21 20:16:42 +0000382 // Record the location of the include directories so that the lexer can find
383 // it later.
384 SrcMgr.setIncludeDirs(IncludeDirs);
Jim Grosbach112a2de2011-05-09 20:05:25 +0000385
Ahmed Charles56440fd2014-03-06 05:51:42 +0000386 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
Evan Chengd60fa58b2011-07-18 20:57:22 +0000387 assert(MRI && "Unable to create target register info!");
388
Ahmed Charles56440fd2014-03-06 05:51:42 +0000389 std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName));
Rafael Espindola227144c2013-05-13 01:16:13 +0000390 assert(MAI && "Unable to create target asm info!");
391
David Blaikie9b620b42014-03-28 20:45:24 +0000392 if (CompressDebugSections) {
393 if (!zlib::isAvailable()) {
Eric Christopher23c6d1f2014-06-19 06:22:01 +0000394 errs() << ProgName
395 << ": build tools with zlib to enable -compress-debug-sections";
David Blaikie9b620b42014-03-28 20:45:24 +0000396 return 1;
397 }
David Blaikie7400a972014-03-27 20:45:58 +0000398 MAI->setCompressDebugSections(true);
David Blaikie9b620b42014-03-28 20:45:24 +0000399 }
David Blaikie7400a972014-03-27 20:45:58 +0000400
Evan Cheng76792992011-07-20 05:58:47 +0000401 // FIXME: This is not pretty. MCContext has a ptr to MCObjectFileInfo and
402 // MCObjectFileInfo needs a MCContext reference in order to initialize itself.
Rafael Espindola75556bc2014-06-28 17:46:19 +0000403 MCObjectFileInfo MOFI;
404 MCContext Ctx(MAI.get(), MRI.get(), &MOFI, &SrcMgr);
405 MOFI.InitMCObjectFileInfo(TripleName, RelocModel, CMModel, Ctx);
Evan Cheng76792992011-07-20 05:58:47 +0000406
Daniel Dunbar4ee0d032011-03-28 22:49:15 +0000407 if (SaveTempLabels)
408 Ctx.setAllowTemporaryLabels(false);
Rafael Espindola0a017a62010-12-10 07:39:47 +0000409
Kevin Enderby6469fc22011-11-01 22:27:22 +0000410 Ctx.setGenDwarfForAssembly(GenDwarfForAssembly);
Eric Christopher4c5bff32014-06-19 06:22:08 +0000411 // Default to 4 for dwarf version.
412 unsigned DwarfVersion = MCOptions.DwarfVersion ? MCOptions.DwarfVersion : 4;
Oliver Stannard7eacbd52014-05-01 08:46:02 +0000413 if (DwarfVersion < 2 || DwarfVersion > 4) {
414 errs() << ProgName << ": Dwarf version " << DwarfVersion
415 << " is not supported." << '\n';
416 return 1;
417 }
418 Ctx.setDwarfVersion(DwarfVersion);
Chandler Carruth10700aad2012-12-17 21:32:42 +0000419 if (!DwarfDebugFlags.empty())
Kevin Enderbye7739d42011-12-09 18:09:40 +0000420 Ctx.setDwarfDebugFlags(StringRef(DwarfDebugFlags));
Kevin Enderbye82ada62013-01-16 17:46:23 +0000421 if (!DwarfDebugProducer.empty())
422 Ctx.setDwarfDebugProducer(StringRef(DwarfDebugProducer));
Chandler Carruth10700aad2012-12-17 21:32:42 +0000423 if (!DebugCompilationDir.empty())
424 Ctx.setCompilationDir(DebugCompilationDir);
Eric Christopher906da232012-12-18 00:31:01 +0000425 if (!MainFileName.empty())
426 Ctx.setMainFileName(MainFileName);
Kevin Enderby6469fc22011-11-01 22:27:22 +0000427
James Molloy4c493e82011-09-07 17:24:38 +0000428 // Package up features to be passed to target/subtarget
429 std::string FeaturesStr;
430 if (MAttrs.size()) {
431 SubtargetFeatures Features;
432 for (unsigned i = 0; i != MAttrs.size(); ++i)
433 Features.AddFeature(MAttrs[i]);
434 FeaturesStr = Features.getString();
435 }
436
Craig Topper8c8ee4d2014-12-12 07:52:16 +0000437 std::unique_ptr<tool_output_file> Out = GetOutputStream();
Dan Gohman268b0f42010-08-20 01:07:01 +0000438 if (!Out)
439 return 1;
440
Dan Gohmana2233f22010-09-01 14:20:41 +0000441 formatted_raw_ostream FOS(Out->os());
Ahmed Charles56440fd2014-03-06 05:51:42 +0000442 std::unique_ptr<MCStreamer> Str;
Chris Lattnera61e93d2009-08-17 04:23:44 +0000443
Ahmed Charles56440fd2014-03-06 05:51:42 +0000444 std::unique_ptr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo());
445 std::unique_ptr<MCSubtargetInfo> STI(
446 TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr));
Evan Cheng91111d22011-07-09 05:47:46 +0000447
Craig Toppere6cb63e2014-04-25 04:24:47 +0000448 MCInstPrinter *IP = nullptr;
Kevin Enderbyf92f9902009-09-04 21:45:34 +0000449 if (FileType == OFT_AssemblyFile) {
Kevin Enderby62183c42012-10-22 22:31:46 +0000450 IP =
Craig Topper54bfde72012-04-02 06:09:36 +0000451 TheTarget->createMCInstPrinter(OutputAsmVariant, *MAI, *MCII, *MRI, *STI);
Jim Grosbach3fdf7cf2014-06-11 20:26:40 +0000452
453 // Set the display preference for hex vs. decimal immediates.
454 IP->setPrintImmHex(PrintImmHex);
455
456 // Set up the AsmStreamer.
Craig Toppere6cb63e2014-04-25 04:24:47 +0000457 MCCodeEmitter *CE = nullptr;
458 MCAsmBackend *MAB = nullptr;
Daniel Dunbarecd0c8a2010-12-16 03:05:59 +0000459 if (ShowEncoding) {
Jim Grosbachc3b04272012-05-15 17:35:52 +0000460 CE = TheTarget->createMCCodeEmitter(*MCII, *MRI, *STI, Ctx);
Bill Wendling58e2d3d2013-09-09 02:37:14 +0000461 MAB = TheTarget->createMCAsmBackend(*MRI, TripleName, MCPU);
Daniel Dunbarecd0c8a2010-12-16 03:05:59 +0000462 }
Rafael Espindola52845cf2014-03-20 21:48:20 +0000463 Str.reset(TheTarget->createAsmStreamer(Ctx, FOS, /*asmverbose*/ true,
Rafael Espindola566fcfe2014-05-07 13:00:43 +0000464 /*useDwarfDirectory*/ true, IP, CE,
465 MAB, ShowInst));
Jim Grosbach78309c42011-12-05 23:20:14 +0000466
Daniel Dunbarb09b8902010-03-23 23:47:12 +0000467 } else if (FileType == OFT_Null) {
Peter Collingbournef4498a42015-02-19 00:45:04 +0000468 Str.reset(TheTarget->createNullStreamer(Ctx));
Daniel Dunbar3016db32009-08-21 09:11:24 +0000469 } else {
470 assert(FileType == OFT_ObjectFile && "Invalid file type!");
Jim Grosbachc3b04272012-05-15 17:35:52 +0000471 MCCodeEmitter *CE = TheTarget->createMCCodeEmitter(*MCII, *MRI, *STI, Ctx);
Bill Wendling58e2d3d2013-09-09 02:37:14 +0000472 MCAsmBackend *MAB = TheTarget->createMCAsmBackend(*MRI, TripleName, MCPU);
Rafael Espindola7b61ddf2014-10-15 16:12:52 +0000473 Str.reset(TheTarget->createMCObjectStreamer(TripleName, Ctx, *MAB, FOS, CE,
474 *STI, RelaxAll));
475 if (NoExecStack)
476 Str->InitSections(true);
Daniel Dunbar3016db32009-08-21 09:11:24 +0000477 }
Daniel Dunbara10e5192009-06-24 23:30:00 +0000478
Richard Bartondef81b92012-04-16 11:32:10 +0000479 int Res = 1;
Kevin Enderby168ffb32012-12-05 18:13:19 +0000480 bool disassemble = false;
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000481 switch (Action) {
Chris Lattnerb0133452009-06-21 20:16:42 +0000482 case AC_AsLex:
Craig Topper4e05fc32014-12-12 07:52:19 +0000483 Res = AsLexInput(SrcMgr, *MAI, Out->os());
Richard Bartondef81b92012-04-16 11:32:10 +0000484 break;
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000485 case AC_Assemble:
Eric Christopher23c6d1f2014-06-19 06:22:01 +0000486 Res = AssembleInput(ProgName, TheTarget, SrcMgr, Ctx, *Str, *MAI, *STI,
Eric Christopher4c5bff32014-06-19 06:22:08 +0000487 *MCII, MCOptions);
Richard Bartondef81b92012-04-16 11:32:10 +0000488 break;
Kevin Enderby62183c42012-10-22 22:31:46 +0000489 case AC_MDisassemble:
Eli Bendersky6f5d70e2013-02-26 23:04:17 +0000490 assert(IP && "Expected assembly output");
Kevin Enderby62183c42012-10-22 22:31:46 +0000491 IP->setUseMarkup(1);
Kevin Enderby168ffb32012-12-05 18:13:19 +0000492 disassemble = true;
493 break;
Sean Callanan7e645502009-12-17 01:49:59 +0000494 case AC_Disassemble:
Kevin Enderby168ffb32012-12-05 18:13:19 +0000495 disassemble = true;
Richard Bartondef81b92012-04-16 11:32:10 +0000496 break;
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000497 }
Kevin Enderby168ffb32012-12-05 18:13:19 +0000498 if (disassemble)
499 Res = Disassembler::disassemble(*TheTarget, TripleName, *STI, *Str,
500 *Buffer, SrcMgr, Out->os());
Richard Bartondef81b92012-04-16 11:32:10 +0000501
502 // Keep output if no errors.
503 if (Res == 0) Out->keep();
504 return Res;
Chris Lattner8dd8a522009-06-18 23:04:45 +0000505}