2003-10-20 19:58:43 +02:00
|
|
|
//===-- llvm-dis.cpp - The low-level LLVM disassembler --------------------===//
|
2005-04-22 02:00:37 +02:00
|
|
|
//
|
2003-10-20 19:47:21 +02:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 21:44:31 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 02:00:37 +02:00
|
|
|
//
|
2003-10-20 19:47:21 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-06 22:29:01 +02:00
|
|
|
//
|
|
|
|
// This utility may be invoked in the following manner:
|
2007-07-05 19:07:56 +02: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 23:34:13 +02:00
|
|
|
// to the x.ll file.
|
2001-06-13 21:55:41 +02:00
|
|
|
// Options:
|
|
|
|
// --help - Output information about command line switches
|
2001-06-06 22:29:01 +02:00
|
|
|
//
|
2001-10-13 09:06:57 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-06 22:29:01 +02:00
|
|
|
|
2009-07-01 18:58:40 +02:00
|
|
|
#include "llvm/LLVMContext.h"
|
2012-06-28 02:05:13 +02:00
|
|
|
#include "llvm/DebugInfo.h"
|
2001-06-06 22:29:01 +02:00
|
|
|
#include "llvm/Module.h"
|
2010-09-03 01:21:44 +02:00
|
|
|
#include "llvm/Type.h"
|
2011-03-11 19:07:33 +01:00
|
|
|
#include "llvm/IntrinsicInst.h"
|
2007-04-22 08:31:02 +02:00
|
|
|
#include "llvm/Bitcode/ReaderWriter.h"
|
2010-09-03 01:21:44 +02:00
|
|
|
#include "llvm/Assembly/AssemblyAnnotationWriter.h"
|
2004-09-02 00:55:40 +02:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2012-02-06 23:30:29 +01:00
|
|
|
#include "llvm/Support/DataStream.h"
|
2010-09-03 01:21:44 +02:00
|
|
|
#include "llvm/Support/FormattedStream.h"
|
2006-12-06 02:18:01 +01:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2007-04-29 09:54:31 +02:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2009-03-06 06:34:10 +01:00
|
|
|
#include "llvm/Support/PrettyStackTrace.h"
|
2010-10-07 22:32:40 +02:00
|
|
|
#include "llvm/Support/ToolOutputFile.h"
|
2010-11-29 19:16:10 +01:00
|
|
|
#include "llvm/Support/Signals.h"
|
2010-12-09 18:36:48 +01:00
|
|
|
#include "llvm/Support/system_error.h"
|
2003-11-11 23:41:34 +01:00
|
|
|
using namespace llvm;
|
|
|
|
|
2002-07-25 18:31:09 +02:00
|
|
|
static cl::opt<std::string>
|
2007-07-05 19:07:56 +02:00
|
|
|
InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
|
2002-07-22 04:10:13 +02:00
|
|
|
|
2002-07-25 18:31:09 +02:00
|
|
|
static cl::opt<std::string>
|
2005-04-22 02:00:37 +02:00
|
|
|
OutputFilename("o", cl::desc("Override output filename"),
|
2002-07-22 04:10:13 +02:00
|
|
|
cl::value_desc("filename"));
|
|
|
|
|
|
|
|
static cl::opt<bool>
|
2009-08-25 17:34:52 +02:00
|
|
|
Force("f", cl::desc("Enable binary output on terminals"));
|
2002-07-22 04:10:13 +02:00
|
|
|
|
2007-02-07 05:39:35 +01:00
|
|
|
static cl::opt<bool>
|
|
|
|
DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden);
|
|
|
|
|
2010-09-03 01:21:44 +02:00
|
|
|
static cl::opt<bool>
|
|
|
|
ShowAnnotations("show-annotations",
|
|
|
|
cl::desc("Add informational comments to the .ll file"));
|
|
|
|
|
|
|
|
namespace {
|
2010-12-16 17:23:30 +01:00
|
|
|
|
2011-03-11 19:07:33 +01:00
|
|
|
static void printDebugLoc(const DebugLoc &DL, formatted_raw_ostream &OS) {
|
|
|
|
OS << DL.getLine() << ":" << DL.getCol();
|
|
|
|
if (MDNode *N = DL.getInlinedAt(getGlobalContext())) {
|
|
|
|
DebugLoc IDL = DebugLoc::getFromDILocation(N);
|
|
|
|
if (!IDL.isUnknown()) {
|
|
|
|
OS << "@";
|
|
|
|
printDebugLoc(IDL,OS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-09-03 01:21:44 +02:00
|
|
|
class CommentWriter : public AssemblyAnnotationWriter {
|
|
|
|
public:
|
|
|
|
void emitFunctionAnnot(const Function *F,
|
|
|
|
formatted_raw_ostream &OS) {
|
|
|
|
OS << "; [#uses=" << F->getNumUses() << ']'; // Output # uses
|
|
|
|
OS << '\n';
|
|
|
|
}
|
|
|
|
void printInfoComment(const Value &V, formatted_raw_ostream &OS) {
|
2011-03-11 19:07:33 +01:00
|
|
|
bool Padded = false;
|
|
|
|
if (!V.getType()->isVoidTy()) {
|
|
|
|
OS.PadToColumn(50);
|
|
|
|
Padded = true;
|
2011-06-18 23:18:23 +02:00
|
|
|
OS << "; [#uses=" << V.getNumUses() << " type=" << *V.getType() << "]"; // Output # uses and type
|
2011-03-11 19:07:33 +01:00
|
|
|
}
|
|
|
|
if (const Instruction *I = dyn_cast<Instruction>(&V)) {
|
|
|
|
const DebugLoc &DL = I->getDebugLoc();
|
|
|
|
if (!DL.isUnknown()) {
|
|
|
|
if (!Padded) {
|
|
|
|
OS.PadToColumn(50);
|
|
|
|
Padded = true;
|
|
|
|
OS << ";";
|
|
|
|
}
|
|
|
|
OS << " [debug line = ";
|
|
|
|
printDebugLoc(DL,OS);
|
|
|
|
OS << "]";
|
|
|
|
}
|
|
|
|
if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I)) {
|
|
|
|
DIVariable Var(DDI->getVariable());
|
|
|
|
if (!Padded) {
|
|
|
|
OS.PadToColumn(50);
|
|
|
|
OS << ";";
|
|
|
|
}
|
|
|
|
OS << " [debug variable = " << Var.getName() << "]";
|
|
|
|
}
|
|
|
|
else if (const DbgValueInst *DVI = dyn_cast<DbgValueInst>(I)) {
|
|
|
|
DIVariable Var(DVI->getVariable());
|
|
|
|
if (!Padded) {
|
|
|
|
OS.PadToColumn(50);
|
|
|
|
OS << ";";
|
|
|
|
}
|
|
|
|
OS << " [debug variable = " << Var.getName() << "]";
|
|
|
|
}
|
|
|
|
}
|
2010-09-03 01:21:44 +02:00
|
|
|
}
|
|
|
|
};
|
2010-12-16 17:23:30 +01:00
|
|
|
|
2010-09-03 01:21:44 +02:00
|
|
|
} // end anon namespace
|
|
|
|
|
2001-07-23 04:35:57 +02:00
|
|
|
int main(int argc, char **argv) {
|
2009-03-06 06:34:10 +01:00
|
|
|
// Print a stack trace if we signal out.
|
|
|
|
sys::PrintStackTraceOnErrorSignal();
|
|
|
|
PrettyStackTraceProgram X(argc, argv);
|
2010-12-16 17:23:30 +01:00
|
|
|
|
2009-07-16 00:16:10 +02:00
|
|
|
LLVMContext &Context = getGlobalContext();
|
2009-03-06 06:34:10 +01:00
|
|
|
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
|
2010-12-16 17:23:30 +01:00
|
|
|
|
|
|
|
|
2009-08-23 04:51:22 +02:00
|
|
|
cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .ll disassembler\n");
|
2001-06-06 22:29:01 +02:00
|
|
|
|
2009-08-23 04:51:22 +02:00
|
|
|
std::string ErrorMessage;
|
|
|
|
std::auto_ptr<Module> M;
|
2010-12-09 18:36:48 +01:00
|
|
|
|
2012-02-06 23:30:29 +01:00
|
|
|
// Use the bitcode streaming interface
|
|
|
|
DataStreamer *streamer = getDataFileStreamer(InputFilename, &ErrorMessage);
|
|
|
|
if (streamer) {
|
|
|
|
std::string DisplayFilename;
|
|
|
|
if (InputFilename == "-")
|
|
|
|
DisplayFilename = "<stdin>";
|
2010-12-16 23:37:52 +01:00
|
|
|
else
|
2012-02-06 23:30:29 +01:00
|
|
|
DisplayFilename = InputFilename;
|
|
|
|
M.reset(getStreamedBitcodeModule(DisplayFilename, streamer, Context,
|
|
|
|
&ErrorMessage));
|
|
|
|
if(M.get() != 0 && M->MaterializeAllPermanently(&ErrorMessage)) {
|
|
|
|
M.reset();
|
|
|
|
}
|
2010-12-16 23:37:52 +01:00
|
|
|
}
|
2007-04-29 09:54:31 +02:00
|
|
|
|
2009-08-23 04:51:22 +02:00
|
|
|
if (M.get() == 0) {
|
|
|
|
errs() << argv[0] << ": ";
|
|
|
|
if (ErrorMessage.size())
|
|
|
|
errs() << ErrorMessage << "\n";
|
|
|
|
else
|
|
|
|
errs() << "bitcode didn't read correctly.\n";
|
|
|
|
return 1;
|
|
|
|
}
|
2010-12-16 17:23:30 +01:00
|
|
|
|
2009-08-23 04:51:22 +02:00
|
|
|
// Just use stdout. We won't actually print anything on it.
|
|
|
|
if (DontPrint)
|
|
|
|
OutputFilename = "-";
|
2010-12-16 17:23:30 +01:00
|
|
|
|
2009-08-23 04:51:22 +02:00
|
|
|
if (OutputFilename.empty()) { // Unspecified output, infer it.
|
|
|
|
if (InputFilename == "-") {
|
|
|
|
OutputFilename = "-";
|
2004-12-30 06:36:08 +01:00
|
|
|
} else {
|
2009-08-23 04:51:22 +02:00
|
|
|
const std::string &IFN = InputFilename;
|
|
|
|
int Len = IFN.length();
|
|
|
|
// If the source ends in .bc, strip it off.
|
|
|
|
if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c')
|
|
|
|
OutputFilename = std::string(IFN.begin(), IFN.end()-3)+".ll";
|
|
|
|
else
|
|
|
|
OutputFilename = IFN+".ll";
|
2004-12-30 06:36:08 +01:00
|
|
|
}
|
2009-08-23 04:51:22 +02:00
|
|
|
}
|
2009-09-11 22:46:33 +02:00
|
|
|
|
2009-08-23 04:51:22 +02:00
|
|
|
std::string ErrorInfo;
|
2010-12-16 17:23:30 +01:00
|
|
|
OwningPtr<tool_output_file>
|
2010-08-20 03:07:01 +02:00
|
|
|
Out(new tool_output_file(OutputFilename.c_str(), ErrorInfo,
|
|
|
|
raw_fd_ostream::F_Binary));
|
2009-08-23 04:51:22 +02:00
|
|
|
if (!ErrorInfo.empty()) {
|
|
|
|
errs() << ErrorInfo << '\n';
|
|
|
|
return 1;
|
|
|
|
}
|
2001-07-23 04:35:57 +02:00
|
|
|
|
2010-09-03 01:21:44 +02:00
|
|
|
OwningPtr<AssemblyAnnotationWriter> Annotator;
|
|
|
|
if (ShowAnnotations)
|
|
|
|
Annotator.reset(new CommentWriter());
|
2010-12-16 17:23:30 +01:00
|
|
|
|
2009-08-23 04:51:22 +02:00
|
|
|
// All that llvm-dis does is write the assembly to a file.
|
2009-09-15 17:33:42 +02:00
|
|
|
if (!DontPrint)
|
2010-09-03 01:21:44 +02:00
|
|
|
M->print(Out->os(), Annotator.get());
|
2006-12-06 02:18:01 +01:00
|
|
|
|
2010-08-20 03:07:01 +02:00
|
|
|
// Declare success.
|
|
|
|
Out->keep();
|
|
|
|
|
2009-08-23 04:51:22 +02:00
|
|
|
return 0;
|
2001-06-06 22:29:01 +02:00
|
|
|
}
|