//===-- X86/Printer.cpp - Convert X86 code to human readable rep. ---------===//
//
// This file contains a printer that converts from our internal representation
// of LLVM code to a nice human readable form that is suitable for debugging.
//
//===----------------------------------------------------------------------===//
#include "X86.h"
#include "X86InstrInfo.h"
#include "llvm/Function.h"
#include "llvm/Constant.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachineConstantPool.h"
#include "llvm/CodeGen/MachineInstr.h"
#include "Support/Statistic.h"
#include "Support/hash_map"
#include "llvm/Type.h"
#include "llvm/Constants.h"
#include "llvm/Assembly/Writer.h"
#include "llvm/DerivedTypes.h"
#include "llvm/SlotCalculator.h"
#include "Support/StringExtras.h"
#include "llvm/Module.h"
namespace {
std::set<const Value *> MangledGlobals;
struct Printer : public MachineFunctionPass {
std::ostream &O;
typedef std::map<const Value *, unsigned> ValueMapTy;
ValueMapTy NumberForBB;
Printer(std::ostream &o) : O(o) {}
const TargetData *TD;
std::string CurrentFnName;
virtual const char *getPassName() const {
return "X86 Assembly Printer";
}
void printMachineInstruction(const MachineInstr *MI, std::ostream &O,
const TargetMachine &TM) const;
void printOp(std::ostream &O, const MachineOperand &MO,
const MRegisterInfo &RI, bool elideOffsetKeyword = false) const;
void printMemReference(std::ostream &O, const MachineInstr *MI,
unsigned Op,
const MRegisterInfo &RI) const;
void printConstantPool(MachineConstantPool *MCP);
bool runOnMachineFunction(MachineFunction &F);
std::string ConstantExprToString(const ConstantExpr* CE);
std::string valToExprString(const Value* V);
bool doInitialization(Module &M);
bool doFinalization(Module &M);
void PrintZeroBytesToPad(int numBytes);
void printConstantValueOnly(const Constant* CV, int numPadBytesAfter = 0);
void printSingleConstantValue(const Constant* CV);
};
} // end of anonymous namespace
/// createX86CodePrinterPass - Print out the specified machine code function to
/// the specified stream. This function should work regardless of whether or
/// not the function is in SSA form or not.
///
Pass *createX86CodePrinterPass(std::ostream &O) {
return new Printer(O);
}
// We don't want identifier names with ., space, or - in them,
// so we replace them with underscores.
static std::string makeNameProper(std::string x) {
std::string tmp;
for (std::string::iterator sI = x.begin(), sEnd = x.end(); sI != sEnd; sI++)
switch (*sI) {
case '.': tmp += "d_"; break;
case ' ': tmp += "s_"; break;
case '-': tmp += "D_"; break;
default: tmp += *sI;
}
return tmp;
}
static std::string getValueName(const Value *V) {
if (V->hasName()) { // Print out the label if it exists...
// Name mangling occurs as follows:
// - If V is not a global, mangling always occurs.
// - Otherwise, mangling occurs when any of the following are true:
// 1) V has internal linkage
// 2) V's name would collide if it is not mangled.
//
if(const GlobalValue* gv = dyn_cast<GlobalValue>(V)) {
if(!gv->hasInternalLinkage() && !MangledGlobals.count(gv)) {
// No internal linkage, name will not collide -> no mangling.
return makeNameProper(gv->getName());
}
}
// Non-global, or global with internal linkage / colliding name -> mangle.
return "l" + utostr(V->getType()->getUniqueID()) + "_" +
makeNameProper(V->getName());
}
static int Count = 0;
Count++;
return "ltmp_" + itostr(Count) + "_" + utostr(V->getType()->getUniqueID());
}
// valToExprString - Helper function for ConstantExprToString().
// Appends result to argument string S.
//
std::string Printer::valToExprString(const Value* V) {
std::string S;
bool