blob: 67bb06b797dea31ed1f74482482553544d21a72a [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"
Chris Lattner92ffdd12009-06-24 00:52:40 +000017#include "llvm/MC/MCContext.h"
Daniel Dunbar65105172009-08-27 00:51:57 +000018#include "llvm/MC/MCCodeEmitter.h"
Chris Lattner11b2fc92009-09-14 03:02:37 +000019#include "llvm/MC/MCInstPrinter.h"
Chris Lattner6c203912009-08-10 18:15:01 +000020#include "llvm/MC/MCSectionMachO.h"
Chris Lattner92ffdd12009-06-24 00:52:40 +000021#include "llvm/MC/MCStreamer.h"
Chris Lattner3d18e712010-04-05 23:07:18 +000022#include "llvm/Target/TargetAsmBackend.h"
23#include "llvm/Target/TargetAsmParser.h"
24#include "llvm/Target/TargetData.h"
25#include "llvm/Target/TargetRegistry.h"
Jim Grosbach685b7732010-10-30 15:57:50 +000026#include "llvm/Target/SubtargetFeature.h" // FIXME.
Chris Lattner3d18e712010-04-05 23:07:18 +000027#include "llvm/Target/TargetMachine.h" // FIXME.
28#include "llvm/Target/TargetSelect.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"
Daniel Dunbar46892112010-03-13 02:20:38 +000038#include "llvm/System/Host.h"
Chris Lattner8dd8a522009-06-18 23:04:45 +000039#include "llvm/System/Signals.h"
Chris Lattnerb257d242009-12-22 22:50:29 +000040#include "Disassembler.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>
54ShowInst("show-inst", cl::desc("Show internal instruction representation"));
55
Daniel Dunbar2eca0252010-08-11 06:37:09 +000056static cl::opt<bool>
57ShowInstOperands("show-inst-operands",
58 cl::desc("Show instructions operands as parsed"));
59
Chris Lattner44790342009-09-20 07:17:49 +000060static cl::opt<unsigned>
61OutputAsmVariant("output-asm-variant",
62 cl::desc("Syntax variant to use for output printing"));
63
Daniel Dunbard821f4a2010-03-25 22:49:09 +000064static cl::opt<bool>
65RelaxAll("mc-relax-all", cl::desc("Relax all fixups"));
66
Daniel Dunbar3ff1a062010-05-23 17:44:06 +000067static cl::opt<bool>
68EnableLogging("enable-api-logging", cl::desc("Enable MC API logging"));
69
Daniel Dunbar3016db32009-08-21 09:11:24 +000070enum OutputFileType {
Daniel Dunbarb09b8902010-03-23 23:47:12 +000071 OFT_Null,
Daniel Dunbar3016db32009-08-21 09:11:24 +000072 OFT_AssemblyFile,
73 OFT_ObjectFile
74};
75static cl::opt<OutputFileType>
76FileType("filetype", cl::init(OFT_AssemblyFile),
77 cl::desc("Choose an output file type:"),
78 cl::values(
79 clEnumValN(OFT_AssemblyFile, "asm",
80 "Emit an assembly ('.s') file"),
Daniel Dunbarb09b8902010-03-23 23:47:12 +000081 clEnumValN(OFT_Null, "null",
82 "Don't emit anything (for timing purposes)"),
Daniel Dunbar3016db32009-08-21 09:11:24 +000083 clEnumValN(OFT_ObjectFile, "obj",
84 "Emit a native object ('.o') file"),
85 clEnumValEnd));
86
Chris Lattnerd70e15b42009-06-21 05:22:37 +000087static cl::list<std::string>
88IncludeDirs("I", cl::desc("Directory of include files"),
89 cl::value_desc("directory"), cl::Prefix);
Chris Lattner8dd8a522009-06-18 23:04:45 +000090
Daniel Dunbar8c6bad22009-07-17 22:38:58 +000091static cl::opt<std::string>
Daniel Dunbar46892112010-03-13 02:20:38 +000092ArchName("arch", cl::desc("Target arch to assemble for, "
93 "see -version for available targets"));
94
95static cl::opt<std::string>
Nick Lewyckyb2449092009-11-01 22:07:54 +000096TripleName("triple", cl::desc("Target triple to assemble for, "
Daniel Dunbar46892112010-03-13 02:20:38 +000097 "see -version for available targets"));
Daniel Dunbar8c6bad22009-07-17 22:38:58 +000098
Jim Grosbach685b7732010-10-30 15:57:50 +000099static cl::opt<std::string>
100MCPU("mcpu",
101 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
102 cl::value_desc("cpu-name"),
103 cl::init(""));
104
Daniel Dunbar322fec62010-03-13 02:20:57 +0000105static cl::opt<bool>
106NoInitialTextSection("n", cl::desc(
107 "Don't assume assembly file starts in the text section"));
108
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000109enum ActionType {
Chris Lattnerb0133452009-06-21 20:16:42 +0000110 AC_AsLex,
Sean Callanan7e645502009-12-17 01:49:59 +0000111 AC_Assemble,
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000112 AC_Disassemble,
113 AC_EDisassemble
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000114};
Chris Lattner8dd8a522009-06-18 23:04:45 +0000115
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000116static cl::opt<ActionType>
117Action(cl::desc("Action to perform:"),
Chris Lattnerb0133452009-06-21 20:16:42 +0000118 cl::init(AC_Assemble),
119 cl::values(clEnumValN(AC_AsLex, "as-lex",
120 "Lex tokens from a .s file"),
121 clEnumValN(AC_Assemble, "assemble",
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000122 "Assemble a .s file (default)"),
Sean Callanan7e645502009-12-17 01:49:59 +0000123 clEnumValN(AC_Disassemble, "disassemble",
124 "Disassemble strings of hex bytes"),
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000125 clEnumValN(AC_EDisassemble, "edis",
126 "Enhanced disassembly of strings of hex bytes"),
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000127 clEnumValEnd));
128
Kevin Enderbyf92f9902009-09-04 21:45:34 +0000129static const Target *GetTarget(const char *ProgName) {
Daniel Dunbar46892112010-03-13 02:20:38 +0000130 // Figure out the target triple.
131 if (TripleName.empty())
132 TripleName = sys::getHostTriple();
133 if (!ArchName.empty()) {
134 llvm::Triple TT(TripleName);
135 TT.setArchName(ArchName);
136 TripleName = TT.str();
137 }
138
Kevin Enderbyf92f9902009-09-04 21:45:34 +0000139 // Get the target specific parser.
140 std::string Error;
141 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
142 if (TheTarget)
143 return TheTarget;
144
145 errs() << ProgName << ": error: unable to get target for '" << TripleName
146 << "', see --version and --triple.\n";
147 return 0;
148}
149
Dan Gohmana2233f22010-09-01 14:20:41 +0000150static tool_output_file *GetOutputStream() {
Dan Gohman268b0f42010-08-20 01:07:01 +0000151 if (OutputFilename == "")
152 OutputFilename = "-";
153
154 std::string Err;
155 tool_output_file *Out = new tool_output_file(OutputFilename.c_str(), Err,
156 raw_fd_ostream::F_Binary);
157 if (!Err.empty()) {
158 errs() << Err << '\n';
159 delete Out;
160 return 0;
161 }
Dan Gohmana2233f22010-09-01 14:20:41 +0000162
163 return Out;
Dan Gohman268b0f42010-08-20 01:07:01 +0000164}
165
Chris Lattnerb0133452009-06-21 20:16:42 +0000166static int AsLexInput(const char *ProgName) {
Chris Lattner8dd8a522009-06-18 23:04:45 +0000167 std::string ErrorMessage;
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000168 MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename,
169 &ErrorMessage);
Chris Lattner8dd8a522009-06-18 23:04:45 +0000170 if (Buffer == 0) {
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000171 errs() << ProgName << ": ";
Chris Lattner8dd8a522009-06-18 23:04:45 +0000172 if (ErrorMessage.size())
173 errs() << ErrorMessage << "\n";
174 else
175 errs() << "input file didn't read correctly.\n";
176 return 1;
177 }
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000178
179 SourceMgr SrcMgr;
180
181 // Tell SrcMgr about this buffer, which is what TGParser will pick up.
182 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
183
184 // Record the location of the include directories so that the lexer can find
185 // it later.
186 SrcMgr.setIncludeDirs(IncludeDirs);
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +0000187
Kevin Enderbyf92f9902009-09-04 21:45:34 +0000188 const Target *TheTarget = GetTarget(ProgName);
189 if (!TheTarget)
190 return 1;
191
Chris Lattner9f59b3d2010-04-03 21:03:50 +0000192 llvm::OwningPtr<MCAsmInfo> MAI(TheTarget->createAsmInfo(TripleName));
Kevin Enderbyf92f9902009-09-04 21:45:34 +0000193 assert(MAI && "Unable to create target asm info!");
194
Sean Callanan7a77eae2010-01-21 00:19:58 +0000195 AsmLexer Lexer(*MAI);
Daniel Dunbar7a85f9c2010-07-18 18:31:28 +0000196 Lexer.setBuffer(SrcMgr.getMemoryBuffer(0));
197
Dan Gohmana2233f22010-09-01 14:20:41 +0000198 OwningPtr<tool_output_file> Out(GetOutputStream());
Dan Gohman268b0f42010-08-20 01:07:01 +0000199 if (!Out)
200 return 1;
201
Chris Lattnerb0133452009-06-21 20:16:42 +0000202 bool Error = false;
Daniel Dunbarbc798162009-07-28 16:56:42 +0000203 while (Lexer.Lex().isNot(AsmToken::Eof)) {
Daniel Dunbar1601f552010-10-25 20:18:46 +0000204 AsmToken Tok = Lexer.getTok();
205
206 switch (Tok.getKind()) {
Chris Lattnerb0133452009-06-21 20:16:42 +0000207 default:
Sean Callanan177934e2010-01-20 23:19:55 +0000208 SrcMgr.PrintMessage(Lexer.getLoc(), "unknown token", "warning");
Chris Lattnerb0133452009-06-21 20:16:42 +0000209 Error = true;
210 break;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000211 case AsmToken::Error:
Chris Lattnerb0133452009-06-21 20:16:42 +0000212 Error = true; // error already printed.
Chris Lattnerd0765612009-06-21 19:21:25 +0000213 break;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000214 case AsmToken::Identifier:
Daniel Dunbar1601f552010-10-25 20:18:46 +0000215 Out->os() << "identifier: " << Lexer.getTok().getString();
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +0000216 break;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000217 case AsmToken::Integer:
Daniel Dunbar1601f552010-10-25 20:18:46 +0000218 Out->os() << "int: " << Lexer.getTok().getString();
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +0000219 break;
Daniel Dunbar3068a932010-09-24 01:59:31 +0000220 case AsmToken::Real:
Daniel Dunbar1601f552010-10-25 20:18:46 +0000221 Out->os() << "real: " << Lexer.getTok().getString();
Daniel Dunbar3068a932010-09-24 01:59:31 +0000222 break;
Daniel Dunbar25c2c622010-09-16 00:42:35 +0000223 case AsmToken::Register:
Daniel Dunbar1601f552010-10-25 20:18:46 +0000224 Out->os() << "register: " << Lexer.getTok().getRegVal();
Daniel Dunbar25c2c622010-09-16 00:42:35 +0000225 break;
226 case AsmToken::String:
Daniel Dunbar1601f552010-10-25 20:18:46 +0000227 Out->os() << "string: " << Lexer.getTok().getString();
Daniel Dunbar25c2c622010-09-16 00:42:35 +0000228 break;
Daniel Dunbar9c4809a2009-07-01 06:56:54 +0000229
Daniel Dunbar1601f552010-10-25 20:18:46 +0000230 case AsmToken::Amp: Out->os() << "Amp"; break;
231 case AsmToken::AmpAmp: Out->os() << "AmpAmp"; break;
232 case AsmToken::At: Out->os() << "At"; break;
233 case AsmToken::Caret: Out->os() << "Caret"; break;
234 case AsmToken::Colon: Out->os() << "Colon"; break;
235 case AsmToken::Comma: Out->os() << "Comma"; break;
236 case AsmToken::Dollar: Out->os() << "Dollar"; break;
237 case AsmToken::Dot: Out->os() << "Dot"; break;
238 case AsmToken::EndOfStatement: Out->os() << "EndOfStatement"; break;
239 case AsmToken::Eof: Out->os() << "Eof"; break;
240 case AsmToken::Equal: Out->os() << "Equal"; break;
241 case AsmToken::EqualEqual: Out->os() << "EqualEqual"; break;
242 case AsmToken::Exclaim: Out->os() << "Exclaim"; break;
243 case AsmToken::ExclaimEqual: Out->os() << "ExclaimEqual"; break;
244 case AsmToken::Greater: Out->os() << "Greater"; break;
245 case AsmToken::GreaterEqual: Out->os() << "GreaterEqual"; break;
246 case AsmToken::GreaterGreater: Out->os() << "GreaterGreater"; break;
247 case AsmToken::Hash: Out->os() << "Hash"; break;
248 case AsmToken::LBrac: Out->os() << "LBrac"; break;
249 case AsmToken::LCurly: Out->os() << "LCurly"; break;
250 case AsmToken::LParen: Out->os() << "LParen"; break;
251 case AsmToken::Less: Out->os() << "Less"; break;
252 case AsmToken::LessEqual: Out->os() << "LessEqual"; break;
253 case AsmToken::LessGreater: Out->os() << "LessGreater"; break;
254 case AsmToken::LessLess: Out->os() << "LessLess"; break;
255 case AsmToken::Minus: Out->os() << "Minus"; break;
256 case AsmToken::Percent: Out->os() << "Percent"; break;
257 case AsmToken::Pipe: Out->os() << "Pipe"; break;
258 case AsmToken::PipePipe: Out->os() << "PipePipe"; break;
259 case AsmToken::Plus: Out->os() << "Plus"; break;
260 case AsmToken::RBrac: Out->os() << "RBrac"; break;
261 case AsmToken::RCurly: Out->os() << "RCurly"; break;
262 case AsmToken::RParen: Out->os() << "RParen"; break;
263 case AsmToken::Slash: Out->os() << "Slash"; break;
264 case AsmToken::Star: Out->os() << "Star"; break;
265 case AsmToken::Tilde: Out->os() << "Tilde"; break;
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +0000266 }
Daniel Dunbar1601f552010-10-25 20:18:46 +0000267
268 // Print the token string.
269 Out->os() << " (\"";
270 Out->os().write_escaped(Tok.getString());
271 Out->os() << "\")\n";
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +0000272 }
Dan Gohman268b0f42010-08-20 01:07:01 +0000273
274 // Keep output if no errors.
275 if (Error == 0) Out->keep();
276
Chris Lattnerb0133452009-06-21 20:16:42 +0000277 return Error;
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000278}
279
Daniel Dunbarf59ee962009-07-28 20:47:52 +0000280static int AssembleInput(const char *ProgName) {
Daniel Dunbar80d484e2009-08-14 03:48:55 +0000281 const Target *TheTarget = GetTarget(ProgName);
282 if (!TheTarget)
283 return 1;
284
Daniel Dunbarf59ee962009-07-28 20:47:52 +0000285 std::string Error;
286 MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename, &Error);
Chris Lattnerb0133452009-06-21 20:16:42 +0000287 if (Buffer == 0) {
288 errs() << ProgName << ": ";
Daniel Dunbarf59ee962009-07-28 20:47:52 +0000289 if (Error.size())
290 errs() << Error << "\n";
Chris Lattnerb0133452009-06-21 20:16:42 +0000291 else
292 errs() << "input file didn't read correctly.\n";
293 return 1;
294 }
295
296 SourceMgr SrcMgr;
297
Daniel Dunbar5c947db2009-08-19 16:25:53 +0000298 // Tell SrcMgr about this buffer, which is what the parser will pick up.
Chris Lattnerb0133452009-06-21 20:16:42 +0000299 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
300
301 // Record the location of the include directories so that the lexer can find
302 // it later.
303 SrcMgr.setIncludeDirs(IncludeDirs);
304
Chris Lattner768ea2a2010-03-11 22:53:35 +0000305
Chris Lattner9f59b3d2010-04-03 21:03:50 +0000306 llvm::OwningPtr<MCAsmInfo> MAI(TheTarget->createAsmInfo(TripleName));
Chris Lattner768ea2a2010-03-11 22:53:35 +0000307 assert(MAI && "Unable to create target asm info!");
308
309 MCContext Ctx(*MAI);
Daniel Dunbar80d484e2009-08-14 03:48:55 +0000310
Jim Grosbach685b7732010-10-30 15:57:50 +0000311 // Package up features to be passed to target/subtarget
312 std::string FeaturesStr;
313 if (MCPU.size()) {
314 SubtargetFeatures Features;
315 Features.setCPU(MCPU);
316 FeaturesStr = Features.getString();
317 }
318
Daniel Dunbar80d484e2009-08-14 03:48:55 +0000319 // FIXME: We shouldn't need to do this (and link in codegen).
Jim Grosbach685b7732010-10-30 15:57:50 +0000320 // When we split this out, we should do it in a way that makes
321 // it straightforward to switch subtargets on the fly (.e.g,
322 // the .cpu and .code16 directives).
323 OwningPtr<TargetMachine> TM(TheTarget->createTargetMachine(TripleName,
324 FeaturesStr));
Daniel Dunbar80d484e2009-08-14 03:48:55 +0000325
Chris Lattnera61e93d2009-08-17 04:23:44 +0000326 if (!TM) {
327 errs() << ProgName << ": error: could not create target for triple '"
328 << TripleName << "'.\n";
329 return 1;
Daniel Dunbar80d484e2009-08-14 03:48:55 +0000330 }
331
Dan Gohmana2233f22010-09-01 14:20:41 +0000332 OwningPtr<tool_output_file> Out(GetOutputStream());
Dan Gohman268b0f42010-08-20 01:07:01 +0000333 if (!Out)
334 return 1;
335
Dan Gohmana2233f22010-09-01 14:20:41 +0000336 formatted_raw_ostream FOS(Out->os());
Daniel Dunbar3016db32009-08-21 09:11:24 +0000337 OwningPtr<MCStreamer> Str;
Chris Lattnera61e93d2009-08-17 04:23:44 +0000338
Kevin Enderbyf92f9902009-09-04 21:45:34 +0000339 if (FileType == OFT_AssemblyFile) {
Chris Lattner90a78592010-03-19 05:48:53 +0000340 MCInstPrinter *IP =
Chris Lattner70129162010-04-04 05:04:31 +0000341 TheTarget->createMCInstPrinter(OutputAsmVariant, *MAI);
Benjamin Kramera3e0ddb2010-07-29 17:48:06 +0000342 MCCodeEmitter *CE = 0;
Daniel Dunbard18dd1d2009-08-27 07:56:39 +0000343 if (ShowEncoding)
Benjamin Kramera3e0ddb2010-07-29 17:48:06 +0000344 CE = TheTarget->createCodeEmitter(*TM, Ctx);
Che-Liang Chiou345b98e2010-11-08 02:21:17 +0000345 Str.reset(TheTarget->createAsmStreamer(Ctx, FOS,
346 TM->getTargetData()->isLittleEndian(),
347 /*asmverbose*/true, IP, CE, ShowInst));
Daniel Dunbarb09b8902010-03-23 23:47:12 +0000348 } else if (FileType == OFT_Null) {
349 Str.reset(createNullStreamer(Ctx));
Daniel Dunbar3016db32009-08-21 09:11:24 +0000350 } else {
351 assert(FileType == OFT_ObjectFile && "Invalid file type!");
Benjamin Kramera3e0ddb2010-07-29 17:48:06 +0000352 MCCodeEmitter *CE = TheTarget->createCodeEmitter(*TM, Ctx);
353 TargetAsmBackend *TAB = TheTarget->createAsmBackend(TripleName);
Matt Fleming638cdb22010-05-21 12:54:43 +0000354 Str.reset(TheTarget->createObjectStreamer(TripleName, Ctx, *TAB,
Dan Gohmana2233f22010-09-01 14:20:41 +0000355 FOS, CE, RelaxAll));
Daniel Dunbar3016db32009-08-21 09:11:24 +0000356 }
Daniel Dunbara10e5192009-06-24 23:30:00 +0000357
Daniel Dunbar3ff1a062010-05-23 17:44:06 +0000358 if (EnableLogging) {
359 Str.reset(createLoggingStreamer(Str.take(), errs()));
360 }
361
Daniel Dunbar7f5bf5a2010-07-18 18:31:33 +0000362 OwningPtr<MCAsmParser> Parser(createMCAsmParser(*TheTarget, SrcMgr, Ctx,
363 *Str.get(), *MAI));
Daniel Dunbar419197c2010-07-19 00:33:49 +0000364 OwningPtr<TargetAsmParser> TAP(TheTarget->createAsmParser(*Parser, *TM));
Daniel Dunbar80d484e2009-08-14 03:48:55 +0000365 if (!TAP) {
366 errs() << ProgName
367 << ": error: this target does not support assembly parsing.\n";
Daniel Dunbarf59ee962009-07-28 20:47:52 +0000368 return 1;
Daniel Dunbar80d484e2009-08-14 03:48:55 +0000369 }
370
Daniel Dunbar2eca0252010-08-11 06:37:09 +0000371 Parser->setShowParsedOperands(ShowInstOperands);
Daniel Dunbar7f5bf5a2010-07-18 18:31:33 +0000372 Parser->setTargetParser(*TAP.get());
Daniel Dunbarcd4eee52009-08-11 04:34:48 +0000373
Daniel Dunbar7f5bf5a2010-07-18 18:31:33 +0000374 int Res = Parser->Run(NoInitialTextSection);
Daniel Dunbarcd4eee52009-08-11 04:34:48 +0000375
Dan Gohman268b0f42010-08-20 01:07:01 +0000376 // Keep output if no errors.
377 if (Res == 0) Out->keep();
Daniel Dunbar14487342010-03-13 19:31:47 +0000378
Daniel Dunbarcd4eee52009-08-11 04:34:48 +0000379 return Res;
Sean Callanan7e645502009-12-17 01:49:59 +0000380}
381
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000382static int DisassembleInput(const char *ProgName, bool Enhanced) {
Daniel Dunbarabf6e362010-03-19 18:07:50 +0000383 const Target *TheTarget = GetTarget(ProgName);
384 if (!TheTarget)
Sean Callanan7e645502009-12-17 01:49:59 +0000385 return 0;
Sean Callanan7e645502009-12-17 01:49:59 +0000386
387 std::string ErrorMessage;
388
389 MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename,
390 &ErrorMessage);
391
392 if (Buffer == 0) {
393 errs() << ProgName << ": ";
394 if (ErrorMessage.size())
395 errs() << ErrorMessage << "\n";
396 else
397 errs() << "input file didn't read correctly.\n";
398 return 1;
399 }
400
Dan Gohmana2233f22010-09-01 14:20:41 +0000401 OwningPtr<tool_output_file> Out(GetOutputStream());
Dan Gohman268b0f42010-08-20 01:07:01 +0000402 if (!Out)
403 return 1;
404
405 int Res;
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000406 if (Enhanced)
Dan Gohmana2233f22010-09-01 14:20:41 +0000407 Res = Disassembler::disassembleEnhanced(TripleName, *Buffer, Out->os());
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000408 else
Dan Gohmana2233f22010-09-01 14:20:41 +0000409 Res = Disassembler::disassemble(*TheTarget, TripleName, *Buffer, Out->os());
Dan Gohman268b0f42010-08-20 01:07:01 +0000410
411 // Keep output if no errors.
412 if (Res == 0) Out->keep();
413
414 return Res;
Sean Callanan7e645502009-12-17 01:49:59 +0000415}
Chris Lattnerb0133452009-06-21 20:16:42 +0000416
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000417
418int main(int argc, char **argv) {
419 // Print a stack trace if we signal out.
420 sys::PrintStackTraceOnErrorSignal();
421 PrettyStackTraceProgram X(argc, argv);
422 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Daniel Dunbar8c6bad22009-07-17 22:38:58 +0000423
Daniel Dunbar80d484e2009-08-14 03:48:55 +0000424 // Initialize targets and assembly printers/parsers.
Daniel Dunbar8c6bad22009-07-17 22:38:58 +0000425 llvm::InitializeAllTargetInfos();
Daniel Dunbar80d484e2009-08-14 03:48:55 +0000426 // FIXME: We shouldn't need to initialize the Target(Machine)s.
427 llvm::InitializeAllTargets();
428 llvm::InitializeAllAsmPrinters();
Daniel Dunbar8c6bad22009-07-17 22:38:58 +0000429 llvm::InitializeAllAsmParsers();
Sean Callanan7e645502009-12-17 01:49:59 +0000430 llvm::InitializeAllDisassemblers();
Daniel Dunbar8c6bad22009-07-17 22:38:58 +0000431
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000432 cl::ParseCommandLineOptions(argc, argv, "llvm machine code playground\n");
Duncan Sands3bd97fe2010-08-28 01:30:02 +0000433 TripleName = Triple::normalize(TripleName);
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000434
435 switch (Action) {
436 default:
Chris Lattnerb0133452009-06-21 20:16:42 +0000437 case AC_AsLex:
438 return AsLexInput(argv[0]);
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000439 case AC_Assemble:
440 return AssembleInput(argv[0]);
Sean Callanan7e645502009-12-17 01:49:59 +0000441 case AC_Disassemble:
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000442 return DisassembleInput(argv[0], false);
443 case AC_EDisassemble:
444 return DisassembleInput(argv[0], true);
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000445 }
Chris Lattner8dd8a522009-06-18 23:04:45 +0000446
447 return 0;
448}
449