//===-- Writer.cpp - Library for writing C files --------------------------===//
//
// This library implements the functionality defined in llvm/Assembly/CWriter.h
// and CLocalVars.h
//
// TODO : Recursive types.
//
//===-----------------------------------------------------------------------==//
#include "llvm/Assembly/CWriter.h"
#include "CLocalVars.h"
#include "llvm/SlotCalculator.h"
#include "llvm/Constants.h"
#include "llvm/DerivedTypes.h"
#include "llvm/Module.h"
#include "llvm/GlobalVariable.h"
#include "llvm/Function.h"
#include "llvm/Argument.h"
#include "llvm/BasicBlock.h"
#include "llvm/iMemory.h"
#include "llvm/iTerminators.h"
#include "llvm/iPHINode.h"
#include "llvm/iOther.h"
#include "llvm/iOperators.h"
#include "llvm/SymbolTable.h"
#include "llvm/Support/InstVisitor.h"
#include "Support/StringExtras.h"
#include "Support/STLExtras.h"
#include <algorithm>
#include <strstream>
using std::string;
using std::map;
using std::vector;
using std::ostream;
//===-----------------------------------------------------------------------==//
//
// Implementation of the CLocalVars methods
// Appends a variable to the LocalVars map if it does not already exist
// Also check that the type exists on the map.
void CLocalVars::addLocalVar(const Type *t, const string & var) {
if (!LocalVars.count(t) ||
find(LocalVars[t].begin(), LocalVars[t].end(), var)
== LocalVars[t].end()) {
LocalVars[t].push_back(var);
}
}
static string calcTypeNameVar(const Type *Ty,
map<const Type *, string> &TypeNames,
string VariableName, string NameSoFar);
static std::string getConstStrValue(const Constant* CPV);
// We dont want identifier names with ., space, - in them.
// So we replace them with _
static string makeNameProper(string x) {
string tmp;
for (string::iterator sI = x.begin(), sEnd = x.end(); sI != sEnd; sI++) {
if (*sI == '.')
tmp += '_';
else if (*sI == ' ')
tmp += '_';
else if (*sI == '-')
tmp += "__";
else
tmp += *sI;
}
return tmp;
}
static string getConstantName(const Constant *CPV) {
return CPV->getName();
}
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);
if (ETy == Type::SByteTy) {
for (unsigned i = 0; i < CPV->getNumOperands(); ++i)
if (ETy == Type::SByteTy &&
cast<ConstantSInt>(CPV->getOperand(i))->getValue() < 0) {
isString = false;
break;
}
}
if (isString)