forked from OSchip/llvm-project
parent
ee285c1df9
commit
cdf2a1811e
|
@ -23,6 +23,7 @@
|
||||||
#include "Support/STLExtras.h"
|
#include "Support/STLExtras.h"
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <set>
|
#include <set>
|
||||||
|
#include <sstream>
|
||||||
using std::string;
|
using std::string;
|
||||||
using std::map;
|
using std::map;
|
||||||
using std::ostream;
|
using std::ostream;
|
||||||
|
@ -64,7 +65,7 @@ namespace {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
ostream &printType(const Type *Ty, const string &VariableName = "",
|
ostream &printType(std::ostream &Out, const Type *Ty, const string &VariableName = "",
|
||||||
bool IgnoreName = false, bool namedContext = true);
|
bool IgnoreName = false, bool namedContext = true);
|
||||||
|
|
||||||
void writeOperand(Value *Operand);
|
void writeOperand(Value *Operand);
|
||||||
|
@ -176,7 +177,7 @@ inline bool ptrTypeNameNeedsParens(const string &NameSoFar) {
|
||||||
// Pass the Type* and the variable name and this prints out the variable
|
// Pass the Type* and the variable name and this prints out the variable
|
||||||
// declaration.
|
// declaration.
|
||||||
//
|
//
|
||||||
ostream &CWriter::printType(const Type *Ty, const string &NameSoFar,
|
ostream &CWriter::printType(std::ostream &Out, const Type *Ty, const string &NameSoFar,
|
||||||
bool IgnoreName, bool namedContext) {
|
bool IgnoreName, bool namedContext) {
|
||||||
if (Ty->isPrimitiveType())
|
if (Ty->isPrimitiveType())
|
||||||
switch (Ty->getPrimitiveID()) {
|
switch (Ty->getPrimitiveID()) {
|
||||||
|
@ -208,22 +209,24 @@ ostream &CWriter::printType(const Type *Ty, const string &NameSoFar,
|
||||||
switch (Ty->getPrimitiveID()) {
|
switch (Ty->getPrimitiveID()) {
|
||||||
case Type::FunctionTyID: {
|
case Type::FunctionTyID: {
|
||||||
const FunctionType *MTy = cast<FunctionType>(Ty);
|
const FunctionType *MTy = cast<FunctionType>(Ty);
|
||||||
printType(MTy->getReturnType(), "");
|
std::stringstream FunctionInards;
|
||||||
Out << " (" << NameSoFar << ") (";
|
FunctionInards << " (" << NameSoFar << ") (";
|
||||||
|
|
||||||
for (FunctionType::ParamTypes::const_iterator
|
for (FunctionType::ParamTypes::const_iterator
|
||||||
I = MTy->getParamTypes().begin(),
|
I = MTy->getParamTypes().begin(),
|
||||||
E = MTy->getParamTypes().end(); I != E; ++I) {
|
E = MTy->getParamTypes().end(); I != E; ++I) {
|
||||||
if (I != MTy->getParamTypes().begin())
|
if (I != MTy->getParamTypes().begin())
|
||||||
Out << ", ";
|
FunctionInards << ", ";
|
||||||
printType(*I, "");
|
printType(FunctionInards, *I, "");
|
||||||
}
|
}
|
||||||
if (MTy->isVarArg()) {
|
if (MTy->isVarArg()) {
|
||||||
if (!MTy->getParamTypes().empty())
|
if (!MTy->getParamTypes().empty())
|
||||||
Out << ", ";
|
FunctionInards << ", ";
|
||||||
Out << "...";
|
FunctionInards << "...";
|
||||||
}
|
}
|
||||||
return Out << ")";
|
FunctionInards << ")";
|
||||||
|
string tstr = FunctionInards.str();
|
||||||
|
printType(Out, MTy->getReturnType(), tstr);
|
||||||
|
return Out;
|
||||||
}
|
}
|
||||||
case Type::StructTyID: {
|
case Type::StructTyID: {
|
||||||
const StructType *STy = cast<StructType>(Ty);
|
const StructType *STy = cast<StructType>(Ty);
|
||||||
|
@ -233,7 +236,7 @@ ostream &CWriter::printType(const Type *Ty, const string &NameSoFar,
|
||||||
I = STy->getElementTypes().begin(),
|
I = STy->getElementTypes().begin(),
|
||||||
E = STy->getElementTypes().end(); I != E; ++I) {
|
E = STy->getElementTypes().end(); I != E; ++I) {
|
||||||
Out << " ";
|
Out << " ";
|
||||||
printType(*I, "field" + utostr(Idx++));
|
printType(Out, *I, "field" + utostr(Idx++));
|
||||||
Out << ";\n";
|
Out << ";\n";
|
||||||
}
|
}
|
||||||
return Out << "}";
|
return Out << "}";
|
||||||
|
@ -250,13 +253,13 @@ ostream &CWriter::printType(const Type *Ty, const string &NameSoFar,
|
||||||
PTy->getElementType()->getPrimitiveID() == Type::ArrayTyID)
|
PTy->getElementType()->getPrimitiveID() == Type::ArrayTyID)
|
||||||
ptrName = "(" + ptrName + ")"; //
|
ptrName = "(" + ptrName + ")"; //
|
||||||
|
|
||||||
return printType(PTy->getElementType(), ptrName);
|
return printType(Out, PTy->getElementType(), ptrName);
|
||||||
}
|
}Out <<"--";
|
||||||
|
|
||||||
case Type::ArrayTyID: {
|
case Type::ArrayTyID: {
|
||||||
const ArrayType *ATy = cast<ArrayType>(Ty);
|
const ArrayType *ATy = cast<ArrayType>(Ty);
|
||||||
unsigned NumElements = ATy->getNumElements();
|
unsigned NumElements = ATy->getNumElements();
|
||||||
return printType(ATy->getElementType(),
|
return printType(Out, ATy->getElementType(),
|
||||||
NameSoFar + "[" + utostr(NumElements) + "]");
|
NameSoFar + "[" + utostr(NumElements) + "]");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -340,7 +343,7 @@ void CWriter::printConstant(Constant *CPV) {
|
||||||
switch (CE->getOpcode()) {
|
switch (CE->getOpcode()) {
|
||||||
case Instruction::Cast:
|
case Instruction::Cast:
|
||||||
Out << "((";
|
Out << "((";
|
||||||
printType(CPV->getType());
|
printType(Out, CPV->getType());
|
||||||
Out << ")";
|
Out << ")";
|
||||||
printConstant(cast<Constant>(CPV->getOperand(0)));
|
printConstant(cast<Constant>(CPV->getOperand(0)));
|
||||||
Out << ")";
|
Out << ")";
|
||||||
|
@ -553,7 +556,7 @@ void CWriter::printModule(Module *M) {
|
||||||
for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) {
|
for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) {
|
||||||
if (I->hasExternalLinkage()) {
|
if (I->hasExternalLinkage()) {
|
||||||
Out << "extern ";
|
Out << "extern ";
|
||||||
printType(I->getType()->getElementType(), getValueName(I));
|
printType(Out, I->getType()->getElementType(), getValueName(I));
|
||||||
Out << ";\n";
|
Out << ";\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -581,7 +584,7 @@ void CWriter::printModule(Module *M) {
|
||||||
for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
|
for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
|
||||||
if (!I->isExternal()) {
|
if (!I->isExternal()) {
|
||||||
Out << "extern ";
|
Out << "extern ";
|
||||||
printType(I->getType()->getElementType(), getValueName(I));
|
printType(Out, I->getType()->getElementType(), getValueName(I));
|
||||||
|
|
||||||
Out << ";\n";
|
Out << ";\n";
|
||||||
}
|
}
|
||||||
|
@ -595,7 +598,7 @@ void CWriter::printModule(Module *M) {
|
||||||
if (!I->isExternal()) {
|
if (!I->isExternal()) {
|
||||||
if (I->hasInternalLinkage())
|
if (I->hasInternalLinkage())
|
||||||
Out << "static ";
|
Out << "static ";
|
||||||
printType(I->getType()->getElementType(), getValueName(I));
|
printType(Out, I->getType()->getElementType(), getValueName(I));
|
||||||
|
|
||||||
Out << " = " ;
|
Out << " = " ;
|
||||||
writeOperand(I->getInitializer());
|
writeOperand(I->getInitializer());
|
||||||
|
@ -641,7 +644,7 @@ void CWriter::printSymbolTable(const SymbolTable &ST) {
|
||||||
const Type *Ty = cast<Type>(I->second);
|
const Type *Ty = cast<Type>(I->second);
|
||||||
string Name = "l_" + makeNameProper(I->first);
|
string Name = "l_" + makeNameProper(I->first);
|
||||||
Out << "typedef ";
|
Out << "typedef ";
|
||||||
printType(Ty, Name);
|
printType(Out, Ty, Name);
|
||||||
Out << ";\n";
|
Out << ";\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -678,7 +681,7 @@ void CWriter::printContainedStructs(const Type *Ty,
|
||||||
//Print structure type out..
|
//Print structure type out..
|
||||||
StructPrinted.insert(STy);
|
StructPrinted.insert(STy);
|
||||||
string Name = TypeNames[STy];
|
string Name = TypeNames[STy];
|
||||||
printType(STy, Name, true);
|
printType(Out, STy, Name, true);
|
||||||
Out << ";\n\n";
|
Out << ";\n\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -696,31 +699,29 @@ void CWriter::printFunctionSignature(const Function *F, bool Prototype) {
|
||||||
// to include the general one.
|
// to include the general one.
|
||||||
if (getValueName(F) == "malloc")
|
if (getValueName(F) == "malloc")
|
||||||
needsMalloc = false;
|
needsMalloc = false;
|
||||||
if (F->hasInternalLinkage()) Out << "static ";
|
if (F->hasInternalLinkage()) Out << "static ";
|
||||||
|
|
||||||
// Loop over the arguments, printing them...
|
// Loop over the arguments, printing them...
|
||||||
const FunctionType *FT = cast<FunctionType>(F->getFunctionType());
|
const FunctionType *FT = cast<FunctionType>(F->getFunctionType());
|
||||||
|
|
||||||
// Print out the return type and name...
|
std::stringstream FunctionInards;
|
||||||
printType(F->getReturnType());
|
|
||||||
Out << getValueName(F) << "(";
|
// Print out the name...
|
||||||
|
FunctionInards << getValueName(F) << "(";
|
||||||
|
|
||||||
if (!F->isExternal()) {
|
if (!F->isExternal()) {
|
||||||
if (!F->aempty()) {
|
if (!F->aempty()) {
|
||||||
string ArgName;
|
string ArgName;
|
||||||
if (F->abegin()->hasName() || !Prototype)
|
if (F->abegin()->hasName() || !Prototype)
|
||||||
ArgName = getValueName(F->abegin());
|
ArgName = getValueName(F->abegin());
|
||||||
|
printType(FunctionInards, F->afront().getType(), ArgName);
|
||||||
printType(F->afront().getType(), ArgName);
|
|
||||||
|
|
||||||
for (Function::const_aiterator I = ++F->abegin(), E = F->aend();
|
for (Function::const_aiterator I = ++F->abegin(), E = F->aend();
|
||||||
I != E; ++I) {
|
I != E; ++I) {
|
||||||
Out << ", ";
|
FunctionInards << ", ";
|
||||||
if (I->hasName() || !Prototype)
|
if (I->hasName() || !Prototype)
|
||||||
ArgName = getValueName(I);
|
ArgName = getValueName(I);
|
||||||
else
|
else
|
||||||
ArgName = "";
|
ArgName = "";
|
||||||
printType(I->getType(), ArgName);
|
printType(FunctionInards, I->getType(), ArgName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -728,8 +729,8 @@ void CWriter::printFunctionSignature(const Function *F, bool Prototype) {
|
||||||
for (FunctionType::ParamTypes::const_iterator I =
|
for (FunctionType::ParamTypes::const_iterator I =
|
||||||
FT->getParamTypes().begin(),
|
FT->getParamTypes().begin(),
|
||||||
E = FT->getParamTypes().end(); I != E; ++I) {
|
E = FT->getParamTypes().end(); I != E; ++I) {
|
||||||
if (I != FT->getParamTypes().begin()) Out << ", ";
|
if (I != FT->getParamTypes().begin()) FunctionInards << ", ";
|
||||||
printType(*I);
|
printType(FunctionInards, *I);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -737,10 +738,13 @@ void CWriter::printFunctionSignature(const Function *F, bool Prototype) {
|
||||||
// unless there are no known types, in which case, we just emit ().
|
// unless there are no known types, in which case, we just emit ().
|
||||||
//
|
//
|
||||||
if (FT->isVarArg() && !FT->getParamTypes().empty()) {
|
if (FT->isVarArg() && !FT->getParamTypes().empty()) {
|
||||||
if (FT->getParamTypes().size()) Out << ", ";
|
if (FT->getParamTypes().size()) FunctionInards << ", ";
|
||||||
Out << "..."; // Output varargs portion of signature!
|
FunctionInards << "..."; // Output varargs portion of signature!
|
||||||
}
|
}
|
||||||
Out << ")";
|
FunctionInards << ")";
|
||||||
|
// Print out the return type and the entire signature for that matter
|
||||||
|
printType(Out, F->getReturnType(), FunctionInards.str());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -756,7 +760,7 @@ void CWriter::printFunction(Function *F) {
|
||||||
for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
|
for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
|
||||||
if ((*I)->getType() != Type::VoidTy && !isInlinableInst(**I)) {
|
if ((*I)->getType() != Type::VoidTy && !isInlinableInst(**I)) {
|
||||||
Out << " ";
|
Out << " ";
|
||||||
printType((*I)->getType(), getValueName(*I));
|
printType(Out, (*I)->getType(), getValueName(*I));
|
||||||
Out << ";\n";
|
Out << ";\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -914,7 +918,7 @@ void CWriter::visitBinaryOperator(Instruction &I) {
|
||||||
// binary instructions, shift instructions, setCond instructions.
|
// binary instructions, shift instructions, setCond instructions.
|
||||||
if (isa<PointerType>(I.getType())) {
|
if (isa<PointerType>(I.getType())) {
|
||||||
Out << "(";
|
Out << "(";
|
||||||
printType(I.getType());
|
printType(Out, I.getType());
|
||||||
Out << ")";
|
Out << ")";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -947,7 +951,7 @@ void CWriter::visitBinaryOperator(Instruction &I) {
|
||||||
|
|
||||||
void CWriter::visitCastInst(CastInst &I) {
|
void CWriter::visitCastInst(CastInst &I) {
|
||||||
Out << "(";
|
Out << "(";
|
||||||
printType(I.getType(), string(""),/*ignoreName*/false, /*namedContext*/false);
|
printType(Out, I.getType(), string(""),/*ignoreName*/false, /*namedContext*/false);
|
||||||
Out << ")";
|
Out << ")";
|
||||||
writeOperand(I.getOperand(0));
|
writeOperand(I.getOperand(0));
|
||||||
}
|
}
|
||||||
|
@ -973,9 +977,9 @@ void CWriter::visitCallInst(CallInst &I) {
|
||||||
|
|
||||||
void CWriter::visitMallocInst(MallocInst &I) {
|
void CWriter::visitMallocInst(MallocInst &I) {
|
||||||
Out << "(";
|
Out << "(";
|
||||||
printType(I.getType());
|
printType(Out, I.getType());
|
||||||
Out << ")malloc(sizeof(";
|
Out << ")malloc(sizeof(";
|
||||||
printType(I.getType()->getElementType());
|
printType(Out, I.getType()->getElementType());
|
||||||
Out << ")";
|
Out << ")";
|
||||||
|
|
||||||
if (I.isArrayAllocation()) {
|
if (I.isArrayAllocation()) {
|
||||||
|
@ -987,9 +991,9 @@ void CWriter::visitMallocInst(MallocInst &I) {
|
||||||
|
|
||||||
void CWriter::visitAllocaInst(AllocaInst &I) {
|
void CWriter::visitAllocaInst(AllocaInst &I) {
|
||||||
Out << "(";
|
Out << "(";
|
||||||
printType(I.getType());
|
printType(Out, I.getType());
|
||||||
Out << ") alloca(sizeof(";
|
Out << ") alloca(sizeof(";
|
||||||
printType(I.getType()->getElementType());
|
printType(Out, I.getType()->getElementType());
|
||||||
Out << ")";
|
Out << ")";
|
||||||
if (I.isArrayAllocation()) {
|
if (I.isArrayAllocation()) {
|
||||||
Out << " * " ;
|
Out << " * " ;
|
||||||
|
|
Loading…
Reference in New Issue