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 questionable.
45 // This code uses code from the llvm compiler-rt project and the llvm
46 // Kaleidoscope project.
48 //===----------------------------------------------------------------------===//
50 #include "llvm/LLVMContext.h"
51 #include "llvm/DerivedTypes.h"
52 #include "llvm/ExecutionEngine/ExecutionEngine.h"
53 #include "llvm/ExecutionEngine/JIT.h"
54 #include "llvm/Module.h"
55 #include "llvm/PassManager.h"
56 #include "llvm/Intrinsics.h"
57 #include "llvm/Analysis/Verifier.h"
58 #include "llvm/Target/TargetData.h"
59 #include "llvm/Target/TargetSelect.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"
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.
79 #ifndef USE_GLOBAL_STR_CONSTS
80 #define USE_GLOBAL_STR_CONSTS true
83 // System C++ ABI unwind types from:
84 // http://refspecs.freestandards.org/abi-eh-1.21.html
90 _URC_FOREIGN_EXCEPTION_CAUGHT
= 1,
91 _URC_FATAL_PHASE2_ERROR
= 2,
92 _URC_FATAL_PHASE1_ERROR
= 3,
94 _URC_END_OF_STACK
= 5,
95 _URC_HANDLER_FOUND
= 6,
96 _URC_INSTALL_CONTEXT
= 7,
97 _URC_CONTINUE_UNWIND
= 8
98 } _Unwind_Reason_Code
;
101 _UA_SEARCH_PHASE
= 1,
102 _UA_CLEANUP_PHASE
= 2,
103 _UA_HANDLER_FRAME
= 4,
104 _UA_FORCE_UNWIND
= 8,
105 _UA_END_OF_STACK
= 16
108 struct _Unwind_Exception
;
110 typedef void (*_Unwind_Exception_Cleanup_Fn
) (_Unwind_Reason_Code
,
111 struct _Unwind_Exception
*);
113 struct _Unwind_Exception
{
114 uint64_t exception_class
;
115 _Unwind_Exception_Cleanup_Fn exception_cleanup
;
120 // @@@ The IA-64 ABI says that this structure must be double-word aligned.
121 // Taking that literally does not make much sense generically. Instead
122 // we provide the maximum alignment required by any type for the machine.
123 } __attribute__((__aligned__
));
125 struct _Unwind_Context
;
126 typedef struct _Unwind_Context
*_Unwind_Context_t
;
128 extern const uint8_t *_Unwind_GetLanguageSpecificData (_Unwind_Context_t c
);
129 extern uintptr_t _Unwind_GetGR (_Unwind_Context_t c
, int i
);
130 extern void _Unwind_SetGR (_Unwind_Context_t c
, int i
, uintptr_t n
);
131 extern void _Unwind_SetIP (_Unwind_Context_t
, uintptr_t new_value
);
132 extern uintptr_t _Unwind_GetIP (_Unwind_Context_t context
);
133 extern uintptr_t _Unwind_GetRegionStart (_Unwind_Context_t context
);
141 /// This is our simplistic type info
142 struct OurExceptionType_t
{
148 /// This is our Exception class which relies on a negative offset to calculate
149 /// pointers to its instances from pointers to its unwindException member.
151 /// Note: The above unwind.h defines struct _Unwind_Exception to be aligned
152 /// on a double word boundary. This is necessary to match the standard:
153 /// http://refspecs.freestandards.org/abi-eh-1.21.html
154 struct OurBaseException_t
{
155 struct OurExceptionType_t type
;
157 // Note: This is properly aligned in unwind.h
158 struct _Unwind_Exception unwindException
;
162 // Note: Not needed since we are C++
163 typedef struct OurBaseException_t OurException
;
164 typedef struct _Unwind_Exception OurUnwindException
;
167 // Various globals used to support typeinfo and generatted exceptions in
171 static std::map
<std::string
, llvm::Value
*> namedValues
;
173 int64_t ourBaseFromUnwindOffset
;
175 const unsigned char ourBaseExcpClassChars
[] =
176 {'o', 'b', 'j', '\0', 'b', 'a', 's', '\0'};
179 static uint64_t ourBaseExceptionClass
= 0;
181 static std::vector
<std::string
> ourTypeInfoNames
;
182 static std::map
<int, std::string
> ourTypeInfoNamesIndex
;
184 static llvm::StructType
*ourTypeInfoType
;
185 static llvm::StructType
*ourExceptionType
;
186 static llvm::StructType
*ourUnwindExceptionType
;
188 static llvm::ConstantInt
*ourExceptionNotThrownState
;
189 static llvm::ConstantInt
*ourExceptionThrownState
;
190 static llvm::ConstantInt
*ourExceptionCaughtState
;
192 typedef std::vector
<std::string
> ArgNames
;
193 typedef std::vector
<const llvm::Type
*> ArgTypes
;
196 // Code Generation Utilities
199 /// Utility used to create a function, both declarations and definitions
200 /// @param module for module instance
201 /// @param retType function return type
202 /// @param theArgTypes function's ordered argument types
203 /// @param theArgNames function's ordered arguments needed if use of this
204 /// function corresponds to a function definition. Use empty
205 /// aggregate for function declarations.
206 /// @param functName function name
207 /// @param linkage function linkage
208 /// @param declarationOnly for function declarations
209 /// @param isVarArg function uses vararg arguments
210 /// @returns function instance
211 llvm::Function
*createFunction(llvm::Module
&module
,
212 const llvm::Type
*retType
,
213 const ArgTypes
&theArgTypes
,
214 const ArgNames
&theArgNames
,
215 const std::string
&functName
,
216 llvm::GlobalValue::LinkageTypes linkage
,
217 bool declarationOnly
,
219 llvm::FunctionType
*functType
=
220 llvm::FunctionType::get(retType
, theArgTypes
, isVarArg
);
221 llvm::Function
*ret
=
222 llvm::Function::Create(functType
, linkage
, functName
, &module
);
223 if (!ret
|| declarationOnly
)
228 for (llvm::Function::arg_iterator argIndex
= ret
->arg_begin();
229 i
!= theArgNames
.size();
232 argIndex
->setName(theArgNames
[i
]);
233 namedValues
[theArgNames
[i
]] = argIndex
;
240 /// Create an alloca instruction in the entry block of
241 /// the parent function. This is used for mutable variables etc.
242 /// @param function parent instance
243 /// @param varName stack variable name
244 /// @param type stack variable type
245 /// @param initWith optional constant initialization value
246 /// @returns AllocaInst instance
247 static llvm::AllocaInst
*createEntryBlockAlloca(llvm::Function
&function
,
248 const std::string
&varName
,
249 const llvm::Type
*type
,
250 llvm::Constant
*initWith
= 0) {
251 llvm::BasicBlock
&block
= function
.getEntryBlock();
252 llvm::IRBuilder
<> tmp(&block
, block
.begin());
253 llvm::AllocaInst
*ret
= tmp
.CreateAlloca(type
, 0, varName
.c_str());
256 tmp
.CreateStore(initWith
, ret
);
263 // Code Generation Utilities End
267 // Runtime C Library functions
270 // Note: using an extern "C" block so that static functions can be used
273 // Note: Better ways to decide on bit width
275 /// Prints a 32 bit number, according to the format, to stderr.
276 /// @param intToPrint integer to print
277 /// @param format printf like format to use when printing
278 void print32Int(int intToPrint
, const char *format
) {
280 // Note: No NULL check
281 fprintf(stderr
, format
, intToPrint
);
284 // Note: No NULL check
285 fprintf(stderr
, "::print32Int(...):NULL arg.\n");
290 // Note: Better ways to decide on bit width
292 /// Prints a 64 bit number, according to the format, to stderr.
293 /// @param intToPrint integer to print
294 /// @param format printf like format to use when printing
295 void print64Int(long int intToPrint
, const char *format
) {
297 // Note: No NULL check
298 fprintf(stderr
, format
, intToPrint
);
301 // Note: No NULL check
302 fprintf(stderr
, "::print64Int(...):NULL arg.\n");
307 /// Prints a C string to stderr
308 /// @param toPrint string to print
309 void printStr(char *toPrint
) {
311 fprintf(stderr
, "%s", toPrint
);
314 fprintf(stderr
, "::printStr(...):NULL arg.\n");
319 /// Deletes the true previosly allocated exception whose address
320 /// is calculated from the supplied OurBaseException_t::unwindException
321 /// member address. Handles (ignores), NULL pointers.
322 /// @param expToDelete exception to delete
323 void deleteOurException(OurUnwindException
*expToDelete
) {
326 "deleteOurException(...).\n");
330 (expToDelete
->exception_class
== ourBaseExceptionClass
)) {
332 free(((char*) expToDelete
) + ourBaseFromUnwindOffset
);
337 /// This function is the struct _Unwind_Exception API mandated delete function
338 /// used by foreign exception handlers when deleting our exception
339 /// (OurException), instances.
340 /// @param reason @link http://refspecs.freestandards.org/abi-eh-1.21.html
342 /// @param expToDelete exception instance to delete
343 void deleteFromUnwindOurException(_Unwind_Reason_Code reason
,
344 OurUnwindException
*expToDelete
) {
347 "deleteFromUnwindOurException(...).\n");
350 deleteOurException(expToDelete
);
354 /// Creates (allocates on the heap), an exception (OurException instance),
355 /// of the supplied type info type.
356 /// @param type type info type
357 OurUnwindException
*createOurException(int type
) {
358 size_t size
= sizeof(OurException
);
359 OurException
*ret
= (OurException
*) memset(malloc(size
), 0, size
);
360 (ret
->type
).type
= type
;
361 (ret
->unwindException
).exception_class
= ourBaseExceptionClass
;
362 (ret
->unwindException
).exception_cleanup
= deleteFromUnwindOurException
;
364 return(&(ret
->unwindException
));
368 /// Read a uleb128 encoded value and advance pointer
369 /// See Variable Length Data in:
370 /// @link http://dwarfstd.org/Dwarf3.pdf @unlink
371 /// @param data reference variable holding memory pointer to decode from
372 /// @returns decoded value
373 static uintptr_t readULEB128(const uint8_t **data
) {
374 uintptr_t result
= 0;
377 const uint8_t *p
= *data
;
381 result
|= (byte
& 0x7f) << shift
;
392 /// Read a sleb128 encoded value and advance pointer
393 /// See Variable Length Data in:
394 /// @link http://dwarfstd.org/Dwarf3.pdf @unlink
395 /// @param data reference variable holding memory pointer to decode from
396 /// @returns decoded value
397 static uintptr_t readSLEB128(const uint8_t **data
) {
398 uintptr_t result
= 0;
401 const uint8_t *p
= *data
;
405 result
|= (byte
& 0x7f) << shift
;
412 if ((byte
& 0x40) && (shift
< (sizeof(result
) << 3))) {
413 result
|= (~0 << shift
);
420 /// Read a pointer encoded value and advance pointer
421 /// See Variable Length Data in:
422 /// @link http://dwarfstd.org/Dwarf3.pdf @unlink
423 /// @param data reference variable holding memory pointer to decode from
424 /// @param encoding dwarf encoding type
425 /// @returns decoded value
426 static uintptr_t readEncodedPointer(const uint8_t **data
, uint8_t encoding
) {
427 uintptr_t result
= 0;
428 const uint8_t *p
= *data
;
430 if (encoding
== llvm::dwarf::DW_EH_PE_omit
)
434 switch (encoding
& 0x0F) {
435 case llvm::dwarf::DW_EH_PE_absptr
:
436 result
= *((uintptr_t*)p
);
437 p
+= sizeof(uintptr_t);
439 case llvm::dwarf::DW_EH_PE_uleb128
:
440 result
= readULEB128(&p
);
442 // Note: This case has not been tested
443 case llvm::dwarf::DW_EH_PE_sleb128
:
444 result
= readSLEB128(&p
);
446 case llvm::dwarf::DW_EH_PE_udata2
:
447 result
= *((uint16_t*)p
);
448 p
+= sizeof(uint16_t);
450 case llvm::dwarf::DW_EH_PE_udata4
:
451 result
= *((uint32_t*)p
);
452 p
+= sizeof(uint32_t);
454 case llvm::dwarf::DW_EH_PE_udata8
:
455 result
= *((uint64_t*)p
);
456 p
+= sizeof(uint64_t);
458 case llvm::dwarf::DW_EH_PE_sdata2
:
459 result
= *((int16_t*)p
);
460 p
+= sizeof(int16_t);
462 case llvm::dwarf::DW_EH_PE_sdata4
:
463 result
= *((int32_t*)p
);
464 p
+= sizeof(int32_t);
466 case llvm::dwarf::DW_EH_PE_sdata8
:
467 result
= *((int64_t*)p
);
468 p
+= sizeof(int64_t);
476 // then add relative offset
477 switch (encoding
& 0x70) {
478 case llvm::dwarf::DW_EH_PE_absptr
:
481 case llvm::dwarf::DW_EH_PE_pcrel
:
482 result
+= (uintptr_t)(*data
);
484 case llvm::dwarf::DW_EH_PE_textrel
:
485 case llvm::dwarf::DW_EH_PE_datarel
:
486 case llvm::dwarf::DW_EH_PE_funcrel
:
487 case llvm::dwarf::DW_EH_PE_aligned
:
494 // then apply indirection
495 if (encoding
& llvm::dwarf::DW_EH_PE_indirect
) {
496 result
= *((uintptr_t*)result
);
505 /// Deals with Dwarf actions matching our type infos
506 /// (OurExceptionType_t instances). Returns whether or not a dwarf emitted
507 /// action matches the supplied exception type. If such a match succeeds,
508 /// the resultAction argument will be set with > 0 index value. Only
509 /// corresponding llvm.eh.selector type info arguments, cleanup arguments
510 /// are supported. Filters are not supported.
511 /// See Variable Length Data in:
512 /// @link http://dwarfstd.org/Dwarf3.pdf @unlink
513 /// Also see @link http://refspecs.freestandards.org/abi-eh-1.21.html @unlink
514 /// @param resultAction reference variable which will be set with result
515 /// @param classInfo our array of type info pointers (to globals)
516 /// @param actionEntry index into above type info array or 0 (clean up).
517 /// We do not support filters.
518 /// @param exceptionClass exception class (_Unwind_Exception::exception_class)
519 /// of thrown exception.
520 /// @param exceptionObject thrown _Unwind_Exception instance.
521 /// @returns whether or not a type info was found. False is returned if only
522 /// a cleanup was found
523 static bool handleActionValue(int64_t *resultAction
,
524 struct OurExceptionType_t
**classInfo
,
525 uintptr_t actionEntry
,
526 uint64_t exceptionClass
,
527 struct _Unwind_Exception
*exceptionObject
) {
532 (exceptionClass
!= ourBaseExceptionClass
))
535 struct OurBaseException_t
*excp
= (struct OurBaseException_t
*)
536 (((char*) exceptionObject
) + ourBaseFromUnwindOffset
);
537 struct OurExceptionType_t
*excpType
= &(excp
->type
);
538 int type
= excpType
->type
;
542 "handleActionValue(...): exceptionObject = <%p>, "
548 const uint8_t *actionPos
= (uint8_t*) actionEntry
,
550 int64_t typeOffset
= 0,
553 for (int i
= 0; true; ++i
) {
554 // Each emitted dwarf action corresponds to a 2 tuple of
555 // type info address offset, and action offset to the next
557 typeOffset
= readSLEB128(&actionPos
);
558 tempActionPos
= actionPos
;
559 actionOffset
= readSLEB128(&tempActionPos
);
563 "handleActionValue(...):typeOffset: <%lld>, "
564 "actionOffset: <%lld>.\n",
568 assert((typeOffset
>= 0) &&
569 "handleActionValue(...):filters are not supported.");
571 // Note: A typeOffset == 0 implies that a cleanup llvm.eh.selector
572 // argument has been matched.
573 if ((typeOffset
> 0) &&
574 (type
== (classInfo
[-typeOffset
])->type
)) {
577 "handleActionValue(...):actionValue <%d> found.\n",
580 *resultAction
= i
+ 1;
587 "handleActionValue(...):actionValue not found.\n");
592 actionPos
+= actionOffset
;
599 /// Deals with the Language specific data portion of the emitted dwarf code.
600 /// See @link http://refspecs.freestandards.org/abi-eh-1.21.html @unlink
601 /// @param version unsupported (ignored), unwind version
602 /// @param lsda language specific data area
603 /// @param _Unwind_Action actions minimally supported unwind stage
604 /// (forced specifically not supported)
605 /// @param exceptionClass exception class (_Unwind_Exception::exception_class)
606 /// of thrown exception.
607 /// @param exceptionObject thrown _Unwind_Exception instance.
608 /// @param context unwind system context
609 /// @returns minimally supported unwinding control indicator
610 static _Unwind_Reason_Code
handleLsda(int version
,
612 _Unwind_Action actions
,
613 uint64_t exceptionClass
,
614 struct _Unwind_Exception
*exceptionObject
,
615 _Unwind_Context_t context
) {
616 _Unwind_Reason_Code ret
= _URC_CONTINUE_UNWIND
;
623 "handleLsda(...):lsda is non-zero.\n");
626 // Get the current instruction pointer and offset it before next
627 // instruction in the current frame which threw the exception.
628 uintptr_t pc
= _Unwind_GetIP(context
)-1;
630 // Get beginning current frame's code (as defined by the
631 // emitted dwarf code)
632 uintptr_t funcStart
= _Unwind_GetRegionStart(context
);
633 uintptr_t pcOffset
= pc
- funcStart
;
634 struct OurExceptionType_t
**classInfo
= NULL
;
636 // Note: See JITDwarfEmitter::EmitExceptionTable(...) for corresponding
639 // Parse LSDA header.
640 uint8_t lpStartEncoding
= *lsda
++;
642 if (lpStartEncoding
!= llvm::dwarf::DW_EH_PE_omit
) {
643 readEncodedPointer(&lsda
, lpStartEncoding
);
646 uint8_t ttypeEncoding
= *lsda
++;
647 uintptr_t classInfoOffset
;
649 if (ttypeEncoding
!= llvm::dwarf::DW_EH_PE_omit
) {
650 // Calculate type info locations in emitted dwarf code which
651 // were flagged by type info arguments to llvm.eh.selector
653 classInfoOffset
= readULEB128(&lsda
);
654 classInfo
= (struct OurExceptionType_t
**) (lsda
+ classInfoOffset
);
657 // Walk call-site table looking for range that
658 // includes current PC.
660 uint8_t callSiteEncoding
= *lsda
++;
661 uint32_t callSiteTableLength
= readULEB128(&lsda
);
662 const uint8_t *callSiteTableStart
= lsda
;
663 const uint8_t *callSiteTableEnd
= callSiteTableStart
+
665 const uint8_t *actionTableStart
= callSiteTableEnd
;
666 const uint8_t *callSitePtr
= callSiteTableStart
;
668 bool foreignException
= false;
670 while (callSitePtr
< callSiteTableEnd
) {
671 uintptr_t start
= readEncodedPointer(&callSitePtr
,
673 uintptr_t length
= readEncodedPointer(&callSitePtr
,
675 uintptr_t landingPad
= readEncodedPointer(&callSitePtr
,
678 // Note: Action value
679 uintptr_t actionEntry
= readULEB128(&callSitePtr
);
681 if (exceptionClass
!= ourBaseExceptionClass
) {
682 // We have been notified of a foreign exception being thrown,
683 // and we therefore need to execute cleanup landing pads
685 foreignException
= true;
688 if (landingPad
== 0) {
691 "handleLsda(...): No landing pad found.\n");
694 continue; // no landing pad for this entry
698 actionEntry
+= ((uintptr_t) actionTableStart
) - 1;
703 "handleLsda(...):No action table found.\n");
707 bool exceptionMatched
= false;
709 if ((start
<= pcOffset
) && (pcOffset
< (start
+ length
))) {
712 "handleLsda(...): Landing pad found.\n");
714 int64_t actionValue
= 0;
717 exceptionMatched
= handleActionValue(&actionValue
,
724 if (!(actions
& _UA_SEARCH_PHASE
)) {
727 "handleLsda(...): installed landing pad "
731 // Found landing pad for the PC.
732 // Set Instruction Pointer to so we re-enter function
733 // at landing pad. The landing pad is created by the
734 // compiler to take two parameters in registers.
735 _Unwind_SetGR(context
,
736 __builtin_eh_return_data_regno(0),
737 (uintptr_t)exceptionObject
);
739 // Note: this virtual register directly corresponds
740 // to the return of the llvm.eh.selector intrinsic
741 if (!actionEntry
|| !exceptionMatched
) {
742 // We indicate cleanup only
743 _Unwind_SetGR(context
,
744 __builtin_eh_return_data_regno(1),
748 // Matched type info index of llvm.eh.selector intrinsic
750 _Unwind_SetGR(context
,
751 __builtin_eh_return_data_regno(1),
755 // To execute landing pad set here
756 _Unwind_SetIP(context
, funcStart
+ landingPad
);
757 ret
= _URC_INSTALL_CONTEXT
;
759 else if (exceptionMatched
) {
762 "handleLsda(...): setting handler found.\n");
764 ret
= _URC_HANDLER_FOUND
;
767 // Note: Only non-clean up handlers are marked as
768 // found. Otherwise the clean up handlers will be
769 // re-found and executed during the clean up
773 "handleLsda(...): cleanup handler found.\n");
785 /// This is the personality function which is embedded (dwarf emitted), in the
786 /// dwarf unwind info block. Again see: JITDwarfEmitter.cpp.
787 /// See @link http://refspecs.freestandards.org/abi-eh-1.21.html @unlink
788 /// @param version unsupported (ignored), unwind version
789 /// @param _Unwind_Action actions minimally supported unwind stage
790 /// (forced specifically not supported)
791 /// @param exceptionClass exception class (_Unwind_Exception::exception_class)
792 /// of thrown exception.
793 /// @param exceptionObject thrown _Unwind_Exception instance.
794 /// @param context unwind system context
795 /// @returns minimally supported unwinding control indicator
796 _Unwind_Reason_Code
ourPersonality(int version
,
797 _Unwind_Action actions
,
798 uint64_t exceptionClass
,
799 struct _Unwind_Exception
*exceptionObject
,
800 _Unwind_Context_t context
) {
803 "We are in ourPersonality(...):actions is <%d>.\n",
806 if (actions
& _UA_SEARCH_PHASE
) {
807 fprintf(stderr
, "ourPersonality(...):In search phase.\n");
810 fprintf(stderr
, "ourPersonality(...):In non-search phase.\n");
814 const uint8_t *lsda
= _Unwind_GetLanguageSpecificData(context
);
818 "ourPersonality(...):lsda = <%p>.\n",
822 // The real work of the personality function is captured here
823 return(handleLsda(version
,
832 /// Generates our _Unwind_Exception class from a given character array.
833 /// thereby handling arbitrary lengths (not in standard), and handling
835 /// See @link http://refspecs.freestandards.org/abi-eh-1.21.html @unlink
836 /// @param classChars char array to encode. NULL values not checkedf
837 /// @param classCharsSize number of chars in classChars. Value is not checked.
838 /// @returns class value
839 uint64_t genClass(const unsigned char classChars
[], size_t classCharsSize
)
841 uint64_t ret
= classChars
[0];
843 for (unsigned i
= 1; i
< classCharsSize
; ++i
) {
845 ret
+= classChars
[i
];
854 // Runtime C Library functions End
858 // Code generation functions
861 /// Generates code to print given constant string
862 /// @param context llvm context
863 /// @param module code for module instance
864 /// @param builder builder instance
865 /// @param toPrint string to print
866 /// @param useGlobal A value of true (default) indicates a GlobalValue is
867 /// generated, and is used to hold the constant string. A value of
868 /// false indicates that the constant string will be stored on the
870 void generateStringPrint(llvm::LLVMContext
&context
,
871 llvm::Module
&module
,
872 llvm::IRBuilder
<> &builder
,
874 bool useGlobal
= true) {
875 llvm::Function
*printFunct
= module
.getFunction("printStr");
877 llvm::Value
*stringVar
;
878 llvm::Constant
*stringConstant
=
879 llvm::ConstantArray::get(context
, toPrint
);
882 // Note: Does not work without allocation
884 new llvm::GlobalVariable(module
,
885 stringConstant
->getType(),
887 llvm::GlobalValue::LinkerPrivateLinkage
,
892 stringVar
= builder
.CreateAlloca(stringConstant
->getType());
893 builder
.CreateStore(stringConstant
, stringVar
);
897 builder
.CreatePointerCast(stringVar
,
898 builder
.getInt8Ty()->getPointerTo());
899 builder
.CreateCall(printFunct
, cast
);
903 /// Generates code to print given runtime integer according to constant
904 /// string format, and a given print function.
905 /// @param context llvm context
906 /// @param module code for module instance
907 /// @param builder builder instance
908 /// @param printFunct function used to "print" integer
909 /// @param toPrint string to print
910 /// @param format printf like formating string for print
911 /// @param useGlobal A value of true (default) indicates a GlobalValue is
912 /// generated, and is used to hold the constant string. A value of
913 /// false indicates that the constant string will be stored on the
915 void generateIntegerPrint(llvm::LLVMContext
&context
,
916 llvm::Module
&module
,
917 llvm::IRBuilder
<> &builder
,
918 llvm::Function
&printFunct
,
919 llvm::Value
&toPrint
,
921 bool useGlobal
= true) {
922 llvm::Constant
*stringConstant
= llvm::ConstantArray::get(context
, format
);
923 llvm::Value
*stringVar
;
926 // Note: Does not seem to work without allocation
928 new llvm::GlobalVariable(module
,
929 stringConstant
->getType(),
931 llvm::GlobalValue::LinkerPrivateLinkage
,
936 stringVar
= builder
.CreateAlloca(stringConstant
->getType());
937 builder
.CreateStore(stringConstant
, stringVar
);
941 builder
.CreateBitCast(stringVar
,
942 builder
.getInt8Ty()->getPointerTo());
943 builder
.CreateCall2(&printFunct
, &toPrint
, cast
);
947 /// Generates code to handle finally block type semantics: always runs
948 /// regardless of whether a thrown exception is passing through or the
949 /// parent function is simply exiting. In addition to printing some state
950 /// to stderr, this code will resume the exception handling--runs the
951 /// unwind resume block, if the exception has not been previously caught
952 /// by a catch clause, and will otherwise execute the end block (terminator
953 /// block). In addition this function creates the corresponding function's
954 /// stack storage for the exception pointer and catch flag status.
955 /// @param context llvm context
956 /// @param module code for module instance
957 /// @param builder builder instance
958 /// @param toAddTo parent function to add block to
959 /// @param blockName block name of new "finally" block.
960 /// @param functionId output id used for printing
961 /// @param terminatorBlock terminator "end" block
962 /// @param unwindResumeBlock unwind resume block
963 /// @param exceptionCaughtFlag reference exception caught/thrown status storage
964 /// @param exceptionStorage reference to exception pointer storage
965 /// @returns newly created block
966 static llvm::BasicBlock
*createFinallyBlock(llvm::LLVMContext
&context
,
967 llvm::Module
&module
,
968 llvm::IRBuilder
<> &builder
,
969 llvm::Function
&toAddTo
,
970 std::string
&blockName
,
971 std::string
&functionId
,
972 llvm::BasicBlock
&terminatorBlock
,
973 llvm::BasicBlock
&unwindResumeBlock
,
974 llvm::Value
**exceptionCaughtFlag
,
975 llvm::Value
**exceptionStorage
) {
976 assert(exceptionCaughtFlag
&&
977 "ExceptionDemo::createFinallyBlock(...):exceptionCaughtFlag "
979 assert(exceptionStorage
&&
980 "ExceptionDemo::createFinallyBlock(...):exceptionStorage "
983 *exceptionCaughtFlag
=
984 createEntryBlockAlloca(toAddTo
,
986 ourExceptionNotThrownState
->getType(),
987 ourExceptionNotThrownState
);
989 const llvm::PointerType
*exceptionStorageType
=
990 builder
.getInt8Ty()->getPointerTo();
992 createEntryBlockAlloca(toAddTo
,
994 exceptionStorageType
,
995 llvm::ConstantPointerNull::get(
996 exceptionStorageType
));
998 llvm::BasicBlock
*ret
= llvm::BasicBlock::Create(context
,
1002 builder
.SetInsertPoint(ret
);
1004 std::ostringstream bufferToPrint
;
1005 bufferToPrint
<< "Gen: Executing finally block "
1006 << blockName
<< " in " << functionId
<< "\n";
1007 generateStringPrint(context
,
1010 bufferToPrint
.str(),
1011 USE_GLOBAL_STR_CONSTS
);
1013 llvm::SwitchInst
*theSwitch
=
1014 builder
.CreateSwitch(builder
.CreateLoad(*exceptionCaughtFlag
),
1017 theSwitch
->addCase(ourExceptionCaughtState
, &terminatorBlock
);
1018 theSwitch
->addCase(ourExceptionThrownState
, &unwindResumeBlock
);
1024 /// Generates catch block semantics which print a string to indicate type of
1025 /// catch executed, sets an exception caught flag, and executes passed in
1026 /// end block (terminator block).
1027 /// @param context llvm context
1028 /// @param module code for module instance
1029 /// @param builder builder instance
1030 /// @param toAddTo parent function to add block to
1031 /// @param blockName block name of new "catch" block.
1032 /// @param functionId output id used for printing
1033 /// @param terminatorBlock terminator "end" block
1034 /// @param exceptionCaughtFlag exception caught/thrown status
1035 /// @returns newly created block
1036 static llvm::BasicBlock
*createCatchBlock(llvm::LLVMContext
&context
,
1037 llvm::Module
&module
,
1038 llvm::IRBuilder
<> &builder
,
1039 llvm::Function
&toAddTo
,
1040 std::string
&blockName
,
1041 std::string
&functionId
,
1042 llvm::BasicBlock
&terminatorBlock
,
1043 llvm::Value
&exceptionCaughtFlag
) {
1045 llvm::BasicBlock
*ret
= llvm::BasicBlock::Create(context
,
1049 builder
.SetInsertPoint(ret
);
1051 std::ostringstream bufferToPrint
;
1052 bufferToPrint
<< "Gen: Executing catch block "
1057 generateStringPrint(context
,
1060 bufferToPrint
.str(),
1061 USE_GLOBAL_STR_CONSTS
);
1062 builder
.CreateStore(ourExceptionCaughtState
, &exceptionCaughtFlag
);
1063 builder
.CreateBr(&terminatorBlock
);
1069 /// Generates a function which invokes a function (toInvoke) and, whose
1070 /// unwind block will "catch" the type info types correspondingly held in the
1071 /// exceptionTypesToCatch argument. If the toInvoke function throws an
1072 /// exception which does not match any type info types contained in
1073 /// exceptionTypesToCatch, the generated code will call _Unwind_Resume
1074 /// with the raised exception. On the other hand the generated code will
1075 /// normally exit if the toInvoke function does not throw an exception.
1076 /// The generated "finally" block is always run regardless of the cause of
1077 /// the generated function exit.
1078 /// The generated function is returned after being verified.
1079 /// @param module code for module instance
1080 /// @param builder builder instance
1081 /// @param fpm a function pass manager holding optional IR to IR
1083 /// @param toInvoke inner function to invoke
1084 /// @param ourId id used to printing purposes
1085 /// @param numExceptionsToCatch length of exceptionTypesToCatch array
1086 /// @param exceptionTypesToCatch array of type info types to "catch"
1087 /// @returns generated function
1089 llvm::Function
*createCatchWrappedInvokeFunction(llvm::Module
&module
,
1090 llvm::IRBuilder
<> &builder
,
1091 llvm::FunctionPassManager
&fpm
,
1092 llvm::Function
&toInvoke
,
1094 unsigned numExceptionsToCatch
,
1095 unsigned exceptionTypesToCatch
[]) {
1097 llvm::LLVMContext
&context
= module
.getContext();
1098 llvm::Function
*toPrint32Int
= module
.getFunction("print32Int");
1101 argTypes
.push_back(builder
.getInt32Ty());
1104 argNames
.push_back("exceptTypeToThrow");
1106 llvm::Function
*ret
= createFunction(module
,
1107 builder
.getVoidTy(),
1111 llvm::Function::ExternalLinkage
,
1115 // Block which calls invoke
1116 llvm::BasicBlock
*entryBlock
= llvm::BasicBlock::Create(context
,
1119 // Normal block for invoke
1120 llvm::BasicBlock
*normalBlock
= llvm::BasicBlock::Create(context
,
1123 // Unwind block for invoke
1124 llvm::BasicBlock
*exceptionBlock
=
1125 llvm::BasicBlock::Create(context
, "exception", ret
);
1127 // Block which routes exception to correct catch handler block
1128 llvm::BasicBlock
*exceptionRouteBlock
=
1129 llvm::BasicBlock::Create(context
, "exceptionRoute", ret
);
1131 // Foreign exception handler
1132 llvm::BasicBlock
*externalExceptionBlock
=
1133 llvm::BasicBlock::Create(context
, "externalException", ret
);
1135 // Block which calls _Unwind_Resume
1136 llvm::BasicBlock
*unwindResumeBlock
=
1137 llvm::BasicBlock::Create(context
, "unwindResume", ret
);
1139 // Clean up block which delete exception if needed
1140 llvm::BasicBlock
*endBlock
=
1141 llvm::BasicBlock::Create(context
, "end", ret
);
1143 std::string nextName
;
1144 std::vector
<llvm::BasicBlock
*> catchBlocks(numExceptionsToCatch
);
1145 llvm::Value
*exceptionCaughtFlag
= NULL
;
1146 llvm::Value
*exceptionStorage
= NULL
;
1148 // Finally block which will branch to unwindResumeBlock if
1149 // exception is not caught. Initializes/allocates stack locations.
1150 llvm::BasicBlock
*finallyBlock
= createFinallyBlock(context
,
1154 nextName
= "finally",
1158 &exceptionCaughtFlag
,
1161 for (unsigned i
= 0; i
< numExceptionsToCatch
; ++i
) {
1162 nextName
= ourTypeInfoNames
[exceptionTypesToCatch
[i
]];
1164 // One catch block per type info to be caught
1165 catchBlocks
[i
] = createCatchBlock(context
,
1172 *exceptionCaughtFlag
);
1177 builder
.SetInsertPoint(entryBlock
);
1179 std::vector
<llvm::Value
*> args
;
1180 args
.push_back(namedValues
["exceptTypeToThrow"]);
1181 builder
.CreateInvoke(&toInvoke
,
1189 builder
.SetInsertPoint(endBlock
);
1191 generateStringPrint(context
,
1194 "Gen: In end block: exiting in " + ourId
+ ".\n",
1195 USE_GLOBAL_STR_CONSTS
);
1196 llvm::Function
*deleteOurException
=
1197 module
.getFunction("deleteOurException");
1199 // Note: function handles NULL exceptions
1200 builder
.CreateCall(deleteOurException
,
1201 builder
.CreateLoad(exceptionStorage
));
1202 builder
.CreateRetVoid();
1206 builder
.SetInsertPoint(normalBlock
);
1208 generateStringPrint(context
,
1211 "Gen: No exception in " + ourId
+ "!\n",
1212 USE_GLOBAL_STR_CONSTS
);
1214 // Finally block is always called
1215 builder
.CreateBr(finallyBlock
);
1217 // Unwind Resume Block
1219 builder
.SetInsertPoint(unwindResumeBlock
);
1221 llvm::Function
*resumeOurException
=
1222 module
.getFunction("_Unwind_Resume");
1223 builder
.CreateCall(resumeOurException
,
1224 builder
.CreateLoad(exceptionStorage
));
1225 builder
.CreateUnreachable();
1229 builder
.SetInsertPoint(exceptionBlock
);
1231 llvm::Function
*ehException
= module
.getFunction("llvm.eh.exception");
1233 // Retrieve thrown exception
1234 llvm::Value
*unwindException
= builder
.CreateCall(ehException
);
1236 // Store exception and flag
1237 builder
.CreateStore(unwindException
, exceptionStorage
);
1238 builder
.CreateStore(ourExceptionThrownState
, exceptionCaughtFlag
);
1239 llvm::Function
*personality
= module
.getFunction("ourPersonality");
1240 llvm::Value
*functPtr
=
1241 builder
.CreatePointerCast(personality
,
1242 builder
.getInt8Ty()->getPointerTo());
1245 args
.push_back(unwindException
);
1246 args
.push_back(functPtr
);
1248 // Note: Skipping index 0
1249 for (unsigned i
= 0; i
< numExceptionsToCatch
; ++i
) {
1250 // Set up type infos to be caught
1251 args
.push_back(module
.getGlobalVariable(
1252 ourTypeInfoNames
[exceptionTypesToCatch
[i
]]));
1255 args
.push_back(llvm::ConstantInt::get(builder
.getInt32Ty(), 0));
1257 llvm::Function
*ehSelector
= module
.getFunction("llvm.eh.selector");
1259 // Set up this exeption block as the landing pad which will handle
1260 // given type infos. See case Intrinsic::eh_selector in
1261 // SelectionDAGBuilder::visitIntrinsicCall(...) and AddCatchInfo(...)
1262 // implemented in FunctionLoweringInfo.cpp to see how the implementation
1263 // handles this call. This landing pad (this exception block), will be
1264 // called either because it nees to cleanup (call finally) or a type
1265 // info was found which matched the thrown exception.
1266 llvm::Value
*retTypeInfoIndex
= builder
.CreateCall(ehSelector
,
1270 // Retrieve exception_class member from thrown exception
1271 // (_Unwind_Exception instance). This member tells us whether or not
1272 // the exception is foreign.
1273 llvm::Value
*unwindExceptionClass
=
1274 builder
.CreateLoad(builder
.CreateStructGEP(
1275 builder
.CreatePointerCast(unwindException
,
1276 ourUnwindExceptionType
->getPointerTo()),
1279 // Branch to the externalExceptionBlock if the exception is foreign or
1280 // to a catch router if not. Either way the finally block will be run.
1281 builder
.CreateCondBr(builder
.CreateICmpEQ(unwindExceptionClass
,
1282 llvm::ConstantInt::get(builder
.getInt64Ty(),
1283 ourBaseExceptionClass
)),
1284 exceptionRouteBlock
,
1285 externalExceptionBlock
);
1287 // External Exception Block
1289 builder
.SetInsertPoint(externalExceptionBlock
);
1291 generateStringPrint(context
,
1294 "Gen: Foreign exception received.\n",
1295 USE_GLOBAL_STR_CONSTS
);
1297 // Branch to the finally block
1298 builder
.CreateBr(finallyBlock
);
1300 // Exception Route Block
1302 builder
.SetInsertPoint(exceptionRouteBlock
);
1304 // Casts exception pointer (_Unwind_Exception instance) to parent
1305 // (OurException instance).
1307 // Note: ourBaseFromUnwindOffset is usually negative
1308 llvm::Value
*typeInfoThrown
=
1309 builder
.CreatePointerCast(builder
.CreateConstGEP1_64(unwindException
,
1310 ourBaseFromUnwindOffset
),
1311 ourExceptionType
->getPointerTo());
1313 // Retrieve thrown exception type info type
1315 // Note: Index is not relative to pointer but instead to structure
1316 // unlike a true getelementptr (GEP) instruction
1317 typeInfoThrown
= builder
.CreateStructGEP(typeInfoThrown
, 0);
1319 llvm::Value
*typeInfoThrownType
=
1320 builder
.CreateStructGEP(typeInfoThrown
, 0);
1322 generateIntegerPrint(context
,
1326 *(builder
.CreateLoad(typeInfoThrownType
)),
1327 "Gen: Exception type <%d> received (stack unwound) "
1331 USE_GLOBAL_STR_CONSTS
);
1333 // Route to matched type info catch block or run cleanup finally block
1334 llvm::SwitchInst
*switchToCatchBlock
=
1335 builder
.CreateSwitch(retTypeInfoIndex
,
1337 numExceptionsToCatch
);
1339 unsigned nextTypeToCatch
;
1341 for (unsigned i
= 1; i
<= numExceptionsToCatch
; ++i
) {
1342 nextTypeToCatch
= i
- 1;
1343 switchToCatchBlock
->addCase(llvm::ConstantInt::get(
1344 llvm::Type::getInt32Ty(context
), i
),
1345 catchBlocks
[nextTypeToCatch
]);
1348 llvm::verifyFunction(*ret
);
1355 /// Generates function which throws either an exception matched to a runtime
1356 /// determined type info type (argument to generated function), or if this
1357 /// runtime value matches nativeThrowType, throws a foreign exception by
1358 /// calling nativeThrowFunct.
1359 /// @param module code for module instance
1360 /// @param builder builder instance
1361 /// @param fpm a function pass manager holding optional IR to IR
1363 /// @param ourId id used to printing purposes
1364 /// @param nativeThrowType a runtime argument of this value results in
1365 /// nativeThrowFunct being called to generate/throw exception.
1366 /// @param nativeThrowFunct function which will throw a foreign exception
1367 /// if the above nativeThrowType matches generated function's arg.
1368 /// @returns generated function
1370 llvm::Function
*createThrowExceptionFunction(llvm::Module
&module
,
1371 llvm::IRBuilder
<> &builder
,
1372 llvm::FunctionPassManager
&fpm
,
1374 int32_t nativeThrowType
,
1375 llvm::Function
&nativeThrowFunct
) {
1376 llvm::LLVMContext
&context
= module
.getContext();
1377 namedValues
.clear();
1378 ArgTypes unwindArgTypes
;
1379 unwindArgTypes
.push_back(builder
.getInt32Ty());
1380 ArgNames unwindArgNames
;
1381 unwindArgNames
.push_back("exceptTypeToThrow");
1383 llvm::Function
*ret
= createFunction(module
,
1384 builder
.getVoidTy(),
1388 llvm::Function::ExternalLinkage
,
1392 // Throws either one of our exception or a native C++ exception depending
1393 // on a runtime argument value containing a type info type.
1394 llvm::BasicBlock
*entryBlock
= llvm::BasicBlock::Create(context
,
1397 // Throws a foreign exception
1398 llvm::BasicBlock
*nativeThrowBlock
=
1399 llvm::BasicBlock::Create(context
,
1402 // Throws one of our Exceptions
1403 llvm::BasicBlock
*generatedThrowBlock
=
1404 llvm::BasicBlock::Create(context
,
1407 // Retrieved runtime type info type to throw
1408 llvm::Value
*exceptionType
= namedValues
["exceptTypeToThrow"];
1410 // nativeThrowBlock block
1412 builder
.SetInsertPoint(nativeThrowBlock
);
1414 // Throws foreign exception
1415 builder
.CreateCall(&nativeThrowFunct
, exceptionType
);
1416 builder
.CreateUnreachable();
1420 builder
.SetInsertPoint(entryBlock
);
1422 llvm::Function
*toPrint32Int
= module
.getFunction("print32Int");
1423 generateIntegerPrint(context
,
1428 "\nGen: About to throw exception type <%d> in " +
1431 USE_GLOBAL_STR_CONSTS
);
1433 // Switches on runtime type info type value to determine whether or not
1434 // a foreign exception is thrown. Defaults to throwing one of our
1435 // generated exceptions.
1436 llvm::SwitchInst
*theSwitch
= builder
.CreateSwitch(exceptionType
,
1437 generatedThrowBlock
,
1440 theSwitch
->addCase(llvm::ConstantInt::get(llvm::Type::getInt32Ty(context
),
1444 // generatedThrow block
1446 builder
.SetInsertPoint(generatedThrowBlock
);
1448 llvm::Function
*createOurException
=
1449 module
.getFunction("createOurException");
1450 llvm::Function
*raiseOurException
=
1451 module
.getFunction("_Unwind_RaiseException");
1453 // Creates exception to throw with runtime type info type.
1454 llvm::Value
*exception
=
1455 builder
.CreateCall(createOurException
,
1456 namedValues
["exceptTypeToThrow"]);
1458 // Throw generated Exception
1459 builder
.CreateCall(raiseOurException
, exception
);
1460 builder
.CreateUnreachable();
1462 llvm::verifyFunction(*ret
);
1468 static void createStandardUtilityFunctions(unsigned numTypeInfos
,
1469 llvm::Module
&module
,
1470 llvm::IRBuilder
<> &builder
);
1472 /// Creates test code by generating and organizing these functions into the
1473 /// test case. The test case consists of an outer function setup to invoke
1474 /// an inner function within an environment having multiple catch and single
1475 /// finally blocks. This inner function is also setup to invoke a throw
1476 /// function within an evironment similar in nature to the outer function's
1477 /// catch and finally blocks. Each of these two functions catch mutually
1478 /// exclusive subsets (even or odd) of the type info types configured
1479 /// for this this. All generated functions have a runtime argument which
1480 /// holds a type info type to throw that each function takes and passes it
1481 /// to the inner one if such a inner function exists. This type info type is
1482 /// looked at by the generated throw function to see whether or not it should
1483 /// throw a generated exception with the same type info type, or instead call
1484 /// a supplied a function which in turn will throw a foreign exception.
1485 /// @param module code for module instance
1486 /// @param builder builder instance
1487 /// @param fpm a function pass manager holding optional IR to IR
1489 /// @param nativeThrowFunctName name of external function which will throw
1490 /// a foreign exception
1491 /// @returns outermost generated test function.
1492 llvm::Function
*createUnwindExceptionTest(llvm::Module
&module
,
1493 llvm::IRBuilder
<> &builder
,
1494 llvm::FunctionPassManager
&fpm
,
1495 std::string nativeThrowFunctName
) {
1496 // Number of type infos to generate
1497 unsigned numTypeInfos
= 6;
1499 // Initialze intrisics and external functions to use along with exception
1500 // and type info globals.
1501 createStandardUtilityFunctions(numTypeInfos
,
1504 llvm::Function
*nativeThrowFunct
=
1505 module
.getFunction(nativeThrowFunctName
);
1507 // Create exception throw function using the value ~0 to cause
1508 // foreign exceptions to be thrown.
1509 llvm::Function
*throwFunct
=
1510 createThrowExceptionFunction(module
,
1516 // Inner function will catch even type infos
1517 unsigned innerExceptionTypesToCatch
[] = {6, 2, 4};
1518 size_t numExceptionTypesToCatch
= sizeof(innerExceptionTypesToCatch
) /
1521 // Generate inner function.
1522 llvm::Function
*innerCatchFunct
=
1523 createCatchWrappedInvokeFunction(module
,
1528 numExceptionTypesToCatch
,
1529 innerExceptionTypesToCatch
);
1531 // Outer function will catch odd type infos
1532 unsigned outerExceptionTypesToCatch
[] = {3, 1, 5};
1533 numExceptionTypesToCatch
= sizeof(outerExceptionTypesToCatch
) /
1536 // Generate outer function
1537 llvm::Function
*outerCatchFunct
=
1538 createCatchWrappedInvokeFunction(module
,
1543 numExceptionTypesToCatch
,
1544 outerExceptionTypesToCatch
);
1546 // Return outer function to run
1547 return(outerCatchFunct
);
1551 /// Represents our foreign exceptions
1552 class OurCppRunException
: public std::runtime_error
{
1554 OurCppRunException(const std::string reason
) :
1555 std::runtime_error(reason
) {}
1557 OurCppRunException (const OurCppRunException
&toCopy
) :
1558 std::runtime_error(toCopy
) {}
1560 OurCppRunException
&operator = (const OurCppRunException
&toCopy
) {
1561 return(reinterpret_cast<OurCppRunException
&>(
1562 std::runtime_error::operator=(toCopy
)));
1565 ~OurCppRunException (void) throw () {}
1569 /// Throws foreign C++ exception.
1570 /// @param ignoreIt unused parameter that allows function to match implied
1571 /// generated function contract.
1573 void throwCppException (int32_t ignoreIt
) {
1574 throw(OurCppRunException("thrown by throwCppException(...)"));
1577 typedef void (*OurExceptionThrowFunctType
) (int32_t typeToThrow
);
1579 /// This is a test harness which runs test by executing generated
1580 /// function with a type info type to throw. Harness wraps the execution
1581 /// of generated function in a C++ try catch clause.
1582 /// @param engine execution engine to use for executing generated function.
1583 /// This demo program expects this to be a JIT instance for demo
1585 /// @param function generated test function to run
1586 /// @param typeToThrow type info type of generated exception to throw, or
1587 /// indicator to cause foreign exception to be thrown.
1589 void runExceptionThrow(llvm::ExecutionEngine
*engine
,
1590 llvm::Function
*function
,
1591 int32_t typeToThrow
) {
1593 // Find test's function pointer
1594 OurExceptionThrowFunctType functPtr
=
1595 reinterpret_cast<OurExceptionThrowFunctType
>(
1596 reinterpret_cast<intptr_t>(engine
->getPointerToFunction(function
)));
1600 (*functPtr
)(typeToThrow
);
1602 catch (OurCppRunException exc
) {
1603 // Catch foreign C++ exception
1605 "\nrunExceptionThrow(...):In C++ catch OurCppRunException "
1606 "with reason: %s.\n",
1610 // Catch all exceptions including our generated ones. I'm not sure
1611 // why this latter functionality should work, as it seems that
1612 // our exceptions should be foreign to C++ (the _Unwind_Exception::
1613 // exception_class should be different from the one used by C++), and
1614 // therefore C++ should ignore the generated exceptions.
1617 "\nrunExceptionThrow(...):In C++ catch all.\n");
1622 // End test functions
1625 typedef llvm::ArrayRef
<const llvm::Type
*> TypeArray
;
1627 /// This initialization routine creates type info globals and
1628 /// adds external function declarations to module.
1629 /// @param numTypeInfos number of linear type info associated type info types
1630 /// to create as GlobalVariable instances, starting with the value 1.
1631 /// @param module code for module instance
1632 /// @param builder builder instance
1633 static void createStandardUtilityFunctions(unsigned numTypeInfos
,
1634 llvm::Module
&module
,
1635 llvm::IRBuilder
<> &builder
) {
1637 llvm::LLVMContext
&context
= module
.getContext();
1639 // Exception initializations
1641 // Setup exception catch state
1642 ourExceptionNotThrownState
=
1643 llvm::ConstantInt::get(llvm::Type::getInt8Ty(context
), 0),
1644 ourExceptionThrownState
=
1645 llvm::ConstantInt::get(llvm::Type::getInt8Ty(context
), 1),
1646 ourExceptionCaughtState
=
1647 llvm::ConstantInt::get(llvm::Type::getInt8Ty(context
), 2),
1651 // Create our type info type
1652 ourTypeInfoType
= llvm::StructType::get(context
,
1653 TypeArray(builder
.getInt32Ty()));
1655 // Create OurException type
1656 ourExceptionType
= llvm::StructType::get(context
,
1657 TypeArray(ourTypeInfoType
));
1659 // Create portion of _Unwind_Exception type
1661 // Note: Declaring only a portion of the _Unwind_Exception struct.
1662 // Does this cause problems?
1663 ourUnwindExceptionType
=
1664 llvm::StructType::get(context
, TypeArray(builder
.getInt64Ty()));
1665 struct OurBaseException_t dummyException
;
1667 // Calculate offset of OurException::unwindException member.
1668 ourBaseFromUnwindOffset
= ((uintptr_t) &dummyException
) -
1669 ((uintptr_t) &(dummyException
.unwindException
));
1673 "createStandardUtilityFunctions(...):ourBaseFromUnwindOffset "
1674 "= %lld, sizeof(struct OurBaseException_t) - "
1675 "sizeof(struct _Unwind_Exception) = %lu.\n",
1676 ourBaseFromUnwindOffset
,
1677 sizeof(struct OurBaseException_t
) -
1678 sizeof(struct _Unwind_Exception
));
1681 size_t numChars
= sizeof(ourBaseExcpClassChars
) / sizeof(char);
1683 // Create our _Unwind_Exception::exception_class value
1684 ourBaseExceptionClass
= genClass(ourBaseExcpClassChars
, numChars
);
1688 std::string baseStr
= "typeInfo", typeInfoName
;
1689 std::ostringstream typeInfoNameBuilder
;
1690 std::vector
<llvm::Constant
*> structVals
;
1692 llvm::Constant
*nextStruct
;
1693 llvm::GlobalVariable
*nextGlobal
= NULL
;
1695 // Generate each type info
1697 // Note: First type info is not used.
1698 for (unsigned i
= 0; i
<= numTypeInfos
; ++i
) {
1700 structVals
.push_back(llvm::ConstantInt::get(builder
.getInt32Ty(), i
));
1701 nextStruct
= llvm::ConstantStruct::get(ourTypeInfoType
, structVals
);
1703 typeInfoNameBuilder
.str("");
1704 typeInfoNameBuilder
<< baseStr
<< i
;
1705 typeInfoName
= typeInfoNameBuilder
.str();
1707 // Note: Does not seem to work without allocation
1709 new llvm::GlobalVariable(module
,
1712 llvm::GlobalValue::ExternalLinkage
,
1716 ourTypeInfoNames
.push_back(typeInfoName
);
1717 ourTypeInfoNamesIndex
[i
] = typeInfoName
;
1722 llvm::Function
*funct
= NULL
;
1726 const llvm::Type
*retType
= builder
.getVoidTy();
1729 argTypes
.push_back(builder
.getInt32Ty());
1730 argTypes
.push_back(builder
.getInt8Ty()->getPointerTo());
1734 createFunction(module
,
1739 llvm::Function::ExternalLinkage
,
1745 retType
= builder
.getVoidTy();
1748 argTypes
.push_back(builder
.getInt64Ty());
1749 argTypes
.push_back(builder
.getInt8Ty()->getPointerTo());
1753 createFunction(module
,
1758 llvm::Function::ExternalLinkage
,
1764 retType
= builder
.getVoidTy();
1767 argTypes
.push_back(builder
.getInt8Ty()->getPointerTo());
1771 createFunction(module
,
1776 llvm::Function::ExternalLinkage
,
1780 // throwCppException
1782 retType
= builder
.getVoidTy();
1785 argTypes
.push_back(builder
.getInt32Ty());
1789 createFunction(module
,
1793 "throwCppException",
1794 llvm::Function::ExternalLinkage
,
1798 // deleteOurException
1800 retType
= builder
.getVoidTy();
1803 argTypes
.push_back(builder
.getInt8Ty()->getPointerTo());
1807 createFunction(module
,
1811 "deleteOurException",
1812 llvm::Function::ExternalLinkage
,
1816 // createOurException
1818 retType
= builder
.getInt8Ty()->getPointerTo();
1821 argTypes
.push_back(builder
.getInt32Ty());
1825 createFunction(module
,
1829 "createOurException",
1830 llvm::Function::ExternalLinkage
,
1834 // _Unwind_RaiseException
1836 retType
= builder
.getInt32Ty();
1839 argTypes
.push_back(builder
.getInt8Ty()->getPointerTo());
1843 funct
= createFunction(module
,
1847 "_Unwind_RaiseException",
1848 llvm::Function::ExternalLinkage
,
1852 funct
->addFnAttr(llvm::Attribute::NoReturn
);
1856 retType
= builder
.getInt32Ty();
1859 argTypes
.push_back(builder
.getInt8Ty()->getPointerTo());
1863 funct
= createFunction(module
,
1868 llvm::Function::ExternalLinkage
,
1872 funct
->addFnAttr(llvm::Attribute::NoReturn
);
1876 retType
= builder
.getInt32Ty();
1879 argTypes
.push_back(builder
.getInt32Ty());
1880 argTypes
.push_back(builder
.getInt32Ty());
1881 argTypes
.push_back(builder
.getInt64Ty());
1882 argTypes
.push_back(builder
.getInt8Ty()->getPointerTo());
1883 argTypes
.push_back(builder
.getInt8Ty()->getPointerTo());
1887 createFunction(module
,
1892 llvm::Function::ExternalLinkage
,
1896 // llvm.eh.selector intrinsic
1898 getDeclaration(&module
, llvm::Intrinsic::eh_selector
);
1900 // llvm.eh.exception intrinsic
1902 getDeclaration(&module
, llvm::Intrinsic::eh_exception
);
1904 // llvm.eh.typeid.for intrinsic
1906 getDeclaration(&module
, llvm::Intrinsic::eh_typeid_for
);
1910 //===----------------------------------------------------------------------===//
1911 // Main test driver code.
1912 //===----------------------------------------------------------------------===//
1914 /// Demo main routine which takes the type info types to throw. A test will
1915 /// be run for each given type info type. While type info types with the value
1916 /// of -1 will trigger a foreign C++ exception to be thrown; type info types
1917 /// <= 6 and >= 1 will be caught by test functions; and type info types > 6
1918 /// will result in exceptions which pass through to the test harness. All other
1919 /// type info types are not supported and could cause a crash.
1920 int main(int argc
, char *argv
[]) {
1923 "\nUsage: ExceptionDemo <exception type to throw> "
1924 "[<type 2>...<type n>].\n"
1925 " Each type must have the value of 1 - 6 for "
1926 "generated exceptions to be caught;\n"
1927 " the value -1 for foreign C++ exceptions to be "
1928 "generated and thrown;\n"
1929 " or the values > 6 for exceptions to be ignored.\n"
1930 "\nTry: ExceptionDemo 2 3 7 -1\n"
1931 " for a full test.\n\n");
1935 // If not set, exception handling will not be turned on
1936 llvm::JITExceptionHandling
= true;
1938 llvm::InitializeNativeTarget();
1939 llvm::LLVMContext
&context
= llvm::getGlobalContext();
1940 llvm::IRBuilder
<> theBuilder(context
);
1942 // Make the module, which holds all the code.
1943 llvm::Module
*module
= new llvm::Module("my cool jit", context
);
1945 // Build engine with JIT
1946 llvm::EngineBuilder
factory(module
);
1947 factory
.setEngineKind(llvm::EngineKind::JIT
);
1948 factory
.setAllocateGVsWithCode(false);
1949 llvm::ExecutionEngine
*executionEngine
= factory
.create();
1952 llvm::FunctionPassManager
fpm(module
);
1954 // Set up the optimizer pipeline.
1955 // Start with registering info about how the
1956 // target lays out data structures.
1957 fpm
.add(new llvm::TargetData(*executionEngine
->getTargetData()));
1959 // Optimizations turned on
1960 #ifdef ADD_OPT_PASSES
1962 // Basic AliasAnslysis support for GVN.
1963 fpm
.add(llvm::createBasicAliasAnalysisPass());
1965 // Promote allocas to registers.
1966 fpm
.add(llvm::createPromoteMemoryToRegisterPass());
1968 // Do simple "peephole" optimizations and bit-twiddling optzns.
1969 fpm
.add(llvm::createInstructionCombiningPass());
1971 // Reassociate expressions.
1972 fpm
.add(llvm::createReassociatePass());
1974 // Eliminate Common SubExpressions.
1975 fpm
.add(llvm::createGVNPass());
1977 // Simplify the control flow graph (deleting unreachable
1979 fpm
.add(llvm::createCFGSimplificationPass());
1980 #endif // ADD_OPT_PASSES
1982 fpm
.doInitialization();
1984 // Generate test code using function throwCppException(...) as
1985 // the function which throws foreign exceptions.
1986 llvm::Function
*toRun
=
1987 createUnwindExceptionTest(*module
,
1990 "throwCppException");
1992 fprintf(stderr
, "\nBegin module dump:\n\n");
1996 fprintf(stderr
, "\nEnd module dump:\n");
1998 fprintf(stderr
, "\n\nBegin Test:\n");
2000 for (int i
= 1; i
< argc
; ++i
) {
2001 // Run test for each argument whose value is the exception
2003 runExceptionThrow(executionEngine
,
2005 (unsigned) strtoul(argv
[i
], NULL
, 10));
2008 fprintf(stderr
, "\nEnd Test:\n\n");
2011 delete executionEngine
;