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