From 21447223892055631e6a5735a3c687337ff8a73b Mon Sep 17 00:00:00 2001 From: "Vikram S. Adve" Date: Sat, 10 Nov 2001 02:03:06 +0000 Subject: [PATCH] Several bug fixes in printing constants, particularly with unsized arrays and multi-dimensional arrays. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@1239 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Target/SparcV9/SparcV9AsmPrinter.cpp | 138 +++++++++++++++-------- 1 file changed, 92 insertions(+), 46 deletions(-) diff --git a/lib/Target/SparcV9/SparcV9AsmPrinter.cpp b/lib/Target/SparcV9/SparcV9AsmPrinter.cpp index dd653057034..ed8059d12e9 100644 --- a/lib/Target/SparcV9/SparcV9AsmPrinter.cpp +++ b/lib/Target/SparcV9/SparcV9AsmPrinter.cpp @@ -58,9 +58,10 @@ private : void emitBasicBlock(const BasicBlock *BB); void emitMachineInst(const MachineInstr *MI); - void printGlobalVariable(const GlobalVariable* GV); - void printSingleConstant(const ConstPoolVal* CV, string valID = string("")); - void printConstant( const ConstPoolVal* CV, string valID = string("")); + void printGlobalVariable( const GlobalVariable* GV); + void printSingleConstant( const ConstPoolVal* CV); + void printConstantValueOnly(const ConstPoolVal* CV); + void printConstant( const ConstPoolVal* CV, string valID=string("")); unsigned int printOperands(const MachineInstr *MI, unsigned int opNum); void printOneOperand(const MachineOperand &Op); @@ -376,17 +377,24 @@ TypeToDataDirective(const Type* type) } } +// Get the size of the constant for the given target. +// If this is an unsized array, return 0. +// inline unsigned int ConstantToSize(const ConstPoolVal* CV, const TargetMachine& target) { - if (ConstPoolArray* AV = dyn_cast(CV)) - if (ArrayTypeIsString((ArrayType*) CV->getType())) - return 1 + AV->getNumOperands(); + if (ConstPoolArray* CPA = dyn_cast(CV)) + { + ArrayType *aty = cast(CPA->getType()); + if (ArrayTypeIsString(aty)) + return 1 + CPA->getNumOperands(); + else if (! aty->isSized()) + return 0; + } return target.findOptimalStorageSize(CV->getType()); } - inline unsigned int TypeToSize(const Type* type, const TargetMachine& target) { @@ -395,28 +403,52 @@ unsigned int TypeToSize(const Type* type, const TargetMachine& target) // Align data larger than one L1 cache line on L1 cache line boundaries. -// Align all smaller types on the next higher 2^x boundary (4, 8, ...). +// Align all smaller data on the next higher 2^x boundary (4, 8, ...). +// +inline unsigned int +SizeToAlignment(unsigned int size, const TargetMachine& target) +{ + unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1); + if (size > (unsigned) cacheLineSize / 2) + return cacheLineSize; + else + for (unsigned sz=1; /*no condition*/; sz *= 2) + if (sz >= size) + return sz; +} + +// Get the size of the type and then use SizeToAlignment. +// If this is an unsized array, just return the L1 cache line size +// (viz., the default behavior for large global objects). // inline unsigned int TypeToAlignment(const Type* type, const TargetMachine& target) { - unsigned int typeSize = target.findOptimalStorageSize(type); - unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1); - if (typeSize > (int) cacheLineSize / 2) - return cacheLineSize; - else - for (unsigned sz=1; /*no condition*/; sz *= 2) - if (sz >= typeSize) - return sz; + if (ArrayType* aty = dyn_cast(type)) + if (! aty->isSized()) + return target.getCacheInfo().getCacheLineSize(1); + + return SizeToAlignment(target.findOptimalStorageSize(type), target); +} + +// Get the size of the constant and then use SizeToAlignment. +// Handles strings as a special case; +inline unsigned int +ConstantToAlignment(const ConstPoolVal* CV, const TargetMachine& target) +{ + unsigned int constantSize; + if (ConstPoolArray* CPA = dyn_cast(CV)) + if (ArrayTypeIsString(cast(CPA->getType()))) + return SizeToAlignment(1 + CPA->getNumOperands(), target); + + return TypeToAlignment(CV->getType(), target); } +// Print a single constant value. void -SparcAsmPrinter::printSingleConstant(const ConstPoolVal* CV,string valID) +SparcAsmPrinter::printSingleConstant(const ConstPoolVal* CV) { - if (valID.length() == 0) - valID = getID(CV); - assert(CV->getType() != Type::VoidTy && CV->getType() != Type::TypeTy && CV->getType() != Type::LabelTy && @@ -451,50 +483,64 @@ SparcAsmPrinter::printSingleConstant(const ConstPoolVal* CV,string valID) } } +// Print a constant value or values (it may be an aggregate). +// Uses printSingleConstant() to print each individual value. +void +SparcAsmPrinter::printConstantValueOnly(const ConstPoolVal* CV) +{ + ConstPoolArray *CPA = dyn_cast(CV); + + if (CPA && isStringCompatible(CPA)) + { // print the string alone and return + toAsm << "\t" << ".ascii" << "\t" << getAsCString(CPA) << endl; + } + else if (CPA) + { // Not a string. Print the values in successive locations + const vector& constValues = CPA->getValues(); + for (unsigned i=1; i < constValues.size(); i++) + this->printConstantValueOnly(cast(constValues[i].get())); + } + else if (ConstPoolStruct *CPS = dyn_cast(CV)) + { // Print the fields in successive locations + const vector& constValues = CPS->getValues(); + for (unsigned i=1; i < constValues.size(); i++) + this->printConstantValueOnly(cast(constValues[i].get())); + } + else + this->printSingleConstant(CV); +} + +// Print a constant (which may be an aggregate) prefixed by all the +// appropriate directives. Uses printConstantValueOnly() to print the +// value or values. void SparcAsmPrinter::printConstant(const ConstPoolVal* CV, string valID) { if (valID.length() == 0) valID = getID(CV); - assert(CV->getType() != Type::VoidTy && - CV->getType() != Type::TypeTy && - CV->getType() != Type::LabelTy && - "Unexpected type for ConstPoolVal"); - - toAsm << "\t.align\t" << TypeToAlignment(CV->getType(), Target) + toAsm << "\t.align\t" << ConstantToAlignment(CV, Target) << endl; // Print .size and .type only if it is not a string. ConstPoolArray *CPA = dyn_cast(CV); - if (CPA && isStringCompatible(CPA)) { // print it as a string and return toAsm << valID << ":" << endl; - toAsm << "\t" << TypeToDataDirective(CV->getType()) << "\t" - << getAsCString(CPA) << endl; + toAsm << "\t" << ".ascii" << "\t" << getAsCString(CPA) << endl; return; } - + toAsm << "\t.type" << "\t" << valID << ",#object" << endl; - toAsm << "\t.size" << "\t" << valID << "," - << ConstantToSize(CV, Target) << endl; + + unsigned int constSize = ConstantToSize(CV, Target); + if (constSize) + toAsm << "\t.size" << "\t" << valID << "," + << constSize << endl; + toAsm << valID << ":" << endl; - if (CPA) - { // Not a string. Print the values in successive locations - const vector& constValues = CPA->getValues(); - for (unsigned i=1; i < constValues.size(); i++) - this->printSingleConstant(cast(constValues[i].get())); - } - else if (ConstPoolStruct *CPS = dyn_cast(CV)) - { // Print the fields in successive locations - const vector& constValues = CPA->getValues(); - for (unsigned i=1; i < constValues.size(); i++) - this->printSingleConstant(cast(constValues[i].get())); - } - else - this->printSingleConstant(CV, valID); + this->printConstantValueOnly(CV); }