blob: d930aea1e54324bed96746b15fab53b0b8ca6864 [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"
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"
22#include "llvm/MC/MCRegisterInfo.h"
Chris Lattner6c203912009-08-10 18:15:01 +000023#include "llvm/MC/MCSectionMachO.h"
Chris Lattner92ffdd12009-06-24 00:52:40 +000024#include "llvm/MC/MCStreamer.h"
Evan Cheng91111d22011-07-09 05:47:46 +000025#include "llvm/MC/MCSubtargetInfo.h"
Evan Cheng8264e272011-06-29 01:14:12 +000026#include "llvm/MC/SubtargetFeature.h"
Chris Lattner3d18e712010-04-05 23:07:18 +000027#include "llvm/Target/TargetAsmBackend.h"
28#include "llvm/Target/TargetAsmParser.h"
29#include "llvm/Target/TargetData.h"
30#include "llvm/Target/TargetRegistry.h"
Rafael Espindola0a017a62010-12-10 07:39:47 +000031#include "llvm/Target/TargetAsmInfo.h" // FIXME.
32#include "llvm/Target/TargetLowering.h" // FIXME.
33#include "llvm/Target/TargetLoweringObjectFile.h" // FIXME.
Chris Lattner3d18e712010-04-05 23:07:18 +000034#include "llvm/Target/TargetMachine.h" // FIXME.
35#include "llvm/Target/TargetSelect.h"
Chris Lattner92ffdd12009-06-24 00:52:40 +000036#include "llvm/ADT/OwningPtr.h"
Chris Lattner8dd8a522009-06-18 23:04:45 +000037#include "llvm/Support/CommandLine.h"
Dan Gohman268b0f42010-08-20 01:07:01 +000038#include "llvm/Support/FileUtilities.h"
Daniel Dunbar80d484e2009-08-14 03:48:55 +000039#include "llvm/Support/FormattedStream.h"
Chris Lattner8dd8a522009-06-18 23:04:45 +000040#include "llvm/Support/ManagedStatic.h"
41#include "llvm/Support/MemoryBuffer.h"
42#include "llvm/Support/PrettyStackTrace.h"
Chris Lattnerd70e15b42009-06-21 05:22:37 +000043#include "llvm/Support/SourceMgr.h"
Dan Gohman0df7ea42010-10-07 20:32:40 +000044#include "llvm/Support/ToolOutputFile.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000045#include "llvm/Support/Host.h"
46#include "llvm/Support/Signals.h"
Michael J. Spencer7b6fef82010-12-09 17:36:48 +000047#include "llvm/Support/system_error.h"
Chris Lattnerb257d242009-12-22 22:50:29 +000048#include "Disassembler.h"
Chris Lattner8dd8a522009-06-18 23:04:45 +000049using namespace llvm;
50
51static cl::opt<std::string>
52InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
53
54static cl::opt<std::string>
55OutputFilename("o", cl::desc("Output filename"),
56 cl::value_desc("filename"));
57
Daniel Dunbard18dd1d2009-08-27 07:56:39 +000058static cl::opt<bool>
59ShowEncoding("show-encoding", cl::desc("Show instruction encodings"));
60
Daniel Dunbare3ee3322010-02-03 18:18:30 +000061static cl::opt<bool>
62ShowInst("show-inst", cl::desc("Show internal instruction representation"));
63
Daniel Dunbar2eca0252010-08-11 06:37:09 +000064static cl::opt<bool>
65ShowInstOperands("show-inst-operands",
66 cl::desc("Show instructions operands as parsed"));
67
Chris Lattner44790342009-09-20 07:17:49 +000068static cl::opt<unsigned>
69OutputAsmVariant("output-asm-variant",
70 cl::desc("Syntax variant to use for output printing"));
71
Daniel Dunbard821f4a2010-03-25 22:49:09 +000072static cl::opt<bool>
73RelaxAll("mc-relax-all", cl::desc("Relax all fixups"));
74
Daniel Dunbar3ff1a062010-05-23 17:44:06 +000075static cl::opt<bool>
Rafael Espindolab3eca9b2011-01-23 17:55:27 +000076NoExecStack("mc-no-exec-stack", cl::desc("File doesn't need an exec stack"));
77
78static cl::opt<bool>
Daniel Dunbar3ff1a062010-05-23 17:44:06 +000079EnableLogging("enable-api-logging", cl::desc("Enable MC API logging"));
80
Daniel Dunbar3016db32009-08-21 09:11:24 +000081enum OutputFileType {
Daniel Dunbarb09b8902010-03-23 23:47:12 +000082 OFT_Null,
Daniel Dunbar3016db32009-08-21 09:11:24 +000083 OFT_AssemblyFile,
84 OFT_ObjectFile
85};
86static cl::opt<OutputFileType>
87FileType("filetype", cl::init(OFT_AssemblyFile),
88 cl::desc("Choose an output file type:"),
89 cl::values(
90 clEnumValN(OFT_AssemblyFile, "asm",
91 "Emit an assembly ('.s') file"),
Daniel Dunbarb09b8902010-03-23 23:47:12 +000092 clEnumValN(OFT_Null, "null",
93 "Don't emit anything (for timing purposes)"),
Daniel Dunbar3016db32009-08-21 09:11:24 +000094 clEnumValN(OFT_ObjectFile, "obj",
95 "Emit a native object ('.o') file"),
96 clEnumValEnd));
97
Chris Lattnerd70e15b42009-06-21 05:22:37 +000098static cl::list<std::string>
99IncludeDirs("I", cl::desc("Directory of include files"),
100 cl::value_desc("directory"), cl::Prefix);
Chris Lattner8dd8a522009-06-18 23:04:45 +0000101
Daniel Dunbar8c6bad22009-07-17 22:38:58 +0000102static cl::opt<std::string>
Daniel Dunbar46892112010-03-13 02:20:38 +0000103ArchName("arch", cl::desc("Target arch to assemble for, "
Bill Wendlinge3031142011-06-17 20:35:21 +0000104 "see -version for available targets"));
Daniel Dunbar46892112010-03-13 02:20:38 +0000105
106static cl::opt<std::string>
Nick Lewyckyb2449092009-11-01 22:07:54 +0000107TripleName("triple", cl::desc("Target triple to assemble for, "
Daniel Dunbar46892112010-03-13 02:20:38 +0000108 "see -version for available targets"));
Daniel Dunbar8c6bad22009-07-17 22:38:58 +0000109
Jim Grosbach685b7732010-10-30 15:57:50 +0000110static cl::opt<std::string>
111MCPU("mcpu",
112 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
113 cl::value_desc("cpu-name"),
114 cl::init(""));
115
Evan Cheng2129f592011-07-19 06:37:02 +0000116static cl::opt<Reloc::Model>
117RelocModel("relocation-model",
118 cl::desc("Choose relocation model"),
119 cl::init(Reloc::Default),
120 cl::values(
121 clEnumValN(Reloc::Default, "default",
122 "Target default relocation model"),
123 clEnumValN(Reloc::Static, "static",
124 "Non-relocatable code"),
125 clEnumValN(Reloc::PIC_, "pic",
126 "Fully relocatable, position independent code"),
127 clEnumValN(Reloc::DynamicNoPIC, "dynamic-no-pic",
128 "Relocatable external references, non-relocatable code"),
129 clEnumValEnd));
130
Daniel Dunbar322fec62010-03-13 02:20:57 +0000131static cl::opt<bool>
Bill Wendlinge3031142011-06-17 20:35:21 +0000132NoInitialTextSection("n", cl::desc("Don't assume assembly file starts "
133 "in the text section"));
Daniel Dunbar322fec62010-03-13 02:20:57 +0000134
Daniel Dunbar4ee0d032011-03-28 22:49:15 +0000135static cl::opt<bool>
Bill Wendlinge3031142011-06-17 20:35:21 +0000136SaveTempLabels("L", cl::desc("Don't discard temporary labels"));
Daniel Dunbar4ee0d032011-03-28 22:49:15 +0000137
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000138enum ActionType {
Chris Lattnerb0133452009-06-21 20:16:42 +0000139 AC_AsLex,
Sean Callanan7e645502009-12-17 01:49:59 +0000140 AC_Assemble,
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000141 AC_Disassemble,
142 AC_EDisassemble
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000143};
Chris Lattner8dd8a522009-06-18 23:04:45 +0000144
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000145static cl::opt<ActionType>
146Action(cl::desc("Action to perform:"),
Chris Lattnerb0133452009-06-21 20:16:42 +0000147 cl::init(AC_Assemble),
148 cl::values(clEnumValN(AC_AsLex, "as-lex",
149 "Lex tokens from a .s file"),
150 clEnumValN(AC_Assemble, "assemble",
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000151 "Assemble a .s file (default)"),
Sean Callanan7e645502009-12-17 01:49:59 +0000152 clEnumValN(AC_Disassemble, "disassemble",
153 "Disassemble strings of hex bytes"),
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000154 clEnumValN(AC_EDisassemble, "edis",
155 "Enhanced disassembly of strings of hex bytes"),
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000156 clEnumValEnd));
157
Kevin Enderbyf92f9902009-09-04 21:45:34 +0000158static const Target *GetTarget(const char *ProgName) {
Daniel Dunbar46892112010-03-13 02:20:38 +0000159 // Figure out the target triple.
160 if (TripleName.empty())
161 TripleName = sys::getHostTriple();
162 if (!ArchName.empty()) {
163 llvm::Triple TT(TripleName);
164 TT.setArchName(ArchName);
165 TripleName = TT.str();
166 }
167
Kevin Enderbyf92f9902009-09-04 21:45:34 +0000168 // Get the target specific parser.
169 std::string Error;
170 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
171 if (TheTarget)
172 return TheTarget;
173
174 errs() << ProgName << ": error: unable to get target for '" << TripleName
175 << "', see --version and --triple.\n";
176 return 0;
177}
178
Dan Gohmana2233f22010-09-01 14:20:41 +0000179static tool_output_file *GetOutputStream() {
Dan Gohman268b0f42010-08-20 01:07:01 +0000180 if (OutputFilename == "")
181 OutputFilename = "-";
182
183 std::string Err;
184 tool_output_file *Out = new tool_output_file(OutputFilename.c_str(), Err,
185 raw_fd_ostream::F_Binary);
186 if (!Err.empty()) {
187 errs() << Err << '\n';
188 delete Out;
189 return 0;
190 }
Dan Gohmana2233f22010-09-01 14:20:41 +0000191
192 return Out;
Dan Gohman268b0f42010-08-20 01:07:01 +0000193}
194
Chris Lattnerb0133452009-06-21 20:16:42 +0000195static int AsLexInput(const char *ProgName) {
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +0000196 OwningPtr<MemoryBuffer> BufferPtr;
197 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename, BufferPtr)) {
Michael J. Spencer7b6fef82010-12-09 17:36:48 +0000198 errs() << ProgName << ": " << ec.message() << '\n';
Chris Lattner8dd8a522009-06-18 23:04:45 +0000199 return 1;
200 }
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +0000201 MemoryBuffer *Buffer = BufferPtr.take();
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000202
203 SourceMgr SrcMgr;
Jim Grosbach112a2de2011-05-09 20:05:25 +0000204
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000205 // Tell SrcMgr about this buffer, which is what TGParser will pick up.
206 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
Jim Grosbach112a2de2011-05-09 20:05:25 +0000207
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000208 // Record the location of the include directories so that the lexer can find
209 // it later.
210 SrcMgr.setIncludeDirs(IncludeDirs);
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +0000211
Kevin Enderbyf92f9902009-09-04 21:45:34 +0000212 const Target *TheTarget = GetTarget(ProgName);
213 if (!TheTarget)
214 return 1;
215
Evan Cheng1705ab02011-07-14 23:50:31 +0000216 llvm::OwningPtr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(TripleName));
Kevin Enderbyf92f9902009-09-04 21:45:34 +0000217 assert(MAI && "Unable to create target asm info!");
218
Sean Callanan7a77eae2010-01-21 00:19:58 +0000219 AsmLexer Lexer(*MAI);
Daniel Dunbar7a85f9c2010-07-18 18:31:28 +0000220 Lexer.setBuffer(SrcMgr.getMemoryBuffer(0));
221
Dan Gohmana2233f22010-09-01 14:20:41 +0000222 OwningPtr<tool_output_file> Out(GetOutputStream());
Dan Gohman268b0f42010-08-20 01:07:01 +0000223 if (!Out)
224 return 1;
225
Chris Lattnerb0133452009-06-21 20:16:42 +0000226 bool Error = false;
Daniel Dunbarbc798162009-07-28 16:56:42 +0000227 while (Lexer.Lex().isNot(AsmToken::Eof)) {
Daniel Dunbar1601f552010-10-25 20:18:46 +0000228 AsmToken Tok = Lexer.getTok();
229
230 switch (Tok.getKind()) {
Chris Lattnerb0133452009-06-21 20:16:42 +0000231 default:
Sean Callanan177934e2010-01-20 23:19:55 +0000232 SrcMgr.PrintMessage(Lexer.getLoc(), "unknown token", "warning");
Chris Lattnerb0133452009-06-21 20:16:42 +0000233 Error = true;
234 break;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000235 case AsmToken::Error:
Chris Lattnerb0133452009-06-21 20:16:42 +0000236 Error = true; // error already printed.
Chris Lattnerd0765612009-06-21 19:21:25 +0000237 break;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000238 case AsmToken::Identifier:
Daniel Dunbar1601f552010-10-25 20:18:46 +0000239 Out->os() << "identifier: " << Lexer.getTok().getString();
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +0000240 break;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000241 case AsmToken::Integer:
Daniel Dunbar1601f552010-10-25 20:18:46 +0000242 Out->os() << "int: " << Lexer.getTok().getString();
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +0000243 break;
Daniel Dunbar3068a932010-09-24 01:59:31 +0000244 case AsmToken::Real:
Daniel Dunbar1601f552010-10-25 20:18:46 +0000245 Out->os() << "real: " << Lexer.getTok().getString();
Daniel Dunbar3068a932010-09-24 01:59:31 +0000246 break;
Daniel Dunbar25c2c622010-09-16 00:42:35 +0000247 case AsmToken::Register:
Daniel Dunbar1601f552010-10-25 20:18:46 +0000248 Out->os() << "register: " << Lexer.getTok().getRegVal();
Daniel Dunbar25c2c622010-09-16 00:42:35 +0000249 break;
250 case AsmToken::String:
Daniel Dunbar1601f552010-10-25 20:18:46 +0000251 Out->os() << "string: " << Lexer.getTok().getString();
Daniel Dunbar25c2c622010-09-16 00:42:35 +0000252 break;
Daniel Dunbar9c4809a2009-07-01 06:56:54 +0000253
Daniel Dunbar1601f552010-10-25 20:18:46 +0000254 case AsmToken::Amp: Out->os() << "Amp"; break;
255 case AsmToken::AmpAmp: Out->os() << "AmpAmp"; break;
256 case AsmToken::At: Out->os() << "At"; break;
257 case AsmToken::Caret: Out->os() << "Caret"; break;
258 case AsmToken::Colon: Out->os() << "Colon"; break;
259 case AsmToken::Comma: Out->os() << "Comma"; break;
260 case AsmToken::Dollar: Out->os() << "Dollar"; break;
261 case AsmToken::Dot: Out->os() << "Dot"; break;
262 case AsmToken::EndOfStatement: Out->os() << "EndOfStatement"; break;
263 case AsmToken::Eof: Out->os() << "Eof"; break;
264 case AsmToken::Equal: Out->os() << "Equal"; break;
265 case AsmToken::EqualEqual: Out->os() << "EqualEqual"; break;
266 case AsmToken::Exclaim: Out->os() << "Exclaim"; break;
267 case AsmToken::ExclaimEqual: Out->os() << "ExclaimEqual"; break;
268 case AsmToken::Greater: Out->os() << "Greater"; break;
269 case AsmToken::GreaterEqual: Out->os() << "GreaterEqual"; break;
270 case AsmToken::GreaterGreater: Out->os() << "GreaterGreater"; break;
271 case AsmToken::Hash: Out->os() << "Hash"; break;
272 case AsmToken::LBrac: Out->os() << "LBrac"; break;
273 case AsmToken::LCurly: Out->os() << "LCurly"; break;
274 case AsmToken::LParen: Out->os() << "LParen"; break;
275 case AsmToken::Less: Out->os() << "Less"; break;
276 case AsmToken::LessEqual: Out->os() << "LessEqual"; break;
277 case AsmToken::LessGreater: Out->os() << "LessGreater"; break;
278 case AsmToken::LessLess: Out->os() << "LessLess"; break;
279 case AsmToken::Minus: Out->os() << "Minus"; break;
280 case AsmToken::Percent: Out->os() << "Percent"; break;
281 case AsmToken::Pipe: Out->os() << "Pipe"; break;
282 case AsmToken::PipePipe: Out->os() << "PipePipe"; break;
283 case AsmToken::Plus: Out->os() << "Plus"; break;
284 case AsmToken::RBrac: Out->os() << "RBrac"; break;
285 case AsmToken::RCurly: Out->os() << "RCurly"; break;
286 case AsmToken::RParen: Out->os() << "RParen"; break;
287 case AsmToken::Slash: Out->os() << "Slash"; break;
288 case AsmToken::Star: Out->os() << "Star"; break;
289 case AsmToken::Tilde: Out->os() << "Tilde"; break;
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +0000290 }
Daniel Dunbar1601f552010-10-25 20:18:46 +0000291
292 // Print the token string.
293 Out->os() << " (\"";
294 Out->os().write_escaped(Tok.getString());
295 Out->os() << "\")\n";
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +0000296 }
Dan Gohman268b0f42010-08-20 01:07:01 +0000297
298 // Keep output if no errors.
299 if (Error == 0) Out->keep();
Jim Grosbach112a2de2011-05-09 20:05:25 +0000300
Chris Lattnerb0133452009-06-21 20:16:42 +0000301 return Error;
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000302}
303
Daniel Dunbarf59ee962009-07-28 20:47:52 +0000304static int AssembleInput(const char *ProgName) {
Daniel Dunbar80d484e2009-08-14 03:48:55 +0000305 const Target *TheTarget = GetTarget(ProgName);
306 if (!TheTarget)
307 return 1;
308
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +0000309 OwningPtr<MemoryBuffer> BufferPtr;
310 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename, BufferPtr)) {
Michael J. Spencer7b6fef82010-12-09 17:36:48 +0000311 errs() << ProgName << ": " << ec.message() << '\n';
Chris Lattnerb0133452009-06-21 20:16:42 +0000312 return 1;
313 }
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +0000314 MemoryBuffer *Buffer = BufferPtr.take();
Jim Grosbach112a2de2011-05-09 20:05:25 +0000315
Chris Lattnerb0133452009-06-21 20:16:42 +0000316 SourceMgr SrcMgr;
Jim Grosbach112a2de2011-05-09 20:05:25 +0000317
Daniel Dunbar5c947db2009-08-19 16:25:53 +0000318 // Tell SrcMgr about this buffer, which is what the parser will pick up.
Chris Lattnerb0133452009-06-21 20:16:42 +0000319 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
Jim Grosbach112a2de2011-05-09 20:05:25 +0000320
Chris Lattnerb0133452009-06-21 20:16:42 +0000321 // Record the location of the include directories so that the lexer can find
322 // it later.
323 SrcMgr.setIncludeDirs(IncludeDirs);
Jim Grosbach112a2de2011-05-09 20:05:25 +0000324
325
Evan Cheng1705ab02011-07-14 23:50:31 +0000326 llvm::OwningPtr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(TripleName));
Chris Lattner768ea2a2010-03-11 22:53:35 +0000327 assert(MAI && "Unable to create target asm info!");
Jim Grosbach112a2de2011-05-09 20:05:25 +0000328
Evan Chengd60fa58b2011-07-18 20:57:22 +0000329 llvm::OwningPtr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
330 assert(MRI && "Unable to create target register info!");
331
Jim Grosbach685b7732010-10-30 15:57:50 +0000332 // Package up features to be passed to target/subtarget
333 std::string FeaturesStr;
Jim Grosbach685b7732010-10-30 15:57:50 +0000334
Daniel Dunbar80d484e2009-08-14 03:48:55 +0000335 // FIXME: We shouldn't need to do this (and link in codegen).
Jim Grosbach685b7732010-10-30 15:57:50 +0000336 // When we split this out, we should do it in a way that makes
337 // it straightforward to switch subtargets on the fly (.e.g,
338 // the .cpu and .code16 directives).
339 OwningPtr<TargetMachine> TM(TheTarget->createTargetMachine(TripleName,
Evan Chengfe6e4052011-06-30 01:53:36 +0000340 MCPU,
Evan Cheng2129f592011-07-19 06:37:02 +0000341 FeaturesStr,
342 RelocModel));
Daniel Dunbar80d484e2009-08-14 03:48:55 +0000343
Chris Lattnera61e93d2009-08-17 04:23:44 +0000344 if (!TM) {
345 errs() << ProgName << ": error: could not create target for triple '"
346 << TripleName << "'.\n";
347 return 1;
Daniel Dunbar80d484e2009-08-14 03:48:55 +0000348 }
349
Rafael Espindola0a017a62010-12-10 07:39:47 +0000350 const TargetAsmInfo *tai = new TargetAsmInfo(*TM);
Evan Cheng76792992011-07-20 05:58:47 +0000351 // FIXME: This is not pretty. MCContext has a ptr to MCObjectFileInfo and
352 // MCObjectFileInfo needs a MCContext reference in order to initialize itself.
353 OwningPtr<MCObjectFileInfo> MOFI(new MCObjectFileInfo());
354 MCContext Ctx(*MAI, *MRI, MOFI.get(), tai);
355 MOFI->InitMCObjectFileInfo(TripleName, RelocModel, Ctx);
356
Daniel Dunbar4ee0d032011-03-28 22:49:15 +0000357 if (SaveTempLabels)
358 Ctx.setAllowTemporaryLabels(false);
Rafael Espindola0a017a62010-12-10 07:39:47 +0000359
Dan Gohmana2233f22010-09-01 14:20:41 +0000360 OwningPtr<tool_output_file> Out(GetOutputStream());
Dan Gohman268b0f42010-08-20 01:07:01 +0000361 if (!Out)
362 return 1;
363
Dan Gohmana2233f22010-09-01 14:20:41 +0000364 formatted_raw_ostream FOS(Out->os());
Daniel Dunbar3016db32009-08-21 09:11:24 +0000365 OwningPtr<MCStreamer> Str;
Chris Lattnera61e93d2009-08-17 04:23:44 +0000366
Rafael Espindola0a017a62010-12-10 07:39:47 +0000367 const TargetLoweringObjectFile &TLOF =
368 TM->getTargetLowering()->getObjFileLowering();
369 const_cast<TargetLoweringObjectFile&>(TLOF).Initialize(Ctx, *TM);
370
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000371 OwningPtr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo());
Evan Cheng91111d22011-07-09 05:47:46 +0000372 OwningPtr<MCSubtargetInfo>
373 STI(TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr));
374
Rafael Espindolab3eca9b2011-01-23 17:55:27 +0000375 // FIXME: There is a bit of code duplication with addPassesToEmitFile.
Kevin Enderbyf92f9902009-09-04 21:45:34 +0000376 if (FileType == OFT_AssemblyFile) {
Chris Lattner90a78592010-03-19 05:48:53 +0000377 MCInstPrinter *IP =
Evan Chengab37af92011-07-06 19:45:42 +0000378 TheTarget->createMCInstPrinter(OutputAsmVariant, *MAI);
Benjamin Kramera3e0ddb2010-07-29 17:48:06 +0000379 MCCodeEmitter *CE = 0;
Daniel Dunbarecd0c8a2010-12-16 03:05:59 +0000380 TargetAsmBackend *TAB = 0;
381 if (ShowEncoding) {
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000382 CE = TheTarget->createCodeEmitter(*MCII, *STI, Ctx);
Daniel Dunbarecd0c8a2010-12-16 03:05:59 +0000383 TAB = TheTarget->createAsmBackend(TripleName);
384 }
Rafael Espindola0a017a62010-12-10 07:39:47 +0000385 Str.reset(TheTarget->createAsmStreamer(Ctx, FOS, /*asmverbose*/true,
Rafael Espindolaa3181d12011-04-30 03:44:37 +0000386 /*useLoc*/ true,
387 /*useCFI*/ true, IP, CE, TAB,
Bill Wendlingb74b9de2011-06-17 20:55:01 +0000388 ShowInst));
Daniel Dunbarb09b8902010-03-23 23:47:12 +0000389 } else if (FileType == OFT_Null) {
390 Str.reset(createNullStreamer(Ctx));
Daniel Dunbar3016db32009-08-21 09:11:24 +0000391 } else {
392 assert(FileType == OFT_ObjectFile && "Invalid file type!");
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000393 MCCodeEmitter *CE = TheTarget->createCodeEmitter(*MCII, *STI, Ctx);
Benjamin Kramera3e0ddb2010-07-29 17:48:06 +0000394 TargetAsmBackend *TAB = TheTarget->createAsmBackend(TripleName);
Matt Fleming638cdb22010-05-21 12:54:43 +0000395 Str.reset(TheTarget->createObjectStreamer(TripleName, Ctx, *TAB,
Rafael Espindolab3eca9b2011-01-23 17:55:27 +0000396 FOS, CE, RelaxAll,
397 NoExecStack));
Daniel Dunbar3016db32009-08-21 09:11:24 +0000398 }
Daniel Dunbara10e5192009-06-24 23:30:00 +0000399
Daniel Dunbar3ff1a062010-05-23 17:44:06 +0000400 if (EnableLogging) {
401 Str.reset(createLoggingStreamer(Str.take(), errs()));
402 }
403
Daniel Dunbar7f5bf5a2010-07-18 18:31:33 +0000404 OwningPtr<MCAsmParser> Parser(createMCAsmParser(*TheTarget, SrcMgr, Ctx,
405 *Str.get(), *MAI));
Evan Cheng91111d22011-07-09 05:47:46 +0000406 OwningPtr<TargetAsmParser> TAP(TheTarget->createAsmParser(*STI, *Parser));
Daniel Dunbar80d484e2009-08-14 03:48:55 +0000407 if (!TAP) {
Jim Grosbach112a2de2011-05-09 20:05:25 +0000408 errs() << ProgName
Daniel Dunbar80d484e2009-08-14 03:48:55 +0000409 << ": error: this target does not support assembly parsing.\n";
Daniel Dunbarf59ee962009-07-28 20:47:52 +0000410 return 1;
Daniel Dunbar80d484e2009-08-14 03:48:55 +0000411 }
412
Daniel Dunbar2eca0252010-08-11 06:37:09 +0000413 Parser->setShowParsedOperands(ShowInstOperands);
Daniel Dunbar7f5bf5a2010-07-18 18:31:33 +0000414 Parser->setTargetParser(*TAP.get());
Daniel Dunbarcd4eee52009-08-11 04:34:48 +0000415
Daniel Dunbar7f5bf5a2010-07-18 18:31:33 +0000416 int Res = Parser->Run(NoInitialTextSection);
Daniel Dunbarcd4eee52009-08-11 04:34:48 +0000417
Dan Gohman268b0f42010-08-20 01:07:01 +0000418 // Keep output if no errors.
419 if (Res == 0) Out->keep();
Daniel Dunbar14487342010-03-13 19:31:47 +0000420
Daniel Dunbarcd4eee52009-08-11 04:34:48 +0000421 return Res;
Sean Callanan7e645502009-12-17 01:49:59 +0000422}
423
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000424static int DisassembleInput(const char *ProgName, bool Enhanced) {
Daniel Dunbarabf6e362010-03-19 18:07:50 +0000425 const Target *TheTarget = GetTarget(ProgName);
426 if (!TheTarget)
Sean Callanan7e645502009-12-17 01:49:59 +0000427 return 0;
Sean Callanan7e645502009-12-17 01:49:59 +0000428
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +0000429 OwningPtr<MemoryBuffer> Buffer;
430 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename, Buffer)) {
Michael J. Spencer7b6fef82010-12-09 17:36:48 +0000431 errs() << ProgName << ": " << ec.message() << '\n';
Sean Callanan7e645502009-12-17 01:49:59 +0000432 return 1;
433 }
Jim Grosbach112a2de2011-05-09 20:05:25 +0000434
Dan Gohmana2233f22010-09-01 14:20:41 +0000435 OwningPtr<tool_output_file> Out(GetOutputStream());
Dan Gohman268b0f42010-08-20 01:07:01 +0000436 if (!Out)
437 return 1;
438
439 int Res;
Bill Wendling00f0cdd2011-03-21 04:13:46 +0000440 if (Enhanced) {
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +0000441 Res =
442 Disassembler::disassembleEnhanced(TripleName, *Buffer.take(), Out->os());
Bill Wendling00f0cdd2011-03-21 04:13:46 +0000443 } else {
Evan Chengab37af92011-07-06 19:45:42 +0000444 Res = Disassembler::disassemble(*TheTarget, TripleName,
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +0000445 *Buffer.take(), Out->os());
Bill Wendling00f0cdd2011-03-21 04:13:46 +0000446 }
Dan Gohman268b0f42010-08-20 01:07:01 +0000447
448 // Keep output if no errors.
449 if (Res == 0) Out->keep();
450
451 return Res;
Sean Callanan7e645502009-12-17 01:49:59 +0000452}
Chris Lattnerb0133452009-06-21 20:16:42 +0000453
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000454
455int main(int argc, char **argv) {
456 // Print a stack trace if we signal out.
457 sys::PrintStackTraceOnErrorSignal();
458 PrettyStackTraceProgram X(argc, argv);
459 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Daniel Dunbar8c6bad22009-07-17 22:38:58 +0000460
Daniel Dunbar80d484e2009-08-14 03:48:55 +0000461 // Initialize targets and assembly printers/parsers.
Daniel Dunbar8c6bad22009-07-17 22:38:58 +0000462 llvm::InitializeAllTargetInfos();
Daniel Dunbar80d484e2009-08-14 03:48:55 +0000463 // FIXME: We shouldn't need to initialize the Target(Machine)s.
464 llvm::InitializeAllTargets();
Evan Cheng1705ab02011-07-14 23:50:31 +0000465 llvm::InitializeAllMCAsmInfos();
Evan Cheng2129f592011-07-19 06:37:02 +0000466 llvm::InitializeAllMCCodeGenInfos();
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000467 llvm::InitializeAllMCInstrInfos();
Evan Chengd60fa58b2011-07-18 20:57:22 +0000468 llvm::InitializeAllMCRegisterInfos();
Evan Cheng91111d22011-07-09 05:47:46 +0000469 llvm::InitializeAllMCSubtargetInfos();
Daniel Dunbar80d484e2009-08-14 03:48:55 +0000470 llvm::InitializeAllAsmPrinters();
Daniel Dunbar8c6bad22009-07-17 22:38:58 +0000471 llvm::InitializeAllAsmParsers();
Sean Callanan7e645502009-12-17 01:49:59 +0000472 llvm::InitializeAllDisassemblers();
Jim Grosbach112a2de2011-05-09 20:05:25 +0000473
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000474 cl::ParseCommandLineOptions(argc, argv, "llvm machine code playground\n");
Duncan Sands3bd97fe2010-08-28 01:30:02 +0000475 TripleName = Triple::normalize(TripleName);
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000476
477 switch (Action) {
478 default:
Chris Lattnerb0133452009-06-21 20:16:42 +0000479 case AC_AsLex:
480 return AsLexInput(argv[0]);
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000481 case AC_Assemble:
482 return AssembleInput(argv[0]);
Sean Callanan7e645502009-12-17 01:49:59 +0000483 case AC_Disassemble:
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000484 return DisassembleInput(argv[0], false);
485 case AC_EDisassemble:
486 return DisassembleInput(argv[0], true);
Chris Lattnerd70e15b42009-06-21 05:22:37 +0000487 }
Jim Grosbach112a2de2011-05-09 20:05:25 +0000488
Chris Lattner8dd8a522009-06-18 23:04:45 +0000489 return 0;
490}
491