blob: 61fd2c486cf13e979770a507ab9e0b8d40825260 [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"
David Blaikie9c3857c2014-03-28 21:00:25 +000029#include "llvm/Support/Compression.h"
Dan Gohman268b0f42010-08-20 01:07:01 +000030#include "llvm/Support/FileUtilities.h"
Daniel Dunbar80d484e2009-08-14 03:48:55 +000031#include "llvm/Support/FormattedStream.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000032#include "llvm/Support/Host.h"
Chris Lattner8dd8a522009-06-18 23:04:45 +000033#include "llvm/Support/ManagedStatic.h"
34#include "llvm/Support/MemoryBuffer.h"
35#include "llvm/Support/PrettyStackTrace.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000036#include "llvm/Support/Signals.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000037#include "llvm/Support/SourceMgr.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000038#include "llvm/Support/TargetRegistry.h"
39#include "llvm/Support/TargetSelect.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000040#include "llvm/Support/ToolOutputFile.h"
Chris Lattner8dd8a522009-06-18 23:04:45 +000041using namespace llvm;
42
43static cl::opt<std::string>
44InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
45
46static cl::opt<std::string>
47OutputFilename("o", cl::desc("Output filename"),
48 cl::value_desc("filename"));
49
Daniel Dunbard18dd1d2009-08-27 07:56:39 +000050static cl::opt<bool>
51ShowEncoding("show-encoding", cl::desc("Show instruction encodings"));
52
Daniel Dunbare3ee3322010-02-03 18:18:30 +000053static cl::opt<bool>
David Blaikie7400a972014-03-27 20:45:58 +000054CompressDebugSections("compress-debug-sections", cl::desc("Compress DWARF debug sections"));
55
56static cl::opt<bool>
Daniel Dunbare3ee3322010-02-03 18:18:30 +000057ShowInst("show-inst", cl::desc("Show internal instruction representation"));
58
Daniel Dunbar2eca0252010-08-11 06:37:09 +000059static cl::opt<bool>
60ShowInstOperands("show-inst-operands",
61 cl::desc("Show instructions operands as parsed"));
62
Chris Lattner44790342009-09-20 07:17:49 +000063static cl::opt<unsigned>
64OutputAsmVariant("output-asm-variant",
65 cl::desc("Syntax variant to use for output printing"));
66
Daniel Dunbard821f4a2010-03-25 22:49:09 +000067static cl::opt<bool>
68RelaxAll("mc-relax-all", cl::desc("Relax all fixups"));
69
Daniel Dunbar3ff1a062010-05-23 17:44:06 +000070static cl::opt<bool>
Rafael Espindolab3eca9b2011-01-23 17:55:27 +000071NoExecStack("mc-no-exec-stack", cl::desc("File doesn't need an exec stack"));
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>
Bill Wendlinge3031142011-06-17 20:35:21 +0000150SaveTempLabels("L", cl::desc("Don't discard temporary labels"));
Daniel Dunbar4ee0d032011-03-28 22:49:15 +0000151
Kevin Enderby6469fc22011-11-01 22:27:22 +0000152static cl::opt<bool>
153GenDwarfForAssembly("g", cl::desc("Generate dwarf debugging info for assembly "
154 "source files"));
155
Chandler Carruth10700aad2012-12-17 21:32:42 +0000156static cl::opt<std::string>
157DebugCompilationDir("fdebug-compilation-dir",
158 cl::desc("Specifies the debug info's compilation dir"));
159
Eric Christopher906da232012-12-18 00:31:01 +0000160static cl::opt<std::string>
161MainFileName("main-file-name",
162 cl::desc("Specifies the name we should consider the input file"));
163
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000164enum ActionType {
Chris Lattnerb0133452009-06-21 20:16:42 +0000165 AC_AsLex,
Sean Callanan7e645502009-12-17 01:49:59 +0000166 AC_Assemble,
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000167 AC_Disassemble,
Kevin Enderby168ffb32012-12-05 18:13:19 +0000168 AC_MDisassemble,
169 AC_HDisassemble
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000170};
Chris Lattner8dd8a522009-06-18 23:04:45 +0000171
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000172static cl::opt<ActionType>
173Action(cl::desc("Action to perform:"),
Chris Lattnerb0133452009-06-21 20:16:42 +0000174 cl::init(AC_Assemble),
175 cl::values(clEnumValN(AC_AsLex, "as-lex",
176 "Lex tokens from a .s file"),
177 clEnumValN(AC_Assemble, "assemble",
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000178 "Assemble a .s file (default)"),
Sean Callanan7e645502009-12-17 01:49:59 +0000179 clEnumValN(AC_Disassemble, "disassemble",
180 "Disassemble strings of hex bytes"),
Kevin Enderby62183c42012-10-22 22:31:46 +0000181 clEnumValN(AC_MDisassemble, "mdis",
182 "Marked up disassembly of strings of hex bytes"),
Kevin Enderby168ffb32012-12-05 18:13:19 +0000183 clEnumValN(AC_HDisassemble, "hdis",
184 "Disassemble strings of hex bytes printing "
185 "immediates as hex"),
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000186 clEnumValEnd));
187
Kevin Enderbyf92f9902009-09-04 21:45:34 +0000188static const Target *GetTarget(const char *ProgName) {
Daniel Dunbar46892112010-03-13 02:20:38 +0000189 // Figure out the target triple.
190 if (TripleName.empty())
Sebastian Pop94441fb2011-11-01 21:32:20 +0000191 TripleName = sys::getDefaultTargetTriple();
Evan Chenge64f0e52011-07-26 19:02:16 +0000192 Triple TheTriple(Triple::normalize(TripleName));
193
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000194 // Get the target specific parser.
195 std::string Error;
196 const Target *TheTarget = TargetRegistry::lookupTarget(ArchName, TheTriple,
197 Error);
198 if (!TheTarget) {
199 errs() << ProgName << ": " << Error;
200 return 0;
Daniel Dunbar46892112010-03-13 02:20:38 +0000201 }
202
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000203 // Update the triple name and return the found target.
Evan Chenge64f0e52011-07-26 19:02:16 +0000204 TripleName = TheTriple.getTriple();
205 return TheTarget;
Kevin Enderbyf92f9902009-09-04 21:45:34 +0000206}
207
Dan Gohmana2233f22010-09-01 14:20:41 +0000208static tool_output_file *GetOutputStream() {
Dan Gohman268b0f42010-08-20 01:07:01 +0000209 if (OutputFilename == "")
210 OutputFilename = "-";
211
212 std::string Err;
Rafael Espindola6d354812013-07-16 19:44:17 +0000213 tool_output_file *Out =
Rafael Espindola90c7f1c2014-02-24 18:20:12 +0000214 new tool_output_file(OutputFilename.c_str(), Err, sys::fs::F_None);
Dan Gohman268b0f42010-08-20 01:07:01 +0000215 if (!Err.empty()) {
216 errs() << Err << '\n';
217 delete Out;
218 return 0;
219 }
Dan Gohmana2233f22010-09-01 14:20:41 +0000220
221 return Out;
Dan Gohman268b0f42010-08-20 01:07:01 +0000222}
223
Kevin Enderbye7739d42011-12-09 18:09:40 +0000224static std::string DwarfDebugFlags;
225static void setDwarfDebugFlags(int argc, char **argv) {
226 if (!getenv("RC_DEBUG_OPTIONS"))
227 return;
228 for (int i = 0; i < argc; i++) {
229 DwarfDebugFlags += argv[i];
230 if (i + 1 < argc)
231 DwarfDebugFlags += " ";
232 }
233}
234
Kevin Enderbye82ada62013-01-16 17:46:23 +0000235static std::string DwarfDebugProducer;
236static void setDwarfDebugProducer(void) {
237 if(!getenv("DEBUG_PRODUCER"))
238 return;
239 DwarfDebugProducer += getenv("DEBUG_PRODUCER");
240}
241
Richard Bartondef81b92012-04-16 11:32:10 +0000242static int AsLexInput(SourceMgr &SrcMgr, MCAsmInfo &MAI, tool_output_file *Out) {
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000243
Richard Bartondef81b92012-04-16 11:32:10 +0000244 AsmLexer Lexer(MAI);
Daniel Dunbar7a85f9c2010-07-18 18:31:28 +0000245 Lexer.setBuffer(SrcMgr.getMemoryBuffer(0));
246
Chris Lattnerb0133452009-06-21 20:16:42 +0000247 bool Error = false;
Daniel Dunbarbc798162009-07-28 16:56:42 +0000248 while (Lexer.Lex().isNot(AsmToken::Eof)) {
Daniel Dunbar1601f552010-10-25 20:18:46 +0000249 AsmToken Tok = Lexer.getTok();
250
251 switch (Tok.getKind()) {
Chris Lattnerb0133452009-06-21 20:16:42 +0000252 default:
Chris Lattner03b80a42011-10-16 05:43:57 +0000253 SrcMgr.PrintMessage(Lexer.getLoc(), SourceMgr::DK_Warning,
254 "unknown token");
Chris Lattnerb0133452009-06-21 20:16:42 +0000255 Error = true;
256 break;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000257 case AsmToken::Error:
Chris Lattnerb0133452009-06-21 20:16:42 +0000258 Error = true; // error already printed.
Chris Lattnerd0765612009-06-21 19:21:25 +0000259 break;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000260 case AsmToken::Identifier:
Daniel Dunbar1601f552010-10-25 20:18:46 +0000261 Out->os() << "identifier: " << Lexer.getTok().getString();
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +0000262 break;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000263 case AsmToken::Integer:
Daniel Dunbar1601f552010-10-25 20:18:46 +0000264 Out->os() << "int: " << Lexer.getTok().getString();
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +0000265 break;
Daniel Dunbar3068a932010-09-24 01:59:31 +0000266 case AsmToken::Real:
Daniel Dunbar1601f552010-10-25 20:18:46 +0000267 Out->os() << "real: " << Lexer.getTok().getString();
Daniel Dunbar3068a932010-09-24 01:59:31 +0000268 break;
Daniel Dunbar25c2c622010-09-16 00:42:35 +0000269 case AsmToken::String:
Daniel Dunbar1601f552010-10-25 20:18:46 +0000270 Out->os() << "string: " << Lexer.getTok().getString();
Daniel Dunbar25c2c622010-09-16 00:42:35 +0000271 break;
Daniel Dunbar9c4809a2009-07-01 06:56:54 +0000272
Daniel Dunbar1601f552010-10-25 20:18:46 +0000273 case AsmToken::Amp: Out->os() << "Amp"; break;
274 case AsmToken::AmpAmp: Out->os() << "AmpAmp"; break;
275 case AsmToken::At: Out->os() << "At"; break;
276 case AsmToken::Caret: Out->os() << "Caret"; break;
277 case AsmToken::Colon: Out->os() << "Colon"; break;
278 case AsmToken::Comma: Out->os() << "Comma"; break;
279 case AsmToken::Dollar: Out->os() << "Dollar"; break;
280 case AsmToken::Dot: Out->os() << "Dot"; break;
281 case AsmToken::EndOfStatement: Out->os() << "EndOfStatement"; break;
282 case AsmToken::Eof: Out->os() << "Eof"; break;
283 case AsmToken::Equal: Out->os() << "Equal"; break;
284 case AsmToken::EqualEqual: Out->os() << "EqualEqual"; break;
285 case AsmToken::Exclaim: Out->os() << "Exclaim"; break;
286 case AsmToken::ExclaimEqual: Out->os() << "ExclaimEqual"; break;
287 case AsmToken::Greater: Out->os() << "Greater"; break;
288 case AsmToken::GreaterEqual: Out->os() << "GreaterEqual"; break;
289 case AsmToken::GreaterGreater: Out->os() << "GreaterGreater"; break;
290 case AsmToken::Hash: Out->os() << "Hash"; break;
291 case AsmToken::LBrac: Out->os() << "LBrac"; break;
292 case AsmToken::LCurly: Out->os() << "LCurly"; break;
293 case AsmToken::LParen: Out->os() << "LParen"; break;
294 case AsmToken::Less: Out->os() << "Less"; break;
295 case AsmToken::LessEqual: Out->os() << "LessEqual"; break;
296 case AsmToken::LessGreater: Out->os() << "LessGreater"; break;
297 case AsmToken::LessLess: Out->os() << "LessLess"; break;
298 case AsmToken::Minus: Out->os() << "Minus"; break;
299 case AsmToken::Percent: Out->os() << "Percent"; break;
300 case AsmToken::Pipe: Out->os() << "Pipe"; break;
301 case AsmToken::PipePipe: Out->os() << "PipePipe"; break;
302 case AsmToken::Plus: Out->os() << "Plus"; break;
303 case AsmToken::RBrac: Out->os() << "RBrac"; break;
304 case AsmToken::RCurly: Out->os() << "RCurly"; break;
305 case AsmToken::RParen: Out->os() << "RParen"; break;
306 case AsmToken::Slash: Out->os() << "Slash"; break;
307 case AsmToken::Star: Out->os() << "Star"; break;
308 case AsmToken::Tilde: Out->os() << "Tilde"; break;
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +0000309 }
Daniel Dunbar1601f552010-10-25 20:18:46 +0000310
311 // Print the token string.
312 Out->os() << " (\"";
313 Out->os().write_escaped(Tok.getString());
314 Out->os() << "\")\n";
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +0000315 }
Dan Gohman268b0f42010-08-20 01:07:01 +0000316
Chris Lattnerb0133452009-06-21 20:16:42 +0000317 return Error;
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000318}
319
NAKAMURA Takumi89b8c172014-01-22 03:12:43 +0000320static int AssembleInput(const char *ProgName, const Target *TheTarget,
Richard Bartondef81b92012-04-16 11:32:10 +0000321 SourceMgr &SrcMgr, MCContext &Ctx, MCStreamer &Str,
Joey Gouly0e76fa72013-09-12 10:28:05 +0000322 MCAsmInfo &MAI, MCSubtargetInfo &STI, MCInstrInfo &MCII) {
Ahmed Charles56440fd2014-03-06 05:51:42 +0000323 std::unique_ptr<MCAsmParser> Parser(createMCAsmParser(SrcMgr, Ctx, Str, MAI));
324 std::unique_ptr<MCTargetAsmParser> TAP(
325 TheTarget->createMCAsmParser(STI, *Parser, MCII));
Richard Bartondef81b92012-04-16 11:32:10 +0000326 if (!TAP) {
327 errs() << ProgName
328 << ": error: this target does not support assembly parsing.\n";
329 return 1;
330 }
331
332 Parser->setShowParsedOperands(ShowInstOperands);
333 Parser->setTargetParser(*TAP.get());
334
335 int Res = Parser->Run(NoInitialTextSection);
336
337 return Res;
338}
339
340int main(int argc, char **argv) {
341 // Print a stack trace if we signal out.
342 sys::PrintStackTraceOnErrorSignal();
343 PrettyStackTraceProgram X(argc, argv);
344 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
345
346 // Initialize targets and assembly printers/parsers.
347 llvm::InitializeAllTargetInfos();
348 llvm::InitializeAllTargetMCs();
349 llvm::InitializeAllAsmParsers();
350 llvm::InitializeAllDisassemblers();
351
352 // Register the target printer for --version.
353 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
354
355 cl::ParseCommandLineOptions(argc, argv, "llvm machine code playground\n");
356 TripleName = Triple::normalize(TripleName);
357 setDwarfDebugFlags(argc, argv);
358
Kevin Enderbye82ada62013-01-16 17:46:23 +0000359 setDwarfDebugProducer();
360
Richard Bartondef81b92012-04-16 11:32:10 +0000361 const char *ProgName = argv[0];
Daniel Dunbar80d484e2009-08-14 03:48:55 +0000362 const Target *TheTarget = GetTarget(ProgName);
363 if (!TheTarget)
364 return 1;
365
Ahmed Charles56440fd2014-03-06 05:51:42 +0000366 std::unique_ptr<MemoryBuffer> BufferPtr;
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +0000367 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename, BufferPtr)) {
Michael J. Spencer7b6fef82010-12-09 17:36:48 +0000368 errs() << ProgName << ": " << ec.message() << '\n';
Chris Lattnerb0133452009-06-21 20:16:42 +0000369 return 1;
370 }
Ahmed Charles96c9d952014-03-05 10:19:29 +0000371 MemoryBuffer *Buffer = BufferPtr.release();
Jim Grosbach112a2de2011-05-09 20:05:25 +0000372
Chris Lattnerb0133452009-06-21 20:16:42 +0000373 SourceMgr SrcMgr;
Jim Grosbach112a2de2011-05-09 20:05:25 +0000374
Daniel Dunbar5c947db2009-08-19 16:25:53 +0000375 // Tell SrcMgr about this buffer, which is what the parser will pick up.
Chris Lattnerb0133452009-06-21 20:16:42 +0000376 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
Jim Grosbach112a2de2011-05-09 20:05:25 +0000377
Chris Lattnerb0133452009-06-21 20:16:42 +0000378 // Record the location of the include directories so that the lexer can find
379 // it later.
380 SrcMgr.setIncludeDirs(IncludeDirs);
Jim Grosbach112a2de2011-05-09 20:05:25 +0000381
Ahmed Charles56440fd2014-03-06 05:51:42 +0000382 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
Evan Chengd60fa58b2011-07-18 20:57:22 +0000383 assert(MRI && "Unable to create target register info!");
384
Ahmed Charles56440fd2014-03-06 05:51:42 +0000385 std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName));
Rafael Espindola227144c2013-05-13 01:16:13 +0000386 assert(MAI && "Unable to create target asm info!");
387
David Blaikie9b620b42014-03-28 20:45:24 +0000388 if (CompressDebugSections) {
389 if (!zlib::isAvailable()) {
390 errs() << ProgName << ": build tools with zlib to enable -compress-debug-sections";
391 return 1;
392 }
David Blaikie7400a972014-03-27 20:45:58 +0000393 MAI->setCompressDebugSections(true);
David Blaikie9b620b42014-03-28 20:45:24 +0000394 }
David Blaikie7400a972014-03-27 20:45:58 +0000395
Evan Cheng76792992011-07-20 05:58:47 +0000396 // FIXME: This is not pretty. MCContext has a ptr to MCObjectFileInfo and
397 // MCObjectFileInfo needs a MCContext reference in order to initialize itself.
Ahmed Charles56440fd2014-03-06 05:51:42 +0000398 std::unique_ptr<MCObjectFileInfo> MOFI(new MCObjectFileInfo());
Bill Wendlingbc07a892013-06-18 07:20:20 +0000399 MCContext Ctx(MAI.get(), MRI.get(), MOFI.get(), &SrcMgr);
Evan Chengbbf3b0d2011-07-20 19:50:42 +0000400 MOFI->InitMCObjectFileInfo(TripleName, RelocModel, CMModel, Ctx);
Evan Cheng76792992011-07-20 05:58:47 +0000401
Daniel Dunbar4ee0d032011-03-28 22:49:15 +0000402 if (SaveTempLabels)
403 Ctx.setAllowTemporaryLabels(false);
Rafael Espindola0a017a62010-12-10 07:39:47 +0000404
Kevin Enderby6469fc22011-11-01 22:27:22 +0000405 Ctx.setGenDwarfForAssembly(GenDwarfForAssembly);
Chandler Carruth10700aad2012-12-17 21:32:42 +0000406 if (!DwarfDebugFlags.empty())
Kevin Enderbye7739d42011-12-09 18:09:40 +0000407 Ctx.setDwarfDebugFlags(StringRef(DwarfDebugFlags));
Kevin Enderbye82ada62013-01-16 17:46:23 +0000408 if (!DwarfDebugProducer.empty())
409 Ctx.setDwarfDebugProducer(StringRef(DwarfDebugProducer));
Chandler Carruth10700aad2012-12-17 21:32:42 +0000410 if (!DebugCompilationDir.empty())
411 Ctx.setCompilationDir(DebugCompilationDir);
Eric Christopher906da232012-12-18 00:31:01 +0000412 if (!MainFileName.empty())
413 Ctx.setMainFileName(MainFileName);
Kevin Enderby6469fc22011-11-01 22:27:22 +0000414
James Molloy4c493e82011-09-07 17:24:38 +0000415 // Package up features to be passed to target/subtarget
416 std::string FeaturesStr;
417 if (MAttrs.size()) {
418 SubtargetFeatures Features;
419 for (unsigned i = 0; i != MAttrs.size(); ++i)
420 Features.AddFeature(MAttrs[i]);
421 FeaturesStr = Features.getString();
422 }
423
Ahmed Charles56440fd2014-03-06 05:51:42 +0000424 std::unique_ptr<tool_output_file> Out(GetOutputStream());
Dan Gohman268b0f42010-08-20 01:07:01 +0000425 if (!Out)
426 return 1;
427
Dan Gohmana2233f22010-09-01 14:20:41 +0000428 formatted_raw_ostream FOS(Out->os());
Ahmed Charles56440fd2014-03-06 05:51:42 +0000429 std::unique_ptr<MCStreamer> Str;
Chris Lattnera61e93d2009-08-17 04:23:44 +0000430
Ahmed Charles56440fd2014-03-06 05:51:42 +0000431 std::unique_ptr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo());
432 std::unique_ptr<MCSubtargetInfo> STI(
433 TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr));
Evan Cheng91111d22011-07-09 05:47:46 +0000434
Eli Bendersky6f5d70e2013-02-26 23:04:17 +0000435 MCInstPrinter *IP = NULL;
Kevin Enderbyf92f9902009-09-04 21:45:34 +0000436 if (FileType == OFT_AssemblyFile) {
Kevin Enderby62183c42012-10-22 22:31:46 +0000437 IP =
Craig Topper54bfde72012-04-02 06:09:36 +0000438 TheTarget->createMCInstPrinter(OutputAsmVariant, *MAI, *MCII, *MRI, *STI);
Benjamin Kramera3e0ddb2010-07-29 17:48:06 +0000439 MCCodeEmitter *CE = 0;
Evan Cheng5928e692011-07-25 23:24:55 +0000440 MCAsmBackend *MAB = 0;
Daniel Dunbarecd0c8a2010-12-16 03:05:59 +0000441 if (ShowEncoding) {
Jim Grosbachc3b04272012-05-15 17:35:52 +0000442 CE = TheTarget->createMCCodeEmitter(*MCII, *MRI, *STI, Ctx);
Bill Wendling58e2d3d2013-09-09 02:37:14 +0000443 MAB = TheTarget->createMCAsmBackend(*MRI, TripleName, MCPU);
Daniel Dunbarecd0c8a2010-12-16 03:05:59 +0000444 }
Rafael Espindola52845cf2014-03-20 21:48:20 +0000445 Str.reset(TheTarget->createAsmStreamer(Ctx, FOS, /*asmverbose*/ true,
446 /*UseCFI*/ true,
447 /*useDwarfDirectory*/
448 true, IP, CE, MAB, ShowInst));
Jim Grosbach78309c42011-12-05 23:20:14 +0000449
Daniel Dunbarb09b8902010-03-23 23:47:12 +0000450 } else if (FileType == OFT_Null) {
451 Str.reset(createNullStreamer(Ctx));
Daniel Dunbar3016db32009-08-21 09:11:24 +0000452 } else {
453 assert(FileType == OFT_ObjectFile && "Invalid file type!");
Jim Grosbachc3b04272012-05-15 17:35:52 +0000454 MCCodeEmitter *CE = TheTarget->createMCCodeEmitter(*MCII, *MRI, *STI, Ctx);
Bill Wendling58e2d3d2013-09-09 02:37:14 +0000455 MCAsmBackend *MAB = TheTarget->createMCAsmBackend(*MRI, TripleName, MCPU);
Evan Cheng3a792252011-07-26 00:42:34 +0000456 Str.reset(TheTarget->createMCObjectStreamer(TripleName, Ctx, *MAB,
Rafael Espindolae41383f2014-01-26 06:38:58 +0000457 FOS, CE, *STI, RelaxAll,
Evan Cheng3a792252011-07-26 00:42:34 +0000458 NoExecStack));
Daniel Dunbar3016db32009-08-21 09:11:24 +0000459 }
Daniel Dunbara10e5192009-06-24 23:30:00 +0000460
Richard Bartondef81b92012-04-16 11:32:10 +0000461 int Res = 1;
Kevin Enderby168ffb32012-12-05 18:13:19 +0000462 bool disassemble = false;
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000463 switch (Action) {
Chris Lattnerb0133452009-06-21 20:16:42 +0000464 case AC_AsLex:
Richard Bartondef81b92012-04-16 11:32:10 +0000465 Res = AsLexInput(SrcMgr, *MAI, Out.get());
466 break;
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000467 case AC_Assemble:
Joey Gouly0e76fa72013-09-12 10:28:05 +0000468 Res = AssembleInput(ProgName, TheTarget, SrcMgr, Ctx, *Str, *MAI, *STI, *MCII);
Richard Bartondef81b92012-04-16 11:32:10 +0000469 break;
Kevin Enderby62183c42012-10-22 22:31:46 +0000470 case AC_MDisassemble:
Eli Bendersky6f5d70e2013-02-26 23:04:17 +0000471 assert(IP && "Expected assembly output");
Kevin Enderby62183c42012-10-22 22:31:46 +0000472 IP->setUseMarkup(1);
Kevin Enderby168ffb32012-12-05 18:13:19 +0000473 disassemble = true;
474 break;
475 case AC_HDisassemble:
Eli Bendersky6f5d70e2013-02-26 23:04:17 +0000476 assert(IP && "Expected assembly output");
Kevin Enderby168ffb32012-12-05 18:13:19 +0000477 IP->setPrintImmHex(1);
478 disassemble = true;
479 break;
Sean Callanan7e645502009-12-17 01:49:59 +0000480 case AC_Disassemble:
Kevin Enderby168ffb32012-12-05 18:13:19 +0000481 disassemble = true;
Richard Bartondef81b92012-04-16 11:32:10 +0000482 break;
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000483 }
Kevin Enderby168ffb32012-12-05 18:13:19 +0000484 if (disassemble)
485 Res = Disassembler::disassemble(*TheTarget, TripleName, *STI, *Str,
486 *Buffer, SrcMgr, Out->os());
Richard Bartondef81b92012-04-16 11:32:10 +0000487
488 // Keep output if no errors.
489 if (Res == 0) Out->keep();
490 return Res;
Chris Lattner8dd8a522009-06-18 23:04:45 +0000491}