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"
|
2016-11-11 13:34:58 +08:00
|
|
|
#include "llvm/Bitcode/BitcodeReader.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"
|
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>
|
2017-08-16 06:23:44 +08:00
|
|
|
SetImporting("set-importing",
|
|
|
|
cl::desc("Set lazy loading to pretend to import a module"),
|
|
|
|
cl::Hidden);
|
|
|
|
|
|
|
|
static cl::opt<bool>
|
|
|
|
ShowAnnotations("show-annotations",
|
|
|
|
cl::desc("Add informational comments to the .ll file"));
|
2010-09-03 07:21:44 +08:00
|
|
|
|
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
|
|
|
|
2017-09-16 04:10:09 +08:00
|
|
|
struct LLVMDisDiagnosticHandler : public DiagnosticHandler {
|
|
|
|
char *Prefix;
|
|
|
|
LLVMDisDiagnosticHandler(char *PrefixPtr) : Prefix(PrefixPtr) {}
|
|
|
|
bool handleDiagnostics(const DiagnosticInfo &DI) override {
|
|
|
|
raw_ostream &OS = errs();
|
|
|
|
OS << Prefix << ": ";
|
|
|
|
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;
|
|
|
|
}
|
2017-09-16 03:53:54 +08:00
|
|
|
|
2017-09-16 04:10:09 +08:00
|
|
|
DiagnosticPrinterRawOStream DP(OS);
|
|
|
|
DI.print(DP);
|
|
|
|
OS << '\n';
|
2017-09-16 03:53:54 +08:00
|
|
|
|
2017-09-16 04:10:09 +08:00
|
|
|
if (DI.getSeverity() == DS_Error)
|
|
|
|
exit(1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // end anon namespace
|
2015-01-10 08:07:30 +08:00
|
|
|
|
2016-11-10 01:49:19 +08:00
|
|
|
static ExitOnError ExitOnErr;
|
|
|
|
|
|
|
|
static std::unique_ptr<Module> openInputFile(LLVMContext &Context) {
|
|
|
|
std::unique_ptr<MemoryBuffer> MB =
|
|
|
|
ExitOnErr(errorOrToExpected(MemoryBuffer::getFileOrSTDIN(InputFilename)));
|
2017-08-16 06:23:44 +08:00
|
|
|
std::unique_ptr<Module> M = ExitOnErr(getOwningLazyBitcodeModule(
|
|
|
|
std::move(MB), Context,
|
|
|
|
/*ShouldLazyLoadMetadata=*/true, SetImporting));
|
2016-11-02 08:08:19 +08:00
|
|
|
if (MaterializeMetadata)
|
2016-11-10 01:49:19 +08:00
|
|
|
ExitOnErr(M->materializeMetadata());
|
2016-11-02 08:08:19 +08:00
|
|
|
else
|
2016-11-10 01:49:19 +08:00
|
|
|
ExitOnErr(M->materializeAll());
|
|
|
|
return M;
|
2016-06-22 07:42:48 +08:00
|
|
|
}
|
|
|
|
|
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-11-10 01:49:19 +08:00
|
|
|
ExitOnErr.setBanner(std::string(argv[0]) + ": error: ");
|
|
|
|
|
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.
|
2017-09-16 04:10:09 +08:00
|
|
|
Context.setDiagnosticHandler(
|
|
|
|
llvm::make_unique<LLVMDisDiagnosticHandler>(argv[0]));
|
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-11-17 08:42:08 +08:00
|
|
|
std::unique_ptr<Module> M = openInputFile(Context);
|
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;
|
2017-09-23 09:03:17 +08:00
|
|
|
std::unique_ptr<ToolOutputFile> Out(
|
|
|
|
new ToolOutputFile(OutputFilename, EC, sys::fs::F_None));
|
2014-08-26 02:16:47 +08:00
|
|
|
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
|
|
|
}
|