CPPBackend.cpp 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077
  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::Unwind: {
  1064. Out << "new UnwindInst("
  1065. << bbname << ");";
  1066. break;
  1067. }
  1068. case Instruction::Unreachable: {
  1069. Out << "new UnreachableInst("
  1070. << "mod->getContext(), "
  1071. << bbname << ");";
  1072. break;
  1073. }
  1074. case Instruction::Add:
  1075. case Instruction::FAdd:
  1076. case Instruction::Sub:
  1077. case Instruction::FSub:
  1078. case Instruction::Mul:
  1079. case Instruction::FMul:
  1080. case Instruction::UDiv:
  1081. case Instruction::SDiv:
  1082. case Instruction::FDiv:
  1083. case Instruction::URem:
  1084. case Instruction::SRem:
  1085. case Instruction::FRem:
  1086. case Instruction::And:
  1087. case Instruction::Or:
  1088. case Instruction::Xor:
  1089. case Instruction::Shl:
  1090. case Instruction::LShr:
  1091. case Instruction::AShr:{
  1092. Out << "BinaryOperator* " << iName << " = BinaryOperator::Create(";
  1093. switch (I->getOpcode()) {
  1094. case Instruction::Add: Out << "Instruction::Add"; break;
  1095. case Instruction::FAdd: Out << "Instruction::FAdd"; break;
  1096. case Instruction::Sub: Out << "Instruction::Sub"; break;
  1097. case Instruction::FSub: Out << "Instruction::FSub"; break;
  1098. case Instruction::Mul: Out << "Instruction::Mul"; break;
  1099. case Instruction::FMul: Out << "Instruction::FMul"; break;
  1100. case Instruction::UDiv:Out << "Instruction::UDiv"; break;
  1101. case Instruction::SDiv:Out << "Instruction::SDiv"; break;
  1102. case Instruction::FDiv:Out << "Instruction::FDiv"; break;
  1103. case Instruction::URem:Out << "Instruction::URem"; break;
  1104. case Instruction::SRem:Out << "Instruction::SRem"; break;
  1105. case Instruction::FRem:Out << "Instruction::FRem"; break;
  1106. case Instruction::And: Out << "Instruction::And"; break;
  1107. case Instruction::Or: Out << "Instruction::Or"; break;
  1108. case Instruction::Xor: Out << "Instruction::Xor"; break;
  1109. case Instruction::Shl: Out << "Instruction::Shl"; break;
  1110. case Instruction::LShr:Out << "Instruction::LShr"; break;
  1111. case Instruction::AShr:Out << "Instruction::AShr"; break;
  1112. default: Out << "Instruction::BadOpCode"; break;
  1113. }
  1114. Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
  1115. printEscapedString(I->getName());
  1116. Out << "\", " << bbname << ");";
  1117. break;
  1118. }
  1119. case Instruction::FCmp: {
  1120. Out << "FCmpInst* " << iName << " = new FCmpInst(*" << bbname << ", ";
  1121. switch (cast<FCmpInst>(I)->getPredicate()) {
  1122. case FCmpInst::FCMP_FALSE: Out << "FCmpInst::FCMP_FALSE"; break;
  1123. case FCmpInst::FCMP_OEQ : Out << "FCmpInst::FCMP_OEQ"; break;
  1124. case FCmpInst::FCMP_OGT : Out << "FCmpInst::FCMP_OGT"; break;
  1125. case FCmpInst::FCMP_OGE : Out << "FCmpInst::FCMP_OGE"; break;
  1126. case FCmpInst::FCMP_OLT : Out << "FCmpInst::FCMP_OLT"; break;
  1127. case FCmpInst::FCMP_OLE : Out << "FCmpInst::FCMP_OLE"; break;
  1128. case FCmpInst::FCMP_ONE : Out << "FCmpInst::FCMP_ONE"; break;
  1129. case FCmpInst::FCMP_ORD : Out << "FCmpInst::FCMP_ORD"; break;
  1130. case FCmpInst::FCMP_UNO : Out << "FCmpInst::FCMP_UNO"; break;
  1131. case FCmpInst::FCMP_UEQ : Out << "FCmpInst::FCMP_UEQ"; break;
  1132. case FCmpInst::FCMP_UGT : Out << "FCmpInst::FCMP_UGT"; break;
  1133. case FCmpInst::FCMP_UGE : Out << "FCmpInst::FCMP_UGE"; break;
  1134. case FCmpInst::FCMP_ULT : Out << "FCmpInst::FCMP_ULT"; break;
  1135. case FCmpInst::FCMP_ULE : Out << "FCmpInst::FCMP_ULE"; break;
  1136. case FCmpInst::FCMP_UNE : Out << "FCmpInst::FCMP_UNE"; break;
  1137. case FCmpInst::FCMP_TRUE : Out << "FCmpInst::FCMP_TRUE"; break;
  1138. default: Out << "FCmpInst::BAD_ICMP_PREDICATE"; break;
  1139. }
  1140. Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
  1141. printEscapedString(I->getName());
  1142. Out << "\");";
  1143. break;
  1144. }
  1145. case Instruction::ICmp: {
  1146. Out << "ICmpInst* " << iName << " = new ICmpInst(*" << bbname << ", ";
  1147. switch (cast<ICmpInst>(I)->getPredicate()) {
  1148. case ICmpInst::ICMP_EQ: Out << "ICmpInst::ICMP_EQ"; break;
  1149. case ICmpInst::ICMP_NE: Out << "ICmpInst::ICMP_NE"; break;
  1150. case ICmpInst::ICMP_ULE: Out << "ICmpInst::ICMP_ULE"; break;
  1151. case ICmpInst::ICMP_SLE: Out << "ICmpInst::ICMP_SLE"; break;
  1152. case ICmpInst::ICMP_UGE: Out << "ICmpInst::ICMP_UGE"; break;
  1153. case ICmpInst::ICMP_SGE: Out << "ICmpInst::ICMP_SGE"; break;
  1154. case ICmpInst::ICMP_ULT: Out << "ICmpInst::ICMP_ULT"; break;
  1155. case ICmpInst::ICMP_SLT: Out << "ICmpInst::ICMP_SLT"; break;
  1156. case ICmpInst::ICMP_UGT: Out << "ICmpInst::ICMP_UGT"; break;
  1157. case ICmpInst::ICMP_SGT: Out << "ICmpInst::ICMP_SGT"; break;
  1158. default: Out << "ICmpInst::BAD_ICMP_PREDICATE"; break;
  1159. }
  1160. Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
  1161. printEscapedString(I->getName());
  1162. Out << "\");";
  1163. break;
  1164. }
  1165. case Instruction::Alloca: {
  1166. const AllocaInst* allocaI = cast<AllocaInst>(I);
  1167. Out << "AllocaInst* " << iName << " = new AllocaInst("
  1168. << getCppName(allocaI->getAllocatedType()) << ", ";
  1169. if (allocaI->isArrayAllocation())
  1170. Out << opNames[0] << ", ";
  1171. Out << "\"";
  1172. printEscapedString(allocaI->getName());
  1173. Out << "\", " << bbname << ");";
  1174. if (allocaI->getAlignment())
  1175. nl(Out) << iName << "->setAlignment("
  1176. << allocaI->getAlignment() << ");";
  1177. break;
  1178. }
  1179. case Instruction::Load: {
  1180. const LoadInst* load = cast<LoadInst>(I);
  1181. Out << "LoadInst* " << iName << " = new LoadInst("
  1182. << opNames[0] << ", \"";
  1183. printEscapedString(load->getName());
  1184. Out << "\", " << (load->isVolatile() ? "true" : "false" )
  1185. << ", " << bbname << ");";
  1186. if (load->getAlignment())
  1187. nl(Out) << iName << "->setAlignment("
  1188. << load->getAlignment() << ");";
  1189. if (load->isAtomic()) {
  1190. StringRef Ordering = ConvertAtomicOrdering(load->getOrdering());
  1191. StringRef CrossThread = ConvertAtomicSynchScope(load->getSynchScope());
  1192. nl(Out) << iName << "->setAtomic("
  1193. << Ordering << ", " << CrossThread << ");";
  1194. }
  1195. break;
  1196. }
  1197. case Instruction::Store: {
  1198. const StoreInst* store = cast<StoreInst>(I);
  1199. Out << "StoreInst* " << iName << " = new StoreInst("
  1200. << opNames[0] << ", "
  1201. << opNames[1] << ", "
  1202. << (store->isVolatile() ? "true" : "false")
  1203. << ", " << bbname << ");";
  1204. if (store->getAlignment())
  1205. nl(Out) << iName << "->setAlignment("
  1206. << store->getAlignment() << ");";
  1207. if (store->isAtomic()) {
  1208. StringRef Ordering = ConvertAtomicOrdering(store->getOrdering());
  1209. StringRef CrossThread = ConvertAtomicSynchScope(store->getSynchScope());
  1210. nl(Out) << iName << "->setAtomic("
  1211. << Ordering << ", " << CrossThread << ");";
  1212. }
  1213. break;
  1214. }
  1215. case Instruction::GetElementPtr: {
  1216. const GetElementPtrInst* gep = cast<GetElementPtrInst>(I);
  1217. if (gep->getNumOperands() <= 2) {
  1218. Out << "GetElementPtrInst* " << iName << " = GetElementPtrInst::Create("
  1219. << opNames[0];
  1220. if (gep->getNumOperands() == 2)
  1221. Out << ", " << opNames[1];
  1222. } else {
  1223. Out << "std::vector<Value*> " << iName << "_indices;";
  1224. nl(Out);
  1225. for (unsigned i = 1; i < gep->getNumOperands(); ++i ) {
  1226. Out << iName << "_indices.push_back("
  1227. << opNames[i] << ");";
  1228. nl(Out);
  1229. }
  1230. Out << "Instruction* " << iName << " = GetElementPtrInst::Create("
  1231. << opNames[0] << ", " << iName << "_indices";
  1232. }
  1233. Out << ", \"";
  1234. printEscapedString(gep->getName());
  1235. Out << "\", " << bbname << ");";
  1236. break;
  1237. }
  1238. case Instruction::PHI: {
  1239. const PHINode* phi = cast<PHINode>(I);
  1240. Out << "PHINode* " << iName << " = PHINode::Create("
  1241. << getCppName(phi->getType()) << ", "
  1242. << phi->getNumIncomingValues() << ", \"";
  1243. printEscapedString(phi->getName());
  1244. Out << "\", " << bbname << ");";
  1245. nl(Out);
  1246. for (unsigned i = 0; i < phi->getNumIncomingValues(); ++i) {
  1247. Out << iName << "->addIncoming("
  1248. << opNames[PHINode::getOperandNumForIncomingValue(i)] << ", "
  1249. << getOpName(phi->getIncomingBlock(i)) << ");";
  1250. nl(Out);
  1251. }
  1252. break;
  1253. }
  1254. case Instruction::Trunc:
  1255. case Instruction::ZExt:
  1256. case Instruction::SExt:
  1257. case Instruction::FPTrunc:
  1258. case Instruction::FPExt:
  1259. case Instruction::FPToUI:
  1260. case Instruction::FPToSI:
  1261. case Instruction::UIToFP:
  1262. case Instruction::SIToFP:
  1263. case Instruction::PtrToInt:
  1264. case Instruction::IntToPtr:
  1265. case Instruction::BitCast: {
  1266. const CastInst* cst = cast<CastInst>(I);
  1267. Out << "CastInst* " << iName << " = new ";
  1268. switch (I->getOpcode()) {
  1269. case Instruction::Trunc: Out << "TruncInst"; break;
  1270. case Instruction::ZExt: Out << "ZExtInst"; break;
  1271. case Instruction::SExt: Out << "SExtInst"; break;
  1272. case Instruction::FPTrunc: Out << "FPTruncInst"; break;
  1273. case Instruction::FPExt: Out << "FPExtInst"; break;
  1274. case Instruction::FPToUI: Out << "FPToUIInst"; break;
  1275. case Instruction::FPToSI: Out << "FPToSIInst"; break;
  1276. case Instruction::UIToFP: Out << "UIToFPInst"; break;
  1277. case Instruction::SIToFP: Out << "SIToFPInst"; break;
  1278. case Instruction::PtrToInt: Out << "PtrToIntInst"; break;
  1279. case Instruction::IntToPtr: Out << "IntToPtrInst"; break;
  1280. case Instruction::BitCast: Out << "BitCastInst"; break;
  1281. default: assert(0 && "Unreachable"); break;
  1282. }
  1283. Out << "(" << opNames[0] << ", "
  1284. << getCppName(cst->getType()) << ", \"";
  1285. printEscapedString(cst->getName());
  1286. Out << "\", " << bbname << ");";
  1287. break;
  1288. }
  1289. case Instruction::Call: {
  1290. const CallInst* call = cast<CallInst>(I);
  1291. if (const InlineAsm* ila = dyn_cast<InlineAsm>(call->getCalledValue())) {
  1292. Out << "InlineAsm* " << getCppName(ila) << " = InlineAsm::get("
  1293. << getCppName(ila->getFunctionType()) << ", \""
  1294. << ila->getAsmString() << "\", \""
  1295. << ila->getConstraintString() << "\","
  1296. << (ila->hasSideEffects() ? "true" : "false") << ");";
  1297. nl(Out);
  1298. }
  1299. if (call->getNumArgOperands() > 1) {
  1300. Out << "std::vector<Value*> " << iName << "_params;";
  1301. nl(Out);
  1302. for (unsigned i = 0; i < call->getNumArgOperands(); ++i) {
  1303. Out << iName << "_params.push_back(" << opNames[i] << ");";
  1304. nl(Out);
  1305. }
  1306. Out << "CallInst* " << iName << " = CallInst::Create("
  1307. << opNames[call->getNumArgOperands()] << ", "
  1308. << iName << "_params, \"";
  1309. } else if (call->getNumArgOperands() == 1) {
  1310. Out << "CallInst* " << iName << " = CallInst::Create("
  1311. << opNames[call->getNumArgOperands()] << ", " << opNames[0] << ", \"";
  1312. } else {
  1313. Out << "CallInst* " << iName << " = CallInst::Create("
  1314. << opNames[call->getNumArgOperands()] << ", \"";
  1315. }
  1316. printEscapedString(call->getName());
  1317. Out << "\", " << bbname << ");";
  1318. nl(Out) << iName << "->setCallingConv(";
  1319. printCallingConv(call->getCallingConv());
  1320. Out << ");";
  1321. nl(Out) << iName << "->setTailCall("
  1322. << (call->isTailCall() ? "true" : "false");
  1323. Out << ");";
  1324. nl(Out);
  1325. printAttributes(call->getAttributes(), iName);
  1326. Out << iName << "->setAttributes(" << iName << "_PAL);";
  1327. nl(Out);
  1328. break;
  1329. }
  1330. case Instruction::Select: {
  1331. const SelectInst* sel = cast<SelectInst>(I);
  1332. Out << "SelectInst* " << getCppName(sel) << " = SelectInst::Create(";
  1333. Out << opNames[0] << ", " << opNames[1] << ", " << opNames[2] << ", \"";
  1334. printEscapedString(sel->getName());
  1335. Out << "\", " << bbname << ");";
  1336. break;
  1337. }
  1338. case Instruction::UserOp1:
  1339. /// FALL THROUGH
  1340. case Instruction::UserOp2: {
  1341. /// FIXME: What should be done here?
  1342. break;
  1343. }
  1344. case Instruction::VAArg: {
  1345. const VAArgInst* va = cast<VAArgInst>(I);
  1346. Out << "VAArgInst* " << getCppName(va) << " = new VAArgInst("
  1347. << opNames[0] << ", " << getCppName(va->getType()) << ", \"";
  1348. printEscapedString(va->getName());
  1349. Out << "\", " << bbname << ");";
  1350. break;
  1351. }
  1352. case Instruction::ExtractElement: {
  1353. const ExtractElementInst* eei = cast<ExtractElementInst>(I);
  1354. Out << "ExtractElementInst* " << getCppName(eei)
  1355. << " = new ExtractElementInst(" << opNames[0]
  1356. << ", " << opNames[1] << ", \"";
  1357. printEscapedString(eei->getName());
  1358. Out << "\", " << bbname << ");";
  1359. break;
  1360. }
  1361. case Instruction::InsertElement: {
  1362. const InsertElementInst* iei = cast<InsertElementInst>(I);
  1363. Out << "InsertElementInst* " << getCppName(iei)
  1364. << " = InsertElementInst::Create(" << opNames[0]
  1365. << ", " << opNames[1] << ", " << opNames[2] << ", \"";
  1366. printEscapedString(iei->getName());
  1367. Out << "\", " << bbname << ");";
  1368. break;
  1369. }
  1370. case Instruction::ShuffleVector: {
  1371. const ShuffleVectorInst* svi = cast<ShuffleVectorInst>(I);
  1372. Out << "ShuffleVectorInst* " << getCppName(svi)
  1373. << " = new ShuffleVectorInst(" << opNames[0]
  1374. << ", " << opNames[1] << ", " << opNames[2] << ", \"";
  1375. printEscapedString(svi->getName());
  1376. Out << "\", " << bbname << ");";
  1377. break;
  1378. }
  1379. case Instruction::ExtractValue: {
  1380. const ExtractValueInst *evi = cast<ExtractValueInst>(I);
  1381. Out << "std::vector<unsigned> " << iName << "_indices;";
  1382. nl(Out);
  1383. for (unsigned i = 0; i < evi->getNumIndices(); ++i) {
  1384. Out << iName << "_indices.push_back("
  1385. << evi->idx_begin()[i] << ");";
  1386. nl(Out);
  1387. }
  1388. Out << "ExtractValueInst* " << getCppName(evi)
  1389. << " = ExtractValueInst::Create(" << opNames[0]
  1390. << ", "
  1391. << iName << "_indices, \"";
  1392. printEscapedString(evi->getName());
  1393. Out << "\", " << bbname << ");";
  1394. break;
  1395. }
  1396. case Instruction::InsertValue: {
  1397. const InsertValueInst *ivi = cast<InsertValueInst>(I);
  1398. Out << "std::vector<unsigned> " << iName << "_indices;";
  1399. nl(Out);
  1400. for (unsigned i = 0; i < ivi->getNumIndices(); ++i) {
  1401. Out << iName << "_indices.push_back("
  1402. << ivi->idx_begin()[i] << ");";
  1403. nl(Out);
  1404. }
  1405. Out << "InsertValueInst* " << getCppName(ivi)
  1406. << " = InsertValueInst::Create(" << opNames[0]
  1407. << ", " << opNames[1] << ", "
  1408. << iName << "_indices, \"";
  1409. printEscapedString(ivi->getName());
  1410. Out << "\", " << bbname << ");";
  1411. break;
  1412. }
  1413. case Instruction::Fence: {
  1414. const FenceInst *fi = cast<FenceInst>(I);
  1415. StringRef Ordering = ConvertAtomicOrdering(fi->getOrdering());
  1416. StringRef CrossThread = ConvertAtomicSynchScope(fi->getSynchScope());
  1417. Out << "FenceInst* " << iName
  1418. << " = new FenceInst(mod->getContext(), "
  1419. << Ordering << ", " << CrossThread << ", " << bbname
  1420. << ");";
  1421. break;
  1422. }
  1423. case Instruction::AtomicCmpXchg: {
  1424. const AtomicCmpXchgInst *cxi = cast<AtomicCmpXchgInst>(I);
  1425. StringRef Ordering = ConvertAtomicOrdering(cxi->getOrdering());
  1426. StringRef CrossThread = ConvertAtomicSynchScope(cxi->getSynchScope());
  1427. Out << "AtomicCmpXchgInst* " << iName
  1428. << " = new AtomicCmpXchgInst("
  1429. << opNames[0] << ", " << opNames[1] << ", " << opNames[2] << ", "
  1430. << Ordering << ", " << CrossThread << ", " << bbname
  1431. << ");";
  1432. nl(Out) << iName << "->setName(\"";
  1433. printEscapedString(cxi->getName());
  1434. Out << "\");";
  1435. break;
  1436. }
  1437. case Instruction::AtomicRMW: {
  1438. const AtomicRMWInst *rmwi = cast<AtomicRMWInst>(I);
  1439. StringRef Ordering = ConvertAtomicOrdering(rmwi->getOrdering());
  1440. StringRef CrossThread = ConvertAtomicSynchScope(rmwi->getSynchScope());
  1441. StringRef Operation;
  1442. switch (rmwi->getOperation()) {
  1443. case AtomicRMWInst::Xchg: Operation = "AtomicRMWInst::Xchg"; break;
  1444. case AtomicRMWInst::Add: Operation = "AtomicRMWInst::Add"; break;
  1445. case AtomicRMWInst::Sub: Operation = "AtomicRMWInst::Sub"; break;
  1446. case AtomicRMWInst::And: Operation = "AtomicRMWInst::And"; break;
  1447. case AtomicRMWInst::Nand: Operation = "AtomicRMWInst::Nand"; break;
  1448. case AtomicRMWInst::Or: Operation = "AtomicRMWInst::Or"; break;
  1449. case AtomicRMWInst::Xor: Operation = "AtomicRMWInst::Xor"; break;
  1450. case AtomicRMWInst::Max: Operation = "AtomicRMWInst::Max"; break;
  1451. case AtomicRMWInst::Min: Operation = "AtomicRMWInst::Min"; break;
  1452. case AtomicRMWInst::UMax: Operation = "AtomicRMWInst::UMax"; break;
  1453. case AtomicRMWInst::UMin: Operation = "AtomicRMWInst::UMin"; break;
  1454. case AtomicRMWInst::BAD_BINOP: llvm_unreachable("Bad atomic operation");
  1455. }
  1456. Out << "AtomicRMWInst* " << iName
  1457. << " = new AtomicRMWInst("
  1458. << Operation << ", "
  1459. << opNames[0] << ", " << opNames[1] << ", "
  1460. << Ordering << ", " << CrossThread << ", " << bbname
  1461. << ");";
  1462. nl(Out) << iName << "->setName(\"";
  1463. printEscapedString(rmwi->getName());
  1464. Out << "\");";
  1465. break;
  1466. }
  1467. }
  1468. DefinedValues.insert(I);
  1469. nl(Out);
  1470. delete [] opNames;
  1471. }
  1472. // Print out the types, constants and declarations needed by one function
  1473. void CppWriter::printFunctionUses(const Function* F) {
  1474. nl(Out) << "// Type Definitions"; nl(Out);
  1475. if (!is_inline) {
  1476. // Print the function's return type
  1477. printType(F->getReturnType());
  1478. // Print the function's function type
  1479. printType(F->getFunctionType());
  1480. // Print the types of each of the function's arguments
  1481. for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
  1482. AI != AE; ++AI) {
  1483. printType(AI->getType());
  1484. }
  1485. }
  1486. // Print type definitions for every type referenced by an instruction and
  1487. // make a note of any global values or constants that are referenced
  1488. SmallPtrSet<GlobalValue*,64> gvs;
  1489. SmallPtrSet<Constant*,64> consts;
  1490. for (Function::const_iterator BB = F->begin(), BE = F->end();
  1491. BB != BE; ++BB){
  1492. for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
  1493. I != E; ++I) {
  1494. // Print the type of the instruction itself
  1495. printType(I->getType());
  1496. // Print the type of each of the instruction's operands
  1497. for (unsigned i = 0; i < I->getNumOperands(); ++i) {
  1498. Value* operand = I->getOperand(i);
  1499. printType(operand->getType());
  1500. // If the operand references a GVal or Constant, make a note of it
  1501. if (GlobalValue* GV = dyn_cast<GlobalValue>(operand)) {
  1502. gvs.insert(GV);
  1503. if (GenerationType != GenFunction)
  1504. if (GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
  1505. if (GVar->hasInitializer())
  1506. consts.insert(GVar->getInitializer());
  1507. } else if (Constant* C = dyn_cast<Constant>(operand)) {
  1508. consts.insert(C);
  1509. for (unsigned j = 0; j < C->getNumOperands(); ++j) {
  1510. // If the operand references a GVal or Constant, make a note of it
  1511. Value* operand = C->getOperand(j);
  1512. printType(operand->getType());
  1513. if (GlobalValue* GV = dyn_cast<GlobalValue>(operand)) {
  1514. gvs.insert(GV);
  1515. if (GenerationType != GenFunction)
  1516. if (GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
  1517. if (GVar->hasInitializer())
  1518. consts.insert(GVar->getInitializer());
  1519. }
  1520. }
  1521. }
  1522. }
  1523. }
  1524. }
  1525. // Print the function declarations for any functions encountered
  1526. nl(Out) << "// Function Declarations"; nl(Out);
  1527. for (SmallPtrSet<GlobalValue*,64>::iterator I = gvs.begin(), E = gvs.end();
  1528. I != E; ++I) {
  1529. if (Function* Fun = dyn_cast<Function>(*I)) {
  1530. if (!is_inline || Fun != F)
  1531. printFunctionHead(Fun);
  1532. }
  1533. }
  1534. // Print the global variable declarations for any variables encountered
  1535. nl(Out) << "// Global Variable Declarations"; nl(Out);
  1536. for (SmallPtrSet<GlobalValue*,64>::iterator I = gvs.begin(), E = gvs.end();
  1537. I != E; ++I) {
  1538. if (GlobalVariable* F = dyn_cast<GlobalVariable>(*I))
  1539. printVariableHead(F);
  1540. }
  1541. // Print the constants found
  1542. nl(Out) << "// Constant Definitions"; nl(Out);
  1543. for (SmallPtrSet<Constant*,64>::iterator I = consts.begin(),
  1544. E = consts.end(); I != E; ++I) {
  1545. printConstant(*I);
  1546. }
  1547. // Process the global variables definitions now that all the constants have
  1548. // been emitted. These definitions just couple the gvars with their constant
  1549. // initializers.
  1550. if (GenerationType != GenFunction) {
  1551. nl(Out) << "// Global Variable Definitions"; nl(Out);
  1552. for (SmallPtrSet<GlobalValue*,64>::iterator I = gvs.begin(), E = gvs.end();
  1553. I != E; ++I) {
  1554. if (GlobalVariable* GV = dyn_cast<GlobalVariable>(*I))
  1555. printVariableBody(GV);
  1556. }
  1557. }
  1558. }
  1559. void CppWriter::printFunctionHead(const Function* F) {
  1560. nl(Out) << "Function* " << getCppName(F);
  1561. Out << " = mod->getFunction(\"";
  1562. printEscapedString(F->getName());
  1563. Out << "\");";
  1564. nl(Out) << "if (!" << getCppName(F) << ") {";
  1565. nl(Out) << getCppName(F);
  1566. Out<< " = Function::Create(";
  1567. nl(Out,1) << "/*Type=*/" << getCppName(F->getFunctionType()) << ",";
  1568. nl(Out) << "/*Linkage=*/";
  1569. printLinkageType(F->getLinkage());
  1570. Out << ",";
  1571. nl(Out) << "/*Name=*/\"";
  1572. printEscapedString(F->getName());
  1573. Out << "\", mod); " << (F->isDeclaration()? "// (external, no body)" : "");
  1574. nl(Out,-1);
  1575. printCppName(F);
  1576. Out << "->setCallingConv(";
  1577. printCallingConv(F->getCallingConv());
  1578. Out << ");";
  1579. nl(Out);
  1580. if (F->hasSection()) {
  1581. printCppName(F);
  1582. Out << "->setSection(\"" << F->getSection() << "\");";
  1583. nl(Out);
  1584. }
  1585. if (F->getAlignment()) {
  1586. printCppName(F);
  1587. Out << "->setAlignment(" << F->getAlignment() << ");";
  1588. nl(Out);
  1589. }
  1590. if (F->getVisibility() != GlobalValue::DefaultVisibility) {
  1591. printCppName(F);
  1592. Out << "->setVisibility(";
  1593. printVisibilityType(F->getVisibility());
  1594. Out << ");";
  1595. nl(Out);
  1596. }
  1597. if (F->hasGC()) {
  1598. printCppName(F);
  1599. Out << "->setGC(\"" << F->getGC() << "\");";
  1600. nl(Out);
  1601. }
  1602. Out << "}";
  1603. nl(Out);
  1604. printAttributes(F->getAttributes(), getCppName(F));
  1605. printCppName(F);
  1606. Out << "->setAttributes(" << getCppName(F) << "_PAL);";
  1607. nl(Out);
  1608. }
  1609. void CppWriter::printFunctionBody(const Function *F) {
  1610. if (F->isDeclaration())
  1611. return; // external functions have no bodies.
  1612. // Clear the DefinedValues and ForwardRefs maps because we can't have
  1613. // cross-function forward refs
  1614. ForwardRefs.clear();
  1615. DefinedValues.clear();
  1616. // Create all the argument values
  1617. if (!is_inline) {
  1618. if (!F->arg_empty()) {
  1619. Out << "Function::arg_iterator args = " << getCppName(F)
  1620. << "->arg_begin();";
  1621. nl(Out);
  1622. }
  1623. for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
  1624. AI != AE; ++AI) {
  1625. Out << "Value* " << getCppName(AI) << " = args++;";
  1626. nl(Out);
  1627. if (AI->hasName()) {
  1628. Out << getCppName(AI) << "->setName(\"";
  1629. printEscapedString(AI->getName());
  1630. Out << "\");";
  1631. nl(Out);
  1632. }
  1633. }
  1634. }
  1635. // Create all the basic blocks
  1636. nl(Out);
  1637. for (Function::const_iterator BI = F->begin(), BE = F->end();
  1638. BI != BE; ++BI) {
  1639. std::string bbname(getCppName(BI));
  1640. Out << "BasicBlock* " << bbname <<
  1641. " = BasicBlock::Create(mod->getContext(), \"";
  1642. if (BI->hasName())
  1643. printEscapedString(BI->getName());
  1644. Out << "\"," << getCppName(BI->getParent()) << ",0);";
  1645. nl(Out);
  1646. }
  1647. // Output all of its basic blocks... for the function
  1648. for (Function::const_iterator BI = F->begin(), BE = F->end();
  1649. BI != BE; ++BI) {
  1650. std::string bbname(getCppName(BI));
  1651. nl(Out) << "// Block " << BI->getName() << " (" << bbname << ")";
  1652. nl(Out);
  1653. // Output all of the instructions in the basic block...
  1654. for (BasicBlock::const_iterator I = BI->begin(), E = BI->end();
  1655. I != E; ++I) {
  1656. printInstruction(I,bbname);
  1657. }
  1658. }
  1659. // Loop over the ForwardRefs and resolve them now that all instructions
  1660. // are generated.
  1661. if (!ForwardRefs.empty()) {
  1662. nl(Out) << "// Resolve Forward References";
  1663. nl(Out);
  1664. }
  1665. while (!ForwardRefs.empty()) {
  1666. ForwardRefMap::iterator I = ForwardRefs.begin();
  1667. Out << I->second << "->replaceAllUsesWith("
  1668. << getCppName(I->first) << "); delete " << I->second << ";";
  1669. nl(Out);
  1670. ForwardRefs.erase(I);
  1671. }
  1672. }
  1673. void CppWriter::printInline(const std::string& fname,
  1674. const std::string& func) {
  1675. const Function* F = TheModule->getFunction(func);
  1676. if (!F) {
  1677. error(std::string("Function '") + func + "' not found in input module");
  1678. return;
  1679. }
  1680. if (F->isDeclaration()) {
  1681. error(std::string("Function '") + func + "' is external!");
  1682. return;
  1683. }
  1684. nl(Out) << "BasicBlock* " << fname << "(Module* mod, Function *"
  1685. << getCppName(F);
  1686. unsigned arg_count = 1;
  1687. for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
  1688. AI != AE; ++AI) {
  1689. Out << ", Value* arg_" << arg_count;
  1690. }
  1691. Out << ") {";
  1692. nl(Out);
  1693. is_inline = true;
  1694. printFunctionUses(F);
  1695. printFunctionBody(F);
  1696. is_inline = false;
  1697. Out << "return " << getCppName(F->begin()) << ";";
  1698. nl(Out) << "}";
  1699. nl(Out);
  1700. }
  1701. void CppWriter::printModuleBody() {
  1702. // Print out all the type definitions
  1703. nl(Out) << "// Type Definitions"; nl(Out);
  1704. printTypes(TheModule);
  1705. // Functions can call each other and global variables can reference them so
  1706. // define all the functions first before emitting their function bodies.
  1707. nl(Out) << "// Function Declarations"; nl(Out);
  1708. for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
  1709. I != E; ++I)
  1710. printFunctionHead(I);
  1711. // Process the global variables declarations. We can't initialze them until
  1712. // after the constants are printed so just print a header for each global
  1713. nl(Out) << "// Global Variable Declarations\n"; nl(Out);
  1714. for (Module::const_global_iterator I = TheModule->global_begin(),
  1715. E = TheModule->global_end(); I != E; ++I) {
  1716. printVariableHead(I);
  1717. }
  1718. // Print out all the constants definitions. Constants don't recurse except
  1719. // through GlobalValues. All GlobalValues have been declared at this point
  1720. // so we can proceed to generate the constants.
  1721. nl(Out) << "// Constant Definitions"; nl(Out);
  1722. printConstants(TheModule);
  1723. // Process the global variables definitions now that all the constants have
  1724. // been emitted. These definitions just couple the gvars with their constant
  1725. // initializers.
  1726. nl(Out) << "// Global Variable Definitions"; nl(Out);
  1727. for (Module::const_global_iterator I = TheModule->global_begin(),
  1728. E = TheModule->global_end(); I != E; ++I) {
  1729. printVariableBody(I);
  1730. }
  1731. // Finally, we can safely put out all of the function bodies.
  1732. nl(Out) << "// Function Definitions"; nl(Out);
  1733. for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
  1734. I != E; ++I) {
  1735. if (!I->isDeclaration()) {
  1736. nl(Out) << "// Function: " << I->getName() << " (" << getCppName(I)
  1737. << ")";
  1738. nl(Out) << "{";
  1739. nl(Out,1);
  1740. printFunctionBody(I);
  1741. nl(Out,-1) << "}";
  1742. nl(Out);
  1743. }
  1744. }
  1745. }
  1746. void CppWriter::printProgram(const std::string& fname,
  1747. const std::string& mName) {
  1748. Out << "#include <llvm/LLVMContext.h>\n";
  1749. Out << "#include <llvm/Module.h>\n";
  1750. Out << "#include <llvm/DerivedTypes.h>\n";
  1751. Out << "#include <llvm/Constants.h>\n";
  1752. Out << "#include <llvm/GlobalVariable.h>\n";
  1753. Out << "#include <llvm/Function.h>\n";
  1754. Out << "#include <llvm/CallingConv.h>\n";
  1755. Out << "#include <llvm/BasicBlock.h>\n";
  1756. Out << "#include <llvm/Instructions.h>\n";
  1757. Out << "#include <llvm/InlineAsm.h>\n";
  1758. Out << "#include <llvm/Support/FormattedStream.h>\n";
  1759. Out << "#include <llvm/Support/MathExtras.h>\n";
  1760. Out << "#include <llvm/Pass.h>\n";
  1761. Out << "#include <llvm/PassManager.h>\n";
  1762. Out << "#include <llvm/ADT/SmallVector.h>\n";
  1763. Out << "#include <llvm/Analysis/Verifier.h>\n";
  1764. Out << "#include <llvm/Assembly/PrintModulePass.h>\n";
  1765. Out << "#include <algorithm>\n";
  1766. Out << "using namespace llvm;\n\n";
  1767. Out << "Module* " << fname << "();\n\n";
  1768. Out << "int main(int argc, char**argv) {\n";
  1769. Out << " Module* Mod = " << fname << "();\n";
  1770. Out << " verifyModule(*Mod, PrintMessageAction);\n";
  1771. Out << " PassManager PM;\n";
  1772. Out << " PM.add(createPrintModulePass(&outs()));\n";
  1773. Out << " PM.run(*Mod);\n";
  1774. Out << " return 0;\n";
  1775. Out << "}\n\n";
  1776. printModule(fname,mName);
  1777. }
  1778. void CppWriter::printModule(const std::string& fname,
  1779. const std::string& mName) {
  1780. nl(Out) << "Module* " << fname << "() {";
  1781. nl(Out,1) << "// Module Construction";
  1782. nl(Out) << "Module* mod = new Module(\"";
  1783. printEscapedString(mName);
  1784. Out << "\", getGlobalContext());";
  1785. if (!TheModule->getTargetTriple().empty()) {
  1786. nl(Out) << "mod->setDataLayout(\"" << TheModule->getDataLayout() << "\");";
  1787. }
  1788. if (!TheModule->getTargetTriple().empty()) {
  1789. nl(Out) << "mod->setTargetTriple(\"" << TheModule->getTargetTriple()
  1790. << "\");";
  1791. }
  1792. if (!TheModule->getModuleInlineAsm().empty()) {
  1793. nl(Out) << "mod->setModuleInlineAsm(\"";
  1794. printEscapedString(TheModule->getModuleInlineAsm());
  1795. Out << "\");";
  1796. }
  1797. nl(Out);
  1798. // Loop over the dependent libraries and emit them.
  1799. Module::lib_iterator LI = TheModule->lib_begin();
  1800. Module::lib_iterator LE = TheModule->lib_end();
  1801. while (LI != LE) {
  1802. Out << "mod->addLibrary(\"" << *LI << "\");";
  1803. nl(Out);
  1804. ++LI;
  1805. }
  1806. printModuleBody();
  1807. nl(Out) << "return mod;";
  1808. nl(Out,-1) << "}";
  1809. nl(Out);
  1810. }
  1811. void CppWriter::printContents(const std::string& fname,
  1812. const std::string& mName) {
  1813. Out << "\nModule* " << fname << "(Module *mod) {\n";
  1814. Out << "\nmod->setModuleIdentifier(\"";
  1815. printEscapedString(mName);
  1816. Out << "\");\n";
  1817. printModuleBody();
  1818. Out << "\nreturn mod;\n";
  1819. Out << "\n}\n";
  1820. }
  1821. void CppWriter::printFunction(const std::string& fname,
  1822. const std::string& funcName) {
  1823. const Function* F = TheModule->getFunction(funcName);
  1824. if (!F) {
  1825. error(std::string("Function '") + funcName + "' not found in input module");
  1826. return;
  1827. }
  1828. Out << "\nFunction* " << fname << "(Module *mod) {\n";
  1829. printFunctionUses(F);
  1830. printFunctionHead(F);
  1831. printFunctionBody(F);
  1832. Out << "return " << getCppName(F) << ";\n";
  1833. Out << "}\n";
  1834. }
  1835. void CppWriter::printFunctions() {
  1836. const Module::FunctionListType &funcs = TheModule->getFunctionList();
  1837. Module::const_iterator I = funcs.begin();
  1838. Module::const_iterator IE = funcs.end();
  1839. for (; I != IE; ++I) {
  1840. const Function &func = *I;
  1841. if (!func.isDeclaration()) {
  1842. std::string name("define_");
  1843. name += func.getName();
  1844. printFunction(name, func.getName());
  1845. }
  1846. }
  1847. }
  1848. void CppWriter::printVariable(const std::string& fname,
  1849. const std::string& varName) {
  1850. const GlobalVariable* GV = TheModule->getNamedGlobal(varName);
  1851. if (!GV) {
  1852. error(std::string("Variable '") + varName + "' not found in input module");
  1853. return;
  1854. }
  1855. Out << "\nGlobalVariable* " << fname << "(Module *mod) {\n";
  1856. printVariableUses(GV);
  1857. printVariableHead(GV);
  1858. printVariableBody(GV);
  1859. Out << "return " << getCppName(GV) << ";\n";
  1860. Out << "}\n";
  1861. }
  1862. void CppWriter::printType(const std::string &fname,
  1863. const std::string &typeName) {
  1864. Type* Ty = TheModule->getTypeByName(typeName);
  1865. if (!Ty) {
  1866. error(std::string("Type '") + typeName + "' not found in input module");
  1867. return;
  1868. }
  1869. Out << "\nType* " << fname << "(Module *mod) {\n";
  1870. printType(Ty);
  1871. Out << "return " << getCppName(Ty) << ";\n";
  1872. Out << "}\n";
  1873. }
  1874. bool CppWriter::runOnModule(Module &M) {
  1875. TheModule = &M;
  1876. // Emit a header
  1877. Out << "// Generated by llvm2cpp - DO NOT MODIFY!\n\n";
  1878. // Get the name of the function we're supposed to generate
  1879. std::string fname = FuncName.getValue();
  1880. // Get the name of the thing we are to generate
  1881. std::string tgtname = NameToGenerate.getValue();
  1882. if (GenerationType == GenModule ||
  1883. GenerationType == GenContents ||
  1884. GenerationType == GenProgram ||
  1885. GenerationType == GenFunctions) {
  1886. if (tgtname == "!bad!") {
  1887. if (M.getModuleIdentifier() == "-")
  1888. tgtname = "<stdin>";
  1889. else
  1890. tgtname = M.getModuleIdentifier();
  1891. }
  1892. } else if (tgtname == "!bad!")
  1893. error("You must use the -for option with -gen-{function,variable,type}");
  1894. switch (WhatToGenerate(GenerationType)) {
  1895. case GenProgram:
  1896. if (fname.empty())
  1897. fname = "makeLLVMModule";
  1898. printProgram(fname,tgtname);
  1899. break;
  1900. case GenModule:
  1901. if (fname.empty())
  1902. fname = "makeLLVMModule";
  1903. printModule(fname,tgtname);
  1904. break;
  1905. case GenContents:
  1906. if (fname.empty())
  1907. fname = "makeLLVMModuleContents";
  1908. printContents(fname,tgtname);
  1909. break;
  1910. case GenFunction:
  1911. if (fname.empty())
  1912. fname = "makeLLVMFunction";
  1913. printFunction(fname,tgtname);
  1914. break;
  1915. case GenFunctions:
  1916. printFunctions();
  1917. break;
  1918. case GenInline:
  1919. if (fname.empty())
  1920. fname = "makeLLVMInline";
  1921. printInline(fname,tgtname);
  1922. break;
  1923. case GenVariable:
  1924. if (fname.empty())
  1925. fname = "makeLLVMVariable";
  1926. printVariable(fname,tgtname);
  1927. break;
  1928. case GenType:
  1929. if (fname.empty())
  1930. fname = "makeLLVMType";
  1931. printType(fname,tgtname);
  1932. break;
  1933. }
  1934. return false;
  1935. }
  1936. char CppWriter::ID = 0;
  1937. //===----------------------------------------------------------------------===//
  1938. // External Interface declaration
  1939. //===----------------------------------------------------------------------===//
  1940. bool CPPTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
  1941. formatted_raw_ostream &o,
  1942. CodeGenFileType FileType,
  1943. bool DisableVerify) {
  1944. if (FileType != TargetMachine::CGFT_AssemblyFile) return true;
  1945. PM.add(new CppWriter(o));
  1946. return false;
  1947. }