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