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
This commit is contained in:
Chris Lattner 2004-08-16 23:25:21 +00:00
parent 055acae18c
commit a3840795a5
2 changed files with 30 additions and 218 deletions

View File

@ -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 <set>
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<std::string> FnStubs, GVStubs, LinkOnceStubs;
std::set<std::string> Strings;
PowerPCAsmPrinter(std::ostream &o, TargetMachine &tm) : O(o),
TM(reinterpret_cast<PowerPCTargetMachine&>(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<PowerPCTargetMachine&>(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<ConstantBool>(CV)) {
assert(CB == ConstantBool::True);
O << "1";
} else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
O << CI->getValue();
else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
O << CI->getValue();
else if (const GlobalValue *GV = dyn_cast<GlobalValue>(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<ConstantExpr>(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<Value*> 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<PointerType>(OpTy)
&& (Ty == Type::LongTy || Ty == Type::ULongTy
|| Ty == Type::IntTy || Ty == Type::UIntTy))
|| (isa<PointerType>(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<Function>(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

View File

@ -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 <set>
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<std::string> FnStubs, GVStubs, LinkOnceStubs;
std::set<std::string> Strings;
PowerPCAsmPrinter(std::ostream &o, TargetMachine &tm) : O(o),
TM(reinterpret_cast<PowerPCTargetMachine&>(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<PowerPCTargetMachine&>(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<ConstantBool>(CV)) {
assert(CB == ConstantBool::True);
O << "1";
} else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
O << CI->getValue();
else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
O << CI->getValue();
else if (const GlobalValue *GV = dyn_cast<GlobalValue>(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<ConstantExpr>(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<Value*> 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<PointerType>(OpTy)
&& (Ty == Type::LongTy || Ty == Type::ULongTy
|| Ty == Type::IntTy || Ty == Type::UIntTy))
|| (isa<PointerType>(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<Function>(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