From b78e8382e8ea512dcbdf70fccf0d7b48bf299bcf Mon Sep 17 00:00:00 2001 From: Robert Bocchino Date: Fri, 20 Jan 2006 20:43:57 +0000 Subject: [PATCH] Make the C writer work with packed types. printContainedStructs is still not quite right and will be fixed later. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@25488 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Target/CBackend/CBackend.cpp | 53 +++++++++++++++++++++++++++++--- lib/Target/CBackend/Writer.cpp | 53 +++++++++++++++++++++++++++++--- 2 files changed, 98 insertions(+), 8 deletions(-) diff --git a/lib/Target/CBackend/CBackend.cpp b/lib/Target/CBackend/CBackend.cpp index 87a575b813c..a9df7eb9aaf 100644 --- a/lib/Target/CBackend/CBackend.cpp +++ b/lib/Target/CBackend/CBackend.cpp @@ -132,6 +132,7 @@ namespace { void printConstant(Constant *CPV); void printConstantArray(ConstantArray *CPA); + void printConstantPacked(ConstantPacked *CP); // isInlinableInst - Attempt to inline instructions into their uses to build // trees as much as possible. To do this, we have to consistently decide @@ -329,7 +330,8 @@ std::ostream &CWriter::printType(std::ostream &Out, const Type *Ty, const PointerType *PTy = cast(Ty); std::string ptrName = "*" + NameSoFar; - if (isa(PTy->getElementType())) + if (isa(PTy->getElementType()) || + isa(PTy->getElementType())) ptrName = "(" + ptrName + ")"; return printType(Out, PTy->getElementType(), ptrName); @@ -343,6 +345,14 @@ std::ostream &CWriter::printType(std::ostream &Out, const Type *Ty, NameSoFar + "[" + utostr(NumElements) + "]"); } + case Type::PackedTyID: { + const PackedType *PTy = cast(Ty); + unsigned NumElements = PTy->getNumElements(); + if (NumElements == 0) NumElements = 1; + return printType(Out, PTy->getElementType(), + NameSoFar + "[" + utostr(NumElements) + "]"); + } + case Type::OpaqueTyID: { static int Count = 0; std::string TyName = "struct opaque_" + itostr(Count++); @@ -426,6 +436,19 @@ void CWriter::printConstantArray(ConstantArray *CPA) { } } +void CWriter::printConstantPacked(ConstantPacked *CP) { + Out << '{'; + if (CP->getNumOperands()) { + Out << ' '; + printConstant(cast(CP->getOperand(0))); + for (unsigned i = 1, e = CP->getNumOperands(); i != e; ++i) { + Out << ", "; + printConstant(cast(CP->getOperand(i))); + } + } + Out << " }"; +} + // isFPCSafeToPrint - Returns true if we may assume that CFP may be written out // textually as a double (rather than as a reference to a stack-allocated // variable). We decide this by converting CFP to a string and back into a @@ -641,6 +664,25 @@ void CWriter::printConstant(Constant *CPV) { } break; + case Type::PackedTyID: + if (isa(CPV) || isa(CPV)) { + const PackedType *AT = cast(CPV->getType()); + Out << '{'; + if (AT->getNumElements()) { + Out << ' '; + Constant *CZ = Constant::getNullValue(AT->getElementType()); + printConstant(CZ); + for (unsigned i = 1, e = AT->getNumElements(); i != e; ++i) { + Out << ", "; + printConstant(CZ); + } + } + Out << " }"; + } else { + printConstantPacked(cast(CPV)); + } + break; + case Type::StructTyID: if (isa(CPV) || isa(CPV)) { const StructType *ST = cast(CPV->getType()); @@ -936,7 +978,8 @@ bool CWriter::doInitialization(Module &M) { // the compiler figure out the rest of the zeros. Out << " = " ; if (isa(I->getInitializer()->getType()) || - isa(I->getInitializer()->getType())) { + isa(I->getInitializer()->getType()) || + isa(I->getInitializer()->getType())) { Out << "{ 0 }"; } else { // Just print it out normally. @@ -987,7 +1030,7 @@ void CWriter::printFloatingPointConstants(Function &F) { /// printSymbolTable - Run through symbol table looking for type names. If a -/// type name is found, emit it's declaration... +/// type name is found, emit its declaration... /// void CWriter::printModuleTypes(const SymbolTable &ST) { // We are only interested in the type plane of the symbol table. @@ -1035,6 +1078,9 @@ void CWriter::printModuleTypes(const SymbolTable &ST) { // Push the struct onto the stack and recursively push all structs // this one depends on. +// +// TODO: Make this work properly with packed types +// void CWriter::printContainedStructs(const Type *Ty, std::set &StructPrinted){ // Don't walk through pointers. @@ -1056,7 +1102,6 @@ void CWriter::printContainedStructs(const Type *Ty, } } - void CWriter::printFunctionSignature(const Function *F, bool Prototype) { if (F->hasInternalLinkage()) Out << "static "; diff --git a/lib/Target/CBackend/Writer.cpp b/lib/Target/CBackend/Writer.cpp index 87a575b813c..a9df7eb9aaf 100644 --- a/lib/Target/CBackend/Writer.cpp +++ b/lib/Target/CBackend/Writer.cpp @@ -132,6 +132,7 @@ namespace { void printConstant(Constant *CPV); void printConstantArray(ConstantArray *CPA); + void printConstantPacked(ConstantPacked *CP); // isInlinableInst - Attempt to inline instructions into their uses to build // trees as much as possible. To do this, we have to consistently decide @@ -329,7 +330,8 @@ std::ostream &CWriter::printType(std::ostream &Out, const Type *Ty, const PointerType *PTy = cast(Ty); std::string ptrName = "*" + NameSoFar; - if (isa(PTy->getElementType())) + if (isa(PTy->getElementType()) || + isa(PTy->getElementType())) ptrName = "(" + ptrName + ")"; return printType(Out, PTy->getElementType(), ptrName); @@ -343,6 +345,14 @@ std::ostream &CWriter::printType(std::ostream &Out, const Type *Ty, NameSoFar + "[" + utostr(NumElements) + "]"); } + case Type::PackedTyID: { + const PackedType *PTy = cast(Ty); + unsigned NumElements = PTy->getNumElements(); + if (NumElements == 0) NumElements = 1; + return printType(Out, PTy->getElementType(), + NameSoFar + "[" + utostr(NumElements) + "]"); + } + case Type::OpaqueTyID: { static int Count = 0; std::string TyName = "struct opaque_" + itostr(Count++); @@ -426,6 +436,19 @@ void CWriter::printConstantArray(ConstantArray *CPA) { } } +void CWriter::printConstantPacked(ConstantPacked *CP) { + Out << '{'; + if (CP->getNumOperands()) { + Out << ' '; + printConstant(cast(CP->getOperand(0))); + for (unsigned i = 1, e = CP->getNumOperands(); i != e; ++i) { + Out << ", "; + printConstant(cast(CP->getOperand(i))); + } + } + Out << " }"; +} + // isFPCSafeToPrint - Returns true if we may assume that CFP may be written out // textually as a double (rather than as a reference to a stack-allocated // variable). We decide this by converting CFP to a string and back into a @@ -641,6 +664,25 @@ void CWriter::printConstant(Constant *CPV) { } break; + case Type::PackedTyID: + if (isa(CPV) || isa(CPV)) { + const PackedType *AT = cast(CPV->getType()); + Out << '{'; + if (AT->getNumElements()) { + Out << ' '; + Constant *CZ = Constant::getNullValue(AT->getElementType()); + printConstant(CZ); + for (unsigned i = 1, e = AT->getNumElements(); i != e; ++i) { + Out << ", "; + printConstant(CZ); + } + } + Out << " }"; + } else { + printConstantPacked(cast(CPV)); + } + break; + case Type::StructTyID: if (isa(CPV) || isa(CPV)) { const StructType *ST = cast(CPV->getType()); @@ -936,7 +978,8 @@ bool CWriter::doInitialization(Module &M) { // the compiler figure out the rest of the zeros. Out << " = " ; if (isa(I->getInitializer()->getType()) || - isa(I->getInitializer()->getType())) { + isa(I->getInitializer()->getType()) || + isa(I->getInitializer()->getType())) { Out << "{ 0 }"; } else { // Just print it out normally. @@ -987,7 +1030,7 @@ void CWriter::printFloatingPointConstants(Function &F) { /// printSymbolTable - Run through symbol table looking for type names. If a -/// type name is found, emit it's declaration... +/// type name is found, emit its declaration... /// void CWriter::printModuleTypes(const SymbolTable &ST) { // We are only interested in the type plane of the symbol table. @@ -1035,6 +1078,9 @@ void CWriter::printModuleTypes(const SymbolTable &ST) { // Push the struct onto the stack and recursively push all structs // this one depends on. +// +// TODO: Make this work properly with packed types +// void CWriter::printContainedStructs(const Type *Ty, std::set &StructPrinted){ // Don't walk through pointers. @@ -1056,7 +1102,6 @@ void CWriter::printContainedStructs(const Type *Ty, } } - void CWriter::printFunctionSignature(const Function *F, bool Prototype) { if (F->hasInternalLinkage()) Out << "static ";