CPPBackend.cpp 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072
  1. //===-- CPPBackend.cpp - Library for converting LLVM code to C++ code -----===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements the writing of the LLVM IR as a set of C++ calls to the
  11. // LLVM IR interface. The input module is assumed to be verified.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "CPPTargetMachine.h"
  15. #include "llvm/CallingConv.h"
  16. #include "llvm/Constants.h"
  17. #include "llvm/DerivedTypes.h"
  18. #include "llvm/InlineAsm.h"
  19. #include "llvm/Instruction.h"
  20. #include "llvm/Instructions.h"
  21. #include "llvm/Module.h"
  22. #include "llvm/Pass.h"
  23. #include "llvm/PassManager.h"
  24. #include "llvm/MC/MCAsmInfo.h"
  25. #include "llvm/MC/MCInstrInfo.h"
  26. #include "llvm/MC/MCSubtargetInfo.h"
  27. #include "llvm/ADT/SmallPtrSet.h"
  28. #include "llvm/Support/CommandLine.h"
  29. #include "llvm/Support/ErrorHandling.h"
  30. #include "llvm/Support/FormattedStream.h"
  31. #include "llvm/Support/TargetRegistry.h"
  32. #include "llvm/ADT/StringExtras.h"
  33. #include "llvm/Config/config.h"
  34. #include <algorithm>
  35. #include <set>
  36. #include <map>
  37. using namespace llvm;
  38. static cl::opt<std::string>
  39. FuncName("cppfname", cl::desc("Specify the name of the generated function"),
  40. cl::value_desc("function name"));
  41. enum WhatToGenerate {
  42. GenProgram,
  43. GenModule,
  44. GenContents,
  45. GenFunction,
  46. GenFunctions,
  47. GenInline,
  48. GenVariable,
  49. GenType
  50. };
  51. static cl::opt<WhatToGenerate> GenerationType("cppgen", cl::Optional,
  52. cl::desc("Choose what kind of output to generate"),
  53. cl::init(GenProgram),
  54. cl::values(
  55. clEnumValN(GenProgram, "program", "Generate a complete program"),
  56. clEnumValN(GenModule, "module", "Generate a module definition"),
  57. clEnumValN(GenContents, "contents", "Generate contents of a module"),
  58. clEnumValN(GenFunction, "function", "Generate a function definition"),
  59. clEnumValN(GenFunctions,"functions", "Generate all function definitions"),
  60. clEnumValN(GenInline, "inline", "Generate an inline function"),
  61. clEnumValN(GenVariable, "variable", "Generate a variable definition"),
  62. clEnumValN(GenType, "type", "Generate a type definition"),
  63. clEnumValEnd
  64. )
  65. );
  66. static cl::opt<std::string> NameToGenerate("cppfor", cl::Optional,
  67. cl::desc("Specify the name of the thing to generate"),
  68. cl::init("!bad!"));
  69. extern "C" void LLVMInitializeCppBackendTarget() {
  70. // Register the target.
  71. RegisterTargetMachine<CPPTargetMachine> X(TheCppBackendTarget);
  72. }
  73. namespace {
  74. typedef std::vector<Type*> TypeList;
  75. typedef std::map<Type*,std::string> TypeMap;
  76. typedef std::map<const Value*,std::string> ValueMap;
  77. typedef std::set<std::string> NameSet;
  78. typedef std::set<Type*> TypeSet;
  79. typedef std::set<const Value*> ValueSet;
  80. typedef std::map<const Value*,std::string> ForwardRefMap;
  81. /// CppWriter - This class is the main chunk of code that converts an LLVM
  82. /// module to a C++ translation unit.
  83. class CppWriter : public ModulePass {
  84. formatted_raw_ostream &Out;
  85. const Module *TheModule;
  86. uint64_t uniqueNum;
  87. TypeMap TypeNames;
  88. ValueMap ValueNames;
  89. NameSet UsedNames;
  90. TypeSet DefinedTypes;
  91. ValueSet DefinedValues;
  92. ForwardRefMap ForwardRefs;
  93. bool is_inline;
  94. unsigned indent_level;
  95. public:
  96. static char ID;
  97. explicit CppWriter(formatted_raw_ostream &o) :
  98. ModulePass(ID), Out(o), uniqueNum(0), is_inline(false), indent_level(0){}
  99. virtual const char *getPassName() const { return "C++ backend"; }
  100. bool runOnModule(Module &M);
  101. void printProgram(const std::string& fname, const std::string& modName );
  102. void printModule(const std::string& fname, const std::string& modName );
  103. void printContents(const std::string& fname, const std::string& modName );
  104. void printFunction(const std::string& fname, const std::string& funcName );
  105. void printFunctions();
  106. void printInline(const std::string& fname, const std::string& funcName );
  107. void printVariable(const std::string& fname, const std::string& varName );
  108. void printType(const std::string& fname, const std::string& typeName );
  109. void error(const std::string& msg);
  110. formatted_raw_ostream& nl(formatted_raw_ostream &Out, int delta = 0);
  111. inline void in() { indent_level++; }
  112. inline void out() { if (indent_level >0) indent_level--; }
  113. private:
  114. void printLinkageType(GlobalValue::LinkageTypes LT);
  115. void printVisibilityType(GlobalValue::VisibilityTypes VisTypes);
  116. void printCallingConv(CallingConv::ID cc);
  117. void printEscapedString(const std::string& str);
  118. void printCFP(const ConstantFP* CFP);
  119. std::string getCppName(Type* val);
  120. inline void printCppName(Type* val);
  121. std::string getCppName(const Value* val);
  122. inline void printCppName(const Value* val);
  123. void printAttributes(const AttrListPtr &PAL, const std::string &name);
  124. void printType(Type* Ty);
  125. void printTypes(const Module* M);
  126. void printConstant(const Constant *CPV);
  127. void printConstants(const Module* M);
  128. void printVariableUses(const GlobalVariable *GV);
  129. void printVariableHead(const GlobalVariable *GV);
  130. void printVariableBody(const GlobalVariable *GV);
  131. void printFunctionUses(const Function *F);
  132. void printFunctionHead(const Function *F);
  133. void printFunctionBody(const Function *F);
  134. void printInstruction(const Instruction *I, const std::string& bbname);
  135. std::string getOpName(const Value*);
  136. void printModuleBody();
  137. };
  138. } // end anonymous namespace.
  139. formatted_raw_ostream &CppWriter::nl(formatted_raw_ostream &Out, int delta) {
  140. Out << '\n';
  141. if (delta >= 0 || indent_level >= unsigned(-delta))
  142. indent_level += delta;
  143. Out.indent(indent_level);
  144. return Out;
  145. }
  146. static inline void sanitize(std::string &str) {
  147. for (size_t i = 0; i < str.length(); ++i)
  148. if (!isalnum(str[i]) && str[i] != '_')
  149. str[i] = '_';
  150. }
  151. static std::string getTypePrefix(Type *Ty) {
  152. switch (Ty->getTypeID()) {
  153. case Type::VoidTyID: return "void_";
  154. case Type::IntegerTyID:
  155. return "int" + utostr(cast<IntegerType>(Ty)->getBitWidth()) + "_";
  156. case Type::FloatTyID: return "float_";
  157. case Type::DoubleTyID: return "double_";
  158. case Type::LabelTyID: return "label_";
  159. case Type::FunctionTyID: return "func_";
  160. case Type::StructTyID: return "struct_";
  161. case Type::ArrayTyID: return "array_";
  162. case Type::PointerTyID: return "ptr_";
  163. case Type::VectorTyID: return "packed_";
  164. default: return "other_";
  165. }
  166. }
  167. void CppWriter::error(const std::string& msg) {
  168. report_fatal_error(msg);
  169. }
  170. // printCFP - Print a floating point constant .. very carefully :)
  171. // This makes sure that conversion to/from floating yields the same binary
  172. // result so that we don't lose precision.
  173. void CppWriter::printCFP(const ConstantFP *CFP) {
  174. bool ignored;
  175. APFloat APF = APFloat(CFP->getValueAPF()); // copy
  176. if (CFP->getType() == Type::getFloatTy(CFP->getContext()))
  177. APF.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &ignored);
  178. Out << "ConstantFP::get(mod->getContext(), ";
  179. Out << "APFloat(";
  180. #if HAVE_PRINTF_A
  181. char Buffer[100];
  182. sprintf(Buffer, "%A", APF.convertToDouble());
  183. if ((!strncmp(Buffer, "0x", 2) ||
  184. !strncmp(Buffer, "-0x", 3) ||
  185. !strncmp(Buffer, "+0x", 3)) &&
  186. APF.bitwiseIsEqual(APFloat(atof(Buffer)))) {
  187. if (CFP->getType() == Type::getDoubleTy(CFP->getContext()))
  188. Out << "BitsToDouble(" << Buffer << ")";
  189. else
  190. Out << "BitsToFloat((float)" << Buffer << ")";
  191. Out << ")";
  192. } else {
  193. #endif
  194. std::string StrVal = ftostr(CFP->getValueAPF());
  195. while (StrVal[0] == ' ')
  196. StrVal.erase(StrVal.begin());
  197. // Check to make sure that the stringized number is not some string like
  198. // "Inf" or NaN. Check that the string matches the "[-+]?[0-9]" regex.
  199. if (((StrVal[0] >= '0' && StrVal[0] <= '9') ||
  200. ((StrVal[0] == '-' || StrVal[0] == '+') &&
  201. (StrVal[1] >= '0' && StrVal[1] <= '9'))) &&
  202. (CFP->isExactlyValue(atof(StrVal.c_str())))) {
  203. if (CFP->getType() == Type::getDoubleTy(CFP->getContext()))
  204. Out << StrVal;
  205. else
  206. Out << StrVal << "f";
  207. } else if (CFP->getType() == Type::getDoubleTy(CFP->getContext()))
  208. Out << "BitsToDouble(0x"
  209. << utohexstr(CFP->getValueAPF().bitcastToAPInt().getZExtValue())
  210. << "ULL) /* " << StrVal << " */";
  211. else
  212. Out << "BitsToFloat(0x"
  213. << utohexstr((uint32_t)CFP->getValueAPF().
  214. bitcastToAPInt().getZExtValue())
  215. << "U) /* " << StrVal << " */";
  216. Out << ")";
  217. #if HAVE_PRINTF_A
  218. }
  219. #endif
  220. Out << ")";
  221. }
  222. void CppWriter::printCallingConv(CallingConv::ID cc){
  223. // Print the calling convention.
  224. switch (cc) {
  225. case CallingConv::C: Out << "CallingConv::C"; break;
  226. case CallingConv::Fast: Out << "CallingConv::Fast"; break;
  227. case CallingConv::Cold: Out << "CallingConv::Cold"; break;
  228. case CallingConv::FirstTargetCC: Out << "CallingConv::FirstTargetCC"; break;
  229. default: Out << cc; break;
  230. }
  231. }
  232. void CppWriter::printLinkageType(GlobalValue::LinkageTypes LT) {
  233. switch (LT) {
  234. case GlobalValue::InternalLinkage:
  235. Out << "GlobalValue::InternalLinkage"; break;
  236. case GlobalValue::PrivateLinkage:
  237. Out << "GlobalValue::PrivateLinkage"; break;
  238. case GlobalValue::LinkerPrivateLinkage:
  239. Out << "GlobalValue::LinkerPrivateLinkage"; break;
  240. case GlobalValue::LinkerPrivateWeakLinkage:
  241. Out << "GlobalValue::LinkerPrivateWeakLinkage"; break;
  242. case GlobalValue::LinkerPrivateWeakDefAutoLinkage:
  243. Out << "GlobalValue::LinkerPrivateWeakDefAutoLinkage"; break;
  244. case GlobalValue::AvailableExternallyLinkage:
  245. Out << "GlobalValue::AvailableExternallyLinkage "; break;
  246. case GlobalValue::LinkOnceAnyLinkage:
  247. Out << "GlobalValue::LinkOnceAnyLinkage "; break;
  248. case GlobalValue::LinkOnceODRLinkage:
  249. Out << "GlobalValue::LinkOnceODRLinkage "; break;
  250. case GlobalValue::WeakAnyLinkage:
  251. Out << "GlobalValue::WeakAnyLinkage"; break;
  252. case GlobalValue::WeakODRLinkage:
  253. Out << "GlobalValue::WeakODRLinkage"; break;
  254. case GlobalValue::AppendingLinkage:
  255. Out << "GlobalValue::AppendingLinkage"; break;
  256. case GlobalValue::ExternalLinkage:
  257. Out << "GlobalValue::ExternalLinkage"; break;
  258. case GlobalValue::DLLImportLinkage:
  259. Out << "GlobalValue::DLLImportLinkage"; break;
  260. case GlobalValue::DLLExportLinkage:
  261. Out << "GlobalValue::DLLExportLinkage"; break;
  262. case GlobalValue::ExternalWeakLinkage:
  263. Out << "GlobalValue::ExternalWeakLinkage"; break;
  264. case GlobalValue::CommonLinkage:
  265. Out << "GlobalValue::CommonLinkage"; break;
  266. }
  267. }
  268. void CppWriter::printVisibilityType(GlobalValue::VisibilityTypes VisType) {
  269. switch (VisType) {
  270. case GlobalValue::DefaultVisibility:
  271. Out << "GlobalValue::DefaultVisibility";
  272. break;
  273. case GlobalValue::HiddenVisibility:
  274. Out << "GlobalValue::HiddenVisibility";
  275. break;
  276. case GlobalValue::ProtectedVisibility:
  277. Out << "GlobalValue::ProtectedVisibility";
  278. break;
  279. }
  280. }
  281. // printEscapedString - Print each character of the specified string, escaping
  282. // it if it is not printable or if it is an escape char.
  283. void CppWriter::printEscapedString(const std::string &Str) {
  284. for (unsigned i = 0, e = Str.size(); i != e; ++i) {
  285. unsigned char C = Str[i];
  286. if (isprint(C) && C != '"' && C != '\\') {
  287. Out << C;
  288. } else {
  289. Out << "\\x"
  290. << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
  291. << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
  292. }
  293. }
  294. }
  295. std::string CppWriter::getCppName(Type* Ty) {
  296. // First, handle the primitive types .. easy
  297. if (Ty->isPrimitiveType() || Ty->isIntegerTy()) {
  298. switch (Ty->getTypeID()) {
  299. case Type::VoidTyID: return "Type::getVoidTy(mod->getContext())";
  300. case Type::IntegerTyID: {
  301. unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth();
  302. return "IntegerType::get(mod->getContext(), " + utostr(BitWidth) + ")";
  303. }
  304. case Type::X86_FP80TyID: return "Type::getX86_FP80Ty(mod->getContext())";
  305. case Type::FloatTyID: return "Type::getFloatTy(mod->getContext())";
  306. case Type::DoubleTyID: return "Type::getDoubleTy(mod->getContext())";
  307. case Type::LabelTyID: return "Type::getLabelTy(mod->getContext())";
  308. case Type::X86_MMXTyID: return "Type::getX86_MMXTy(mod->getContext())";
  309. default:
  310. error("Invalid primitive type");
  311. break;
  312. }
  313. // shouldn't be returned, but make it sensible
  314. return "Type::getVoidTy(mod->getContext())";
  315. }
  316. // Now, see if we've seen the type before and return that
  317. TypeMap::iterator I = TypeNames.find(Ty);
  318. if (I != TypeNames.end())
  319. return I->second;
  320. // Okay, let's build a new name for this type. Start with a prefix
  321. const char* prefix = 0;
  322. switch (Ty->getTypeID()) {
  323. case Type::FunctionTyID: prefix = "FuncTy_"; break;
  324. case Type::StructTyID: prefix = "StructTy_"; break;
  325. case Type::ArrayTyID: prefix = "ArrayTy_"; break;
  326. case Type::PointerTyID: prefix = "PointerTy_"; break;
  327. case Type::VectorTyID: prefix = "VectorTy_"; break;
  328. default: prefix = "OtherTy_"; break; // prevent breakage
  329. }
  330. // See if the type has a name in the symboltable and build accordingly
  331. std::string name;
  332. if (StructType *STy = dyn_cast<StructType>(Ty))
  333. if (STy->hasName())
  334. name = STy->getName();
  335. if (name.empty())
  336. name = utostr(uniqueNum++);
  337. name = std::string(prefix) + name;
  338. sanitize(name);
  339. // Save the name
  340. return TypeNames[Ty] = name;
  341. }
  342. void CppWriter::printCppName(Type* Ty) {
  343. printEscapedString(getCppName(Ty));
  344. }
  345. std::string CppWriter::getCppName(const Value* val) {
  346. std::string name;
  347. ValueMap::iterator I = ValueNames.find(val);
  348. if (I != ValueNames.end() && I->first == val)
  349. return I->second;
  350. if (const GlobalVariable* GV = dyn_cast<GlobalVariable>(val)) {
  351. name = std::string("gvar_") +
  352. getTypePrefix(GV->getType()->getElementType());
  353. } else if (isa<Function>(val)) {
  354. name = std::string("func_");
  355. } else if (const Constant* C = dyn_cast<Constant>(val)) {
  356. name = std::string("const_") + getTypePrefix(C->getType());
  357. } else if (const Argument* Arg = dyn_cast<Argument>(val)) {
  358. if (is_inline) {
  359. unsigned argNum = std::distance(Arg->getParent()->arg_begin(),
  360. Function::const_arg_iterator(Arg)) + 1;
  361. name = std::string("arg_") + utostr(argNum);
  362. NameSet::iterator NI = UsedNames.find(name);
  363. if (NI != UsedNames.end())
  364. name += std::string("_") + utostr(uniqueNum++);
  365. UsedNames.insert(name);
  366. return ValueNames[val] = name;
  367. } else {
  368. name = getTypePrefix(val->getType());
  369. }
  370. } else {
  371. name = getTypePrefix(val->getType());
  372. }
  373. if (val->hasName())
  374. name += val->getName();
  375. else
  376. name += utostr(uniqueNum++);
  377. sanitize(name);
  378. NameSet::iterator NI = UsedNames.find(name);
  379. if (NI != UsedNames.end())
  380. name += std::string("_") + utostr(uniqueNum++);
  381. UsedNames.insert(name);
  382. return ValueNames[val] = name;
  383. }
  384. void CppWriter::printCppName(const Value* val) {
  385. printEscapedString(getCppName(val));
  386. }
  387. void CppWriter::printAttributes(const AttrListPtr &PAL,
  388. const std::string &name) {
  389. Out << "AttrListPtr " << name << "_PAL;";
  390. nl(Out);
  391. if (!PAL.isEmpty()) {
  392. Out << '{'; in(); nl(Out);
  393. Out << "SmallVector<AttributeWithIndex, 4> Attrs;"; nl(Out);
  394. Out << "AttributeWithIndex PAWI;"; nl(Out);
  395. for (unsigned i = 0; i < PAL.getNumSlots(); ++i) {
  396. unsigned index = PAL.getSlot(i).Index;
  397. Attributes attrs = PAL.getSlot(i).Attrs;
  398. Out << "PAWI.Index = " << index << "U; PAWI.Attrs = Attribute::None ";
  399. #define HANDLE_ATTR(X) \
  400. if (attrs & Attribute::X) \
  401. Out << " | Attribute::" #X; \
  402. attrs &= ~Attribute::X;
  403. HANDLE_ATTR(SExt);
  404. HANDLE_ATTR(ZExt);
  405. HANDLE_ATTR(NoReturn);
  406. HANDLE_ATTR(InReg);
  407. HANDLE_ATTR(StructRet);
  408. HANDLE_ATTR(NoUnwind);
  409. HANDLE_ATTR(NoAlias);
  410. HANDLE_ATTR(ByVal);
  411. HANDLE_ATTR(Nest);
  412. HANDLE_ATTR(ReadNone);
  413. HANDLE_ATTR(ReadOnly);
  414. HANDLE_ATTR(NoInline);
  415. HANDLE_ATTR(AlwaysInline);
  416. HANDLE_ATTR(OptimizeForSize);
  417. HANDLE_ATTR(StackProtect);
  418. HANDLE_ATTR(StackProtectReq);
  419. HANDLE_ATTR(NoCapture);
  420. HANDLE_ATTR(NoRedZone);
  421. HANDLE_ATTR(NoImplicitFloat);
  422. HANDLE_ATTR(Naked);
  423. HANDLE_ATTR(InlineHint);
  424. HANDLE_ATTR(ReturnsTwice);
  425. HANDLE_ATTR(UWTable);
  426. HANDLE_ATTR(NonLazyBind);
  427. #undef HANDLE_ATTR
  428. if (attrs & Attribute::StackAlignment)
  429. Out << " | Attribute::constructStackAlignmentFromInt("
  430. << Attribute::getStackAlignmentFromAttrs(attrs)
  431. << ")";
  432. attrs &= ~Attribute::StackAlignment;
  433. assert(attrs == 0 && "Unhandled attribute!");
  434. Out << ";";
  435. nl(Out);
  436. Out << "Attrs.push_back(PAWI);";
  437. nl(Out);
  438. }
  439. Out << name << "_PAL = AttrListPtr::get(Attrs.begin(), Attrs.end());";
  440. nl(Out);
  441. out(); nl(Out);
  442. Out << '}'; nl(Out);
  443. }
  444. }
  445. void CppWriter::printType(Type* Ty) {
  446. // We don't print definitions for primitive types
  447. if (Ty->isPrimitiveType() || Ty->isIntegerTy())
  448. return;
  449. // If we already defined this type, we don't need to define it again.
  450. if (DefinedTypes.find(Ty) != DefinedTypes.end())
  451. return;
  452. // Everything below needs the name for the type so get it now.
  453. std::string typeName(getCppName(Ty));
  454. // Print the type definition
  455. switch (Ty->getTypeID()) {
  456. case Type::FunctionTyID: {
  457. FunctionType* FT = cast<FunctionType>(Ty);
  458. Out << "std::vector<Type*>" << typeName << "_args;";
  459. nl(Out);
  460. FunctionType::param_iterator PI = FT->param_begin();
  461. FunctionType::param_iterator PE = FT->param_end();
  462. for (; PI != PE; ++PI) {
  463. Type* argTy = static_cast<Type*>(*PI);
  464. printType(argTy);
  465. std::string argName(getCppName(argTy));
  466. Out << typeName << "_args.push_back(" << argName;
  467. Out << ");";
  468. nl(Out);
  469. }
  470. printType(FT->getReturnType());
  471. std::string retTypeName(getCppName(FT->getReturnType()));
  472. Out << "FunctionType* " << typeName << " = FunctionType::get(";
  473. in(); nl(Out) << "/*Result=*/" << retTypeName;
  474. Out << ",";
  475. nl(Out) << "/*Params=*/" << typeName << "_args,";
  476. nl(Out) << "/*isVarArg=*/" << (FT->isVarArg() ? "true" : "false") << ");";
  477. out();
  478. nl(Out);
  479. break;
  480. }
  481. case Type::StructTyID: {
  482. StructType* ST = cast<StructType>(Ty);
  483. if (!ST->isLiteral()) {
  484. Out << "StructType *" << typeName << " = mod->getTypeByName(\"";
  485. printEscapedString(ST->getName());
  486. Out << "\");";
  487. nl(Out);
  488. Out << "if (!" << typeName << ") {";
  489. nl(Out);
  490. Out << typeName << " = ";
  491. Out << "StructType::create(mod->getContext(), \"";
  492. printEscapedString(ST->getName());
  493. Out << "\");";
  494. nl(Out);
  495. Out << "}";
  496. nl(Out);
  497. // Indicate that this type is now defined.
  498. DefinedTypes.insert(Ty);
  499. }
  500. Out << "std::vector<Type*>" << typeName << "_fields;";
  501. nl(Out);
  502. StructType::element_iterator EI = ST->element_begin();
  503. StructType::element_iterator EE = ST->element_end();
  504. for (; EI != EE; ++EI) {
  505. Type* fieldTy = static_cast<Type*>(*EI);
  506. printType(fieldTy);
  507. std::string fieldName(getCppName(fieldTy));
  508. Out << typeName << "_fields.push_back(" << fieldName;
  509. Out << ");";
  510. nl(Out);
  511. }
  512. if (ST->isLiteral()) {
  513. Out << "StructType *" << typeName << " = ";
  514. Out << "StructType::get(" << "mod->getContext(), ";
  515. } else {
  516. Out << "if (" << typeName << "->isOpaque()) {";
  517. nl(Out);
  518. Out << typeName << "->setBody(";
  519. }
  520. Out << typeName << "_fields, /*isPacked=*/"
  521. << (ST->isPacked() ? "true" : "false") << ");";
  522. nl(Out);
  523. if (!ST->isLiteral()) {
  524. Out << "}";
  525. nl(Out);
  526. }
  527. break;
  528. }
  529. case Type::ArrayTyID: {
  530. ArrayType* AT = cast<ArrayType>(Ty);
  531. Type* ET = AT->getElementType();
  532. printType(ET);
  533. if (DefinedTypes.find(Ty) == DefinedTypes.end()) {
  534. std::string elemName(getCppName(ET));
  535. Out << "ArrayType* " << typeName << " = ArrayType::get("
  536. << elemName
  537. << ", " << utostr(AT->getNumElements()) << ");";
  538. nl(Out);
  539. }
  540. break;
  541. }
  542. case Type::PointerTyID: {
  543. PointerType* PT = cast<PointerType>(Ty);
  544. Type* ET = PT->getElementType();
  545. printType(ET);
  546. if (DefinedTypes.find(Ty) == DefinedTypes.end()) {
  547. std::string elemName(getCppName(ET));
  548. Out << "PointerType* " << typeName << " = PointerType::get("
  549. << elemName
  550. << ", " << utostr(PT->getAddressSpace()) << ");";
  551. nl(Out);
  552. }
  553. break;
  554. }
  555. case Type::VectorTyID: {
  556. VectorType* PT = cast<VectorType>(Ty);
  557. Type* ET = PT->getElementType();
  558. printType(ET);
  559. if (DefinedTypes.find(Ty) == DefinedTypes.end()) {
  560. std::string elemName(getCppName(ET));
  561. Out << "VectorType* " << typeName << " = VectorType::get("
  562. << elemName
  563. << ", " << utostr(PT->getNumElements()) << ");";
  564. nl(Out);
  565. }
  566. break;
  567. }
  568. default:
  569. error("Invalid TypeID");
  570. }
  571. // Indicate that this type is now defined.
  572. DefinedTypes.insert(Ty);
  573. // Finally, separate the type definition from other with a newline.
  574. nl(Out);
  575. }
  576. void CppWriter::printTypes(const Module* M) {
  577. // Add all of the global variables to the value table.
  578. for (Module::const_global_iterator I = TheModule->global_begin(),
  579. E = TheModule->global_end(); I != E; ++I) {
  580. if (I->hasInitializer())
  581. printType(I->getInitializer()->getType());
  582. printType(I->getType());
  583. }
  584. // Add all the functions to the table
  585. for (Module::const_iterator FI = TheModule->begin(), FE = TheModule->end();
  586. FI != FE; ++FI) {
  587. printType(FI->getReturnType());
  588. printType(FI->getFunctionType());
  589. // Add all the function arguments
  590. for (Function::const_arg_iterator AI = FI->arg_begin(),
  591. AE = FI->arg_end(); AI != AE; ++AI) {
  592. printType(AI->getType());
  593. }
  594. // Add all of the basic blocks and instructions
  595. for (Function::const_iterator BB = FI->begin(),
  596. E = FI->end(); BB != E; ++BB) {
  597. printType(BB->getType());
  598. for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;
  599. ++I) {
  600. printType(I->getType());
  601. for (unsigned i = 0; i < I->getNumOperands(); ++i)
  602. printType(I->getOperand(i)->getType());
  603. }
  604. }
  605. }
  606. }
  607. // printConstant - Print out a constant pool entry...
  608. void CppWriter::printConstant(const Constant *CV) {
  609. // First, if the constant is actually a GlobalValue (variable or function)
  610. // or its already in the constant list then we've printed it already and we
  611. // can just return.
  612. if (isa<GlobalValue>(CV) || ValueNames.find(CV) != ValueNames.end())
  613. return;
  614. std::string constName(getCppName(CV));
  615. std::string typeName(getCppName(CV->getType()));
  616. if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
  617. std::string constValue = CI->getValue().toString(10, true);
  618. Out << "ConstantInt* " << constName
  619. << " = ConstantInt::get(mod->getContext(), APInt("
  620. << cast<IntegerType>(CI->getType())->getBitWidth()
  621. << ", StringRef(\"" << constValue << "\"), 10));";
  622. } else if (isa<ConstantAggregateZero>(CV)) {
  623. Out << "ConstantAggregateZero* " << constName
  624. << " = ConstantAggregateZero::get(" << typeName << ");";
  625. } else if (isa<ConstantPointerNull>(CV)) {
  626. Out << "ConstantPointerNull* " << constName
  627. << " = ConstantPointerNull::get(" << typeName << ");";
  628. } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
  629. Out << "ConstantFP* " << constName << " = ";
  630. printCFP(CFP);
  631. Out << ";";
  632. } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
  633. Out << "std::vector<Constant*> " << constName << "_elems;";
  634. nl(Out);
  635. unsigned N = CA->getNumOperands();
  636. for (unsigned i = 0; i < N; ++i) {
  637. printConstant(CA->getOperand(i)); // recurse to print operands
  638. Out << constName << "_elems.push_back("
  639. << getCppName(CA->getOperand(i)) << ");";
  640. nl(Out);
  641. }
  642. Out << "Constant* " << constName << " = ConstantArray::get("
  643. << typeName << ", " << constName << "_elems);";
  644. } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
  645. Out << "std::vector<Constant*> " << constName << "_fields;";
  646. nl(Out);
  647. unsigned N = CS->getNumOperands();
  648. for (unsigned i = 0; i < N; i++) {
  649. printConstant(CS->getOperand(i));
  650. Out << constName << "_fields.push_back("
  651. << getCppName(CS->getOperand(i)) << ");";
  652. nl(Out);
  653. }
  654. Out << "Constant* " << constName << " = ConstantStruct::get("
  655. << typeName << ", " << constName << "_fields);";
  656. } else if (const ConstantVector *CVec = dyn_cast<ConstantVector>(CV)) {
  657. Out << "std::vector<Constant*> " << constName << "_elems;";
  658. nl(Out);
  659. unsigned N = CVec->getNumOperands();
  660. for (unsigned i = 0; i < N; ++i) {
  661. printConstant(CVec->getOperand(i));
  662. Out << constName << "_elems.push_back("
  663. << getCppName(CVec->getOperand(i)) << ");";
  664. nl(Out);
  665. }
  666. Out << "Constant* " << constName << " = ConstantVector::get("
  667. << typeName << ", " << constName << "_elems);";
  668. } else if (isa<UndefValue>(CV)) {
  669. Out << "UndefValue* " << constName << " = UndefValue::get("
  670. << typeName << ");";
  671. } else if (const ConstantDataSequential *CDS =
  672. dyn_cast<ConstantDataSequential>(CV)) {
  673. if (CDS->isString()) {
  674. Out << "Constant *" << constName <<
  675. " = ConstantDataArray::getString(mod->getContext(), \"";
  676. StringRef Str = CDS->getAsString();
  677. bool nullTerminate = false;
  678. if (Str.back() == 0) {
  679. Str = Str.drop_back();
  680. nullTerminate = true;
  681. }
  682. printEscapedString(Str);
  683. // Determine if we want null termination or not.
  684. if (nullTerminate)
  685. Out << "\", true);";
  686. else
  687. Out << "\", false);";// No null terminator
  688. } else {
  689. // TODO: Could generate more efficient code generating CDS calls instead.
  690. Out << "std::vector<Constant*> " << constName << "_elems;";
  691. nl(Out);
  692. for (unsigned i = 0; i != CDS->getNumElements(); ++i) {
  693. Constant *Elt = CDS->getElementAsConstant(i);
  694. printConstant(Elt);
  695. Out << constName << "_elems.push_back(" << getCppName(Elt) << ");";
  696. nl(Out);
  697. }
  698. Out << "Constant* " << constName;
  699. if (isa<ArrayType>(CDS->getType()))
  700. Out << " = ConstantArray::get(";
  701. else
  702. Out << " = ConstantVector::get(";
  703. Out << typeName << ", " << constName << "_elems);";
  704. }
  705. } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
  706. if (CE->getOpcode() == Instruction::GetElementPtr) {
  707. Out << "std::vector<Constant*> " << constName << "_indices;";
  708. nl(Out);
  709. printConstant(CE->getOperand(0));
  710. for (unsigned i = 1; i < CE->getNumOperands(); ++i ) {
  711. printConstant(CE->getOperand(i));
  712. Out << constName << "_indices.push_back("
  713. << getCppName(CE->getOperand(i)) << ");";
  714. nl(Out);
  715. }
  716. Out << "Constant* " << constName
  717. << " = ConstantExpr::getGetElementPtr("
  718. << getCppName(CE->getOperand(0)) << ", "
  719. << constName << "_indices);";
  720. } else if (CE->isCast()) {
  721. printConstant(CE->getOperand(0));
  722. Out << "Constant* " << constName << " = ConstantExpr::getCast(";
  723. switch (CE->getOpcode()) {
  724. default: llvm_unreachable("Invalid cast opcode");
  725. case Instruction::Trunc: Out << "Instruction::Trunc"; break;
  726. case Instruction::ZExt: Out << "Instruction::ZExt"; break;
  727. case Instruction::SExt: Out << "Instruction::SExt"; break;
  728. case Instruction::FPTrunc: Out << "Instruction::FPTrunc"; break;
  729. case Instruction::FPExt: Out << "Instruction::FPExt"; break;
  730. case Instruction::FPToUI: Out << "Instruction::FPToUI"; break;
  731. case Instruction::FPToSI: Out << "Instruction::FPToSI"; break;
  732. case Instruction::UIToFP: Out << "Instruction::UIToFP"; break;
  733. case Instruction::SIToFP: Out << "Instruction::SIToFP"; break;
  734. case Instruction::PtrToInt: Out << "Instruction::PtrToInt"; break;
  735. case Instruction::IntToPtr: Out << "Instruction::IntToPtr"; break;
  736. case Instruction::BitCast: Out << "Instruction::BitCast"; break;
  737. }
  738. Out << ", " << getCppName(CE->getOperand(0)) << ", "
  739. << getCppName(CE->getType()) << ");";
  740. } else {
  741. unsigned N = CE->getNumOperands();
  742. for (unsigned i = 0; i < N; ++i ) {
  743. printConstant(CE->getOperand(i));
  744. }
  745. Out << "Constant* " << constName << " = ConstantExpr::";
  746. switch (CE->getOpcode()) {
  747. case Instruction::Add: Out << "getAdd("; break;
  748. case Instruction::FAdd: Out << "getFAdd("; break;
  749. case Instruction::Sub: Out << "getSub("; break;
  750. case Instruction::FSub: Out << "getFSub("; break;
  751. case Instruction::Mul: Out << "getMul("; break;
  752. case Instruction::FMul: Out << "getFMul("; break;
  753. case Instruction::UDiv: Out << "getUDiv("; break;
  754. case Instruction::SDiv: Out << "getSDiv("; break;
  755. case Instruction::FDiv: Out << "getFDiv("; break;
  756. case Instruction::URem: Out << "getURem("; break;
  757. case Instruction::SRem: Out << "getSRem("; break;
  758. case Instruction::FRem: Out << "getFRem("; break;
  759. case Instruction::And: Out << "getAnd("; break;
  760. case Instruction::Or: Out << "getOr("; break;
  761. case Instruction::Xor: Out << "getXor("; break;
  762. case Instruction::ICmp:
  763. Out << "getICmp(ICmpInst::ICMP_";
  764. switch (CE->getPredicate()) {
  765. case ICmpInst::ICMP_EQ: Out << "EQ"; break;
  766. case ICmpInst::ICMP_NE: Out << "NE"; break;
  767. case ICmpInst::ICMP_SLT: Out << "SLT"; break;
  768. case ICmpInst::ICMP_ULT: Out << "ULT"; break;
  769. case ICmpInst::ICMP_SGT: Out << "SGT"; break;
  770. case ICmpInst::ICMP_UGT: Out << "UGT"; break;
  771. case ICmpInst::ICMP_SLE: Out << "SLE"; break;
  772. case ICmpInst::ICMP_ULE: Out << "ULE"; break;
  773. case ICmpInst::ICMP_SGE: Out << "SGE"; break;
  774. case ICmpInst::ICMP_UGE: Out << "UGE"; break;
  775. default: error("Invalid ICmp Predicate");
  776. }
  777. break;
  778. case Instruction::FCmp:
  779. Out << "getFCmp(FCmpInst::FCMP_";
  780. switch (CE->getPredicate()) {
  781. case FCmpInst::FCMP_FALSE: Out << "FALSE"; break;
  782. case FCmpInst::FCMP_ORD: Out << "ORD"; break;
  783. case FCmpInst::FCMP_UNO: Out << "UNO"; break;
  784. case FCmpInst::FCMP_OEQ: Out << "OEQ"; break;
  785. case FCmpInst::FCMP_UEQ: Out << "UEQ"; break;
  786. case FCmpInst::FCMP_ONE: Out << "ONE"; break;
  787. case FCmpInst::FCMP_UNE: Out << "UNE"; break;
  788. case FCmpInst::FCMP_OLT: Out << "OLT"; break;
  789. case FCmpInst::FCMP_ULT: Out << "ULT"; break;
  790. case FCmpInst::FCMP_OGT: Out << "OGT"; break;
  791. case FCmpInst::FCMP_UGT: Out << "UGT"; break;
  792. case FCmpInst::FCMP_OLE: Out << "OLE"; break;
  793. case FCmpInst::FCMP_ULE: Out << "ULE"; break;
  794. case FCmpInst::FCMP_OGE: Out << "OGE"; break;
  795. case FCmpInst::FCMP_UGE: Out << "UGE"; break;
  796. case FCmpInst::FCMP_TRUE: Out << "TRUE"; break;
  797. default: error("Invalid FCmp Predicate");
  798. }
  799. break;
  800. case Instruction::Shl: Out << "getShl("; break;
  801. case Instruction::LShr: Out << "getLShr("; break;
  802. case Instruction::AShr: Out << "getAShr("; break;
  803. case Instruction::Select: Out << "getSelect("; break;
  804. case Instruction::ExtractElement: Out << "getExtractElement("; break;
  805. case Instruction::InsertElement: Out << "getInsertElement("; break;
  806. case Instruction::ShuffleVector: Out << "getShuffleVector("; break;
  807. default:
  808. error("Invalid constant expression");
  809. break;
  810. }
  811. Out << getCppName(CE->getOperand(0));
  812. for (unsigned i = 1; i < CE->getNumOperands(); ++i)
  813. Out << ", " << getCppName(CE->getOperand(i));
  814. Out << ");";
  815. }
  816. } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV)) {
  817. Out << "Constant* " << constName << " = ";
  818. Out << "BlockAddress::get(" << getOpName(BA->getBasicBlock()) << ");";
  819. } else {
  820. error("Bad Constant");
  821. Out << "Constant* " << constName << " = 0; ";
  822. }
  823. nl(Out);
  824. }
  825. void CppWriter::printConstants(const Module* M) {
  826. // Traverse all the global variables looking for constant initializers
  827. for (Module::const_global_iterator I = TheModule->global_begin(),
  828. E = TheModule->global_end(); I != E; ++I)
  829. if (I->hasInitializer())
  830. printConstant(I->getInitializer());
  831. // Traverse the LLVM functions looking for constants
  832. for (Module::const_iterator FI = TheModule->begin(), FE = TheModule->end();
  833. FI != FE; ++FI) {
  834. // Add all of the basic blocks and instructions
  835. for (Function::const_iterator BB = FI->begin(),
  836. E = FI->end(); BB != E; ++BB) {
  837. for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;
  838. ++I) {
  839. for (unsigned i = 0; i < I->getNumOperands(); ++i) {
  840. if (Constant* C = dyn_cast<Constant>(I->getOperand(i))) {
  841. printConstant(C);
  842. }
  843. }
  844. }
  845. }
  846. }
  847. }
  848. void CppWriter::printVariableUses(const GlobalVariable *GV) {
  849. nl(Out) << "// Type Definitions";
  850. nl(Out);
  851. printType(GV->getType());
  852. if (GV->hasInitializer()) {
  853. const Constant *Init = GV->getInitializer();
  854. printType(Init->getType());
  855. if (const Function *F = dyn_cast<Function>(Init)) {
  856. nl(Out)<< "/ Function Declarations"; nl(Out);
  857. printFunctionHead(F);
  858. } else if (const GlobalVariable* gv = dyn_cast<GlobalVariable>(Init)) {
  859. nl(Out) << "// Global Variable Declarations"; nl(Out);
  860. printVariableHead(gv);
  861. nl(Out) << "// Global Variable Definitions"; nl(Out);
  862. printVariableBody(gv);
  863. } else {
  864. nl(Out) << "// Constant Definitions"; nl(Out);
  865. printConstant(Init);
  866. }
  867. }
  868. }
  869. void CppWriter::printVariableHead(const GlobalVariable *GV) {
  870. nl(Out) << "GlobalVariable* " << getCppName(GV);
  871. if (is_inline) {
  872. Out << " = mod->getGlobalVariable(mod->getContext(), ";
  873. printEscapedString(GV->getName());
  874. Out << ", " << getCppName(GV->getType()->getElementType()) << ",true)";
  875. nl(Out) << "if (!" << getCppName(GV) << ") {";
  876. in(); nl(Out) << getCppName(GV);
  877. }
  878. Out << " = new GlobalVariable(/*Module=*/*mod, ";
  879. nl(Out) << "/*Type=*/";
  880. printCppName(GV->getType()->getElementType());
  881. Out << ",";
  882. nl(Out) << "/*isConstant=*/" << (GV->isConstant()?"true":"false");
  883. Out << ",";
  884. nl(Out) << "/*Linkage=*/";
  885. printLinkageType(GV->getLinkage());
  886. Out << ",";
  887. nl(Out) << "/*Initializer=*/0, ";
  888. if (GV->hasInitializer()) {
  889. Out << "// has initializer, specified below";
  890. }
  891. nl(Out) << "/*Name=*/\"";
  892. printEscapedString(GV->getName());
  893. Out << "\");";
  894. nl(Out);
  895. if (GV->hasSection()) {
  896. printCppName(GV);
  897. Out << "->setSection(\"";
  898. printEscapedString(GV->getSection());
  899. Out << "\");";
  900. nl(Out);
  901. }
  902. if (GV->getAlignment()) {
  903. printCppName(GV);
  904. Out << "->setAlignment(" << utostr(GV->getAlignment()) << ");";
  905. nl(Out);
  906. }
  907. if (GV->getVisibility() != GlobalValue::DefaultVisibility) {
  908. printCppName(GV);
  909. Out << "->setVisibility(";
  910. printVisibilityType(GV->getVisibility());
  911. Out << ");";
  912. nl(Out);
  913. }
  914. if (GV->isThreadLocal()) {
  915. printCppName(GV);
  916. Out << "->setThreadLocal(true);";
  917. nl(Out);
  918. }
  919. if (is_inline) {
  920. out(); Out << "}"; nl(Out);
  921. }
  922. }
  923. void CppWriter::printVariableBody(const GlobalVariable *GV) {
  924. if (GV->hasInitializer()) {
  925. printCppName(GV);
  926. Out << "->setInitializer(";
  927. Out << getCppName(GV->getInitializer()) << ");";
  928. nl(Out);
  929. }
  930. }
  931. std::string CppWriter::getOpName(const Value* V) {
  932. if (!isa<Instruction>(V) || DefinedValues.find(V) != DefinedValues.end())
  933. return getCppName(V);
  934. // See if its alread in the map of forward references, if so just return the
  935. // name we already set up for it
  936. ForwardRefMap::const_iterator I = ForwardRefs.find(V);
  937. if (I != ForwardRefs.end())
  938. return I->second;
  939. // This is a new forward reference. Generate a unique name for it
  940. std::string result(std::string("fwdref_") + utostr(uniqueNum++));
  941. // Yes, this is a hack. An Argument is the smallest instantiable value that
  942. // we can make as a placeholder for the real value. We'll replace these
  943. // Argument instances later.
  944. Out << "Argument* " << result << " = new Argument("
  945. << getCppName(V->getType()) << ");";
  946. nl(Out);
  947. ForwardRefs[V] = result;
  948. return result;
  949. }
  950. static StringRef ConvertAtomicOrdering(AtomicOrdering Ordering) {
  951. switch (Ordering) {
  952. case NotAtomic: return "NotAtomic";
  953. case Unordered: return "Unordered";
  954. case Monotonic: return "Monotonic";
  955. case Acquire: return "Acquire";
  956. case Release: return "Release";
  957. case AcquireRelease: return "AcquireRelease";
  958. case SequentiallyConsistent: return "SequentiallyConsistent";
  959. }
  960. llvm_unreachable("Unknown ordering");
  961. }
  962. static StringRef ConvertAtomicSynchScope(SynchronizationScope SynchScope) {
  963. switch (SynchScope) {
  964. case SingleThread: return "SingleThread";
  965. case CrossThread: return "CrossThread";
  966. }
  967. llvm_unreachable("Unknown synch scope");
  968. }
  969. // printInstruction - This member is called for each Instruction in a function.
  970. void CppWriter::printInstruction(const Instruction *I,
  971. const std::string& bbname) {
  972. std::string iName(getCppName(I));
  973. // Before we emit this instruction, we need to take care of generating any
  974. // forward references. So, we get the names of all the operands in advance
  975. const unsigned Ops(I->getNumOperands());
  976. std::string* opNames = new std::string[Ops];
  977. for (unsigned i = 0; i < Ops; i++)
  978. opNames[i] = getOpName(I->getOperand(i));
  979. switch (I->getOpcode()) {
  980. default:
  981. error("Invalid instruction");
  982. break;
  983. case Instruction::Ret: {
  984. const ReturnInst* ret = cast<ReturnInst>(I);
  985. Out << "ReturnInst::Create(mod->getContext(), "
  986. << (ret->getReturnValue() ? opNames[0] + ", " : "") << bbname << ");";
  987. break;
  988. }
  989. case Instruction::Br: {
  990. const BranchInst* br = cast<BranchInst>(I);
  991. Out << "BranchInst::Create(" ;
  992. if (br->getNumOperands() == 3) {
  993. Out << opNames[2] << ", "
  994. << opNames[1] << ", "
  995. << opNames[0] << ", ";
  996. } else if (br->getNumOperands() == 1) {
  997. Out << opNames[0] << ", ";
  998. } else {
  999. error("Branch with 2 operands?");
  1000. }
  1001. Out << bbname << ");";
  1002. break;
  1003. }
  1004. case Instruction::Switch: {
  1005. const SwitchInst *SI = cast<SwitchInst>(I);
  1006. Out << "SwitchInst* " << iName << " = SwitchInst::Create("
  1007. << getOpName(SI->getCondition()) << ", "
  1008. << getOpName(SI->getDefaultDest()) << ", "
  1009. << SI->getNumCases() << ", " << bbname << ");";
  1010. nl(Out);
  1011. unsigned NumCases = SI->getNumCases();
  1012. for (unsigned i = 0; i < NumCases; ++i) {
  1013. const ConstantInt* CaseVal = SI->getCaseValue(i);
  1014. const BasicBlock *BB = SI->getCaseSuccessor(i);
  1015. Out << iName << "->addCase("
  1016. << getOpName(CaseVal) << ", "
  1017. << getOpName(BB) << ");";
  1018. nl(Out);
  1019. }
  1020. break;
  1021. }
  1022. case Instruction::IndirectBr: {
  1023. const IndirectBrInst *IBI = cast<IndirectBrInst>(I);
  1024. Out << "IndirectBrInst *" << iName << " = IndirectBrInst::Create("
  1025. << opNames[0] << ", " << IBI->getNumDestinations() << ");";
  1026. nl(Out);
  1027. for (unsigned i = 1; i != IBI->getNumOperands(); ++i) {
  1028. Out << iName << "->addDestination(" << opNames[i] << ");";
  1029. nl(Out);
  1030. }
  1031. break;
  1032. }
  1033. case Instruction::Resume: {
  1034. Out << "ResumeInst::Create(mod->getContext(), " << opNames[0]
  1035. << ", " << bbname << ");";
  1036. break;
  1037. }
  1038. case Instruction::Invoke: {
  1039. const InvokeInst* inv = cast<InvokeInst>(I);
  1040. Out << "std::vector<Value*> " << iName << "_params;";
  1041. nl(Out);
  1042. for (unsigned i = 0; i < inv->getNumArgOperands(); ++i) {
  1043. Out << iName << "_params.push_back("
  1044. << getOpName(inv->getArgOperand(i)) << ");";
  1045. nl(Out);
  1046. }
  1047. // FIXME: This shouldn't use magic numbers -3, -2, and -1.
  1048. Out << "InvokeInst *" << iName << " = InvokeInst::Create("
  1049. << getOpName(inv->getCalledFunction()) << ", "
  1050. << getOpName(inv->getNormalDest()) << ", "
  1051. << getOpName(inv->getUnwindDest()) << ", "
  1052. << iName << "_params, \"";
  1053. printEscapedString(inv->getName());
  1054. Out << "\", " << bbname << ");";
  1055. nl(Out) << iName << "->setCallingConv(";
  1056. printCallingConv(inv->getCallingConv());
  1057. Out << ");";
  1058. printAttributes(inv->getAttributes(), iName);
  1059. Out << iName << "->setAttributes(" << iName << "_PAL);";
  1060. nl(Out);
  1061. break;
  1062. }
  1063. case Instruction::Unreachable: {
  1064. Out << "new UnreachableInst("
  1065. << "mod->getContext(), "
  1066. << bbname << ");";
  1067. break;
  1068. }
  1069. case Instruction::Add:
  1070. case Instruction::FAdd:
  1071. case Instruction::Sub:
  1072. case Instruction::FSub:
  1073. case Instruction::Mul:
  1074. case Instruction::FMul:
  1075. case Instruction::UDiv:
  1076. case Instruction::SDiv:
  1077. case Instruction::FDiv:
  1078. case Instruction::URem:
  1079. case Instruction::SRem:
  1080. case Instruction::FRem:
  1081. case Instruction::And:
  1082. case Instruction::Or:
  1083. case Instruction::Xor:
  1084. case Instruction::Shl:
  1085. case Instruction::LShr:
  1086. case Instruction::AShr:{
  1087. Out << "BinaryOperator* " << iName << " = BinaryOperator::Create(";
  1088. switch (I->getOpcode()) {
  1089. case Instruction::Add: Out << "Instruction::Add"; break;
  1090. case Instruction::FAdd: Out << "Instruction::FAdd"; break;
  1091. case Instruction::Sub: Out << "Instruction::Sub"; break;
  1092. case Instruction::FSub: Out << "Instruction::FSub"; break;
  1093. case Instruction::Mul: Out << "Instruction::Mul"; break;
  1094. case Instruction::FMul: Out << "Instruction::FMul"; break;
  1095. case Instruction::UDiv:Out << "Instruction::UDiv"; break;
  1096. case Instruction::SDiv:Out << "Instruction::SDiv"; break;
  1097. case Instruction::FDiv:Out << "Instruction::FDiv"; break;
  1098. case Instruction::URem:Out << "Instruction::URem"; break;
  1099. case Instruction::SRem:Out << "Instruction::SRem"; break;
  1100. case Instruction::FRem:Out << "Instruction::FRem"; break;
  1101. case Instruction::And: Out << "Instruction::And"; break;
  1102. case Instruction::Or: Out << "Instruction::Or"; break;
  1103. case Instruction::Xor: Out << "Instruction::Xor"; break;
  1104. case Instruction::Shl: Out << "Instruction::Shl"; break;
  1105. case Instruction::LShr:Out << "Instruction::LShr"; break;
  1106. case Instruction::AShr:Out << "Instruction::AShr"; break;
  1107. default: Out << "Instruction::BadOpCode"; break;
  1108. }
  1109. Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
  1110. printEscapedString(I->getName());
  1111. Out << "\", " << bbname << ");";
  1112. break;
  1113. }
  1114. case Instruction::FCmp: {
  1115. Out << "FCmpInst* " << iName << " = new FCmpInst(*" << bbname << ", ";
  1116. switch (cast<FCmpInst>(I)->getPredicate()) {
  1117. case FCmpInst::FCMP_FALSE: Out << "FCmpInst::FCMP_FALSE"; break;
  1118. case FCmpInst::FCMP_OEQ : Out << "FCmpInst::FCMP_OEQ"; break;
  1119. case FCmpInst::FCMP_OGT : Out << "FCmpInst::FCMP_OGT"; break;
  1120. case FCmpInst::FCMP_OGE : Out << "FCmpInst::FCMP_OGE"; break;
  1121. case FCmpInst::FCMP_OLT : Out << "FCmpInst::FCMP_OLT"; break;
  1122. case FCmpInst::FCMP_OLE : Out << "FCmpInst::FCMP_OLE"; break;
  1123. case FCmpInst::FCMP_ONE : Out << "FCmpInst::FCMP_ONE"; break;
  1124. case FCmpInst::FCMP_ORD : Out << "FCmpInst::FCMP_ORD"; break;
  1125. case FCmpInst::FCMP_UNO : Out << "FCmpInst::FCMP_UNO"; break;
  1126. case FCmpInst::FCMP_UEQ : Out << "FCmpInst::FCMP_UEQ"; break;
  1127. case FCmpInst::FCMP_UGT : Out << "FCmpInst::FCMP_UGT"; break;
  1128. case FCmpInst::FCMP_UGE : Out << "FCmpInst::FCMP_UGE"; break;
  1129. case FCmpInst::FCMP_ULT : Out << "FCmpInst::FCMP_ULT"; break;
  1130. case FCmpInst::FCMP_ULE : Out << "FCmpInst::FCMP_ULE"; break;
  1131. case FCmpInst::FCMP_UNE : Out << "FCmpInst::FCMP_UNE"; break;
  1132. case FCmpInst::FCMP_TRUE : Out << "FCmpInst::FCMP_TRUE"; break;
  1133. default: Out << "FCmpInst::BAD_ICMP_PREDICATE"; break;
  1134. }
  1135. Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
  1136. printEscapedString(I->getName());
  1137. Out << "\");";
  1138. break;
  1139. }
  1140. case Instruction::ICmp: {
  1141. Out << "ICmpInst* " << iName << " = new ICmpInst(*" << bbname << ", ";
  1142. switch (cast<ICmpInst>(I)->getPredicate()) {
  1143. case ICmpInst::ICMP_EQ: Out << "ICmpInst::ICMP_EQ"; break;
  1144. case ICmpInst::ICMP_NE: Out << "ICmpInst::ICMP_NE"; break;
  1145. case ICmpInst::ICMP_ULE: Out << "ICmpInst::ICMP_ULE"; break;
  1146. case ICmpInst::ICMP_SLE: Out << "ICmpInst::ICMP_SLE"; break;
  1147. case ICmpInst::ICMP_UGE: Out << "ICmpInst::ICMP_UGE"; break;
  1148. case ICmpInst::ICMP_SGE: Out << "ICmpInst::ICMP_SGE"; break;
  1149. case ICmpInst::ICMP_ULT: Out << "ICmpInst::ICMP_ULT"; break;
  1150. case ICmpInst::ICMP_SLT: Out << "ICmpInst::ICMP_SLT"; break;
  1151. case ICmpInst::ICMP_UGT: Out << "ICmpInst::ICMP_UGT"; break;
  1152. case ICmpInst::ICMP_SGT: Out << "ICmpInst::ICMP_SGT"; break;
  1153. default: Out << "ICmpInst::BAD_ICMP_PREDICATE"; break;
  1154. }
  1155. Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
  1156. printEscapedString(I->getName());
  1157. Out << "\");";
  1158. break;
  1159. }
  1160. case Instruction::Alloca: {
  1161. const AllocaInst* allocaI = cast<AllocaInst>(I);
  1162. Out << "AllocaInst* " << iName << " = new AllocaInst("
  1163. << getCppName(allocaI->getAllocatedType()) << ", ";
  1164. if (allocaI->isArrayAllocation())
  1165. Out << opNames[0] << ", ";
  1166. Out << "\"";
  1167. printEscapedString(allocaI->getName());
  1168. Out << "\", " << bbname << ");";
  1169. if (allocaI->getAlignment())
  1170. nl(Out) << iName << "->setAlignment("
  1171. << allocaI->getAlignment() << ");";
  1172. break;
  1173. }
  1174. case Instruction::Load: {
  1175. const LoadInst* load = cast<LoadInst>(I);
  1176. Out << "LoadInst* " << iName << " = new LoadInst("
  1177. << opNames[0] << ", \"";
  1178. printEscapedString(load->getName());
  1179. Out << "\", " << (load->isVolatile() ? "true" : "false" )
  1180. << ", " << bbname << ");";
  1181. if (load->getAlignment())
  1182. nl(Out) << iName << "->setAlignment("
  1183. << load->getAlignment() << ");";
  1184. if (load->isAtomic()) {
  1185. StringRef Ordering = ConvertAtomicOrdering(load->getOrdering());
  1186. StringRef CrossThread = ConvertAtomicSynchScope(load->getSynchScope());
  1187. nl(Out) << iName << "->setAtomic("
  1188. << Ordering << ", " << CrossThread << ");";
  1189. }
  1190. break;
  1191. }
  1192. case Instruction::Store: {
  1193. const StoreInst* store = cast<StoreInst>(I);
  1194. Out << "StoreInst* " << iName << " = new StoreInst("
  1195. << opNames[0] << ", "
  1196. << opNames[1] << ", "
  1197. << (store->isVolatile() ? "true" : "false")
  1198. << ", " << bbname << ");";
  1199. if (store->getAlignment())
  1200. nl(Out) << iName << "->setAlignment("
  1201. << store->getAlignment() << ");";
  1202. if (store->isAtomic()) {
  1203. StringRef Ordering = ConvertAtomicOrdering(store->getOrdering());
  1204. StringRef CrossThread = ConvertAtomicSynchScope(store->getSynchScope());
  1205. nl(Out) << iName << "->setAtomic("
  1206. << Ordering << ", " << CrossThread << ");";
  1207. }
  1208. break;
  1209. }
  1210. case Instruction::GetElementPtr: {
  1211. const GetElementPtrInst* gep = cast<GetElementPtrInst>(I);
  1212. if (gep->getNumOperands() <= 2) {
  1213. Out << "GetElementPtrInst* " << iName << " = GetElementPtrInst::Create("
  1214. << opNames[0];
  1215. if (gep->getNumOperands() == 2)
  1216. Out << ", " << opNames[1];
  1217. } else {
  1218. Out << "std::vector<Value*> " << iName << "_indices;";
  1219. nl(Out);
  1220. for (unsigned i = 1; i < gep->getNumOperands(); ++i ) {
  1221. Out << iName << "_indices.push_back("
  1222. << opNames[i] << ");";
  1223. nl(Out);
  1224. }
  1225. Out << "Instruction* " << iName << " = GetElementPtrInst::Create("
  1226. << opNames[0] << ", " << iName << "_indices";
  1227. }
  1228. Out << ", \"";
  1229. printEscapedString(gep->getName());
  1230. Out << "\", " << bbname << ");";
  1231. break;
  1232. }
  1233. case Instruction::PHI: {
  1234. const PHINode* phi = cast<PHINode>(I);
  1235. Out << "PHINode* " << iName << " = PHINode::Create("
  1236. << getCppName(phi->getType()) << ", "
  1237. << phi->getNumIncomingValues() << ", \"";
  1238. printEscapedString(phi->getName());
  1239. Out << "\", " << bbname << ");";
  1240. nl(Out);
  1241. for (unsigned i = 0; i < phi->getNumIncomingValues(); ++i) {
  1242. Out << iName << "->addIncoming("
  1243. << opNames[PHINode::getOperandNumForIncomingValue(i)] << ", "
  1244. << getOpName(phi->getIncomingBlock(i)) << ");";
  1245. nl(Out);
  1246. }
  1247. break;
  1248. }
  1249. case Instruction::Trunc:
  1250. case Instruction::ZExt:
  1251. case Instruction::SExt:
  1252. case Instruction::FPTrunc:
  1253. case Instruction::FPExt:
  1254. case Instruction::FPToUI:
  1255. case Instruction::FPToSI:
  1256. case Instruction::UIToFP:
  1257. case Instruction::SIToFP:
  1258. case Instruction::PtrToInt:
  1259. case Instruction::IntToPtr:
  1260. case Instruction::BitCast: {
  1261. const CastInst* cst = cast<CastInst>(I);
  1262. Out << "CastInst* " << iName << " = new ";
  1263. switch (I->getOpcode()) {
  1264. case Instruction::Trunc: Out << "TruncInst"; break;
  1265. case Instruction::ZExt: Out << "ZExtInst"; break;
  1266. case Instruction::SExt: Out << "SExtInst"; break;
  1267. case Instruction::FPTrunc: Out << "FPTruncInst"; break;
  1268. case Instruction::FPExt: Out << "FPExtInst"; break;
  1269. case Instruction::FPToUI: Out << "FPToUIInst"; break;
  1270. case Instruction::FPToSI: Out << "FPToSIInst"; break;
  1271. case Instruction::UIToFP: Out << "UIToFPInst"; break;
  1272. case Instruction::SIToFP: Out << "SIToFPInst"; break;
  1273. case Instruction::PtrToInt: Out << "PtrToIntInst"; break;
  1274. case Instruction::IntToPtr: Out << "IntToPtrInst"; break;
  1275. case Instruction::BitCast: Out << "BitCastInst"; break;
  1276. default: assert(0 && "Unreachable"); break;
  1277. }
  1278. Out << "(" << opNames[0] << ", "
  1279. << getCppName(cst->getType()) << ", \"";
  1280. printEscapedString(cst->getName());
  1281. Out << "\", " << bbname << ");";
  1282. break;
  1283. }
  1284. case Instruction::Call: {
  1285. const CallInst* call = cast<CallInst>(I);
  1286. if (const InlineAsm* ila = dyn_cast<InlineAsm>(call->getCalledValue())) {
  1287. Out << "InlineAsm* " << getCppName(ila) << " = InlineAsm::get("
  1288. << getCppName(ila->getFunctionType()) << ", \""
  1289. << ila->getAsmString() << "\", \""
  1290. << ila->getConstraintString() << "\","
  1291. << (ila->hasSideEffects() ? "true" : "false") << ");";
  1292. nl(Out);
  1293. }
  1294. if (call->getNumArgOperands() > 1) {
  1295. Out << "std::vector<Value*> " << iName << "_params;";
  1296. nl(Out);
  1297. for (unsigned i = 0; i < call->getNumArgOperands(); ++i) {
  1298. Out << iName << "_params.push_back(" << opNames[i] << ");";
  1299. nl(Out);
  1300. }
  1301. Out << "CallInst* " << iName << " = CallInst::Create("
  1302. << opNames[call->getNumArgOperands()] << ", "
  1303. << iName << "_params, \"";
  1304. } else if (call->getNumArgOperands() == 1) {
  1305. Out << "CallInst* " << iName << " = CallInst::Create("
  1306. << opNames[call->getNumArgOperands()] << ", " << opNames[0] << ", \"";
  1307. } else {
  1308. Out << "CallInst* " << iName << " = CallInst::Create("
  1309. << opNames[call->getNumArgOperands()] << ", \"";
  1310. }
  1311. printEscapedString(call->getName());
  1312. Out << "\", " << bbname << ");";
  1313. nl(Out) << iName << "->setCallingConv(";
  1314. printCallingConv(call->getCallingConv());
  1315. Out << ");";
  1316. nl(Out) << iName << "->setTailCall("
  1317. << (call->isTailCall() ? "true" : "false");
  1318. Out << ");";
  1319. nl(Out);
  1320. printAttributes(call->getAttributes(), iName);
  1321. Out << iName << "->setAttributes(" << iName << "_PAL);";
  1322. nl(Out);
  1323. break;
  1324. }
  1325. case Instruction::Select: {
  1326. const SelectInst* sel = cast<SelectInst>(I);
  1327. Out << "SelectInst* " << getCppName(sel) << " = SelectInst::Create(";
  1328. Out << opNames[0] << ", " << opNames[1] << ", " << opNames[2] << ", \"";
  1329. printEscapedString(sel->getName());
  1330. Out << "\", " << bbname << ");";
  1331. break;
  1332. }
  1333. case Instruction::UserOp1:
  1334. /// FALL THROUGH
  1335. case Instruction::UserOp2: {
  1336. /// FIXME: What should be done here?
  1337. break;
  1338. }
  1339. case Instruction::VAArg: {
  1340. const VAArgInst* va = cast<VAArgInst>(I);
  1341. Out << "VAArgInst* " << getCppName(va) << " = new VAArgInst("
  1342. << opNames[0] << ", " << getCppName(va->getType()) << ", \"";
  1343. printEscapedString(va->getName());
  1344. Out << "\", " << bbname << ");";
  1345. break;
  1346. }
  1347. case Instruction::ExtractElement: {
  1348. const ExtractElementInst* eei = cast<ExtractElementInst>(I);
  1349. Out << "ExtractElementInst* " << getCppName(eei)
  1350. << " = new ExtractElementInst(" << opNames[0]
  1351. << ", " << opNames[1] << ", \"";
  1352. printEscapedString(eei->getName());
  1353. Out << "\", " << bbname << ");";
  1354. break;
  1355. }
  1356. case Instruction::InsertElement: {
  1357. const InsertElementInst* iei = cast<InsertElementInst>(I);
  1358. Out << "InsertElementInst* " << getCppName(iei)
  1359. << " = InsertElementInst::Create(" << opNames[0]
  1360. << ", " << opNames[1] << ", " << opNames[2] << ", \"";
  1361. printEscapedString(iei->getName());
  1362. Out << "\", " << bbname << ");";
  1363. break;
  1364. }
  1365. case Instruction::ShuffleVector: {
  1366. const ShuffleVectorInst* svi = cast<ShuffleVectorInst>(I);
  1367. Out << "ShuffleVectorInst* " << getCppName(svi)
  1368. << " = new ShuffleVectorInst(" << opNames[0]
  1369. << ", " << opNames[1] << ", " << opNames[2] << ", \"";
  1370. printEscapedString(svi->getName());
  1371. Out << "\", " << bbname << ");";
  1372. break;
  1373. }
  1374. case Instruction::ExtractValue: {
  1375. const ExtractValueInst *evi = cast<ExtractValueInst>(I);
  1376. Out << "std::vector<unsigned> " << iName << "_indices;";
  1377. nl(Out);
  1378. for (unsigned i = 0; i < evi->getNumIndices(); ++i) {
  1379. Out << iName << "_indices.push_back("
  1380. << evi->idx_begin()[i] << ");";
  1381. nl(Out);
  1382. }
  1383. Out << "ExtractValueInst* " << getCppName(evi)
  1384. << " = ExtractValueInst::Create(" << opNames[0]
  1385. << ", "
  1386. << iName << "_indices, \"";
  1387. printEscapedString(evi->getName());
  1388. Out << "\", " << bbname << ");";
  1389. break;
  1390. }
  1391. case Instruction::InsertValue: {
  1392. const InsertValueInst *ivi = cast<InsertValueInst>(I);
  1393. Out << "std::vector<unsigned> " << iName << "_indices;";
  1394. nl(Out);
  1395. for (unsigned i = 0; i < ivi->getNumIndices(); ++i) {
  1396. Out << iName << "_indices.push_back("
  1397. << ivi->idx_begin()[i] << ");";
  1398. nl(Out);
  1399. }
  1400. Out << "InsertValueInst* " << getCppName(ivi)
  1401. << " = InsertValueInst::Create(" << opNames[0]
  1402. << ", " << opNames[1] << ", "
  1403. << iName << "_indices, \"";
  1404. printEscapedString(ivi->getName());
  1405. Out << "\", " << bbname << ");";
  1406. break;
  1407. }
  1408. case Instruction::Fence: {
  1409. const FenceInst *fi = cast<FenceInst>(I);
  1410. StringRef Ordering = ConvertAtomicOrdering(fi->getOrdering());
  1411. StringRef CrossThread = ConvertAtomicSynchScope(fi->getSynchScope());
  1412. Out << "FenceInst* " << iName
  1413. << " = new FenceInst(mod->getContext(), "
  1414. << Ordering << ", " << CrossThread << ", " << bbname
  1415. << ");";
  1416. break;
  1417. }
  1418. case Instruction::AtomicCmpXchg: {
  1419. const AtomicCmpXchgInst *cxi = cast<AtomicCmpXchgInst>(I);
  1420. StringRef Ordering = ConvertAtomicOrdering(cxi->getOrdering());
  1421. StringRef CrossThread = ConvertAtomicSynchScope(cxi->getSynchScope());
  1422. Out << "AtomicCmpXchgInst* " << iName
  1423. << " = new AtomicCmpXchgInst("
  1424. << opNames[0] << ", " << opNames[1] << ", " << opNames[2] << ", "
  1425. << Ordering << ", " << CrossThread << ", " << bbname
  1426. << ");";
  1427. nl(Out) << iName << "->setName(\"";
  1428. printEscapedString(cxi->getName());
  1429. Out << "\");";
  1430. break;
  1431. }
  1432. case Instruction::AtomicRMW: {
  1433. const AtomicRMWInst *rmwi = cast<AtomicRMWInst>(I);
  1434. StringRef Ordering = ConvertAtomicOrdering(rmwi->getOrdering());
  1435. StringRef CrossThread = ConvertAtomicSynchScope(rmwi->getSynchScope());
  1436. StringRef Operation;
  1437. switch (rmwi->getOperation()) {
  1438. case AtomicRMWInst::Xchg: Operation = "AtomicRMWInst::Xchg"; break;
  1439. case AtomicRMWInst::Add: Operation = "AtomicRMWInst::Add"; break;
  1440. case AtomicRMWInst::Sub: Operation = "AtomicRMWInst::Sub"; break;
  1441. case AtomicRMWInst::And: Operation = "AtomicRMWInst::And"; break;
  1442. case AtomicRMWInst::Nand: Operation = "AtomicRMWInst::Nand"; break;
  1443. case AtomicRMWInst::Or: Operation = "AtomicRMWInst::Or"; break;
  1444. case AtomicRMWInst::Xor: Operation = "AtomicRMWInst::Xor"; break;
  1445. case AtomicRMWInst::Max: Operation = "AtomicRMWInst::Max"; break;
  1446. case AtomicRMWInst::Min: Operation = "AtomicRMWInst::Min"; break;
  1447. case AtomicRMWInst::UMax: Operation = "AtomicRMWInst::UMax"; break;
  1448. case AtomicRMWInst::UMin: Operation = "AtomicRMWInst::UMin"; break;
  1449. case AtomicRMWInst::BAD_BINOP: llvm_unreachable("Bad atomic operation");
  1450. }
  1451. Out << "AtomicRMWInst* " << iName
  1452. << " = new AtomicRMWInst("
  1453. << Operation << ", "
  1454. << opNames[0] << ", " << opNames[1] << ", "
  1455. << Ordering << ", " << CrossThread << ", " << bbname
  1456. << ");";
  1457. nl(Out) << iName << "->setName(\"";
  1458. printEscapedString(rmwi->getName());
  1459. Out << "\");";
  1460. break;
  1461. }
  1462. }
  1463. DefinedValues.insert(I);
  1464. nl(Out);
  1465. delete [] opNames;
  1466. }
  1467. // Print out the types, constants and declarations needed by one function
  1468. void CppWriter::printFunctionUses(const Function* F) {
  1469. nl(Out) << "// Type Definitions"; nl(Out);
  1470. if (!is_inline) {
  1471. // Print the function's return type
  1472. printType(F->getReturnType());
  1473. // Print the function's function type
  1474. printType(F->getFunctionType());
  1475. // Print the types of each of the function's arguments
  1476. for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
  1477. AI != AE; ++AI) {
  1478. printType(AI->getType());
  1479. }
  1480. }
  1481. // Print type definitions for every type referenced by an instruction and
  1482. // make a note of any global values or constants that are referenced
  1483. SmallPtrSet<GlobalValue*,64> gvs;
  1484. SmallPtrSet<Constant*,64> consts;
  1485. for (Function::const_iterator BB = F->begin(), BE = F->end();
  1486. BB != BE; ++BB){
  1487. for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
  1488. I != E; ++I) {
  1489. // Print the type of the instruction itself
  1490. printType(I->getType());
  1491. // Print the type of each of the instruction's operands
  1492. for (unsigned i = 0; i < I->getNumOperands(); ++i) {
  1493. Value* operand = I->getOperand(i);
  1494. printType(operand->getType());
  1495. // If the operand references a GVal or Constant, make a note of it
  1496. if (GlobalValue* GV = dyn_cast<GlobalValue>(operand)) {
  1497. gvs.insert(GV);
  1498. if (GenerationType != GenFunction)
  1499. if (GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
  1500. if (GVar->hasInitializer())
  1501. consts.insert(GVar->getInitializer());
  1502. } else if (Constant* C = dyn_cast<Constant>(operand)) {
  1503. consts.insert(C);
  1504. for (unsigned j = 0; j < C->getNumOperands(); ++j) {
  1505. // If the operand references a GVal or Constant, make a note of it
  1506. Value* operand = C->getOperand(j);
  1507. printType(operand->getType());
  1508. if (GlobalValue* GV = dyn_cast<GlobalValue>(operand)) {
  1509. gvs.insert(GV);
  1510. if (GenerationType != GenFunction)
  1511. if (GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
  1512. if (GVar->hasInitializer())
  1513. consts.insert(GVar->getInitializer());
  1514. }
  1515. }
  1516. }
  1517. }
  1518. }
  1519. }
  1520. // Print the function declarations for any functions encountered
  1521. nl(Out) << "// Function Declarations"; nl(Out);
  1522. for (SmallPtrSet<GlobalValue*,64>::iterator I = gvs.begin(), E = gvs.end();
  1523. I != E; ++I) {
  1524. if (Function* Fun = dyn_cast<Function>(*I)) {
  1525. if (!is_inline || Fun != F)
  1526. printFunctionHead(Fun);
  1527. }
  1528. }
  1529. // Print the global variable declarations for any variables encountered
  1530. nl(Out) << "// Global Variable Declarations"; nl(Out);
  1531. for (SmallPtrSet<GlobalValue*,64>::iterator I = gvs.begin(), E = gvs.end();
  1532. I != E; ++I) {
  1533. if (GlobalVariable* F = dyn_cast<GlobalVariable>(*I))
  1534. printVariableHead(F);
  1535. }
  1536. // Print the constants found
  1537. nl(Out) << "// Constant Definitions"; nl(Out);
  1538. for (SmallPtrSet<Constant*,64>::iterator I = consts.begin(),
  1539. E = consts.end(); I != E; ++I) {
  1540. printConstant(*I);
  1541. }
  1542. // Process the global variables definitions now that all the constants have
  1543. // been emitted. These definitions just couple the gvars with their constant
  1544. // initializers.
  1545. if (GenerationType != GenFunction) {
  1546. nl(Out) << "// Global Variable Definitions"; nl(Out);
  1547. for (SmallPtrSet<GlobalValue*,64>::iterator I = gvs.begin(), E = gvs.end();
  1548. I != E; ++I) {
  1549. if (GlobalVariable* GV = dyn_cast<GlobalVariable>(*I))
  1550. printVariableBody(GV);
  1551. }
  1552. }
  1553. }
  1554. void CppWriter::printFunctionHead(const Function* F) {
  1555. nl(Out) << "Function* " << getCppName(F);
  1556. Out << " = mod->getFunction(\"";
  1557. printEscapedString(F->getName());
  1558. Out << "\");";
  1559. nl(Out) << "if (!" << getCppName(F) << ") {";
  1560. nl(Out) << getCppName(F);
  1561. Out<< " = Function::Create(";
  1562. nl(Out,1) << "/*Type=*/" << getCppName(F->getFunctionType()) << ",";
  1563. nl(Out) << "/*Linkage=*/";
  1564. printLinkageType(F->getLinkage());
  1565. Out << ",";
  1566. nl(Out) << "/*Name=*/\"";
  1567. printEscapedString(F->getName());
  1568. Out << "\", mod); " << (F->isDeclaration()? "// (external, no body)" : "");
  1569. nl(Out,-1);
  1570. printCppName(F);
  1571. Out << "->setCallingConv(";
  1572. printCallingConv(F->getCallingConv());
  1573. Out << ");";
  1574. nl(Out);
  1575. if (F->hasSection()) {
  1576. printCppName(F);
  1577. Out << "->setSection(\"" << F->getSection() << "\");";
  1578. nl(Out);
  1579. }
  1580. if (F->getAlignment()) {
  1581. printCppName(F);
  1582. Out << "->setAlignment(" << F->getAlignment() << ");";
  1583. nl(Out);
  1584. }
  1585. if (F->getVisibility() != GlobalValue::DefaultVisibility) {
  1586. printCppName(F);
  1587. Out << "->setVisibility(";
  1588. printVisibilityType(F->getVisibility());
  1589. Out << ");";
  1590. nl(Out);
  1591. }
  1592. if (F->hasGC()) {
  1593. printCppName(F);
  1594. Out << "->setGC(\"" << F->getGC() << "\");";
  1595. nl(Out);
  1596. }
  1597. Out << "}";
  1598. nl(Out);
  1599. printAttributes(F->getAttributes(), getCppName(F));
  1600. printCppName(F);
  1601. Out << "->setAttributes(" << getCppName(F) << "_PAL);";
  1602. nl(Out);
  1603. }
  1604. void CppWriter::printFunctionBody(const Function *F) {
  1605. if (F->isDeclaration())
  1606. return; // external functions have no bodies.
  1607. // Clear the DefinedValues and ForwardRefs maps because we can't have
  1608. // cross-function forward refs
  1609. ForwardRefs.clear();
  1610. DefinedValues.clear();
  1611. // Create all the argument values
  1612. if (!is_inline) {
  1613. if (!F->arg_empty()) {
  1614. Out << "Function::arg_iterator args = " << getCppName(F)
  1615. << "->arg_begin();";
  1616. nl(Out);
  1617. }
  1618. for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
  1619. AI != AE; ++AI) {
  1620. Out << "Value* " << getCppName(AI) << " = args++;";
  1621. nl(Out);
  1622. if (AI->hasName()) {
  1623. Out << getCppName(AI) << "->setName(\"";
  1624. printEscapedString(AI->getName());
  1625. Out << "\");";
  1626. nl(Out);
  1627. }
  1628. }
  1629. }
  1630. // Create all the basic blocks
  1631. nl(Out);
  1632. for (Function::const_iterator BI = F->begin(), BE = F->end();
  1633. BI != BE; ++BI) {
  1634. std::string bbname(getCppName(BI));
  1635. Out << "BasicBlock* " << bbname <<
  1636. " = BasicBlock::Create(mod->getContext(), \"";
  1637. if (BI->hasName())
  1638. printEscapedString(BI->getName());
  1639. Out << "\"," << getCppName(BI->getParent()) << ",0);";
  1640. nl(Out);
  1641. }
  1642. // Output all of its basic blocks... for the function
  1643. for (Function::const_iterator BI = F->begin(), BE = F->end();
  1644. BI != BE; ++BI) {
  1645. std::string bbname(getCppName(BI));
  1646. nl(Out) << "// Block " << BI->getName() << " (" << bbname << ")";
  1647. nl(Out);
  1648. // Output all of the instructions in the basic block...
  1649. for (BasicBlock::const_iterator I = BI->begin(), E = BI->end();
  1650. I != E; ++I) {
  1651. printInstruction(I,bbname);
  1652. }
  1653. }
  1654. // Loop over the ForwardRefs and resolve them now that all instructions
  1655. // are generated.
  1656. if (!ForwardRefs.empty()) {
  1657. nl(Out) << "// Resolve Forward References";
  1658. nl(Out);
  1659. }
  1660. while (!ForwardRefs.empty()) {
  1661. ForwardRefMap::iterator I = ForwardRefs.begin();
  1662. Out << I->second << "->replaceAllUsesWith("
  1663. << getCppName(I->first) << "); delete " << I->second << ";";
  1664. nl(Out);
  1665. ForwardRefs.erase(I);
  1666. }
  1667. }
  1668. void CppWriter::printInline(const std::string& fname,
  1669. const std::string& func) {
  1670. const Function* F = TheModule->getFunction(func);
  1671. if (!F) {
  1672. error(std::string("Function '") + func + "' not found in input module");
  1673. return;
  1674. }
  1675. if (F->isDeclaration()) {
  1676. error(std::string("Function '") + func + "' is external!");
  1677. return;
  1678. }
  1679. nl(Out) << "BasicBlock* " << fname << "(Module* mod, Function *"
  1680. << getCppName(F);
  1681. unsigned arg_count = 1;
  1682. for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
  1683. AI != AE; ++AI) {
  1684. Out << ", Value* arg_" << arg_count;
  1685. }
  1686. Out << ") {";
  1687. nl(Out);
  1688. is_inline = true;
  1689. printFunctionUses(F);
  1690. printFunctionBody(F);
  1691. is_inline = false;
  1692. Out << "return " << getCppName(F->begin()) << ";";
  1693. nl(Out) << "}";
  1694. nl(Out);
  1695. }
  1696. void CppWriter::printModuleBody() {
  1697. // Print out all the type definitions
  1698. nl(Out) << "// Type Definitions"; nl(Out);
  1699. printTypes(TheModule);
  1700. // Functions can call each other and global variables can reference them so
  1701. // define all the functions first before emitting their function bodies.
  1702. nl(Out) << "// Function Declarations"; nl(Out);
  1703. for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
  1704. I != E; ++I)
  1705. printFunctionHead(I);
  1706. // Process the global variables declarations. We can't initialze them until
  1707. // after the constants are printed so just print a header for each global
  1708. nl(Out) << "// Global Variable Declarations\n"; nl(Out);
  1709. for (Module::const_global_iterator I = TheModule->global_begin(),
  1710. E = TheModule->global_end(); I != E; ++I) {
  1711. printVariableHead(I);
  1712. }
  1713. // Print out all the constants definitions. Constants don't recurse except
  1714. // through GlobalValues. All GlobalValues have been declared at this point
  1715. // so we can proceed to generate the constants.
  1716. nl(Out) << "// Constant Definitions"; nl(Out);
  1717. printConstants(TheModule);
  1718. // Process the global variables definitions now that all the constants have
  1719. // been emitted. These definitions just couple the gvars with their constant
  1720. // initializers.
  1721. nl(Out) << "// Global Variable Definitions"; nl(Out);
  1722. for (Module::const_global_iterator I = TheModule->global_begin(),
  1723. E = TheModule->global_end(); I != E; ++I) {
  1724. printVariableBody(I);
  1725. }
  1726. // Finally, we can safely put out all of the function bodies.
  1727. nl(Out) << "// Function Definitions"; nl(Out);
  1728. for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
  1729. I != E; ++I) {
  1730. if (!I->isDeclaration()) {
  1731. nl(Out) << "// Function: " << I->getName() << " (" << getCppName(I)
  1732. << ")";
  1733. nl(Out) << "{";
  1734. nl(Out,1);
  1735. printFunctionBody(I);
  1736. nl(Out,-1) << "}";
  1737. nl(Out);
  1738. }
  1739. }
  1740. }
  1741. void CppWriter::printProgram(const std::string& fname,
  1742. const std::string& mName) {
  1743. Out << "#include <llvm/LLVMContext.h>\n";
  1744. Out << "#include <llvm/Module.h>\n";
  1745. Out << "#include <llvm/DerivedTypes.h>\n";
  1746. Out << "#include <llvm/Constants.h>\n";
  1747. Out << "#include <llvm/GlobalVariable.h>\n";
  1748. Out << "#include <llvm/Function.h>\n";
  1749. Out << "#include <llvm/CallingConv.h>\n";
  1750. Out << "#include <llvm/BasicBlock.h>\n";
  1751. Out << "#include <llvm/Instructions.h>\n";
  1752. Out << "#include <llvm/InlineAsm.h>\n";
  1753. Out << "#include <llvm/Support/FormattedStream.h>\n";
  1754. Out << "#include <llvm/Support/MathExtras.h>\n";
  1755. Out << "#include <llvm/Pass.h>\n";
  1756. Out << "#include <llvm/PassManager.h>\n";
  1757. Out << "#include <llvm/ADT/SmallVector.h>\n";
  1758. Out << "#include <llvm/Analysis/Verifier.h>\n";
  1759. Out << "#include <llvm/Assembly/PrintModulePass.h>\n";
  1760. Out << "#include <algorithm>\n";
  1761. Out << "using namespace llvm;\n\n";
  1762. Out << "Module* " << fname << "();\n\n";
  1763. Out << "int main(int argc, char**argv) {\n";
  1764. Out << " Module* Mod = " << fname << "();\n";
  1765. Out << " verifyModule(*Mod, PrintMessageAction);\n";
  1766. Out << " PassManager PM;\n";
  1767. Out << " PM.add(createPrintModulePass(&outs()));\n";
  1768. Out << " PM.run(*Mod);\n";
  1769. Out << " return 0;\n";
  1770. Out << "}\n\n";
  1771. printModule(fname,mName);
  1772. }
  1773. void CppWriter::printModule(const std::string& fname,
  1774. const std::string& mName) {
  1775. nl(Out) << "Module* " << fname << "() {";
  1776. nl(Out,1) << "// Module Construction";
  1777. nl(Out) << "Module* mod = new Module(\"";
  1778. printEscapedString(mName);
  1779. Out << "\", getGlobalContext());";
  1780. if (!TheModule->getTargetTriple().empty()) {
  1781. nl(Out) << "mod->setDataLayout(\"" << TheModule->getDataLayout() << "\");";
  1782. }
  1783. if (!TheModule->getTargetTriple().empty()) {
  1784. nl(Out) << "mod->setTargetTriple(\"" << TheModule->getTargetTriple()
  1785. << "\");";
  1786. }
  1787. if (!TheModule->getModuleInlineAsm().empty()) {
  1788. nl(Out) << "mod->setModuleInlineAsm(\"";
  1789. printEscapedString(TheModule->getModuleInlineAsm());
  1790. Out << "\");";
  1791. }
  1792. nl(Out);
  1793. // Loop over the dependent libraries and emit them.
  1794. Module::lib_iterator LI = TheModule->lib_begin();
  1795. Module::lib_iterator LE = TheModule->lib_end();
  1796. while (LI != LE) {
  1797. Out << "mod->addLibrary(\"" << *LI << "\");";
  1798. nl(Out);
  1799. ++LI;
  1800. }
  1801. printModuleBody();
  1802. nl(Out) << "return mod;";
  1803. nl(Out,-1) << "}";
  1804. nl(Out);
  1805. }
  1806. void CppWriter::printContents(const std::string& fname,
  1807. const std::string& mName) {
  1808. Out << "\nModule* " << fname << "(Module *mod) {\n";
  1809. Out << "\nmod->setModuleIdentifier(\"";
  1810. printEscapedString(mName);
  1811. Out << "\");\n";
  1812. printModuleBody();
  1813. Out << "\nreturn mod;\n";
  1814. Out << "\n}\n";
  1815. }
  1816. void CppWriter::printFunction(const std::string& fname,
  1817. const std::string& funcName) {
  1818. const Function* F = TheModule->getFunction(funcName);
  1819. if (!F) {
  1820. error(std::string("Function '") + funcName + "' not found in input module");
  1821. return;
  1822. }
  1823. Out << "\nFunction* " << fname << "(Module *mod) {\n";
  1824. printFunctionUses(F);
  1825. printFunctionHead(F);
  1826. printFunctionBody(F);
  1827. Out << "return " << getCppName(F) << ";\n";
  1828. Out << "}\n";
  1829. }
  1830. void CppWriter::printFunctions() {
  1831. const Module::FunctionListType &funcs = TheModule->getFunctionList();
  1832. Module::const_iterator I = funcs.begin();
  1833. Module::const_iterator IE = funcs.end();
  1834. for (; I != IE; ++I) {
  1835. const Function &func = *I;
  1836. if (!func.isDeclaration()) {
  1837. std::string name("define_");
  1838. name += func.getName();
  1839. printFunction(name, func.getName());
  1840. }
  1841. }
  1842. }
  1843. void CppWriter::printVariable(const std::string& fname,
  1844. const std::string& varName) {
  1845. const GlobalVariable* GV = TheModule->getNamedGlobal(varName);
  1846. if (!GV) {
  1847. error(std::string("Variable '") + varName + "' not found in input module");
  1848. return;
  1849. }
  1850. Out << "\nGlobalVariable* " << fname << "(Module *mod) {\n";
  1851. printVariableUses(GV);
  1852. printVariableHead(GV);
  1853. printVariableBody(GV);
  1854. Out << "return " << getCppName(GV) << ";\n";
  1855. Out << "}\n";
  1856. }
  1857. void CppWriter::printType(const std::string &fname,
  1858. const std::string &typeName) {
  1859. Type* Ty = TheModule->getTypeByName(typeName);
  1860. if (!Ty) {
  1861. error(std::string("Type '") + typeName + "' not found in input module");
  1862. return;
  1863. }
  1864. Out << "\nType* " << fname << "(Module *mod) {\n";
  1865. printType(Ty);
  1866. Out << "return " << getCppName(Ty) << ";\n";
  1867. Out << "}\n";
  1868. }
  1869. bool CppWriter::runOnModule(Module &M) {
  1870. TheModule = &M;
  1871. // Emit a header
  1872. Out << "// Generated by llvm2cpp - DO NOT MODIFY!\n\n";
  1873. // Get the name of the function we're supposed to generate
  1874. std::string fname = FuncName.getValue();
  1875. // Get the name of the thing we are to generate
  1876. std::string tgtname = NameToGenerate.getValue();
  1877. if (GenerationType == GenModule ||
  1878. GenerationType == GenContents ||
  1879. GenerationType == GenProgram ||
  1880. GenerationType == GenFunctions) {
  1881. if (tgtname == "!bad!") {
  1882. if (M.getModuleIdentifier() == "-")
  1883. tgtname = "<stdin>";
  1884. else
  1885. tgtname = M.getModuleIdentifier();
  1886. }
  1887. } else if (tgtname == "!bad!")
  1888. error("You must use the -for option with -gen-{function,variable,type}");
  1889. switch (WhatToGenerate(GenerationType)) {
  1890. case GenProgram:
  1891. if (fname.empty())
  1892. fname = "makeLLVMModule";
  1893. printProgram(fname,tgtname);
  1894. break;
  1895. case GenModule:
  1896. if (fname.empty())
  1897. fname = "makeLLVMModule";
  1898. printModule(fname,tgtname);
  1899. break;
  1900. case GenContents:
  1901. if (fname.empty())
  1902. fname = "makeLLVMModuleContents";
  1903. printContents(fname,tgtname);
  1904. break;
  1905. case GenFunction:
  1906. if (fname.empty())
  1907. fname = "makeLLVMFunction";
  1908. printFunction(fname,tgtname);
  1909. break;
  1910. case GenFunctions:
  1911. printFunctions();
  1912. break;
  1913. case GenInline:
  1914. if (fname.empty())
  1915. fname = "makeLLVMInline";
  1916. printInline(fname,tgtname);
  1917. break;
  1918. case GenVariable:
  1919. if (fname.empty())
  1920. fname = "makeLLVMVariable";
  1921. printVariable(fname,tgtname);
  1922. break;
  1923. case GenType:
  1924. if (fname.empty())
  1925. fname = "makeLLVMType";
  1926. printType(fname,tgtname);
  1927. break;
  1928. }
  1929. return false;
  1930. }
  1931. char CppWriter::ID = 0;
  1932. //===----------------------------------------------------------------------===//
  1933. // External Interface declaration
  1934. //===----------------------------------------------------------------------===//
  1935. bool CPPTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
  1936. formatted_raw_ostream &o,
  1937. CodeGenFileType FileType,
  1938. bool DisableVerify) {
  1939. if (FileType != TargetMachine::CGFT_AssemblyFile) return true;
  1940. PM.add(new CppWriter(o));
  1941. return false;
  1942. }