2003-10-20 17:58:43 +00:00
|
|
|
//===-- llvm-dis.cpp - The low-level LLVM disassembler --------------------===//
|
2005-04-22 00:00:37 +00:00
|
|
|
//
|
2003-10-20 17:47:21 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:44:31 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 00:00:37 +00:00
|
|
|
//
|
2003-10-20 17:47:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-06 20:29:01 +00:00
|
|
|
//
|
|
|
|
// This utility may be invoked in the following manner:
|
2007-07-05 17:07:56 +00:00
|
|
|
// llvm-dis [options] - Read LLVM bitcode from stdin, write asm to stdout
|
|
|
|
// llvm-dis [options] x.bc - Read LLVM bitcode from the x.bc file, write asm
|
2003-08-28 21:34:13 +00:00
|
|
|
// to the x.ll file.
|
2001-06-13 19:55:41 +00:00
|
|
|
// Options:
|
|
|
|
// --help - Output information about command line switches
|
2001-06-06 20:29:01 +00:00
|
|
|
//
|
2001-10-13 07:06:57 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2012-12-04 10:44:52 +00:00
|
|
|
#include "llvm/Bitcode/ReaderWriter.h"
|
2014-01-07 12:34:26 +00:00
|
|
|
#include "llvm/IR/AssemblyAnnotationWriter.h"
|
2014-03-06 00:46:21 +00:00
|
|
|
#include "llvm/IR/DebugInfo.h"
|
2015-01-10 00:07:30 +00:00
|
|
|
#include "llvm/IR/DiagnosticInfo.h"
|
|
|
|
#include "llvm/IR/DiagnosticPrinter.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/IntrinsicInst.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
|
|
|
#include "llvm/IR/Type.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2012-02-06 22:30:29 +00:00
|
|
|
#include "llvm/Support/DataStream.h"
|
2014-04-29 23:26:49 +00:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2010-09-02 23:21:44 +00:00
|
|
|
#include "llvm/Support/FormattedStream.h"
|
2006-12-06 01:18:01 +00:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2007-04-29 07:54:31 +00:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2009-03-06 05:34:10 +00:00
|
|
|
#include "llvm/Support/PrettyStackTrace.h"
|
2010-11-29 18:16:10 +00:00
|
|
|
#include "llvm/Support/Signals.h"
|
2012-12-04 10:44:52 +00:00
|
|
|
#include "llvm/Support/ToolOutputFile.h"
|
2014-06-12 17:38:55 +00:00
|
|
|
#include <system_error>
|
2003-11-11 22:41:34 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2002-07-25 16:31:09 +00:00
|
|
|
static cl::opt<std::string>
|
2007-07-05 17:07:56 +00:00
|
|
|
InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
|
2002-07-22 02:10:13 +00:00
|
|
|
|
2002-07-25 16:31:09 +00:00
|
|
|
static cl::opt<std::string>
|
2005-04-22 00:00:37 +00:00
|
|
|
OutputFilename("o", cl::desc("Override output filename"),
|
2002-07-22 02:10:13 +00:00
|
|
|
cl::value_desc("filename"));
|
|
|
|
|
|
|
|
static cl::opt<bool>
|
2009-08-25 15:34:52 +00:00
|
|
|
Force("f", cl::desc("Enable binary output on terminals"));
|
2002-07-22 02:10:13 +00:00
|
|
|
|
2007-02-07 04:39:35 +00:00
|
|
|
static cl::opt<bool>
|
|
|
|
DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden);
|
|
|
|
|
2010-09-02 23:21:44 +00:00
|
|
|
static cl::opt<bool>
|
|
|
|
ShowAnnotations("show-annotations",
|
|
|
|
cl::desc("Add informational comments to the .ll file"));
|
|
|
|
|
2015-04-15 03:14:06 +00:00
|
|
|
static cl::opt<bool> PreserveAssemblyUseListOrder(
|
|
|
|
"preserve-ll-uselistorder",
|
|
|
|
cl::desc("Preserve use-list order when writing LLVM assembly."),
|
|
|
|
cl::init(false), cl::Hidden);
|
|
|
|
|
2010-09-02 23:21:44 +00:00
|
|
|
namespace {
|
2010-12-16 16:23:30 +00:00
|
|
|
|
2011-03-11 18:07:33 +00:00
|
|
|
static void printDebugLoc(const DebugLoc &DL, formatted_raw_ostream &OS) {
|
|
|
|
OS << DL.getLine() << ":" << DL.getCol();
|
2015-04-29 16:38:44 +00:00
|
|
|
if (DILocation *IDL = DL.getInlinedAt()) {
|
2015-03-30 20:04:06 +00:00
|
|
|
OS << "@";
|
|
|
|
printDebugLoc(IDL, OS);
|
2011-03-11 18:07:33 +00:00
|
|
|
}
|
|
|
|
}
|
2010-09-02 23:21:44 +00:00
|
|
|
class CommentWriter : public AssemblyAnnotationWriter {
|
|
|
|
public:
|
|
|
|
void emitFunctionAnnot(const Function *F,
|
2014-03-08 08:27:28 +00:00
|
|
|
formatted_raw_ostream &OS) override {
|
2010-09-02 23:21:44 +00:00
|
|
|
OS << "; [#uses=" << F->getNumUses() << ']'; // Output # uses
|
|
|
|
OS << '\n';
|
|
|
|
}
|
2014-03-08 08:27:28 +00:00
|
|
|
void printInfoComment(const Value &V, formatted_raw_ostream &OS) override {
|
2011-03-11 18:07:33 +00:00
|
|
|
bool Padded = false;
|
|
|
|
if (!V.getType()->isVoidTy()) {
|
|
|
|
OS.PadToColumn(50);
|
|
|
|
Padded = true;
|
2015-05-11 21:20:20 +00:00
|
|
|
// Output # uses and type
|
|
|
|
OS << "; [#uses=" << V.getNumUses() << " type=" << *V.getType() << "]";
|
2011-03-11 18:07:33 +00:00
|
|
|
}
|
|
|
|
if (const Instruction *I = dyn_cast<Instruction>(&V)) {
|
2015-03-30 20:04:06 +00:00
|
|
|
if (const DebugLoc &DL = I->getDebugLoc()) {
|
2011-03-11 18:07:33 +00:00
|
|
|
if (!Padded) {
|
|
|
|
OS.PadToColumn(50);
|
|
|
|
Padded = true;
|
|
|
|
OS << ";";
|
|
|
|
}
|
|
|
|
OS << " [debug line = ";
|
|
|
|
printDebugLoc(DL,OS);
|
|
|
|
OS << "]";
|
|
|
|
}
|
|
|
|
if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I)) {
|
|
|
|
if (!Padded) {
|
|
|
|
OS.PadToColumn(50);
|
|
|
|
OS << ";";
|
|
|
|
}
|
2015-04-21 18:44:06 +00:00
|
|
|
OS << " [debug variable = " << DDI->getVariable()->getName() << "]";
|
2011-03-11 18:07:33 +00:00
|
|
|
}
|
|
|
|
else if (const DbgValueInst *DVI = dyn_cast<DbgValueInst>(I)) {
|
|
|
|
if (!Padded) {
|
|
|
|
OS.PadToColumn(50);
|
|
|
|
OS << ";";
|
|
|
|
}
|
2015-04-21 18:44:06 +00:00
|
|
|
OS << " [debug variable = " << DVI->getVariable()->getName() << "]";
|
2011-03-11 18:07:33 +00:00
|
|
|
}
|
|
|
|
}
|
2010-09-02 23:21:44 +00:00
|
|
|
}
|
|
|
|
};
|
2010-12-16 16:23:30 +00:00
|
|
|
|
2010-09-02 23:21:44 +00:00
|
|
|
} // end anon namespace
|
|
|
|
|
2015-01-10 00:07:30 +00:00
|
|
|
static void diagnosticHandler(const DiagnosticInfo &DI, void *Context) {
|
|
|
|
raw_ostream &OS = errs();
|
|
|
|
OS << (char *)Context << ": ";
|
2015-02-25 01:10:03 +00:00
|
|
|
switch (DI.getSeverity()) {
|
|
|
|
case DS_Error: OS << "error: "; break;
|
|
|
|
case DS_Warning: OS << "warning: "; break;
|
|
|
|
case DS_Remark: OS << "remark: "; break;
|
|
|
|
case DS_Note: OS << "note: "; break;
|
|
|
|
}
|
|
|
|
|
2015-01-10 00:07:30 +00:00
|
|
|
DiagnosticPrinterRawOStream DP(OS);
|
|
|
|
DI.print(DP);
|
|
|
|
OS << '\n';
|
2015-02-25 01:10:03 +00:00
|
|
|
|
|
|
|
if (DI.getSeverity() == DS_Error)
|
|
|
|
exit(1);
|
2015-01-10 00:07:30 +00:00
|
|
|
}
|
|
|
|
|
2001-07-23 02:35:57 +00:00
|
|
|
int main(int argc, char **argv) {
|
2009-03-06 05:34:10 +00:00
|
|
|
// Print a stack trace if we signal out.
|
|
|
|
sys::PrintStackTraceOnErrorSignal();
|
|
|
|
PrettyStackTraceProgram X(argc, argv);
|
2010-12-16 16:23:30 +00:00
|
|
|
|
2009-07-15 22:16:10 +00:00
|
|
|
LLVMContext &Context = getGlobalContext();
|
2009-03-06 05:34:10 +00:00
|
|
|
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
|
2010-12-16 16:23:30 +00:00
|
|
|
|
2015-01-10 00:07:30 +00:00
|
|
|
Context.setDiagnosticHandler(diagnosticHandler, argv[0]);
|
2010-12-16 16:23:30 +00:00
|
|
|
|
2009-08-23 02:51:22 +00:00
|
|
|
cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .ll disassembler\n");
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2009-08-23 02:51:22 +00:00
|
|
|
std::string ErrorMessage;
|
2014-03-06 05:51:42 +00:00
|
|
|
std::unique_ptr<Module> M;
|
2010-12-09 17:36:48 +00:00
|
|
|
|
2012-02-06 22:30:29 +00:00
|
|
|
// Use the bitcode streaming interface
|
2014-12-18 05:08:43 +00:00
|
|
|
DataStreamer *Streamer = getDataFileStreamer(InputFilename, &ErrorMessage);
|
|
|
|
if (Streamer) {
|
2012-02-06 22:30:29 +00:00
|
|
|
std::string DisplayFilename;
|
|
|
|
if (InputFilename == "-")
|
|
|
|
DisplayFilename = "<stdin>";
|
2010-12-16 22:37:52 +00:00
|
|
|
else
|
2012-02-06 22:30:29 +00:00
|
|
|
DisplayFilename = InputFilename;
|
2014-12-18 05:08:43 +00:00
|
|
|
ErrorOr<std::unique_ptr<Module>> MOrErr =
|
|
|
|
getStreamedBitcodeModule(DisplayFilename, Streamer, Context);
|
2015-01-10 00:07:30 +00:00
|
|
|
M = std::move(*MOrErr);
|
|
|
|
M->materializeAllPermanently();
|
2015-05-11 21:20:20 +00:00
|
|
|
} else {
|
|
|
|
errs() << argv[0] << ": " << ErrorMessage << '\n';
|
|
|
|
return 1;
|
2009-08-23 02:51:22 +00:00
|
|
|
}
|
2010-12-16 16:23:30 +00:00
|
|
|
|
2009-08-23 02:51:22 +00:00
|
|
|
// Just use stdout. We won't actually print anything on it.
|
|
|
|
if (DontPrint)
|
|
|
|
OutputFilename = "-";
|
2010-12-16 16:23:30 +00:00
|
|
|
|
2009-08-23 02:51:22 +00:00
|
|
|
if (OutputFilename.empty()) { // Unspecified output, infer it.
|
|
|
|
if (InputFilename == "-") {
|
|
|
|
OutputFilename = "-";
|
2004-12-30 05:36:08 +00:00
|
|
|
} else {
|
2015-05-11 21:20:20 +00:00
|
|
|
StringRef IFN = InputFilename;
|
|
|
|
OutputFilename = (IFN.endswith(".bc") ? IFN.drop_back(3) : IFN).str();
|
|
|
|
OutputFilename += ".ll";
|
2004-12-30 05:36:08 +00:00
|
|
|
}
|
2009-08-23 02:51:22 +00:00
|
|
|
}
|
2009-09-11 20:46:33 +00:00
|
|
|
|
2014-08-25 18:16:47 +00:00
|
|
|
std::error_code EC;
|
2014-03-06 05:51:42 +00:00
|
|
|
std::unique_ptr<tool_output_file> Out(
|
2014-08-25 18:16:47 +00:00
|
|
|
new tool_output_file(OutputFilename, EC, sys::fs::F_None));
|
|
|
|
if (EC) {
|
|
|
|
errs() << EC.message() << '\n';
|
2009-08-23 02:51:22 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2001-07-23 02:35:57 +00:00
|
|
|
|
2014-03-06 05:51:42 +00:00
|
|
|
std::unique_ptr<AssemblyAnnotationWriter> Annotator;
|
2010-09-02 23:21:44 +00:00
|
|
|
if (ShowAnnotations)
|
|
|
|
Annotator.reset(new CommentWriter());
|
2010-12-16 16:23:30 +00:00
|
|
|
|
2009-08-23 02:51:22 +00:00
|
|
|
// All that llvm-dis does is write the assembly to a file.
|
2009-09-15 15:33:42 +00:00
|
|
|
if (!DontPrint)
|
2015-04-15 03:14:06 +00:00
|
|
|
M->print(Out->os(), Annotator.get(), PreserveAssemblyUseListOrder);
|
2006-12-06 01:18:01 +00:00
|
|
|
|
2010-08-20 01:07:01 +00:00
|
|
|
// Declare success.
|
|
|
|
Out->keep();
|
|
|
|
|
2009-08-23 02:51:22 +00:00
|
|
|
return 0;
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|