1 //===-- ExceptionDemo.cpp - An example using llvm Exceptions --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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
27 // causes a usage to be printed to stderr
29 // ExceptionDemo 2 3 7 -1
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
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://sourcery.mentor.com/public/cxx-abi/abi-eh.html (v1.22)
46 // This code uses code from the llvm compiler-rt project and the llvm
47 // Kaleidoscope project.
49 //===----------------------------------------------------------------------===//
51 #include "llvm/LLVMContext.h"
52 #include "llvm/DerivedTypes.h"
53 #include "llvm/ExecutionEngine/ExecutionEngine.h"
54 #include "llvm/ExecutionEngine/JIT.h"
55 #include "llvm/Module.h"
56 #include "llvm/PassManager.h"
57 #include "llvm/Intrinsics.h"
58 #include "llvm/Analysis/Verifier.h"
59 #include "llvm/Target/TargetData.h"
60 #include "llvm/Target/TargetOptions.h"
61 #include "llvm/Transforms/Scalar.h"
62 #include "llvm/Support/IRBuilder.h"
63 #include "llvm/Support/Dwarf.h"
64 #include "llvm/Support/TargetSelect.h"
67 // See use of UpgradeExceptionHandling(...) below
68 #include "llvm/AutoUpgrade.h"
71 // FIXME: Although all systems tested with (Linux, OS X), do not need this
72 // header file included. A user on ubuntu reported, undefined symbols
73 // for stderr, and fprintf, and the addition of this include fixed the
74 // issue for them. Given that LLVM's best practices include the goal
75 // of reducing the number of redundant header files included, the
76 // correct solution would be to find out why these symbols are not
77 // defined for the system in question, and fix the issue by finding out
78 // which LLVM header file, if any, would include these symbols.
85 #ifndef USE_GLOBAL_STR_CONSTS
86 #define USE_GLOBAL_STR_CONSTS true
89 // System C++ ABI unwind types from:
90 // http://sourcery.mentor.com/public/cxx-abi/abi-eh.html (v1.22)
96 _URC_FOREIGN_EXCEPTION_CAUGHT = 1,
97 _URC_FATAL_PHASE2_ERROR = 2,
98 _URC_FATAL_PHASE1_ERROR = 3,
100 _URC_END_OF_STACK = 5,
101 _URC_HANDLER_FOUND = 6,
102 _URC_INSTALL_CONTEXT = 7,
103 _URC_CONTINUE_UNWIND = 8
104 } _Unwind_Reason_Code;
107 _UA_SEARCH_PHASE = 1,
108 _UA_CLEANUP_PHASE = 2,
109 _UA_HANDLER_FRAME = 4,
110 _UA_FORCE_UNWIND = 8,
111 _UA_END_OF_STACK = 16
114 struct _Unwind_Exception;
116 typedef void (*_Unwind_Exception_Cleanup_Fn) (_Unwind_Reason_Code,
117 struct _Unwind_Exception *);
119 struct _Unwind_Exception {
120 uint64_t exception_class;
121 _Unwind_Exception_Cleanup_Fn exception_cleanup;
126 // @@@ The IA-64 ABI says that this structure must be double-word aligned.
127 // Taking that literally does not make much sense generically. Instead
128 // we provide the maximum alignment required by any type for the machine.
129 } __attribute__((__aligned__));
131 struct _Unwind_Context;
132 typedef struct _Unwind_Context *_Unwind_Context_t;
134 extern const uint8_t *_Unwind_GetLanguageSpecificData (_Unwind_Context_t c);
135 extern uintptr_t _Unwind_GetGR (_Unwind_Context_t c, int i);
136 extern void _Unwind_SetGR (_Unwind_Context_t c, int i, uintptr_t n);
137 extern void _Unwind_SetIP (_Unwind_Context_t, uintptr_t new_value);
138 extern uintptr_t _Unwind_GetIP (_Unwind_Context_t context);
139 extern uintptr_t _Unwind_GetRegionStart (_Unwind_Context_t context);
147 /// This is our simplistic type info
148 struct OurExceptionType_t {
154 /// This is our Exception class which relies on a negative offset to calculate
155 /// pointers to its instances from pointers to its unwindException member.
157 /// Note: The above unwind.h defines struct _Unwind_Exception to be aligned
158 /// on a double word boundary. This is necessary to match the standard:
159 /// http://refspecs.freestandards.org/abi-eh-1.21.html
160 struct OurBaseException_t {
161 struct OurExceptionType_t type;
163 // Note: This is properly aligned in unwind.h
164 struct _Unwind_Exception unwindException;
168 // Note: Not needed since we are C++
169 typedef struct OurBaseException_t OurException;
170 typedef struct _Unwind_Exception OurUnwindException;
173 // Various globals used to support typeinfo and generatted exceptions in
177 static std::map<std::string, llvm::Value*> namedValues;
179 int64_t ourBaseFromUnwindOffset;
181 const unsigned char ourBaseExcpClassChars[] =
182 {'o', 'b', 'j', '\0', 'b', 'a', 's', '\0'};
185 static uint64_t ourBaseExceptionClass = 0;
187 static std::vector<std::string> ourTypeInfoNames;
188 static std::map<int, std::string> ourTypeInfoNamesIndex;
190 static llvm::StructType *ourTypeInfoType;
191 #ifndef OLD_EXC_SYSTEM
192 static llvm::StructType *ourCaughtResultType;
194 static llvm::StructType *ourExceptionType;
195 static llvm::StructType *ourUnwindExceptionType;
197 static llvm::ConstantInt *ourExceptionNotThrownState;
198 static llvm::ConstantInt *ourExceptionThrownState;
199 static llvm::ConstantInt *ourExceptionCaughtState;
201 typedef std::vector<std::string> ArgNames;
202 typedef std::vector<llvm::Type*> ArgTypes;
205 // Code Generation Utilities
208 /// Utility used to create a function, both declarations and definitions
209 /// @param module for module instance
210 /// @param retType function return type
211 /// @param theArgTypes function's ordered argument types
212 /// @param theArgNames function's ordered arguments needed if use of this
213 /// function corresponds to a function definition. Use empty
214 /// aggregate for function declarations.
215 /// @param functName function name
216 /// @param linkage function linkage
217 /// @param declarationOnly for function declarations
218 /// @param isVarArg function uses vararg arguments
219 /// @returns function instance
220 llvm::Function *createFunction(llvm::Module &module,
222 const ArgTypes &theArgTypes,
223 const ArgNames &theArgNames,
224 const std::string &functName,
225 llvm::GlobalValue::LinkageTypes linkage,
226 bool declarationOnly,
228 llvm::FunctionType *functType =
229 llvm::FunctionType::get(retType, theArgTypes, isVarArg);
230 llvm::Function *ret =
231 llvm::Function::Create(functType, linkage, functName, &module);
232 if (!ret || declarationOnly)
237 for (llvm::Function::arg_iterator argIndex = ret->arg_begin();
238 i != theArgNames.size();
241 argIndex->setName(theArgNames[i]);
242 namedValues[theArgNames[i]] = argIndex;
249 /// Create an alloca instruction in the entry block of
250 /// the parent function. This is used for mutable variables etc.
251 /// @param function parent instance
252 /// @param varName stack variable name
253 /// @param type stack variable type
254 /// @param initWith optional constant initialization value
255 /// @returns AllocaInst instance
256 static llvm::AllocaInst *createEntryBlockAlloca(llvm::Function &function,
257 const std::string &varName,
259 llvm::Constant *initWith = 0) {
260 llvm::BasicBlock &block = function.getEntryBlock();
261 llvm::IRBuilder<> tmp(&block, block.begin());
262 llvm::AllocaInst *ret = tmp.CreateAlloca(type, 0, varName.c_str());
265 tmp.CreateStore(initWith, ret);
272 // Code Generation Utilities End
276 // Runtime C Library functions
279 // Note: using an extern "C" block so that static functions can be used
282 // Note: Better ways to decide on bit width
284 /// Prints a 32 bit number, according to the format, to stderr.
285 /// @param intToPrint integer to print
286 /// @param format printf like format to use when printing
287 void print32Int(int intToPrint, const char *format) {
289 // Note: No NULL check
290 fprintf(stderr, format, intToPrint);
293 // Note: No NULL check
294 fprintf(stderr, "::print32Int(...):NULL arg.\n");
299 // Note: Better ways to decide on bit width
301 /// Prints a 64 bit number, according to the format, to stderr.
302 /// @param intToPrint integer to print
303 /// @param format printf like format to use when printing
304 void print64Int(long int intToPrint, const char *format) {
306 // Note: No NULL check
307 fprintf(stderr, format, intToPrint);
310 // Note: No NULL check
311 fprintf(stderr, "::print64Int(...):NULL arg.\n");
316 /// Prints a C string to stderr
317 /// @param toPrint string to print
318 void printStr(char *toPrint) {
320 fprintf(stderr, "%s", toPrint);
323 fprintf(stderr, "::printStr(...):NULL arg.\n");
328 /// Deletes the true previosly allocated exception whose address
329 /// is calculated from the supplied OurBaseException_t::unwindException
330 /// member address. Handles (ignores), NULL pointers.
331 /// @param expToDelete exception to delete
332 void deleteOurException(OurUnwindException *expToDelete) {
335 "deleteOurException(...).\n");
339 (expToDelete->exception_class == ourBaseExceptionClass)) {
341 free(((char*) expToDelete) + ourBaseFromUnwindOffset);
346 /// This function is the struct _Unwind_Exception API mandated delete function
347 /// used by foreign exception handlers when deleting our exception
348 /// (OurException), instances.
349 /// @param reason @link http://refspecs.freestandards.org/abi-eh-1.21.html
351 /// @param expToDelete exception instance to delete
352 void deleteFromUnwindOurException(_Unwind_Reason_Code reason,
353 OurUnwindException *expToDelete) {
356 "deleteFromUnwindOurException(...).\n");
359 deleteOurException(expToDelete);
363 /// Creates (allocates on the heap), an exception (OurException instance),
364 /// of the supplied type info type.
365 /// @param type type info type
366 OurUnwindException *createOurException(int type) {
367 size_t size = sizeof(OurException);
368 OurException *ret = (OurException*) memset(malloc(size), 0, size);
369 (ret->type).type = type;
370 (ret->unwindException).exception_class = ourBaseExceptionClass;
371 (ret->unwindException).exception_cleanup = deleteFromUnwindOurException;
373 return(&(ret->unwindException));
377 /// Read a uleb128 encoded value and advance pointer
378 /// See Variable Length Data in:
379 /// @link http://dwarfstd.org/Dwarf3.pdf @unlink
380 /// @param data reference variable holding memory pointer to decode from
381 /// @returns decoded value
382 static uintptr_t readULEB128(const uint8_t **data) {
383 uintptr_t result = 0;
386 const uint8_t *p = *data;
390 result |= (byte & 0x7f) << shift;
401 /// Read a sleb128 encoded value and advance pointer
402 /// See Variable Length Data in:
403 /// @link http://dwarfstd.org/Dwarf3.pdf @unlink
404 /// @param data reference variable holding memory pointer to decode from
405 /// @returns decoded value
406 static uintptr_t readSLEB128(const uint8_t **data) {
407 uintptr_t result = 0;
410 const uint8_t *p = *data;
414 result |= (byte & 0x7f) << shift;
421 if ((byte & 0x40) && (shift < (sizeof(result) << 3))) {
422 result |= (~0 << shift);
429 /// Read a pointer encoded value and advance pointer
430 /// See Variable Length Data in:
431 /// @link http://dwarfstd.org/Dwarf3.pdf @unlink
432 /// @param data reference variable holding memory pointer to decode from
433 /// @param encoding dwarf encoding type
434 /// @returns decoded value
435 static uintptr_t readEncodedPointer(const uint8_t **data, uint8_t encoding) {
436 uintptr_t result = 0;
437 const uint8_t *p = *data;
439 if (encoding == llvm::dwarf::DW_EH_PE_omit)
443 switch (encoding & 0x0F) {
444 case llvm::dwarf::DW_EH_PE_absptr:
445 result = *((uintptr_t*)p);
446 p += sizeof(uintptr_t);
448 case llvm::dwarf::DW_EH_PE_uleb128:
449 result = readULEB128(&p);
451 // Note: This case has not been tested
452 case llvm::dwarf::DW_EH_PE_sleb128:
453 result = readSLEB128(&p);
455 case llvm::dwarf::DW_EH_PE_udata2:
456 result = *((uint16_t*)p);
457 p += sizeof(uint16_t);
459 case llvm::dwarf::DW_EH_PE_udata4:
460 result = *((uint32_t*)p);
461 p += sizeof(uint32_t);
463 case llvm::dwarf::DW_EH_PE_udata8:
464 result = *((uint64_t*)p);
465 p += sizeof(uint64_t);
467 case llvm::dwarf::DW_EH_PE_sdata2:
468 result = *((int16_t*)p);
469 p += sizeof(int16_t);
471 case llvm::dwarf::DW_EH_PE_sdata4:
472 result = *((int32_t*)p);
473 p += sizeof(int32_t);
475 case llvm::dwarf::DW_EH_PE_sdata8:
476 result = *((int64_t*)p);
477 p += sizeof(int64_t);
485 // then add relative offset
486 switch (encoding & 0x70) {
487 case llvm::dwarf::DW_EH_PE_absptr:
490 case llvm::dwarf::DW_EH_PE_pcrel:
491 result += (uintptr_t)(*data);
493 case llvm::dwarf::DW_EH_PE_textrel:
494 case llvm::dwarf::DW_EH_PE_datarel:
495 case llvm::dwarf::DW_EH_PE_funcrel:
496 case llvm::dwarf::DW_EH_PE_aligned:
503 // then apply indirection
504 if (encoding & llvm::dwarf::DW_EH_PE_indirect) {
505 result = *((uintptr_t*)result);
514 /// Deals with Dwarf actions matching our type infos
515 /// (OurExceptionType_t instances). Returns whether or not a dwarf emitted
516 /// action matches the supplied exception type. If such a match succeeds,
517 /// the resultAction argument will be set with > 0 index value. Only
518 /// corresponding llvm.eh.selector type info arguments, cleanup arguments
519 /// are supported. Filters are not supported.
520 /// See Variable Length Data in:
521 /// @link http://dwarfstd.org/Dwarf3.pdf @unlink
522 /// Also see @link http://refspecs.freestandards.org/abi-eh-1.21.html @unlink
523 /// @param resultAction reference variable which will be set with result
524 /// @param classInfo our array of type info pointers (to globals)
525 /// @param actionEntry index into above type info array or 0 (clean up).
526 /// We do not support filters.
527 /// @param exceptionClass exception class (_Unwind_Exception::exception_class)
528 /// of thrown exception.
529 /// @param exceptionObject thrown _Unwind_Exception instance.
530 /// @returns whether or not a type info was found. False is returned if only
531 /// a cleanup was found
532 static bool handleActionValue(int64_t *resultAction,
533 struct OurExceptionType_t **classInfo,
534 uintptr_t actionEntry,
535 uint64_t exceptionClass,
536 struct _Unwind_Exception *exceptionObject) {
541 (exceptionClass != ourBaseExceptionClass))
544 struct OurBaseException_t *excp = (struct OurBaseException_t*)
545 (((char*) exceptionObject) + ourBaseFromUnwindOffset);
546 struct OurExceptionType_t *excpType = &(excp->type);
547 int type = excpType->type;
551 "handleActionValue(...): exceptionObject = <%p>, "
557 const uint8_t *actionPos = (uint8_t*) actionEntry,
559 int64_t typeOffset = 0,
562 for (int i = 0; true; ++i) {
563 // Each emitted dwarf action corresponds to a 2 tuple of
564 // type info address offset, and action offset to the next
566 typeOffset = readSLEB128(&actionPos);
567 tempActionPos = actionPos;
568 actionOffset = readSLEB128(&tempActionPos);
572 "handleActionValue(...):typeOffset: <%lld>, "
573 "actionOffset: <%lld>.\n",
577 assert((typeOffset >= 0) &&
578 "handleActionValue(...):filters are not supported.");
580 // Note: A typeOffset == 0 implies that a cleanup llvm.eh.selector
581 // argument has been matched.
582 if ((typeOffset > 0) &&
583 (type == (classInfo[-typeOffset])->type)) {
586 "handleActionValue(...):actionValue <%d> found.\n",
589 *resultAction = i + 1;
596 "handleActionValue(...):actionValue not found.\n");
601 actionPos += actionOffset;
608 /// Deals with the Language specific data portion of the emitted dwarf code.
609 /// See @link http://refspecs.freestandards.org/abi-eh-1.21.html @unlink
610 /// @param version unsupported (ignored), unwind version
611 /// @param lsda language specific data area
612 /// @param _Unwind_Action actions minimally supported unwind stage
613 /// (forced specifically not supported)
614 /// @param exceptionClass exception class (_Unwind_Exception::exception_class)
615 /// of thrown exception.
616 /// @param exceptionObject thrown _Unwind_Exception instance.
617 /// @param context unwind system context
618 /// @returns minimally supported unwinding control indicator
619 static _Unwind_Reason_Code handleLsda(int version,
621 _Unwind_Action actions,
622 uint64_t exceptionClass,
623 struct _Unwind_Exception *exceptionObject,
624 _Unwind_Context_t context) {
625 _Unwind_Reason_Code ret = _URC_CONTINUE_UNWIND;
632 "handleLsda(...):lsda is non-zero.\n");
635 // Get the current instruction pointer and offset it before next
636 // instruction in the current frame which threw the exception.
637 uintptr_t pc = _Unwind_GetIP(context)-1;
639 // Get beginning current frame's code (as defined by the
640 // emitted dwarf code)
641 uintptr_t funcStart = _Unwind_GetRegionStart(context);
642 uintptr_t pcOffset = pc - funcStart;
643 struct OurExceptionType_t **classInfo = NULL;
645 // Note: See JITDwarfEmitter::EmitExceptionTable(...) for corresponding
648 // Parse LSDA header.
649 uint8_t lpStartEncoding = *lsda++;
651 if (lpStartEncoding != llvm::dwarf::DW_EH_PE_omit) {
652 readEncodedPointer(&lsda, lpStartEncoding);
655 uint8_t ttypeEncoding = *lsda++;
656 uintptr_t classInfoOffset;
658 if (ttypeEncoding != llvm::dwarf::DW_EH_PE_omit) {
659 // Calculate type info locations in emitted dwarf code which
660 // were flagged by type info arguments to llvm.eh.selector
662 classInfoOffset = readULEB128(&lsda);
663 classInfo = (struct OurExceptionType_t**) (lsda + classInfoOffset);
666 // Walk call-site table looking for range that
667 // includes current PC.
669 uint8_t callSiteEncoding = *lsda++;
670 uint32_t callSiteTableLength = readULEB128(&lsda);
671 const uint8_t *callSiteTableStart = lsda;
672 const uint8_t *callSiteTableEnd = callSiteTableStart +
674 const uint8_t *actionTableStart = callSiteTableEnd;
675 const uint8_t *callSitePtr = callSiteTableStart;
677 bool foreignException = false;
679 while (callSitePtr < callSiteTableEnd) {
680 uintptr_t start = readEncodedPointer(&callSitePtr,
682 uintptr_t length = readEncodedPointer(&callSitePtr,
684 uintptr_t landingPad = readEncodedPointer(&callSitePtr,
687 // Note: Action value
688 uintptr_t actionEntry = readULEB128(&callSitePtr);
690 if (exceptionClass != ourBaseExceptionClass) {
691 // We have been notified of a foreign exception being thrown,
692 // and we therefore need to execute cleanup landing pads
694 foreignException = true;
697 if (landingPad == 0) {
700 "handleLsda(...): No landing pad found.\n");
703 continue; // no landing pad for this entry
707 actionEntry += ((uintptr_t) actionTableStart) - 1;
712 "handleLsda(...):No action table found.\n");
716 bool exceptionMatched = false;
718 if ((start <= pcOffset) && (pcOffset < (start + length))) {
721 "handleLsda(...): Landing pad found.\n");
723 int64_t actionValue = 0;
726 exceptionMatched = handleActionValue(&actionValue,
733 if (!(actions & _UA_SEARCH_PHASE)) {
736 "handleLsda(...): installed landing pad "
740 // Found landing pad for the PC.
741 // Set Instruction Pointer to so we re-enter function
742 // at landing pad. The landing pad is created by the
743 // compiler to take two parameters in registers.
744 _Unwind_SetGR(context,
745 __builtin_eh_return_data_regno(0),
746 (uintptr_t)exceptionObject);
748 // Note: this virtual register directly corresponds
749 // to the return of the llvm.eh.selector intrinsic
750 if (!actionEntry || !exceptionMatched) {
751 // We indicate cleanup only
752 _Unwind_SetGR(context,
753 __builtin_eh_return_data_regno(1),
757 // Matched type info index of llvm.eh.selector intrinsic
759 _Unwind_SetGR(context,
760 __builtin_eh_return_data_regno(1),
764 // To execute landing pad set here
765 _Unwind_SetIP(context, funcStart + landingPad);
766 ret = _URC_INSTALL_CONTEXT;
768 else if (exceptionMatched) {
771 "handleLsda(...): setting handler found.\n");
773 ret = _URC_HANDLER_FOUND;
776 // Note: Only non-clean up handlers are marked as
777 // found. Otherwise the clean up handlers will be
778 // re-found and executed during the clean up
782 "handleLsda(...): cleanup handler found.\n");
794 /// This is the personality function which is embedded (dwarf emitted), in the
795 /// dwarf unwind info block. Again see: JITDwarfEmitter.cpp.
796 /// See @link http://refspecs.freestandards.org/abi-eh-1.21.html @unlink
797 /// @param version unsupported (ignored), unwind version
798 /// @param _Unwind_Action actions minimally supported unwind stage
799 /// (forced specifically not supported)
800 /// @param exceptionClass exception class (_Unwind_Exception::exception_class)
801 /// of thrown exception.
802 /// @param exceptionObject thrown _Unwind_Exception instance.
803 /// @param context unwind system context
804 /// @returns minimally supported unwinding control indicator
805 _Unwind_Reason_Code ourPersonality(int version,
806 _Unwind_Action actions,
807 uint64_t exceptionClass,
808 struct _Unwind_Exception *exceptionObject,
809 _Unwind_Context_t context) {
812 "We are in ourPersonality(...):actions is <%d>.\n",
815 if (actions & _UA_SEARCH_PHASE) {
816 fprintf(stderr, "ourPersonality(...):In search phase.\n");
819 fprintf(stderr, "ourPersonality(...):In non-search phase.\n");
823 const uint8_t *lsda = _Unwind_GetLanguageSpecificData(context);
827 "ourPersonality(...):lsda = <%p>.\n",
831 // The real work of the personality function is captured here
832 return(handleLsda(version,
841 /// Generates our _Unwind_Exception class from a given character array.
842 /// thereby handling arbitrary lengths (not in standard), and handling
844 /// See @link http://refspecs.freestandards.org/abi-eh-1.21.html @unlink
845 /// @param classChars char array to encode. NULL values not checkedf
846 /// @param classCharsSize number of chars in classChars. Value is not checked.
847 /// @returns class value
848 uint64_t genClass(const unsigned char classChars[], size_t classCharsSize)
850 uint64_t ret = classChars[0];
852 for (unsigned i = 1; i < classCharsSize; ++i) {
854 ret += classChars[i];
863 // Runtime C Library functions End
867 // Code generation functions
870 /// Generates code to print given constant string
871 /// @param context llvm context
872 /// @param module code for module instance
873 /// @param builder builder instance
874 /// @param toPrint string to print
875 /// @param useGlobal A value of true (default) indicates a GlobalValue is
876 /// generated, and is used to hold the constant string. A value of
877 /// false indicates that the constant string will be stored on the
879 void generateStringPrint(llvm::LLVMContext &context,
880 llvm::Module &module,
881 llvm::IRBuilder<> &builder,
883 bool useGlobal = true) {
884 llvm::Function *printFunct = module.getFunction("printStr");
886 llvm::Value *stringVar;
887 llvm::Constant *stringConstant =
888 llvm::ConstantArray::get(context, toPrint);
891 // Note: Does not work without allocation
893 new llvm::GlobalVariable(module,
894 stringConstant->getType(),
896 llvm::GlobalValue::LinkerPrivateLinkage,
901 stringVar = builder.CreateAlloca(stringConstant->getType());
902 builder.CreateStore(stringConstant, stringVar);
905 llvm::Value *cast = builder.CreatePointerCast(stringVar,
906 builder.getInt8PtrTy());
907 builder.CreateCall(printFunct, cast);
911 /// Generates code to print given runtime integer according to constant
912 /// string format, and a given print function.
913 /// @param context llvm context
914 /// @param module code for module instance
915 /// @param builder builder instance
916 /// @param printFunct function used to "print" integer
917 /// @param toPrint string to print
918 /// @param format printf like formating string for print
919 /// @param useGlobal A value of true (default) indicates a GlobalValue is
920 /// generated, and is used to hold the constant string. A value of
921 /// false indicates that the constant string will be stored on the
923 void generateIntegerPrint(llvm::LLVMContext &context,
924 llvm::Module &module,
925 llvm::IRBuilder<> &builder,
926 llvm::Function &printFunct,
927 llvm::Value &toPrint,
929 bool useGlobal = true) {
930 llvm::Constant *stringConstant = llvm::ConstantArray::get(context, format);
931 llvm::Value *stringVar;
934 // Note: Does not seem to work without allocation
936 new llvm::GlobalVariable(module,
937 stringConstant->getType(),
939 llvm::GlobalValue::LinkerPrivateLinkage,
944 stringVar = builder.CreateAlloca(stringConstant->getType());
945 builder.CreateStore(stringConstant, stringVar);
948 llvm::Value *cast = builder.CreateBitCast(stringVar,
949 builder.getInt8PtrTy());
950 builder.CreateCall2(&printFunct, &toPrint, cast);
954 /// Generates code to handle finally block type semantics: always runs
955 /// regardless of whether a thrown exception is passing through or the
956 /// parent function is simply exiting. In addition to printing some state
957 /// to stderr, this code will resume the exception handling--runs the
958 /// unwind resume block, if the exception has not been previously caught
959 /// by a catch clause, and will otherwise execute the end block (terminator
960 /// block). In addition this function creates the corresponding function's
961 /// stack storage for the exception pointer and catch flag status.
962 /// @param context llvm context
963 /// @param module code for module instance
964 /// @param builder builder instance
965 /// @param toAddTo parent function to add block to
966 /// @param blockName block name of new "finally" block.
967 /// @param functionId output id used for printing
968 /// @param terminatorBlock terminator "end" block
969 /// @param unwindResumeBlock unwind resume block
970 /// @param exceptionCaughtFlag reference exception caught/thrown status storage
971 /// @param exceptionStorage reference to exception pointer storage
972 #ifndef OLD_EXC_SYSTEM
973 /// @param caughtResultStorage reference to landingpad result storage
975 /// @returns newly created block
976 static llvm::BasicBlock *createFinallyBlock(llvm::LLVMContext &context,
977 llvm::Module &module,
978 llvm::IRBuilder<> &builder,
979 llvm::Function &toAddTo,
980 std::string &blockName,
981 std::string &functionId,
982 llvm::BasicBlock &terminatorBlock,
983 llvm::BasicBlock &unwindResumeBlock,
984 llvm::Value **exceptionCaughtFlag,
985 llvm::Value **exceptionStorage
986 #ifndef OLD_EXC_SYSTEM
987 ,llvm::Value **caughtResultStorage
990 assert(exceptionCaughtFlag &&
991 "ExceptionDemo::createFinallyBlock(...):exceptionCaughtFlag "
993 assert(exceptionStorage &&
994 "ExceptionDemo::createFinallyBlock(...):exceptionStorage "
997 #ifndef OLD_EXC_SYSTEM
998 assert(caughtResultStorage &&
999 "ExceptionDemo::createFinallyBlock(...):caughtResultStorage "
1003 *exceptionCaughtFlag = createEntryBlockAlloca(toAddTo,
1005 ourExceptionNotThrownState->getType(),
1006 ourExceptionNotThrownState);
1008 llvm::PointerType *exceptionStorageType = builder.getInt8PtrTy();
1009 *exceptionStorage = createEntryBlockAlloca(toAddTo,
1011 exceptionStorageType,
1012 llvm::ConstantPointerNull::get(
1013 exceptionStorageType));
1014 #ifndef OLD_EXC_SYSTEM
1015 *caughtResultStorage = createEntryBlockAlloca(toAddTo,
1016 "caughtResultStorage",
1017 ourCaughtResultType,
1018 llvm::ConstantAggregateZero::get(
1019 ourCaughtResultType));
1022 llvm::BasicBlock *ret = llvm::BasicBlock::Create(context,
1026 builder.SetInsertPoint(ret);
1028 std::ostringstream bufferToPrint;
1029 bufferToPrint << "Gen: Executing finally block "
1030 << blockName << " in " << functionId << "\n";
1031 generateStringPrint(context,
1034 bufferToPrint.str(),
1035 USE_GLOBAL_STR_CONSTS);
1037 llvm::SwitchInst *theSwitch = builder.CreateSwitch(builder.CreateLoad(
1038 *exceptionCaughtFlag),
1041 theSwitch->addCase(ourExceptionCaughtState, &terminatorBlock);
1042 theSwitch->addCase(ourExceptionThrownState, &unwindResumeBlock);
1048 /// Generates catch block semantics which print a string to indicate type of
1049 /// catch executed, sets an exception caught flag, and executes passed in
1050 /// end block (terminator block).
1051 /// @param context llvm context
1052 /// @param module code for module instance
1053 /// @param builder builder instance
1054 /// @param toAddTo parent function to add block to
1055 /// @param blockName block name of new "catch" block.
1056 /// @param functionId output id used for printing
1057 /// @param terminatorBlock terminator "end" block
1058 /// @param exceptionCaughtFlag exception caught/thrown status
1059 /// @returns newly created block
1060 static llvm::BasicBlock *createCatchBlock(llvm::LLVMContext &context,
1061 llvm::Module &module,
1062 llvm::IRBuilder<> &builder,
1063 llvm::Function &toAddTo,
1064 std::string &blockName,
1065 std::string &functionId,
1066 llvm::BasicBlock &terminatorBlock,
1067 llvm::Value &exceptionCaughtFlag) {
1069 llvm::BasicBlock *ret = llvm::BasicBlock::Create(context,
1073 builder.SetInsertPoint(ret);
1075 std::ostringstream bufferToPrint;
1076 bufferToPrint << "Gen: Executing catch block "
1081 generateStringPrint(context,
1084 bufferToPrint.str(),
1085 USE_GLOBAL_STR_CONSTS);
1086 builder.CreateStore(ourExceptionCaughtState, &exceptionCaughtFlag);
1087 builder.CreateBr(&terminatorBlock);
1093 /// Generates a function which invokes a function (toInvoke) and, whose
1094 /// unwind block will "catch" the type info types correspondingly held in the
1095 /// exceptionTypesToCatch argument. If the toInvoke function throws an
1096 /// exception which does not match any type info types contained in
1097 /// exceptionTypesToCatch, the generated code will call _Unwind_Resume
1098 /// with the raised exception. On the other hand the generated code will
1099 /// normally exit if the toInvoke function does not throw an exception.
1100 /// The generated "finally" block is always run regardless of the cause of
1101 /// the generated function exit.
1102 /// The generated function is returned after being verified.
1103 /// @param module code for module instance
1104 /// @param builder builder instance
1105 /// @param fpm a function pass manager holding optional IR to IR
1107 /// @param toInvoke inner function to invoke
1108 /// @param ourId id used to printing purposes
1109 /// @param numExceptionsToCatch length of exceptionTypesToCatch array
1110 /// @param exceptionTypesToCatch array of type info types to "catch"
1111 /// @returns generated function
1113 llvm::Function *createCatchWrappedInvokeFunction(llvm::Module &module,
1114 llvm::IRBuilder<> &builder,
1115 llvm::FunctionPassManager &fpm,
1116 llvm::Function &toInvoke,
1118 unsigned numExceptionsToCatch,
1119 unsigned exceptionTypesToCatch[]) {
1121 llvm::LLVMContext &context = module.getContext();
1122 llvm::Function *toPrint32Int = module.getFunction("print32Int");
1125 argTypes.push_back(builder.getInt32Ty());
1128 argNames.push_back("exceptTypeToThrow");
1130 llvm::Function *ret = createFunction(module,
1131 builder.getVoidTy(),
1135 llvm::Function::ExternalLinkage,
1139 // Block which calls invoke
1140 llvm::BasicBlock *entryBlock = llvm::BasicBlock::Create(context,
1143 // Normal block for invoke
1144 llvm::BasicBlock *normalBlock = llvm::BasicBlock::Create(context,
1147 // Unwind block for invoke
1148 llvm::BasicBlock *exceptionBlock = llvm::BasicBlock::Create(context,
1152 // Block which routes exception to correct catch handler block
1153 llvm::BasicBlock *exceptionRouteBlock = llvm::BasicBlock::Create(context,
1157 // Foreign exception handler
1158 llvm::BasicBlock *externalExceptionBlock = llvm::BasicBlock::Create(context,
1159 "externalException",
1162 // Block which calls _Unwind_Resume
1163 llvm::BasicBlock *unwindResumeBlock = llvm::BasicBlock::Create(context,
1167 // Clean up block which delete exception if needed
1168 llvm::BasicBlock *endBlock = llvm::BasicBlock::Create(context, "end", ret);
1170 std::string nextName;
1171 std::vector<llvm::BasicBlock*> catchBlocks(numExceptionsToCatch);
1172 llvm::Value *exceptionCaughtFlag = NULL;
1173 llvm::Value *exceptionStorage = NULL;
1174 #ifndef OLD_EXC_SYSTEM
1175 llvm::Value *caughtResultStorage = NULL;
1178 // Finally block which will branch to unwindResumeBlock if
1179 // exception is not caught. Initializes/allocates stack locations.
1180 llvm::BasicBlock *finallyBlock = createFinallyBlock(context,
1184 nextName = "finally",
1188 &exceptionCaughtFlag,
1190 #ifndef OLD_EXC_SYSTEM
1191 ,&caughtResultStorage
1195 for (unsigned i = 0; i < numExceptionsToCatch; ++i) {
1196 nextName = ourTypeInfoNames[exceptionTypesToCatch[i]];
1198 // One catch block per type info to be caught
1199 catchBlocks[i] = createCatchBlock(context,
1206 *exceptionCaughtFlag);
1211 builder.SetInsertPoint(entryBlock);
1213 std::vector<llvm::Value*> args;
1214 args.push_back(namedValues["exceptTypeToThrow"]);
1215 builder.CreateInvoke(&toInvoke,
1222 builder.SetInsertPoint(endBlock);
1224 generateStringPrint(context,
1227 "Gen: In end block: exiting in " + ourId + ".\n",
1228 USE_GLOBAL_STR_CONSTS);
1229 llvm::Function *deleteOurException = module.getFunction("deleteOurException");
1231 // Note: function handles NULL exceptions
1232 builder.CreateCall(deleteOurException,
1233 builder.CreateLoad(exceptionStorage));
1234 builder.CreateRetVoid();
1238 builder.SetInsertPoint(normalBlock);
1240 generateStringPrint(context,
1243 "Gen: No exception in " + ourId + "!\n",
1244 USE_GLOBAL_STR_CONSTS);
1246 // Finally block is always called
1247 builder.CreateBr(finallyBlock);
1249 // Unwind Resume Block
1251 builder.SetInsertPoint(unwindResumeBlock);
1254 #ifndef OLD_EXC_SYSTEM
1255 builder.CreateResume(builder.CreateLoad(caughtResultStorage));
1257 llvm::Function *resumeOurException = module.getFunction("_Unwind_Resume");
1258 builder.CreateCall(resumeOurException,
1259 builder.CreateLoad(exceptionStorage));
1260 builder.CreateUnreachable();
1265 builder.SetInsertPoint(exceptionBlock);
1267 llvm::Function *personality = module.getFunction("ourPersonality");
1269 #ifndef OLD_EXC_SYSTEM
1270 llvm::LandingPadInst *caughtResult =
1271 builder.CreateLandingPad(ourCaughtResultType,
1273 numExceptionsToCatch,
1276 caughtResult->setCleanup(true);
1278 for (unsigned i = 0; i < numExceptionsToCatch; ++i) {
1279 // Set up type infos to be caught
1280 caughtResult->addClause(module.getGlobalVariable(
1281 ourTypeInfoNames[exceptionTypesToCatch[i]]));
1284 llvm::Value *unwindException = builder.CreateExtractValue(caughtResult, 0);
1285 llvm::Value *retTypeInfoIndex = builder.CreateExtractValue(caughtResult, 1);
1287 // FIXME: Redundant storage which, beyond utilizing value of
1288 // caughtResultStore for unwindException storage, may be alleviated
1289 // alltogether with a block rearrangement
1290 builder.CreateStore(caughtResult, caughtResultStorage);
1291 builder.CreateStore(unwindException, exceptionStorage);
1292 builder.CreateStore(ourExceptionThrownState, exceptionCaughtFlag);
1294 llvm::Function *ehException = module.getFunction("llvm.eh.exception");
1296 // Retrieve thrown exception
1297 llvm::Value *unwindException = builder.CreateCall(ehException);
1299 // Store exception and flag
1300 builder.CreateStore(unwindException, exceptionStorage);
1301 builder.CreateStore(ourExceptionThrownState, exceptionCaughtFlag);
1302 llvm::Value *functPtr = builder.CreatePointerCast(personality,
1303 builder.getInt8PtrTy());
1306 args.push_back(unwindException);
1307 args.push_back(functPtr);
1309 // Note: Skipping index 0
1310 for (unsigned i = 0; i < numExceptionsToCatch; ++i) {
1311 // Set up type infos to be caught
1312 args.push_back(module.getGlobalVariable(
1313 ourTypeInfoNames[exceptionTypesToCatch[i]]));
1316 args.push_back(llvm::ConstantInt::get(builder.getInt32Ty(), 0));
1318 llvm::Function *ehSelector = module.getFunction("llvm.eh.selector");
1320 // Set up this exeption block as the landing pad which will handle
1321 // given type infos. See case Intrinsic::eh_selector in
1322 // SelectionDAGBuilder::visitIntrinsicCall(...) and AddCatchInfo(...)
1323 // implemented in FunctionLoweringInfo.cpp to see how the implementation
1324 // handles this call. This landing pad (this exception block), will be
1325 // called either because it nees to cleanup (call finally) or a type
1326 // info was found which matched the thrown exception.
1327 llvm::Value *retTypeInfoIndex = builder.CreateCall(ehSelector, args);
1330 // Retrieve exception_class member from thrown exception
1331 // (_Unwind_Exception instance). This member tells us whether or not
1332 // the exception is foreign.
1333 llvm::Value *unwindExceptionClass =
1334 builder.CreateLoad(builder.CreateStructGEP(
1335 builder.CreatePointerCast(unwindException,
1336 ourUnwindExceptionType->getPointerTo()),
1339 // Branch to the externalExceptionBlock if the exception is foreign or
1340 // to a catch router if not. Either way the finally block will be run.
1341 builder.CreateCondBr(builder.CreateICmpEQ(unwindExceptionClass,
1342 llvm::ConstantInt::get(builder.getInt64Ty(),
1343 ourBaseExceptionClass)),
1344 exceptionRouteBlock,
1345 externalExceptionBlock);
1347 // External Exception Block
1349 builder.SetInsertPoint(externalExceptionBlock);
1351 generateStringPrint(context,
1354 "Gen: Foreign exception received.\n",
1355 USE_GLOBAL_STR_CONSTS);
1357 // Branch to the finally block
1358 builder.CreateBr(finallyBlock);
1360 // Exception Route Block
1362 builder.SetInsertPoint(exceptionRouteBlock);
1364 // Casts exception pointer (_Unwind_Exception instance) to parent
1365 // (OurException instance).
1367 // Note: ourBaseFromUnwindOffset is usually negative
1368 llvm::Value *typeInfoThrown = builder.CreatePointerCast(
1369 builder.CreateConstGEP1_64(unwindException,
1370 ourBaseFromUnwindOffset),
1371 ourExceptionType->getPointerTo());
1373 // Retrieve thrown exception type info type
1375 // Note: Index is not relative to pointer but instead to structure
1376 // unlike a true getelementptr (GEP) instruction
1377 typeInfoThrown = builder.CreateStructGEP(typeInfoThrown, 0);
1379 llvm::Value *typeInfoThrownType =
1380 builder.CreateStructGEP(typeInfoThrown, 0);
1382 generateIntegerPrint(context,
1386 *(builder.CreateLoad(typeInfoThrownType)),
1387 "Gen: Exception type <%d> received (stack unwound) "
1391 USE_GLOBAL_STR_CONSTS);
1393 // Route to matched type info catch block or run cleanup finally block
1394 llvm::SwitchInst *switchToCatchBlock = builder.CreateSwitch(retTypeInfoIndex,
1396 numExceptionsToCatch);
1398 unsigned nextTypeToCatch;
1400 for (unsigned i = 1; i <= numExceptionsToCatch; ++i) {
1401 nextTypeToCatch = i - 1;
1402 switchToCatchBlock->addCase(llvm::ConstantInt::get(
1403 llvm::Type::getInt32Ty(context), i),
1404 catchBlocks[nextTypeToCatch]);
1407 #ifdef OLD_EXC_SYSTEM
1408 // Must be run before verifier
1409 UpgradeExceptionHandling(&module);
1413 llvm::verifyFunction(*ret);
1420 /// Generates function which throws either an exception matched to a runtime
1421 /// determined type info type (argument to generated function), or if this
1422 /// runtime value matches nativeThrowType, throws a foreign exception by
1423 /// calling nativeThrowFunct.
1424 /// @param module code for module instance
1425 /// @param builder builder instance
1426 /// @param fpm a function pass manager holding optional IR to IR
1428 /// @param ourId id used to printing purposes
1429 /// @param nativeThrowType a runtime argument of this value results in
1430 /// nativeThrowFunct being called to generate/throw exception.
1431 /// @param nativeThrowFunct function which will throw a foreign exception
1432 /// if the above nativeThrowType matches generated function's arg.
1433 /// @returns generated function
1435 llvm::Function *createThrowExceptionFunction(llvm::Module &module,
1436 llvm::IRBuilder<> &builder,
1437 llvm::FunctionPassManager &fpm,
1439 int32_t nativeThrowType,
1440 llvm::Function &nativeThrowFunct) {
1441 llvm::LLVMContext &context = module.getContext();
1442 namedValues.clear();
1443 ArgTypes unwindArgTypes;
1444 unwindArgTypes.push_back(builder.getInt32Ty());
1445 ArgNames unwindArgNames;
1446 unwindArgNames.push_back("exceptTypeToThrow");
1448 llvm::Function *ret = createFunction(module,
1449 builder.getVoidTy(),
1453 llvm::Function::ExternalLinkage,
1457 // Throws either one of our exception or a native C++ exception depending
1458 // on a runtime argument value containing a type info type.
1459 llvm::BasicBlock *entryBlock = llvm::BasicBlock::Create(context,
1462 // Throws a foreign exception
1463 llvm::BasicBlock *nativeThrowBlock = llvm::BasicBlock::Create(context,
1466 // Throws one of our Exceptions
1467 llvm::BasicBlock *generatedThrowBlock = llvm::BasicBlock::Create(context,
1470 // Retrieved runtime type info type to throw
1471 llvm::Value *exceptionType = namedValues["exceptTypeToThrow"];
1473 // nativeThrowBlock block
1475 builder.SetInsertPoint(nativeThrowBlock);
1477 // Throws foreign exception
1478 builder.CreateCall(&nativeThrowFunct, exceptionType);
1479 builder.CreateUnreachable();
1483 builder.SetInsertPoint(entryBlock);
1485 llvm::Function *toPrint32Int = module.getFunction("print32Int");
1486 generateIntegerPrint(context,
1491 "\nGen: About to throw exception type <%d> in " +
1494 USE_GLOBAL_STR_CONSTS);
1496 // Switches on runtime type info type value to determine whether or not
1497 // a foreign exception is thrown. Defaults to throwing one of our
1498 // generated exceptions.
1499 llvm::SwitchInst *theSwitch = builder.CreateSwitch(exceptionType,
1500 generatedThrowBlock,
1503 theSwitch->addCase(llvm::ConstantInt::get(llvm::Type::getInt32Ty(context),
1507 // generatedThrow block
1509 builder.SetInsertPoint(generatedThrowBlock);
1511 llvm::Function *createOurException = module.getFunction("createOurException");
1512 llvm::Function *raiseOurException = module.getFunction(
1513 "_Unwind_RaiseException");
1515 // Creates exception to throw with runtime type info type.
1516 llvm::Value *exception = builder.CreateCall(createOurException,
1517 namedValues["exceptTypeToThrow"]);
1519 // Throw generated Exception
1520 builder.CreateCall(raiseOurException, exception);
1521 builder.CreateUnreachable();
1523 llvm::verifyFunction(*ret);
1529 static void createStandardUtilityFunctions(unsigned numTypeInfos,
1530 llvm::Module &module,
1531 llvm::IRBuilder<> &builder);
1533 /// Creates test code by generating and organizing these functions into the
1534 /// test case. The test case consists of an outer function setup to invoke
1535 /// an inner function within an environment having multiple catch and single
1536 /// finally blocks. This inner function is also setup to invoke a throw
1537 /// function within an evironment similar in nature to the outer function's
1538 /// catch and finally blocks. Each of these two functions catch mutually
1539 /// exclusive subsets (even or odd) of the type info types configured
1540 /// for this this. All generated functions have a runtime argument which
1541 /// holds a type info type to throw that each function takes and passes it
1542 /// to the inner one if such a inner function exists. This type info type is
1543 /// looked at by the generated throw function to see whether or not it should
1544 /// throw a generated exception with the same type info type, or instead call
1545 /// a supplied a function which in turn will throw a foreign exception.
1546 /// @param module code for module instance
1547 /// @param builder builder instance
1548 /// @param fpm a function pass manager holding optional IR to IR
1550 /// @param nativeThrowFunctName name of external function which will throw
1551 /// a foreign exception
1552 /// @returns outermost generated test function.
1553 llvm::Function *createUnwindExceptionTest(llvm::Module &module,
1554 llvm::IRBuilder<> &builder,
1555 llvm::FunctionPassManager &fpm,
1556 std::string nativeThrowFunctName) {
1557 // Number of type infos to generate
1558 unsigned numTypeInfos = 6;
1560 // Initialze intrisics and external functions to use along with exception
1561 // and type info globals.
1562 createStandardUtilityFunctions(numTypeInfos,
1565 llvm::Function *nativeThrowFunct = module.getFunction(nativeThrowFunctName);
1567 // Create exception throw function using the value ~0 to cause
1568 // foreign exceptions to be thrown.
1569 llvm::Function *throwFunct = createThrowExceptionFunction(module,
1575 // Inner function will catch even type infos
1576 unsigned innerExceptionTypesToCatch[] = {6, 2, 4};
1577 size_t numExceptionTypesToCatch = sizeof(innerExceptionTypesToCatch) /
1580 // Generate inner function.
1581 llvm::Function *innerCatchFunct = createCatchWrappedInvokeFunction(module,
1586 numExceptionTypesToCatch,
1587 innerExceptionTypesToCatch);
1589 // Outer function will catch odd type infos
1590 unsigned outerExceptionTypesToCatch[] = {3, 1, 5};
1591 numExceptionTypesToCatch = sizeof(outerExceptionTypesToCatch) /
1594 // Generate outer function
1595 llvm::Function *outerCatchFunct = createCatchWrappedInvokeFunction(module,
1600 numExceptionTypesToCatch,
1601 outerExceptionTypesToCatch);
1603 // Return outer function to run
1604 return(outerCatchFunct);
1608 /// Represents our foreign exceptions
1609 class OurCppRunException : public std::runtime_error {
1611 OurCppRunException(const std::string reason) :
1612 std::runtime_error(reason) {}
1614 OurCppRunException (const OurCppRunException &toCopy) :
1615 std::runtime_error(toCopy) {}
1617 OurCppRunException &operator = (const OurCppRunException &toCopy) {
1618 return(reinterpret_cast<OurCppRunException&>(
1619 std::runtime_error::operator=(toCopy)));
1622 ~OurCppRunException (void) throw () {}
1626 /// Throws foreign C++ exception.
1627 /// @param ignoreIt unused parameter that allows function to match implied
1628 /// generated function contract.
1630 void throwCppException (int32_t ignoreIt) {
1631 throw(OurCppRunException("thrown by throwCppException(...)"));
1634 typedef void (*OurExceptionThrowFunctType) (int32_t typeToThrow);
1636 /// This is a test harness which runs test by executing generated
1637 /// function with a type info type to throw. Harness wraps the execution
1638 /// of generated function in a C++ try catch clause.
1639 /// @param engine execution engine to use for executing generated function.
1640 /// This demo program expects this to be a JIT instance for demo
1642 /// @param function generated test function to run
1643 /// @param typeToThrow type info type of generated exception to throw, or
1644 /// indicator to cause foreign exception to be thrown.
1646 void runExceptionThrow(llvm::ExecutionEngine *engine,
1647 llvm::Function *function,
1648 int32_t typeToThrow) {
1650 // Find test's function pointer
1651 OurExceptionThrowFunctType functPtr =
1652 reinterpret_cast<OurExceptionThrowFunctType>(
1653 reinterpret_cast<intptr_t>(engine->getPointerToFunction(function)));
1657 (*functPtr)(typeToThrow);
1659 catch (OurCppRunException exc) {
1660 // Catch foreign C++ exception
1662 "\nrunExceptionThrow(...):In C++ catch OurCppRunException "
1663 "with reason: %s.\n",
1667 // Catch all exceptions including our generated ones. This latter
1668 // functionality works according to the example in rules 1.6.4 of
1669 // http://sourcery.mentor.com/public/cxx-abi/abi-eh.html (v1.22),
1670 // given that these will be exceptions foreign to C++
1671 // (the _Unwind_Exception::exception_class should be different from
1672 // the one used by C++).
1674 "\nrunExceptionThrow(...):In C++ catch all.\n");
1679 // End test functions
1682 typedef llvm::ArrayRef<llvm::Type*> TypeArray;
1684 /// This initialization routine creates type info globals and
1685 /// adds external function declarations to module.
1686 /// @param numTypeInfos number of linear type info associated type info types
1687 /// to create as GlobalVariable instances, starting with the value 1.
1688 /// @param module code for module instance
1689 /// @param builder builder instance
1690 static void createStandardUtilityFunctions(unsigned numTypeInfos,
1691 llvm::Module &module,
1692 llvm::IRBuilder<> &builder) {
1694 llvm::LLVMContext &context = module.getContext();
1696 // Exception initializations
1698 // Setup exception catch state
1699 ourExceptionNotThrownState =
1700 llvm::ConstantInt::get(llvm::Type::getInt8Ty(context), 0),
1701 ourExceptionThrownState =
1702 llvm::ConstantInt::get(llvm::Type::getInt8Ty(context), 1),
1703 ourExceptionCaughtState =
1704 llvm::ConstantInt::get(llvm::Type::getInt8Ty(context), 2),
1708 // Create our type info type
1709 ourTypeInfoType = llvm::StructType::get(context,
1710 TypeArray(builder.getInt32Ty()));
1712 #ifndef OLD_EXC_SYSTEM
1714 llvm::Type *caughtResultFieldTypes[] = {
1715 builder.getInt8PtrTy(),
1716 builder.getInt32Ty()
1719 // Create our landingpad result type
1720 ourCaughtResultType = llvm::StructType::get(context,
1721 TypeArray(caughtResultFieldTypes));
1725 // Create OurException type
1726 ourExceptionType = llvm::StructType::get(context,
1727 TypeArray(ourTypeInfoType));
1729 // Create portion of _Unwind_Exception type
1731 // Note: Declaring only a portion of the _Unwind_Exception struct.
1732 // Does this cause problems?
1733 ourUnwindExceptionType =
1734 llvm::StructType::get(context,
1735 TypeArray(builder.getInt64Ty()));
1737 struct OurBaseException_t dummyException;
1739 // Calculate offset of OurException::unwindException member.
1740 ourBaseFromUnwindOffset = ((uintptr_t) &dummyException) -
1741 ((uintptr_t) &(dummyException.unwindException));
1745 "createStandardUtilityFunctions(...):ourBaseFromUnwindOffset "
1746 "= %lld, sizeof(struct OurBaseException_t) - "
1747 "sizeof(struct _Unwind_Exception) = %lu.\n",
1748 ourBaseFromUnwindOffset,
1749 sizeof(struct OurBaseException_t) -
1750 sizeof(struct _Unwind_Exception));
1753 size_t numChars = sizeof(ourBaseExcpClassChars) / sizeof(char);
1755 // Create our _Unwind_Exception::exception_class value
1756 ourBaseExceptionClass = genClass(ourBaseExcpClassChars, numChars);
1760 std::string baseStr = "typeInfo", typeInfoName;
1761 std::ostringstream typeInfoNameBuilder;
1762 std::vector<llvm::Constant*> structVals;
1764 llvm::Constant *nextStruct;
1765 llvm::GlobalVariable *nextGlobal = NULL;
1767 // Generate each type info
1769 // Note: First type info is not used.
1770 for (unsigned i = 0; i <= numTypeInfos; ++i) {
1772 structVals.push_back(llvm::ConstantInt::get(builder.getInt32Ty(), i));
1773 nextStruct = llvm::ConstantStruct::get(ourTypeInfoType, structVals);
1775 typeInfoNameBuilder.str("");
1776 typeInfoNameBuilder << baseStr << i;
1777 typeInfoName = typeInfoNameBuilder.str();
1779 // Note: Does not seem to work without allocation
1781 new llvm::GlobalVariable(module,
1784 llvm::GlobalValue::ExternalLinkage,
1788 ourTypeInfoNames.push_back(typeInfoName);
1789 ourTypeInfoNamesIndex[i] = typeInfoName;
1794 llvm::Function *funct = NULL;
1798 llvm::Type *retType = builder.getVoidTy();
1801 argTypes.push_back(builder.getInt32Ty());
1802 argTypes.push_back(builder.getInt8PtrTy());
1806 createFunction(module,
1811 llvm::Function::ExternalLinkage,
1817 retType = builder.getVoidTy();
1820 argTypes.push_back(builder.getInt64Ty());
1821 argTypes.push_back(builder.getInt8PtrTy());
1825 createFunction(module,
1830 llvm::Function::ExternalLinkage,
1836 retType = builder.getVoidTy();
1839 argTypes.push_back(builder.getInt8PtrTy());
1843 createFunction(module,
1848 llvm::Function::ExternalLinkage,
1852 // throwCppException
1854 retType = builder.getVoidTy();
1857 argTypes.push_back(builder.getInt32Ty());
1861 createFunction(module,
1865 "throwCppException",
1866 llvm::Function::ExternalLinkage,
1870 // deleteOurException
1872 retType = builder.getVoidTy();
1875 argTypes.push_back(builder.getInt8PtrTy());
1879 createFunction(module,
1883 "deleteOurException",
1884 llvm::Function::ExternalLinkage,
1888 // createOurException
1890 retType = builder.getInt8PtrTy();
1893 argTypes.push_back(builder.getInt32Ty());
1897 createFunction(module,
1901 "createOurException",
1902 llvm::Function::ExternalLinkage,
1906 // _Unwind_RaiseException
1908 retType = builder.getInt32Ty();
1911 argTypes.push_back(builder.getInt8PtrTy());
1915 funct = createFunction(module,
1919 "_Unwind_RaiseException",
1920 llvm::Function::ExternalLinkage,
1924 funct->addFnAttr(llvm::Attribute::NoReturn);
1928 retType = builder.getInt32Ty();
1931 argTypes.push_back(builder.getInt8PtrTy());
1935 funct = createFunction(module,
1940 llvm::Function::ExternalLinkage,
1944 funct->addFnAttr(llvm::Attribute::NoReturn);
1948 retType = builder.getInt32Ty();
1951 argTypes.push_back(builder.getInt32Ty());
1952 argTypes.push_back(builder.getInt32Ty());
1953 argTypes.push_back(builder.getInt64Ty());
1954 argTypes.push_back(builder.getInt8PtrTy());
1955 argTypes.push_back(builder.getInt8PtrTy());
1959 createFunction(module,
1964 llvm::Function::ExternalLinkage,
1968 // llvm.eh.selector intrinsic
1970 getDeclaration(&module, llvm::Intrinsic::eh_selector);
1972 // llvm.eh.exception intrinsic
1974 getDeclaration(&module, llvm::Intrinsic::eh_exception);
1976 // llvm.eh.typeid.for intrinsic
1978 getDeclaration(&module, llvm::Intrinsic::eh_typeid_for);
1982 //===----------------------------------------------------------------------===//
1983 // Main test driver code.
1984 //===----------------------------------------------------------------------===//
1986 /// Demo main routine which takes the type info types to throw. A test will
1987 /// be run for each given type info type. While type info types with the value
1988 /// of -1 will trigger a foreign C++ exception to be thrown; type info types
1989 /// <= 6 and >= 1 will be caught by test functions; and type info types > 6
1990 /// will result in exceptions which pass through to the test harness. All other
1991 /// type info types are not supported and could cause a crash.
1992 int main(int argc, char *argv[]) {
1995 "\nUsage: ExceptionDemo <exception type to throw> "
1996 "[<type 2>...<type n>].\n"
1997 " Each type must have the value of 1 - 6 for "
1998 "generated exceptions to be caught;\n"
1999 " the value -1 for foreign C++ exceptions to be "
2000 "generated and thrown;\n"
2001 " or the values > 6 for exceptions to be ignored.\n"
2002 "\nTry: ExceptionDemo 2 3 7 -1\n"
2003 " for a full test.\n\n");
2007 // If not set, exception handling will not be turned on
2008 llvm::JITExceptionHandling = true;
2010 llvm::InitializeNativeTarget();
2011 llvm::LLVMContext &context = llvm::getGlobalContext();
2012 llvm::IRBuilder<> theBuilder(context);
2014 // Make the module, which holds all the code.
2015 llvm::Module *module = new llvm::Module("my cool jit", context);
2017 // Build engine with JIT
2018 llvm::EngineBuilder factory(module);
2019 factory.setEngineKind(llvm::EngineKind::JIT);
2020 factory.setAllocateGVsWithCode(false);
2021 llvm::ExecutionEngine *executionEngine = factory.create();
2024 llvm::FunctionPassManager fpm(module);
2026 // Set up the optimizer pipeline.
2027 // Start with registering info about how the
2028 // target lays out data structures.
2029 fpm.add(new llvm::TargetData(*executionEngine->getTargetData()));
2031 // Optimizations turned on
2032 #ifdef ADD_OPT_PASSES
2034 // Basic AliasAnslysis support for GVN.
2035 fpm.add(llvm::createBasicAliasAnalysisPass());
2037 // Promote allocas to registers.
2038 fpm.add(llvm::createPromoteMemoryToRegisterPass());
2040 // Do simple "peephole" optimizations and bit-twiddling optzns.
2041 fpm.add(llvm::createInstructionCombiningPass());
2043 // Reassociate expressions.
2044 fpm.add(llvm::createReassociatePass());
2046 // Eliminate Common SubExpressions.
2047 fpm.add(llvm::createGVNPass());
2049 // Simplify the control flow graph (deleting unreachable
2051 fpm.add(llvm::createCFGSimplificationPass());
2052 #endif // ADD_OPT_PASSES
2054 fpm.doInitialization();
2056 // Generate test code using function throwCppException(...) as
2057 // the function which throws foreign exceptions.
2058 llvm::Function *toRun =
2059 createUnwindExceptionTest(*module,
2062 "throwCppException");
2064 fprintf(stderr, "\nBegin module dump:\n\n");
2068 fprintf(stderr, "\nEnd module dump:\n");
2070 fprintf(stderr, "\n\nBegin Test:\n");
2072 for (int i = 1; i < argc; ++i) {
2073 // Run test for each argument whose value is the exception
2075 runExceptionThrow(executionEngine,
2077 (unsigned) strtoul(argv[i], NULL, 10));
2080 fprintf(stderr, "\nEnd Test:\n\n");
2083 delete executionEngine;