ExceptionDemo.cpp 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982
  1. //===-- ExceptionDemo.cpp - An example using llvm Exceptions --------------===//
  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. // Demo program which implements an example LLVM exception implementation, and
  11. // shows several test cases including the handling of foreign exceptions.
  12. // It is run with type info types arguments to throw. A test will
  13. // be run for each given type info type. While type info types with the value
  14. // of -1 will trigger a foreign C++ exception to be thrown; type info types
  15. // <= 6 and >= 1 will cause the associated generated exceptions to be thrown
  16. // and caught by generated test functions; and type info types > 6
  17. // will result in exceptions which pass through to the test harness. All other
  18. // type info types are not supported and could cause a crash. In all cases,
  19. // the "finally" blocks of every generated test functions will executed
  20. // regardless of whether or not that test function ignores or catches the
  21. // thrown exception.
  22. //
  23. // examples:
  24. //
  25. // ExceptionDemo
  26. //
  27. // causes a usage to be printed to stderr
  28. //
  29. // ExceptionDemo 2 3 7 -1
  30. //
  31. // results in the following cases:
  32. // - Value 2 causes an exception with a type info type of 2 to be
  33. // thrown and caught by an inner generated test function.
  34. // - Value 3 causes an exception with a type info type of 3 to be
  35. // thrown and caught by an outer generated test function.
  36. // - Value 7 causes an exception with a type info type of 7 to be
  37. // thrown and NOT be caught by any generated function.
  38. // - Value -1 causes a foreign C++ exception to be thrown and not be
  39. // caught by any generated function
  40. //
  41. // Cases -1 and 7 are caught by a C++ test harness where the validity of
  42. // of a C++ catch(...) clause catching a generated exception with a
  43. // type info type of 7 is explained by: example in rules 1.6.4 in
  44. // http://mentorembedded.github.com/cxx-abi/abi-eh.html (v1.22)
  45. //
  46. // This code uses code from the llvm compiler-rt project and the llvm
  47. // Kaleidoscope project.
  48. //
  49. //===----------------------------------------------------------------------===//
  50. #include "llvm/ADT/STLExtras.h"
  51. #include "llvm/IR/Verifier.h"
  52. #include "llvm/ExecutionEngine/MCJIT.h"
  53. #include "llvm/ExecutionEngine/SectionMemoryManager.h"
  54. #include "llvm/IR/DataLayout.h"
  55. #include "llvm/IR/DerivedTypes.h"
  56. #include "llvm/IR/IRBuilder.h"
  57. #include "llvm/IR/Intrinsics.h"
  58. #include "llvm/IR/LLVMContext.h"
  59. #include "llvm/IR/LegacyPassManager.h"
  60. #include "llvm/IR/Module.h"
  61. #include "llvm/Support/Dwarf.h"
  62. #include "llvm/Support/TargetSelect.h"
  63. #include "llvm/Target/TargetOptions.h"
  64. #include "llvm/Transforms/Scalar.h"
  65. // FIXME: Although all systems tested with (Linux, OS X), do not need this
  66. // header file included. A user on ubuntu reported, undefined symbols
  67. // for stderr, and fprintf, and the addition of this include fixed the
  68. // issue for them. Given that LLVM's best practices include the goal
  69. // of reducing the number of redundant header files included, the
  70. // correct solution would be to find out why these symbols are not
  71. // defined for the system in question, and fix the issue by finding out
  72. // which LLVM header file, if any, would include these symbols.
  73. #include <cstdio>
  74. #include <sstream>
  75. #include <stdexcept>
  76. #include <inttypes.h>
  77. #include <unwind.h>
  78. #ifndef USE_GLOBAL_STR_CONSTS
  79. #define USE_GLOBAL_STR_CONSTS true
  80. #endif
  81. //
  82. // Example types
  83. //
  84. /// This is our simplistic type info
  85. struct OurExceptionType_t {
  86. /// type info type
  87. int type;
  88. };
  89. /// This is our Exception class which relies on a negative offset to calculate
  90. /// pointers to its instances from pointers to its unwindException member.
  91. ///
  92. /// Note: The above unwind.h defines struct _Unwind_Exception to be aligned
  93. /// on a double word boundary. This is necessary to match the standard:
  94. /// http://mentorembedded.github.com/cxx-abi/abi-eh.html
  95. struct OurBaseException_t {
  96. struct OurExceptionType_t type;
  97. // Note: This is properly aligned in unwind.h
  98. struct _Unwind_Exception unwindException;
  99. };
  100. // Note: Not needed since we are C++
  101. typedef struct OurBaseException_t OurException;
  102. typedef struct _Unwind_Exception OurUnwindException;
  103. //
  104. // Various globals used to support typeinfo and generatted exceptions in
  105. // general
  106. //
  107. static std::map<std::string, llvm::Value*> namedValues;
  108. int64_t ourBaseFromUnwindOffset;
  109. const unsigned char ourBaseExcpClassChars[] =
  110. {'o', 'b', 'j', '\0', 'b', 'a', 's', '\0'};
  111. static uint64_t ourBaseExceptionClass = 0;
  112. static std::vector<std::string> ourTypeInfoNames;
  113. static std::map<int, std::string> ourTypeInfoNamesIndex;
  114. static llvm::StructType *ourTypeInfoType;
  115. static llvm::StructType *ourCaughtResultType;
  116. static llvm::StructType *ourExceptionType;
  117. static llvm::StructType *ourUnwindExceptionType;
  118. static llvm::ConstantInt *ourExceptionNotThrownState;
  119. static llvm::ConstantInt *ourExceptionThrownState;
  120. static llvm::ConstantInt *ourExceptionCaughtState;
  121. typedef std::vector<std::string> ArgNames;
  122. typedef std::vector<llvm::Type*> ArgTypes;
  123. //
  124. // Code Generation Utilities
  125. //
  126. /// Utility used to create a function, both declarations and definitions
  127. /// @param module for module instance
  128. /// @param retType function return type
  129. /// @param theArgTypes function's ordered argument types
  130. /// @param theArgNames function's ordered arguments needed if use of this
  131. /// function corresponds to a function definition. Use empty
  132. /// aggregate for function declarations.
  133. /// @param functName function name
  134. /// @param linkage function linkage
  135. /// @param declarationOnly for function declarations
  136. /// @param isVarArg function uses vararg arguments
  137. /// @returns function instance
  138. llvm::Function *createFunction(llvm::Module &module,
  139. llvm::Type *retType,
  140. const ArgTypes &theArgTypes,
  141. const ArgNames &theArgNames,
  142. const std::string &functName,
  143. llvm::GlobalValue::LinkageTypes linkage,
  144. bool declarationOnly,
  145. bool isVarArg) {
  146. llvm::FunctionType *functType =
  147. llvm::FunctionType::get(retType, theArgTypes, isVarArg);
  148. llvm::Function *ret =
  149. llvm::Function::Create(functType, linkage, functName, &module);
  150. if (!ret || declarationOnly)
  151. return(ret);
  152. namedValues.clear();
  153. unsigned i = 0;
  154. for (llvm::Function::arg_iterator argIndex = ret->arg_begin();
  155. i != theArgNames.size();
  156. ++argIndex, ++i) {
  157. argIndex->setName(theArgNames[i]);
  158. namedValues[theArgNames[i]] = argIndex;
  159. }
  160. return(ret);
  161. }
  162. /// Create an alloca instruction in the entry block of
  163. /// the parent function. This is used for mutable variables etc.
  164. /// @param function parent instance
  165. /// @param varName stack variable name
  166. /// @param type stack variable type
  167. /// @param initWith optional constant initialization value
  168. /// @returns AllocaInst instance
  169. static llvm::AllocaInst *createEntryBlockAlloca(llvm::Function &function,
  170. const std::string &varName,
  171. llvm::Type *type,
  172. llvm::Constant *initWith = 0) {
  173. llvm::BasicBlock &block = function.getEntryBlock();
  174. llvm::IRBuilder<> tmp(&block, block.begin());
  175. llvm::AllocaInst *ret = tmp.CreateAlloca(type, 0, varName);
  176. if (initWith)
  177. tmp.CreateStore(initWith, ret);
  178. return(ret);
  179. }
  180. //
  181. // Code Generation Utilities End
  182. //
  183. //
  184. // Runtime C Library functions
  185. //
  186. // Note: using an extern "C" block so that static functions can be used
  187. extern "C" {
  188. // Note: Better ways to decide on bit width
  189. //
  190. /// Prints a 32 bit number, according to the format, to stderr.
  191. /// @param intToPrint integer to print
  192. /// @param format printf like format to use when printing
  193. void print32Int(int intToPrint, const char *format) {
  194. if (format) {
  195. // Note: No NULL check
  196. fprintf(stderr, format, intToPrint);
  197. }
  198. else {
  199. // Note: No NULL check
  200. fprintf(stderr, "::print32Int(...):NULL arg.\n");
  201. }
  202. }
  203. // Note: Better ways to decide on bit width
  204. //
  205. /// Prints a 64 bit number, according to the format, to stderr.
  206. /// @param intToPrint integer to print
  207. /// @param format printf like format to use when printing
  208. void print64Int(long int intToPrint, const char *format) {
  209. if (format) {
  210. // Note: No NULL check
  211. fprintf(stderr, format, intToPrint);
  212. }
  213. else {
  214. // Note: No NULL check
  215. fprintf(stderr, "::print64Int(...):NULL arg.\n");
  216. }
  217. }
  218. /// Prints a C string to stderr
  219. /// @param toPrint string to print
  220. void printStr(char *toPrint) {
  221. if (toPrint) {
  222. fprintf(stderr, "%s", toPrint);
  223. }
  224. else {
  225. fprintf(stderr, "::printStr(...):NULL arg.\n");
  226. }
  227. }
  228. /// Deletes the true previously allocated exception whose address
  229. /// is calculated from the supplied OurBaseException_t::unwindException
  230. /// member address. Handles (ignores), NULL pointers.
  231. /// @param expToDelete exception to delete
  232. void deleteOurException(OurUnwindException *expToDelete) {
  233. #ifdef DEBUG
  234. fprintf(stderr,
  235. "deleteOurException(...).\n");
  236. #endif
  237. if (expToDelete &&
  238. (expToDelete->exception_class == ourBaseExceptionClass)) {
  239. free(((char*) expToDelete) + ourBaseFromUnwindOffset);
  240. }
  241. }
  242. /// This function is the struct _Unwind_Exception API mandated delete function
  243. /// used by foreign exception handlers when deleting our exception
  244. /// (OurException), instances.
  245. /// @param reason See @link http://mentorembedded.github.com/cxx-abi/abi-eh.html
  246. /// @unlink
  247. /// @param expToDelete exception instance to delete
  248. void deleteFromUnwindOurException(_Unwind_Reason_Code reason,
  249. OurUnwindException *expToDelete) {
  250. #ifdef DEBUG
  251. fprintf(stderr,
  252. "deleteFromUnwindOurException(...).\n");
  253. #endif
  254. deleteOurException(expToDelete);
  255. }
  256. /// Creates (allocates on the heap), an exception (OurException instance),
  257. /// of the supplied type info type.
  258. /// @param type type info type
  259. OurUnwindException *createOurException(int type) {
  260. size_t size = sizeof(OurException);
  261. OurException *ret = (OurException*) memset(malloc(size), 0, size);
  262. (ret->type).type = type;
  263. (ret->unwindException).exception_class = ourBaseExceptionClass;
  264. (ret->unwindException).exception_cleanup = deleteFromUnwindOurException;
  265. return(&(ret->unwindException));
  266. }
  267. /// Read a uleb128 encoded value and advance pointer
  268. /// See Variable Length Data in:
  269. /// @link http://dwarfstd.org/Dwarf3.pdf @unlink
  270. /// @param data reference variable holding memory pointer to decode from
  271. /// @returns decoded value
  272. static uintptr_t readULEB128(const uint8_t **data) {
  273. uintptr_t result = 0;
  274. uintptr_t shift = 0;
  275. unsigned char byte;
  276. const uint8_t *p = *data;
  277. do {
  278. byte = *p++;
  279. result |= (byte & 0x7f) << shift;
  280. shift += 7;
  281. }
  282. while (byte & 0x80);
  283. *data = p;
  284. return result;
  285. }
  286. /// Read a sleb128 encoded value and advance pointer
  287. /// See Variable Length Data in:
  288. /// @link http://dwarfstd.org/Dwarf3.pdf @unlink
  289. /// @param data reference variable holding memory pointer to decode from
  290. /// @returns decoded value
  291. static uintptr_t readSLEB128(const uint8_t **data) {
  292. uintptr_t result = 0;
  293. uintptr_t shift = 0;
  294. unsigned char byte;
  295. const uint8_t *p = *data;
  296. do {
  297. byte = *p++;
  298. result |= (byte & 0x7f) << shift;
  299. shift += 7;
  300. }
  301. while (byte & 0x80);
  302. *data = p;
  303. if ((byte & 0x40) && (shift < (sizeof(result) << 3))) {
  304. result |= (~0 << shift);
  305. }
  306. return result;
  307. }
  308. unsigned getEncodingSize(uint8_t Encoding) {
  309. if (Encoding == llvm::dwarf::DW_EH_PE_omit)
  310. return 0;
  311. switch (Encoding & 0x0F) {
  312. case llvm::dwarf::DW_EH_PE_absptr:
  313. return sizeof(uintptr_t);
  314. case llvm::dwarf::DW_EH_PE_udata2:
  315. return sizeof(uint16_t);
  316. case llvm::dwarf::DW_EH_PE_udata4:
  317. return sizeof(uint32_t);
  318. case llvm::dwarf::DW_EH_PE_udata8:
  319. return sizeof(uint64_t);
  320. case llvm::dwarf::DW_EH_PE_sdata2:
  321. return sizeof(int16_t);
  322. case llvm::dwarf::DW_EH_PE_sdata4:
  323. return sizeof(int32_t);
  324. case llvm::dwarf::DW_EH_PE_sdata8:
  325. return sizeof(int64_t);
  326. default:
  327. // not supported
  328. abort();
  329. }
  330. }
  331. /// Read a pointer encoded value and advance pointer
  332. /// See Variable Length Data in:
  333. /// @link http://dwarfstd.org/Dwarf3.pdf @unlink
  334. /// @param data reference variable holding memory pointer to decode from
  335. /// @param encoding dwarf encoding type
  336. /// @returns decoded value
  337. static uintptr_t readEncodedPointer(const uint8_t **data, uint8_t encoding) {
  338. uintptr_t result = 0;
  339. const uint8_t *p = *data;
  340. if (encoding == llvm::dwarf::DW_EH_PE_omit)
  341. return(result);
  342. // first get value
  343. switch (encoding & 0x0F) {
  344. case llvm::dwarf::DW_EH_PE_absptr:
  345. result = *((uintptr_t*)p);
  346. p += sizeof(uintptr_t);
  347. break;
  348. case llvm::dwarf::DW_EH_PE_uleb128:
  349. result = readULEB128(&p);
  350. break;
  351. // Note: This case has not been tested
  352. case llvm::dwarf::DW_EH_PE_sleb128:
  353. result = readSLEB128(&p);
  354. break;
  355. case llvm::dwarf::DW_EH_PE_udata2:
  356. result = *((uint16_t*)p);
  357. p += sizeof(uint16_t);
  358. break;
  359. case llvm::dwarf::DW_EH_PE_udata4:
  360. result = *((uint32_t*)p);
  361. p += sizeof(uint32_t);
  362. break;
  363. case llvm::dwarf::DW_EH_PE_udata8:
  364. result = *((uint64_t*)p);
  365. p += sizeof(uint64_t);
  366. break;
  367. case llvm::dwarf::DW_EH_PE_sdata2:
  368. result = *((int16_t*)p);
  369. p += sizeof(int16_t);
  370. break;
  371. case llvm::dwarf::DW_EH_PE_sdata4:
  372. result = *((int32_t*)p);
  373. p += sizeof(int32_t);
  374. break;
  375. case llvm::dwarf::DW_EH_PE_sdata8:
  376. result = *((int64_t*)p);
  377. p += sizeof(int64_t);
  378. break;
  379. default:
  380. // not supported
  381. abort();
  382. break;
  383. }
  384. // then add relative offset
  385. switch (encoding & 0x70) {
  386. case llvm::dwarf::DW_EH_PE_absptr:
  387. // do nothing
  388. break;
  389. case llvm::dwarf::DW_EH_PE_pcrel:
  390. result += (uintptr_t)(*data);
  391. break;
  392. case llvm::dwarf::DW_EH_PE_textrel:
  393. case llvm::dwarf::DW_EH_PE_datarel:
  394. case llvm::dwarf::DW_EH_PE_funcrel:
  395. case llvm::dwarf::DW_EH_PE_aligned:
  396. default:
  397. // not supported
  398. abort();
  399. break;
  400. }
  401. // then apply indirection
  402. if (encoding & llvm::dwarf::DW_EH_PE_indirect) {
  403. result = *((uintptr_t*)result);
  404. }
  405. *data = p;
  406. return result;
  407. }
  408. /// Deals with Dwarf actions matching our type infos
  409. /// (OurExceptionType_t instances). Returns whether or not a dwarf emitted
  410. /// action matches the supplied exception type. If such a match succeeds,
  411. /// the resultAction argument will be set with > 0 index value. Only
  412. /// corresponding llvm.eh.selector type info arguments, cleanup arguments
  413. /// are supported. Filters are not supported.
  414. /// See Variable Length Data in:
  415. /// @link http://dwarfstd.org/Dwarf3.pdf @unlink
  416. /// Also see @link http://mentorembedded.github.com/cxx-abi/abi-eh.html @unlink
  417. /// @param resultAction reference variable which will be set with result
  418. /// @param classInfo our array of type info pointers (to globals)
  419. /// @param actionEntry index into above type info array or 0 (clean up).
  420. /// We do not support filters.
  421. /// @param exceptionClass exception class (_Unwind_Exception::exception_class)
  422. /// of thrown exception.
  423. /// @param exceptionObject thrown _Unwind_Exception instance.
  424. /// @returns whether or not a type info was found. False is returned if only
  425. /// a cleanup was found
  426. static bool handleActionValue(int64_t *resultAction,
  427. uint8_t TTypeEncoding,
  428. const uint8_t *ClassInfo,
  429. uintptr_t actionEntry,
  430. uint64_t exceptionClass,
  431. struct _Unwind_Exception *exceptionObject) {
  432. bool ret = false;
  433. if (!resultAction ||
  434. !exceptionObject ||
  435. (exceptionClass != ourBaseExceptionClass))
  436. return(ret);
  437. struct OurBaseException_t *excp = (struct OurBaseException_t*)
  438. (((char*) exceptionObject) + ourBaseFromUnwindOffset);
  439. struct OurExceptionType_t *excpType = &(excp->type);
  440. int type = excpType->type;
  441. #ifdef DEBUG
  442. fprintf(stderr,
  443. "handleActionValue(...): exceptionObject = <%p>, "
  444. "excp = <%p>.\n",
  445. (void*)exceptionObject,
  446. (void*)excp);
  447. #endif
  448. const uint8_t *actionPos = (uint8_t*) actionEntry,
  449. *tempActionPos;
  450. int64_t typeOffset = 0,
  451. actionOffset;
  452. for (int i = 0; true; ++i) {
  453. // Each emitted dwarf action corresponds to a 2 tuple of
  454. // type info address offset, and action offset to the next
  455. // emitted action.
  456. typeOffset = readSLEB128(&actionPos);
  457. tempActionPos = actionPos;
  458. actionOffset = readSLEB128(&tempActionPos);
  459. #ifdef DEBUG
  460. fprintf(stderr,
  461. "handleActionValue(...):typeOffset: <%" PRIi64 ">, "
  462. "actionOffset: <%" PRIi64 ">.\n",
  463. typeOffset,
  464. actionOffset);
  465. #endif
  466. assert((typeOffset >= 0) &&
  467. "handleActionValue(...):filters are not supported.");
  468. // Note: A typeOffset == 0 implies that a cleanup llvm.eh.selector
  469. // argument has been matched.
  470. if (typeOffset > 0) {
  471. #ifdef DEBUG
  472. fprintf(stderr,
  473. "handleActionValue(...):actionValue <%d> found.\n",
  474. i);
  475. #endif
  476. unsigned EncSize = getEncodingSize(TTypeEncoding);
  477. const uint8_t *EntryP = ClassInfo - typeOffset * EncSize;
  478. uintptr_t P = readEncodedPointer(&EntryP, TTypeEncoding);
  479. struct OurExceptionType_t *ThisClassInfo =
  480. reinterpret_cast<struct OurExceptionType_t *>(P);
  481. if (ThisClassInfo->type == type) {
  482. *resultAction = i + 1;
  483. ret = true;
  484. break;
  485. }
  486. }
  487. #ifdef DEBUG
  488. fprintf(stderr,
  489. "handleActionValue(...):actionValue not found.\n");
  490. #endif
  491. if (!actionOffset)
  492. break;
  493. actionPos += actionOffset;
  494. }
  495. return(ret);
  496. }
  497. /// Deals with the Language specific data portion of the emitted dwarf code.
  498. /// See @link http://mentorembedded.github.com/cxx-abi/abi-eh.html @unlink
  499. /// @param version unsupported (ignored), unwind version
  500. /// @param lsda language specific data area
  501. /// @param _Unwind_Action actions minimally supported unwind stage
  502. /// (forced specifically not supported)
  503. /// @param exceptionClass exception class (_Unwind_Exception::exception_class)
  504. /// of thrown exception.
  505. /// @param exceptionObject thrown _Unwind_Exception instance.
  506. /// @param context unwind system context
  507. /// @returns minimally supported unwinding control indicator
  508. static _Unwind_Reason_Code handleLsda(int version, const uint8_t *lsda,
  509. _Unwind_Action actions,
  510. _Unwind_Exception_Class exceptionClass,
  511. struct _Unwind_Exception *exceptionObject,
  512. struct _Unwind_Context *context) {
  513. _Unwind_Reason_Code ret = _URC_CONTINUE_UNWIND;
  514. if (!lsda)
  515. return(ret);
  516. #ifdef DEBUG
  517. fprintf(stderr,
  518. "handleLsda(...):lsda is non-zero.\n");
  519. #endif
  520. // Get the current instruction pointer and offset it before next
  521. // instruction in the current frame which threw the exception.
  522. uintptr_t pc = _Unwind_GetIP(context)-1;
  523. // Get beginning current frame's code (as defined by the
  524. // emitted dwarf code)
  525. uintptr_t funcStart = _Unwind_GetRegionStart(context);
  526. uintptr_t pcOffset = pc - funcStart;
  527. const uint8_t *ClassInfo = NULL;
  528. // Note: See JITDwarfEmitter::EmitExceptionTable(...) for corresponding
  529. // dwarf emission
  530. // Parse LSDA header.
  531. uint8_t lpStartEncoding = *lsda++;
  532. if (lpStartEncoding != llvm::dwarf::DW_EH_PE_omit) {
  533. readEncodedPointer(&lsda, lpStartEncoding);
  534. }
  535. uint8_t ttypeEncoding = *lsda++;
  536. uintptr_t classInfoOffset;
  537. if (ttypeEncoding != llvm::dwarf::DW_EH_PE_omit) {
  538. // Calculate type info locations in emitted dwarf code which
  539. // were flagged by type info arguments to llvm.eh.selector
  540. // intrinsic
  541. classInfoOffset = readULEB128(&lsda);
  542. ClassInfo = lsda + classInfoOffset;
  543. }
  544. // Walk call-site table looking for range that
  545. // includes current PC.
  546. uint8_t callSiteEncoding = *lsda++;
  547. uint32_t callSiteTableLength = readULEB128(&lsda);
  548. const uint8_t *callSiteTableStart = lsda;
  549. const uint8_t *callSiteTableEnd = callSiteTableStart +
  550. callSiteTableLength;
  551. const uint8_t *actionTableStart = callSiteTableEnd;
  552. const uint8_t *callSitePtr = callSiteTableStart;
  553. while (callSitePtr < callSiteTableEnd) {
  554. uintptr_t start = readEncodedPointer(&callSitePtr,
  555. callSiteEncoding);
  556. uintptr_t length = readEncodedPointer(&callSitePtr,
  557. callSiteEncoding);
  558. uintptr_t landingPad = readEncodedPointer(&callSitePtr,
  559. callSiteEncoding);
  560. // Note: Action value
  561. uintptr_t actionEntry = readULEB128(&callSitePtr);
  562. if (exceptionClass != ourBaseExceptionClass) {
  563. // We have been notified of a foreign exception being thrown,
  564. // and we therefore need to execute cleanup landing pads
  565. actionEntry = 0;
  566. }
  567. if (landingPad == 0) {
  568. #ifdef DEBUG
  569. fprintf(stderr,
  570. "handleLsda(...): No landing pad found.\n");
  571. #endif
  572. continue; // no landing pad for this entry
  573. }
  574. if (actionEntry) {
  575. actionEntry += ((uintptr_t) actionTableStart) - 1;
  576. }
  577. else {
  578. #ifdef DEBUG
  579. fprintf(stderr,
  580. "handleLsda(...):No action table found.\n");
  581. #endif
  582. }
  583. bool exceptionMatched = false;
  584. if ((start <= pcOffset) && (pcOffset < (start + length))) {
  585. #ifdef DEBUG
  586. fprintf(stderr,
  587. "handleLsda(...): Landing pad found.\n");
  588. #endif
  589. int64_t actionValue = 0;
  590. if (actionEntry) {
  591. exceptionMatched = handleActionValue(&actionValue,
  592. ttypeEncoding,
  593. ClassInfo,
  594. actionEntry,
  595. exceptionClass,
  596. exceptionObject);
  597. }
  598. if (!(actions & _UA_SEARCH_PHASE)) {
  599. #ifdef DEBUG
  600. fprintf(stderr,
  601. "handleLsda(...): installed landing pad "
  602. "context.\n");
  603. #endif
  604. // Found landing pad for the PC.
  605. // Set Instruction Pointer to so we re-enter function
  606. // at landing pad. The landing pad is created by the
  607. // compiler to take two parameters in registers.
  608. _Unwind_SetGR(context,
  609. __builtin_eh_return_data_regno(0),
  610. (uintptr_t)exceptionObject);
  611. // Note: this virtual register directly corresponds
  612. // to the return of the llvm.eh.selector intrinsic
  613. if (!actionEntry || !exceptionMatched) {
  614. // We indicate cleanup only
  615. _Unwind_SetGR(context,
  616. __builtin_eh_return_data_regno(1),
  617. 0);
  618. }
  619. else {
  620. // Matched type info index of llvm.eh.selector intrinsic
  621. // passed here.
  622. _Unwind_SetGR(context,
  623. __builtin_eh_return_data_regno(1),
  624. actionValue);
  625. }
  626. // To execute landing pad set here
  627. _Unwind_SetIP(context, funcStart + landingPad);
  628. ret = _URC_INSTALL_CONTEXT;
  629. }
  630. else if (exceptionMatched) {
  631. #ifdef DEBUG
  632. fprintf(stderr,
  633. "handleLsda(...): setting handler found.\n");
  634. #endif
  635. ret = _URC_HANDLER_FOUND;
  636. }
  637. else {
  638. // Note: Only non-clean up handlers are marked as
  639. // found. Otherwise the clean up handlers will be
  640. // re-found and executed during the clean up
  641. // phase.
  642. #ifdef DEBUG
  643. fprintf(stderr,
  644. "handleLsda(...): cleanup handler found.\n");
  645. #endif
  646. }
  647. break;
  648. }
  649. }
  650. return(ret);
  651. }
  652. /// This is the personality function which is embedded (dwarf emitted), in the
  653. /// dwarf unwind info block. Again see: JITDwarfEmitter.cpp.
  654. /// See @link http://mentorembedded.github.com/cxx-abi/abi-eh.html @unlink
  655. /// @param version unsupported (ignored), unwind version
  656. /// @param _Unwind_Action actions minimally supported unwind stage
  657. /// (forced specifically not supported)
  658. /// @param exceptionClass exception class (_Unwind_Exception::exception_class)
  659. /// of thrown exception.
  660. /// @param exceptionObject thrown _Unwind_Exception instance.
  661. /// @param context unwind system context
  662. /// @returns minimally supported unwinding control indicator
  663. _Unwind_Reason_Code ourPersonality(int version, _Unwind_Action actions,
  664. _Unwind_Exception_Class exceptionClass,
  665. struct _Unwind_Exception *exceptionObject,
  666. struct _Unwind_Context *context) {
  667. #ifdef DEBUG
  668. fprintf(stderr,
  669. "We are in ourPersonality(...):actions is <%d>.\n",
  670. actions);
  671. if (actions & _UA_SEARCH_PHASE) {
  672. fprintf(stderr, "ourPersonality(...):In search phase.\n");
  673. }
  674. else {
  675. fprintf(stderr, "ourPersonality(...):In non-search phase.\n");
  676. }
  677. #endif
  678. const uint8_t *lsda = _Unwind_GetLanguageSpecificData(context);
  679. #ifdef DEBUG
  680. fprintf(stderr,
  681. "ourPersonality(...):lsda = <%p>.\n",
  682. (void*)lsda);
  683. #endif
  684. // The real work of the personality function is captured here
  685. return(handleLsda(version,
  686. lsda,
  687. actions,
  688. exceptionClass,
  689. exceptionObject,
  690. context));
  691. }
  692. /// Generates our _Unwind_Exception class from a given character array.
  693. /// thereby handling arbitrary lengths (not in standard), and handling
  694. /// embedded \0s.
  695. /// See @link http://mentorembedded.github.com/cxx-abi/abi-eh.html @unlink
  696. /// @param classChars char array to encode. NULL values not checkedf
  697. /// @param classCharsSize number of chars in classChars. Value is not checked.
  698. /// @returns class value
  699. uint64_t genClass(const unsigned char classChars[], size_t classCharsSize)
  700. {
  701. uint64_t ret = classChars[0];
  702. for (unsigned i = 1; i < classCharsSize; ++i) {
  703. ret <<= 8;
  704. ret += classChars[i];
  705. }
  706. return(ret);
  707. }
  708. } // extern "C"
  709. //
  710. // Runtime C Library functions End
  711. //
  712. //
  713. // Code generation functions
  714. //
  715. /// Generates code to print given constant string
  716. /// @param context llvm context
  717. /// @param module code for module instance
  718. /// @param builder builder instance
  719. /// @param toPrint string to print
  720. /// @param useGlobal A value of true (default) indicates a GlobalValue is
  721. /// generated, and is used to hold the constant string. A value of
  722. /// false indicates that the constant string will be stored on the
  723. /// stack.
  724. void generateStringPrint(llvm::LLVMContext &context,
  725. llvm::Module &module,
  726. llvm::IRBuilder<> &builder,
  727. std::string toPrint,
  728. bool useGlobal = true) {
  729. llvm::Function *printFunct = module.getFunction("printStr");
  730. llvm::Value *stringVar;
  731. llvm::Constant *stringConstant =
  732. llvm::ConstantDataArray::getString(context, toPrint);
  733. if (useGlobal) {
  734. // Note: Does not work without allocation
  735. stringVar =
  736. new llvm::GlobalVariable(module,
  737. stringConstant->getType(),
  738. true,
  739. llvm::GlobalValue::PrivateLinkage,
  740. stringConstant,
  741. "");
  742. }
  743. else {
  744. stringVar = builder.CreateAlloca(stringConstant->getType());
  745. builder.CreateStore(stringConstant, stringVar);
  746. }
  747. llvm::Value *cast = builder.CreatePointerCast(stringVar,
  748. builder.getInt8PtrTy());
  749. builder.CreateCall(printFunct, cast);
  750. }
  751. /// Generates code to print given runtime integer according to constant
  752. /// string format, and a given print function.
  753. /// @param context llvm context
  754. /// @param module code for module instance
  755. /// @param builder builder instance
  756. /// @param printFunct function used to "print" integer
  757. /// @param toPrint string to print
  758. /// @param format printf like formating string for print
  759. /// @param useGlobal A value of true (default) indicates a GlobalValue is
  760. /// generated, and is used to hold the constant string. A value of
  761. /// false indicates that the constant string will be stored on the
  762. /// stack.
  763. void generateIntegerPrint(llvm::LLVMContext &context,
  764. llvm::Module &module,
  765. llvm::IRBuilder<> &builder,
  766. llvm::Function &printFunct,
  767. llvm::Value &toPrint,
  768. std::string format,
  769. bool useGlobal = true) {
  770. llvm::Constant *stringConstant =
  771. llvm::ConstantDataArray::getString(context, format);
  772. llvm::Value *stringVar;
  773. if (useGlobal) {
  774. // Note: Does not seem to work without allocation
  775. stringVar =
  776. new llvm::GlobalVariable(module,
  777. stringConstant->getType(),
  778. true,
  779. llvm::GlobalValue::PrivateLinkage,
  780. stringConstant,
  781. "");
  782. }
  783. else {
  784. stringVar = builder.CreateAlloca(stringConstant->getType());
  785. builder.CreateStore(stringConstant, stringVar);
  786. }
  787. llvm::Value *cast = builder.CreateBitCast(stringVar,
  788. builder.getInt8PtrTy());
  789. builder.CreateCall(&printFunct, {&toPrint, cast});
  790. }
  791. /// Generates code to handle finally block type semantics: always runs
  792. /// regardless of whether a thrown exception is passing through or the
  793. /// parent function is simply exiting. In addition to printing some state
  794. /// to stderr, this code will resume the exception handling--runs the
  795. /// unwind resume block, if the exception has not been previously caught
  796. /// by a catch clause, and will otherwise execute the end block (terminator
  797. /// block). In addition this function creates the corresponding function's
  798. /// stack storage for the exception pointer and catch flag status.
  799. /// @param context llvm context
  800. /// @param module code for module instance
  801. /// @param builder builder instance
  802. /// @param toAddTo parent function to add block to
  803. /// @param blockName block name of new "finally" block.
  804. /// @param functionId output id used for printing
  805. /// @param terminatorBlock terminator "end" block
  806. /// @param unwindResumeBlock unwind resume block
  807. /// @param exceptionCaughtFlag reference exception caught/thrown status storage
  808. /// @param exceptionStorage reference to exception pointer storage
  809. /// @param caughtResultStorage reference to landingpad result storage
  810. /// @returns newly created block
  811. static llvm::BasicBlock *createFinallyBlock(llvm::LLVMContext &context,
  812. llvm::Module &module,
  813. llvm::IRBuilder<> &builder,
  814. llvm::Function &toAddTo,
  815. std::string &blockName,
  816. std::string &functionId,
  817. llvm::BasicBlock &terminatorBlock,
  818. llvm::BasicBlock &unwindResumeBlock,
  819. llvm::Value **exceptionCaughtFlag,
  820. llvm::Value **exceptionStorage,
  821. llvm::Value **caughtResultStorage) {
  822. assert(exceptionCaughtFlag &&
  823. "ExceptionDemo::createFinallyBlock(...):exceptionCaughtFlag "
  824. "is NULL");
  825. assert(exceptionStorage &&
  826. "ExceptionDemo::createFinallyBlock(...):exceptionStorage "
  827. "is NULL");
  828. assert(caughtResultStorage &&
  829. "ExceptionDemo::createFinallyBlock(...):caughtResultStorage "
  830. "is NULL");
  831. *exceptionCaughtFlag = createEntryBlockAlloca(toAddTo,
  832. "exceptionCaught",
  833. ourExceptionNotThrownState->getType(),
  834. ourExceptionNotThrownState);
  835. llvm::PointerType *exceptionStorageType = builder.getInt8PtrTy();
  836. *exceptionStorage = createEntryBlockAlloca(toAddTo,
  837. "exceptionStorage",
  838. exceptionStorageType,
  839. llvm::ConstantPointerNull::get(
  840. exceptionStorageType));
  841. *caughtResultStorage = createEntryBlockAlloca(toAddTo,
  842. "caughtResultStorage",
  843. ourCaughtResultType,
  844. llvm::ConstantAggregateZero::get(
  845. ourCaughtResultType));
  846. llvm::BasicBlock *ret = llvm::BasicBlock::Create(context,
  847. blockName,
  848. &toAddTo);
  849. builder.SetInsertPoint(ret);
  850. std::ostringstream bufferToPrint;
  851. bufferToPrint << "Gen: Executing finally block "
  852. << blockName << " in " << functionId << "\n";
  853. generateStringPrint(context,
  854. module,
  855. builder,
  856. bufferToPrint.str(),
  857. USE_GLOBAL_STR_CONSTS);
  858. llvm::SwitchInst *theSwitch = builder.CreateSwitch(builder.CreateLoad(
  859. *exceptionCaughtFlag),
  860. &terminatorBlock,
  861. 2);
  862. theSwitch->addCase(ourExceptionCaughtState, &terminatorBlock);
  863. theSwitch->addCase(ourExceptionThrownState, &unwindResumeBlock);
  864. return(ret);
  865. }
  866. /// Generates catch block semantics which print a string to indicate type of
  867. /// catch executed, sets an exception caught flag, and executes passed in
  868. /// end block (terminator block).
  869. /// @param context llvm context
  870. /// @param module code for module instance
  871. /// @param builder builder instance
  872. /// @param toAddTo parent function to add block to
  873. /// @param blockName block name of new "catch" block.
  874. /// @param functionId output id used for printing
  875. /// @param terminatorBlock terminator "end" block
  876. /// @param exceptionCaughtFlag exception caught/thrown status
  877. /// @returns newly created block
  878. static llvm::BasicBlock *createCatchBlock(llvm::LLVMContext &context,
  879. llvm::Module &module,
  880. llvm::IRBuilder<> &builder,
  881. llvm::Function &toAddTo,
  882. std::string &blockName,
  883. std::string &functionId,
  884. llvm::BasicBlock &terminatorBlock,
  885. llvm::Value &exceptionCaughtFlag) {
  886. llvm::BasicBlock *ret = llvm::BasicBlock::Create(context,
  887. blockName,
  888. &toAddTo);
  889. builder.SetInsertPoint(ret);
  890. std::ostringstream bufferToPrint;
  891. bufferToPrint << "Gen: Executing catch block "
  892. << blockName
  893. << " in "
  894. << functionId
  895. << std::endl;
  896. generateStringPrint(context,
  897. module,
  898. builder,
  899. bufferToPrint.str(),
  900. USE_GLOBAL_STR_CONSTS);
  901. builder.CreateStore(ourExceptionCaughtState, &exceptionCaughtFlag);
  902. builder.CreateBr(&terminatorBlock);
  903. return(ret);
  904. }
  905. /// Generates a function which invokes a function (toInvoke) and, whose
  906. /// unwind block will "catch" the type info types correspondingly held in the
  907. /// exceptionTypesToCatch argument. If the toInvoke function throws an
  908. /// exception which does not match any type info types contained in
  909. /// exceptionTypesToCatch, the generated code will call _Unwind_Resume
  910. /// with the raised exception. On the other hand the generated code will
  911. /// normally exit if the toInvoke function does not throw an exception.
  912. /// The generated "finally" block is always run regardless of the cause of
  913. /// the generated function exit.
  914. /// The generated function is returned after being verified.
  915. /// @param module code for module instance
  916. /// @param builder builder instance
  917. /// @param fpm a function pass manager holding optional IR to IR
  918. /// transformations
  919. /// @param toInvoke inner function to invoke
  920. /// @param ourId id used to printing purposes
  921. /// @param numExceptionsToCatch length of exceptionTypesToCatch array
  922. /// @param exceptionTypesToCatch array of type info types to "catch"
  923. /// @returns generated function
  924. static llvm::Function *createCatchWrappedInvokeFunction(
  925. llvm::Module &module, llvm::IRBuilder<> &builder,
  926. llvm::legacy::FunctionPassManager &fpm, llvm::Function &toInvoke,
  927. std::string ourId, unsigned numExceptionsToCatch,
  928. unsigned exceptionTypesToCatch[]) {
  929. llvm::LLVMContext &context = module.getContext();
  930. llvm::Function *toPrint32Int = module.getFunction("print32Int");
  931. ArgTypes argTypes;
  932. argTypes.push_back(builder.getInt32Ty());
  933. ArgNames argNames;
  934. argNames.push_back("exceptTypeToThrow");
  935. llvm::Function *ret = createFunction(module,
  936. builder.getVoidTy(),
  937. argTypes,
  938. argNames,
  939. ourId,
  940. llvm::Function::ExternalLinkage,
  941. false,
  942. false);
  943. // Block which calls invoke
  944. llvm::BasicBlock *entryBlock = llvm::BasicBlock::Create(context,
  945. "entry",
  946. ret);
  947. // Normal block for invoke
  948. llvm::BasicBlock *normalBlock = llvm::BasicBlock::Create(context,
  949. "normal",
  950. ret);
  951. // Unwind block for invoke
  952. llvm::BasicBlock *exceptionBlock = llvm::BasicBlock::Create(context,
  953. "exception",
  954. ret);
  955. // Block which routes exception to correct catch handler block
  956. llvm::BasicBlock *exceptionRouteBlock = llvm::BasicBlock::Create(context,
  957. "exceptionRoute",
  958. ret);
  959. // Foreign exception handler
  960. llvm::BasicBlock *externalExceptionBlock = llvm::BasicBlock::Create(context,
  961. "externalException",
  962. ret);
  963. // Block which calls _Unwind_Resume
  964. llvm::BasicBlock *unwindResumeBlock = llvm::BasicBlock::Create(context,
  965. "unwindResume",
  966. ret);
  967. // Clean up block which delete exception if needed
  968. llvm::BasicBlock *endBlock = llvm::BasicBlock::Create(context, "end", ret);
  969. std::string nextName;
  970. std::vector<llvm::BasicBlock*> catchBlocks(numExceptionsToCatch);
  971. llvm::Value *exceptionCaughtFlag = NULL;
  972. llvm::Value *exceptionStorage = NULL;
  973. llvm::Value *caughtResultStorage = NULL;
  974. // Finally block which will branch to unwindResumeBlock if
  975. // exception is not caught. Initializes/allocates stack locations.
  976. llvm::BasicBlock *finallyBlock = createFinallyBlock(context,
  977. module,
  978. builder,
  979. *ret,
  980. nextName = "finally",
  981. ourId,
  982. *endBlock,
  983. *unwindResumeBlock,
  984. &exceptionCaughtFlag,
  985. &exceptionStorage,
  986. &caughtResultStorage
  987. );
  988. for (unsigned i = 0; i < numExceptionsToCatch; ++i) {
  989. nextName = ourTypeInfoNames[exceptionTypesToCatch[i]];
  990. // One catch block per type info to be caught
  991. catchBlocks[i] = createCatchBlock(context,
  992. module,
  993. builder,
  994. *ret,
  995. nextName,
  996. ourId,
  997. *finallyBlock,
  998. *exceptionCaughtFlag);
  999. }
  1000. // Entry Block
  1001. builder.SetInsertPoint(entryBlock);
  1002. std::vector<llvm::Value*> args;
  1003. args.push_back(namedValues["exceptTypeToThrow"]);
  1004. builder.CreateInvoke(&toInvoke,
  1005. normalBlock,
  1006. exceptionBlock,
  1007. args);
  1008. // End Block
  1009. builder.SetInsertPoint(endBlock);
  1010. generateStringPrint(context,
  1011. module,
  1012. builder,
  1013. "Gen: In end block: exiting in " + ourId + ".\n",
  1014. USE_GLOBAL_STR_CONSTS);
  1015. llvm::Function *deleteOurException = module.getFunction("deleteOurException");
  1016. // Note: function handles NULL exceptions
  1017. builder.CreateCall(deleteOurException,
  1018. builder.CreateLoad(exceptionStorage));
  1019. builder.CreateRetVoid();
  1020. // Normal Block
  1021. builder.SetInsertPoint(normalBlock);
  1022. generateStringPrint(context,
  1023. module,
  1024. builder,
  1025. "Gen: No exception in " + ourId + "!\n",
  1026. USE_GLOBAL_STR_CONSTS);
  1027. // Finally block is always called
  1028. builder.CreateBr(finallyBlock);
  1029. // Unwind Resume Block
  1030. builder.SetInsertPoint(unwindResumeBlock);
  1031. builder.CreateResume(builder.CreateLoad(caughtResultStorage));
  1032. // Exception Block
  1033. builder.SetInsertPoint(exceptionBlock);
  1034. llvm::Function *personality = module.getFunction("ourPersonality");
  1035. ret->setPersonalityFn(personality);
  1036. llvm::LandingPadInst *caughtResult =
  1037. builder.CreateLandingPad(ourCaughtResultType,
  1038. numExceptionsToCatch,
  1039. "landingPad");
  1040. caughtResult->setCleanup(true);
  1041. for (unsigned i = 0; i < numExceptionsToCatch; ++i) {
  1042. // Set up type infos to be caught
  1043. caughtResult->addClause(module.getGlobalVariable(
  1044. ourTypeInfoNames[exceptionTypesToCatch[i]]));
  1045. }
  1046. llvm::Value *unwindException = builder.CreateExtractValue(caughtResult, 0);
  1047. llvm::Value *retTypeInfoIndex = builder.CreateExtractValue(caughtResult, 1);
  1048. // FIXME: Redundant storage which, beyond utilizing value of
  1049. // caughtResultStore for unwindException storage, may be alleviated
  1050. // altogether with a block rearrangement
  1051. builder.CreateStore(caughtResult, caughtResultStorage);
  1052. builder.CreateStore(unwindException, exceptionStorage);
  1053. builder.CreateStore(ourExceptionThrownState, exceptionCaughtFlag);
  1054. // Retrieve exception_class member from thrown exception
  1055. // (_Unwind_Exception instance). This member tells us whether or not
  1056. // the exception is foreign.
  1057. llvm::Value *unwindExceptionClass =
  1058. builder.CreateLoad(builder.CreateStructGEP(
  1059. ourUnwindExceptionType,
  1060. builder.CreatePointerCast(unwindException,
  1061. ourUnwindExceptionType->getPointerTo()),
  1062. 0));
  1063. // Branch to the externalExceptionBlock if the exception is foreign or
  1064. // to a catch router if not. Either way the finally block will be run.
  1065. builder.CreateCondBr(builder.CreateICmpEQ(unwindExceptionClass,
  1066. llvm::ConstantInt::get(builder.getInt64Ty(),
  1067. ourBaseExceptionClass)),
  1068. exceptionRouteBlock,
  1069. externalExceptionBlock);
  1070. // External Exception Block
  1071. builder.SetInsertPoint(externalExceptionBlock);
  1072. generateStringPrint(context,
  1073. module,
  1074. builder,
  1075. "Gen: Foreign exception received.\n",
  1076. USE_GLOBAL_STR_CONSTS);
  1077. // Branch to the finally block
  1078. builder.CreateBr(finallyBlock);
  1079. // Exception Route Block
  1080. builder.SetInsertPoint(exceptionRouteBlock);
  1081. // Casts exception pointer (_Unwind_Exception instance) to parent
  1082. // (OurException instance).
  1083. //
  1084. // Note: ourBaseFromUnwindOffset is usually negative
  1085. llvm::Value *typeInfoThrown = builder.CreatePointerCast(
  1086. builder.CreateConstGEP1_64(unwindException,
  1087. ourBaseFromUnwindOffset),
  1088. ourExceptionType->getPointerTo());
  1089. // Retrieve thrown exception type info type
  1090. //
  1091. // Note: Index is not relative to pointer but instead to structure
  1092. // unlike a true getelementptr (GEP) instruction
  1093. typeInfoThrown = builder.CreateStructGEP(ourExceptionType, typeInfoThrown, 0);
  1094. llvm::Value *typeInfoThrownType =
  1095. builder.CreateStructGEP(builder.getInt8PtrTy(), typeInfoThrown, 0);
  1096. generateIntegerPrint(context,
  1097. module,
  1098. builder,
  1099. *toPrint32Int,
  1100. *(builder.CreateLoad(typeInfoThrownType)),
  1101. "Gen: Exception type <%d> received (stack unwound) "
  1102. " in " +
  1103. ourId +
  1104. ".\n",
  1105. USE_GLOBAL_STR_CONSTS);
  1106. // Route to matched type info catch block or run cleanup finally block
  1107. llvm::SwitchInst *switchToCatchBlock = builder.CreateSwitch(retTypeInfoIndex,
  1108. finallyBlock,
  1109. numExceptionsToCatch);
  1110. unsigned nextTypeToCatch;
  1111. for (unsigned i = 1; i <= numExceptionsToCatch; ++i) {
  1112. nextTypeToCatch = i - 1;
  1113. switchToCatchBlock->addCase(llvm::ConstantInt::get(
  1114. llvm::Type::getInt32Ty(context), i),
  1115. catchBlocks[nextTypeToCatch]);
  1116. }
  1117. llvm::verifyFunction(*ret);
  1118. fpm.run(*ret);
  1119. return(ret);
  1120. }
  1121. /// Generates function which throws either an exception matched to a runtime
  1122. /// determined type info type (argument to generated function), or if this
  1123. /// runtime value matches nativeThrowType, throws a foreign exception by
  1124. /// calling nativeThrowFunct.
  1125. /// @param module code for module instance
  1126. /// @param builder builder instance
  1127. /// @param fpm a function pass manager holding optional IR to IR
  1128. /// transformations
  1129. /// @param ourId id used to printing purposes
  1130. /// @param nativeThrowType a runtime argument of this value results in
  1131. /// nativeThrowFunct being called to generate/throw exception.
  1132. /// @param nativeThrowFunct function which will throw a foreign exception
  1133. /// if the above nativeThrowType matches generated function's arg.
  1134. /// @returns generated function
  1135. static llvm::Function *
  1136. createThrowExceptionFunction(llvm::Module &module, llvm::IRBuilder<> &builder,
  1137. llvm::legacy::FunctionPassManager &fpm,
  1138. std::string ourId, int32_t nativeThrowType,
  1139. llvm::Function &nativeThrowFunct) {
  1140. llvm::LLVMContext &context = module.getContext();
  1141. namedValues.clear();
  1142. ArgTypes unwindArgTypes;
  1143. unwindArgTypes.push_back(builder.getInt32Ty());
  1144. ArgNames unwindArgNames;
  1145. unwindArgNames.push_back("exceptTypeToThrow");
  1146. llvm::Function *ret = createFunction(module,
  1147. builder.getVoidTy(),
  1148. unwindArgTypes,
  1149. unwindArgNames,
  1150. ourId,
  1151. llvm::Function::ExternalLinkage,
  1152. false,
  1153. false);
  1154. // Throws either one of our exception or a native C++ exception depending
  1155. // on a runtime argument value containing a type info type.
  1156. llvm::BasicBlock *entryBlock = llvm::BasicBlock::Create(context,
  1157. "entry",
  1158. ret);
  1159. // Throws a foreign exception
  1160. llvm::BasicBlock *nativeThrowBlock = llvm::BasicBlock::Create(context,
  1161. "nativeThrow",
  1162. ret);
  1163. // Throws one of our Exceptions
  1164. llvm::BasicBlock *generatedThrowBlock = llvm::BasicBlock::Create(context,
  1165. "generatedThrow",
  1166. ret);
  1167. // Retrieved runtime type info type to throw
  1168. llvm::Value *exceptionType = namedValues["exceptTypeToThrow"];
  1169. // nativeThrowBlock block
  1170. builder.SetInsertPoint(nativeThrowBlock);
  1171. // Throws foreign exception
  1172. builder.CreateCall(&nativeThrowFunct, exceptionType);
  1173. builder.CreateUnreachable();
  1174. // entry block
  1175. builder.SetInsertPoint(entryBlock);
  1176. llvm::Function *toPrint32Int = module.getFunction("print32Int");
  1177. generateIntegerPrint(context,
  1178. module,
  1179. builder,
  1180. *toPrint32Int,
  1181. *exceptionType,
  1182. "\nGen: About to throw exception type <%d> in " +
  1183. ourId +
  1184. ".\n",
  1185. USE_GLOBAL_STR_CONSTS);
  1186. // Switches on runtime type info type value to determine whether or not
  1187. // a foreign exception is thrown. Defaults to throwing one of our
  1188. // generated exceptions.
  1189. llvm::SwitchInst *theSwitch = builder.CreateSwitch(exceptionType,
  1190. generatedThrowBlock,
  1191. 1);
  1192. theSwitch->addCase(llvm::ConstantInt::get(llvm::Type::getInt32Ty(context),
  1193. nativeThrowType),
  1194. nativeThrowBlock);
  1195. // generatedThrow block
  1196. builder.SetInsertPoint(generatedThrowBlock);
  1197. llvm::Function *createOurException = module.getFunction("createOurException");
  1198. llvm::Function *raiseOurException = module.getFunction(
  1199. "_Unwind_RaiseException");
  1200. // Creates exception to throw with runtime type info type.
  1201. llvm::Value *exception = builder.CreateCall(createOurException,
  1202. namedValues["exceptTypeToThrow"]);
  1203. // Throw generated Exception
  1204. builder.CreateCall(raiseOurException, exception);
  1205. builder.CreateUnreachable();
  1206. llvm::verifyFunction(*ret);
  1207. fpm.run(*ret);
  1208. return(ret);
  1209. }
  1210. static void createStandardUtilityFunctions(unsigned numTypeInfos,
  1211. llvm::Module &module,
  1212. llvm::IRBuilder<> &builder);
  1213. /// Creates test code by generating and organizing these functions into the
  1214. /// test case. The test case consists of an outer function setup to invoke
  1215. /// an inner function within an environment having multiple catch and single
  1216. /// finally blocks. This inner function is also setup to invoke a throw
  1217. /// function within an evironment similar in nature to the outer function's
  1218. /// catch and finally blocks. Each of these two functions catch mutually
  1219. /// exclusive subsets (even or odd) of the type info types configured
  1220. /// for this this. All generated functions have a runtime argument which
  1221. /// holds a type info type to throw that each function takes and passes it
  1222. /// to the inner one if such a inner function exists. This type info type is
  1223. /// looked at by the generated throw function to see whether or not it should
  1224. /// throw a generated exception with the same type info type, or instead call
  1225. /// a supplied a function which in turn will throw a foreign exception.
  1226. /// @param module code for module instance
  1227. /// @param builder builder instance
  1228. /// @param fpm a function pass manager holding optional IR to IR
  1229. /// transformations
  1230. /// @param nativeThrowFunctName name of external function which will throw
  1231. /// a foreign exception
  1232. /// @returns outermost generated test function.
  1233. llvm::Function *
  1234. createUnwindExceptionTest(llvm::Module &module, llvm::IRBuilder<> &builder,
  1235. llvm::legacy::FunctionPassManager &fpm,
  1236. std::string nativeThrowFunctName) {
  1237. // Number of type infos to generate
  1238. unsigned numTypeInfos = 6;
  1239. // Initialze intrisics and external functions to use along with exception
  1240. // and type info globals.
  1241. createStandardUtilityFunctions(numTypeInfos,
  1242. module,
  1243. builder);
  1244. llvm::Function *nativeThrowFunct = module.getFunction(nativeThrowFunctName);
  1245. // Create exception throw function using the value ~0 to cause
  1246. // foreign exceptions to be thrown.
  1247. llvm::Function *throwFunct = createThrowExceptionFunction(module,
  1248. builder,
  1249. fpm,
  1250. "throwFunct",
  1251. ~0,
  1252. *nativeThrowFunct);
  1253. // Inner function will catch even type infos
  1254. unsigned innerExceptionTypesToCatch[] = {6, 2, 4};
  1255. size_t numExceptionTypesToCatch = sizeof(innerExceptionTypesToCatch) /
  1256. sizeof(unsigned);
  1257. // Generate inner function.
  1258. llvm::Function *innerCatchFunct = createCatchWrappedInvokeFunction(module,
  1259. builder,
  1260. fpm,
  1261. *throwFunct,
  1262. "innerCatchFunct",
  1263. numExceptionTypesToCatch,
  1264. innerExceptionTypesToCatch);
  1265. // Outer function will catch odd type infos
  1266. unsigned outerExceptionTypesToCatch[] = {3, 1, 5};
  1267. numExceptionTypesToCatch = sizeof(outerExceptionTypesToCatch) /
  1268. sizeof(unsigned);
  1269. // Generate outer function
  1270. llvm::Function *outerCatchFunct = createCatchWrappedInvokeFunction(module,
  1271. builder,
  1272. fpm,
  1273. *innerCatchFunct,
  1274. "outerCatchFunct",
  1275. numExceptionTypesToCatch,
  1276. outerExceptionTypesToCatch);
  1277. // Return outer function to run
  1278. return(outerCatchFunct);
  1279. }
  1280. namespace {
  1281. /// Represents our foreign exceptions
  1282. class OurCppRunException : public std::runtime_error {
  1283. public:
  1284. OurCppRunException(const std::string reason) :
  1285. std::runtime_error(reason) {}
  1286. OurCppRunException (const OurCppRunException &toCopy) :
  1287. std::runtime_error(toCopy) {}
  1288. OurCppRunException &operator = (const OurCppRunException &toCopy) {
  1289. return(reinterpret_cast<OurCppRunException&>(
  1290. std::runtime_error::operator=(toCopy)));
  1291. }
  1292. ~OurCppRunException(void) throw() override {}
  1293. };
  1294. } // end anonymous namespace
  1295. /// Throws foreign C++ exception.
  1296. /// @param ignoreIt unused parameter that allows function to match implied
  1297. /// generated function contract.
  1298. extern "C"
  1299. void throwCppException (int32_t ignoreIt) {
  1300. throw(OurCppRunException("thrown by throwCppException(...)"));
  1301. }
  1302. typedef void (*OurExceptionThrowFunctType) (int32_t typeToThrow);
  1303. /// This is a test harness which runs test by executing generated
  1304. /// function with a type info type to throw. Harness wraps the execution
  1305. /// of generated function in a C++ try catch clause.
  1306. /// @param engine execution engine to use for executing generated function.
  1307. /// This demo program expects this to be a JIT instance for demo
  1308. /// purposes.
  1309. /// @param function generated test function to run
  1310. /// @param typeToThrow type info type of generated exception to throw, or
  1311. /// indicator to cause foreign exception to be thrown.
  1312. static
  1313. void runExceptionThrow(llvm::ExecutionEngine *engine,
  1314. llvm::Function *function,
  1315. int32_t typeToThrow) {
  1316. // Find test's function pointer
  1317. OurExceptionThrowFunctType functPtr =
  1318. reinterpret_cast<OurExceptionThrowFunctType>(
  1319. reinterpret_cast<intptr_t>(engine->getPointerToFunction(function)));
  1320. try {
  1321. // Run test
  1322. (*functPtr)(typeToThrow);
  1323. }
  1324. catch (OurCppRunException exc) {
  1325. // Catch foreign C++ exception
  1326. fprintf(stderr,
  1327. "\nrunExceptionThrow(...):In C++ catch OurCppRunException "
  1328. "with reason: %s.\n",
  1329. exc.what());
  1330. }
  1331. catch (...) {
  1332. // Catch all exceptions including our generated ones. This latter
  1333. // functionality works according to the example in rules 1.6.4 of
  1334. // http://mentorembedded.github.com/cxx-abi/abi-eh.html (v1.22),
  1335. // given that these will be exceptions foreign to C++
  1336. // (the _Unwind_Exception::exception_class should be different from
  1337. // the one used by C++).
  1338. fprintf(stderr,
  1339. "\nrunExceptionThrow(...):In C++ catch all.\n");
  1340. }
  1341. }
  1342. //
  1343. // End test functions
  1344. //
  1345. typedef llvm::ArrayRef<llvm::Type*> TypeArray;
  1346. /// This initialization routine creates type info globals and
  1347. /// adds external function declarations to module.
  1348. /// @param numTypeInfos number of linear type info associated type info types
  1349. /// to create as GlobalVariable instances, starting with the value 1.
  1350. /// @param module code for module instance
  1351. /// @param builder builder instance
  1352. static void createStandardUtilityFunctions(unsigned numTypeInfos,
  1353. llvm::Module &module,
  1354. llvm::IRBuilder<> &builder) {
  1355. llvm::LLVMContext &context = module.getContext();
  1356. // Exception initializations
  1357. // Setup exception catch state
  1358. ourExceptionNotThrownState =
  1359. llvm::ConstantInt::get(llvm::Type::getInt8Ty(context), 0),
  1360. ourExceptionThrownState =
  1361. llvm::ConstantInt::get(llvm::Type::getInt8Ty(context), 1),
  1362. ourExceptionCaughtState =
  1363. llvm::ConstantInt::get(llvm::Type::getInt8Ty(context), 2),
  1364. // Create our type info type
  1365. ourTypeInfoType = llvm::StructType::get(context,
  1366. TypeArray(builder.getInt32Ty()));
  1367. llvm::Type *caughtResultFieldTypes[] = {
  1368. builder.getInt8PtrTy(),
  1369. builder.getInt32Ty()
  1370. };
  1371. // Create our landingpad result type
  1372. ourCaughtResultType = llvm::StructType::get(context,
  1373. TypeArray(caughtResultFieldTypes));
  1374. // Create OurException type
  1375. ourExceptionType = llvm::StructType::get(context,
  1376. TypeArray(ourTypeInfoType));
  1377. // Create portion of _Unwind_Exception type
  1378. //
  1379. // Note: Declaring only a portion of the _Unwind_Exception struct.
  1380. // Does this cause problems?
  1381. ourUnwindExceptionType =
  1382. llvm::StructType::get(context,
  1383. TypeArray(builder.getInt64Ty()));
  1384. struct OurBaseException_t dummyException;
  1385. // Calculate offset of OurException::unwindException member.
  1386. ourBaseFromUnwindOffset = ((uintptr_t) &dummyException) -
  1387. ((uintptr_t) &(dummyException.unwindException));
  1388. #ifdef DEBUG
  1389. fprintf(stderr,
  1390. "createStandardUtilityFunctions(...):ourBaseFromUnwindOffset "
  1391. "= %" PRIi64 ", sizeof(struct OurBaseException_t) - "
  1392. "sizeof(struct _Unwind_Exception) = %lu.\n",
  1393. ourBaseFromUnwindOffset,
  1394. sizeof(struct OurBaseException_t) -
  1395. sizeof(struct _Unwind_Exception));
  1396. #endif
  1397. size_t numChars = sizeof(ourBaseExcpClassChars) / sizeof(char);
  1398. // Create our _Unwind_Exception::exception_class value
  1399. ourBaseExceptionClass = genClass(ourBaseExcpClassChars, numChars);
  1400. // Type infos
  1401. std::string baseStr = "typeInfo", typeInfoName;
  1402. std::ostringstream typeInfoNameBuilder;
  1403. std::vector<llvm::Constant*> structVals;
  1404. llvm::Constant *nextStruct;
  1405. // Generate each type info
  1406. //
  1407. // Note: First type info is not used.
  1408. for (unsigned i = 0; i <= numTypeInfos; ++i) {
  1409. structVals.clear();
  1410. structVals.push_back(llvm::ConstantInt::get(builder.getInt32Ty(), i));
  1411. nextStruct = llvm::ConstantStruct::get(ourTypeInfoType, structVals);
  1412. typeInfoNameBuilder.str("");
  1413. typeInfoNameBuilder << baseStr << i;
  1414. typeInfoName = typeInfoNameBuilder.str();
  1415. // Note: Does not seem to work without allocation
  1416. new llvm::GlobalVariable(module,
  1417. ourTypeInfoType,
  1418. true,
  1419. llvm::GlobalValue::ExternalLinkage,
  1420. nextStruct,
  1421. typeInfoName);
  1422. ourTypeInfoNames.push_back(typeInfoName);
  1423. ourTypeInfoNamesIndex[i] = typeInfoName;
  1424. }
  1425. ArgNames argNames;
  1426. ArgTypes argTypes;
  1427. llvm::Function *funct = NULL;
  1428. // print32Int
  1429. llvm::Type *retType = builder.getVoidTy();
  1430. argTypes.clear();
  1431. argTypes.push_back(builder.getInt32Ty());
  1432. argTypes.push_back(builder.getInt8PtrTy());
  1433. argNames.clear();
  1434. createFunction(module,
  1435. retType,
  1436. argTypes,
  1437. argNames,
  1438. "print32Int",
  1439. llvm::Function::ExternalLinkage,
  1440. true,
  1441. false);
  1442. // print64Int
  1443. retType = builder.getVoidTy();
  1444. argTypes.clear();
  1445. argTypes.push_back(builder.getInt64Ty());
  1446. argTypes.push_back(builder.getInt8PtrTy());
  1447. argNames.clear();
  1448. createFunction(module,
  1449. retType,
  1450. argTypes,
  1451. argNames,
  1452. "print64Int",
  1453. llvm::Function::ExternalLinkage,
  1454. true,
  1455. false);
  1456. // printStr
  1457. retType = builder.getVoidTy();
  1458. argTypes.clear();
  1459. argTypes.push_back(builder.getInt8PtrTy());
  1460. argNames.clear();
  1461. createFunction(module,
  1462. retType,
  1463. argTypes,
  1464. argNames,
  1465. "printStr",
  1466. llvm::Function::ExternalLinkage,
  1467. true,
  1468. false);
  1469. // throwCppException
  1470. retType = builder.getVoidTy();
  1471. argTypes.clear();
  1472. argTypes.push_back(builder.getInt32Ty());
  1473. argNames.clear();
  1474. createFunction(module,
  1475. retType,
  1476. argTypes,
  1477. argNames,
  1478. "throwCppException",
  1479. llvm::Function::ExternalLinkage,
  1480. true,
  1481. false);
  1482. // deleteOurException
  1483. retType = builder.getVoidTy();
  1484. argTypes.clear();
  1485. argTypes.push_back(builder.getInt8PtrTy());
  1486. argNames.clear();
  1487. createFunction(module,
  1488. retType,
  1489. argTypes,
  1490. argNames,
  1491. "deleteOurException",
  1492. llvm::Function::ExternalLinkage,
  1493. true,
  1494. false);
  1495. // createOurException
  1496. retType = builder.getInt8PtrTy();
  1497. argTypes.clear();
  1498. argTypes.push_back(builder.getInt32Ty());
  1499. argNames.clear();
  1500. createFunction(module,
  1501. retType,
  1502. argTypes,
  1503. argNames,
  1504. "createOurException",
  1505. llvm::Function::ExternalLinkage,
  1506. true,
  1507. false);
  1508. // _Unwind_RaiseException
  1509. retType = builder.getInt32Ty();
  1510. argTypes.clear();
  1511. argTypes.push_back(builder.getInt8PtrTy());
  1512. argNames.clear();
  1513. funct = createFunction(module,
  1514. retType,
  1515. argTypes,
  1516. argNames,
  1517. "_Unwind_RaiseException",
  1518. llvm::Function::ExternalLinkage,
  1519. true,
  1520. false);
  1521. funct->setDoesNotReturn();
  1522. // _Unwind_Resume
  1523. retType = builder.getInt32Ty();
  1524. argTypes.clear();
  1525. argTypes.push_back(builder.getInt8PtrTy());
  1526. argNames.clear();
  1527. funct = createFunction(module,
  1528. retType,
  1529. argTypes,
  1530. argNames,
  1531. "_Unwind_Resume",
  1532. llvm::Function::ExternalLinkage,
  1533. true,
  1534. false);
  1535. funct->setDoesNotReturn();
  1536. // ourPersonality
  1537. retType = builder.getInt32Ty();
  1538. argTypes.clear();
  1539. argTypes.push_back(builder.getInt32Ty());
  1540. argTypes.push_back(builder.getInt32Ty());
  1541. argTypes.push_back(builder.getInt64Ty());
  1542. argTypes.push_back(builder.getInt8PtrTy());
  1543. argTypes.push_back(builder.getInt8PtrTy());
  1544. argNames.clear();
  1545. createFunction(module,
  1546. retType,
  1547. argTypes,
  1548. argNames,
  1549. "ourPersonality",
  1550. llvm::Function::ExternalLinkage,
  1551. true,
  1552. false);
  1553. // llvm.eh.typeid.for intrinsic
  1554. getDeclaration(&module, llvm::Intrinsic::eh_typeid_for);
  1555. }
  1556. //===----------------------------------------------------------------------===//
  1557. // Main test driver code.
  1558. //===----------------------------------------------------------------------===//
  1559. /// Demo main routine which takes the type info types to throw. A test will
  1560. /// be run for each given type info type. While type info types with the value
  1561. /// of -1 will trigger a foreign C++ exception to be thrown; type info types
  1562. /// <= 6 and >= 1 will be caught by test functions; and type info types > 6
  1563. /// will result in exceptions which pass through to the test harness. All other
  1564. /// type info types are not supported and could cause a crash.
  1565. int main(int argc, char *argv[]) {
  1566. if (argc == 1) {
  1567. fprintf(stderr,
  1568. "\nUsage: ExceptionDemo <exception type to throw> "
  1569. "[<type 2>...<type n>].\n"
  1570. " Each type must have the value of 1 - 6 for "
  1571. "generated exceptions to be caught;\n"
  1572. " the value -1 for foreign C++ exceptions to be "
  1573. "generated and thrown;\n"
  1574. " or the values > 6 for exceptions to be ignored.\n"
  1575. "\nTry: ExceptionDemo 2 3 7 -1\n"
  1576. " for a full test.\n\n");
  1577. return(0);
  1578. }
  1579. // If not set, exception handling will not be turned on
  1580. llvm::TargetOptions Opts;
  1581. llvm::InitializeNativeTarget();
  1582. llvm::InitializeNativeTargetAsmPrinter();
  1583. llvm::LLVMContext Context;
  1584. llvm::IRBuilder<> theBuilder(Context);
  1585. // Make the module, which holds all the code.
  1586. std::unique_ptr<llvm::Module> Owner =
  1587. llvm::make_unique<llvm::Module>("my cool jit", Context);
  1588. llvm::Module *module = Owner.get();
  1589. std::unique_ptr<llvm::RTDyldMemoryManager> MemMgr(new llvm::SectionMemoryManager());
  1590. // Build engine with JIT
  1591. llvm::EngineBuilder factory(std::move(Owner));
  1592. factory.setEngineKind(llvm::EngineKind::JIT);
  1593. factory.setTargetOptions(Opts);
  1594. factory.setMCJITMemoryManager(std::move(MemMgr));
  1595. llvm::ExecutionEngine *executionEngine = factory.create();
  1596. {
  1597. llvm::legacy::FunctionPassManager fpm(module);
  1598. // Set up the optimizer pipeline.
  1599. // Start with registering info about how the
  1600. // target lays out data structures.
  1601. module->setDataLayout(executionEngine->getDataLayout());
  1602. // Optimizations turned on
  1603. #ifdef ADD_OPT_PASSES
  1604. // Basic AliasAnslysis support for GVN.
  1605. fpm.add(llvm::createBasicAliasAnalysisPass());
  1606. // Promote allocas to registers.
  1607. fpm.add(llvm::createPromoteMemoryToRegisterPass());
  1608. // Do simple "peephole" optimizations and bit-twiddling optzns.
  1609. fpm.add(llvm::createInstructionCombiningPass());
  1610. // Reassociate expressions.
  1611. fpm.add(llvm::createReassociatePass());
  1612. // Eliminate Common SubExpressions.
  1613. fpm.add(llvm::createGVNPass());
  1614. // Simplify the control flow graph (deleting unreachable
  1615. // blocks, etc).
  1616. fpm.add(llvm::createCFGSimplificationPass());
  1617. #endif // ADD_OPT_PASSES
  1618. fpm.doInitialization();
  1619. // Generate test code using function throwCppException(...) as
  1620. // the function which throws foreign exceptions.
  1621. llvm::Function *toRun =
  1622. createUnwindExceptionTest(*module,
  1623. theBuilder,
  1624. fpm,
  1625. "throwCppException");
  1626. executionEngine->finalizeObject();
  1627. fprintf(stderr, "\nBegin module dump:\n\n");
  1628. module->dump();
  1629. fprintf(stderr, "\nEnd module dump:\n");
  1630. fprintf(stderr, "\n\nBegin Test:\n");
  1631. for (int i = 1; i < argc; ++i) {
  1632. // Run test for each argument whose value is the exception
  1633. // type to throw.
  1634. runExceptionThrow(executionEngine,
  1635. toRun,
  1636. (unsigned) strtoul(argv[i], NULL, 10));
  1637. }
  1638. fprintf(stderr, "\nEnd Test:\n\n");
  1639. }
  1640. delete executionEngine;
  1641. return 0;
  1642. }