blob: 280a1bbd9967f5f37abd8bad9eb73924f4a97adc [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
73static cl::opt<bool>
Daniel Dunbar3ff1a062010-05-23 17:44:06 +000074EnableLogging("enable-api-logging", cl::desc("Enable MC API logging"));
75
Daniel Dunbar3016db32009-08-21 09:11:24 +000076enum OutputFileType {
Daniel Dunbarb09b8902010-03-23 23:47:12 +000077 OFT_Null,
Daniel Dunbar3016db32009-08-21 09:11:24 +000078 OFT_AssemblyFile,
79 OFT_ObjectFile
80};
81static cl::opt<OutputFileType>
82FileType("filetype", cl::init(OFT_AssemblyFile),
83 cl::desc("Choose an output file type:"),
84 cl::values(
85 clEnumValN(OFT_AssemblyFile, "asm",
86 "Emit an assembly ('.s') file"),
Daniel Dunbarb09b8902010-03-23 23:47:12 +000087 clEnumValN(OFT_Null, "null",
88 "Don't emit anything (for timing purposes)"),
Daniel Dunbar3016db32009-08-21 09:11:24 +000089 clEnumValN(OFT_ObjectFile, "obj",
90 "Emit a native object ('.o') file"),
91 clEnumValEnd));
92
Chris Lattnerd70e15b42009-06-21 05:22:37 +000093static cl::list<std::string>
94IncludeDirs("I", cl::desc("Directory of include files"),
95 cl::value_desc("directory"), cl::Prefix);
Chris Lattner8dd8a522009-06-18 23:04:45 +000096
Daniel Dunbar8c6bad22009-07-17 22:38:58 +000097static cl::opt<std::string>
Daniel Dunbar46892112010-03-13 02:20:38 +000098ArchName("arch", cl::desc("Target arch to assemble for, "
Bill Wendlinge3031142011-06-17 20:35:21 +000099 "see -version for available targets"));
Daniel Dunbar46892112010-03-13 02:20:38 +0000100
101static cl::opt<std::string>
Nick Lewyckyb2449092009-11-01 22:07:54 +0000102TripleName("triple", cl::desc("Target triple to assemble for, "
Daniel Dunbar46892112010-03-13 02:20:38 +0000103 "see -version for available targets"));
Daniel Dunbar8c6bad22009-07-17 22:38:58 +0000104
Jim Grosbach685b7732010-10-30 15:57:50 +0000105static cl::opt<std::string>
106MCPU("mcpu",
107 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
108 cl::value_desc("cpu-name"),
109 cl::init(""));
110
James Molloy4c493e82011-09-07 17:24:38 +0000111static cl::list<std::string>
112MAttrs("mattr",
113 cl::CommaSeparated,
114 cl::desc("Target specific attributes (-mattr=help for details)"),
115 cl::value_desc("a1,+a2,-a3,..."));
116
Evan Cheng2129f592011-07-19 06:37:02 +0000117static cl::opt<Reloc::Model>
118RelocModel("relocation-model",
119 cl::desc("Choose relocation model"),
120 cl::init(Reloc::Default),
121 cl::values(
122 clEnumValN(Reloc::Default, "default",
123 "Target default relocation model"),
124 clEnumValN(Reloc::Static, "static",
125 "Non-relocatable code"),
126 clEnumValN(Reloc::PIC_, "pic",
127 "Fully relocatable, position independent code"),
128 clEnumValN(Reloc::DynamicNoPIC, "dynamic-no-pic",
129 "Relocatable external references, non-relocatable code"),
130 clEnumValEnd));
131
Evan Chengefd9b422011-07-20 07:51:56 +0000132static cl::opt<llvm::CodeModel::Model>
133CMModel("code-model",
134 cl::desc("Choose code model"),
135 cl::init(CodeModel::Default),
136 cl::values(clEnumValN(CodeModel::Default, "default",
137 "Target default code model"),
138 clEnumValN(CodeModel::Small, "small",
139 "Small code model"),
140 clEnumValN(CodeModel::Kernel, "kernel",
141 "Kernel code model"),
142 clEnumValN(CodeModel::Medium, "medium",
143 "Medium code model"),
144 clEnumValN(CodeModel::Large, "large",
145 "Large code model"),
146 clEnumValEnd));
147
Daniel Dunbar322fec62010-03-13 02:20:57 +0000148static cl::opt<bool>
Bill Wendlinge3031142011-06-17 20:35:21 +0000149NoInitialTextSection("n", cl::desc("Don't assume assembly file starts "
150 "in the text section"));
Daniel Dunbar322fec62010-03-13 02:20:57 +0000151
Daniel Dunbar4ee0d032011-03-28 22:49:15 +0000152static cl::opt<bool>
Bill Wendlinge3031142011-06-17 20:35:21 +0000153SaveTempLabels("L", cl::desc("Don't discard temporary labels"));
Daniel Dunbar4ee0d032011-03-28 22:49:15 +0000154
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000155enum ActionType {
Chris Lattnerb0133452009-06-21 20:16:42 +0000156 AC_AsLex,
Sean Callanan7e645502009-12-17 01:49:59 +0000157 AC_Assemble,
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000158 AC_Disassemble,
159 AC_EDisassemble
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000160};
Chris Lattner8dd8a522009-06-18 23:04:45 +0000161
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000162static cl::opt<ActionType>
163Action(cl::desc("Action to perform:"),
Chris Lattnerb0133452009-06-21 20:16:42 +0000164 cl::init(AC_Assemble),
165 cl::values(clEnumValN(AC_AsLex, "as-lex",
166 "Lex tokens from a .s file"),
167 clEnumValN(AC_Assemble, "assemble",
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000168 "Assemble a .s file (default)"),
Sean Callanan7e645502009-12-17 01:49:59 +0000169 clEnumValN(AC_Disassemble, "disassemble",
170 "Disassemble strings of hex bytes"),
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000171 clEnumValN(AC_EDisassemble, "edis",
172 "Enhanced disassembly of strings of hex bytes"),
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000173 clEnumValEnd));
174
Kevin Enderbyf92f9902009-09-04 21:45:34 +0000175static const Target *GetTarget(const char *ProgName) {
Daniel Dunbar46892112010-03-13 02:20:38 +0000176 // Figure out the target triple.
177 if (TripleName.empty())
178 TripleName = sys::getHostTriple();
Evan Chenge64f0e52011-07-26 19:02:16 +0000179 Triple TheTriple(Triple::normalize(TripleName));
180
181 const Target *TheTarget = 0;
Daniel Dunbar46892112010-03-13 02:20:38 +0000182 if (!ArchName.empty()) {
Evan Chenge64f0e52011-07-26 19:02:16 +0000183 for (TargetRegistry::iterator it = TargetRegistry::begin(),
184 ie = TargetRegistry::end(); it != ie; ++it) {
185 if (ArchName == it->getName()) {
186 TheTarget = &*it;
187 break;
188 }
189 }
190
191 if (!TheTarget) {
192 errs() << ProgName << ": error: invalid target '" << ArchName << "'.\n";
193 return 0;
194 }
195
196 // Adjust the triple to match (if known), otherwise stick with the
197 // module/host triple.
198 Triple::ArchType Type = Triple::getArchTypeForLLVMName(ArchName);
199 if (Type != Triple::UnknownArch)
200 TheTriple.setArch(Type);
201 } else {
202 // Get the target specific parser.
203 std::string Error;
204 TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), Error);
205 if (TheTarget == 0) {
206 errs() << ProgName << ": error: unable to get target for '"
207 << TheTriple.getTriple()
208 << "', see --version and --triple.\n";
209 return 0;
210 }
Daniel Dunbar46892112010-03-13 02:20:38 +0000211 }
212
Evan Chenge64f0e52011-07-26 19:02:16 +0000213 TripleName = TheTriple.getTriple();
214 return TheTarget;
Kevin Enderbyf92f9902009-09-04 21:45:34 +0000215}
216
Dan Gohmana2233f22010-09-01 14:20:41 +0000217static tool_output_file *GetOutputStream() {
Dan Gohman268b0f42010-08-20 01:07:01 +0000218 if (OutputFilename == "")
219 OutputFilename = "-";
220
221 std::string Err;
222 tool_output_file *Out = new tool_output_file(OutputFilename.c_str(), Err,
223 raw_fd_ostream::F_Binary);
224 if (!Err.empty()) {
225 errs() << Err << '\n';
226 delete Out;
227 return 0;
228 }
Dan Gohmana2233f22010-09-01 14:20:41 +0000229
230 return Out;
Dan Gohman268b0f42010-08-20 01:07:01 +0000231}
232
Chris Lattnerb0133452009-06-21 20:16:42 +0000233static int AsLexInput(const char *ProgName) {
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +0000234 OwningPtr<MemoryBuffer> BufferPtr;
235 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename, BufferPtr)) {
Michael J. Spencer7b6fef82010-12-09 17:36:48 +0000236 errs() << ProgName << ": " << ec.message() << '\n';
Chris Lattner8dd8a522009-06-18 23:04:45 +0000237 return 1;
238 }
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +0000239 MemoryBuffer *Buffer = BufferPtr.take();
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000240
241 SourceMgr SrcMgr;
Jim Grosbach112a2de2011-05-09 20:05:25 +0000242
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000243 // Tell SrcMgr about this buffer, which is what TGParser will pick up.
244 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
Jim Grosbach112a2de2011-05-09 20:05:25 +0000245
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000246 // Record the location of the include directories so that the lexer can find
247 // it later.
248 SrcMgr.setIncludeDirs(IncludeDirs);
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +0000249
Kevin Enderbyf92f9902009-09-04 21:45:34 +0000250 const Target *TheTarget = GetTarget(ProgName);
251 if (!TheTarget)
252 return 1;
253
Evan Cheng1705ab02011-07-14 23:50:31 +0000254 llvm::OwningPtr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(TripleName));
Kevin Enderbyf92f9902009-09-04 21:45:34 +0000255 assert(MAI && "Unable to create target asm info!");
256
Sean Callanan7a77eae2010-01-21 00:19:58 +0000257 AsmLexer Lexer(*MAI);
Daniel Dunbar7a85f9c2010-07-18 18:31:28 +0000258 Lexer.setBuffer(SrcMgr.getMemoryBuffer(0));
259
Dan Gohmana2233f22010-09-01 14:20:41 +0000260 OwningPtr<tool_output_file> Out(GetOutputStream());
Dan Gohman268b0f42010-08-20 01:07:01 +0000261 if (!Out)
262 return 1;
263
Chris Lattnerb0133452009-06-21 20:16:42 +0000264 bool Error = false;
Daniel Dunbarbc798162009-07-28 16:56:42 +0000265 while (Lexer.Lex().isNot(AsmToken::Eof)) {
Daniel Dunbar1601f552010-10-25 20:18:46 +0000266 AsmToken Tok = Lexer.getTok();
267
268 switch (Tok.getKind()) {
Chris Lattnerb0133452009-06-21 20:16:42 +0000269 default:
Chris Lattner03b80a42011-10-16 05:43:57 +0000270 SrcMgr.PrintMessage(Lexer.getLoc(), SourceMgr::DK_Warning,
271 "unknown token");
Chris Lattnerb0133452009-06-21 20:16:42 +0000272 Error = true;
273 break;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000274 case AsmToken::Error:
Chris Lattnerb0133452009-06-21 20:16:42 +0000275 Error = true; // error already printed.
Chris Lattnerd0765612009-06-21 19:21:25 +0000276 break;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000277 case AsmToken::Identifier:
Daniel Dunbar1601f552010-10-25 20:18:46 +0000278 Out->os() << "identifier: " << Lexer.getTok().getString();
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +0000279 break;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000280 case AsmToken::Integer:
Daniel Dunbar1601f552010-10-25 20:18:46 +0000281 Out->os() << "int: " << Lexer.getTok().getString();
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +0000282 break;
Daniel Dunbar3068a932010-09-24 01:59:31 +0000283 case AsmToken::Real:
Daniel Dunbar1601f552010-10-25 20:18:46 +0000284 Out->os() << "real: " << Lexer.getTok().getString();
Daniel Dunbar3068a932010-09-24 01:59:31 +0000285 break;
Daniel Dunbar25c2c622010-09-16 00:42:35 +0000286 case AsmToken::Register:
Daniel Dunbar1601f552010-10-25 20:18:46 +0000287 Out->os() << "register: " << Lexer.getTok().getRegVal();
Daniel Dunbar25c2c622010-09-16 00:42:35 +0000288 break;
289 case AsmToken::String:
Daniel Dunbar1601f552010-10-25 20:18:46 +0000290 Out->os() << "string: " << Lexer.getTok().getString();
Daniel Dunbar25c2c622010-09-16 00:42:35 +0000291 break;
Daniel Dunbar9c4809a2009-07-01 06:56:54 +0000292
Daniel Dunbar1601f552010-10-25 20:18:46 +0000293 case AsmToken::Amp: Out->os() << "Amp"; break;
294 case AsmToken::AmpAmp: Out->os() << "AmpAmp"; break;
295 case AsmToken::At: Out->os() << "At"; break;
296 case AsmToken::Caret: Out->os() << "Caret"; break;
297 case AsmToken::Colon: Out->os() << "Colon"; break;
298 case AsmToken::Comma: Out->os() << "Comma"; break;
299 case AsmToken::Dollar: Out->os() << "Dollar"; break;
300 case AsmToken::Dot: Out->os() << "Dot"; break;
301 case AsmToken::EndOfStatement: Out->os() << "EndOfStatement"; break;
302 case AsmToken::Eof: Out->os() << "Eof"; break;
303 case AsmToken::Equal: Out->os() << "Equal"; break;
304 case AsmToken::EqualEqual: Out->os() << "EqualEqual"; break;
305 case AsmToken::Exclaim: Out->os() << "Exclaim"; break;
306 case AsmToken::ExclaimEqual: Out->os() << "ExclaimEqual"; break;
307 case AsmToken::Greater: Out->os() << "Greater"; break;
308 case AsmToken::GreaterEqual: Out->os() << "GreaterEqual"; break;
309 case AsmToken::GreaterGreater: Out->os() << "GreaterGreater"; break;
310 case AsmToken::Hash: Out->os() << "Hash"; break;
311 case AsmToken::LBrac: Out->os() << "LBrac"; break;
312 case AsmToken::LCurly: Out->os() << "LCurly"; break;
313 case AsmToken::LParen: Out->os() << "LParen"; break;
314 case AsmToken::Less: Out->os() << "Less"; break;
315 case AsmToken::LessEqual: Out->os() << "LessEqual"; break;
316 case AsmToken::LessGreater: Out->os() << "LessGreater"; break;
317 case AsmToken::LessLess: Out->os() << "LessLess"; break;
318 case AsmToken::Minus: Out->os() << "Minus"; break;
319 case AsmToken::Percent: Out->os() << "Percent"; break;
320 case AsmToken::Pipe: Out->os() << "Pipe"; break;
321 case AsmToken::PipePipe: Out->os() << "PipePipe"; break;
322 case AsmToken::Plus: Out->os() << "Plus"; break;
323 case AsmToken::RBrac: Out->os() << "RBrac"; break;
324 case AsmToken::RCurly: Out->os() << "RCurly"; break;
325 case AsmToken::RParen: Out->os() << "RParen"; break;
326 case AsmToken::Slash: Out->os() << "Slash"; break;
327 case AsmToken::Star: Out->os() << "Star"; break;
328 case AsmToken::Tilde: Out->os() << "Tilde"; break;
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +0000329 }
Daniel Dunbar1601f552010-10-25 20:18:46 +0000330
331 // Print the token string.
332 Out->os() << " (\"";
333 Out->os().write_escaped(Tok.getString());
334 Out->os() << "\")\n";
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +0000335 }
Dan Gohman268b0f42010-08-20 01:07:01 +0000336
337 // Keep output if no errors.
338 if (Error == 0) Out->keep();
Jim Grosbach112a2de2011-05-09 20:05:25 +0000339
Chris Lattnerb0133452009-06-21 20:16:42 +0000340 return Error;
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000341}
342
Daniel Dunbarf59ee962009-07-28 20:47:52 +0000343static int AssembleInput(const char *ProgName) {
Daniel Dunbar80d484e2009-08-14 03:48:55 +0000344 const Target *TheTarget = GetTarget(ProgName);
345 if (!TheTarget)
346 return 1;
347
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +0000348 OwningPtr<MemoryBuffer> BufferPtr;
349 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename, BufferPtr)) {
Michael J. Spencer7b6fef82010-12-09 17:36:48 +0000350 errs() << ProgName << ": " << ec.message() << '\n';
Chris Lattnerb0133452009-06-21 20:16:42 +0000351 return 1;
352 }
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +0000353 MemoryBuffer *Buffer = BufferPtr.take();
Jim Grosbach112a2de2011-05-09 20:05:25 +0000354
Chris Lattnerb0133452009-06-21 20:16:42 +0000355 SourceMgr SrcMgr;
Jim Grosbach112a2de2011-05-09 20:05:25 +0000356
Daniel Dunbar5c947db2009-08-19 16:25:53 +0000357 // Tell SrcMgr about this buffer, which is what the parser will pick up.
Chris Lattnerb0133452009-06-21 20:16:42 +0000358 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
Jim Grosbach112a2de2011-05-09 20:05:25 +0000359
Chris Lattnerb0133452009-06-21 20:16:42 +0000360 // Record the location of the include directories so that the lexer can find
361 // it later.
362 SrcMgr.setIncludeDirs(IncludeDirs);
Jim Grosbach112a2de2011-05-09 20:05:25 +0000363
364
Evan Cheng1705ab02011-07-14 23:50:31 +0000365 llvm::OwningPtr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(TripleName));
Chris Lattner768ea2a2010-03-11 22:53:35 +0000366 assert(MAI && "Unable to create target asm info!");
Jim Grosbach112a2de2011-05-09 20:05:25 +0000367
Evan Chengd60fa58b2011-07-18 20:57:22 +0000368 llvm::OwningPtr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
369 assert(MRI && "Unable to create target register info!");
370
Evan Cheng76792992011-07-20 05:58:47 +0000371 // FIXME: This is not pretty. MCContext has a ptr to MCObjectFileInfo and
372 // MCObjectFileInfo needs a MCContext reference in order to initialize itself.
373 OwningPtr<MCObjectFileInfo> MOFI(new MCObjectFileInfo());
Evan Chengbbf3b0d2011-07-20 19:50:42 +0000374 MCContext Ctx(*MAI, *MRI, MOFI.get());
375 MOFI->InitMCObjectFileInfo(TripleName, RelocModel, CMModel, Ctx);
Evan Cheng76792992011-07-20 05:58:47 +0000376
Daniel Dunbar4ee0d032011-03-28 22:49:15 +0000377 if (SaveTempLabels)
378 Ctx.setAllowTemporaryLabels(false);
Rafael Espindola0a017a62010-12-10 07:39:47 +0000379
James Molloy4c493e82011-09-07 17:24:38 +0000380 // Package up features to be passed to target/subtarget
381 std::string FeaturesStr;
382 if (MAttrs.size()) {
383 SubtargetFeatures Features;
384 for (unsigned i = 0; i != MAttrs.size(); ++i)
385 Features.AddFeature(MAttrs[i]);
386 FeaturesStr = Features.getString();
387 }
388
Dan Gohmana2233f22010-09-01 14:20:41 +0000389 OwningPtr<tool_output_file> Out(GetOutputStream());
Dan Gohman268b0f42010-08-20 01:07:01 +0000390 if (!Out)
391 return 1;
392
Dan Gohmana2233f22010-09-01 14:20:41 +0000393 formatted_raw_ostream FOS(Out->os());
Daniel Dunbar3016db32009-08-21 09:11:24 +0000394 OwningPtr<MCStreamer> Str;
Chris Lattnera61e93d2009-08-17 04:23:44 +0000395
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000396 OwningPtr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo());
Evan Cheng91111d22011-07-09 05:47:46 +0000397 OwningPtr<MCSubtargetInfo>
398 STI(TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr));
399
Rafael Espindolab3eca9b2011-01-23 17:55:27 +0000400 // FIXME: There is a bit of code duplication with addPassesToEmitFile.
Kevin Enderbyf92f9902009-09-04 21:45:34 +0000401 if (FileType == OFT_AssemblyFile) {
Chris Lattner90a78592010-03-19 05:48:53 +0000402 MCInstPrinter *IP =
James Molloy4c493e82011-09-07 17:24:38 +0000403 TheTarget->createMCInstPrinter(OutputAsmVariant, *MAI, *STI);
Benjamin Kramera3e0ddb2010-07-29 17:48:06 +0000404 MCCodeEmitter *CE = 0;
Evan Cheng5928e692011-07-25 23:24:55 +0000405 MCAsmBackend *MAB = 0;
Daniel Dunbarecd0c8a2010-12-16 03:05:59 +0000406 if (ShowEncoding) {
Evan Cheng3a792252011-07-26 00:42:34 +0000407 CE = TheTarget->createMCCodeEmitter(*MCII, *STI, Ctx);
Evan Cheng5928e692011-07-25 23:24:55 +0000408 MAB = TheTarget->createMCAsmBackend(TripleName);
Daniel Dunbarecd0c8a2010-12-16 03:05:59 +0000409 }
Rafael Espindola0a017a62010-12-10 07:39:47 +0000410 Str.reset(TheTarget->createAsmStreamer(Ctx, FOS, /*asmverbose*/true,
Rafael Espindolaa3181d12011-04-30 03:44:37 +0000411 /*useLoc*/ true,
Nick Lewycky40f8f2f2011-10-17 23:05:28 +0000412 /*useCFI*/ true,
413 /*useDwarfDirectory*/ true,
414 IP, CE, MAB, ShowInst));
415
Daniel Dunbarb09b8902010-03-23 23:47:12 +0000416 } else if (FileType == OFT_Null) {
417 Str.reset(createNullStreamer(Ctx));
Daniel Dunbar3016db32009-08-21 09:11:24 +0000418 } else {
419 assert(FileType == OFT_ObjectFile && "Invalid file type!");
Evan Cheng3a792252011-07-26 00:42:34 +0000420 MCCodeEmitter *CE = TheTarget->createMCCodeEmitter(*MCII, *STI, Ctx);
Evan Cheng5928e692011-07-25 23:24:55 +0000421 MCAsmBackend *MAB = TheTarget->createMCAsmBackend(TripleName);
Evan Cheng3a792252011-07-26 00:42:34 +0000422 Str.reset(TheTarget->createMCObjectStreamer(TripleName, Ctx, *MAB,
423 FOS, CE, RelaxAll,
424 NoExecStack));
Daniel Dunbar3016db32009-08-21 09:11:24 +0000425 }
Daniel Dunbara10e5192009-06-24 23:30:00 +0000426
Daniel Dunbar3ff1a062010-05-23 17:44:06 +0000427 if (EnableLogging) {
428 Str.reset(createLoggingStreamer(Str.take(), errs()));
429 }
430
Jim Grosbach345768c2011-08-16 18:33:49 +0000431 OwningPtr<MCAsmParser> Parser(createMCAsmParser(SrcMgr, Ctx,
432 *Str.get(), *MAI));
Evan Cheng11424442011-07-26 00:24:13 +0000433 OwningPtr<MCTargetAsmParser> TAP(TheTarget->createMCAsmParser(*STI, *Parser));
Daniel Dunbar80d484e2009-08-14 03:48:55 +0000434 if (!TAP) {
Jim Grosbach112a2de2011-05-09 20:05:25 +0000435 errs() << ProgName
Daniel Dunbar80d484e2009-08-14 03:48:55 +0000436 << ": error: this target does not support assembly parsing.\n";
Daniel Dunbarf59ee962009-07-28 20:47:52 +0000437 return 1;
Daniel Dunbar80d484e2009-08-14 03:48:55 +0000438 }
439
Daniel Dunbar2eca0252010-08-11 06:37:09 +0000440 Parser->setShowParsedOperands(ShowInstOperands);
Daniel Dunbar7f5bf5a2010-07-18 18:31:33 +0000441 Parser->setTargetParser(*TAP.get());
Daniel Dunbarcd4eee52009-08-11 04:34:48 +0000442
Daniel Dunbar7f5bf5a2010-07-18 18:31:33 +0000443 int Res = Parser->Run(NoInitialTextSection);
Daniel Dunbarcd4eee52009-08-11 04:34:48 +0000444
Dan Gohman268b0f42010-08-20 01:07:01 +0000445 // Keep output if no errors.
446 if (Res == 0) Out->keep();
Daniel Dunbar14487342010-03-13 19:31:47 +0000447
Daniel Dunbarcd4eee52009-08-11 04:34:48 +0000448 return Res;
Sean Callanan7e645502009-12-17 01:49:59 +0000449}
450
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000451static int DisassembleInput(const char *ProgName, bool Enhanced) {
Daniel Dunbarabf6e362010-03-19 18:07:50 +0000452 const Target *TheTarget = GetTarget(ProgName);
453 if (!TheTarget)
Sean Callanan7e645502009-12-17 01:49:59 +0000454 return 0;
Sean Callanan7e645502009-12-17 01:49:59 +0000455
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +0000456 OwningPtr<MemoryBuffer> Buffer;
457 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename, Buffer)) {
Michael J. Spencer7b6fef82010-12-09 17:36:48 +0000458 errs() << ProgName << ": " << ec.message() << '\n';
Sean Callanan7e645502009-12-17 01:49:59 +0000459 return 1;
460 }
Jim Grosbach112a2de2011-05-09 20:05:25 +0000461
Dan Gohmana2233f22010-09-01 14:20:41 +0000462 OwningPtr<tool_output_file> Out(GetOutputStream());
Dan Gohman268b0f42010-08-20 01:07:01 +0000463 if (!Out)
464 return 1;
465
466 int Res;
Bill Wendling00f0cdd2011-03-21 04:13:46 +0000467 if (Enhanced) {
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +0000468 Res =
469 Disassembler::disassembleEnhanced(TripleName, *Buffer.take(), Out->os());
Bill Wendling00f0cdd2011-03-21 04:13:46 +0000470 } else {
James Molloy4c493e82011-09-07 17:24:38 +0000471 // Package up features to be passed to target/subtarget
472 std::string FeaturesStr;
473 if (MAttrs.size()) {
474 SubtargetFeatures Features;
475 for (unsigned i = 0; i != MAttrs.size(); ++i)
476 Features.AddFeature(MAttrs[i]);
477 FeaturesStr = Features.getString();
478 }
479
480 Res = Disassembler::disassemble(*TheTarget, TripleName, MCPU, FeaturesStr,
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +0000481 *Buffer.take(), Out->os());
Bill Wendling00f0cdd2011-03-21 04:13:46 +0000482 }
Dan Gohman268b0f42010-08-20 01:07:01 +0000483
484 // Keep output if no errors.
485 if (Res == 0) Out->keep();
486
487 return Res;
Sean Callanan7e645502009-12-17 01:49:59 +0000488}
Chris Lattnerb0133452009-06-21 20:16:42 +0000489
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000490
491int main(int argc, char **argv) {
492 // Print a stack trace if we signal out.
493 sys::PrintStackTraceOnErrorSignal();
494 PrettyStackTraceProgram X(argc, argv);
495 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Daniel Dunbar8c6bad22009-07-17 22:38:58 +0000496
Daniel Dunbar80d484e2009-08-14 03:48:55 +0000497 // Initialize targets and assembly printers/parsers.
Daniel Dunbar8c6bad22009-07-17 22:38:58 +0000498 llvm::InitializeAllTargetInfos();
Evan Cheng8c886a42011-07-22 21:58:54 +0000499 llvm::InitializeAllTargetMCs();
Daniel Dunbar8c6bad22009-07-17 22:38:58 +0000500 llvm::InitializeAllAsmParsers();
Sean Callanan7e645502009-12-17 01:49:59 +0000501 llvm::InitializeAllDisassemblers();
Jim Grosbach112a2de2011-05-09 20:05:25 +0000502
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000503 cl::ParseCommandLineOptions(argc, argv, "llvm machine code playground\n");
Duncan Sands3bd97fe2010-08-28 01:30:02 +0000504 TripleName = Triple::normalize(TripleName);
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000505
506 switch (Action) {
507 default:
Chris Lattnerb0133452009-06-21 20:16:42 +0000508 case AC_AsLex:
509 return AsLexInput(argv[0]);
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000510 case AC_Assemble:
511 return AssembleInput(argv[0]);
Sean Callanan7e645502009-12-17 01:49:59 +0000512 case AC_Disassemble:
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000513 return DisassembleInput(argv[0], false);
514 case AC_EDisassemble:
515 return DisassembleInput(argv[0], true);
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000516 }
Jim Grosbach112a2de2011-05-09 20:05:25 +0000517
Chris Lattner8dd8a522009-06-18 23:04:45 +0000518 return 0;
519}