X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=lib%2FVMCore%2FAsmWriter.cpp;h=8c7b06ba0f17f264409a724303051e88e1dc959f;hb=61b91bc156bb9e3d9017a9e93d567f8dccfa3f68;hp=5b4ad43d107a333a8b041b9a65e252a362e7bd7b;hpb=9d73279ec8b38732bc1cfc3921de4949606cd0bf;p=oota-llvm.git diff --git a/lib/VMCore/AsmWriter.cpp b/lib/VMCore/AsmWriter.cpp index 5b4ad43d107..8c7b06ba0f1 100644 --- a/lib/VMCore/AsmWriter.cpp +++ b/lib/VMCore/AsmWriter.cpp @@ -10,27 +10,31 @@ #include "llvm/Assembly/CachedWriter.h" #include "llvm/Assembly/Writer.h" +#include "llvm/Assembly/PrintModulePass.h" #include "llvm/SlotCalculator.h" +#include "llvm/DerivedTypes.h" +#include "llvm/Instruction.h" #include "llvm/Module.h" -#include "llvm/Function.h" -#include "llvm/GlobalVariable.h" -#include "llvm/BasicBlock.h" #include "llvm/Constants.h" #include "llvm/iMemory.h" #include "llvm/iTerminators.h" #include "llvm/iPHINode.h" #include "llvm/iOther.h" #include "llvm/SymbolTable.h" -#include "llvm/Argument.h" +#include "llvm/Support/CFG.h" #include "Support/StringExtras.h" #include "Support/STLExtras.h" #include -#include using std::string; using std::map; using std::vector; using std::ostream; +static RegisterPass +X("printm", "Print module to stderr",PassInfo::Analysis|PassInfo::Optimization); +static RegisterPass +Y("print","Print function to stderr",PassInfo::Analysis|PassInfo::Optimization); + static void WriteAsOperandInternal(ostream &Out, const Value *V, bool PrintName, map &TypeTable, SlotCalculator *Table); @@ -154,8 +158,7 @@ static string calcTypeName(const Type *Ty, vector &TypeStack, break; } default: - assert(0 && "Unhandled case in getTypeProps!"); - Result = ""; + Result = ""; } TypeStack.pop_back(); // Remove self from stack... @@ -269,7 +272,7 @@ static void WriteConstantInt(ostream &Out, const Constant *CV, bool PrintName, (unsigned char)cast(CA->getOperand(i))->getValue() : (unsigned char)cast(CA->getOperand(i))->getValue(); - if (isprint(C)) { + if (isprint(C) && C != '"' && C != '\\') { Out << C; } else { Out << '\\' @@ -317,7 +320,7 @@ static void WriteConstantInt(ostream &Out, const Constant *CV, bool PrintName, } else if (isa(CV)) { Out << "null"; - } else if (ConstantPointerRef *PR = dyn_cast(CV)) { + } else if (const ConstantPointerRef *PR = dyn_cast(CV)) { const GlobalValue *V = PR->getValue(); if (V->hasName()) { Out << "%" << V->getName(); @@ -330,8 +333,28 @@ static void WriteConstantInt(ostream &Out, const Constant *CV, bool PrintName, } else { Out << ""; } + + } else if (const ConstantExpr *CE = dyn_cast(CV)) { + Out << CE->getOpcodeName(); + + bool isGEP = CE->getOpcode() == Instruction::GetElementPtr; + Out << " ("; + + for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) { + printTypeInt(Out, (*OI)->getType(), TypeTable); + WriteAsOperandInternal(Out, *OI, PrintName, TypeTable, Table); + if (OI+1 != CE->op_end()) + Out << ", "; + } + + if (CE->getOpcode() == Instruction::Cast) { + Out << " to "; + printTypeInt(Out, CE->getType(), TypeTable); + } + Out << ")"; + } else { - assert(0 && "Unrecognized constant value!!!"); + Out << ""; } } @@ -379,17 +402,17 @@ static void WriteAsOperandInternal(ostream &Out, const Value *V, bool PrintName, // whole instruction that generated it. // ostream &WriteAsOperand(ostream &Out, const Value *V, bool PrintType, - bool PrintName, SlotCalculator *Table) { + bool PrintName, const Module *Context) { map TypeNames; - const Module *M = getModuleFromVal(V); + if (Context == 0) Context = getModuleFromVal(V); - if (M && M->hasSymbolTable()) - fillTypeNameTable(M, TypeNames); + if (Context && Context->hasSymbolTable()) + fillTypeNameTable(Context, TypeNames); if (PrintType) printTypeInt(Out, V->getType(), TypeNames); - WriteAsOperandInternal(Out, V, PrintName, TypeNames, Table); + WriteAsOperandInternal(Out, V, PrintName, TypeNames, 0); return Out; } @@ -414,7 +437,7 @@ public: inline void write(const GlobalVariable *G) { printGlobal(G); } inline void write(const Function *F) { printFunction(F); } inline void write(const BasicBlock *BB) { printBasicBlock(BB); } - inline void write(const Instruction *I) { printInstruction(I); } + inline void write(const Instruction *I) { printInstruction(*I); } inline void write(const Constant *CPV) { printConstant(CPV); } inline void write(const Type *Ty) { printType(Ty); } @@ -428,7 +451,7 @@ private : void printFunction(const Function *F); void printArgument(const Argument *FA); void printBasicBlock(const BasicBlock *BB); - void printInstruction(const Instruction *I); + void printInstruction(const Instruction &I); // printType - Go to extreme measures to attempt to print out a short, // symbolic version of a type name. @@ -444,7 +467,7 @@ private : // printInfoComment - Print a little comment after the instruction indicating // which slot it occupies. - void printInfoComment(const Value *V); + void printInfoComment(const Value &V); }; @@ -452,7 +475,7 @@ private : // without considering any symbolic types that we may have equal to it. // ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) { - if (FunctionType *FTy = dyn_cast(Ty)) { + if (const FunctionType *FTy = dyn_cast(Ty)) { printType(FTy->getReturnType()) << " ("; for (FunctionType::ParamTypes::const_iterator I = FTy->getParamTypes().begin(), @@ -466,7 +489,7 @@ ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) { Out << "..."; } Out << ")"; - } else if (StructType *STy = dyn_cast(Ty)) { + } else if (const StructType *STy = dyn_cast(Ty)) { Out << "{ "; for (StructType::ElementTypes::const_iterator I = STy->getElementTypes().begin(), @@ -476,13 +499,16 @@ ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) { printType(*I); } Out << " }"; - } else if (PointerType *PTy = dyn_cast(Ty)) { + } else if (const PointerType *PTy = dyn_cast(Ty)) { printType(PTy->getElementType()) << "*"; - } else if (ArrayType *ATy = dyn_cast(Ty)) { + } else if (const ArrayType *ATy = dyn_cast(Ty)) { Out << "[" << ATy->getNumElements() << " x "; printType(ATy->getElementType()) << "]"; + } else if (const OpaqueType *OTy = dyn_cast(Ty)) { + Out << OTy->getDescription(); } else { - assert(Ty->isPrimitiveType() && "Unknown derived type!"); + if (!Ty->isPrimitiveType()) + Out << ""; printType(Ty); } return Out; @@ -501,13 +527,14 @@ void AssemblyWriter::printModule(const Module *M) { if (M->hasSymbolTable()) printSymbolTable(*M->getSymbolTable()); - for_each(M->gbegin(), M->gend(), - bind_obj(this, &AssemblyWriter::printGlobal)); + for (Module::const_giterator I = M->gbegin(), E = M->gend(); I != E; ++I) + printGlobal(I); - Out << "implementation\n"; + Out << "\nimplementation ; Functions:\n"; // Output all of the functions... - for_each(M->begin(), M->end(), bind_obj(this,&AssemblyWriter::printFunction)); + for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) + printFunction(I); } void AssemblyWriter::printGlobal(const GlobalVariable *GV) { @@ -522,7 +549,7 @@ void AssemblyWriter::printGlobal(const GlobalVariable *GV) { if (GV->hasInitializer()) writeOperand(GV->getInitializer(), false, false); - printInfoComment(GV); + printInfoComment(*GV); Out << "\n"; } @@ -564,54 +591,51 @@ void AssemblyWriter::printConstant(const Constant *CPV) { // Write the value out now... writeOperand(CPV, true, false); - printInfoComment(CPV); + printInfoComment(*CPV); Out << "\n"; } // printFunction - Print all aspects of a function. // -void AssemblyWriter::printFunction(const Function *M) { +void AssemblyWriter::printFunction(const Function *F) { // Print out the return type and name... - Out << "\n" << (M->isExternal() ? "declare " : "") - << (M->hasInternalLinkage() ? "internal " : ""); - printType(M->getReturnType()) << " \"" << M->getName() << "\"("; - Table.incorporateFunction(M); + Out << "\n" << (F->isExternal() ? "declare " : "") + << (F->hasInternalLinkage() ? "internal " : ""); + printType(F->getReturnType()) << " %" << F->getName() << "("; + Table.incorporateFunction(F); // Loop over the arguments, printing them... - const FunctionType *MT = M->getFunctionType(); + const FunctionType *FT = F->getFunctionType(); - if (!M->isExternal()) { - for_each(M->getArgumentList().begin(), M->getArgumentList().end(), - bind_obj(this, &AssemblyWriter::printArgument)); + if (!F->isExternal()) { + for(Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I) + printArgument(I); } else { // Loop over the arguments, printing them... - const FunctionType *MT = M->getFunctionType(); - for (FunctionType::ParamTypes::const_iterator I = MT->getParamTypes().begin(), - E = MT->getParamTypes().end(); I != E; ++I) { - if (I != MT->getParamTypes().begin()) Out << ", "; + for (FunctionType::ParamTypes::const_iterator I = FT->getParamTypes().begin(), + E = FT->getParamTypes().end(); I != E; ++I) { + if (I != FT->getParamTypes().begin()) Out << ", "; printType(*I); } } // Finish printing arguments... - if (MT->isVarArg()) { - if (MT->getParamTypes().size()) Out << ", "; + if (FT->isVarArg()) { + if (FT->getParamTypes().size()) Out << ", "; Out << "..."; // Output varargs portion of signature! } - Out << ")\n"; - - if (!M->isExternal()) { - // Loop over the symbol table, emitting all named constants... - if (M->hasSymbolTable()) - printSymbolTable(*M->getSymbolTable()); + Out << ")"; - Out << "begin"; + if (F->isExternal()) { + Out << "\n"; + } else { + Out << " {"; // Output all of its basic blocks... for the function - for_each(M->begin(), M->end(), - bind_obj(this, &AssemblyWriter::printBasicBlock)); + for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I) + printBasicBlock(I); - Out << "end\n"; + Out << "}\n"; } Table.purgeFunction(); @@ -622,7 +646,7 @@ void AssemblyWriter::printFunction(const Function *M) { // void AssemblyWriter::printArgument(const Argument *Arg) { // Insert commas as we go... the first arg doesn't get a comma - if (Arg != Arg->getParent()->getArgumentList().front()) Out << ", "; + if (Arg != &Arg->getParent()->afront()) Out << ", "; // Output type... printType(Arg->getType()); @@ -639,7 +663,7 @@ void AssemblyWriter::printArgument(const Argument *Arg) { void AssemblyWriter::printBasicBlock(const BasicBlock *BB) { if (BB->hasName()) { // Print out the label if it exists... Out << "\n" << BB->getName() << ":"; - } else { + } else if (!BB->use_empty()) { // Don't print block # of no uses... int Slot = Table.getValSlot(BB); Out << "\n;