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"
|
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);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
sys::PrintStackTraceOnErrorSignal();
|
|
|
|
PrettyStackTraceProgram X(argc, argv);
|
2010-12-17 00:23:30 +08:00
|
|
|
|
2009-07-16 06:16:10 +08:00
|
|
|
LLVMContext &Context = getGlobalContext();
|
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
|
|
|
|
2009-08-23 10:51:22 +08:00
|
|
|
std::string ErrorMessage;
|
2014-03-06 13:51:42 +08:00
|
|
|
std::unique_ptr<Module> M;
|
2010-12-10 01:36:48 +08:00
|
|
|
|
2012-02-07 06:30:29 +08:00
|
|
|
// Use the bitcode streaming interface
|
2015-06-17 07:29:49 +08:00
|
|
|
std::unique_ptr<DataStreamer> Streamer =
|
|
|
|
getDataFileStreamer(InputFilename, &ErrorMessage);
|
2014-12-18 13:08:43 +08:00
|
|
|
if (Streamer) {
|
2012-02-07 06:30:29 +08:00
|
|
|
std::string DisplayFilename;
|
|
|
|
if (InputFilename == "-")
|
|
|
|
DisplayFilename = "<stdin>";
|
2010-12-17 06:37:52 +08:00
|
|
|
else
|
2012-02-07 06:30:29 +08:00
|
|
|
DisplayFilename = InputFilename;
|
2014-12-18 13:08:43 +08:00
|
|
|
ErrorOr<std::unique_ptr<Module>> MOrErr =
|
2015-06-17 07:29:49 +08:00
|
|
|
getStreamedBitcodeModule(DisplayFilename, std::move(Streamer), Context);
|
2015-01-10 08:07:30 +08:00
|
|
|
M = std::move(*MOrErr);
|
|
|
|
M->materializeAllPermanently();
|
2015-05-12 05:20:20 +08:00
|
|
|
} else {
|
|
|
|
errs() << argv[0] << ": " << ErrorMessage << '\n';
|
|
|
|
return 1;
|
2009-08-23 10:51:22 +08:00
|
|
|
}
|
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
|
|
|
}
|