* Continue cleanup of type printing code

* Print a newline after a malloc instruction
* Convert unprintable characters to x_ instead of _x so that we don't
  generate identifiers that start with underscores


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@2577 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2002-05-09 15:18:52 +00:00
parent 3ef6dc7bdd
commit deed7a5ae3
2 changed files with 90 additions and 116 deletions

View File

@ -158,33 +158,27 @@ static std::string getConstStrValue(const Constant* CPV) {
} }
} }
// Internal function
// Pass the Type* variable and and the variable name and this prints out the // Pass the Type* variable and and the variable name and this prints out the
// variable declaration. // variable declaration.
// This is different from calcTypeName because if you need to declare an array //
// the size of the array would appear after the variable name itself
// For eg. int a[10];
static string calcTypeNameVar(const Type *Ty, static string calcTypeNameVar(const Type *Ty,
map<const Type *, string> &TypeNames, map<const Type *, string> &TypeNames,
const string &NameSoFar, bool ignoreName = false){ const string &NameSoFar, bool ignoreName = false){
if (Ty->isPrimitiveType()) if (Ty->isPrimitiveType())
switch (Ty->getPrimitiveID()) { switch (Ty->getPrimitiveID()) {
case Type::BoolTyID: case Type::VoidTyID: return "void " + NameSoFar;
return "bool " + NameSoFar; case Type::BoolTyID: return "bool " + NameSoFar;
case Type::UByteTyID: case Type::UByteTyID: return "unsigned char " + NameSoFar;
return "unsigned char " + NameSoFar; case Type::SByteTyID: return "signed char " + NameSoFar;
case Type::SByteTyID: case Type::UShortTyID: return "unsigned short " + NameSoFar;
return "signed char " + NameSoFar; case Type::ShortTyID: return "short " + NameSoFar;
case Type::UShortTyID: case Type::UIntTyID: return "unsigned " + NameSoFar;
return "unsigned long long " + NameSoFar; case Type::IntTyID: return "int " + NameSoFar;
case Type::ULongTyID: case Type::ULongTyID: return "unsigned long long " + NameSoFar;
return "unsigned long long " + NameSoFar; case Type::LongTyID: return "signed long long " + NameSoFar;
case Type::LongTyID:
return "signed long long " + NameSoFar;
case Type::UIntTyID:
return "unsigned " + NameSoFar;
default : default :
return Ty->getDescription() + " " + NameSoFar; cerr << "Unknown primitive type: " << Ty << "\n";
abort();
} }
// Check to see if the type is named. // Check to see if the type is named.
@ -233,14 +227,14 @@ static string calcTypeNameVar(const Type *Ty,
case Type::PointerTyID: { case Type::PointerTyID: {
Result = calcTypeNameVar(cast<const PointerType>(Ty)->getElementType(), Result = calcTypeNameVar(cast<const PointerType>(Ty)->getElementType(),
TypeNames, "(*" + NameSoFar + ")"); TypeNames, "*" + NameSoFar);
break; break;
} }
case Type::ArrayTyID: { case Type::ArrayTyID: {
const ArrayType *ATy = cast<const ArrayType>(Ty); const ArrayType *ATy = cast<const ArrayType>(Ty);
int NumElements = ATy->getNumElements(); int NumElements = ATy->getNumElements();
Result = calcTypeNameVar(ATy->getElementType(), TypeNames, Result = calcTypeNameVar(ATy->getElementType(), TypeNames,
NameSoFar + "[" + itostr(NumElements) + "]"); NameSoFar + "[" + itostr(NumElements) + "]");
break; break;
} }
@ -283,7 +277,6 @@ namespace {
void printGlobal(const GlobalVariable *GV); void printGlobal(const GlobalVariable *GV);
void printFunctionSignature(const Function *F); void printFunctionSignature(const Function *F);
void printFunctionDecl(const Function *F); // Print just the forward decl void printFunctionDecl(const Function *F); // Print just the forward decl
void printFunctionArgument(const Argument *FA);
void printFunction(const Function *); void printFunction(const Function *);
@ -471,24 +464,24 @@ void CInstPrintVisitor::visitInvokeInst(InvokeInst *I) {
void CInstPrintVisitor::visitMallocInst(MallocInst *I) { void CInstPrintVisitor::visitMallocInst(MallocInst *I) {
outputLValue(I); outputLValue(I);
Out << "("; Out << "(";
CW.printType(I->getType());
Out << ")malloc(sizeof(";
CW.printType(I->getType()->getElementType()); CW.printType(I->getType()->getElementType());
Out << "*)malloc(sizeof(";
CW.printTypeVar(I->getType()->getElementType(), "");
Out << ")"; Out << ")";
if (I->isArrayAllocation()) { if (I->isArrayAllocation()) {
Out << " * " ; Out << " * " ;
CW.writeOperand(I->getOperand(0)); CW.writeOperand(I->getOperand(0));
} }
Out << ");"; Out << ");\n";
} }
void CInstPrintVisitor::visitAllocaInst(AllocaInst *I) { void CInstPrintVisitor::visitAllocaInst(AllocaInst *I) {
outputLValue(I); outputLValue(I);
Out << "("; Out << "(";
CW.printTypeVar(I->getType(), ""); CW.printType(I->getType());
Out << ") alloca(sizeof("; Out << ") alloca(sizeof(";
CW.printTypeVar(I->getType()->getElementType(), ""); CW.printType(I->getType()->getElementType());
Out << ")"; Out << ")";
if (I->isArrayAllocation()) { if (I->isArrayAllocation()) {
Out << " * " ; Out << " * " ;
@ -590,9 +583,9 @@ static string makeNameProper(string x) {
string tmp; string tmp;
for (string::iterator sI = x.begin(), sEnd = x.end(); sI != sEnd; sI++) for (string::iterator sI = x.begin(), sEnd = x.end(); sI != sEnd; sI++)
switch (*sI) { switch (*sI) {
case '.': tmp += "_d"; break; case '.': tmp += "d_"; break;
case ' ': tmp += "_s"; break; case ' ': tmp += "s_"; break;
case '-': tmp += "_D"; break; case '-': tmp += "D_"; break;
case '_': tmp += "__"; break; case '_': tmp += "__"; break;
default: tmp += *sI; default: tmp += *sI;
} }
@ -633,8 +626,17 @@ void CWriter::printModule(const Module *M) {
printSymbolTable(*M->getSymbolTable()); printSymbolTable(*M->getSymbolTable());
Out << "\n\n/* Global Data */\n"; Out << "\n\n/* Global Data */\n";
for_each(M->gbegin(), M->gend(), for (Module::const_giterator I = M->gbegin(), E = M->gend(); I != E; ++I) {
bind_obj(this, &CWriter::printGlobal)); GlobalVariable *GV = *I;
if (GV->hasInternalLinkage()) Out << "static ";
printTypeVar(GV->getType()->getElementType(), getValueName(GV));
if (GV->hasInitializer()) {
Out << " = " ;
writeOperand(GV->getInitializer());
}
Out << ";\n";
}
// First output all the declarations of the functions as C requires Functions // First output all the declarations of the functions as C requires Functions
// be declared before they are used. // be declared before they are used.
@ -647,19 +649,6 @@ void CWriter::printModule(const Module *M) {
for_each(M->begin(), M->end(), bind_obj(this, &CWriter::printFunction)); for_each(M->begin(), M->end(), bind_obj(this, &CWriter::printFunction));
} }
// prints the global constants
void CWriter::printGlobal(const GlobalVariable *GV) {
if (GV->hasInternalLinkage()) Out << "static ";
printTypeVar(GV->getType()->getElementType(), getValueName(GV));
if (GV->hasInitializer()) {
Out << " = " ;
writeOperand(GV->getInitializer());
}
Out << ";\n";
}
// printSymbolTable - Run through symbol table looking for named constants // printSymbolTable - Run through symbol table looking for named constants
// if a named constant is found, emit it's declaration... // if a named constant is found, emit it's declaration...
@ -715,8 +704,17 @@ void CWriter::printFunctionSignature(const Function *F) {
Out << " " << getValueName(F) << "("; Out << " " << getValueName(F) << "(";
if (!F->isExternal()) { if (!F->isExternal()) {
for_each(F->getArgumentList().begin(), F->getArgumentList().end(), if (!F->getArgumentList().empty()) {
bind_obj(this, &CWriter::printFunctionArgument)); printTypeVar(F->getArgumentList().front()->getType(),
getValueName(F->getArgumentList().front()));
for (Function::ArgumentListType::const_iterator
I = F->getArgumentList().begin()+1,
E = F->getArgumentList().end(); I != E; ++I) {
Out << ", ";
printTypeVar((*I)->getType(), getValueName(*I));
}
}
} else { } else {
// Loop over the arguments, printing them... // Loop over the arguments, printing them...
for (FunctionType::ParamTypes::const_iterator I = for (FunctionType::ParamTypes::const_iterator I =
@ -736,17 +734,6 @@ void CWriter::printFunctionSignature(const Function *F) {
} }
// printFunctionArgument - This member is called for every argument that
// is passed into the method. Simply print it out
//
void CWriter::printFunctionArgument(const Argument *Arg) {
// Insert commas as we go... the first arg doesn't get a comma
if (Arg != Arg->getParent()->getArgumentList().front()) Out << ", ";
// Output type...
printTypeVar(Arg->getType(), getValueName(Arg));
}
void CWriter::printFunction(const Function *F) { void CWriter::printFunction(const Function *F) {
if (F->isExternal()) return; if (F->isExternal()) return;

View File

@ -158,33 +158,27 @@ static std::string getConstStrValue(const Constant* CPV) {
} }
} }
// Internal function
// Pass the Type* variable and and the variable name and this prints out the // Pass the Type* variable and and the variable name and this prints out the
// variable declaration. // variable declaration.
// This is different from calcTypeName because if you need to declare an array //
// the size of the array would appear after the variable name itself
// For eg. int a[10];
static string calcTypeNameVar(const Type *Ty, static string calcTypeNameVar(const Type *Ty,
map<const Type *, string> &TypeNames, map<const Type *, string> &TypeNames,
const string &NameSoFar, bool ignoreName = false){ const string &NameSoFar, bool ignoreName = false){
if (Ty->isPrimitiveType()) if (Ty->isPrimitiveType())
switch (Ty->getPrimitiveID()) { switch (Ty->getPrimitiveID()) {
case Type::BoolTyID: case Type::VoidTyID: return "void " + NameSoFar;
return "bool " + NameSoFar; case Type::BoolTyID: return "bool " + NameSoFar;
case Type::UByteTyID: case Type::UByteTyID: return "unsigned char " + NameSoFar;
return "unsigned char " + NameSoFar; case Type::SByteTyID: return "signed char " + NameSoFar;
case Type::SByteTyID: case Type::UShortTyID: return "unsigned short " + NameSoFar;
return "signed char " + NameSoFar; case Type::ShortTyID: return "short " + NameSoFar;
case Type::UShortTyID: case Type::UIntTyID: return "unsigned " + NameSoFar;
return "unsigned long long " + NameSoFar; case Type::IntTyID: return "int " + NameSoFar;
case Type::ULongTyID: case Type::ULongTyID: return "unsigned long long " + NameSoFar;
return "unsigned long long " + NameSoFar; case Type::LongTyID: return "signed long long " + NameSoFar;
case Type::LongTyID:
return "signed long long " + NameSoFar;
case Type::UIntTyID:
return "unsigned " + NameSoFar;
default : default :
return Ty->getDescription() + " " + NameSoFar; cerr << "Unknown primitive type: " << Ty << "\n";
abort();
} }
// Check to see if the type is named. // Check to see if the type is named.
@ -233,14 +227,14 @@ static string calcTypeNameVar(const Type *Ty,
case Type::PointerTyID: { case Type::PointerTyID: {
Result = calcTypeNameVar(cast<const PointerType>(Ty)->getElementType(), Result = calcTypeNameVar(cast<const PointerType>(Ty)->getElementType(),
TypeNames, "(*" + NameSoFar + ")"); TypeNames, "*" + NameSoFar);
break; break;
} }
case Type::ArrayTyID: { case Type::ArrayTyID: {
const ArrayType *ATy = cast<const ArrayType>(Ty); const ArrayType *ATy = cast<const ArrayType>(Ty);
int NumElements = ATy->getNumElements(); int NumElements = ATy->getNumElements();
Result = calcTypeNameVar(ATy->getElementType(), TypeNames, Result = calcTypeNameVar(ATy->getElementType(), TypeNames,
NameSoFar + "[" + itostr(NumElements) + "]"); NameSoFar + "[" + itostr(NumElements) + "]");
break; break;
} }
@ -283,7 +277,6 @@ namespace {
void printGlobal(const GlobalVariable *GV); void printGlobal(const GlobalVariable *GV);
void printFunctionSignature(const Function *F); void printFunctionSignature(const Function *F);
void printFunctionDecl(const Function *F); // Print just the forward decl void printFunctionDecl(const Function *F); // Print just the forward decl
void printFunctionArgument(const Argument *FA);
void printFunction(const Function *); void printFunction(const Function *);
@ -471,24 +464,24 @@ void CInstPrintVisitor::visitInvokeInst(InvokeInst *I) {
void CInstPrintVisitor::visitMallocInst(MallocInst *I) { void CInstPrintVisitor::visitMallocInst(MallocInst *I) {
outputLValue(I); outputLValue(I);
Out << "("; Out << "(";
CW.printType(I->getType());
Out << ")malloc(sizeof(";
CW.printType(I->getType()->getElementType()); CW.printType(I->getType()->getElementType());
Out << "*)malloc(sizeof(";
CW.printTypeVar(I->getType()->getElementType(), "");
Out << ")"; Out << ")";
if (I->isArrayAllocation()) { if (I->isArrayAllocation()) {
Out << " * " ; Out << " * " ;
CW.writeOperand(I->getOperand(0)); CW.writeOperand(I->getOperand(0));
} }
Out << ");"; Out << ");\n";
} }
void CInstPrintVisitor::visitAllocaInst(AllocaInst *I) { void CInstPrintVisitor::visitAllocaInst(AllocaInst *I) {
outputLValue(I); outputLValue(I);
Out << "("; Out << "(";
CW.printTypeVar(I->getType(), ""); CW.printType(I->getType());
Out << ") alloca(sizeof("; Out << ") alloca(sizeof(";
CW.printTypeVar(I->getType()->getElementType(), ""); CW.printType(I->getType()->getElementType());
Out << ")"; Out << ")";
if (I->isArrayAllocation()) { if (I->isArrayAllocation()) {
Out << " * " ; Out << " * " ;
@ -590,9 +583,9 @@ static string makeNameProper(string x) {
string tmp; string tmp;
for (string::iterator sI = x.begin(), sEnd = x.end(); sI != sEnd; sI++) for (string::iterator sI = x.begin(), sEnd = x.end(); sI != sEnd; sI++)
switch (*sI) { switch (*sI) {
case '.': tmp += "_d"; break; case '.': tmp += "d_"; break;
case ' ': tmp += "_s"; break; case ' ': tmp += "s_"; break;
case '-': tmp += "_D"; break; case '-': tmp += "D_"; break;
case '_': tmp += "__"; break; case '_': tmp += "__"; break;
default: tmp += *sI; default: tmp += *sI;
} }
@ -633,8 +626,17 @@ void CWriter::printModule(const Module *M) {
printSymbolTable(*M->getSymbolTable()); printSymbolTable(*M->getSymbolTable());
Out << "\n\n/* Global Data */\n"; Out << "\n\n/* Global Data */\n";
for_each(M->gbegin(), M->gend(), for (Module::const_giterator I = M->gbegin(), E = M->gend(); I != E; ++I) {
bind_obj(this, &CWriter::printGlobal)); GlobalVariable *GV = *I;
if (GV->hasInternalLinkage()) Out << "static ";
printTypeVar(GV->getType()->getElementType(), getValueName(GV));
if (GV->hasInitializer()) {
Out << " = " ;
writeOperand(GV->getInitializer());
}
Out << ";\n";
}
// First output all the declarations of the functions as C requires Functions // First output all the declarations of the functions as C requires Functions
// be declared before they are used. // be declared before they are used.
@ -647,19 +649,6 @@ void CWriter::printModule(const Module *M) {
for_each(M->begin(), M->end(), bind_obj(this, &CWriter::printFunction)); for_each(M->begin(), M->end(), bind_obj(this, &CWriter::printFunction));
} }
// prints the global constants
void CWriter::printGlobal(const GlobalVariable *GV) {
if (GV->hasInternalLinkage()) Out << "static ";
printTypeVar(GV->getType()->getElementType(), getValueName(GV));
if (GV->hasInitializer()) {
Out << " = " ;
writeOperand(GV->getInitializer());
}
Out << ";\n";
}
// printSymbolTable - Run through symbol table looking for named constants // printSymbolTable - Run through symbol table looking for named constants
// if a named constant is found, emit it's declaration... // if a named constant is found, emit it's declaration...
@ -715,8 +704,17 @@ void CWriter::printFunctionSignature(const Function *F) {
Out << " " << getValueName(F) << "("; Out << " " << getValueName(F) << "(";
if (!F->isExternal()) { if (!F->isExternal()) {
for_each(F->getArgumentList().begin(), F->getArgumentList().end(), if (!F->getArgumentList().empty()) {
bind_obj(this, &CWriter::printFunctionArgument)); printTypeVar(F->getArgumentList().front()->getType(),
getValueName(F->getArgumentList().front()));
for (Function::ArgumentListType::const_iterator
I = F->getArgumentList().begin()+1,
E = F->getArgumentList().end(); I != E; ++I) {
Out << ", ";
printTypeVar((*I)->getType(), getValueName(*I));
}
}
} else { } else {
// Loop over the arguments, printing them... // Loop over the arguments, printing them...
for (FunctionType::ParamTypes::const_iterator I = for (FunctionType::ParamTypes::const_iterator I =
@ -736,17 +734,6 @@ void CWriter::printFunctionSignature(const Function *F) {
} }
// printFunctionArgument - This member is called for every argument that
// is passed into the method. Simply print it out
//
void CWriter::printFunctionArgument(const Argument *Arg) {
// Insert commas as we go... the first arg doesn't get a comma
if (Arg != Arg->getParent()->getArgumentList().front()) Out << ", ";
// Output type...
printTypeVar(Arg->getType(), getValueName(Arg));
}
void CWriter::printFunction(const Function *F) { void CWriter::printFunction(const Function *F) {
if (F->isExternal()) return; if (F->isExternal()) return;