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