From 2f499023741bb7a507380569d82281356f759a45 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Thu, 9 May 2002 04:21:21 +0000 Subject: [PATCH] Simplify and clean up function calls. Now indirect function calls work, except that function pointer types are not correct. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@2573 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Target/CBackend/CBackend.cpp | 86 ++++++++++++++------------------ lib/Target/CBackend/Writer.cpp | 86 ++++++++++++++------------------ 2 files changed, 74 insertions(+), 98 deletions(-) diff --git a/lib/Target/CBackend/CBackend.cpp b/lib/Target/CBackend/CBackend.cpp index 630abecbd0c..f4d83f7c2d2 100644 --- a/lib/Target/CBackend/CBackend.cpp +++ b/lib/Target/CBackend/CBackend.cpp @@ -55,23 +55,6 @@ static string calcTypeNameVar(const Type *Ty, static std::string getConstStrValue(const Constant* CPV); -// We dont want identifier names with ., space, - in them. -// So we replace them with _ -static string makeNameProper(string x) { - string tmp; - for (string::iterator sI = x.begin(), sEnd = x.end(); sI != sEnd; sI++) { - if (*sI == '.') - tmp += '_'; - else if (*sI == ' ') - tmp += '_'; - else if (*sI == '-') - tmp += "__"; - else - tmp += *sI; - } - return tmp; -} - static std::string getConstArrayStrValue(const Constant* CPV) { std::string Result; @@ -614,11 +597,10 @@ void CInstPrintVisitor::printPhiFromNextBlock(TerminatorInst *tI, int indx) { // Implement all "other" instructions, except for PHINode void CInstPrintVisitor::visitCastInst(CastInst *I) { outputLValue(I); - Operand = I->getNumOperands() ? I->getOperand(0) : 0; Out << "("; CW.printType(I->getType(), Out); Out << ")"; - CW.writeOperand(Operand, Out); + CW.writeOperand(I->getOperand(0), Out); Out << ";\n"; } @@ -628,33 +610,21 @@ void CInstPrintVisitor::visitCallInst(CallInst *I) { else Out << " "; - Operand = I->getNumOperands() ? I->getOperand(0) : 0; - const PointerType *PTy = dyn_cast(Operand->getType()); - const FunctionType *MTy = PTy - ? dyn_cast(PTy->getElementType()):0; - const Type *RetTy = MTy ? MTy->getReturnType() : 0; + const PointerType *PTy = cast(I->getCalledValue()->getType()); + const FunctionType *FTy = cast(PTy->getElementType()); + const Type *RetTy = FTy->getReturnType(); - // 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(RetTy)|| - !isa(cast(RetTy)))){ - Out << " "; - Out << makeNameProper(Operand->getName()); - } else { - Out << makeNameProper(Operand->getName()); - } - Out << "("; - if (I->getNumOperands() > 1) + Out << CW.getValueName(I->getOperand(0)) << "("; + + if (I->getNumOperands() != 0) { CW.writeOperand(I->getOperand(1), Out); - for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; ++op) { - Out << ","; - CW.writeOperand(I->getOperand(op), Out); + + for (unsigned op = 2, Eop = I->getNumOperands(); op != Eop; ++op) { + Out << ", "; + CW.writeOperand(I->getOperand(op), Out); + } } - - Out << " );\n"; + Out << ");\n"; } // Specific Instruction type classes... note that all of the casts are @@ -819,10 +789,30 @@ void CInstPrintVisitor::visitBinaryOperator(Instruction *I) { /* END : CInstPrintVisitor implementation */ +// We dont want identifier names with ., space, - in them. +// So we replace them with _ +static string makeNameProper(string x) { + string tmp; + for (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; + case '_': tmp += "__"; break; + default: tmp += *sI; + } + + return tmp; +} + string CWriter::getValueName(const Value *V) { - if (V->hasName()) // Print out the label if it exists... + if (V->hasName()) { // Print out the label if it exists... + if (isa(V)) // Do not mangle globals... + return makeNameProper(V->getName()); + return "l_" + makeNameProper(V->getName()) + "_" + - utostr(V->getType()->getUniqueID()); + utostr(V->getType()->getUniqueID()); + } int Slot = Table.getValSlot(V); assert(Slot >= 0 && "Invalid value!"); @@ -952,7 +942,7 @@ void CWriter::printFunctionSignature(const Function *F) { // Print out the return type and name... printType(F->getReturnType(), Out); - Out << " " << makeNameProper(F->getName()) << "("; + Out << " " << getValueName(F) << "("; if (!F->isExternal()) { for_each(F->getArgumentList().begin(), F->getArgumentList().end(), @@ -1050,9 +1040,7 @@ void CWriter::writeOperand(const Value *Operand, Out << "(&"; // Global values are references as their addresses by llvm if (PrintName && Operand->hasName()) { - // If Operand has a name. - Out << "l_" << makeNameProper(Operand->getName()) << "_" << - Operand->getType()->getUniqueID(); + Out << getValueName(Operand); } else if (const Constant *CPV = dyn_cast(Operand)) { if (isa(CPV)) Out << "NULL"; diff --git a/lib/Target/CBackend/Writer.cpp b/lib/Target/CBackend/Writer.cpp index 630abecbd0c..f4d83f7c2d2 100644 --- a/lib/Target/CBackend/Writer.cpp +++ b/lib/Target/CBackend/Writer.cpp @@ -55,23 +55,6 @@ static string calcTypeNameVar(const Type *Ty, static std::string getConstStrValue(const Constant* CPV); -// We dont want identifier names with ., space, - in them. -// So we replace them with _ -static string makeNameProper(string x) { - string tmp; - for (string::iterator sI = x.begin(), sEnd = x.end(); sI != sEnd; sI++) { - if (*sI == '.') - tmp += '_'; - else if (*sI == ' ') - tmp += '_'; - else if (*sI == '-') - tmp += "__"; - else - tmp += *sI; - } - return tmp; -} - static std::string getConstArrayStrValue(const Constant* CPV) { std::string Result; @@ -614,11 +597,10 @@ void CInstPrintVisitor::printPhiFromNextBlock(TerminatorInst *tI, int indx) { // Implement all "other" instructions, except for PHINode void CInstPrintVisitor::visitCastInst(CastInst *I) { outputLValue(I); - Operand = I->getNumOperands() ? I->getOperand(0) : 0; Out << "("; CW.printType(I->getType(), Out); Out << ")"; - CW.writeOperand(Operand, Out); + CW.writeOperand(I->getOperand(0), Out); Out << ";\n"; } @@ -628,33 +610,21 @@ void CInstPrintVisitor::visitCallInst(CallInst *I) { else Out << " "; - Operand = I->getNumOperands() ? I->getOperand(0) : 0; - const PointerType *PTy = dyn_cast(Operand->getType()); - const FunctionType *MTy = PTy - ? dyn_cast(PTy->getElementType()):0; - const Type *RetTy = MTy ? MTy->getReturnType() : 0; + const PointerType *PTy = cast(I->getCalledValue()->getType()); + const FunctionType *FTy = cast(PTy->getElementType()); + const Type *RetTy = FTy->getReturnType(); - // 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(RetTy)|| - !isa(cast(RetTy)))){ - Out << " "; - Out << makeNameProper(Operand->getName()); - } else { - Out << makeNameProper(Operand->getName()); - } - Out << "("; - if (I->getNumOperands() > 1) + Out << CW.getValueName(I->getOperand(0)) << "("; + + if (I->getNumOperands() != 0) { CW.writeOperand(I->getOperand(1), Out); - for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; ++op) { - Out << ","; - CW.writeOperand(I->getOperand(op), Out); + + for (unsigned op = 2, Eop = I->getNumOperands(); op != Eop; ++op) { + Out << ", "; + CW.writeOperand(I->getOperand(op), Out); + } } - - Out << " );\n"; + Out << ");\n"; } // Specific Instruction type classes... note that all of the casts are @@ -819,10 +789,30 @@ void CInstPrintVisitor::visitBinaryOperator(Instruction *I) { /* END : CInstPrintVisitor implementation */ +// We dont want identifier names with ., space, - in them. +// So we replace them with _ +static string makeNameProper(string x) { + string tmp; + for (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; + case '_': tmp += "__"; break; + default: tmp += *sI; + } + + return tmp; +} + string CWriter::getValueName(const Value *V) { - if (V->hasName()) // Print out the label if it exists... + if (V->hasName()) { // Print out the label if it exists... + if (isa(V)) // Do not mangle globals... + return makeNameProper(V->getName()); + return "l_" + makeNameProper(V->getName()) + "_" + - utostr(V->getType()->getUniqueID()); + utostr(V->getType()->getUniqueID()); + } int Slot = Table.getValSlot(V); assert(Slot >= 0 && "Invalid value!"); @@ -952,7 +942,7 @@ void CWriter::printFunctionSignature(const Function *F) { // Print out the return type and name... printType(F->getReturnType(), Out); - Out << " " << makeNameProper(F->getName()) << "("; + Out << " " << getValueName(F) << "("; if (!F->isExternal()) { for_each(F->getArgumentList().begin(), F->getArgumentList().end(), @@ -1050,9 +1040,7 @@ void CWriter::writeOperand(const Value *Operand, Out << "(&"; // Global values are references as their addresses by llvm if (PrintName && Operand->hasName()) { - // If Operand has a name. - Out << "l_" << makeNameProper(Operand->getName()) << "_" << - Operand->getType()->getUniqueID(); + Out << getValueName(Operand); } else if (const Constant *CPV = dyn_cast(Operand)) { if (isa(CPV)) Out << "NULL"; -- 2.34.1