//
//===----------------------------------------------------------------------===//
-#include "llvm/Assembly/Writer.h"
+#include "llvm/Assembly/CachedWriter.h"
#include "llvm/Analysis/SlotCalculator.h"
#include "llvm/Module.h"
#include "llvm/Method.h"
#include "llvm/GlobalVariable.h"
#include "llvm/BasicBlock.h"
-#include "llvm/ConstPoolVals.h"
-#include "llvm/iOther.h"
+#include "llvm/ConstantVals.h"
#include "llvm/iMemory.h"
#include "llvm/iTerminators.h"
+#include "llvm/iPHINode.h"
+#include "llvm/iOther.h"
#include "llvm/SymbolTable.h"
-#include "llvm/Support/STLExtras.h"
-#include "llvm/Support/StringExtras.h"
+#include "Support/StringExtras.h"
+#include "Support/STLExtras.h"
#include <algorithm>
#include <map>
+using std::string;
+using std::map;
+using std::vector;
+using std::ostream;
static const Module *getModuleFromVal(const Value *V) {
if (const MethodArgument *MA =dyn_cast<const MethodArgument>(V))
if (PrintName && V->hasName()) {
Out << " %" << V->getName();
} else {
- if (const ConstPoolVal *CPV = dyn_cast<const ConstPoolVal>(V)) {
+ if (const Constant *CPV = dyn_cast<const Constant>(V)) {
Out << " " << CPV->getStrValue();
} else {
int Slot;
//
const Type *Ty = cast<const Type>(I->second);
if (!isa<PointerType>(Ty) ||
- !cast<PointerType>(Ty)->getValueType()->isPrimitiveType())
- TypeNames.insert(make_pair(Ty, "%"+I->first));
+ !cast<PointerType>(Ty)->getElementType()->isPrimitiveType())
+ TypeNames.insert(std::make_pair(Ty, "%"+I->first));
}
}
}
break;
}
case Type::PointerTyID:
- Result = calcTypeName(cast<const PointerType>(Ty)->getValueType(),
+ Result = calcTypeName(cast<const PointerType>(Ty)->getElementType(),
TypeStack, TypeNames) + " *";
break;
case Type::ArrayTyID: {
//
vector<const Type *> TypeStack;
string TypeName = calcTypeName(Ty, TypeStack, TypeNames);
- TypeNames.insert(make_pair(Ty, TypeName)); // Cache type name for later use
+ TypeNames.insert(std::make_pair(Ty, TypeName));//Cache type name for later use
return Out << TypeName;
}
inline void write(const Method *M) { printMethod(M); }
inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
inline void write(const Instruction *I) { printInstruction(I); }
- inline void write(const ConstPoolVal *CPV) { printConstant(CPV); }
+ inline void write(const Constant *CPV) { printConstant(CPV); }
+ inline void write(const Type *Ty) { printType(Ty); }
private :
void printModule(const Module *M);
void printSymbolTable(const SymbolTable &ST);
- void printConstant(const ConstPoolVal *CPV);
+ void printConstant(const Constant *CPV);
void printGlobal(const GlobalVariable *GV);
void printMethod(const Method *M);
void printMethodArgument(const MethodArgument *MA);
void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
if (GV->hasName()) Out << "%" << GV->getName() << " = ";
+ if (GV->hasInternalLinkage()) Out << "internal ";
if (!GV->hasInitializer()) Out << "uninitialized ";
Out << (GV->isConstant() ? "constant " : "global ");
- printType(GV->getType()->getValueType());
+ printType(GV->getType()->getElementType());
if (GV->hasInitializer())
writeOperand(GV->getInitializer(), false, false);
printInfoComment(GV);
- Out << endl;
+ Out << "\n";
}
for (; I != End; ++I) {
const Value *V = I->second;
- if (const ConstPoolVal *CPV = dyn_cast<const ConstPoolVal>(V)) {
+ if (const Constant *CPV = dyn_cast<const Constant>(V)) {
printConstant(CPV);
} else if (const Type *Ty = dyn_cast<const Type>(V)) {
- Out << "\t%" << I->first << " = type " << Ty->getDescription() << endl;
+ Out << "\t%" << I->first << " = type " << Ty->getDescription() << "\n";
}
}
}
// printConstant - Print out a constant pool entry...
//
-void AssemblyWriter::printConstant(const ConstPoolVal *CPV) {
+void AssemblyWriter::printConstant(const Constant *CPV) {
// Don't print out unnamed constants, they will be inlined
if (!CPV->hasName()) return;
else Out << "<badref>";
}
- Out << endl;
+ Out << "\n";
}
// printMethod - Print all aspects of a method.
//
void AssemblyWriter::printMethod(const Method *M) {
// Print out the return type and name...
- Out << "\n" << (M->isExternal() ? "declare " : "");
+ Out << "\n" << (M->isExternal() ? "declare " : "")
+ << (M->hasInternalLinkage() ? "internal " : "");
printType(M->getReturnType()) << " \"" << M->getName() << "\"(";
Table.incorporateMethod(M);
if (Slot >= 0) Out << ":" << Slot;
else Out << ":<badref>";
}
- Out << "\t[#uses=" << V->use_size() << "]"; // Output # uses
+ Out << " [#uses=" << V->use_size() << "]"; // Output # uses
}
}
} else if (isa<ReturnInst>(I) && !Operand) {
Out << " void";
} else if (isa<CallInst>(I)) {
- // TODO: Should try to print out short form of the Call instruction
- writeOperand(Operand, true);
+ const PointerType *PTy = dyn_cast<PointerType>(Operand->getType());
+ const MethodType *MTy = PTy ?dyn_cast<MethodType>(PTy->getElementType()):0;
+ const Type *RetTy = MTy ? MTy->getReturnType() : 0;
+
+ // If possible, print out the short form of the call instruction, but we can
+ // only do this if the first argument is a pointer to a nonvararg method,
+ // and if the value returned is not a pointer to a method.
+ //
+ if (RetTy && !MTy->isVarArg() &&
+ (!isa<PointerType>(RetTy)||!isa<MethodType>(cast<PointerType>(RetTy)))){
+ Out << " "; printType(RetTy);
+ writeOperand(Operand, false);
+ } else {
+ writeOperand(Operand, true);
+ }
Out << "(";
if (I->getNumOperands() > 1) writeOperand(I->getOperand(1), true);
for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; ++op) {
} else if (I->getOpcode() == Instruction::Malloc ||
I->getOpcode() == Instruction::Alloca) {
Out << " ";
- printType(cast<const PointerType>(I->getType())->getValueType());
+ printType(cast<const PointerType>(I->getType())->getElementType());
if (I->getNumOperands()) {
Out << ",";
writeOperand(I->getOperand(0), true);
}
printInfoComment(I);
- Out << endl;
+ Out << "\n";
}
W.write(BB);
}
-void WriteToAssembly(const ConstPoolVal *CPV, ostream &o) {
+void WriteToAssembly(const Constant *CPV, ostream &o) {
if (CPV == 0) { o << "<null> constant pool value\n"; return; }
o << " " << CPV->getType()->getDescription() << " " << CPV->getStrValue();
}
W.write(I);
}
+
+void CachedWriter::setModule(const Module *M) {
+ delete SC; delete AW;
+ if (M) {
+ SC = new SlotCalculator(M, true);
+ AW = new AssemblyWriter(Out, *SC, M);
+ } else {
+ SC = 0; AW = 0;
+ }
+}
+
+CachedWriter::~CachedWriter() {
+ delete AW;
+ delete SC;
+}
+
+CachedWriter &CachedWriter::operator<<(const Value *V) {
+ assert(AW && SC && "CachedWriter does not have a current module!");
+ switch (V->getValueType()) {
+ case Value::ConstantVal:
+ Out << " "; AW->write(V->getType());
+ Out << " " << cast<Constant>(V)->getStrValue(); break;
+ case Value::MethodArgumentVal:
+ AW->write(V->getType()); Out << " " << V->getName(); break;
+ case Value::TypeVal: AW->write(cast<const Type>(V)); break;
+ case Value::InstructionVal: AW->write(cast<Instruction>(V)); break;
+ case Value::BasicBlockVal: AW->write(cast<BasicBlock>(V)); break;
+ case Value::MethodVal: AW->write(cast<Method>(V)); break;
+ case Value::GlobalVariableVal: AW->write(cast<GlobalVariable>(V)); break;
+ case Value::ModuleVal: AW->write(cast<Module>(V)); break;
+ default: Out << "<unknown value type: " << V->getValueType() << ">"; break;
+ }
+ return *this;
+}