using std::map;
using std::ostream;
-static std::string getConstStrValue(const Constant* CPV);
-
-
-static std::string getConstArrayStrValue(const Constant* CPV) {
- std::string Result;
-
- // As a special case, print the array as a string if it is an array of
- // ubytes or an array of sbytes with positive values.
- //
- const Type *ETy = cast<ArrayType>(CPV->getType())->getElementType();
- bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
-
- // Make sure the last character is a null char, as automatically added by C
- if (CPV->getNumOperands() == 0 ||
- !cast<Constant>(*(CPV->op_end()-1))->isNullValue())
- isString = false;
-
- if (isString) {
- Result = "\"";
- // Do not include the last character, which we know is null
- for (unsigned i = 0, e = CPV->getNumOperands()-1; i != e; ++i) {
- unsigned char C = (ETy == Type::SByteTy) ?
- (unsigned char)cast<ConstantSInt>(CPV->getOperand(i))->getValue() :
- (unsigned char)cast<ConstantUInt>(CPV->getOperand(i))->getValue();
-
- if (isprint(C)) {
- Result += C;
- } else {
- switch (C) {
- case '\n': Result += "\\n"; break;
- case '\t': Result += "\\t"; break;
- case '\r': Result += "\\r"; break;
- case '\v': Result += "\\v"; break;
- case '\a': Result += "\\a"; break;
- default:
- Result += "\\x";
- Result += ( C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A');
- Result += ((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A');
- break;
- }
- }
- }
- Result += "\"";
- } else {
- Result = "{";
- if (CPV->getNumOperands()) {
- Result += " " + getConstStrValue(cast<Constant>(CPV->getOperand(0)));
- for (unsigned i = 1; i < CPV->getNumOperands(); i++)
- Result += ", " + getConstStrValue(cast<Constant>(CPV->getOperand(i)));
- }
- Result += " }";
- }
-
- return Result;
-}
-
-static std::string getConstStrValue(const Constant* CPV) {
- switch (CPV->getType()->getPrimitiveID()) {
- case Type::BoolTyID: return CPV == ConstantBool::False ? "0" : "1";
- case Type::SByteTyID:
- case Type::ShortTyID:
- case Type::IntTyID: return itostr(cast<ConstantSInt>(CPV)->getValue());
- case Type::LongTyID: return itostr(cast<ConstantSInt>(CPV)->getValue())+"ll";
-
- case Type::UByteTyID:
- case Type::UShortTyID:return utostr(cast<ConstantUInt>(CPV)->getValue());
- case Type::UIntTyID: return utostr(cast<ConstantUInt>(CPV)->getValue())+"u";
- case Type::ULongTyID:return utostr(cast<ConstantUInt>(CPV)->getValue())+"ull";
-
- case Type::FloatTyID:
- case Type::DoubleTyID: return ftostr(cast<ConstantFP>(CPV)->getValue());
-
- case Type::ArrayTyID: return getConstArrayStrValue(CPV);
-
- case Type::StructTyID: {
- std::string Result = "{";
- if (CPV->getNumOperands()) {
- Result += " " + getConstStrValue(cast<Constant>(CPV->getOperand(0)));
- for (unsigned i = 1; i < CPV->getNumOperands(); i++)
- Result += ", " + getConstStrValue(cast<Constant>(CPV->getOperand(i)));
- }
- return Result + " }";
- }
-
- default:
- std::cerr << "Unknown constant type: " << CPV << "\n";
- abort();
- return "";
- }
-}
-
-// Pass the Type* variable and and the variable name and this prints out the
-// variable declaration.
-//
-static string calcTypeNameVar(const Type *Ty,
- map<const Type *, string> &TypeNames,
- const string &NameSoFar, bool ignoreName = false){
- if (Ty->isPrimitiveType())
- switch (Ty->getPrimitiveID()) {
- case Type::VoidTyID: return "void " + NameSoFar;
- case Type::BoolTyID: return "bool " + NameSoFar;
- case Type::UByteTyID: return "unsigned char " + NameSoFar;
- case Type::SByteTyID: return "signed char " + NameSoFar;
- case Type::UShortTyID: return "unsigned short " + NameSoFar;
- case Type::ShortTyID: return "short " + NameSoFar;
- case Type::UIntTyID: return "unsigned " + NameSoFar;
- case Type::IntTyID: return "int " + NameSoFar;
- case Type::ULongTyID: return "unsigned long long " + NameSoFar;
- case Type::LongTyID: return "signed long long " + NameSoFar;
- case Type::FloatTyID: return "float " + NameSoFar;
- case Type::DoubleTyID: return "double " + NameSoFar;
- default :
- std::cerr << "Unknown primitive type: " << Ty << "\n";
- abort();
- }
-
- // Check to see if the type is named.
- if (!ignoreName) {
- map<const Type *, string>::iterator I = TypeNames.find(Ty);
- if (I != TypeNames.end())
- return I->second + " " + NameSoFar;
- }
-
- string Result;
- switch (Ty->getPrimitiveID()) {
- case Type::FunctionTyID: {
- const FunctionType *MTy = cast<FunctionType>(Ty);
- Result += calcTypeNameVar(MTy->getReturnType(), TypeNames, "");
- Result += " " + NameSoFar + " (";
- for (FunctionType::ParamTypes::const_iterator
- I = MTy->getParamTypes().begin(),
- E = MTy->getParamTypes().end(); I != E; ++I) {
- if (I != MTy->getParamTypes().begin())
- Result += ", ";
- Result += calcTypeNameVar(*I, TypeNames, "");
- }
- if (MTy->isVarArg()) {
- if (!MTy->getParamTypes().empty())
- Result += ", ";
- Result += "...";
- }
- return Result + ")";
- }
- case Type::StructTyID: {
- const StructType *STy = cast<const StructType>(Ty);
- Result = NameSoFar + " {\n";
- unsigned indx = 0;
- for (StructType::ElementTypes::const_iterator
- I = STy->getElementTypes().begin(),
- E = STy->getElementTypes().end(); I != E; ++I) {
- Result += " " +calcTypeNameVar(*I, TypeNames, "field" + utostr(indx++));
- Result += ";\n";
- }
- return Result + "}";
- }
-
- case Type::PointerTyID:
- return calcTypeNameVar(cast<const PointerType>(Ty)->getElementType(),
- TypeNames, "*" + NameSoFar);
-
- case Type::ArrayTyID: {
- const ArrayType *ATy = cast<const ArrayType>(Ty);
- int NumElements = ATy->getNumElements();
- return calcTypeNameVar(ATy->getElementType(), TypeNames,
- NameSoFar + "[" + itostr(NumElements) + "]");
- }
- default:
- assert(0 && "Unhandled case in getTypeProps!");
- abort();
- }
-
- return Result;
-}
-
namespace {
class CWriter : public InstVisitor<CWriter> {
ostream& Out;
inline void write(Module *M) { printModule(M); }
- ostream& printType(const Type *Ty, const string &VariableName = "") {
- return Out << calcTypeNameVar(Ty, TypeNames, VariableName);
- }
+ ostream &printType(const Type *Ty, const string &VariableName = "",
+ bool IgnoreName = false);
void writeOperand(Value *Operand);
void writeOperandInternal(Value *Operand);
void printFunction(Function *);
+ void printConstant(Constant *CPV);
+ void printConstantArray(ConstantArray *CPA);
+
// isInlinableInst - Attempt to inline instructions into their uses to build
// trees as much as possible. To do this, we have to consistently decide
// what is acceptable to inline, so that variable declarations don't get
void visitBranchInst(BranchInst &I);
void visitPHINode(PHINode &I) {}
- void visitNot(GenericUnaryInst &I);
void visitBinaryOperator(Instruction &I);
void visitCastInst (CastInst &I);
}
void printBranchToBlock(BasicBlock *CurBlock, BasicBlock *SuccBlock,
unsigned Indent);
- void printIndexingExpr(MemAccessInst &MAI);
+ void printIndexingExpression(Value *Ptr, User::op_iterator I,
+ User::op_iterator E);
};
}
return "ltmp_" + itostr(Slot) + "_" + utostr(V->getType()->getUniqueID());
}
-void CWriter::writeOperandInternal(Value *Operand) {
- if (Operand->hasName()) {
- Out << getValueName(Operand);
- } else if (Constant *CPV = dyn_cast<Constant>(Operand)) {
+// Pass the Type* and the variable name and this prints out the variable
+// declaration.
+//
+ostream &CWriter::printType(const Type *Ty, const string &NameSoFar,
+ bool IgnoreName = false) {
+ if (Ty->isPrimitiveType())
+ switch (Ty->getPrimitiveID()) {
+ case Type::VoidTyID: return Out << "void " << NameSoFar;
+ case Type::BoolTyID: return Out << "bool " << NameSoFar;
+ case Type::UByteTyID: return Out << "unsigned char " << NameSoFar;
+ case Type::SByteTyID: return Out << "signed char " << NameSoFar;
+ case Type::UShortTyID: return Out << "unsigned short " << NameSoFar;
+ case Type::ShortTyID: return Out << "short " << NameSoFar;
+ case Type::UIntTyID: return Out << "unsigned " << NameSoFar;
+ case Type::IntTyID: return Out << "int " << NameSoFar;
+ case Type::ULongTyID: return Out << "unsigned long long " << NameSoFar;
+ case Type::LongTyID: return Out << "signed long long " << NameSoFar;
+ case Type::FloatTyID: return Out << "float " << NameSoFar;
+ case Type::DoubleTyID: return Out << "double " << NameSoFar;
+ default :
+ std::cerr << "Unknown primitive type: " << Ty << "\n";
+ abort();
+ }
+
+ // Check to see if the type is named.
+ if (!IgnoreName) {
+ map<const Type *, string>::iterator I = TypeNames.find(Ty);
+ if (I != TypeNames.end()) {
+ return Out << I->second << " " << NameSoFar;
+ }
+ }
+
+ switch (Ty->getPrimitiveID()) {
+ case Type::FunctionTyID: {
+ const FunctionType *MTy = cast<FunctionType>(Ty);
+ printType(MTy->getReturnType(), "");
+ Out << " " << NameSoFar << " (";
+
+ for (FunctionType::ParamTypes::const_iterator
+ I = MTy->getParamTypes().begin(),
+ E = MTy->getParamTypes().end(); I != E; ++I) {
+ if (I != MTy->getParamTypes().begin())
+ Out << ", ";
+ printType(*I, "");
+ }
+ if (MTy->isVarArg()) {
+ if (!MTy->getParamTypes().empty())
+ Out << ", ";
+ Out << "...";
+ }
+ return Out << ")";
+ }
+ case Type::StructTyID: {
+ const StructType *STy = cast<StructType>(Ty);
+ Out << NameSoFar + " {\n";
+ unsigned Idx = 0;
+ for (StructType::ElementTypes::const_iterator
+ I = STy->getElementTypes().begin(),
+ E = STy->getElementTypes().end(); I != E; ++I) {
+ Out << " ";
+ printType(*I, "field" + utostr(Idx++));
+ Out << ";\n";
+ }
+ return Out << "}";
+ }
+
+ case Type::PointerTyID: {
+ const PointerType *PTy = cast<PointerType>(Ty);
+ return printType(PTy->getElementType(), "(*" + NameSoFar + ")");
+ }
+
+ case Type::ArrayTyID: {
+ const ArrayType *ATy = cast<ArrayType>(Ty);
+ unsigned NumElements = ATy->getNumElements();
+ return printType(ATy->getElementType(),
+ NameSoFar + "[" + utostr(NumElements) + "]");
+ }
+ default:
+ assert(0 && "Unhandled case in getTypeProps!");
+ abort();
+ }
+
+ return Out;
+}
+
+void CWriter::printConstantArray(ConstantArray *CPA) {
+
+ // As a special case, print the array as a string if it is an array of
+ // ubytes or an array of sbytes with positive values.
+ //
+ const Type *ETy = CPA->getType()->getElementType();
+ bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
+
+ // Make sure the last character is a null char, as automatically added by C
+ if (CPA->getNumOperands() == 0 ||
+ !cast<Constant>(*(CPA->op_end()-1))->isNullValue())
+ isString = false;
+
+ if (isString) {
+ Out << "\"";
+ // Do not include the last character, which we know is null
+ for (unsigned i = 0, e = CPA->getNumOperands()-1; i != e; ++i) {
+ unsigned char C = (ETy == Type::SByteTy) ?
+ (unsigned char)cast<ConstantSInt>(CPA->getOperand(i))->getValue() :
+ (unsigned char)cast<ConstantUInt>(CPA->getOperand(i))->getValue();
+
+ if (isprint(C)) {
+ Out << C;
+ } else {
+ switch (C) {
+ case '\n': Out << "\\n"; break;
+ case '\t': Out << "\\t"; break;
+ case '\r': Out << "\\r"; break;
+ case '\v': Out << "\\v"; break;
+ case '\a': Out << "\\a"; break;
+ default:
+ Out << "\\x";
+ Out << ( C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A');
+ Out << ((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A');
+ break;
+ }
+ }
+ }
+ Out << "\"";
+ } else {
+ Out << "{";
+ if (CPA->getNumOperands()) {
+ Out << " ";
+ printConstant(cast<Constant>(CPA->getOperand(0)));
+ for (unsigned i = 1, e = CPA->getNumOperands(); i != e; ++i) {
+ Out << ", ";
+ printConstant(cast<Constant>(CPA->getOperand(i)));
+ }
+ }
+ Out << " }";
+ }
+}
+
+
+// printConstant - The LLVM Constant to C Constant converter.
+void CWriter::printConstant(Constant *CPV) {
+ if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
+ switch (CE->getOpcode()) {
+ case Instruction::Cast:
+ Out << "((";
+ printType(CPV->getType());
+ Out << ")";
+ printConstant(cast<Constant>(CPV->getOperand(0)));
+ Out << ")";
+ return;
+
+ case Instruction::GetElementPtr:
+ Out << "&(";
+ printIndexingExpression(CPV->getOperand(0),
+ CPV->op_begin()+1, CPV->op_end());
+ Out << ")";
+ return;
+ case Instruction::Add:
+ Out << "(";
+ printConstant(cast<Constant>(CPV->getOperand(0)));
+ Out << " + ";
+ printConstant(cast<Constant>(CPV->getOperand(1)));
+ Out << ")";
+ return;
+ case Instruction::Sub:
+ Out << "(";
+ printConstant(cast<Constant>(CPV->getOperand(0)));
+ Out << " - ";
+ printConstant(cast<Constant>(CPV->getOperand(1)));
+ Out << ")";
+ return;
+
+ default:
+ std::cerr << "CWriter Error: Unhandled constant expression: "
+ << CE << "\n";
+ abort();
+ }
+ }
+
+ switch (CPV->getType()->getPrimitiveID()) {
+ case Type::BoolTyID:
+ Out << (CPV == ConstantBool::False ? "0" : "1"); break;
+ case Type::SByteTyID:
+ case Type::ShortTyID:
+ case Type::IntTyID:
+ Out << cast<ConstantSInt>(CPV)->getValue(); break;
+ case Type::LongTyID:
+ Out << cast<ConstantSInt>(CPV)->getValue() << "ll"; break;
+
+ case Type::UByteTyID:
+ case Type::UShortTyID:
+ Out << cast<ConstantUInt>(CPV)->getValue(); break;
+ case Type::UIntTyID:
+ Out << cast<ConstantUInt>(CPV)->getValue() << "u"; break;
+ case Type::ULongTyID:
+ Out << cast<ConstantUInt>(CPV)->getValue() << "ull"; break;
+
+ case Type::FloatTyID:
+ case Type::DoubleTyID:
+ Out << cast<ConstantFP>(CPV)->getValue(); break;
+
+ case Type::ArrayTyID:
+ printConstantArray(cast<ConstantArray>(CPV));
+ break;
+
+ case Type::StructTyID: {
+ Out << "{";
+ if (CPV->getNumOperands()) {
+ Out << " ";
+ printConstant(cast<Constant>(CPV->getOperand(0)));
+ for (unsigned i = 1, e = CPV->getNumOperands(); i != e; ++i) {
+ Out << ", ";
+ printConstant(cast<Constant>(CPV->getOperand(i)));
+ }
+ }
+ Out << " }";
+ break;
+ }
+
+ case Type::PointerTyID:
if (isa<ConstantPointerNull>(CPV)) {
Out << "((";
printType(CPV->getType(), "");
Out << ")NULL)";
- } else
- Out << getConstStrValue(CPV);
- } else {
- int Slot = Table.getValSlot(Operand);
- assert(Slot >= 0 && "Malformed LLVM!");
- Out << "ltmp_" << Slot << "_" << Operand->getType()->getUniqueID();
+ break;
+ } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CPV)) {
+ writeOperand(CPR->getValue());
+ break;
+ }
+ // FALL THROUGH
+ default:
+ std::cerr << "Unknown constant type: " << CPV << "\n";
+ abort();
}
}
-void CWriter::writeOperand(Value *Operand) {
+void CWriter::writeOperandInternal(Value *Operand) {
if (Instruction *I = dyn_cast<Instruction>(Operand))
if (isInlinableInst(*I)) {
// Should we inline this instruction to build a tree?
Out << ")";
return;
}
+
+ if (Operand->hasName()) {
+ Out << getValueName(Operand);
+ } else if (Constant *CPV = dyn_cast<Constant>(Operand)) {
+ printConstant(CPV);
+ } else {
+ int Slot = Table.getValSlot(Operand);
+ assert(Slot >= 0 && "Malformed LLVM!");
+ Out << "ltmp_" << Slot << "_" << Operand->getType()->getUniqueID();
+ }
+}
+void CWriter::writeOperand(Value *Operand) {
if (isa<GlobalVariable>(Operand))
Out << "(&"; // Global variables are references as their addresses by llvm
<< "#ifndef NULL\n#define NULL 0\n#endif\n\n"
<< "typedef unsigned char bool;\n"
- << "\n\n/* Global Symbols */\n";
+ << "\n\n/* Global Declarations */\n";
+
+ // First output all the declarations for the program, because C requires
+ // Functions & globals to be declared before they are used.
+ //
// Loop over the symbol table, emitting all named constants...
if (M->hasSymbolTable())
printSymbolTable(*M->getSymbolTable());
- Out << "\n\n/* Global Data */\n";
- for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) {
- if (I->hasInternalLinkage()) Out << "static ";
- printType(I->getType()->getElementType(), getValueName(I));
+ // Global variable declarations...
+ if (!M->gempty()) {
+ Out << "\n/* Global Variable Declarations */\n";
+ for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) {
+ Out << (I->hasExternalLinkage() ? "extern " : "static ");
+ printType(I->getType()->getElementType(), getValueName(I));
+ Out << ";\n";
+ }
+ }
+
+ // Function declarations
+ if (!M->empty()) {
+ Out << "\n/* Function Declarations */\n";
+ for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
+ printFunctionDecl(I);
+ }
- if (I->hasInitializer()) {
- Out << " = " ;
- writeOperand(I->getInitializer());
+ // Output the global variable contents...
+ if (!M->gempty()) {
+ Out << "\n\n/* Global Data */\n";
+ for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) {
+ if (I->hasInternalLinkage()) Out << "static ";
+ printType(I->getType()->getElementType(), getValueName(I));
+
+ if (I->hasInitializer()) {
+ Out << " = " ;
+ writeOperand(I->getInitializer());
+ }
+ Out << ";\n";
}
- Out << ";\n";
}
- // First output all the declarations of the functions as C requires Functions
- // be declared before they are used.
- //
- Out << "\n\n/* Function Declarations */\n";
- for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
- printFunctionDecl(I);
-
// Output all of the functions...
- Out << "\n\n/* Function Bodies */\n";
- for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
- printFunction(I);
+ if (!M->empty()) {
+ Out << "\n\n/* Function Bodies */\n";
+ for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
+ printFunction(I);
+ }
}
for (; I != End; ++I)
if (const Type *Ty = dyn_cast<StructType>(I->second)) {
- string Name = "struct l_" + makeNameProper(I->first);
+ string Name = "struct l_" + makeNameProper(I->first);
Out << Name << ";\n";
-
TypeNames.insert(std::make_pair(Ty, Name));
}
}
else
Out << "typedef ";
- Out << calcTypeNameVar(Ty, TypeNames, Name, true) << ";\n";
+ printType(Ty, Name, true);
+ Out << ";\n";
}
}
}
}
-void CWriter::visitNot(GenericUnaryInst &I) {
- Out << "~";
- writeOperand(I.getOperand(0));
-}
-
void CWriter::visitBinaryOperator(Instruction &I) {
// binary instructions, shift instructions, setCond instructions.
if (isa<PointerType>(I.getType())) {
Out << ")";
}
-void CWriter::printIndexingExpr(MemAccessInst &MAI) {
- MemAccessInst::op_iterator I = MAI.idx_begin(), E = MAI.idx_end();
+void CWriter::printIndexingExpression(Value *Ptr, User::op_iterator I,
+ User::op_iterator E) {
+ bool HasImplicitAddress = false;
+ // If accessing a global value with no indexing, avoid *(&GV) syndrome
+ if (GlobalValue *V = dyn_cast<GlobalValue>(Ptr)) {
+ HasImplicitAddress = true;
+ } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Ptr)) {
+ HasImplicitAddress = true;
+ Ptr = CPR->getValue(); // Get to the global...
+ }
+
if (I == E) {
- // If accessing a global value with no indexing, avoid *(&GV) syndrome
- if (GlobalValue *V = dyn_cast<GlobalValue>(MAI.getPointerOperand())) {
- writeOperandInternal(V);
- return;
- }
+ if (!HasImplicitAddress)
+ Out << "*"; // Implicit zero first argument: '*x' is equivalent to 'x[0]'
- Out << "*"; // Implicit zero first argument: '*x' is equivalent to 'x[0]'
+ writeOperandInternal(Ptr);
+ return;
}
- writeOperand(MAI.getPointerOperand());
+ const Constant *CI = dyn_cast<Constant>(I->get());
+ if (HasImplicitAddress && (!CI || !CI->isNullValue()))
+ Out << "(&";
+
+ writeOperandInternal(Ptr);
- if (I == E) return;
+ if (HasImplicitAddress && (!CI || !CI->isNullValue()))
+ Out << ")";
// Print out the -> operator if possible...
- const Constant *CI = dyn_cast<Constant>(I->get());
- if (CI && CI->isNullValue() && I+1 != E &&
- (*(I+1))->getType() == Type::UByteTy) {
- Out << "->field" << cast<ConstantUInt>(*(I+1))->getValue();
- I += 2;
+ if (CI && CI->isNullValue() && I+1 != E) {
+ if ((*(I+1))->getType() == Type::UByteTy) {
+ Out << (HasImplicitAddress ? "." : "->");
+ Out << "field" << cast<ConstantUInt>(*(I+1))->getValue();
+ I += 2;
+ } else { // Performing array indexing. Just skip the 0
+ ++I;
+ }
+ } else if (HasImplicitAddress) {
+
}
for (; I != E; ++I)
}
void CWriter::visitLoadInst(LoadInst &I) {
- printIndexingExpr(I);
+ printIndexingExpression(I.getPointerOperand(), I.idx_begin(), I.idx_end());
}
void CWriter::visitStoreInst(StoreInst &I) {
- printIndexingExpr(I);
+ printIndexingExpression(I.getPointerOperand(), I.idx_begin(), I.idx_end());
Out << " = ";
writeOperand(I.getOperand(0));
}
void CWriter::visitGetElementPtrInst(GetElementPtrInst &I) {
Out << "&";
- printIndexingExpr(I);
+ printIndexingExpression(I.getPointerOperand(), I.idx_begin(), I.idx_end());
}
//===----------------------------------------------------------------------===//