blob: d882e01529de7f088a4e24b40bf7502705bb130b [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
Daniel Dunbar7f5bf5a2010-07-18 18:31:33 +000015#include "llvm/MC/MCParser/AsmLexer.h"
Chris Lattner00646cf2010-01-22 01:44:57 +000016#include "llvm/MC/MCParser/MCAsmLexer.h"
Evan Cheng5928e692011-07-25 23:24:55 +000017#include "llvm/MC/MCAsmBackend.h"
Chris Lattner92ffdd12009-06-24 00:52:40 +000018#include "llvm/MC/MCContext.h"
Daniel Dunbar65105172009-08-27 00:51:57 +000019#include "llvm/MC/MCCodeEmitter.h"
Chris Lattner11b2fc92009-09-14 03:02:37 +000020#include "llvm/MC/MCInstPrinter.h"
Evan Chengc5e6d2f2011-07-11 03:57:24 +000021#include "llvm/MC/MCInstrInfo.h"
Evan Cheng76792992011-07-20 05:58:47 +000022#include "llvm/MC/MCObjectFileInfo.h"
23#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"
Evan Cheng8264e272011-06-29 01:14:12 +000028#include "llvm/MC/SubtargetFeature.h"
Chris Lattner92ffdd12009-06-24 00:52:40 +000029#include "llvm/ADT/OwningPtr.h"
Chris Lattner8dd8a522009-06-18 23:04:45 +000030#include "llvm/Support/CommandLine.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"
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"
Chris Lattnerd70e15b42009-06-21 05:22:37 +000036#include "llvm/Support/SourceMgr.h"
Dan Gohman0df7ea42010-10-07 20:32:40 +000037#include "llvm/Support/ToolOutputFile.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000038#include "llvm/Support/Host.h"
39#include "llvm/Support/Signals.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000040#include "llvm/Support/TargetRegistry.h"
41#include "llvm/Support/TargetSelect.h"
Michael J. Spencer7b6fef82010-12-09 17:36:48 +000042#include "llvm/Support/system_error.h"
Chris Lattnerb257d242009-12-22 22:50:29 +000043#include "Disassembler.h"
Chris Lattner8dd8a522009-06-18 23:04:45 +000044using namespace llvm;
45
46static cl::opt<std::string>
47InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
48
49static cl::opt<std::string>
50OutputFilename("o", cl::desc("Output filename"),
51 cl::value_desc("filename"));
52
Daniel Dunbard18dd1d2009-08-27 07:56:39 +000053static cl::opt<bool>
54ShowEncoding("show-encoding", cl::desc("Show instruction encodings"));
55
Daniel Dunbare3ee3322010-02-03 18:18:30 +000056static cl::opt<bool>
57ShowInst("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
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000156enum ActionType {
Chris Lattnerb0133452009-06-21 20:16:42 +0000157 AC_AsLex,
Sean Callanan7e645502009-12-17 01:49:59 +0000158 AC_Assemble,
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000159 AC_Disassemble,
160 AC_EDisassemble
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000161};
Chris Lattner8dd8a522009-06-18 23:04:45 +0000162
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000163static cl::opt<ActionType>
164Action(cl::desc("Action to perform:"),
Chris Lattnerb0133452009-06-21 20:16:42 +0000165 cl::init(AC_Assemble),
166 cl::values(clEnumValN(AC_AsLex, "as-lex",
167 "Lex tokens from a .s file"),
168 clEnumValN(AC_Assemble, "assemble",
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000169 "Assemble a .s file (default)"),
Sean Callanan7e645502009-12-17 01:49:59 +0000170 clEnumValN(AC_Disassemble, "disassemble",
171 "Disassemble strings of hex bytes"),
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000172 clEnumValN(AC_EDisassemble, "edis",
173 "Enhanced disassembly of strings of hex bytes"),
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000174 clEnumValEnd));
175
Kevin Enderbyf92f9902009-09-04 21:45:34 +0000176static const Target *GetTarget(const char *ProgName) {
Daniel Dunbar46892112010-03-13 02:20:38 +0000177 // Figure out the target triple.
178 if (TripleName.empty())
Sebastian Pop94441fb2011-11-01 21:32:20 +0000179 TripleName = sys::getDefaultTargetTriple();
Evan Chenge64f0e52011-07-26 19:02:16 +0000180 Triple TheTriple(Triple::normalize(TripleName));
181
182 const Target *TheTarget = 0;
Daniel Dunbar46892112010-03-13 02:20:38 +0000183 if (!ArchName.empty()) {
Evan Chenge64f0e52011-07-26 19:02:16 +0000184 for (TargetRegistry::iterator it = TargetRegistry::begin(),
185 ie = TargetRegistry::end(); it != ie; ++it) {
186 if (ArchName == it->getName()) {
187 TheTarget = &*it;
188 break;
189 }
190 }
191
192 if (!TheTarget) {
193 errs() << ProgName << ": error: invalid target '" << ArchName << "'.\n";
194 return 0;
195 }
196
197 // Adjust the triple to match (if known), otherwise stick with the
198 // module/host triple.
199 Triple::ArchType Type = Triple::getArchTypeForLLVMName(ArchName);
200 if (Type != Triple::UnknownArch)
201 TheTriple.setArch(Type);
202 } else {
203 // Get the target specific parser.
204 std::string Error;
205 TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), Error);
206 if (TheTarget == 0) {
207 errs() << ProgName << ": error: unable to get target for '"
208 << TheTriple.getTriple()
209 << "', see --version and --triple.\n";
210 return 0;
211 }
Daniel Dunbar46892112010-03-13 02:20:38 +0000212 }
213
Evan Chenge64f0e52011-07-26 19:02:16 +0000214 TripleName = TheTriple.getTriple();
215 return TheTarget;
Kevin Enderbyf92f9902009-09-04 21:45:34 +0000216}
217
Dan Gohmana2233f22010-09-01 14:20:41 +0000218static tool_output_file *GetOutputStream() {
Dan Gohman268b0f42010-08-20 01:07:01 +0000219 if (OutputFilename == "")
220 OutputFilename = "-";
221
222 std::string Err;
223 tool_output_file *Out = new tool_output_file(OutputFilename.c_str(), Err,
224 raw_fd_ostream::F_Binary);
225 if (!Err.empty()) {
226 errs() << Err << '\n';
227 delete Out;
228 return 0;
229 }
Dan Gohmana2233f22010-09-01 14:20:41 +0000230
231 return Out;
Dan Gohman268b0f42010-08-20 01:07:01 +0000232}
233
Kevin Enderbye7739d42011-12-09 18:09:40 +0000234static std::string DwarfDebugFlags;
235static void setDwarfDebugFlags(int argc, char **argv) {
236 if (!getenv("RC_DEBUG_OPTIONS"))
237 return;
238 for (int i = 0; i < argc; i++) {
239 DwarfDebugFlags += argv[i];
240 if (i + 1 < argc)
241 DwarfDebugFlags += " ";
242 }
243}
244
Chris Lattnerb0133452009-06-21 20:16:42 +0000245static int AsLexInput(const char *ProgName) {
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +0000246 OwningPtr<MemoryBuffer> BufferPtr;
247 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename, BufferPtr)) {
Michael J. Spencer7b6fef82010-12-09 17:36:48 +0000248 errs() << ProgName << ": " << ec.message() << '\n';
Chris Lattner8dd8a522009-06-18 23:04:45 +0000249 return 1;
250 }
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +0000251 MemoryBuffer *Buffer = BufferPtr.take();
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000252
253 SourceMgr SrcMgr;
Jim Grosbach112a2de2011-05-09 20:05:25 +0000254
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000255 // Tell SrcMgr about this buffer, which is what TGParser will pick up.
256 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
Jim Grosbach112a2de2011-05-09 20:05:25 +0000257
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000258 // Record the location of the include directories so that the lexer can find
259 // it later.
260 SrcMgr.setIncludeDirs(IncludeDirs);
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +0000261
Kevin Enderbyf92f9902009-09-04 21:45:34 +0000262 const Target *TheTarget = GetTarget(ProgName);
263 if (!TheTarget)
264 return 1;
265
Evan Cheng1705ab02011-07-14 23:50:31 +0000266 llvm::OwningPtr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(TripleName));
Kevin Enderbyf92f9902009-09-04 21:45:34 +0000267 assert(MAI && "Unable to create target asm info!");
268
Sean Callanan7a77eae2010-01-21 00:19:58 +0000269 AsmLexer Lexer(*MAI);
Daniel Dunbar7a85f9c2010-07-18 18:31:28 +0000270 Lexer.setBuffer(SrcMgr.getMemoryBuffer(0));
271
Dan Gohmana2233f22010-09-01 14:20:41 +0000272 OwningPtr<tool_output_file> Out(GetOutputStream());
Dan Gohman268b0f42010-08-20 01:07:01 +0000273 if (!Out)
274 return 1;
275
Chris Lattnerb0133452009-06-21 20:16:42 +0000276 bool Error = false;
Daniel Dunbarbc798162009-07-28 16:56:42 +0000277 while (Lexer.Lex().isNot(AsmToken::Eof)) {
Daniel Dunbar1601f552010-10-25 20:18:46 +0000278 AsmToken Tok = Lexer.getTok();
279
280 switch (Tok.getKind()) {
Chris Lattnerb0133452009-06-21 20:16:42 +0000281 default:
Chris Lattner03b80a42011-10-16 05:43:57 +0000282 SrcMgr.PrintMessage(Lexer.getLoc(), SourceMgr::DK_Warning,
283 "unknown token");
Chris Lattnerb0133452009-06-21 20:16:42 +0000284 Error = true;
285 break;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000286 case AsmToken::Error:
Chris Lattnerb0133452009-06-21 20:16:42 +0000287 Error = true; // error already printed.
Chris Lattnerd0765612009-06-21 19:21:25 +0000288 break;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000289 case AsmToken::Identifier:
Daniel Dunbar1601f552010-10-25 20:18:46 +0000290 Out->os() << "identifier: " << Lexer.getTok().getString();
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +0000291 break;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000292 case AsmToken::Integer:
Daniel Dunbar1601f552010-10-25 20:18:46 +0000293 Out->os() << "int: " << Lexer.getTok().getString();
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +0000294 break;
Daniel Dunbar3068a932010-09-24 01:59:31 +0000295 case AsmToken::Real:
Daniel Dunbar1601f552010-10-25 20:18:46 +0000296 Out->os() << "real: " << Lexer.getTok().getString();
Daniel Dunbar3068a932010-09-24 01:59:31 +0000297 break;
Daniel Dunbar25c2c622010-09-16 00:42:35 +0000298 case AsmToken::Register:
Daniel Dunbar1601f552010-10-25 20:18:46 +0000299 Out->os() << "register: " << Lexer.getTok().getRegVal();
Daniel Dunbar25c2c622010-09-16 00:42:35 +0000300 break;
301 case AsmToken::String:
Daniel Dunbar1601f552010-10-25 20:18:46 +0000302 Out->os() << "string: " << Lexer.getTok().getString();
Daniel Dunbar25c2c622010-09-16 00:42:35 +0000303 break;
Daniel Dunbar9c4809a2009-07-01 06:56:54 +0000304
Daniel Dunbar1601f552010-10-25 20:18:46 +0000305 case AsmToken::Amp: Out->os() << "Amp"; break;
306 case AsmToken::AmpAmp: Out->os() << "AmpAmp"; break;
307 case AsmToken::At: Out->os() << "At"; break;
308 case AsmToken::Caret: Out->os() << "Caret"; break;
309 case AsmToken::Colon: Out->os() << "Colon"; break;
310 case AsmToken::Comma: Out->os() << "Comma"; break;
311 case AsmToken::Dollar: Out->os() << "Dollar"; break;
312 case AsmToken::Dot: Out->os() << "Dot"; break;
313 case AsmToken::EndOfStatement: Out->os() << "EndOfStatement"; break;
314 case AsmToken::Eof: Out->os() << "Eof"; break;
315 case AsmToken::Equal: Out->os() << "Equal"; break;
316 case AsmToken::EqualEqual: Out->os() << "EqualEqual"; break;
317 case AsmToken::Exclaim: Out->os() << "Exclaim"; break;
318 case AsmToken::ExclaimEqual: Out->os() << "ExclaimEqual"; break;
319 case AsmToken::Greater: Out->os() << "Greater"; break;
320 case AsmToken::GreaterEqual: Out->os() << "GreaterEqual"; break;
321 case AsmToken::GreaterGreater: Out->os() << "GreaterGreater"; break;
322 case AsmToken::Hash: Out->os() << "Hash"; break;
323 case AsmToken::LBrac: Out->os() << "LBrac"; break;
324 case AsmToken::LCurly: Out->os() << "LCurly"; break;
325 case AsmToken::LParen: Out->os() << "LParen"; break;
326 case AsmToken::Less: Out->os() << "Less"; break;
327 case AsmToken::LessEqual: Out->os() << "LessEqual"; break;
328 case AsmToken::LessGreater: Out->os() << "LessGreater"; break;
329 case AsmToken::LessLess: Out->os() << "LessLess"; break;
330 case AsmToken::Minus: Out->os() << "Minus"; break;
331 case AsmToken::Percent: Out->os() << "Percent"; break;
332 case AsmToken::Pipe: Out->os() << "Pipe"; break;
333 case AsmToken::PipePipe: Out->os() << "PipePipe"; break;
334 case AsmToken::Plus: Out->os() << "Plus"; break;
335 case AsmToken::RBrac: Out->os() << "RBrac"; break;
336 case AsmToken::RCurly: Out->os() << "RCurly"; break;
337 case AsmToken::RParen: Out->os() << "RParen"; break;
338 case AsmToken::Slash: Out->os() << "Slash"; break;
339 case AsmToken::Star: Out->os() << "Star"; break;
340 case AsmToken::Tilde: Out->os() << "Tilde"; break;
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +0000341 }
Daniel Dunbar1601f552010-10-25 20:18:46 +0000342
343 // Print the token string.
344 Out->os() << " (\"";
345 Out->os().write_escaped(Tok.getString());
346 Out->os() << "\")\n";
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +0000347 }
Dan Gohman268b0f42010-08-20 01:07:01 +0000348
349 // Keep output if no errors.
350 if (Error == 0) Out->keep();
Jim Grosbach112a2de2011-05-09 20:05:25 +0000351
Chris Lattnerb0133452009-06-21 20:16:42 +0000352 return Error;
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000353}
354
Daniel Dunbarf59ee962009-07-28 20:47:52 +0000355static int AssembleInput(const char *ProgName) {
Daniel Dunbar80d484e2009-08-14 03:48:55 +0000356 const Target *TheTarget = GetTarget(ProgName);
357 if (!TheTarget)
358 return 1;
359
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +0000360 OwningPtr<MemoryBuffer> BufferPtr;
361 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename, BufferPtr)) {
Michael J. Spencer7b6fef82010-12-09 17:36:48 +0000362 errs() << ProgName << ": " << ec.message() << '\n';
Chris Lattnerb0133452009-06-21 20:16:42 +0000363 return 1;
364 }
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +0000365 MemoryBuffer *Buffer = BufferPtr.take();
Jim Grosbach112a2de2011-05-09 20:05:25 +0000366
Chris Lattnerb0133452009-06-21 20:16:42 +0000367 SourceMgr SrcMgr;
Jim Grosbach112a2de2011-05-09 20:05:25 +0000368
Daniel Dunbar5c947db2009-08-19 16:25:53 +0000369 // Tell SrcMgr about this buffer, which is what the parser will pick up.
Chris Lattnerb0133452009-06-21 20:16:42 +0000370 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
Jim Grosbach112a2de2011-05-09 20:05:25 +0000371
Chris Lattnerb0133452009-06-21 20:16:42 +0000372 // Record the location of the include directories so that the lexer can find
373 // it later.
374 SrcMgr.setIncludeDirs(IncludeDirs);
Jim Grosbach112a2de2011-05-09 20:05:25 +0000375
376
Evan Cheng1705ab02011-07-14 23:50:31 +0000377 llvm::OwningPtr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(TripleName));
Chris Lattner768ea2a2010-03-11 22:53:35 +0000378 assert(MAI && "Unable to create target asm info!");
Jim Grosbach112a2de2011-05-09 20:05:25 +0000379
Evan Chengd60fa58b2011-07-18 20:57:22 +0000380 llvm::OwningPtr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
381 assert(MRI && "Unable to create target register info!");
382
Evan Cheng76792992011-07-20 05:58:47 +0000383 // FIXME: This is not pretty. MCContext has a ptr to MCObjectFileInfo and
384 // MCObjectFileInfo needs a MCContext reference in order to initialize itself.
385 OwningPtr<MCObjectFileInfo> MOFI(new MCObjectFileInfo());
Jim Grosbach8ff25d62012-01-26 23:20:07 +0000386 MCContext Ctx(*MAI, *MRI, MOFI.get(), &SrcMgr);
Evan Chengbbf3b0d2011-07-20 19:50:42 +0000387 MOFI->InitMCObjectFileInfo(TripleName, RelocModel, CMModel, Ctx);
Evan Cheng76792992011-07-20 05:58:47 +0000388
Daniel Dunbar4ee0d032011-03-28 22:49:15 +0000389 if (SaveTempLabels)
390 Ctx.setAllowTemporaryLabels(false);
Rafael Espindola0a017a62010-12-10 07:39:47 +0000391
Kevin Enderby6469fc22011-11-01 22:27:22 +0000392 Ctx.setGenDwarfForAssembly(GenDwarfForAssembly);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000393 if (!DwarfDebugFlags.empty())
394 Ctx.setDwarfDebugFlags(StringRef(DwarfDebugFlags));
Kevin Enderby6469fc22011-11-01 22:27:22 +0000395
James Molloy4c493e82011-09-07 17:24:38 +0000396 // Package up features to be passed to target/subtarget
397 std::string FeaturesStr;
398 if (MAttrs.size()) {
399 SubtargetFeatures Features;
400 for (unsigned i = 0; i != MAttrs.size(); ++i)
401 Features.AddFeature(MAttrs[i]);
402 FeaturesStr = Features.getString();
403 }
404
Dan Gohmana2233f22010-09-01 14:20:41 +0000405 OwningPtr<tool_output_file> Out(GetOutputStream());
Dan Gohman268b0f42010-08-20 01:07:01 +0000406 if (!Out)
407 return 1;
408
Dan Gohmana2233f22010-09-01 14:20:41 +0000409 formatted_raw_ostream FOS(Out->os());
Daniel Dunbar3016db32009-08-21 09:11:24 +0000410 OwningPtr<MCStreamer> Str;
Chris Lattnera61e93d2009-08-17 04:23:44 +0000411
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000412 OwningPtr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo());
Evan Cheng91111d22011-07-09 05:47:46 +0000413 OwningPtr<MCSubtargetInfo>
414 STI(TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr));
415
Rafael Espindolab3eca9b2011-01-23 17:55:27 +0000416 // FIXME: There is a bit of code duplication with addPassesToEmitFile.
Kevin Enderbyf92f9902009-09-04 21:45:34 +0000417 if (FileType == OFT_AssemblyFile) {
Chris Lattner90a78592010-03-19 05:48:53 +0000418 MCInstPrinter *IP =
Craig Topper54bfde72012-04-02 06:09:36 +0000419 TheTarget->createMCInstPrinter(OutputAsmVariant, *MAI, *MCII, *MRI, *STI);
Benjamin Kramera3e0ddb2010-07-29 17:48:06 +0000420 MCCodeEmitter *CE = 0;
Evan Cheng5928e692011-07-25 23:24:55 +0000421 MCAsmBackend *MAB = 0;
Daniel Dunbarecd0c8a2010-12-16 03:05:59 +0000422 if (ShowEncoding) {
Evan Cheng3a792252011-07-26 00:42:34 +0000423 CE = TheTarget->createMCCodeEmitter(*MCII, *STI, Ctx);
Evan Cheng5928e692011-07-25 23:24:55 +0000424 MAB = TheTarget->createMCAsmBackend(TripleName);
Daniel Dunbarecd0c8a2010-12-16 03:05:59 +0000425 }
Rafael Espindola0a017a62010-12-10 07:39:47 +0000426 Str.reset(TheTarget->createAsmStreamer(Ctx, FOS, /*asmverbose*/true,
Rafael Espindolaa3181d12011-04-30 03:44:37 +0000427 /*useLoc*/ true,
Nick Lewycky40f8f2f2011-10-17 23:05:28 +0000428 /*useCFI*/ true,
429 /*useDwarfDirectory*/ true,
430 IP, CE, MAB, ShowInst));
Jim Grosbach78309c42011-12-05 23:20:14 +0000431
Daniel Dunbarb09b8902010-03-23 23:47:12 +0000432 } else if (FileType == OFT_Null) {
433 Str.reset(createNullStreamer(Ctx));
Daniel Dunbar3016db32009-08-21 09:11:24 +0000434 } else {
435 assert(FileType == OFT_ObjectFile && "Invalid file type!");
Evan Cheng3a792252011-07-26 00:42:34 +0000436 MCCodeEmitter *CE = TheTarget->createMCCodeEmitter(*MCII, *STI, Ctx);
Evan Cheng5928e692011-07-25 23:24:55 +0000437 MCAsmBackend *MAB = TheTarget->createMCAsmBackend(TripleName);
Evan Cheng3a792252011-07-26 00:42:34 +0000438 Str.reset(TheTarget->createMCObjectStreamer(TripleName, Ctx, *MAB,
439 FOS, CE, RelaxAll,
440 NoExecStack));
Daniel Dunbar3016db32009-08-21 09:11:24 +0000441 }
Daniel Dunbara10e5192009-06-24 23:30:00 +0000442
Jim Grosbach345768c2011-08-16 18:33:49 +0000443 OwningPtr<MCAsmParser> Parser(createMCAsmParser(SrcMgr, Ctx,
444 *Str.get(), *MAI));
Evan Cheng11424442011-07-26 00:24:13 +0000445 OwningPtr<MCTargetAsmParser> TAP(TheTarget->createMCAsmParser(*STI, *Parser));
Daniel Dunbar80d484e2009-08-14 03:48:55 +0000446 if (!TAP) {
Jim Grosbach112a2de2011-05-09 20:05:25 +0000447 errs() << ProgName
Daniel Dunbar80d484e2009-08-14 03:48:55 +0000448 << ": error: this target does not support assembly parsing.\n";
Daniel Dunbarf59ee962009-07-28 20:47:52 +0000449 return 1;
Daniel Dunbar80d484e2009-08-14 03:48:55 +0000450 }
451
Daniel Dunbar2eca0252010-08-11 06:37:09 +0000452 Parser->setShowParsedOperands(ShowInstOperands);
Daniel Dunbar7f5bf5a2010-07-18 18:31:33 +0000453 Parser->setTargetParser(*TAP.get());
Daniel Dunbarcd4eee52009-08-11 04:34:48 +0000454
Daniel Dunbar7f5bf5a2010-07-18 18:31:33 +0000455 int Res = Parser->Run(NoInitialTextSection);
Daniel Dunbarcd4eee52009-08-11 04:34:48 +0000456
Dan Gohman268b0f42010-08-20 01:07:01 +0000457 // Keep output if no errors.
458 if (Res == 0) Out->keep();
Daniel Dunbar14487342010-03-13 19:31:47 +0000459
Daniel Dunbarcd4eee52009-08-11 04:34:48 +0000460 return Res;
Sean Callanan7e645502009-12-17 01:49:59 +0000461}
462
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000463static int DisassembleInput(const char *ProgName, bool Enhanced) {
Daniel Dunbarabf6e362010-03-19 18:07:50 +0000464 const Target *TheTarget = GetTarget(ProgName);
465 if (!TheTarget)
Sean Callanan7e645502009-12-17 01:49:59 +0000466 return 0;
Sean Callanan7e645502009-12-17 01:49:59 +0000467
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +0000468 OwningPtr<MemoryBuffer> Buffer;
469 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename, Buffer)) {
Michael J. Spencer7b6fef82010-12-09 17:36:48 +0000470 errs() << ProgName << ": " << ec.message() << '\n';
Sean Callanan7e645502009-12-17 01:49:59 +0000471 return 1;
472 }
Jim Grosbach112a2de2011-05-09 20:05:25 +0000473
Dan Gohmana2233f22010-09-01 14:20:41 +0000474 OwningPtr<tool_output_file> Out(GetOutputStream());
Dan Gohman268b0f42010-08-20 01:07:01 +0000475 if (!Out)
476 return 1;
477
478 int Res;
Bill Wendling00f0cdd2011-03-21 04:13:46 +0000479 if (Enhanced) {
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +0000480 Res =
481 Disassembler::disassembleEnhanced(TripleName, *Buffer.take(), Out->os());
Bill Wendling00f0cdd2011-03-21 04:13:46 +0000482 } else {
James Molloy4c493e82011-09-07 17:24:38 +0000483 // Package up features to be passed to target/subtarget
484 std::string FeaturesStr;
485 if (MAttrs.size()) {
486 SubtargetFeatures Features;
487 for (unsigned i = 0; i != MAttrs.size(); ++i)
488 Features.AddFeature(MAttrs[i]);
489 FeaturesStr = Features.getString();
490 }
491
492 Res = Disassembler::disassemble(*TheTarget, TripleName, MCPU, FeaturesStr,
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +0000493 *Buffer.take(), Out->os());
Bill Wendling00f0cdd2011-03-21 04:13:46 +0000494 }
Dan Gohman268b0f42010-08-20 01:07:01 +0000495
496 // Keep output if no errors.
497 if (Res == 0) Out->keep();
498
499 return Res;
Sean Callanan7e645502009-12-17 01:49:59 +0000500}
Chris Lattnerb0133452009-06-21 20:16:42 +0000501
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000502
503int main(int argc, char **argv) {
504 // Print a stack trace if we signal out.
505 sys::PrintStackTraceOnErrorSignal();
506 PrettyStackTraceProgram X(argc, argv);
507 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Daniel Dunbar8c6bad22009-07-17 22:38:58 +0000508
Daniel Dunbar80d484e2009-08-14 03:48:55 +0000509 // Initialize targets and assembly printers/parsers.
Daniel Dunbar8c6bad22009-07-17 22:38:58 +0000510 llvm::InitializeAllTargetInfos();
Evan Cheng8c886a42011-07-22 21:58:54 +0000511 llvm::InitializeAllTargetMCs();
Daniel Dunbar8c6bad22009-07-17 22:38:58 +0000512 llvm::InitializeAllAsmParsers();
Sean Callanan7e645502009-12-17 01:49:59 +0000513 llvm::InitializeAllDisassemblers();
Jim Grosbach112a2de2011-05-09 20:05:25 +0000514
Duncan Sands48992552012-03-08 14:24:32 +0000515 // Register the target printer for --version.
516 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
517
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000518 cl::ParseCommandLineOptions(argc, argv, "llvm machine code playground\n");
Duncan Sands3bd97fe2010-08-28 01:30:02 +0000519 TripleName = Triple::normalize(TripleName);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000520 setDwarfDebugFlags(argc, argv);
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000521
522 switch (Action) {
Chris Lattnerb0133452009-06-21 20:16:42 +0000523 case AC_AsLex:
524 return AsLexInput(argv[0]);
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000525 case AC_Assemble:
526 return AssembleInput(argv[0]);
Sean Callanan7e645502009-12-17 01:49:59 +0000527 case AC_Disassemble:
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000528 return DisassembleInput(argv[0], false);
529 case AC_EDisassemble:
530 return DisassembleInput(argv[0], true);
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000531 }
Chris Lattner8dd8a522009-06-18 23:04:45 +0000532}