2006-05-29 00:57:22 +00:00
|
|
|
//===-- CppWriter.cpp - Printing LLVM IR as a C++ Source File -------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:44:31 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2006-05-29 00:57:22 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the writing of the LLVM IR as a set of C++ calls to the
|
|
|
|
// LLVM IR interface. The input module is assumed to be verified.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/CallingConv.h"
|
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
|
|
|
#include "llvm/InlineAsm.h"
|
|
|
|
#include "llvm/Instruction.h"
|
|
|
|
#include "llvm/Instructions.h"
|
2007-04-11 09:54:08 +00:00
|
|
|
#include "llvm/ParameterAttributes.h"
|
2006-05-29 00:57:22 +00:00
|
|
|
#include "llvm/Module.h"
|
2007-01-06 07:24:44 +00:00
|
|
|
#include "llvm/TypeSymbolTable.h"
|
2006-05-29 00:57:22 +00:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2007-06-16 20:48:27 +00:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2006-05-30 21:18:23 +00:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2006-12-06 01:18:01 +00:00
|
|
|
#include "llvm/Support/CFG.h"
|
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
|
|
|
#include "llvm/Support/MathExtras.h"
|
2006-06-01 23:43:47 +00:00
|
|
|
#include "llvm/Config/config.h"
|
2006-05-29 00:57:22 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <iostream>
|
2006-05-30 03:43:49 +00:00
|
|
|
#include <set>
|
2006-05-29 00:57:22 +00:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2006-05-30 21:18:23 +00:00
|
|
|
static cl::opt<std::string>
|
|
|
|
FuncName("funcname", cl::desc("Specify the name of the generated function"),
|
|
|
|
cl::value_desc("function name"));
|
|
|
|
|
2006-05-31 17:31:38 +00:00
|
|
|
enum WhatToGenerate {
|
|
|
|
GenProgram,
|
|
|
|
GenModule,
|
|
|
|
GenContents,
|
|
|
|
GenFunction,
|
2007-11-13 18:22:33 +00:00
|
|
|
GenFunctions,
|
2006-06-01 23:43:47 +00:00
|
|
|
GenInline,
|
2006-05-31 17:31:38 +00:00
|
|
|
GenVariable,
|
|
|
|
GenType
|
|
|
|
};
|
|
|
|
|
|
|
|
static cl::opt<WhatToGenerate> GenerationType(cl::Optional,
|
|
|
|
cl::desc("Choose what kind of output to generate"),
|
|
|
|
cl::init(GenProgram),
|
|
|
|
cl::values(
|
2007-11-13 18:22:33 +00:00
|
|
|
clEnumValN(GenProgram, "gen-program", "Generate a complete program"),
|
|
|
|
clEnumValN(GenModule, "gen-module", "Generate a module definition"),
|
|
|
|
clEnumValN(GenContents, "gen-contents", "Generate contents of a module"),
|
|
|
|
clEnumValN(GenFunction, "gen-function", "Generate a function definition"),
|
|
|
|
clEnumValN(GenFunctions,"gen-functions", "Generate all function definitions"),
|
|
|
|
clEnumValN(GenInline, "gen-inline", "Generate an inline function"),
|
|
|
|
clEnumValN(GenVariable, "gen-variable", "Generate a variable definition"),
|
|
|
|
clEnumValN(GenType, "gen-type", "Generate a type definition"),
|
2006-05-31 17:31:38 +00:00
|
|
|
clEnumValEnd
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
static cl::opt<std::string> NameToGenerate("for", cl::Optional,
|
|
|
|
cl::desc("Specify the name of the thing to generate"),
|
|
|
|
cl::init("!bad!"));
|
2006-05-30 21:18:23 +00:00
|
|
|
|
2006-05-29 00:57:22 +00:00
|
|
|
namespace {
|
|
|
|
typedef std::vector<const Type*> TypeList;
|
|
|
|
typedef std::map<const Type*,std::string> TypeMap;
|
|
|
|
typedef std::map<const Value*,std::string> ValueMap;
|
2006-05-30 03:43:49 +00:00
|
|
|
typedef std::set<std::string> NameSet;
|
2006-05-30 21:18:23 +00:00
|
|
|
typedef std::set<const Type*> TypeSet;
|
|
|
|
typedef std::set<const Value*> ValueSet;
|
|
|
|
typedef std::map<const Value*,std::string> ForwardRefMap;
|
2006-05-29 00:57:22 +00:00
|
|
|
|
|
|
|
class CppWriter {
|
2006-05-31 17:31:38 +00:00
|
|
|
const char* progname;
|
2006-05-29 00:57:22 +00:00
|
|
|
std::ostream &Out;
|
|
|
|
const Module *TheModule;
|
2006-05-31 20:18:56 +00:00
|
|
|
uint64_t uniqueNum;
|
2006-05-29 00:57:22 +00:00
|
|
|
TypeMap TypeNames;
|
|
|
|
ValueMap ValueNames;
|
|
|
|
TypeMap UnresolvedTypes;
|
|
|
|
TypeList TypeStack;
|
2006-05-30 03:43:49 +00:00
|
|
|
NameSet UsedNames;
|
2006-05-30 21:18:23 +00:00
|
|
|
TypeSet DefinedTypes;
|
|
|
|
ValueSet DefinedValues;
|
|
|
|
ForwardRefMap ForwardRefs;
|
2006-06-01 23:43:47 +00:00
|
|
|
bool is_inline;
|
2006-05-29 00:57:22 +00:00
|
|
|
|
|
|
|
public:
|
2006-05-31 17:31:38 +00:00
|
|
|
inline CppWriter(std::ostream &o, const Module *M, const char* pn="llvm2cpp")
|
|
|
|
: progname(pn), Out(o), TheModule(M), uniqueNum(0), TypeNames(),
|
2006-06-01 23:43:47 +00:00
|
|
|
ValueNames(), UnresolvedTypes(), TypeStack(), is_inline(false) { }
|
2006-05-29 00:57:22 +00:00
|
|
|
|
|
|
|
const Module* getModule() { return TheModule; }
|
|
|
|
|
2006-05-31 17:31:38 +00:00
|
|
|
void printProgram(const std::string& fname, const std::string& modName );
|
|
|
|
void printModule(const std::string& fname, const std::string& modName );
|
|
|
|
void printContents(const std::string& fname, const std::string& modName );
|
|
|
|
void printFunction(const std::string& fname, const std::string& funcName );
|
2007-11-13 18:22:33 +00:00
|
|
|
void printFunctions();
|
2006-06-01 23:43:47 +00:00
|
|
|
void printInline(const std::string& fname, const std::string& funcName );
|
2006-05-31 17:31:38 +00:00
|
|
|
void printVariable(const std::string& fname, const std::string& varName );
|
|
|
|
void printType(const std::string& fname, const std::string& typeName );
|
|
|
|
|
|
|
|
void error(const std::string& msg);
|
2006-05-29 18:08:06 +00:00
|
|
|
|
|
|
|
private:
|
2006-05-29 00:57:22 +00:00
|
|
|
void printLinkageType(GlobalValue::LinkageTypes LT);
|
2007-11-27 13:23:08 +00:00
|
|
|
void printVisibilityType(GlobalValue::VisibilityTypes VisTypes);
|
2006-05-29 00:57:22 +00:00
|
|
|
void printCallingConv(unsigned cc);
|
2006-05-31 17:31:38 +00:00
|
|
|
void printEscapedString(const std::string& str);
|
|
|
|
void printCFP(const ConstantFP* CFP);
|
2006-05-29 00:57:22 +00:00
|
|
|
|
|
|
|
std::string getCppName(const Type* val);
|
|
|
|
inline void printCppName(const Type* val);
|
2006-05-30 21:18:23 +00:00
|
|
|
|
2006-05-31 17:31:38 +00:00
|
|
|
std::string getCppName(const Value* val);
|
|
|
|
inline void printCppName(const Value* val);
|
2006-05-30 21:18:23 +00:00
|
|
|
|
2007-11-27 13:23:08 +00:00
|
|
|
void printParamAttrs(const ParamAttrsList* PAL, const std::string &name);
|
2006-05-31 17:31:38 +00:00
|
|
|
bool printTypeInternal(const Type* Ty);
|
|
|
|
inline void printType(const Type* Ty);
|
|
|
|
void printTypes(const Module* M);
|
2006-05-30 21:18:23 +00:00
|
|
|
|
2006-05-31 17:31:38 +00:00
|
|
|
void printConstant(const Constant *CPV);
|
|
|
|
void printConstants(const Module* M);
|
2006-05-30 21:18:23 +00:00
|
|
|
|
2006-05-31 17:31:38 +00:00
|
|
|
void printVariableUses(const GlobalVariable *GV);
|
|
|
|
void printVariableHead(const GlobalVariable *GV);
|
|
|
|
void printVariableBody(const GlobalVariable *GV);
|
2006-05-30 21:18:23 +00:00
|
|
|
|
2006-05-31 17:31:38 +00:00
|
|
|
void printFunctionUses(const Function *F);
|
|
|
|
void printFunctionHead(const Function *F);
|
|
|
|
void printFunctionBody(const Function *F);
|
|
|
|
void printInstruction(const Instruction *I, const std::string& bbname);
|
|
|
|
std::string getOpName(Value*);
|
2006-05-30 21:18:23 +00:00
|
|
|
|
2006-05-31 17:31:38 +00:00
|
|
|
void printModuleBody();
|
2006-05-30 21:18:23 +00:00
|
|
|
|
2006-05-31 17:31:38 +00:00
|
|
|
};
|
2006-05-29 18:08:06 +00:00
|
|
|
|
2006-08-14 22:35:15 +00:00
|
|
|
static unsigned indent_level = 0;
|
|
|
|
inline std::ostream& nl(std::ostream& Out, int delta = 0) {
|
|
|
|
Out << "\n";
|
|
|
|
if (delta >= 0 || indent_level >= unsigned(-delta))
|
|
|
|
indent_level += delta;
|
|
|
|
for (unsigned i = 0; i < indent_level; ++i)
|
|
|
|
Out << " ";
|
|
|
|
return Out;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void in() { indent_level++; }
|
|
|
|
inline void out() { if (indent_level >0) indent_level--; }
|
|
|
|
|
2006-05-30 10:21:41 +00:00
|
|
|
inline void
|
|
|
|
sanitize(std::string& str) {
|
|
|
|
for (size_t i = 0; i < str.length(); ++i)
|
|
|
|
if (!isalnum(str[i]) && str[i] != '_')
|
|
|
|
str[i] = '_';
|
|
|
|
}
|
|
|
|
|
For PR1064:
Implement the arbitrary bit-width integer feature. The feature allows
integers of any bitwidth (up to 64) to be defined instead of just 1, 8,
16, 32, and 64 bit integers.
This change does several things:
1. Introduces a new Derived Type, IntegerType, to represent the number of
bits in an integer. The Type classes SubclassData field is used to
store the number of bits. This allows 2^23 bits in an integer type.
2. Removes the five integer Type::TypeID values for the 1, 8, 16, 32 and
64-bit integers. These are replaced with just IntegerType which is not
a primitive any more.
3. Adjust the rest of LLVM to account for this change.
Note that while this incremental change lays the foundation for arbitrary
bit-width integers, LLVM has not yet been converted to actually deal with
them in any significant way. Most optimization passes, for example, will
still only deal with the byte-width integer types. Future increments
will rectify this situation.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@33113 91177308-0d34-0410-b5e6-96231b3b80d8
2007-01-12 07:05:14 +00:00
|
|
|
inline std::string
|
2006-05-30 03:43:49 +00:00
|
|
|
getTypePrefix(const Type* Ty ) {
|
|
|
|
switch (Ty->getTypeID()) {
|
For PR1064:
Implement the arbitrary bit-width integer feature. The feature allows
integers of any bitwidth (up to 64) to be defined instead of just 1, 8,
16, 32, and 64 bit integers.
This change does several things:
1. Introduces a new Derived Type, IntegerType, to represent the number of
bits in an integer. The Type classes SubclassData field is used to
store the number of bits. This allows 2^23 bits in an integer type.
2. Removes the five integer Type::TypeID values for the 1, 8, 16, 32 and
64-bit integers. These are replaced with just IntegerType which is not
a primitive any more.
3. Adjust the rest of LLVM to account for this change.
Note that while this incremental change lays the foundation for arbitrary
bit-width integers, LLVM has not yet been converted to actually deal with
them in any significant way. Most optimization passes, for example, will
still only deal with the byte-width integer types. Future increments
will rectify this situation.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@33113 91177308-0d34-0410-b5e6-96231b3b80d8
2007-01-12 07:05:14 +00:00
|
|
|
case Type::VoidTyID: return "void_";
|
|
|
|
case Type::IntegerTyID:
|
|
|
|
return std::string("int") + utostr(cast<IntegerType>(Ty)->getBitWidth()) +
|
|
|
|
"_";
|
|
|
|
case Type::FloatTyID: return "float_";
|
|
|
|
case Type::DoubleTyID: return "double_";
|
|
|
|
case Type::LabelTyID: return "label_";
|
|
|
|
case Type::FunctionTyID: return "func_";
|
|
|
|
case Type::StructTyID: return "struct_";
|
|
|
|
case Type::ArrayTyID: return "array_";
|
|
|
|
case Type::PointerTyID: return "ptr_";
|
2007-02-15 02:26:10 +00:00
|
|
|
case Type::VectorTyID: return "packed_";
|
For PR1064:
Implement the arbitrary bit-width integer feature. The feature allows
integers of any bitwidth (up to 64) to be defined instead of just 1, 8,
16, 32, and 64 bit integers.
This change does several things:
1. Introduces a new Derived Type, IntegerType, to represent the number of
bits in an integer. The Type classes SubclassData field is used to
store the number of bits. This allows 2^23 bits in an integer type.
2. Removes the five integer Type::TypeID values for the 1, 8, 16, 32 and
64-bit integers. These are replaced with just IntegerType which is not
a primitive any more.
3. Adjust the rest of LLVM to account for this change.
Note that while this incremental change lays the foundation for arbitrary
bit-width integers, LLVM has not yet been converted to actually deal with
them in any significant way. Most optimization passes, for example, will
still only deal with the byte-width integer types. Future increments
will rectify this situation.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@33113 91177308-0d34-0410-b5e6-96231b3b80d8
2007-01-12 07:05:14 +00:00
|
|
|
case Type::OpaqueTyID: return "opaque_";
|
|
|
|
default: return "other_";
|
2006-05-30 03:43:49 +00:00
|
|
|
}
|
For PR1064:
Implement the arbitrary bit-width integer feature. The feature allows
integers of any bitwidth (up to 64) to be defined instead of just 1, 8,
16, 32, and 64 bit integers.
This change does several things:
1. Introduces a new Derived Type, IntegerType, to represent the number of
bits in an integer. The Type classes SubclassData field is used to
store the number of bits. This allows 2^23 bits in an integer type.
2. Removes the five integer Type::TypeID values for the 1, 8, 16, 32 and
64-bit integers. These are replaced with just IntegerType which is not
a primitive any more.
3. Adjust the rest of LLVM to account for this change.
Note that while this incremental change lays the foundation for arbitrary
bit-width integers, LLVM has not yet been converted to actually deal with
them in any significant way. Most optimization passes, for example, will
still only deal with the byte-width integer types. Future increments
will rectify this situation.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@33113 91177308-0d34-0410-b5e6-96231b3b80d8
2007-01-12 07:05:14 +00:00
|
|
|
return "unknown_";
|
2006-05-30 03:43:49 +00:00
|
|
|
}
|
|
|
|
|
2006-05-31 17:31:38 +00:00
|
|
|
// Looks up the type in the symbol table and returns a pointer to its name or
|
|
|
|
// a null pointer if it wasn't found. Note that this isn't the same as the
|
|
|
|
// Mode::getTypeName function which will return an empty string, not a null
|
|
|
|
// pointer if the name is not found.
|
2006-05-29 00:57:22 +00:00
|
|
|
inline const std::string*
|
2007-01-06 07:24:44 +00:00
|
|
|
findTypeName(const TypeSymbolTable& ST, const Type* Ty)
|
2006-05-29 00:57:22 +00:00
|
|
|
{
|
2007-01-06 07:24:44 +00:00
|
|
|
TypeSymbolTable::const_iterator TI = ST.begin();
|
|
|
|
TypeSymbolTable::const_iterator TE = ST.end();
|
2006-05-29 00:57:22 +00:00
|
|
|
for (;TI != TE; ++TI)
|
|
|
|
if (TI->second == Ty)
|
|
|
|
return &(TI->first);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-05-31 17:31:38 +00:00
|
|
|
void
|
|
|
|
CppWriter::error(const std::string& msg) {
|
|
|
|
std::cerr << progname << ": " << msg << "\n";
|
|
|
|
exit(2);
|
2006-05-29 00:57:22 +00:00
|
|
|
}
|
|
|
|
|
2006-05-31 17:31:38 +00:00
|
|
|
// printCFP - Print a floating point constant .. very carefully :)
|
|
|
|
// This makes sure that conversion to/from floating yields the same binary
|
|
|
|
// result so that we don't lose precision.
|
|
|
|
void
|
|
|
|
CppWriter::printCFP(const ConstantFP *CFP) {
|
2007-09-06 18:13:44 +00:00
|
|
|
APFloat APF = APFloat(CFP->getValueAPF()); // copy
|
|
|
|
if (CFP->getType() == Type::FloatTy)
|
|
|
|
APF.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven);
|
2006-06-01 23:43:47 +00:00
|
|
|
Out << "ConstantFP::get(";
|
|
|
|
if (CFP->getType() == Type::DoubleTy)
|
|
|
|
Out << "Type::DoubleTy, ";
|
|
|
|
else
|
|
|
|
Out << "Type::FloatTy, ";
|
2007-09-06 18:13:44 +00:00
|
|
|
Out << "APFloat(";
|
2006-05-31 17:31:38 +00:00
|
|
|
#if HAVE_PRINTF_A
|
|
|
|
char Buffer[100];
|
2007-09-06 18:13:44 +00:00
|
|
|
sprintf(Buffer, "%A", APF.convertToDouble());
|
2006-05-31 17:31:38 +00:00
|
|
|
if ((!strncmp(Buffer, "0x", 2) ||
|
|
|
|
!strncmp(Buffer, "-0x", 3) ||
|
|
|
|
!strncmp(Buffer, "+0x", 3)) &&
|
2007-09-06 18:13:44 +00:00
|
|
|
APF.bitwiseIsEqual(APFloat(atof(Buffer)))) {
|
2006-06-01 23:43:47 +00:00
|
|
|
if (CFP->getType() == Type::DoubleTy)
|
|
|
|
Out << "BitsToDouble(" << Buffer << ")";
|
|
|
|
else
|
2007-09-06 18:13:44 +00:00
|
|
|
Out << "BitsToFloat((float)" << Buffer << ")";
|
|
|
|
Out << ")";
|
|
|
|
} else {
|
2006-05-31 17:31:38 +00:00
|
|
|
#endif
|
2007-09-06 18:13:44 +00:00
|
|
|
std::string StrVal = ftostr(CFP->getValueAPF());
|
2006-06-01 23:43:47 +00:00
|
|
|
|
|
|
|
while (StrVal[0] == ' ')
|
|
|
|
StrVal.erase(StrVal.begin());
|
|
|
|
|
|
|
|
// Check to make sure that the stringized number is not some string like
|
|
|
|
// "Inf" or NaN. Check that the string matches the "[-+]?[0-9]" regex.
|
|
|
|
if (((StrVal[0] >= '0' && StrVal[0] <= '9') ||
|
|
|
|
((StrVal[0] == '-' || StrVal[0] == '+') &&
|
|
|
|
(StrVal[1] >= '0' && StrVal[1] <= '9'))) &&
|
2007-09-06 18:13:44 +00:00
|
|
|
(CFP->isExactlyValue(atof(StrVal.c_str())))) {
|
2006-06-01 23:43:47 +00:00
|
|
|
if (CFP->getType() == Type::DoubleTy)
|
|
|
|
Out << StrVal;
|
|
|
|
else
|
2007-09-06 18:13:44 +00:00
|
|
|
Out << StrVal << "f";
|
|
|
|
}
|
2006-06-01 23:43:47 +00:00
|
|
|
else if (CFP->getType() == Type::DoubleTy)
|
2007-09-06 18:13:44 +00:00
|
|
|
Out << "BitsToDouble(0x" << std::hex
|
2007-09-12 03:30:33 +00:00
|
|
|
<< CFP->getValueAPF().convertToAPInt().getZExtValue()
|
2006-06-01 23:43:47 +00:00
|
|
|
<< std::dec << "ULL) /* " << StrVal << " */";
|
|
|
|
else
|
2007-09-06 18:13:44 +00:00
|
|
|
Out << "BitsToFloat(0x" << std::hex
|
2007-09-12 03:30:33 +00:00
|
|
|
<< (uint32_t)CFP->getValueAPF().convertToAPInt().getZExtValue()
|
2006-06-01 23:43:47 +00:00
|
|
|
<< std::dec << "U) /* " << StrVal << " */";
|
2007-09-06 18:13:44 +00:00
|
|
|
Out << ")";
|
2006-05-31 17:31:38 +00:00
|
|
|
#if HAVE_PRINTF_A
|
2006-05-31 04:43:19 +00:00
|
|
|
}
|
2006-05-31 17:31:38 +00:00
|
|
|
#endif
|
2006-06-01 23:43:47 +00:00
|
|
|
Out << ")";
|
2006-05-31 04:43:19 +00:00
|
|
|
}
|
|
|
|
|
2006-05-29 00:57:22 +00:00
|
|
|
void
|
|
|
|
CppWriter::printCallingConv(unsigned cc){
|
|
|
|
// Print the calling convention.
|
|
|
|
switch (cc) {
|
|
|
|
case CallingConv::C: Out << "CallingConv::C"; break;
|
|
|
|
case CallingConv::Fast: Out << "CallingConv::Fast"; break;
|
|
|
|
case CallingConv::Cold: Out << "CallingConv::Cold"; break;
|
|
|
|
case CallingConv::FirstTargetCC: Out << "CallingConv::FirstTargetCC"; break;
|
2006-05-30 03:43:49 +00:00
|
|
|
default: Out << cc; break;
|
2006-05-29 00:57:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CppWriter::printLinkageType(GlobalValue::LinkageTypes LT) {
|
|
|
|
switch (LT) {
|
|
|
|
case GlobalValue::InternalLinkage:
|
|
|
|
Out << "GlobalValue::InternalLinkage"; break;
|
|
|
|
case GlobalValue::LinkOnceLinkage:
|
|
|
|
Out << "GlobalValue::LinkOnceLinkage "; break;
|
|
|
|
case GlobalValue::WeakLinkage:
|
|
|
|
Out << "GlobalValue::WeakLinkage"; break;
|
|
|
|
case GlobalValue::AppendingLinkage:
|
|
|
|
Out << "GlobalValue::AppendingLinkage"; break;
|
|
|
|
case GlobalValue::ExternalLinkage:
|
|
|
|
Out << "GlobalValue::ExternalLinkage"; break;
|
2006-09-14 18:23:27 +00:00
|
|
|
case GlobalValue::DLLImportLinkage:
|
2007-07-11 19:51:06 +00:00
|
|
|
Out << "GlobalValue::DLLImportLinkage"; break;
|
2006-09-14 18:23:27 +00:00
|
|
|
case GlobalValue::DLLExportLinkage:
|
2007-07-11 19:51:06 +00:00
|
|
|
Out << "GlobalValue::DLLExportLinkage"; break;
|
2006-09-14 18:23:27 +00:00
|
|
|
case GlobalValue::ExternalWeakLinkage:
|
|
|
|
Out << "GlobalValue::ExternalWeakLinkage"; break;
|
2006-05-29 00:57:22 +00:00
|
|
|
case GlobalValue::GhostLinkage:
|
|
|
|
Out << "GlobalValue::GhostLinkage"; break;
|
|
|
|
}
|
2006-05-31 17:31:38 +00:00
|
|
|
}
|
|
|
|
|
2007-11-27 13:23:08 +00:00
|
|
|
void
|
|
|
|
CppWriter::printVisibilityType(GlobalValue::VisibilityTypes VisType) {
|
|
|
|
switch (VisType) {
|
|
|
|
default: assert(0 && "Unknown GVar visibility");
|
|
|
|
case GlobalValue::DefaultVisibility:
|
|
|
|
Out << "GlobalValue::DefaultVisibility";
|
|
|
|
break;
|
|
|
|
case GlobalValue::HiddenVisibility:
|
|
|
|
Out << "GlobalValue::HiddenVisibility";
|
|
|
|
break;
|
|
|
|
case GlobalValue::ProtectedVisibility:
|
|
|
|
Out << "GlobalValue::ProtectedVisibility";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-05-31 17:31:38 +00:00
|
|
|
// printEscapedString - Print each character of the specified string, escaping
|
|
|
|
// it if it is not printable or if it is an escape char.
|
|
|
|
void
|
|
|
|
CppWriter::printEscapedString(const std::string &Str) {
|
|
|
|
for (unsigned i = 0, e = Str.size(); i != e; ++i) {
|
|
|
|
unsigned char C = Str[i];
|
|
|
|
if (isprint(C) && C != '"' && C != '\\') {
|
|
|
|
Out << C;
|
|
|
|
} else {
|
|
|
|
Out << "\\x"
|
|
|
|
<< (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
|
|
|
|
<< (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
CppWriter::getCppName(const Type* Ty)
|
|
|
|
{
|
|
|
|
// First, handle the primitive types .. easy
|
2007-01-15 02:27:26 +00:00
|
|
|
if (Ty->isPrimitiveType() || Ty->isInteger()) {
|
2006-05-31 17:31:38 +00:00
|
|
|
switch (Ty->getTypeID()) {
|
2006-12-31 06:02:26 +00:00
|
|
|
case Type::VoidTyID: return "Type::VoidTy";
|
For PR1064:
Implement the arbitrary bit-width integer feature. The feature allows
integers of any bitwidth (up to 64) to be defined instead of just 1, 8,
16, 32, and 64 bit integers.
This change does several things:
1. Introduces a new Derived Type, IntegerType, to represent the number of
bits in an integer. The Type classes SubclassData field is used to
store the number of bits. This allows 2^23 bits in an integer type.
2. Removes the five integer Type::TypeID values for the 1, 8, 16, 32 and
64-bit integers. These are replaced with just IntegerType which is not
a primitive any more.
3. Adjust the rest of LLVM to account for this change.
Note that while this incremental change lays the foundation for arbitrary
bit-width integers, LLVM has not yet been converted to actually deal with
them in any significant way. Most optimization passes, for example, will
still only deal with the byte-width integer types. Future increments
will rectify this situation.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@33113 91177308-0d34-0410-b5e6-96231b3b80d8
2007-01-12 07:05:14 +00:00
|
|
|
case Type::IntegerTyID: {
|
|
|
|
unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth();
|
|
|
|
return "IntegerType::get(" + utostr(BitWidth) + ")";
|
|
|
|
}
|
2006-12-31 06:02:26 +00:00
|
|
|
case Type::FloatTyID: return "Type::FloatTy";
|
|
|
|
case Type::DoubleTyID: return "Type::DoubleTy";
|
|
|
|
case Type::LabelTyID: return "Type::LabelTy";
|
2006-05-31 17:31:38 +00:00
|
|
|
default:
|
|
|
|
error("Invalid primitive type");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return "Type::VoidTy"; // shouldn't be returned, but make it sensible
|
|
|
|
}
|
2006-05-30 10:21:41 +00:00
|
|
|
|
2006-05-31 17:31:38 +00:00
|
|
|
// Now, see if we've seen the type before and return that
|
|
|
|
TypeMap::iterator I = TypeNames.find(Ty);
|
|
|
|
if (I != TypeNames.end())
|
|
|
|
return I->second;
|
2006-05-29 00:57:22 +00:00
|
|
|
|
2006-05-31 17:31:38 +00:00
|
|
|
// Okay, let's build a new name for this type. Start with a prefix
|
|
|
|
const char* prefix = 0;
|
|
|
|
switch (Ty->getTypeID()) {
|
|
|
|
case Type::FunctionTyID: prefix = "FuncTy_"; break;
|
|
|
|
case Type::StructTyID: prefix = "StructTy_"; break;
|
|
|
|
case Type::ArrayTyID: prefix = "ArrayTy_"; break;
|
|
|
|
case Type::PointerTyID: prefix = "PointerTy_"; break;
|
|
|
|
case Type::OpaqueTyID: prefix = "OpaqueTy_"; break;
|
2007-02-15 03:39:18 +00:00
|
|
|
case Type::VectorTyID: prefix = "VectorTy_"; break;
|
2006-05-31 17:31:38 +00:00
|
|
|
default: prefix = "OtherTy_"; break; // prevent breakage
|
2006-05-29 00:57:22 +00:00
|
|
|
}
|
2006-05-31 17:31:38 +00:00
|
|
|
|
|
|
|
// See if the type has a name in the symboltable and build accordingly
|
2007-01-06 07:24:44 +00:00
|
|
|
const std::string* tName = findTypeName(TheModule->getTypeSymbolTable(), Ty);
|
2006-05-31 17:31:38 +00:00
|
|
|
std::string name;
|
|
|
|
if (tName)
|
|
|
|
name = std::string(prefix) + *tName;
|
|
|
|
else
|
|
|
|
name = std::string(prefix) + utostr(uniqueNum++);
|
|
|
|
sanitize(name);
|
|
|
|
|
|
|
|
// Save the name
|
|
|
|
return TypeNames[Ty] = name;
|
2006-05-29 00:57:22 +00:00
|
|
|
}
|
|
|
|
|
2006-05-31 17:31:38 +00:00
|
|
|
void
|
|
|
|
CppWriter::printCppName(const Type* Ty)
|
|
|
|
{
|
|
|
|
printEscapedString(getCppName(Ty));
|
2006-05-30 10:21:41 +00:00
|
|
|
}
|
|
|
|
|
2006-05-31 17:31:38 +00:00
|
|
|
std::string
|
|
|
|
CppWriter::getCppName(const Value* val) {
|
|
|
|
std::string name;
|
|
|
|
ValueMap::iterator I = ValueNames.find(val);
|
|
|
|
if (I != ValueNames.end() && I->first == val)
|
|
|
|
return I->second;
|
|
|
|
|
|
|
|
if (const GlobalVariable* GV = dyn_cast<GlobalVariable>(val)) {
|
|
|
|
name = std::string("gvar_") +
|
|
|
|
getTypePrefix(GV->getType()->getElementType());
|
2006-11-02 20:25:50 +00:00
|
|
|
} else if (isa<Function>(val)) {
|
2006-05-31 17:31:38 +00:00
|
|
|
name = std::string("func_");
|
|
|
|
} else if (const Constant* C = dyn_cast<Constant>(val)) {
|
|
|
|
name = std::string("const_") + getTypePrefix(C->getType());
|
2006-06-01 23:43:47 +00:00
|
|
|
} else if (const Argument* Arg = dyn_cast<Argument>(val)) {
|
|
|
|
if (is_inline) {
|
|
|
|
unsigned argNum = std::distance(Arg->getParent()->arg_begin(),
|
|
|
|
Function::const_arg_iterator(Arg)) + 1;
|
|
|
|
name = std::string("arg_") + utostr(argNum);
|
|
|
|
NameSet::iterator NI = UsedNames.find(name);
|
|
|
|
if (NI != UsedNames.end())
|
|
|
|
name += std::string("_") + utostr(uniqueNum++);
|
|
|
|
UsedNames.insert(name);
|
|
|
|
return ValueNames[val] = name;
|
|
|
|
} else {
|
|
|
|
name = getTypePrefix(val->getType());
|
|
|
|
}
|
2006-05-31 17:31:38 +00:00
|
|
|
} else {
|
|
|
|
name = getTypePrefix(val->getType());
|
|
|
|
}
|
|
|
|
name += (val->hasName() ? val->getName() : utostr(uniqueNum++));
|
|
|
|
sanitize(name);
|
|
|
|
NameSet::iterator NI = UsedNames.find(name);
|
|
|
|
if (NI != UsedNames.end())
|
|
|
|
name += std::string("_") + utostr(uniqueNum++);
|
|
|
|
UsedNames.insert(name);
|
|
|
|
return ValueNames[val] = name;
|
2006-05-29 00:57:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2006-05-31 17:31:38 +00:00
|
|
|
CppWriter::printCppName(const Value* val) {
|
|
|
|
printEscapedString(getCppName(val));
|
2006-05-29 00:57:22 +00:00
|
|
|
}
|
|
|
|
|
2007-11-27 13:23:08 +00:00
|
|
|
void
|
|
|
|
CppWriter::printParamAttrs(const ParamAttrsList* PAL, const std::string &name) {
|
|
|
|
Out << "ParamAttrsList *" << name << "_PAL = 0;";
|
|
|
|
nl(Out);
|
|
|
|
if (PAL) {
|
|
|
|
Out << '{'; in(); nl(Out);
|
|
|
|
Out << "ParamAttrsVector Attrs;"; nl(Out);
|
|
|
|
Out << "ParamAttrsWithIndex PAWI;"; nl(Out);
|
|
|
|
for (unsigned i = 0; i < PAL->size(); ++i) {
|
|
|
|
uint16_t index = PAL->getParamIndex(i);
|
2008-02-19 21:38:47 +00:00
|
|
|
ParameterAttributes attrs = PAL->getParamAttrs(index);
|
2007-11-27 13:23:08 +00:00
|
|
|
Out << "PAWI.index = " << index << "; PAWI.attrs = 0 ";
|
|
|
|
if (attrs & ParamAttr::SExt)
|
|
|
|
Out << " | ParamAttr::SExt";
|
|
|
|
if (attrs & ParamAttr::ZExt)
|
|
|
|
Out << " | ParamAttr::ZExt";
|
|
|
|
if (attrs & ParamAttr::StructRet)
|
|
|
|
Out << " | ParamAttr::StructRet";
|
|
|
|
if (attrs & ParamAttr::InReg)
|
|
|
|
Out << " | ParamAttr::InReg";
|
|
|
|
if (attrs & ParamAttr::NoReturn)
|
|
|
|
Out << " | ParamAttr::NoReturn";
|
|
|
|
if (attrs & ParamAttr::NoUnwind)
|
|
|
|
Out << " | ParamAttr::NoUnwind";
|
|
|
|
Out << ";";
|
|
|
|
nl(Out);
|
|
|
|
Out << "Attrs.push_back(PAWI);";
|
|
|
|
nl(Out);
|
|
|
|
}
|
|
|
|
Out << name << "_PAL = ParamAttrsList::get(Attrs);";
|
|
|
|
nl(Out);
|
|
|
|
out(); nl(Out);
|
|
|
|
Out << '}'; nl(Out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-05-29 00:57:22 +00:00
|
|
|
bool
|
2006-05-31 17:31:38 +00:00
|
|
|
CppWriter::printTypeInternal(const Type* Ty) {
|
2006-05-29 00:57:22 +00:00
|
|
|
// We don't print definitions for primitive types
|
2007-01-15 02:27:26 +00:00
|
|
|
if (Ty->isPrimitiveType() || Ty->isInteger())
|
2006-05-29 00:57:22 +00:00
|
|
|
return false;
|
|
|
|
|
2006-05-30 21:18:23 +00:00
|
|
|
// If we already defined this type, we don't need to define it again.
|
|
|
|
if (DefinedTypes.find(Ty) != DefinedTypes.end())
|
|
|
|
return false;
|
2006-05-29 00:57:22 +00:00
|
|
|
|
2006-05-30 21:18:23 +00:00
|
|
|
// Everything below needs the name for the type so get it now.
|
2006-05-29 00:57:22 +00:00
|
|
|
std::string typeName(getCppName(Ty));
|
|
|
|
|
|
|
|
// Search the type stack for recursion. If we find it, then generate this
|
|
|
|
// as an OpaqueType, but make sure not to do this multiple times because
|
|
|
|
// the type could appear in multiple places on the stack. Once the opaque
|
2006-05-30 21:18:23 +00:00
|
|
|
// definition is issued, it must not be re-issued. Consequently we have to
|
2006-05-29 00:57:22 +00:00
|
|
|
// check the UnresolvedTypes list as well.
|
2006-05-31 17:31:38 +00:00
|
|
|
TypeList::const_iterator TI = std::find(TypeStack.begin(),TypeStack.end(),Ty);
|
|
|
|
if (TI != TypeStack.end()) {
|
2006-05-29 00:57:22 +00:00
|
|
|
TypeMap::const_iterator I = UnresolvedTypes.find(Ty);
|
|
|
|
if (I == UnresolvedTypes.end()) {
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << "PATypeHolder " << typeName << "_fwd = OpaqueType::get();";
|
|
|
|
nl(Out);
|
2006-05-29 00:57:22 +00:00
|
|
|
UnresolvedTypes[Ty] = typeName;
|
|
|
|
}
|
2006-05-30 10:21:41 +00:00
|
|
|
return true;
|
2006-05-29 00:57:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We're going to print a derived type which, by definition, contains other
|
|
|
|
// types. So, push this one we're printing onto the type stack to assist with
|
|
|
|
// recursive definitions.
|
2006-05-30 21:18:23 +00:00
|
|
|
TypeStack.push_back(Ty);
|
2006-05-29 00:57:22 +00:00
|
|
|
|
|
|
|
// Print the type definition
|
|
|
|
switch (Ty->getTypeID()) {
|
|
|
|
case Type::FunctionTyID: {
|
|
|
|
const FunctionType* FT = cast<FunctionType>(Ty);
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << "std::vector<const Type*>" << typeName << "_args;";
|
|
|
|
nl(Out);
|
2006-05-29 00:57:22 +00:00
|
|
|
FunctionType::param_iterator PI = FT->param_begin();
|
|
|
|
FunctionType::param_iterator PE = FT->param_end();
|
|
|
|
for (; PI != PE; ++PI) {
|
|
|
|
const Type* argTy = static_cast<const Type*>(*PI);
|
2006-05-31 17:31:38 +00:00
|
|
|
bool isForward = printTypeInternal(argTy);
|
2006-05-29 00:57:22 +00:00
|
|
|
std::string argName(getCppName(argTy));
|
|
|
|
Out << typeName << "_args.push_back(" << argName;
|
|
|
|
if (isForward)
|
|
|
|
Out << "_fwd";
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << ");";
|
|
|
|
nl(Out);
|
2006-05-29 00:57:22 +00:00
|
|
|
}
|
2006-05-31 17:31:38 +00:00
|
|
|
bool isForward = printTypeInternal(FT->getReturnType());
|
2006-05-29 00:57:22 +00:00
|
|
|
std::string retTypeName(getCppName(FT->getReturnType()));
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << "FunctionType* " << typeName << " = FunctionType::get(";
|
|
|
|
in(); nl(Out) << "/*Result=*/" << retTypeName;
|
2006-05-29 00:57:22 +00:00
|
|
|
if (isForward)
|
|
|
|
Out << "_fwd";
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << ",";
|
|
|
|
nl(Out) << "/*Params=*/" << typeName << "_args,";
|
2007-11-27 13:23:08 +00:00
|
|
|
nl(Out) << "/*isVarArg=*/" << (FT->isVarArg() ? "true" : "false") << ");";
|
2006-08-14 22:35:15 +00:00
|
|
|
out();
|
|
|
|
nl(Out);
|
2006-05-29 00:57:22 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Type::StructTyID: {
|
|
|
|
const StructType* ST = cast<StructType>(Ty);
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << "std::vector<const Type*>" << typeName << "_fields;";
|
|
|
|
nl(Out);
|
2006-05-29 00:57:22 +00:00
|
|
|
StructType::element_iterator EI = ST->element_begin();
|
|
|
|
StructType::element_iterator EE = ST->element_end();
|
|
|
|
for (; EI != EE; ++EI) {
|
|
|
|
const Type* fieldTy = static_cast<const Type*>(*EI);
|
2006-05-31 17:31:38 +00:00
|
|
|
bool isForward = printTypeInternal(fieldTy);
|
2006-05-29 00:57:22 +00:00
|
|
|
std::string fieldName(getCppName(fieldTy));
|
|
|
|
Out << typeName << "_fields.push_back(" << fieldName;
|
|
|
|
if (isForward)
|
|
|
|
Out << "_fwd";
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << ");";
|
|
|
|
nl(Out);
|
2006-05-29 00:57:22 +00:00
|
|
|
}
|
|
|
|
Out << "StructType* " << typeName << " = StructType::get("
|
2007-04-11 12:41:49 +00:00
|
|
|
<< typeName << "_fields, /*isPacked=*/"
|
|
|
|
<< (ST->isPacked() ? "true" : "false") << ");";
|
2006-08-14 22:35:15 +00:00
|
|
|
nl(Out);
|
2006-05-29 00:57:22 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Type::ArrayTyID: {
|
|
|
|
const ArrayType* AT = cast<ArrayType>(Ty);
|
|
|
|
const Type* ET = AT->getElementType();
|
2006-05-31 17:31:38 +00:00
|
|
|
bool isForward = printTypeInternal(ET);
|
2006-05-29 00:57:22 +00:00
|
|
|
std::string elemName(getCppName(ET));
|
|
|
|
Out << "ArrayType* " << typeName << " = ArrayType::get("
|
|
|
|
<< elemName << (isForward ? "_fwd" : "")
|
2006-08-14 22:35:15 +00:00
|
|
|
<< ", " << utostr(AT->getNumElements()) << ");";
|
|
|
|
nl(Out);
|
2006-05-29 00:57:22 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Type::PointerTyID: {
|
|
|
|
const PointerType* PT = cast<PointerType>(Ty);
|
|
|
|
const Type* ET = PT->getElementType();
|
2006-05-31 17:31:38 +00:00
|
|
|
bool isForward = printTypeInternal(ET);
|
2006-05-29 00:57:22 +00:00
|
|
|
std::string elemName(getCppName(ET));
|
|
|
|
Out << "PointerType* " << typeName << " = PointerType::get("
|
2007-12-17 01:12:55 +00:00
|
|
|
<< elemName << (isForward ? "_fwd" : "")
|
|
|
|
<< ", " << utostr(PT->getAddressSpace()) << ");";
|
2006-08-14 22:35:15 +00:00
|
|
|
nl(Out);
|
2006-05-29 00:57:22 +00:00
|
|
|
break;
|
|
|
|
}
|
2007-02-15 02:26:10 +00:00
|
|
|
case Type::VectorTyID: {
|
|
|
|
const VectorType* PT = cast<VectorType>(Ty);
|
2006-05-29 00:57:22 +00:00
|
|
|
const Type* ET = PT->getElementType();
|
2006-05-31 17:31:38 +00:00
|
|
|
bool isForward = printTypeInternal(ET);
|
2006-05-29 00:57:22 +00:00
|
|
|
std::string elemName(getCppName(ET));
|
2007-02-15 02:26:10 +00:00
|
|
|
Out << "VectorType* " << typeName << " = VectorType::get("
|
2006-05-29 00:57:22 +00:00
|
|
|
<< elemName << (isForward ? "_fwd" : "")
|
2006-08-14 22:35:15 +00:00
|
|
|
<< ", " << utostr(PT->getNumElements()) << ");";
|
|
|
|
nl(Out);
|
2006-05-29 00:57:22 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Type::OpaqueTyID: {
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << "OpaqueType* " << typeName << " = OpaqueType::get();";
|
|
|
|
nl(Out);
|
2006-05-29 00:57:22 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2006-05-31 17:31:38 +00:00
|
|
|
error("Invalid TypeID");
|
2006-05-29 00:57:22 +00:00
|
|
|
}
|
|
|
|
|
2006-05-29 02:58:15 +00:00
|
|
|
// If the type had a name, make sure we recreate it.
|
|
|
|
const std::string* progTypeName =
|
2007-01-06 07:24:44 +00:00
|
|
|
findTypeName(TheModule->getTypeSymbolTable(),Ty);
|
2007-04-11 12:28:56 +00:00
|
|
|
if (progTypeName) {
|
2006-05-29 02:58:15 +00:00
|
|
|
Out << "mod->addTypeName(\"" << *progTypeName << "\", "
|
2006-08-14 22:35:15 +00:00
|
|
|
<< typeName << ");";
|
|
|
|
nl(Out);
|
2007-04-11 12:28:56 +00:00
|
|
|
}
|
2006-05-29 02:58:15 +00:00
|
|
|
|
2006-05-29 00:57:22 +00:00
|
|
|
// Pop us off the type stack
|
|
|
|
TypeStack.pop_back();
|
2006-05-30 21:18:23 +00:00
|
|
|
|
|
|
|
// Indicate that this type is now defined.
|
|
|
|
DefinedTypes.insert(Ty);
|
|
|
|
|
|
|
|
// Early resolve as many unresolved types as possible. Search the unresolved
|
|
|
|
// types map for the type we just printed. Now that its definition is complete
|
|
|
|
// we can resolve any previous references to it. This prevents a cascade of
|
|
|
|
// unresolved types.
|
|
|
|
TypeMap::iterator I = UnresolvedTypes.find(Ty);
|
|
|
|
if (I != UnresolvedTypes.end()) {
|
|
|
|
Out << "cast<OpaqueType>(" << I->second
|
2006-08-14 22:35:15 +00:00
|
|
|
<< "_fwd.get())->refineAbstractTypeTo(" << I->second << ");";
|
|
|
|
nl(Out);
|
2006-05-30 21:18:23 +00:00
|
|
|
Out << I->second << " = cast<";
|
|
|
|
switch (Ty->getTypeID()) {
|
|
|
|
case Type::FunctionTyID: Out << "FunctionType"; break;
|
|
|
|
case Type::ArrayTyID: Out << "ArrayType"; break;
|
|
|
|
case Type::StructTyID: Out << "StructType"; break;
|
2007-02-15 02:26:10 +00:00
|
|
|
case Type::VectorTyID: Out << "VectorType"; break;
|
2006-05-30 21:18:23 +00:00
|
|
|
case Type::PointerTyID: Out << "PointerType"; break;
|
|
|
|
case Type::OpaqueTyID: Out << "OpaqueType"; break;
|
|
|
|
default: Out << "NoSuchDerivedType"; break;
|
|
|
|
}
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << ">(" << I->second << "_fwd.get());";
|
|
|
|
nl(Out); nl(Out);
|
2006-05-30 21:18:23 +00:00
|
|
|
UnresolvedTypes.erase(I);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, separate the type definition from other with a newline.
|
2006-08-14 22:35:15 +00:00
|
|
|
nl(Out);
|
2006-05-29 00:57:22 +00:00
|
|
|
|
|
|
|
// We weren't a recursive type
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-05-31 17:31:38 +00:00
|
|
|
// Prints a type definition. Returns true if it could not resolve all the types
|
|
|
|
// in the definition but had to use a forward reference.
|
|
|
|
void
|
|
|
|
CppWriter::printType(const Type* Ty) {
|
|
|
|
assert(TypeStack.empty());
|
|
|
|
TypeStack.clear();
|
|
|
|
printTypeInternal(Ty);
|
|
|
|
assert(TypeStack.empty());
|
|
|
|
}
|
|
|
|
|
2006-05-29 00:57:22 +00:00
|
|
|
void
|
|
|
|
CppWriter::printTypes(const Module* M) {
|
2006-05-30 10:21:41 +00:00
|
|
|
|
|
|
|
// Walk the symbol table and print out all its types
|
2007-01-06 07:24:44 +00:00
|
|
|
const TypeSymbolTable& symtab = M->getTypeSymbolTable();
|
|
|
|
for (TypeSymbolTable::const_iterator TI = symtab.begin(), TE = symtab.end();
|
|
|
|
TI != TE; ++TI) {
|
2006-05-30 10:21:41 +00:00
|
|
|
|
|
|
|
// For primitive types and types already defined, just add a name
|
|
|
|
TypeMap::const_iterator TNI = TypeNames.find(TI->second);
|
2007-01-15 02:27:26 +00:00
|
|
|
if (TI->second->isInteger() || TI->second->isPrimitiveType() ||
|
For PR1064:
Implement the arbitrary bit-width integer feature. The feature allows
integers of any bitwidth (up to 64) to be defined instead of just 1, 8,
16, 32, and 64 bit integers.
This change does several things:
1. Introduces a new Derived Type, IntegerType, to represent the number of
bits in an integer. The Type classes SubclassData field is used to
store the number of bits. This allows 2^23 bits in an integer type.
2. Removes the five integer Type::TypeID values for the 1, 8, 16, 32 and
64-bit integers. These are replaced with just IntegerType which is not
a primitive any more.
3. Adjust the rest of LLVM to account for this change.
Note that while this incremental change lays the foundation for arbitrary
bit-width integers, LLVM has not yet been converted to actually deal with
them in any significant way. Most optimization passes, for example, will
still only deal with the byte-width integer types. Future increments
will rectify this situation.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@33113 91177308-0d34-0410-b5e6-96231b3b80d8
2007-01-12 07:05:14 +00:00
|
|
|
TNI != TypeNames.end()) {
|
2006-05-30 10:21:41 +00:00
|
|
|
Out << "mod->addTypeName(\"";
|
|
|
|
printEscapedString(TI->first);
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << "\", " << getCppName(TI->second) << ");";
|
|
|
|
nl(Out);
|
2006-05-30 10:21:41 +00:00
|
|
|
// For everything else, define the type
|
|
|
|
} else {
|
2006-05-31 17:31:38 +00:00
|
|
|
printType(TI->second);
|
2006-05-30 10:21:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-05-29 00:57:22 +00:00
|
|
|
// Add all of the global variables to the value table...
|
|
|
|
for (Module::const_global_iterator I = TheModule->global_begin(),
|
|
|
|
E = TheModule->global_end(); I != E; ++I) {
|
|
|
|
if (I->hasInitializer())
|
2006-05-31 17:31:38 +00:00
|
|
|
printType(I->getInitializer()->getType());
|
|
|
|
printType(I->getType());
|
2006-05-29 00:57:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add all the functions to the table
|
|
|
|
for (Module::const_iterator FI = TheModule->begin(), FE = TheModule->end();
|
|
|
|
FI != FE; ++FI) {
|
2006-05-31 17:31:38 +00:00
|
|
|
printType(FI->getReturnType());
|
|
|
|
printType(FI->getFunctionType());
|
2006-05-29 00:57:22 +00:00
|
|
|
// Add all the function arguments
|
|
|
|
for(Function::const_arg_iterator AI = FI->arg_begin(),
|
|
|
|
AE = FI->arg_end(); AI != AE; ++AI) {
|
2006-05-31 17:31:38 +00:00
|
|
|
printType(AI->getType());
|
2006-05-29 00:57:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add all of the basic blocks and instructions
|
|
|
|
for (Function::const_iterator BB = FI->begin(),
|
|
|
|
E = FI->end(); BB != E; ++BB) {
|
2006-05-31 17:31:38 +00:00
|
|
|
printType(BB->getType());
|
2006-05-29 00:57:22 +00:00
|
|
|
for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;
|
|
|
|
++I) {
|
2006-05-31 17:31:38 +00:00
|
|
|
printType(I->getType());
|
2006-05-30 10:21:41 +00:00
|
|
|
for (unsigned i = 0; i < I->getNumOperands(); ++i)
|
2006-05-31 17:31:38 +00:00
|
|
|
printType(I->getOperand(i)->getType());
|
2006-05-29 00:57:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-29 18:08:06 +00:00
|
|
|
// printConstant - Print out a constant pool entry...
|
2006-05-29 00:57:22 +00:00
|
|
|
void CppWriter::printConstant(const Constant *CV) {
|
2006-05-30 10:21:41 +00:00
|
|
|
// First, if the constant is actually a GlobalValue (variable or function) or
|
|
|
|
// its already in the constant list then we've printed it already and we can
|
|
|
|
// just return.
|
|
|
|
if (isa<GlobalValue>(CV) || ValueNames.find(CV) != ValueNames.end())
|
2006-05-29 18:08:06 +00:00
|
|
|
return;
|
|
|
|
|
2006-05-29 00:57:22 +00:00
|
|
|
std::string constName(getCppName(CV));
|
|
|
|
std::string typeName(getCppName(CV->getType()));
|
|
|
|
if (CV->isNullValue()) {
|
|
|
|
Out << "Constant* " << constName << " = Constant::getNullValue("
|
2006-08-14 22:35:15 +00:00
|
|
|
<< typeName << ");";
|
|
|
|
nl(Out);
|
2006-05-29 00:57:22 +00:00
|
|
|
return;
|
|
|
|
}
|
2006-05-29 18:08:06 +00:00
|
|
|
if (isa<GlobalValue>(CV)) {
|
|
|
|
// Skip variables and functions, we emit them elsewhere
|
|
|
|
return;
|
|
|
|
}
|
2007-01-11 12:24:14 +00:00
|
|
|
if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
|
2007-04-11 13:02:56 +00:00
|
|
|
Out << "ConstantInt* " << constName << " = ConstantInt::get(APInt("
|
|
|
|
<< cast<IntegerType>(CI->getType())->getBitWidth() << ", "
|
2007-03-01 20:55:43 +00:00
|
|
|
<< " \"" << CI->getValue().toStringSigned(10) << "\", 10));";
|
2006-05-29 00:57:22 +00:00
|
|
|
} else if (isa<ConstantAggregateZero>(CV)) {
|
2006-05-30 10:21:41 +00:00
|
|
|
Out << "ConstantAggregateZero* " << constName
|
|
|
|
<< " = ConstantAggregateZero::get(" << typeName << ");";
|
2006-05-29 00:57:22 +00:00
|
|
|
} else if (isa<ConstantPointerNull>(CV)) {
|
2006-05-30 10:21:41 +00:00
|
|
|
Out << "ConstantPointerNull* " << constName
|
|
|
|
<< " = ConstanPointerNull::get(" << typeName << ");";
|
2006-05-29 00:57:22 +00:00
|
|
|
} else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
|
2006-06-01 23:43:47 +00:00
|
|
|
Out << "ConstantFP* " << constName << " = ";
|
2006-05-31 17:31:38 +00:00
|
|
|
printCFP(CFP);
|
2006-06-01 23:43:47 +00:00
|
|
|
Out << ";";
|
2006-05-29 00:57:22 +00:00
|
|
|
} else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
|
2006-12-31 06:02:26 +00:00
|
|
|
if (CA->isString() && CA->getType()->getElementType() == Type::Int8Ty) {
|
2006-05-29 00:57:22 +00:00
|
|
|
Out << "Constant* " << constName << " = ConstantArray::get(\"";
|
2007-06-25 16:45:54 +00:00
|
|
|
std::string tmp = CA->getAsString();
|
|
|
|
bool nullTerminate = false;
|
|
|
|
if (tmp[tmp.length()-1] == 0) {
|
|
|
|
tmp.erase(tmp.length()-1);
|
|
|
|
nullTerminate = true;
|
|
|
|
}
|
|
|
|
printEscapedString(tmp);
|
2006-05-30 10:21:41 +00:00
|
|
|
// Determine if we want null termination or not.
|
2007-06-25 16:45:54 +00:00
|
|
|
if (nullTerminate)
|
2006-05-30 21:18:23 +00:00
|
|
|
Out << "\", true"; // Indicate that the null terminator should be added.
|
2007-06-25 16:45:54 +00:00
|
|
|
else
|
|
|
|
Out << "\", false";// No null terminator
|
2006-05-30 10:21:41 +00:00
|
|
|
Out << ");";
|
|
|
|
} else {
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << "std::vector<Constant*> " << constName << "_elems;";
|
|
|
|
nl(Out);
|
2006-05-29 00:57:22 +00:00
|
|
|
unsigned N = CA->getNumOperands();
|
|
|
|
for (unsigned i = 0; i < N; ++i) {
|
2006-05-30 10:21:41 +00:00
|
|
|
printConstant(CA->getOperand(i)); // recurse to print operands
|
2006-05-29 00:57:22 +00:00
|
|
|
Out << constName << "_elems.push_back("
|
2006-08-14 22:35:15 +00:00
|
|
|
<< getCppName(CA->getOperand(i)) << ");";
|
|
|
|
nl(Out);
|
2006-05-29 00:57:22 +00:00
|
|
|
}
|
|
|
|
Out << "Constant* " << constName << " = ConstantArray::get("
|
|
|
|
<< typeName << ", " << constName << "_elems);";
|
|
|
|
}
|
|
|
|
} else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << "std::vector<Constant*> " << constName << "_fields;";
|
|
|
|
nl(Out);
|
2006-05-29 00:57:22 +00:00
|
|
|
unsigned N = CS->getNumOperands();
|
|
|
|
for (unsigned i = 0; i < N; i++) {
|
|
|
|
printConstant(CS->getOperand(i));
|
|
|
|
Out << constName << "_fields.push_back("
|
2006-08-14 22:35:15 +00:00
|
|
|
<< getCppName(CS->getOperand(i)) << ");";
|
|
|
|
nl(Out);
|
2006-05-29 00:57:22 +00:00
|
|
|
}
|
|
|
|
Out << "Constant* " << constName << " = ConstantStruct::get("
|
|
|
|
<< typeName << ", " << constName << "_fields);";
|
2007-02-15 02:26:10 +00:00
|
|
|
} else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << "std::vector<Constant*> " << constName << "_elems;";
|
|
|
|
nl(Out);
|
2006-05-29 00:57:22 +00:00
|
|
|
unsigned N = CP->getNumOperands();
|
|
|
|
for (unsigned i = 0; i < N; ++i) {
|
|
|
|
printConstant(CP->getOperand(i));
|
|
|
|
Out << constName << "_elems.push_back("
|
2006-08-14 22:35:15 +00:00
|
|
|
<< getCppName(CP->getOperand(i)) << ");";
|
|
|
|
nl(Out);
|
2006-05-29 00:57:22 +00:00
|
|
|
}
|
2007-02-15 02:26:10 +00:00
|
|
|
Out << "Constant* " << constName << " = ConstantVector::get("
|
2006-05-29 00:57:22 +00:00
|
|
|
<< typeName << ", " << constName << "_elems);";
|
|
|
|
} else if (isa<UndefValue>(CV)) {
|
2006-05-30 10:21:41 +00:00
|
|
|
Out << "UndefValue* " << constName << " = UndefValue::get("
|
2006-05-29 18:08:06 +00:00
|
|
|
<< typeName << ");";
|
2006-05-29 00:57:22 +00:00
|
|
|
} else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
|
2006-05-29 18:08:06 +00:00
|
|
|
if (CE->getOpcode() == Instruction::GetElementPtr) {
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << "std::vector<Constant*> " << constName << "_indices;";
|
|
|
|
nl(Out);
|
2006-05-30 10:21:41 +00:00
|
|
|
printConstant(CE->getOperand(0));
|
2006-05-29 18:08:06 +00:00
|
|
|
for (unsigned i = 1; i < CE->getNumOperands(); ++i ) {
|
2006-05-30 10:21:41 +00:00
|
|
|
printConstant(CE->getOperand(i));
|
2006-05-29 18:08:06 +00:00
|
|
|
Out << constName << "_indices.push_back("
|
2006-08-14 22:35:15 +00:00
|
|
|
<< getCppName(CE->getOperand(i)) << ");";
|
|
|
|
nl(Out);
|
2006-05-29 18:08:06 +00:00
|
|
|
}
|
2006-05-30 10:21:41 +00:00
|
|
|
Out << "Constant* " << constName
|
|
|
|
<< " = ConstantExpr::getGetElementPtr("
|
|
|
|
<< getCppName(CE->getOperand(0)) << ", "
|
2007-09-04 17:15:07 +00:00
|
|
|
<< "&" << constName << "_indices[0], "
|
|
|
|
<< constName << "_indices.size()"
|
2007-04-11 12:28:56 +00:00
|
|
|
<< " );";
|
2006-11-27 01:05:10 +00:00
|
|
|
} else if (CE->isCast()) {
|
2006-05-30 10:21:41 +00:00
|
|
|
printConstant(CE->getOperand(0));
|
2006-05-29 18:08:06 +00:00
|
|
|
Out << "Constant* " << constName << " = ConstantExpr::getCast(";
|
2006-12-12 01:31:37 +00:00
|
|
|
switch (CE->getOpcode()) {
|
|
|
|
default: assert(0 && "Invalid cast opcode");
|
|
|
|
case Instruction::Trunc: Out << "Instruction::Trunc"; break;
|
|
|
|
case Instruction::ZExt: Out << "Instruction::ZExt"; break;
|
|
|
|
case Instruction::SExt: Out << "Instruction::SExt"; break;
|
|
|
|
case Instruction::FPTrunc: Out << "Instruction::FPTrunc"; break;
|
|
|
|
case Instruction::FPExt: Out << "Instruction::FPExt"; break;
|
|
|
|
case Instruction::FPToUI: Out << "Instruction::FPToUI"; break;
|
|
|
|
case Instruction::FPToSI: Out << "Instruction::FPToSI"; break;
|
|
|
|
case Instruction::UIToFP: Out << "Instruction::UIToFP"; break;
|
|
|
|
case Instruction::SIToFP: Out << "Instruction::SIToFP"; break;
|
|
|
|
case Instruction::PtrToInt: Out << "Instruction::PtrToInt"; break;
|
|
|
|
case Instruction::IntToPtr: Out << "Instruction::IntToPtr"; break;
|
|
|
|
case Instruction::BitCast: Out << "Instruction::BitCast"; break;
|
|
|
|
}
|
|
|
|
Out << ", " << getCppName(CE->getOperand(0)) << ", "
|
|
|
|
<< getCppName(CE->getType()) << ");";
|
2006-05-29 18:08:06 +00:00
|
|
|
} else {
|
2006-05-30 10:21:41 +00:00
|
|
|
unsigned N = CE->getNumOperands();
|
|
|
|
for (unsigned i = 0; i < N; ++i ) {
|
|
|
|
printConstant(CE->getOperand(i));
|
|
|
|
}
|
2006-05-29 18:08:06 +00:00
|
|
|
Out << "Constant* " << constName << " = ConstantExpr::";
|
|
|
|
switch (CE->getOpcode()) {
|
2006-12-23 06:05:41 +00:00
|
|
|
case Instruction::Add: Out << "getAdd("; break;
|
|
|
|
case Instruction::Sub: Out << "getSub("; break;
|
|
|
|
case Instruction::Mul: Out << "getMul("; break;
|
|
|
|
case Instruction::UDiv: Out << "getUDiv("; break;
|
|
|
|
case Instruction::SDiv: Out << "getSDiv("; break;
|
|
|
|
case Instruction::FDiv: Out << "getFDiv("; break;
|
|
|
|
case Instruction::URem: Out << "getURem("; break;
|
|
|
|
case Instruction::SRem: Out << "getSRem("; break;
|
|
|
|
case Instruction::FRem: Out << "getFRem("; break;
|
|
|
|
case Instruction::And: Out << "getAnd("; break;
|
|
|
|
case Instruction::Or: Out << "getOr("; break;
|
|
|
|
case Instruction::Xor: Out << "getXor("; break;
|
|
|
|
case Instruction::ICmp:
|
|
|
|
Out << "getICmp(ICmpInst::ICMP_";
|
|
|
|
switch (CE->getPredicate()) {
|
|
|
|
case ICmpInst::ICMP_EQ: Out << "EQ"; break;
|
|
|
|
case ICmpInst::ICMP_NE: Out << "NE"; break;
|
|
|
|
case ICmpInst::ICMP_SLT: Out << "SLT"; break;
|
|
|
|
case ICmpInst::ICMP_ULT: Out << "ULT"; break;
|
|
|
|
case ICmpInst::ICMP_SGT: Out << "SGT"; break;
|
|
|
|
case ICmpInst::ICMP_UGT: Out << "UGT"; break;
|
|
|
|
case ICmpInst::ICMP_SLE: Out << "SLE"; break;
|
|
|
|
case ICmpInst::ICMP_ULE: Out << "ULE"; break;
|
|
|
|
case ICmpInst::ICMP_SGE: Out << "SGE"; break;
|
|
|
|
case ICmpInst::ICMP_UGE: Out << "UGE"; break;
|
|
|
|
default: error("Invalid ICmp Predicate");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Instruction::FCmp:
|
|
|
|
Out << "getFCmp(FCmpInst::FCMP_";
|
|
|
|
switch (CE->getPredicate()) {
|
|
|
|
case FCmpInst::FCMP_FALSE: Out << "FALSE"; break;
|
|
|
|
case FCmpInst::FCMP_ORD: Out << "ORD"; break;
|
|
|
|
case FCmpInst::FCMP_UNO: Out << "UNO"; break;
|
|
|
|
case FCmpInst::FCMP_OEQ: Out << "OEQ"; break;
|
|
|
|
case FCmpInst::FCMP_UEQ: Out << "UEQ"; break;
|
|
|
|
case FCmpInst::FCMP_ONE: Out << "ONE"; break;
|
|
|
|
case FCmpInst::FCMP_UNE: Out << "UNE"; break;
|
|
|
|
case FCmpInst::FCMP_OLT: Out << "OLT"; break;
|
|
|
|
case FCmpInst::FCMP_ULT: Out << "ULT"; break;
|
|
|
|
case FCmpInst::FCMP_OGT: Out << "OGT"; break;
|
|
|
|
case FCmpInst::FCMP_UGT: Out << "UGT"; break;
|
|
|
|
case FCmpInst::FCMP_OLE: Out << "OLE"; break;
|
|
|
|
case FCmpInst::FCMP_ULE: Out << "ULE"; break;
|
|
|
|
case FCmpInst::FCMP_OGE: Out << "OGE"; break;
|
|
|
|
case FCmpInst::FCMP_UGE: Out << "UGE"; break;
|
|
|
|
case FCmpInst::FCMP_TRUE: Out << "TRUE"; break;
|
|
|
|
default: error("Invalid FCmp Predicate");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Instruction::Shl: Out << "getShl("; break;
|
|
|
|
case Instruction::LShr: Out << "getLShr("; break;
|
|
|
|
case Instruction::AShr: Out << "getAShr("; break;
|
|
|
|
case Instruction::Select: Out << "getSelect("; break;
|
|
|
|
case Instruction::ExtractElement: Out << "getExtractElement("; break;
|
|
|
|
case Instruction::InsertElement: Out << "getInsertElement("; break;
|
|
|
|
case Instruction::ShuffleVector: Out << "getShuffleVector("; break;
|
2006-05-29 18:08:06 +00:00
|
|
|
default:
|
2006-05-31 17:31:38 +00:00
|
|
|
error("Invalid constant expression");
|
2006-05-29 18:08:06 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
Out << getCppName(CE->getOperand(0));
|
|
|
|
for (unsigned i = 1; i < CE->getNumOperands(); ++i)
|
|
|
|
Out << ", " << getCppName(CE->getOperand(i));
|
|
|
|
Out << ");";
|
2006-05-29 00:57:22 +00:00
|
|
|
}
|
|
|
|
} else {
|
2006-05-31 17:31:38 +00:00
|
|
|
error("Bad Constant");
|
2006-05-29 18:08:06 +00:00
|
|
|
Out << "Constant* " << constName << " = 0; ";
|
2006-05-29 00:57:22 +00:00
|
|
|
}
|
2006-08-14 22:35:15 +00:00
|
|
|
nl(Out);
|
2006-05-29 00:57:22 +00:00
|
|
|
}
|
|
|
|
|
2006-05-31 17:31:38 +00:00
|
|
|
void
|
|
|
|
CppWriter::printConstants(const Module* M) {
|
|
|
|
// Traverse all the global variables looking for constant initializers
|
|
|
|
for (Module::const_global_iterator I = TheModule->global_begin(),
|
|
|
|
E = TheModule->global_end(); I != E; ++I)
|
|
|
|
if (I->hasInitializer())
|
|
|
|
printConstant(I->getInitializer());
|
|
|
|
|
|
|
|
// Traverse the LLVM functions looking for constants
|
|
|
|
for (Module::const_iterator FI = TheModule->begin(), FE = TheModule->end();
|
|
|
|
FI != FE; ++FI) {
|
|
|
|
// Add all of the basic blocks and instructions
|
|
|
|
for (Function::const_iterator BB = FI->begin(),
|
|
|
|
E = FI->end(); BB != E; ++BB) {
|
|
|
|
for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;
|
|
|
|
++I) {
|
|
|
|
for (unsigned i = 0; i < I->getNumOperands(); ++i) {
|
|
|
|
if (Constant* C = dyn_cast<Constant>(I->getOperand(i))) {
|
|
|
|
printConstant(C);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-05-29 00:57:22 +00:00
|
|
|
}
|
2006-05-30 03:43:49 +00:00
|
|
|
}
|
2006-05-29 00:57:22 +00:00
|
|
|
|
2006-05-31 17:31:38 +00:00
|
|
|
void CppWriter::printVariableUses(const GlobalVariable *GV) {
|
2006-08-14 22:35:15 +00:00
|
|
|
nl(Out) << "// Type Definitions";
|
|
|
|
nl(Out);
|
2006-05-31 17:31:38 +00:00
|
|
|
printType(GV->getType());
|
|
|
|
if (GV->hasInitializer()) {
|
|
|
|
Constant* Init = GV->getInitializer();
|
|
|
|
printType(Init->getType());
|
|
|
|
if (Function* F = dyn_cast<Function>(Init)) {
|
2006-08-14 22:35:15 +00:00
|
|
|
nl(Out)<< "/ Function Declarations"; nl(Out);
|
2006-05-31 17:31:38 +00:00
|
|
|
printFunctionHead(F);
|
|
|
|
} else if (GlobalVariable* gv = dyn_cast<GlobalVariable>(Init)) {
|
2006-08-14 22:35:15 +00:00
|
|
|
nl(Out) << "// Global Variable Declarations"; nl(Out);
|
2006-05-31 17:31:38 +00:00
|
|
|
printVariableHead(gv);
|
|
|
|
} else {
|
2006-08-14 22:35:15 +00:00
|
|
|
nl(Out) << "// Constant Definitions"; nl(Out);
|
2006-05-31 17:31:38 +00:00
|
|
|
printConstant(gv);
|
|
|
|
}
|
|
|
|
if (GlobalVariable* gv = dyn_cast<GlobalVariable>(Init)) {
|
2006-08-14 22:35:15 +00:00
|
|
|
nl(Out) << "// Global Variable Definitions"; nl(Out);
|
2006-05-31 17:31:38 +00:00
|
|
|
printVariableBody(gv);
|
|
|
|
}
|
2006-05-30 03:43:49 +00:00
|
|
|
}
|
2006-05-31 17:31:38 +00:00
|
|
|
}
|
2006-05-30 03:43:49 +00:00
|
|
|
|
2006-05-31 17:31:38 +00:00
|
|
|
void CppWriter::printVariableHead(const GlobalVariable *GV) {
|
2006-08-14 22:35:15 +00:00
|
|
|
nl(Out) << "GlobalVariable* " << getCppName(GV);
|
2006-06-01 23:43:47 +00:00
|
|
|
if (is_inline) {
|
|
|
|
Out << " = mod->getGlobalVariable(";
|
|
|
|
printEscapedString(GV->getName());
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << ", " << getCppName(GV->getType()->getElementType()) << ",true)";
|
|
|
|
nl(Out) << "if (!" << getCppName(GV) << ") {";
|
|
|
|
in(); nl(Out) << getCppName(GV);
|
2006-06-01 23:43:47 +00:00
|
|
|
}
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << " = new GlobalVariable(";
|
|
|
|
nl(Out) << "/*Type=*/";
|
2006-05-31 17:31:38 +00:00
|
|
|
printCppName(GV->getType()->getElementType());
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << ",";
|
|
|
|
nl(Out) << "/*isConstant=*/" << (GV->isConstant()?"true":"false");
|
|
|
|
Out << ",";
|
|
|
|
nl(Out) << "/*Linkage=*/";
|
2006-05-31 17:31:38 +00:00
|
|
|
printLinkageType(GV->getLinkage());
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << ",";
|
|
|
|
nl(Out) << "/*Initializer=*/0, ";
|
2006-05-31 17:31:38 +00:00
|
|
|
if (GV->hasInitializer()) {
|
|
|
|
Out << "// has initializer, specified below";
|
2006-05-30 03:43:49 +00:00
|
|
|
}
|
2006-08-14 22:35:15 +00:00
|
|
|
nl(Out) << "/*Name=*/\"";
|
2006-05-31 17:31:38 +00:00
|
|
|
printEscapedString(GV->getName());
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << "\",";
|
|
|
|
nl(Out) << "mod);";
|
|
|
|
nl(Out);
|
2006-05-30 03:43:49 +00:00
|
|
|
|
2006-05-31 17:31:38 +00:00
|
|
|
if (GV->hasSection()) {
|
|
|
|
printCppName(GV);
|
|
|
|
Out << "->setSection(\"";
|
|
|
|
printEscapedString(GV->getSection());
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << "\");";
|
|
|
|
nl(Out);
|
2006-05-31 17:31:38 +00:00
|
|
|
}
|
|
|
|
if (GV->getAlignment()) {
|
|
|
|
printCppName(GV);
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << "->setAlignment(" << utostr(GV->getAlignment()) << ");";
|
|
|
|
nl(Out);
|
2006-05-31 17:31:38 +00:00
|
|
|
};
|
2007-11-27 13:23:08 +00:00
|
|
|
if (GV->getVisibility() != GlobalValue::DefaultVisibility) {
|
|
|
|
printCppName(GV);
|
|
|
|
Out << "->setVisibility(";
|
|
|
|
printVisibilityType(GV->getVisibility());
|
|
|
|
Out << ");";
|
|
|
|
nl(Out);
|
|
|
|
}
|
2006-08-14 22:35:15 +00:00
|
|
|
if (is_inline) {
|
|
|
|
out(); Out << "}"; nl(Out);
|
|
|
|
}
|
2006-05-31 17:31:38 +00:00
|
|
|
}
|
2006-05-30 10:21:41 +00:00
|
|
|
|
2006-05-31 17:31:38 +00:00
|
|
|
void
|
|
|
|
CppWriter::printVariableBody(const GlobalVariable *GV) {
|
|
|
|
if (GV->hasInitializer()) {
|
|
|
|
printCppName(GV);
|
|
|
|
Out << "->setInitializer(";
|
|
|
|
//if (!isa<GlobalValue(GV->getInitializer()))
|
|
|
|
//else
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << getCppName(GV->getInitializer()) << ");";
|
|
|
|
nl(Out);
|
2006-05-29 00:57:22 +00:00
|
|
|
}
|
2006-05-31 17:31:38 +00:00
|
|
|
}
|
2006-05-30 21:18:23 +00:00
|
|
|
|
2006-05-31 17:31:38 +00:00
|
|
|
std::string
|
|
|
|
CppWriter::getOpName(Value* V) {
|
|
|
|
if (!isa<Instruction>(V) || DefinedValues.find(V) != DefinedValues.end())
|
|
|
|
return getCppName(V);
|
|
|
|
|
|
|
|
// See if its alread in the map of forward references, if so just return the
|
|
|
|
// name we already set up for it
|
|
|
|
ForwardRefMap::const_iterator I = ForwardRefs.find(V);
|
|
|
|
if (I != ForwardRefs.end())
|
|
|
|
return I->second;
|
|
|
|
|
|
|
|
// This is a new forward reference. Generate a unique name for it
|
|
|
|
std::string result(std::string("fwdref_") + utostr(uniqueNum++));
|
|
|
|
|
|
|
|
// Yes, this is a hack. An Argument is the smallest instantiable value that
|
|
|
|
// we can make as a placeholder for the real value. We'll replace these
|
|
|
|
// Argument instances later.
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << "Argument* " << result << " = new Argument("
|
|
|
|
<< getCppName(V->getType()) << ");";
|
|
|
|
nl(Out);
|
2006-05-31 17:31:38 +00:00
|
|
|
ForwardRefs[V] = result;
|
|
|
|
return result;
|
2006-05-29 00:57:22 +00:00
|
|
|
}
|
|
|
|
|
2006-05-29 18:08:06 +00:00
|
|
|
// printInstruction - This member is called for each Instruction in a function.
|
|
|
|
void
|
2006-05-31 17:31:38 +00:00
|
|
|
CppWriter::printInstruction(const Instruction *I, const std::string& bbname) {
|
2006-05-29 18:08:06 +00:00
|
|
|
std::string iName(getCppName(I));
|
|
|
|
|
2006-05-30 21:18:23 +00:00
|
|
|
// Before we emit this instruction, we need to take care of generating any
|
|
|
|
// forward references. So, we get the names of all the operands in advance
|
|
|
|
std::string* opNames = new std::string[I->getNumOperands()];
|
|
|
|
for (unsigned i = 0; i < I->getNumOperands(); i++) {
|
|
|
|
opNames[i] = getOpName(I->getOperand(i));
|
|
|
|
}
|
|
|
|
|
2006-05-29 18:08:06 +00:00
|
|
|
switch (I->getOpcode()) {
|
|
|
|
case Instruction::Ret: {
|
|
|
|
const ReturnInst* ret = cast<ReturnInst>(I);
|
2007-04-11 12:28:56 +00:00
|
|
|
Out << "new ReturnInst("
|
2006-05-30 21:18:23 +00:00
|
|
|
<< (ret->getReturnValue() ? opNames[0] + ", " : "") << bbname << ");";
|
2006-05-29 18:08:06 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Instruction::Br: {
|
|
|
|
const BranchInst* br = cast<BranchInst>(I);
|
2007-04-11 12:28:56 +00:00
|
|
|
Out << "new BranchInst(" ;
|
2006-05-29 18:08:06 +00:00
|
|
|
if (br->getNumOperands() == 3 ) {
|
2006-05-30 21:18:23 +00:00
|
|
|
Out << opNames[0] << ", "
|
|
|
|
<< opNames[1] << ", "
|
|
|
|
<< opNames[2] << ", ";
|
2006-05-29 18:08:06 +00:00
|
|
|
|
|
|
|
} else if (br->getNumOperands() == 1) {
|
2006-05-30 21:18:23 +00:00
|
|
|
Out << opNames[0] << ", ";
|
2006-05-29 18:08:06 +00:00
|
|
|
} else {
|
2006-05-31 17:31:38 +00:00
|
|
|
error("Branch with 2 operands?");
|
2006-05-29 18:08:06 +00:00
|
|
|
}
|
|
|
|
Out << bbname << ");";
|
|
|
|
break;
|
|
|
|
}
|
2006-05-30 03:43:49 +00:00
|
|
|
case Instruction::Switch: {
|
|
|
|
const SwitchInst* sw = cast<SwitchInst>(I);
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << "SwitchInst* " << iName << " = new SwitchInst("
|
2006-05-30 21:18:23 +00:00
|
|
|
<< opNames[0] << ", "
|
|
|
|
<< opNames[1] << ", "
|
2006-08-14 22:35:15 +00:00
|
|
|
<< sw->getNumCases() << ", " << bbname << ");";
|
|
|
|
nl(Out);
|
2006-05-30 21:18:23 +00:00
|
|
|
for (unsigned i = 2; i < sw->getNumOperands(); i += 2 ) {
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << iName << "->addCase("
|
2006-05-30 21:18:23 +00:00
|
|
|
<< opNames[i] << ", "
|
2006-08-14 22:35:15 +00:00
|
|
|
<< opNames[i+1] << ");";
|
|
|
|
nl(Out);
|
2006-05-30 03:43:49 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Instruction::Invoke: {
|
|
|
|
const InvokeInst* inv = cast<InvokeInst>(I);
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << "std::vector<Value*> " << iName << "_params;";
|
|
|
|
nl(Out);
|
|
|
|
for (unsigned i = 3; i < inv->getNumOperands(); ++i) {
|
|
|
|
Out << iName << "_params.push_back("
|
|
|
|
<< opNames[i] << ");";
|
|
|
|
nl(Out);
|
|
|
|
}
|
2007-04-11 12:28:56 +00:00
|
|
|
Out << "InvokeInst *" << iName << " = new InvokeInst("
|
2006-05-30 21:18:23 +00:00
|
|
|
<< opNames[0] << ", "
|
|
|
|
<< opNames[1] << ", "
|
|
|
|
<< opNames[2] << ", "
|
2007-08-27 19:04:21 +00:00
|
|
|
<< iName << "_params.begin(), " << iName << "_params.end(), \"";
|
2006-05-30 03:43:49 +00:00
|
|
|
printEscapedString(inv->getName());
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << "\", " << bbname << ");";
|
|
|
|
nl(Out) << iName << "->setCallingConv(";
|
2006-05-30 03:43:49 +00:00
|
|
|
printCallingConv(inv->getCallingConv());
|
|
|
|
Out << ");";
|
2007-11-27 13:23:08 +00:00
|
|
|
printParamAttrs(inv->getParamAttrs(), iName);
|
|
|
|
Out << iName << "->setParamAttrs(" << iName << "_PAL);";
|
|
|
|
nl(Out);
|
2006-05-30 03:43:49 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Instruction::Unwind: {
|
2007-04-11 12:28:56 +00:00
|
|
|
Out << "new UnwindInst("
|
2006-05-30 03:43:49 +00:00
|
|
|
<< bbname << ");";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Instruction::Unreachable:{
|
2007-04-11 12:28:56 +00:00
|
|
|
Out << "new UnreachableInst("
|
2006-05-30 03:43:49 +00:00
|
|
|
<< bbname << ");";
|
|
|
|
break;
|
|
|
|
}
|
2006-05-29 18:08:06 +00:00
|
|
|
case Instruction::Add:
|
|
|
|
case Instruction::Sub:
|
|
|
|
case Instruction::Mul:
|
2006-10-26 06:15:43 +00:00
|
|
|
case Instruction::UDiv:
|
|
|
|
case Instruction::SDiv:
|
|
|
|
case Instruction::FDiv:
|
2006-11-02 01:53:59 +00:00
|
|
|
case Instruction::URem:
|
|
|
|
case Instruction::SRem:
|
|
|
|
case Instruction::FRem:
|
2006-05-29 18:08:06 +00:00
|
|
|
case Instruction::And:
|
|
|
|
case Instruction::Or:
|
|
|
|
case Instruction::Xor:
|
2006-05-30 03:43:49 +00:00
|
|
|
case Instruction::Shl:
|
2006-11-08 06:47:33 +00:00
|
|
|
case Instruction::LShr:
|
|
|
|
case Instruction::AShr:{
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << "BinaryOperator* " << iName << " = BinaryOperator::create(";
|
2006-05-30 03:43:49 +00:00
|
|
|
switch (I->getOpcode()) {
|
|
|
|
case Instruction::Add: Out << "Instruction::Add"; break;
|
|
|
|
case Instruction::Sub: Out << "Instruction::Sub"; break;
|
|
|
|
case Instruction::Mul: Out << "Instruction::Mul"; break;
|
2006-10-26 06:15:43 +00:00
|
|
|
case Instruction::UDiv:Out << "Instruction::UDiv"; break;
|
|
|
|
case Instruction::SDiv:Out << "Instruction::SDiv"; break;
|
|
|
|
case Instruction::FDiv:Out << "Instruction::FDiv"; break;
|
2006-11-02 01:53:59 +00:00
|
|
|
case Instruction::URem:Out << "Instruction::URem"; break;
|
|
|
|
case Instruction::SRem:Out << "Instruction::SRem"; break;
|
|
|
|
case Instruction::FRem:Out << "Instruction::FRem"; break;
|
2006-05-30 03:43:49 +00:00
|
|
|
case Instruction::And: Out << "Instruction::And"; break;
|
2006-08-14 22:35:15 +00:00
|
|
|
case Instruction::Or: Out << "Instruction::Or"; break;
|
2006-05-30 03:43:49 +00:00
|
|
|
case Instruction::Xor: Out << "Instruction::Xor"; break;
|
|
|
|
case Instruction::Shl: Out << "Instruction::Shl"; break;
|
2006-11-08 06:47:33 +00:00
|
|
|
case Instruction::LShr:Out << "Instruction::LShr"; break;
|
|
|
|
case Instruction::AShr:Out << "Instruction::AShr"; break;
|
2006-05-30 03:43:49 +00:00
|
|
|
default: Out << "Instruction::BadOpCode"; break;
|
|
|
|
}
|
2006-05-30 21:18:23 +00:00
|
|
|
Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
|
2006-05-30 03:43:49 +00:00
|
|
|
printEscapedString(I->getName());
|
|
|
|
Out << "\", " << bbname << ");";
|
|
|
|
break;
|
|
|
|
}
|
2006-12-23 06:05:41 +00:00
|
|
|
case Instruction::FCmp: {
|
|
|
|
Out << "FCmpInst* " << iName << " = new FCmpInst(";
|
|
|
|
switch (cast<FCmpInst>(I)->getPredicate()) {
|
|
|
|
case FCmpInst::FCMP_FALSE: Out << "FCmpInst::FCMP_FALSE"; break;
|
|
|
|
case FCmpInst::FCMP_OEQ : Out << "FCmpInst::FCMP_OEQ"; break;
|
|
|
|
case FCmpInst::FCMP_OGT : Out << "FCmpInst::FCMP_OGT"; break;
|
|
|
|
case FCmpInst::FCMP_OGE : Out << "FCmpInst::FCMP_OGE"; break;
|
|
|
|
case FCmpInst::FCMP_OLT : Out << "FCmpInst::FCMP_OLT"; break;
|
|
|
|
case FCmpInst::FCMP_OLE : Out << "FCmpInst::FCMP_OLE"; break;
|
|
|
|
case FCmpInst::FCMP_ONE : Out << "FCmpInst::FCMP_ONE"; break;
|
|
|
|
case FCmpInst::FCMP_ORD : Out << "FCmpInst::FCMP_ORD"; break;
|
|
|
|
case FCmpInst::FCMP_UNO : Out << "FCmpInst::FCMP_UNO"; break;
|
|
|
|
case FCmpInst::FCMP_UEQ : Out << "FCmpInst::FCMP_UEQ"; break;
|
|
|
|
case FCmpInst::FCMP_UGT : Out << "FCmpInst::FCMP_UGT"; break;
|
|
|
|
case FCmpInst::FCMP_UGE : Out << "FCmpInst::FCMP_UGE"; break;
|
|
|
|
case FCmpInst::FCMP_ULT : Out << "FCmpInst::FCMP_ULT"; break;
|
|
|
|
case FCmpInst::FCMP_ULE : Out << "FCmpInst::FCMP_ULE"; break;
|
|
|
|
case FCmpInst::FCMP_UNE : Out << "FCmpInst::FCMP_UNE"; break;
|
|
|
|
case FCmpInst::FCMP_TRUE : Out << "FCmpInst::FCMP_TRUE"; break;
|
|
|
|
default: Out << "FCmpInst::BAD_ICMP_PREDICATE"; break;
|
|
|
|
}
|
|
|
|
Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
|
|
|
|
printEscapedString(I->getName());
|
|
|
|
Out << "\", " << bbname << ");";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Instruction::ICmp: {
|
|
|
|
Out << "ICmpInst* " << iName << " = new ICmpInst(";
|
|
|
|
switch (cast<ICmpInst>(I)->getPredicate()) {
|
|
|
|
case ICmpInst::ICMP_EQ: Out << "ICmpInst::ICMP_EQ"; break;
|
|
|
|
case ICmpInst::ICMP_NE: Out << "ICmpInst::ICMP_NE"; break;
|
|
|
|
case ICmpInst::ICMP_ULE: Out << "ICmpInst::ICMP_ULE"; break;
|
|
|
|
case ICmpInst::ICMP_SLE: Out << "ICmpInst::ICMP_SLE"; break;
|
|
|
|
case ICmpInst::ICMP_UGE: Out << "ICmpInst::ICMP_UGE"; break;
|
|
|
|
case ICmpInst::ICMP_SGE: Out << "ICmpInst::ICMP_SGE"; break;
|
|
|
|
case ICmpInst::ICMP_ULT: Out << "ICmpInst::ICMP_ULT"; break;
|
|
|
|
case ICmpInst::ICMP_SLT: Out << "ICmpInst::ICMP_SLT"; break;
|
|
|
|
case ICmpInst::ICMP_UGT: Out << "ICmpInst::ICMP_UGT"; break;
|
|
|
|
case ICmpInst::ICMP_SGT: Out << "ICmpInst::ICMP_SGT"; break;
|
|
|
|
default: Out << "ICmpInst::BAD_ICMP_PREDICATE"; break;
|
2006-05-30 03:43:49 +00:00
|
|
|
}
|
2006-05-30 21:18:23 +00:00
|
|
|
Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
|
2006-05-30 03:43:49 +00:00
|
|
|
printEscapedString(I->getName());
|
|
|
|
Out << "\", " << bbname << ");";
|
|
|
|
break;
|
|
|
|
}
|
2006-05-29 18:08:06 +00:00
|
|
|
case Instruction::Malloc: {
|
|
|
|
const MallocInst* mallocI = cast<MallocInst>(I);
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << "MallocInst* " << iName << " = new MallocInst("
|
2006-05-29 18:08:06 +00:00
|
|
|
<< getCppName(mallocI->getAllocatedType()) << ", ";
|
|
|
|
if (mallocI->isArrayAllocation())
|
2006-05-30 21:18:23 +00:00
|
|
|
Out << opNames[0] << ", " ;
|
2006-05-29 18:08:06 +00:00
|
|
|
Out << "\"";
|
|
|
|
printEscapedString(mallocI->getName());
|
|
|
|
Out << "\", " << bbname << ");";
|
|
|
|
if (mallocI->getAlignment())
|
2006-08-14 22:35:15 +00:00
|
|
|
nl(Out) << iName << "->setAlignment("
|
2006-05-29 18:08:06 +00:00
|
|
|
<< mallocI->getAlignment() << ");";
|
|
|
|
break;
|
|
|
|
}
|
2006-05-30 03:43:49 +00:00
|
|
|
case Instruction::Free: {
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << "FreeInst* " << iName << " = new FreeInst("
|
2006-05-30 03:43:49 +00:00
|
|
|
<< getCppName(I->getOperand(0)) << ", " << bbname << ");";
|
|
|
|
break;
|
|
|
|
}
|
2006-05-29 18:08:06 +00:00
|
|
|
case Instruction::Alloca: {
|
|
|
|
const AllocaInst* allocaI = cast<AllocaInst>(I);
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << "AllocaInst* " << iName << " = new AllocaInst("
|
2006-05-29 18:08:06 +00:00
|
|
|
<< getCppName(allocaI->getAllocatedType()) << ", ";
|
|
|
|
if (allocaI->isArrayAllocation())
|
2006-05-30 21:18:23 +00:00
|
|
|
Out << opNames[0] << ", ";
|
2006-05-29 18:08:06 +00:00
|
|
|
Out << "\"";
|
|
|
|
printEscapedString(allocaI->getName());
|
|
|
|
Out << "\", " << bbname << ");";
|
|
|
|
if (allocaI->getAlignment())
|
2006-08-14 22:35:15 +00:00
|
|
|
nl(Out) << iName << "->setAlignment("
|
2006-05-29 18:08:06 +00:00
|
|
|
<< allocaI->getAlignment() << ");";
|
|
|
|
break;
|
|
|
|
}
|
2006-05-30 03:43:49 +00:00
|
|
|
case Instruction::Load:{
|
|
|
|
const LoadInst* load = cast<LoadInst>(I);
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << "LoadInst* " << iName << " = new LoadInst("
|
2006-05-30 21:18:23 +00:00
|
|
|
<< opNames[0] << ", \"";
|
2006-05-30 10:21:41 +00:00
|
|
|
printEscapedString(load->getName());
|
|
|
|
Out << "\", " << (load->isVolatile() ? "true" : "false" )
|
2006-08-14 22:35:15 +00:00
|
|
|
<< ", " << bbname << ");";
|
2006-05-30 03:43:49 +00:00
|
|
|
break;
|
|
|
|
}
|
2006-05-29 18:08:06 +00:00
|
|
|
case Instruction::Store: {
|
|
|
|
const StoreInst* store = cast<StoreInst>(I);
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << "StoreInst* " << iName << " = new StoreInst("
|
2006-05-30 21:18:23 +00:00
|
|
|
<< opNames[0] << ", "
|
|
|
|
<< opNames[1] << ", "
|
2006-05-30 10:21:41 +00:00
|
|
|
<< (store->isVolatile() ? "true" : "false")
|
2006-08-14 22:35:15 +00:00
|
|
|
<< ", " << bbname << ");";
|
2006-05-29 18:08:06 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Instruction::GetElementPtr: {
|
|
|
|
const GetElementPtrInst* gep = cast<GetElementPtrInst>(I);
|
|
|
|
if (gep->getNumOperands() <= 2) {
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << "GetElementPtrInst* " << iName << " = new GetElementPtrInst("
|
2006-05-30 21:18:23 +00:00
|
|
|
<< opNames[0];
|
2006-05-29 18:08:06 +00:00
|
|
|
if (gep->getNumOperands() == 2)
|
2006-05-30 21:18:23 +00:00
|
|
|
Out << ", " << opNames[1];
|
2006-05-29 00:57:22 +00:00
|
|
|
} else {
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << "std::vector<Value*> " << iName << "_indices;";
|
|
|
|
nl(Out);
|
2006-05-29 18:08:06 +00:00
|
|
|
for (unsigned i = 1; i < gep->getNumOperands(); ++i ) {
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << iName << "_indices.push_back("
|
|
|
|
<< opNames[i] << ");";
|
|
|
|
nl(Out);
|
2006-05-29 00:57:22 +00:00
|
|
|
}
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << "Instruction* " << iName << " = new GetElementPtrInst("
|
2007-09-04 15:46:09 +00:00
|
|
|
<< opNames[0] << ", " << iName << "_indices.begin(), "
|
|
|
|
<< iName << "_indices.end()";
|
2006-05-29 00:57:22 +00:00
|
|
|
}
|
2006-05-29 18:08:06 +00:00
|
|
|
Out << ", \"";
|
|
|
|
printEscapedString(gep->getName());
|
|
|
|
Out << "\", " << bbname << ");";
|
|
|
|
break;
|
2006-05-29 00:57:22 +00:00
|
|
|
}
|
2006-05-30 03:43:49 +00:00
|
|
|
case Instruction::PHI: {
|
|
|
|
const PHINode* phi = cast<PHINode>(I);
|
2006-05-30 10:21:41 +00:00
|
|
|
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << "PHINode* " << iName << " = new PHINode("
|
2006-05-30 03:43:49 +00:00
|
|
|
<< getCppName(phi->getType()) << ", \"";
|
|
|
|
printEscapedString(phi->getName());
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << "\", " << bbname << ");";
|
|
|
|
nl(Out) << iName << "->reserveOperandSpace("
|
2006-05-30 10:21:41 +00:00
|
|
|
<< phi->getNumIncomingValues()
|
2006-08-14 22:35:15 +00:00
|
|
|
<< ");";
|
|
|
|
nl(Out);
|
2006-05-30 21:18:23 +00:00
|
|
|
for (unsigned i = 0; i < phi->getNumOperands(); i+=2) {
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << iName << "->addIncoming("
|
|
|
|
<< opNames[i] << ", " << opNames[i+1] << ");";
|
|
|
|
nl(Out);
|
2006-05-30 03:43:49 +00:00
|
|
|
}
|
2006-05-29 18:08:06 +00:00
|
|
|
break;
|
2006-05-29 00:57:22 +00:00
|
|
|
}
|
2006-11-27 01:05:10 +00:00
|
|
|
case Instruction::Trunc:
|
|
|
|
case Instruction::ZExt:
|
|
|
|
case Instruction::SExt:
|
|
|
|
case Instruction::FPTrunc:
|
|
|
|
case Instruction::FPExt:
|
|
|
|
case Instruction::FPToUI:
|
|
|
|
case Instruction::FPToSI:
|
|
|
|
case Instruction::UIToFP:
|
|
|
|
case Instruction::SIToFP:
|
|
|
|
case Instruction::PtrToInt:
|
|
|
|
case Instruction::IntToPtr:
|
|
|
|
case Instruction::BitCast: {
|
2006-05-30 03:43:49 +00:00
|
|
|
const CastInst* cst = cast<CastInst>(I);
|
2006-11-27 01:05:10 +00:00
|
|
|
Out << "CastInst* " << iName << " = new ";
|
|
|
|
switch (I->getOpcode()) {
|
2007-02-16 06:34:39 +00:00
|
|
|
case Instruction::Trunc: Out << "TruncInst"; break;
|
|
|
|
case Instruction::ZExt: Out << "ZExtInst"; break;
|
|
|
|
case Instruction::SExt: Out << "SExtInst"; break;
|
|
|
|
case Instruction::FPTrunc: Out << "FPTruncInst"; break;
|
|
|
|
case Instruction::FPExt: Out << "FPExtInst"; break;
|
|
|
|
case Instruction::FPToUI: Out << "FPToUIInst"; break;
|
|
|
|
case Instruction::FPToSI: Out << "FPToSIInst"; break;
|
|
|
|
case Instruction::UIToFP: Out << "UIToFPInst"; break;
|
|
|
|
case Instruction::SIToFP: Out << "SIToFPInst"; break;
|
2007-04-11 12:28:56 +00:00
|
|
|
case Instruction::PtrToInt: Out << "PtrToIntInst"; break;
|
2007-02-16 06:34:39 +00:00
|
|
|
case Instruction::IntToPtr: Out << "IntToPtrInst"; break;
|
|
|
|
case Instruction::BitCast: Out << "BitCastInst"; break;
|
2006-11-27 01:05:10 +00:00
|
|
|
default: assert(!"Unreachable"); break;
|
|
|
|
}
|
|
|
|
Out << "(" << opNames[0] << ", "
|
2006-05-30 03:43:49 +00:00
|
|
|
<< getCppName(cst->getType()) << ", \"";
|
|
|
|
printEscapedString(cst->getName());
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << "\", " << bbname << ");";
|
2006-05-30 03:43:49 +00:00
|
|
|
break;
|
2006-05-29 00:57:22 +00:00
|
|
|
}
|
2006-05-30 03:43:49 +00:00
|
|
|
case Instruction::Call:{
|
|
|
|
const CallInst* call = cast<CallInst>(I);
|
2006-05-30 10:21:41 +00:00
|
|
|
if (InlineAsm* ila = dyn_cast<InlineAsm>(call->getOperand(0))) {
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << "InlineAsm* " << getCppName(ila) << " = InlineAsm::get("
|
2006-05-30 10:21:41 +00:00
|
|
|
<< getCppName(ila->getFunctionType()) << ", \""
|
|
|
|
<< ila->getAsmString() << "\", \""
|
|
|
|
<< ila->getConstraintString() << "\","
|
2006-08-14 22:35:15 +00:00
|
|
|
<< (ila->hasSideEffects() ? "true" : "false") << ");";
|
|
|
|
nl(Out);
|
2006-05-30 10:21:41 +00:00
|
|
|
}
|
2007-08-02 03:30:26 +00:00
|
|
|
if (call->getNumOperands() > 2) {
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << "std::vector<Value*> " << iName << "_params;";
|
|
|
|
nl(Out);
|
|
|
|
for (unsigned i = 1; i < call->getNumOperands(); ++i) {
|
|
|
|
Out << iName << "_params.push_back(" << opNames[i] << ");";
|
|
|
|
nl(Out);
|
|
|
|
}
|
|
|
|
Out << "CallInst* " << iName << " = new CallInst("
|
2007-08-02 03:30:26 +00:00
|
|
|
<< opNames[0] << ", " << iName << "_params.begin(), "
|
|
|
|
<< iName << "_params.end(), \"";
|
2006-05-30 03:43:49 +00:00
|
|
|
} else if (call->getNumOperands() == 2) {
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << "CallInst* " << iName << " = new CallInst("
|
2006-05-30 21:18:23 +00:00
|
|
|
<< opNames[0] << ", " << opNames[1] << ", \"";
|
2006-05-30 03:43:49 +00:00
|
|
|
} else {
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << "CallInst* " << iName << " = new CallInst(" << opNames[0]
|
2006-05-30 21:18:23 +00:00
|
|
|
<< ", \"";
|
2006-05-30 03:43:49 +00:00
|
|
|
}
|
|
|
|
printEscapedString(call->getName());
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << "\", " << bbname << ");";
|
|
|
|
nl(Out) << iName << "->setCallingConv(";
|
2006-05-30 03:43:49 +00:00
|
|
|
printCallingConv(call->getCallingConv());
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << ");";
|
|
|
|
nl(Out) << iName << "->setTailCall("
|
2006-05-30 10:21:41 +00:00
|
|
|
<< (call->isTailCall() ? "true":"false");
|
2006-05-30 03:43:49 +00:00
|
|
|
Out << ");";
|
2007-11-27 13:23:08 +00:00
|
|
|
printParamAttrs(call->getParamAttrs(), iName);
|
|
|
|
Out << iName << "->setParamAttrs(" << iName << "_PAL);";
|
|
|
|
nl(Out);
|
2006-05-30 03:43:49 +00:00
|
|
|
break;
|
2006-05-29 00:57:22 +00:00
|
|
|
}
|
2006-05-30 03:43:49 +00:00
|
|
|
case Instruction::Select: {
|
|
|
|
const SelectInst* sel = cast<SelectInst>(I);
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << "SelectInst* " << getCppName(sel) << " = new SelectInst(";
|
2006-05-30 21:18:23 +00:00
|
|
|
Out << opNames[0] << ", " << opNames[1] << ", " << opNames[2] << ", \"";
|
2006-05-30 03:43:49 +00:00
|
|
|
printEscapedString(sel->getName());
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << "\", " << bbname << ");";
|
2006-05-30 03:43:49 +00:00
|
|
|
break;
|
2006-05-29 00:57:22 +00:00
|
|
|
}
|
2006-05-30 03:43:49 +00:00
|
|
|
case Instruction::UserOp1:
|
|
|
|
/// FALL THROUGH
|
|
|
|
case Instruction::UserOp2: {
|
|
|
|
/// FIXME: What should be done here?
|
|
|
|
break;
|
2006-05-29 00:57:22 +00:00
|
|
|
}
|
2006-05-30 03:43:49 +00:00
|
|
|
case Instruction::VAArg: {
|
|
|
|
const VAArgInst* va = cast<VAArgInst>(I);
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << "VAArgInst* " << getCppName(va) << " = new VAArgInst("
|
2006-05-30 21:18:23 +00:00
|
|
|
<< opNames[0] << ", " << getCppName(va->getType()) << ", \"";
|
2006-05-30 03:43:49 +00:00
|
|
|
printEscapedString(va->getName());
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << "\", " << bbname << ");";
|
2006-05-30 03:43:49 +00:00
|
|
|
break;
|
2006-05-29 00:57:22 +00:00
|
|
|
}
|
2006-05-30 03:43:49 +00:00
|
|
|
case Instruction::ExtractElement: {
|
|
|
|
const ExtractElementInst* eei = cast<ExtractElementInst>(I);
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << "ExtractElementInst* " << getCppName(eei)
|
2006-05-30 21:18:23 +00:00
|
|
|
<< " = new ExtractElementInst(" << opNames[0]
|
|
|
|
<< ", " << opNames[1] << ", \"";
|
2006-05-30 03:43:49 +00:00
|
|
|
printEscapedString(eei->getName());
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << "\", " << bbname << ");";
|
2006-05-30 03:43:49 +00:00
|
|
|
break;
|
2006-05-29 00:57:22 +00:00
|
|
|
}
|
2006-05-30 03:43:49 +00:00
|
|
|
case Instruction::InsertElement: {
|
|
|
|
const InsertElementInst* iei = cast<InsertElementInst>(I);
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << "InsertElementInst* " << getCppName(iei)
|
2006-05-30 21:18:23 +00:00
|
|
|
<< " = new InsertElementInst(" << opNames[0]
|
|
|
|
<< ", " << opNames[1] << ", " << opNames[2] << ", \"";
|
2006-05-30 03:43:49 +00:00
|
|
|
printEscapedString(iei->getName());
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << "\", " << bbname << ");";
|
2006-05-30 03:43:49 +00:00
|
|
|
break;
|
2006-05-29 00:57:22 +00:00
|
|
|
}
|
2006-05-30 03:43:49 +00:00
|
|
|
case Instruction::ShuffleVector: {
|
|
|
|
const ShuffleVectorInst* svi = cast<ShuffleVectorInst>(I);
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << "ShuffleVectorInst* " << getCppName(svi)
|
2006-05-30 21:18:23 +00:00
|
|
|
<< " = new ShuffleVectorInst(" << opNames[0]
|
|
|
|
<< ", " << opNames[1] << ", " << opNames[2] << ", \"";
|
2006-05-30 03:43:49 +00:00
|
|
|
printEscapedString(svi->getName());
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << "\", " << bbname << ");";
|
2006-05-30 03:43:49 +00:00
|
|
|
break;
|
2006-05-29 00:57:22 +00:00
|
|
|
}
|
|
|
|
}
|
2006-06-15 16:09:59 +00:00
|
|
|
DefinedValues.insert(I);
|
2006-08-14 22:35:15 +00:00
|
|
|
nl(Out);
|
2006-05-30 21:18:23 +00:00
|
|
|
delete [] opNames;
|
2006-05-29 00:57:22 +00:00
|
|
|
}
|
|
|
|
|
2006-05-31 17:31:38 +00:00
|
|
|
// Print out the types, constants and declarations needed by one function
|
|
|
|
void CppWriter::printFunctionUses(const Function* F) {
|
|
|
|
|
2006-08-14 22:35:15 +00:00
|
|
|
nl(Out) << "// Type Definitions"; nl(Out);
|
2006-06-01 23:43:47 +00:00
|
|
|
if (!is_inline) {
|
|
|
|
// Print the function's return type
|
|
|
|
printType(F->getReturnType());
|
2006-05-31 17:31:38 +00:00
|
|
|
|
2006-06-01 23:43:47 +00:00
|
|
|
// Print the function's function type
|
|
|
|
printType(F->getFunctionType());
|
2006-05-31 17:31:38 +00:00
|
|
|
|
2006-06-01 23:43:47 +00:00
|
|
|
// Print the types of each of the function's arguments
|
|
|
|
for(Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
|
|
|
|
AI != AE; ++AI) {
|
|
|
|
printType(AI->getType());
|
|
|
|
}
|
2006-05-31 17:31:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Print type definitions for every type referenced by an instruction and
|
|
|
|
// make a note of any global values or constants that are referenced
|
2007-06-16 20:48:27 +00:00
|
|
|
SmallPtrSet<GlobalValue*,64> gvs;
|
|
|
|
SmallPtrSet<Constant*,64> consts;
|
2006-05-31 17:31:38 +00:00
|
|
|
for (Function::const_iterator BB = F->begin(), BE = F->end(); BB != BE; ++BB){
|
|
|
|
for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
|
|
|
|
I != E; ++I) {
|
|
|
|
// Print the type of the instruction itself
|
|
|
|
printType(I->getType());
|
|
|
|
|
|
|
|
// Print the type of each of the instruction's operands
|
|
|
|
for (unsigned i = 0; i < I->getNumOperands(); ++i) {
|
|
|
|
Value* operand = I->getOperand(i);
|
|
|
|
printType(operand->getType());
|
2007-06-16 20:33:24 +00:00
|
|
|
|
|
|
|
// If the operand references a GVal or Constant, make a note of it
|
|
|
|
if (GlobalValue* GV = dyn_cast<GlobalValue>(operand)) {
|
2007-06-16 20:48:27 +00:00
|
|
|
gvs.insert(GV);
|
2007-06-16 20:33:24 +00:00
|
|
|
if (GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
|
|
|
|
if (GVar->hasInitializer())
|
2007-06-16 20:48:27 +00:00
|
|
|
consts.insert(GVar->getInitializer());
|
2007-06-16 20:33:24 +00:00
|
|
|
} else if (Constant* C = dyn_cast<Constant>(operand))
|
2007-06-16 20:48:27 +00:00
|
|
|
consts.insert(C);
|
2006-05-31 17:31:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print the function declarations for any functions encountered
|
2006-08-14 22:35:15 +00:00
|
|
|
nl(Out) << "// Function Declarations"; nl(Out);
|
2007-06-16 20:48:27 +00:00
|
|
|
for (SmallPtrSet<GlobalValue*,64>::iterator I = gvs.begin(), E = gvs.end();
|
2006-05-31 17:31:38 +00:00
|
|
|
I != E; ++I) {
|
2006-06-01 23:43:47 +00:00
|
|
|
if (Function* Fun = dyn_cast<Function>(*I)) {
|
|
|
|
if (!is_inline || Fun != F)
|
|
|
|
printFunctionHead(Fun);
|
|
|
|
}
|
2006-05-31 17:31:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Print the global variable declarations for any variables encountered
|
2006-08-14 22:35:15 +00:00
|
|
|
nl(Out) << "// Global Variable Declarations"; nl(Out);
|
2007-06-16 20:48:27 +00:00
|
|
|
for (SmallPtrSet<GlobalValue*,64>::iterator I = gvs.begin(), E = gvs.end();
|
2006-05-31 17:31:38 +00:00
|
|
|
I != E; ++I) {
|
|
|
|
if (GlobalVariable* F = dyn_cast<GlobalVariable>(*I))
|
|
|
|
printVariableHead(F);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print the constants found
|
2006-08-14 22:35:15 +00:00
|
|
|
nl(Out) << "// Constant Definitions"; nl(Out);
|
2007-06-16 20:48:27 +00:00
|
|
|
for (SmallPtrSet<Constant*,64>::iterator I = consts.begin(), E = consts.end();
|
2006-05-31 17:31:38 +00:00
|
|
|
I != E; ++I) {
|
2006-06-01 23:43:47 +00:00
|
|
|
printConstant(*I);
|
2006-05-31 17:31:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Process the global variables definitions now that all the constants have
|
|
|
|
// been emitted. These definitions just couple the gvars with their constant
|
|
|
|
// initializers.
|
2006-08-14 22:35:15 +00:00
|
|
|
nl(Out) << "// Global Variable Definitions"; nl(Out);
|
2007-06-16 20:48:27 +00:00
|
|
|
for (SmallPtrSet<GlobalValue*,64>::iterator I = gvs.begin(), E = gvs.end();
|
2006-05-31 17:31:38 +00:00
|
|
|
I != E; ++I) {
|
|
|
|
if (GlobalVariable* GV = dyn_cast<GlobalVariable>(*I))
|
|
|
|
printVariableBody(GV);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CppWriter::printFunctionHead(const Function* F) {
|
2006-08-14 22:35:15 +00:00
|
|
|
nl(Out) << "Function* " << getCppName(F);
|
2006-06-01 23:43:47 +00:00
|
|
|
if (is_inline) {
|
|
|
|
Out << " = mod->getFunction(\"";
|
|
|
|
printEscapedString(F->getName());
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << "\", " << getCppName(F->getFunctionType()) << ");";
|
|
|
|
nl(Out) << "if (!" << getCppName(F) << ") {";
|
|
|
|
nl(Out) << getCppName(F);
|
2006-06-01 23:43:47 +00:00
|
|
|
}
|
2006-08-14 22:35:15 +00:00
|
|
|
Out<< " = new Function(";
|
|
|
|
nl(Out,1) << "/*Type=*/" << getCppName(F->getFunctionType()) << ",";
|
|
|
|
nl(Out) << "/*Linkage=*/";
|
2006-05-31 17:31:38 +00:00
|
|
|
printLinkageType(F->getLinkage());
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << ",";
|
|
|
|
nl(Out) << "/*Name=*/\"";
|
2006-05-31 17:31:38 +00:00
|
|
|
printEscapedString(F->getName());
|
2007-01-30 20:08:39 +00:00
|
|
|
Out << "\", mod); " << (F->isDeclaration()? "// (external, no body)" : "");
|
2006-08-14 22:35:15 +00:00
|
|
|
nl(Out,-1);
|
2006-05-31 17:31:38 +00:00
|
|
|
printCppName(F);
|
|
|
|
Out << "->setCallingConv(";
|
|
|
|
printCallingConv(F->getCallingConv());
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << ");";
|
|
|
|
nl(Out);
|
2006-05-31 17:31:38 +00:00
|
|
|
if (F->hasSection()) {
|
|
|
|
printCppName(F);
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << "->setSection(\"" << F->getSection() << "\");";
|
|
|
|
nl(Out);
|
2006-05-31 17:31:38 +00:00
|
|
|
}
|
|
|
|
if (F->getAlignment()) {
|
|
|
|
printCppName(F);
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << "->setAlignment(" << F->getAlignment() << ");";
|
|
|
|
nl(Out);
|
2006-05-31 17:31:38 +00:00
|
|
|
}
|
2007-11-27 13:23:08 +00:00
|
|
|
if (F->getVisibility() != GlobalValue::DefaultVisibility) {
|
|
|
|
printCppName(F);
|
|
|
|
Out << "->setVisibility(";
|
|
|
|
printVisibilityType(F->getVisibility());
|
|
|
|
Out << ");";
|
|
|
|
nl(Out);
|
|
|
|
}
|
2007-12-25 22:16:06 +00:00
|
|
|
if (F->hasCollector()) {
|
|
|
|
printCppName(F);
|
|
|
|
Out << "->setCollector(\"" << F->getCollector() << "\");";
|
|
|
|
nl(Out);
|
|
|
|
}
|
2006-06-01 23:43:47 +00:00
|
|
|
if (is_inline) {
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << "}";
|
|
|
|
nl(Out);
|
2006-06-01 23:43:47 +00:00
|
|
|
}
|
2007-11-27 13:23:08 +00:00
|
|
|
printParamAttrs(F->getParamAttrs(), getCppName(F));
|
|
|
|
printCppName(F);
|
|
|
|
Out << "->setParamAttrs(" << getCppName(F) << "_PAL);";
|
|
|
|
nl(Out);
|
2006-05-31 17:31:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CppWriter::printFunctionBody(const Function *F) {
|
2007-01-30 20:08:39 +00:00
|
|
|
if (F->isDeclaration())
|
2006-05-31 17:31:38 +00:00
|
|
|
return; // external functions have no bodies.
|
|
|
|
|
|
|
|
// Clear the DefinedValues and ForwardRefs maps because we can't have
|
|
|
|
// cross-function forward refs
|
|
|
|
ForwardRefs.clear();
|
|
|
|
DefinedValues.clear();
|
|
|
|
|
|
|
|
// Create all the argument values
|
2006-06-01 23:43:47 +00:00
|
|
|
if (!is_inline) {
|
|
|
|
if (!F->arg_empty()) {
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << "Function::arg_iterator args = " << getCppName(F)
|
|
|
|
<< "->arg_begin();";
|
|
|
|
nl(Out);
|
2006-06-01 23:43:47 +00:00
|
|
|
}
|
|
|
|
for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
|
|
|
|
AI != AE; ++AI) {
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << "Value* " << getCppName(AI) << " = args++;";
|
|
|
|
nl(Out);
|
|
|
|
if (AI->hasName()) {
|
|
|
|
Out << getCppName(AI) << "->setName(\"" << AI->getName() << "\");";
|
|
|
|
nl(Out);
|
|
|
|
}
|
2006-06-01 23:43:47 +00:00
|
|
|
}
|
2006-05-31 17:31:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create all the basic blocks
|
2006-08-14 22:35:15 +00:00
|
|
|
nl(Out);
|
2006-05-31 17:31:38 +00:00
|
|
|
for (Function::const_iterator BI = F->begin(), BE = F->end();
|
|
|
|
BI != BE; ++BI) {
|
|
|
|
std::string bbname(getCppName(BI));
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << "BasicBlock* " << bbname << " = new BasicBlock(\"";
|
2006-05-31 17:31:38 +00:00
|
|
|
if (BI->hasName())
|
|
|
|
printEscapedString(BI->getName());
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << "\"," << getCppName(BI->getParent()) << ",0);";
|
|
|
|
nl(Out);
|
2006-05-31 17:31:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Output all of its basic blocks... for the function
|
|
|
|
for (Function::const_iterator BI = F->begin(), BE = F->end();
|
|
|
|
BI != BE; ++BI) {
|
|
|
|
std::string bbname(getCppName(BI));
|
2006-08-14 22:35:15 +00:00
|
|
|
nl(Out) << "// Block " << BI->getName() << " (" << bbname << ")";
|
|
|
|
nl(Out);
|
2006-05-31 17:31:38 +00:00
|
|
|
|
|
|
|
// Output all of the instructions in the basic block...
|
|
|
|
for (BasicBlock::const_iterator I = BI->begin(), E = BI->end();
|
|
|
|
I != E; ++I) {
|
|
|
|
printInstruction(I,bbname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loop over the ForwardRefs and resolve them now that all instructions
|
|
|
|
// are generated.
|
2006-08-14 22:35:15 +00:00
|
|
|
if (!ForwardRefs.empty()) {
|
|
|
|
nl(Out) << "// Resolve Forward References";
|
|
|
|
nl(Out);
|
|
|
|
}
|
|
|
|
|
2006-05-31 17:31:38 +00:00
|
|
|
while (!ForwardRefs.empty()) {
|
|
|
|
ForwardRefMap::iterator I = ForwardRefs.begin();
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << I->second << "->replaceAllUsesWith("
|
|
|
|
<< getCppName(I->first) << "); delete " << I->second << ";";
|
|
|
|
nl(Out);
|
2006-05-31 17:31:38 +00:00
|
|
|
ForwardRefs.erase(I);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-06-01 23:43:47 +00:00
|
|
|
void CppWriter::printInline(const std::string& fname, const std::string& func) {
|
2007-02-05 21:19:13 +00:00
|
|
|
const Function* F = TheModule->getFunction(func);
|
2006-06-01 23:43:47 +00:00
|
|
|
if (!F) {
|
|
|
|
error(std::string("Function '") + func + "' not found in input module");
|
|
|
|
return;
|
|
|
|
}
|
2007-01-30 20:08:39 +00:00
|
|
|
if (F->isDeclaration()) {
|
2006-06-01 23:43:47 +00:00
|
|
|
error(std::string("Function '") + func + "' is external!");
|
|
|
|
return;
|
|
|
|
}
|
2006-08-14 22:35:15 +00:00
|
|
|
nl(Out) << "BasicBlock* " << fname << "(Module* mod, Function *"
|
2006-06-01 23:43:47 +00:00
|
|
|
<< getCppName(F);
|
|
|
|
unsigned arg_count = 1;
|
|
|
|
for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
|
|
|
|
AI != AE; ++AI) {
|
|
|
|
Out << ", Value* arg_" << arg_count;
|
|
|
|
}
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << ") {";
|
|
|
|
nl(Out);
|
2006-06-01 23:43:47 +00:00
|
|
|
is_inline = true;
|
|
|
|
printFunctionUses(F);
|
|
|
|
printFunctionBody(F);
|
|
|
|
is_inline = false;
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << "return " << getCppName(F->begin()) << ";";
|
|
|
|
nl(Out) << "}";
|
|
|
|
nl(Out);
|
2006-06-01 23:43:47 +00:00
|
|
|
}
|
|
|
|
|
2006-05-31 17:31:38 +00:00
|
|
|
void CppWriter::printModuleBody() {
|
|
|
|
// Print out all the type definitions
|
2006-08-14 22:35:15 +00:00
|
|
|
nl(Out) << "// Type Definitions"; nl(Out);
|
2006-05-31 17:31:38 +00:00
|
|
|
printTypes(TheModule);
|
|
|
|
|
|
|
|
// Functions can call each other and global variables can reference them so
|
|
|
|
// define all the functions first before emitting their function bodies.
|
2006-08-14 22:35:15 +00:00
|
|
|
nl(Out) << "// Function Declarations"; nl(Out);
|
2006-05-31 17:31:38 +00:00
|
|
|
for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
|
|
|
|
I != E; ++I)
|
|
|
|
printFunctionHead(I);
|
|
|
|
|
|
|
|
// Process the global variables declarations. We can't initialze them until
|
|
|
|
// after the constants are printed so just print a header for each global
|
2006-08-14 22:35:15 +00:00
|
|
|
nl(Out) << "// Global Variable Declarations\n"; nl(Out);
|
2006-05-31 17:31:38 +00:00
|
|
|
for (Module::const_global_iterator I = TheModule->global_begin(),
|
|
|
|
E = TheModule->global_end(); I != E; ++I) {
|
|
|
|
printVariableHead(I);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print out all the constants definitions. Constants don't recurse except
|
|
|
|
// through GlobalValues. All GlobalValues have been declared at this point
|
|
|
|
// so we can proceed to generate the constants.
|
2006-08-14 22:35:15 +00:00
|
|
|
nl(Out) << "// Constant Definitions"; nl(Out);
|
2006-05-31 17:31:38 +00:00
|
|
|
printConstants(TheModule);
|
|
|
|
|
|
|
|
// Process the global variables definitions now that all the constants have
|
|
|
|
// been emitted. These definitions just couple the gvars with their constant
|
|
|
|
// initializers.
|
2006-08-14 22:35:15 +00:00
|
|
|
nl(Out) << "// Global Variable Definitions"; nl(Out);
|
2006-05-31 17:31:38 +00:00
|
|
|
for (Module::const_global_iterator I = TheModule->global_begin(),
|
|
|
|
E = TheModule->global_end(); I != E; ++I) {
|
|
|
|
printVariableBody(I);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, we can safely put out all of the function bodies.
|
2006-08-14 22:35:15 +00:00
|
|
|
nl(Out) << "// Function Definitions"; nl(Out);
|
2006-05-31 17:31:38 +00:00
|
|
|
for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
|
|
|
|
I != E; ++I) {
|
2007-01-30 20:08:39 +00:00
|
|
|
if (!I->isDeclaration()) {
|
2006-08-14 22:35:15 +00:00
|
|
|
nl(Out) << "// Function: " << I->getName() << " (" << getCppName(I)
|
|
|
|
<< ")";
|
|
|
|
nl(Out) << "{";
|
|
|
|
nl(Out,1);
|
2006-05-31 17:31:38 +00:00
|
|
|
printFunctionBody(I);
|
2006-08-14 22:35:15 +00:00
|
|
|
nl(Out,-1) << "}";
|
|
|
|
nl(Out);
|
2006-05-31 17:31:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CppWriter::printProgram(
|
|
|
|
const std::string& fname,
|
|
|
|
const std::string& mName
|
|
|
|
) {
|
|
|
|
Out << "#include <llvm/Module.h>\n";
|
|
|
|
Out << "#include <llvm/DerivedTypes.h>\n";
|
|
|
|
Out << "#include <llvm/Constants.h>\n";
|
|
|
|
Out << "#include <llvm/GlobalVariable.h>\n";
|
|
|
|
Out << "#include <llvm/Function.h>\n";
|
|
|
|
Out << "#include <llvm/CallingConv.h>\n";
|
|
|
|
Out << "#include <llvm/BasicBlock.h>\n";
|
|
|
|
Out << "#include <llvm/Instructions.h>\n";
|
|
|
|
Out << "#include <llvm/InlineAsm.h>\n";
|
2007-04-11 12:28:56 +00:00
|
|
|
Out << "#include <llvm/ParameterAttributes.h>\n";
|
2006-06-01 23:43:47 +00:00
|
|
|
Out << "#include <llvm/Support/MathExtras.h>\n";
|
2006-05-31 17:31:38 +00:00
|
|
|
Out << "#include <llvm/Pass.h>\n";
|
|
|
|
Out << "#include <llvm/PassManager.h>\n";
|
|
|
|
Out << "#include <llvm/Analysis/Verifier.h>\n";
|
|
|
|
Out << "#include <llvm/Assembly/PrintModulePass.h>\n";
|
|
|
|
Out << "#include <algorithm>\n";
|
|
|
|
Out << "#include <iostream>\n\n";
|
|
|
|
Out << "using namespace llvm;\n\n";
|
|
|
|
Out << "Module* " << fname << "();\n\n";
|
|
|
|
Out << "int main(int argc, char**argv) {\n";
|
2007-06-16 16:17:35 +00:00
|
|
|
Out << " Module* Mod = " << fname << "();\n";
|
2006-05-31 17:31:38 +00:00
|
|
|
Out << " verifyModule(*Mod, PrintMessageAction);\n";
|
|
|
|
Out << " std::cerr.flush();\n";
|
|
|
|
Out << " std::cout.flush();\n";
|
|
|
|
Out << " PassManager PM;\n";
|
2007-04-11 12:28:56 +00:00
|
|
|
Out << " PM.add(new PrintModulePass(&llvm::cout));\n";
|
2006-05-31 17:31:38 +00:00
|
|
|
Out << " PM.run(*Mod);\n";
|
|
|
|
Out << " return 0;\n";
|
|
|
|
Out << "}\n\n";
|
|
|
|
printModule(fname,mName);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CppWriter::printModule(
|
|
|
|
const std::string& fname,
|
|
|
|
const std::string& mName
|
|
|
|
) {
|
2006-08-14 22:35:15 +00:00
|
|
|
nl(Out) << "Module* " << fname << "() {";
|
|
|
|
nl(Out,1) << "// Module Construction";
|
|
|
|
nl(Out) << "Module* mod = new Module(\"" << mName << "\");";
|
2007-04-11 12:28:56 +00:00
|
|
|
if (!TheModule->getTargetTriple().empty()) {
|
|
|
|
nl(Out) << "mod->setDataLayout(\"" << TheModule->getDataLayout() << "\");";
|
2006-05-31 17:31:38 +00:00
|
|
|
}
|
2006-08-14 22:35:15 +00:00
|
|
|
if (!TheModule->getTargetTriple().empty()) {
|
2007-04-11 12:28:56 +00:00
|
|
|
nl(Out) << "mod->setTargetTriple(\"" << TheModule->getTargetTriple()
|
|
|
|
<< "\");";
|
2006-08-14 22:35:15 +00:00
|
|
|
}
|
2006-05-31 17:31:38 +00:00
|
|
|
|
|
|
|
if (!TheModule->getModuleInlineAsm().empty()) {
|
2007-04-11 12:28:56 +00:00
|
|
|
nl(Out) << "mod->setModuleInlineAsm(\"";
|
2006-05-31 17:31:38 +00:00
|
|
|
printEscapedString(TheModule->getModuleInlineAsm());
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << "\");";
|
2006-05-31 17:31:38 +00:00
|
|
|
}
|
2007-04-11 12:28:56 +00:00
|
|
|
nl(Out);
|
2006-05-31 17:31:38 +00:00
|
|
|
|
|
|
|
// Loop over the dependent libraries and emit them.
|
|
|
|
Module::lib_iterator LI = TheModule->lib_begin();
|
|
|
|
Module::lib_iterator LE = TheModule->lib_end();
|
|
|
|
while (LI != LE) {
|
2006-08-14 22:35:15 +00:00
|
|
|
Out << "mod->addLibrary(\"" << *LI << "\");";
|
|
|
|
nl(Out);
|
2006-05-31 17:31:38 +00:00
|
|
|
++LI;
|
|
|
|
}
|
|
|
|
printModuleBody();
|
2006-08-14 22:35:15 +00:00
|
|
|
nl(Out) << "return mod;";
|
|
|
|
nl(Out,-1) << "}";
|
|
|
|
nl(Out);
|
2006-05-31 17:31:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CppWriter::printContents(
|
|
|
|
const std::string& fname, // Name of generated function
|
|
|
|
const std::string& mName // Name of module generated module
|
|
|
|
) {
|
|
|
|
Out << "\nModule* " << fname << "(Module *mod) {\n";
|
|
|
|
Out << "\nmod->setModuleIdentifier(\"" << mName << "\");\n";
|
|
|
|
printModuleBody();
|
|
|
|
Out << "\nreturn mod;\n";
|
|
|
|
Out << "\n}\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void CppWriter::printFunction(
|
|
|
|
const std::string& fname, // Name of generated function
|
|
|
|
const std::string& funcName // Name of function to generate
|
|
|
|
) {
|
2007-02-05 21:19:13 +00:00
|
|
|
const Function* F = TheModule->getFunction(funcName);
|
2006-05-31 17:31:38 +00:00
|
|
|
if (!F) {
|
|
|
|
error(std::string("Function '") + funcName + "' not found in input module");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Out << "\nFunction* " << fname << "(Module *mod) {\n";
|
|
|
|
printFunctionUses(F);
|
|
|
|
printFunctionHead(F);
|
|
|
|
printFunctionBody(F);
|
|
|
|
Out << "return " << getCppName(F) << ";\n";
|
|
|
|
Out << "}\n";
|
|
|
|
}
|
|
|
|
|
2007-11-13 18:22:33 +00:00
|
|
|
void CppWriter::printFunctions() {
|
|
|
|
const Module::FunctionListType &funcs = TheModule->getFunctionList();
|
|
|
|
Module::const_iterator I = funcs.begin();
|
|
|
|
Module::const_iterator IE = funcs.end();
|
|
|
|
|
|
|
|
for (; I != IE; ++I) {
|
|
|
|
const Function &func = *I;
|
|
|
|
if (!func.isDeclaration()) {
|
|
|
|
std::string name("define_");
|
|
|
|
name += func.getName();
|
|
|
|
printFunction(name, func.getName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-05-31 17:31:38 +00:00
|
|
|
void CppWriter::printVariable(
|
|
|
|
const std::string& fname, /// Name of generated function
|
|
|
|
const std::string& varName // Name of variable to generate
|
|
|
|
) {
|
|
|
|
const GlobalVariable* GV = TheModule->getNamedGlobal(varName);
|
|
|
|
|
|
|
|
if (!GV) {
|
|
|
|
error(std::string("Variable '") + varName + "' not found in input module");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Out << "\nGlobalVariable* " << fname << "(Module *mod) {\n";
|
|
|
|
printVariableUses(GV);
|
|
|
|
printVariableHead(GV);
|
|
|
|
printVariableBody(GV);
|
|
|
|
Out << "return " << getCppName(GV) << ";\n";
|
|
|
|
Out << "}\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void CppWriter::printType(
|
|
|
|
const std::string& fname, /// Name of generated function
|
|
|
|
const std::string& typeName // Name of type to generate
|
|
|
|
) {
|
|
|
|
const Type* Ty = TheModule->getTypeByName(typeName);
|
|
|
|
if (!Ty) {
|
|
|
|
error(std::string("Type '") + typeName + "' not found in input module");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Out << "\nType* " << fname << "(Module *mod) {\n";
|
|
|
|
printType(Ty);
|
|
|
|
Out << "return " << getCppName(Ty) << ";\n";
|
|
|
|
Out << "}\n";
|
|
|
|
}
|
|
|
|
|
2006-05-29 00:57:22 +00:00
|
|
|
} // end anonymous llvm
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
void WriteModuleToCppFile(Module* mod, std::ostream& o) {
|
2006-05-31 17:31:38 +00:00
|
|
|
// Initialize a CppWriter for us to use
|
|
|
|
CppWriter W(o, mod);
|
|
|
|
|
|
|
|
// Emit a header
|
2006-05-31 04:43:19 +00:00
|
|
|
o << "// Generated by llvm2cpp - DO NOT MODIFY!\n\n";
|
2006-05-31 17:31:38 +00:00
|
|
|
|
|
|
|
// Get the name of the function we're supposed to generate
|
2006-05-30 21:18:23 +00:00
|
|
|
std::string fname = FuncName.getValue();
|
2006-05-31 17:31:38 +00:00
|
|
|
|
|
|
|
// Get the name of the thing we are to generate
|
|
|
|
std::string tgtname = NameToGenerate.getValue();
|
|
|
|
if (GenerationType == GenModule ||
|
|
|
|
GenerationType == GenContents ||
|
2007-11-13 18:22:33 +00:00
|
|
|
GenerationType == GenProgram ||
|
|
|
|
GenerationType == GenFunctions) {
|
2006-05-31 17:31:38 +00:00
|
|
|
if (tgtname == "!bad!") {
|
|
|
|
if (mod->getModuleIdentifier() == "-")
|
|
|
|
tgtname = "<stdin>";
|
|
|
|
else
|
|
|
|
tgtname = mod->getModuleIdentifier();
|
|
|
|
}
|
|
|
|
} else if (tgtname == "!bad!") {
|
|
|
|
W.error("You must use the -for option with -gen-{function,variable,type}");
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (WhatToGenerate(GenerationType)) {
|
|
|
|
case GenProgram:
|
|
|
|
if (fname.empty())
|
|
|
|
fname = "makeLLVMModule";
|
|
|
|
W.printProgram(fname,tgtname);
|
|
|
|
break;
|
|
|
|
case GenModule:
|
|
|
|
if (fname.empty())
|
|
|
|
fname = "makeLLVMModule";
|
|
|
|
W.printModule(fname,tgtname);
|
|
|
|
break;
|
|
|
|
case GenContents:
|
|
|
|
if (fname.empty())
|
|
|
|
fname = "makeLLVMModuleContents";
|
|
|
|
W.printContents(fname,tgtname);
|
|
|
|
break;
|
|
|
|
case GenFunction:
|
|
|
|
if (fname.empty())
|
|
|
|
fname = "makeLLVMFunction";
|
|
|
|
W.printFunction(fname,tgtname);
|
|
|
|
break;
|
2007-11-13 18:22:33 +00:00
|
|
|
case GenFunctions:
|
|
|
|
W.printFunctions();
|
|
|
|
break;
|
2006-06-01 23:43:47 +00:00
|
|
|
case GenInline:
|
|
|
|
if (fname.empty())
|
|
|
|
fname = "makeLLVMInline";
|
|
|
|
W.printInline(fname,tgtname);
|
|
|
|
break;
|
2006-05-31 17:31:38 +00:00
|
|
|
case GenVariable:
|
|
|
|
if (fname.empty())
|
|
|
|
fname = "makeLLVMVariable";
|
|
|
|
W.printVariable(fname,tgtname);
|
|
|
|
break;
|
|
|
|
case GenType:
|
|
|
|
if (fname.empty())
|
|
|
|
fname = "makeLLVMType";
|
|
|
|
W.printType(fname,tgtname);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
W.error("Invalid generation option");
|
2006-05-30 21:18:23 +00:00
|
|
|
}
|
2006-05-29 00:57:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|