From a3840795a58b3fe3c7d986bfa20951e8d5e1d6e2 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Mon, 16 Aug 2004 23:25:21 +0000 Subject: [PATCH] Finegrainify namespacification Start using the AsmPrinter base class to factor out a bunch of code git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@15840 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Target/PowerPC/PPC32AsmPrinter.cpp | 124 +++---------------------- lib/Target/PowerPC/PPCAsmPrinter.cpp | 124 +++---------------------- 2 files changed, 30 insertions(+), 218 deletions(-) diff --git a/lib/Target/PowerPC/PPC32AsmPrinter.cpp b/lib/Target/PowerPC/PPC32AsmPrinter.cpp index a20525c814a..b68d1849a76 100644 --- a/lib/Target/PowerPC/PPC32AsmPrinter.cpp +++ b/lib/Target/PowerPC/PPC32AsmPrinter.cpp @@ -24,6 +24,7 @@ #include "llvm/DerivedTypes.h" #include "llvm/Module.h" #include "llvm/Assembly/Writer.h" +#include "llvm/CodeGen/AsmPrinter.h" #include "llvm/CodeGen/MachineConstantPool.h" #include "llvm/CodeGen/MachineFunctionPass.h" #include "llvm/CodeGen/MachineInstr.h" @@ -35,36 +36,17 @@ #include "Support/Statistic.h" #include "Support/StringExtras.h" #include - -namespace llvm { +using namespace llvm; namespace { Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed"); - struct PowerPCAsmPrinter : public MachineFunctionPass { - /// Output stream on which we're printing assembly code. - /// - std::ostream &O; - - /// Target machine description which we query for reg. names, data - /// layout, etc. - /// - PowerPCTargetMachine &TM; - - /// Name-mangler for global names. - /// - Mangler *Mang; + struct PowerPCAsmPrinter : public AsmPrinter { std::set FnStubs, GVStubs, LinkOnceStubs; std::set Strings; - PowerPCAsmPrinter(std::ostream &o, TargetMachine &tm) : O(o), - TM(reinterpret_cast(tm)), LabelNumber(0) {} - - /// Cache of mangled name for current function. This is - /// recalculated at the beginning of each call to - /// runOnMachineFunction(). - /// - std::string CurrentFnName; + PowerPCAsmPrinter(std::ostream &O, TargetMachine &TM) + : AsmPrinter(O, TM), LabelNumber(0) {} /// Unique incrementer for label values for referencing Global values. /// @@ -74,6 +56,10 @@ namespace { return "PowerPC Assembly Printer"; } + PowerPCTargetMachine &getTM() { + return static_cast(TM); + } + /// printInstruction - This method is automatically generated by tablegen /// from the instruction set description. This method returns true if the /// machine instruction was sufficiently described to print it, otherwise it @@ -103,10 +89,8 @@ namespace { void printConstantPool(MachineConstantPool *MCP); bool runOnMachineFunction(MachineFunction &F); - bool doInitialization(Module &M); bool doFinalization(Module &M); void emitGlobalConstant(const Constant* CV); - void emitConstantValueOnly(const Constant *CV); }; } // end of anonymous namespace @@ -115,7 +99,7 @@ namespace { /// using the given target machine description. This should work /// regardless of whether the function is in SSA form or not. /// -FunctionPass *createPPCAsmPrinter(std::ostream &o,TargetMachine &tm) { +FunctionPass *llvm::createPPCAsmPrinter(std::ostream &o,TargetMachine &tm) { return new PowerPCAsmPrinter(o, tm); } @@ -178,76 +162,6 @@ static void printAsCString(std::ostream &O, const ConstantArray *CVA) { O << "\""; } -// Print out the specified constant, without a storage class. Only the -// constants valid in constant expressions can occur here. -void PowerPCAsmPrinter::emitConstantValueOnly(const Constant *CV) { - if (CV->isNullValue()) - O << "0"; - else if (const ConstantBool *CB = dyn_cast(CV)) { - assert(CB == ConstantBool::True); - O << "1"; - } else if (const ConstantSInt *CI = dyn_cast(CV)) - O << CI->getValue(); - else if (const ConstantUInt *CI = dyn_cast(CV)) - O << CI->getValue(); - else if (const GlobalValue *GV = dyn_cast(CV)) - // This is a constant address for a global variable or function. Use the - // name of the variable or function as the address value. - O << Mang->getValueName(GV); - else if (const ConstantExpr *CE = dyn_cast(CV)) { - const TargetData &TD = TM.getTargetData(); - switch (CE->getOpcode()) { - case Instruction::GetElementPtr: { - // generate a symbolic expression for the byte address - const Constant *ptrVal = CE->getOperand(0); - std::vector idxVec(CE->op_begin()+1, CE->op_end()); - if (unsigned Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) { - O << "("; - emitConstantValueOnly(ptrVal); - O << ") + " << Offset; - } else { - emitConstantValueOnly(ptrVal); - } - break; - } - case Instruction::Cast: { - // Support only non-converting or widening casts for now, that is, ones - // that do not involve a change in value. This assertion is really gross, - // and may not even be a complete check. - Constant *Op = CE->getOperand(0); - const Type *OpTy = Op->getType(), *Ty = CE->getType(); - - // Remember, kids, pointers on x86 can be losslessly converted back and - // forth into 32-bit or wider integers, regardless of signedness. :-P - assert(((isa(OpTy) - && (Ty == Type::LongTy || Ty == Type::ULongTy - || Ty == Type::IntTy || Ty == Type::UIntTy)) - || (isa(Ty) - && (OpTy == Type::LongTy || OpTy == Type::ULongTy - || OpTy == Type::IntTy || OpTy == Type::UIntTy)) - || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy)) - && OpTy->isLosslesslyConvertibleTo(Ty)))) - && "FIXME: Don't yet support this kind of constant cast expr"); - O << "("; - emitConstantValueOnly(Op); - O << ")"; - break; - } - case Instruction::Add: - O << "("; - emitConstantValueOnly(CE->getOperand(0)); - O << ") + ("; - emitConstantValueOnly(CE->getOperand(1)); - O << ")"; - break; - default: - assert(0 && "Unsupported operator!"); - } - } else { - assert(0 && "Unknown constant value!"); - } -} - // Print a constant value or values, with the appropriate storage class as a // prefix. void PowerPCAsmPrinter::emitGlobalConstant(const Constant *CV) { @@ -382,9 +296,8 @@ void PowerPCAsmPrinter::printConstantPool(MachineConstantPool *MCP) { /// method to print assembly for each instruction. /// bool PowerPCAsmPrinter::runOnMachineFunction(MachineFunction &MF) { + setupMachineFunction(MF); O << "\n\n"; - // What's my mangled name? - CurrentFnName = Mang->getValueName(MF.getFunction()); // Print out constants referenced by the function printConstantPool(MF.getConstantPool()); @@ -467,20 +380,20 @@ void PowerPCAsmPrinter::printOp(const MachineOperand &MO, // are taken. Those should be emitted as $non_lazy_ptr below. Function *F = dyn_cast(GV); if (F && F->isExternal() && !LoadAddrOp && - TM.CalledFunctions.find(F) != TM.CalledFunctions.end()) { + getTM().CalledFunctions.count(F)) { FnStubs.insert(Name); O << "L" << Name << "$stub"; return; } // External global variables need a non-lazily-resolved stub - if (GV->isExternal() && TM.AddressTaken.find(GV) != TM.AddressTaken.end()) { + if (GV->isExternal() && getTM().AddressTaken.count(GV)) { GVStubs.insert(Name); O << "L" << Name << "$non_lazy_ptr"; return; } - if (F && LoadAddrOp && TM.AddressTaken.find(GV) != TM.AddressTaken.end()) { + if (F && LoadAddrOp && getTM().AddressTaken.count(GV)) { LinkOnceStubs.insert(Name); O << "L" << Name << "$non_lazy_ptr"; return; @@ -626,11 +539,6 @@ void PowerPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) { return; } -bool PowerPCAsmPrinter::doInitialization(Module &M) { - Mang = new Mangler(M, true); - return false; // success -} - // SwitchSection - Switch to the specified section of the executable if we are // not already in it! // @@ -750,8 +658,6 @@ bool PowerPCAsmPrinter::doFinalization(Module &M) { << "\t.long\t" << *i << '\n'; } - delete Mang; + AsmPrinter::doFinalization(M); return false; // success } - -} // End llvm namespace diff --git a/lib/Target/PowerPC/PPCAsmPrinter.cpp b/lib/Target/PowerPC/PPCAsmPrinter.cpp index a20525c814a..b68d1849a76 100644 --- a/lib/Target/PowerPC/PPCAsmPrinter.cpp +++ b/lib/Target/PowerPC/PPCAsmPrinter.cpp @@ -24,6 +24,7 @@ #include "llvm/DerivedTypes.h" #include "llvm/Module.h" #include "llvm/Assembly/Writer.h" +#include "llvm/CodeGen/AsmPrinter.h" #include "llvm/CodeGen/MachineConstantPool.h" #include "llvm/CodeGen/MachineFunctionPass.h" #include "llvm/CodeGen/MachineInstr.h" @@ -35,36 +36,17 @@ #include "Support/Statistic.h" #include "Support/StringExtras.h" #include - -namespace llvm { +using namespace llvm; namespace { Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed"); - struct PowerPCAsmPrinter : public MachineFunctionPass { - /// Output stream on which we're printing assembly code. - /// - std::ostream &O; - - /// Target machine description which we query for reg. names, data - /// layout, etc. - /// - PowerPCTargetMachine &TM; - - /// Name-mangler for global names. - /// - Mangler *Mang; + struct PowerPCAsmPrinter : public AsmPrinter { std::set FnStubs, GVStubs, LinkOnceStubs; std::set Strings; - PowerPCAsmPrinter(std::ostream &o, TargetMachine &tm) : O(o), - TM(reinterpret_cast(tm)), LabelNumber(0) {} - - /// Cache of mangled name for current function. This is - /// recalculated at the beginning of each call to - /// runOnMachineFunction(). - /// - std::string CurrentFnName; + PowerPCAsmPrinter(std::ostream &O, TargetMachine &TM) + : AsmPrinter(O, TM), LabelNumber(0) {} /// Unique incrementer for label values for referencing Global values. /// @@ -74,6 +56,10 @@ namespace { return "PowerPC Assembly Printer"; } + PowerPCTargetMachine &getTM() { + return static_cast(TM); + } + /// printInstruction - This method is automatically generated by tablegen /// from the instruction set description. This method returns true if the /// machine instruction was sufficiently described to print it, otherwise it @@ -103,10 +89,8 @@ namespace { void printConstantPool(MachineConstantPool *MCP); bool runOnMachineFunction(MachineFunction &F); - bool doInitialization(Module &M); bool doFinalization(Module &M); void emitGlobalConstant(const Constant* CV); - void emitConstantValueOnly(const Constant *CV); }; } // end of anonymous namespace @@ -115,7 +99,7 @@ namespace { /// using the given target machine description. This should work /// regardless of whether the function is in SSA form or not. /// -FunctionPass *createPPCAsmPrinter(std::ostream &o,TargetMachine &tm) { +FunctionPass *llvm::createPPCAsmPrinter(std::ostream &o,TargetMachine &tm) { return new PowerPCAsmPrinter(o, tm); } @@ -178,76 +162,6 @@ static void printAsCString(std::ostream &O, const ConstantArray *CVA) { O << "\""; } -// Print out the specified constant, without a storage class. Only the -// constants valid in constant expressions can occur here. -void PowerPCAsmPrinter::emitConstantValueOnly(const Constant *CV) { - if (CV->isNullValue()) - O << "0"; - else if (const ConstantBool *CB = dyn_cast(CV)) { - assert(CB == ConstantBool::True); - O << "1"; - } else if (const ConstantSInt *CI = dyn_cast(CV)) - O << CI->getValue(); - else if (const ConstantUInt *CI = dyn_cast(CV)) - O << CI->getValue(); - else if (const GlobalValue *GV = dyn_cast(CV)) - // This is a constant address for a global variable or function. Use the - // name of the variable or function as the address value. - O << Mang->getValueName(GV); - else if (const ConstantExpr *CE = dyn_cast(CV)) { - const TargetData &TD = TM.getTargetData(); - switch (CE->getOpcode()) { - case Instruction::GetElementPtr: { - // generate a symbolic expression for the byte address - const Constant *ptrVal = CE->getOperand(0); - std::vector idxVec(CE->op_begin()+1, CE->op_end()); - if (unsigned Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) { - O << "("; - emitConstantValueOnly(ptrVal); - O << ") + " << Offset; - } else { - emitConstantValueOnly(ptrVal); - } - break; - } - case Instruction::Cast: { - // Support only non-converting or widening casts for now, that is, ones - // that do not involve a change in value. This assertion is really gross, - // and may not even be a complete check. - Constant *Op = CE->getOperand(0); - const Type *OpTy = Op->getType(), *Ty = CE->getType(); - - // Remember, kids, pointers on x86 can be losslessly converted back and - // forth into 32-bit or wider integers, regardless of signedness. :-P - assert(((isa(OpTy) - && (Ty == Type::LongTy || Ty == Type::ULongTy - || Ty == Type::IntTy || Ty == Type::UIntTy)) - || (isa(Ty) - && (OpTy == Type::LongTy || OpTy == Type::ULongTy - || OpTy == Type::IntTy || OpTy == Type::UIntTy)) - || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy)) - && OpTy->isLosslesslyConvertibleTo(Ty)))) - && "FIXME: Don't yet support this kind of constant cast expr"); - O << "("; - emitConstantValueOnly(Op); - O << ")"; - break; - } - case Instruction::Add: - O << "("; - emitConstantValueOnly(CE->getOperand(0)); - O << ") + ("; - emitConstantValueOnly(CE->getOperand(1)); - O << ")"; - break; - default: - assert(0 && "Unsupported operator!"); - } - } else { - assert(0 && "Unknown constant value!"); - } -} - // Print a constant value or values, with the appropriate storage class as a // prefix. void PowerPCAsmPrinter::emitGlobalConstant(const Constant *CV) { @@ -382,9 +296,8 @@ void PowerPCAsmPrinter::printConstantPool(MachineConstantPool *MCP) { /// method to print assembly for each instruction. /// bool PowerPCAsmPrinter::runOnMachineFunction(MachineFunction &MF) { + setupMachineFunction(MF); O << "\n\n"; - // What's my mangled name? - CurrentFnName = Mang->getValueName(MF.getFunction()); // Print out constants referenced by the function printConstantPool(MF.getConstantPool()); @@ -467,20 +380,20 @@ void PowerPCAsmPrinter::printOp(const MachineOperand &MO, // are taken. Those should be emitted as $non_lazy_ptr below. Function *F = dyn_cast(GV); if (F && F->isExternal() && !LoadAddrOp && - TM.CalledFunctions.find(F) != TM.CalledFunctions.end()) { + getTM().CalledFunctions.count(F)) { FnStubs.insert(Name); O << "L" << Name << "$stub"; return; } // External global variables need a non-lazily-resolved stub - if (GV->isExternal() && TM.AddressTaken.find(GV) != TM.AddressTaken.end()) { + if (GV->isExternal() && getTM().AddressTaken.count(GV)) { GVStubs.insert(Name); O << "L" << Name << "$non_lazy_ptr"; return; } - if (F && LoadAddrOp && TM.AddressTaken.find(GV) != TM.AddressTaken.end()) { + if (F && LoadAddrOp && getTM().AddressTaken.count(GV)) { LinkOnceStubs.insert(Name); O << "L" << Name << "$non_lazy_ptr"; return; @@ -626,11 +539,6 @@ void PowerPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) { return; } -bool PowerPCAsmPrinter::doInitialization(Module &M) { - Mang = new Mangler(M, true); - return false; // success -} - // SwitchSection - Switch to the specified section of the executable if we are // not already in it! // @@ -750,8 +658,6 @@ bool PowerPCAsmPrinter::doFinalization(Module &M) { << "\t.long\t" << *i << '\n'; } - delete Mang; + AsmPrinter::doFinalization(M); return false; // success } - -} // End llvm namespace