mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-06-20 10:24:12 +00:00
* Eliminate using decls
* Fix some styleistic things * Fix bug: CWriter/2002-05-16-NameCollide.ll git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@5988 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@ -2,7 +2,7 @@
|
|||||||
//
|
//
|
||||||
// This library converts LLVM code to C code, compilable by GCC.
|
// This library converts LLVM code to C code, compilable by GCC.
|
||||||
//
|
//
|
||||||
//===-----------------------------------------------------------------------==//
|
//===----------------------------------------------------------------------===//
|
||||||
#include "llvm/Assembly/CWriter.h"
|
#include "llvm/Assembly/CWriter.h"
|
||||||
#include "llvm/Constants.h"
|
#include "llvm/Constants.h"
|
||||||
#include "llvm/DerivedTypes.h"
|
#include "llvm/DerivedTypes.h"
|
||||||
@ -20,23 +20,19 @@
|
|||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <set>
|
#include <set>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
using std::string;
|
|
||||||
using std::map;
|
|
||||||
using std::ostream;
|
|
||||||
|
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
class CWriter : public Pass, public InstVisitor<CWriter> {
|
class CWriter : public Pass, public InstVisitor<CWriter> {
|
||||||
ostream &Out;
|
std::ostream &Out;
|
||||||
SlotCalculator *Table;
|
SlotCalculator *Table;
|
||||||
const Module *TheModule;
|
const Module *TheModule;
|
||||||
map<const Type *, string> TypeNames;
|
std::map<const Type *, std::string> TypeNames;
|
||||||
std::set<const Value*> MangledGlobals;
|
std::set<const Value*> MangledGlobals;
|
||||||
bool needsMalloc;
|
bool needsMalloc;
|
||||||
|
|
||||||
map<const ConstantFP *, unsigned> FPConstantMap;
|
std::map<const ConstantFP *, unsigned> FPConstantMap;
|
||||||
public:
|
public:
|
||||||
CWriter(ostream &o) : Out(o) {}
|
CWriter(std::ostream &o) : Out(o) {}
|
||||||
|
|
||||||
void getAnalysisUsage(AnalysisUsage &AU) const {
|
void getAnalysisUsage(AnalysisUsage &AU) const {
|
||||||
AU.setPreservesAll();
|
AU.setPreservesAll();
|
||||||
@ -61,13 +57,14 @@ namespace {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
ostream &printType(std::ostream &Out, const Type *Ty, const string &VariableName = "",
|
std::ostream &printType(std::ostream &Out, const Type *Ty,
|
||||||
|
const std::string &VariableName = "",
|
||||||
bool IgnoreName = false, bool namedContext = true);
|
bool IgnoreName = false, bool namedContext = true);
|
||||||
|
|
||||||
void writeOperand(Value *Operand);
|
void writeOperand(Value *Operand);
|
||||||
void writeOperandInternal(Value *Operand);
|
void writeOperandInternal(Value *Operand);
|
||||||
|
|
||||||
string getValueName(const Value *V);
|
std::string getValueName(const Value *V);
|
||||||
|
|
||||||
private :
|
private :
|
||||||
bool nameAllUsedStructureTypes(Module &M);
|
bool nameAllUsedStructureTypes(Module &M);
|
||||||
@ -137,9 +134,9 @@ namespace {
|
|||||||
|
|
||||||
// We dont want identifier names with ., space, - in them.
|
// We dont want identifier names with ., space, - in them.
|
||||||
// So we replace them with _
|
// So we replace them with _
|
||||||
static string makeNameProper(string x) {
|
static std::string makeNameProper(std::string x) {
|
||||||
string tmp;
|
std::string tmp;
|
||||||
for (string::iterator sI = x.begin(), sEnd = x.end(); sI != sEnd; sI++)
|
for (std::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;
|
||||||
@ -150,11 +147,11 @@ static string makeNameProper(string x) {
|
|||||||
return tmp;
|
return tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
string CWriter::getValueName(const Value *V) {
|
std::string CWriter::getValueName(const Value *V) {
|
||||||
if (V->hasName()) { // Print out the label if it exists...
|
if (V->hasName()) { // Print out the label if it exists...
|
||||||
if (isa<GlobalValue>(V) && // Do not mangle globals...
|
if (isa<GlobalValue>(V) && // Do not mangle globals...
|
||||||
cast<GlobalValue>(V)->hasExternalLinkage()) // Unless it's internal or
|
(cast<GlobalValue>(V)->hasExternalLinkage() &&// Unless it's internal or
|
||||||
//!MangledGlobals.count(V)) // Unless the name would collide if we don't
|
!MangledGlobals.count(V))) // Unless the name would collide if we don't
|
||||||
return makeNameProper(V->getName());
|
return makeNameProper(V->getName());
|
||||||
|
|
||||||
return "l" + utostr(V->getType()->getUniqueID()) + "_" +
|
return "l" + utostr(V->getType()->getUniqueID()) + "_" +
|
||||||
@ -167,14 +164,15 @@ string CWriter::getValueName(const Value *V) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// A pointer type should not use parens around *'s alone, e.g., (**)
|
// A pointer type should not use parens around *'s alone, e.g., (**)
|
||||||
inline bool ptrTypeNameNeedsParens(const string &NameSoFar) {
|
inline bool ptrTypeNameNeedsParens(const std::string &NameSoFar) {
|
||||||
return (NameSoFar.find_last_not_of('*') != std::string::npos);
|
return (NameSoFar.find_last_not_of('*') != std::string::npos);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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(std::ostream &Out, const Type *Ty, const string &NameSoFar,
|
std::ostream &CWriter::printType(std::ostream &Out, const Type *Ty,
|
||||||
|
const std::string &NameSoFar,
|
||||||
bool IgnoreName, bool namedContext) {
|
bool IgnoreName, bool namedContext) {
|
||||||
if (Ty->isPrimitiveType())
|
if (Ty->isPrimitiveType())
|
||||||
switch (Ty->getPrimitiveID()) {
|
switch (Ty->getPrimitiveID()) {
|
||||||
@ -197,7 +195,7 @@ ostream &CWriter::printType(std::ostream &Out, const Type *Ty, const string &Nam
|
|||||||
|
|
||||||
// Check to see if the type is named.
|
// Check to see if the type is named.
|
||||||
if (!IgnoreName || isa<OpaqueType>(Ty)) {
|
if (!IgnoreName || isa<OpaqueType>(Ty)) {
|
||||||
map<const Type *, string>::iterator I = TypeNames.find(Ty);
|
std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
|
||||||
if (I != TypeNames.end()) {
|
if (I != TypeNames.end()) {
|
||||||
return Out << I->second << " " << NameSoFar;
|
return Out << I->second << " " << NameSoFar;
|
||||||
}
|
}
|
||||||
@ -221,7 +219,7 @@ ostream &CWriter::printType(std::ostream &Out, const Type *Ty, const string &Nam
|
|||||||
FunctionInards << "...";
|
FunctionInards << "...";
|
||||||
}
|
}
|
||||||
FunctionInards << ")";
|
FunctionInards << ")";
|
||||||
string tstr = FunctionInards.str();
|
std::string tstr = FunctionInards.str();
|
||||||
printType(Out, MTy->getReturnType(), tstr);
|
printType(Out, MTy->getReturnType(), tstr);
|
||||||
return Out;
|
return Out;
|
||||||
}
|
}
|
||||||
@ -262,7 +260,7 @@ ostream &CWriter::printType(std::ostream &Out, const Type *Ty, const string &Nam
|
|||||||
|
|
||||||
case Type::OpaqueTyID: {
|
case Type::OpaqueTyID: {
|
||||||
static int Count = 0;
|
static int Count = 0;
|
||||||
string TyName = "struct opaque_" + itostr(Count++);
|
std::string TyName = "struct opaque_" + itostr(Count++);
|
||||||
assert(TypeNames.find(Ty) == TypeNames.end());
|
assert(TypeNames.find(Ty) == TypeNames.end());
|
||||||
TypeNames[Ty] = TyName;
|
TypeNames[Ty] = TyName;
|
||||||
return Out << TyName << " " << NameSoFar;
|
return Out << TyName << " " << NameSoFar;
|
||||||
@ -395,7 +393,7 @@ void CWriter::printConstant(Constant *CPV) {
|
|||||||
case Type::FloatTyID:
|
case Type::FloatTyID:
|
||||||
case Type::DoubleTyID: {
|
case Type::DoubleTyID: {
|
||||||
ConstantFP *FPC = cast<ConstantFP>(CPV);
|
ConstantFP *FPC = cast<ConstantFP>(CPV);
|
||||||
map<const ConstantFP *, unsigned>::iterator I = FPConstantMap.find(FPC);
|
std::map<const ConstantFP*, unsigned>::iterator I = FPConstantMap.find(FPC);
|
||||||
if (I != FPConstantMap.end()) {
|
if (I != FPConstantMap.end()) {
|
||||||
// Because of FP precision problems we must load from a stack allocated
|
// Because of FP precision problems we must load from a stack allocated
|
||||||
// value that holds the value in hex.
|
// value that holds the value in hex.
|
||||||
@ -501,8 +499,7 @@ bool CWriter::nameAllUsedStructureTypes(Module &M) {
|
|||||||
return Changed;
|
return Changed;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void generateAllocaDecl(ostream& Out)
|
static void generateAllocaDecl(std::ostream& Out) {
|
||||||
{
|
|
||||||
// On SunOS, we need to insert the alloca macro & proto for the builtin.
|
// On SunOS, we need to insert the alloca macro & proto for the builtin.
|
||||||
Out << "#ifdef sun\n"
|
Out << "#ifdef sun\n"
|
||||||
<< "extern void *__builtin_alloca(unsigned long);\n"
|
<< "extern void *__builtin_alloca(unsigned long);\n"
|
||||||
@ -516,7 +513,7 @@ void CWriter::printModule(Module *M) {
|
|||||||
// Calculate which global values have names that will collide when we throw
|
// Calculate which global values have names that will collide when we throw
|
||||||
// away type information.
|
// away type information.
|
||||||
{ // Scope to delete the FoundNames set when we are done with it...
|
{ // Scope to delete the FoundNames set when we are done with it...
|
||||||
std::set<string> FoundNames;
|
std::set<std::string> FoundNames;
|
||||||
for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
|
for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
|
||||||
if (I->hasName()) // If the global has a name...
|
if (I->hasName()) // If the global has a name...
|
||||||
if (FoundNames.count(I->getName())) // And the name is already used
|
if (FoundNames.count(I->getName())) // And the name is already used
|
||||||
@ -575,7 +572,8 @@ void CWriter::printModule(Module *M) {
|
|||||||
needsMalloc = true;
|
needsMalloc = true;
|
||||||
for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) {
|
for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) {
|
||||||
// If the function is external and the name collides don't print it.
|
// If the function is external and the name collides don't print it.
|
||||||
// Sometimes the bytecode likes to have multiple "declerations" for external functions
|
// Sometimes the bytecode likes to have multiple "declerations" for
|
||||||
|
// external functions
|
||||||
if (I->hasInternalLinkage() || !MangledGlobals.count(I)){
|
if (I->hasInternalLinkage() || !MangledGlobals.count(I)){
|
||||||
printFunctionSignature(I, true);
|
printFunctionSignature(I, true);
|
||||||
Out << ";\n";
|
Out << ";\n";
|
||||||
@ -642,7 +640,7 @@ void CWriter::printSymbolTable(const SymbolTable &ST) {
|
|||||||
Out << "/* Structure forward decls */\n";
|
Out << "/* Structure forward decls */\n";
|
||||||
for (; I != End; ++I)
|
for (; I != End; ++I)
|
||||||
if (const Type *STy = dyn_cast<StructType>(I->second)) {
|
if (const Type *STy = dyn_cast<StructType>(I->second)) {
|
||||||
string Name = "struct l_" + makeNameProper(I->first);
|
std::string Name = "struct l_" + makeNameProper(I->first);
|
||||||
Out << Name << ";\n";
|
Out << Name << ";\n";
|
||||||
TypeNames.insert(std::make_pair(STy, Name));
|
TypeNames.insert(std::make_pair(STy, Name));
|
||||||
}
|
}
|
||||||
@ -653,7 +651,7 @@ void CWriter::printSymbolTable(const SymbolTable &ST) {
|
|||||||
Out << "/* Typedefs */\n";
|
Out << "/* Typedefs */\n";
|
||||||
for (I = ST.type_begin(Type::TypeTy); I != End; ++I) {
|
for (I = ST.type_begin(Type::TypeTy); I != End; ++I) {
|
||||||
const Type *Ty = cast<Type>(I->second);
|
const Type *Ty = cast<Type>(I->second);
|
||||||
string Name = "l_" + makeNameProper(I->first);
|
std::string Name = "l_" + makeNameProper(I->first);
|
||||||
Out << "typedef ";
|
Out << "typedef ";
|
||||||
printType(Out, Ty, Name);
|
printType(Out, Ty, Name);
|
||||||
Out << ";\n";
|
Out << ";\n";
|
||||||
@ -691,7 +689,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];
|
std::string Name = TypeNames[STy];
|
||||||
printType(Out, STy, Name, true);
|
printType(Out, STy, Name, true);
|
||||||
Out << ";\n\n";
|
Out << ";\n\n";
|
||||||
}
|
}
|
||||||
@ -721,7 +719,7 @@ void CWriter::printFunctionSignature(const Function *F, bool Prototype) {
|
|||||||
|
|
||||||
if (!F->isExternal()) {
|
if (!F->isExternal()) {
|
||||||
if (!F->aempty()) {
|
if (!F->aempty()) {
|
||||||
string ArgName;
|
std::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(FunctionInards, F->afront().getType(), ArgName);
|
||||||
@ -899,14 +897,14 @@ void CWriter::printBranchToBlock(BasicBlock *CurBB, BasicBlock *Succ,
|
|||||||
for (BasicBlock::iterator I = Succ->begin();
|
for (BasicBlock::iterator I = Succ->begin();
|
||||||
PHINode *PN = dyn_cast<PHINode>(I); ++I) {
|
PHINode *PN = dyn_cast<PHINode>(I); ++I) {
|
||||||
// now we have to do the printing
|
// now we have to do the printing
|
||||||
Out << string(Indent, ' ');
|
Out << std::string(Indent, ' ');
|
||||||
outputLValue(PN);
|
outputLValue(PN);
|
||||||
writeOperand(PN->getIncomingValue(PN->getBasicBlockIndex(CurBB)));
|
writeOperand(PN->getIncomingValue(PN->getBasicBlockIndex(CurBB)));
|
||||||
Out << "; /* for PHI node */\n";
|
Out << "; /* for PHI node */\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (CurBB->getNext() != Succ) {
|
if (CurBB->getNext() != Succ) {
|
||||||
Out << string(Indent, ' ') << " goto ";
|
Out << std::string(Indent, ' ') << " goto ";
|
||||||
writeOperand(Succ);
|
writeOperand(Succ);
|
||||||
Out << ";\n";
|
Out << ";\n";
|
||||||
}
|
}
|
||||||
@ -976,8 +974,7 @@ void CWriter::visitBinaryOperator(Instruction &I) {
|
|||||||
|
|
||||||
void CWriter::visitCastInst(CastInst &I) {
|
void CWriter::visitCastInst(CastInst &I) {
|
||||||
Out << "(";
|
Out << "(";
|
||||||
printType(Out, I.getType(), string(""),/*ignoreName*/false,
|
printType(Out, I.getType(), "", /*ignoreName*/false, /*namedContext*/false);
|
||||||
/*namedContext*/false);
|
|
||||||
Out << ")";
|
Out << ")";
|
||||||
if (isa<PointerType>(I.getType())&&I.getOperand(0)->getType()->isIntegral() ||
|
if (isa<PointerType>(I.getType())&&I.getOperand(0)->getType()->isIntegral() ||
|
||||||
isa<PointerType>(I.getOperand(0)->getType())&&I.getType()->isIntegral()) {
|
isa<PointerType>(I.getOperand(0)->getType())&&I.getType()->isIntegral()) {
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
//
|
//
|
||||||
// This library converts LLVM code to C code, compilable by GCC.
|
// This library converts LLVM code to C code, compilable by GCC.
|
||||||
//
|
//
|
||||||
//===-----------------------------------------------------------------------==//
|
//===----------------------------------------------------------------------===//
|
||||||
#include "llvm/Assembly/CWriter.h"
|
#include "llvm/Assembly/CWriter.h"
|
||||||
#include "llvm/Constants.h"
|
#include "llvm/Constants.h"
|
||||||
#include "llvm/DerivedTypes.h"
|
#include "llvm/DerivedTypes.h"
|
||||||
@ -20,23 +20,19 @@
|
|||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <set>
|
#include <set>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
using std::string;
|
|
||||||
using std::map;
|
|
||||||
using std::ostream;
|
|
||||||
|
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
class CWriter : public Pass, public InstVisitor<CWriter> {
|
class CWriter : public Pass, public InstVisitor<CWriter> {
|
||||||
ostream &Out;
|
std::ostream &Out;
|
||||||
SlotCalculator *Table;
|
SlotCalculator *Table;
|
||||||
const Module *TheModule;
|
const Module *TheModule;
|
||||||
map<const Type *, string> TypeNames;
|
std::map<const Type *, std::string> TypeNames;
|
||||||
std::set<const Value*> MangledGlobals;
|
std::set<const Value*> MangledGlobals;
|
||||||
bool needsMalloc;
|
bool needsMalloc;
|
||||||
|
|
||||||
map<const ConstantFP *, unsigned> FPConstantMap;
|
std::map<const ConstantFP *, unsigned> FPConstantMap;
|
||||||
public:
|
public:
|
||||||
CWriter(ostream &o) : Out(o) {}
|
CWriter(std::ostream &o) : Out(o) {}
|
||||||
|
|
||||||
void getAnalysisUsage(AnalysisUsage &AU) const {
|
void getAnalysisUsage(AnalysisUsage &AU) const {
|
||||||
AU.setPreservesAll();
|
AU.setPreservesAll();
|
||||||
@ -61,13 +57,14 @@ namespace {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
ostream &printType(std::ostream &Out, const Type *Ty, const string &VariableName = "",
|
std::ostream &printType(std::ostream &Out, const Type *Ty,
|
||||||
|
const std::string &VariableName = "",
|
||||||
bool IgnoreName = false, bool namedContext = true);
|
bool IgnoreName = false, bool namedContext = true);
|
||||||
|
|
||||||
void writeOperand(Value *Operand);
|
void writeOperand(Value *Operand);
|
||||||
void writeOperandInternal(Value *Operand);
|
void writeOperandInternal(Value *Operand);
|
||||||
|
|
||||||
string getValueName(const Value *V);
|
std::string getValueName(const Value *V);
|
||||||
|
|
||||||
private :
|
private :
|
||||||
bool nameAllUsedStructureTypes(Module &M);
|
bool nameAllUsedStructureTypes(Module &M);
|
||||||
@ -137,9 +134,9 @@ namespace {
|
|||||||
|
|
||||||
// We dont want identifier names with ., space, - in them.
|
// We dont want identifier names with ., space, - in them.
|
||||||
// So we replace them with _
|
// So we replace them with _
|
||||||
static string makeNameProper(string x) {
|
static std::string makeNameProper(std::string x) {
|
||||||
string tmp;
|
std::string tmp;
|
||||||
for (string::iterator sI = x.begin(), sEnd = x.end(); sI != sEnd; sI++)
|
for (std::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;
|
||||||
@ -150,11 +147,11 @@ static string makeNameProper(string x) {
|
|||||||
return tmp;
|
return tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
string CWriter::getValueName(const Value *V) {
|
std::string CWriter::getValueName(const Value *V) {
|
||||||
if (V->hasName()) { // Print out the label if it exists...
|
if (V->hasName()) { // Print out the label if it exists...
|
||||||
if (isa<GlobalValue>(V) && // Do not mangle globals...
|
if (isa<GlobalValue>(V) && // Do not mangle globals...
|
||||||
cast<GlobalValue>(V)->hasExternalLinkage()) // Unless it's internal or
|
(cast<GlobalValue>(V)->hasExternalLinkage() &&// Unless it's internal or
|
||||||
//!MangledGlobals.count(V)) // Unless the name would collide if we don't
|
!MangledGlobals.count(V))) // Unless the name would collide if we don't
|
||||||
return makeNameProper(V->getName());
|
return makeNameProper(V->getName());
|
||||||
|
|
||||||
return "l" + utostr(V->getType()->getUniqueID()) + "_" +
|
return "l" + utostr(V->getType()->getUniqueID()) + "_" +
|
||||||
@ -167,14 +164,15 @@ string CWriter::getValueName(const Value *V) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// A pointer type should not use parens around *'s alone, e.g., (**)
|
// A pointer type should not use parens around *'s alone, e.g., (**)
|
||||||
inline bool ptrTypeNameNeedsParens(const string &NameSoFar) {
|
inline bool ptrTypeNameNeedsParens(const std::string &NameSoFar) {
|
||||||
return (NameSoFar.find_last_not_of('*') != std::string::npos);
|
return (NameSoFar.find_last_not_of('*') != std::string::npos);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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(std::ostream &Out, const Type *Ty, const string &NameSoFar,
|
std::ostream &CWriter::printType(std::ostream &Out, const Type *Ty,
|
||||||
|
const std::string &NameSoFar,
|
||||||
bool IgnoreName, bool namedContext) {
|
bool IgnoreName, bool namedContext) {
|
||||||
if (Ty->isPrimitiveType())
|
if (Ty->isPrimitiveType())
|
||||||
switch (Ty->getPrimitiveID()) {
|
switch (Ty->getPrimitiveID()) {
|
||||||
@ -197,7 +195,7 @@ ostream &CWriter::printType(std::ostream &Out, const Type *Ty, const string &Nam
|
|||||||
|
|
||||||
// Check to see if the type is named.
|
// Check to see if the type is named.
|
||||||
if (!IgnoreName || isa<OpaqueType>(Ty)) {
|
if (!IgnoreName || isa<OpaqueType>(Ty)) {
|
||||||
map<const Type *, string>::iterator I = TypeNames.find(Ty);
|
std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
|
||||||
if (I != TypeNames.end()) {
|
if (I != TypeNames.end()) {
|
||||||
return Out << I->second << " " << NameSoFar;
|
return Out << I->second << " " << NameSoFar;
|
||||||
}
|
}
|
||||||
@ -221,7 +219,7 @@ ostream &CWriter::printType(std::ostream &Out, const Type *Ty, const string &Nam
|
|||||||
FunctionInards << "...";
|
FunctionInards << "...";
|
||||||
}
|
}
|
||||||
FunctionInards << ")";
|
FunctionInards << ")";
|
||||||
string tstr = FunctionInards.str();
|
std::string tstr = FunctionInards.str();
|
||||||
printType(Out, MTy->getReturnType(), tstr);
|
printType(Out, MTy->getReturnType(), tstr);
|
||||||
return Out;
|
return Out;
|
||||||
}
|
}
|
||||||
@ -262,7 +260,7 @@ ostream &CWriter::printType(std::ostream &Out, const Type *Ty, const string &Nam
|
|||||||
|
|
||||||
case Type::OpaqueTyID: {
|
case Type::OpaqueTyID: {
|
||||||
static int Count = 0;
|
static int Count = 0;
|
||||||
string TyName = "struct opaque_" + itostr(Count++);
|
std::string TyName = "struct opaque_" + itostr(Count++);
|
||||||
assert(TypeNames.find(Ty) == TypeNames.end());
|
assert(TypeNames.find(Ty) == TypeNames.end());
|
||||||
TypeNames[Ty] = TyName;
|
TypeNames[Ty] = TyName;
|
||||||
return Out << TyName << " " << NameSoFar;
|
return Out << TyName << " " << NameSoFar;
|
||||||
@ -395,7 +393,7 @@ void CWriter::printConstant(Constant *CPV) {
|
|||||||
case Type::FloatTyID:
|
case Type::FloatTyID:
|
||||||
case Type::DoubleTyID: {
|
case Type::DoubleTyID: {
|
||||||
ConstantFP *FPC = cast<ConstantFP>(CPV);
|
ConstantFP *FPC = cast<ConstantFP>(CPV);
|
||||||
map<const ConstantFP *, unsigned>::iterator I = FPConstantMap.find(FPC);
|
std::map<const ConstantFP*, unsigned>::iterator I = FPConstantMap.find(FPC);
|
||||||
if (I != FPConstantMap.end()) {
|
if (I != FPConstantMap.end()) {
|
||||||
// Because of FP precision problems we must load from a stack allocated
|
// Because of FP precision problems we must load from a stack allocated
|
||||||
// value that holds the value in hex.
|
// value that holds the value in hex.
|
||||||
@ -501,8 +499,7 @@ bool CWriter::nameAllUsedStructureTypes(Module &M) {
|
|||||||
return Changed;
|
return Changed;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void generateAllocaDecl(ostream& Out)
|
static void generateAllocaDecl(std::ostream& Out) {
|
||||||
{
|
|
||||||
// On SunOS, we need to insert the alloca macro & proto for the builtin.
|
// On SunOS, we need to insert the alloca macro & proto for the builtin.
|
||||||
Out << "#ifdef sun\n"
|
Out << "#ifdef sun\n"
|
||||||
<< "extern void *__builtin_alloca(unsigned long);\n"
|
<< "extern void *__builtin_alloca(unsigned long);\n"
|
||||||
@ -516,7 +513,7 @@ void CWriter::printModule(Module *M) {
|
|||||||
// Calculate which global values have names that will collide when we throw
|
// Calculate which global values have names that will collide when we throw
|
||||||
// away type information.
|
// away type information.
|
||||||
{ // Scope to delete the FoundNames set when we are done with it...
|
{ // Scope to delete the FoundNames set when we are done with it...
|
||||||
std::set<string> FoundNames;
|
std::set<std::string> FoundNames;
|
||||||
for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
|
for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
|
||||||
if (I->hasName()) // If the global has a name...
|
if (I->hasName()) // If the global has a name...
|
||||||
if (FoundNames.count(I->getName())) // And the name is already used
|
if (FoundNames.count(I->getName())) // And the name is already used
|
||||||
@ -575,7 +572,8 @@ void CWriter::printModule(Module *M) {
|
|||||||
needsMalloc = true;
|
needsMalloc = true;
|
||||||
for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) {
|
for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) {
|
||||||
// If the function is external and the name collides don't print it.
|
// If the function is external and the name collides don't print it.
|
||||||
// Sometimes the bytecode likes to have multiple "declerations" for external functions
|
// Sometimes the bytecode likes to have multiple "declerations" for
|
||||||
|
// external functions
|
||||||
if (I->hasInternalLinkage() || !MangledGlobals.count(I)){
|
if (I->hasInternalLinkage() || !MangledGlobals.count(I)){
|
||||||
printFunctionSignature(I, true);
|
printFunctionSignature(I, true);
|
||||||
Out << ";\n";
|
Out << ";\n";
|
||||||
@ -642,7 +640,7 @@ void CWriter::printSymbolTable(const SymbolTable &ST) {
|
|||||||
Out << "/* Structure forward decls */\n";
|
Out << "/* Structure forward decls */\n";
|
||||||
for (; I != End; ++I)
|
for (; I != End; ++I)
|
||||||
if (const Type *STy = dyn_cast<StructType>(I->second)) {
|
if (const Type *STy = dyn_cast<StructType>(I->second)) {
|
||||||
string Name = "struct l_" + makeNameProper(I->first);
|
std::string Name = "struct l_" + makeNameProper(I->first);
|
||||||
Out << Name << ";\n";
|
Out << Name << ";\n";
|
||||||
TypeNames.insert(std::make_pair(STy, Name));
|
TypeNames.insert(std::make_pair(STy, Name));
|
||||||
}
|
}
|
||||||
@ -653,7 +651,7 @@ void CWriter::printSymbolTable(const SymbolTable &ST) {
|
|||||||
Out << "/* Typedefs */\n";
|
Out << "/* Typedefs */\n";
|
||||||
for (I = ST.type_begin(Type::TypeTy); I != End; ++I) {
|
for (I = ST.type_begin(Type::TypeTy); I != End; ++I) {
|
||||||
const Type *Ty = cast<Type>(I->second);
|
const Type *Ty = cast<Type>(I->second);
|
||||||
string Name = "l_" + makeNameProper(I->first);
|
std::string Name = "l_" + makeNameProper(I->first);
|
||||||
Out << "typedef ";
|
Out << "typedef ";
|
||||||
printType(Out, Ty, Name);
|
printType(Out, Ty, Name);
|
||||||
Out << ";\n";
|
Out << ";\n";
|
||||||
@ -691,7 +689,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];
|
std::string Name = TypeNames[STy];
|
||||||
printType(Out, STy, Name, true);
|
printType(Out, STy, Name, true);
|
||||||
Out << ";\n\n";
|
Out << ";\n\n";
|
||||||
}
|
}
|
||||||
@ -721,7 +719,7 @@ void CWriter::printFunctionSignature(const Function *F, bool Prototype) {
|
|||||||
|
|
||||||
if (!F->isExternal()) {
|
if (!F->isExternal()) {
|
||||||
if (!F->aempty()) {
|
if (!F->aempty()) {
|
||||||
string ArgName;
|
std::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(FunctionInards, F->afront().getType(), ArgName);
|
||||||
@ -899,14 +897,14 @@ void CWriter::printBranchToBlock(BasicBlock *CurBB, BasicBlock *Succ,
|
|||||||
for (BasicBlock::iterator I = Succ->begin();
|
for (BasicBlock::iterator I = Succ->begin();
|
||||||
PHINode *PN = dyn_cast<PHINode>(I); ++I) {
|
PHINode *PN = dyn_cast<PHINode>(I); ++I) {
|
||||||
// now we have to do the printing
|
// now we have to do the printing
|
||||||
Out << string(Indent, ' ');
|
Out << std::string(Indent, ' ');
|
||||||
outputLValue(PN);
|
outputLValue(PN);
|
||||||
writeOperand(PN->getIncomingValue(PN->getBasicBlockIndex(CurBB)));
|
writeOperand(PN->getIncomingValue(PN->getBasicBlockIndex(CurBB)));
|
||||||
Out << "; /* for PHI node */\n";
|
Out << "; /* for PHI node */\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (CurBB->getNext() != Succ) {
|
if (CurBB->getNext() != Succ) {
|
||||||
Out << string(Indent, ' ') << " goto ";
|
Out << std::string(Indent, ' ') << " goto ";
|
||||||
writeOperand(Succ);
|
writeOperand(Succ);
|
||||||
Out << ";\n";
|
Out << ";\n";
|
||||||
}
|
}
|
||||||
@ -976,8 +974,7 @@ void CWriter::visitBinaryOperator(Instruction &I) {
|
|||||||
|
|
||||||
void CWriter::visitCastInst(CastInst &I) {
|
void CWriter::visitCastInst(CastInst &I) {
|
||||||
Out << "(";
|
Out << "(";
|
||||||
printType(Out, I.getType(), string(""),/*ignoreName*/false,
|
printType(Out, I.getType(), "", /*ignoreName*/false, /*namedContext*/false);
|
||||||
/*namedContext*/false);
|
|
||||||
Out << ")";
|
Out << ")";
|
||||||
if (isa<PointerType>(I.getType())&&I.getOperand(0)->getType()->isIntegral() ||
|
if (isa<PointerType>(I.getType())&&I.getOperand(0)->getType()->isIntegral() ||
|
||||||
isa<PointerType>(I.getOperand(0)->getType())&&I.getType()->isIntegral()) {
|
isa<PointerType>(I.getOperand(0)->getType())&&I.getType()->isIntegral()) {
|
||||||
|
Reference in New Issue
Block a user