blob: 444ee2649fa734f2c700d55c61a85a66948345ae [file] [log] [blame]
Chris Lattnera868bbb2011-04-08 18:02:51 +00001//===-- ExceptionDemo.cpp - An example using llvm Exceptions --------------===//
Garrison Vennf4d2f842010-02-09 23:22:43 +00002//
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 Lattnera868bbb2011-04-08 18:02:51 +00008//===----------------------------------------------------------------------===//
Garrison Vennf4d2f842010-02-09 23:22:43 +00009//
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 Takumi8e70dd52012-10-12 14:11:43 +000013// be run for each given type info type. While type info types with the value
Garrison Vennf4d2f842010-02-09 23:22:43 +000014// of -1 will trigger a foreign C++ exception to be thrown; type info types
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +000015// <= 6 and >= 1 will cause the associated generated exceptions to be thrown
Garrison Vennf4d2f842010-02-09 23:22:43 +000016// 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 Takumi8e70dd52012-10-12 14:11:43 +000019// the "finally" blocks of every generated test functions will executed
Garrison Vennf4d2f842010-02-09 23:22:43 +000020// 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 Takumi8e70dd52012-10-12 14:11:43 +000028//
Garrison Vennf4d2f842010-02-09 23:22:43 +000029// ExceptionDemo 2 3 7 -1
30//
31// results in the following cases:
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +000032// - Value 2 causes an exception with a type info type of 2 to be
Garrison Vennf4d2f842010-02-09 23:22:43 +000033// thrown and caught by an inner generated test function.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +000034// - Value 3 causes an exception with a type info type of 3 to be
Garrison Vennf4d2f842010-02-09 23:22:43 +000035// thrown and caught by an outer generated test function.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +000036// - Value 7 causes an exception with a type info type of 7 to be
Garrison Vennf4d2f842010-02-09 23:22:43 +000037// 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 Takumi8e70dd52012-10-12 14:11:43 +000042// 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
Dmitri Gribenko462462e2013-01-13 15:53:09 +000044// http://mentorembedded.github.com/cxx-abi/abi-eh.html (v1.22)
Garrison Vennf4d2f842010-02-09 23:22:43 +000045//
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +000046// This code uses code from the llvm compiler-rt project and the llvm
Garrison Vennf4d2f842010-02-09 23:22:43 +000047// Kaleidoscope project.
48//
Chris Lattnera868bbb2011-04-08 18:02:51 +000049//===----------------------------------------------------------------------===//
Garrison Vennf4d2f842010-02-09 23:22:43 +000050
NAKAMURA Takumi85c9bac2015-03-02 01:04:34 +000051#include "llvm/ADT/STLExtras.h"
Chandler Carruthd7cd9ac92014-01-13 09:53:45 +000052#include "llvm/IR/Verifier.h"
Rafael Espindolae93dc3b2013-05-05 20:57:58 +000053#include "llvm/ExecutionEngine/MCJIT.h"
54#include "llvm/ExecutionEngine/SectionMemoryManager.h"
Chandler Carruth005f27a2013-01-02 11:56:33 +000055#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 Carruth30d69c22015-02-13 10:01:29 +000060#include "llvm/IR/LegacyPassManager.h"
Chandler Carruth005f27a2013-01-02 11:56:33 +000061#include "llvm/IR/Module.h"
Garrison Vennf4d2f842010-02-09 23:22:43 +000062#include "llvm/Support/Dwarf.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000063#include "llvm/Support/TargetSelect.h"
Chandler Carruth605e30e2012-12-04 10:16:57 +000064#include "llvm/Target/TargetOptions.h"
65#include "llvm/Transforms/Scalar.h"
Garrison Vennf4d2f842010-02-09 23:22:43 +000066
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +000067// 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 Venna0f6ecb2011-04-12 12:30:10 +000069// for stderr, and fprintf, and the addition of this include fixed the
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +000070// 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 Venna0f6ecb2011-04-12 12:30:10 +000073// 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 Venn56c5ca22011-04-11 19:52:49 +000075#include <cstdio>
Garrison Venna0f6ecb2011-04-12 12:30:10 +000076
Garrison Vennf4d2f842010-02-09 23:22:43 +000077#include <sstream>
Garrison Vennf4d2f842010-02-09 23:22:43 +000078#include <stdexcept>
79
David Blaikief4057fe2015-08-14 00:31:49 +000080#include <inttypes.h>
Garrison Vennf4d2f842010-02-09 23:22:43 +000081
82#ifndef USE_GLOBAL_STR_CONSTS
83#define USE_GLOBAL_STR_CONSTS true
84#endif
85
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +000086// System C++ ABI unwind types from:
Dmitri Gribenko462462e2013-01-13 15:53:09 +000087// http://mentorembedded.github.com/cxx-abi/abi-eh.html (v1.22)
Garrison Vennf4d2f842010-02-09 23:22:43 +000088
89extern "C" {
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +000090
Chris Lattnera868bbb2011-04-08 18:02:51 +000091 typedef enum {
Garrison Vennf4d2f842010-02-09 23:22:43 +000092 _URC_NO_REASON = 0,
93 _URC_FOREIGN_EXCEPTION_CAUGHT = 1,
94 _URC_FATAL_PHASE2_ERROR = 2,
95 _URC_FATAL_PHASE1_ERROR = 3,
96 _URC_NORMAL_STOP = 4,
97 _URC_END_OF_STACK = 5,
98 _URC_HANDLER_FOUND = 6,
99 _URC_INSTALL_CONTEXT = 7,
100 _URC_CONTINUE_UNWIND = 8
Chris Lattnera868bbb2011-04-08 18:02:51 +0000101 } _Unwind_Reason_Code;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000102
Chris Lattnera868bbb2011-04-08 18:02:51 +0000103 typedef enum {
Garrison Vennf4d2f842010-02-09 23:22:43 +0000104 _UA_SEARCH_PHASE = 1,
105 _UA_CLEANUP_PHASE = 2,
106 _UA_HANDLER_FRAME = 4,
107 _UA_FORCE_UNWIND = 8,
108 _UA_END_OF_STACK = 16
Chris Lattnera868bbb2011-04-08 18:02:51 +0000109 } _Unwind_Action;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000110
Chris Lattnera868bbb2011-04-08 18:02:51 +0000111 struct _Unwind_Exception;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000112
Chris Lattnera868bbb2011-04-08 18:02:51 +0000113 typedef void (*_Unwind_Exception_Cleanup_Fn) (_Unwind_Reason_Code,
114 struct _Unwind_Exception *);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000115
Chris Lattnera868bbb2011-04-08 18:02:51 +0000116 struct _Unwind_Exception {
Garrison Vennf4d2f842010-02-09 23:22:43 +0000117 uint64_t exception_class;
118 _Unwind_Exception_Cleanup_Fn exception_cleanup;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000119
120 uintptr_t private_1;
121 uintptr_t private_2;
122
Garrison Vennf4d2f842010-02-09 23:22:43 +0000123 // @@@ The IA-64 ABI says that this structure must be double-word aligned.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000124 // Taking that literally does not make much sense generically. Instead
Garrison Vennf4d2f842010-02-09 23:22:43 +0000125 // we provide the maximum alignment required by any type for the machine.
Chris Lattnera868bbb2011-04-08 18:02:51 +0000126 } __attribute__((__aligned__));
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000127
Chris Lattnera868bbb2011-04-08 18:02:51 +0000128 struct _Unwind_Context;
Garrison Venn88bd9d62011-04-10 14:06:52 +0000129 typedef struct _Unwind_Context *_Unwind_Context_t;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000130
Garrison Venn88bd9d62011-04-10 14:06:52 +0000131 extern const uint8_t *_Unwind_GetLanguageSpecificData (_Unwind_Context_t c);
Chris Lattnera868bbb2011-04-08 18:02:51 +0000132 extern uintptr_t _Unwind_GetGR (_Unwind_Context_t c, int i);
133 extern void _Unwind_SetGR (_Unwind_Context_t c, int i, uintptr_t n);
134 extern void _Unwind_SetIP (_Unwind_Context_t, uintptr_t new_value);
135 extern uintptr_t _Unwind_GetIP (_Unwind_Context_t context);
136 extern uintptr_t _Unwind_GetRegionStart (_Unwind_Context_t context);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000137
Garrison Vennf4d2f842010-02-09 23:22:43 +0000138} // extern "C"
139
140//
141// Example types
142//
143
144/// This is our simplistic type info
145struct OurExceptionType_t {
Chris Lattnera868bbb2011-04-08 18:02:51 +0000146 /// type info type
147 int type;
Garrison Vennf4d2f842010-02-09 23:22:43 +0000148};
149
150
151/// This is our Exception class which relies on a negative offset to calculate
152/// pointers to its instances from pointers to its unwindException member.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000153///
Garrison Vennf4d2f842010-02-09 23:22:43 +0000154/// Note: The above unwind.h defines struct _Unwind_Exception to be aligned
155/// on a double word boundary. This is necessary to match the standard:
Dmitri Gribenko462462e2013-01-13 15:53:09 +0000156/// http://mentorembedded.github.com/cxx-abi/abi-eh.html
Garrison Vennf4d2f842010-02-09 23:22:43 +0000157struct OurBaseException_t {
Chris Lattnera868bbb2011-04-08 18:02:51 +0000158 struct OurExceptionType_t type;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000159
Chris Lattnera868bbb2011-04-08 18:02:51 +0000160 // Note: This is properly aligned in unwind.h
161 struct _Unwind_Exception unwindException;
Garrison Vennf4d2f842010-02-09 23:22:43 +0000162};
163
164
165// Note: Not needed since we are C++
166typedef struct OurBaseException_t OurException;
167typedef struct _Unwind_Exception OurUnwindException;
168
169//
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000170// Various globals used to support typeinfo and generatted exceptions in
Garrison Vennf4d2f842010-02-09 23:22:43 +0000171// general
172//
173
174static std::map<std::string, llvm::Value*> namedValues;
175
176int64_t ourBaseFromUnwindOffset;
177
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000178const unsigned char ourBaseExcpClassChars[] =
Chris Lattnera868bbb2011-04-08 18:02:51 +0000179{'o', 'b', 'j', '\0', 'b', 'a', 's', '\0'};
Garrison Vennf4d2f842010-02-09 23:22:43 +0000180
181
182static uint64_t ourBaseExceptionClass = 0;
183
184static std::vector<std::string> ourTypeInfoNames;
185static std::map<int, std::string> ourTypeInfoNamesIndex;
186
Garrison Venn88bd9d62011-04-10 14:06:52 +0000187static llvm::StructType *ourTypeInfoType;
Garrison Venn8cb00352011-09-22 15:45:14 +0000188static llvm::StructType *ourCaughtResultType;
Garrison Venn88bd9d62011-04-10 14:06:52 +0000189static llvm::StructType *ourExceptionType;
190static llvm::StructType *ourUnwindExceptionType;
Garrison Vennf4d2f842010-02-09 23:22:43 +0000191
Garrison Venn88bd9d62011-04-10 14:06:52 +0000192static llvm::ConstantInt *ourExceptionNotThrownState;
193static llvm::ConstantInt *ourExceptionThrownState;
194static llvm::ConstantInt *ourExceptionCaughtState;
Garrison Vennf4d2f842010-02-09 23:22:43 +0000195
196typedef std::vector<std::string> ArgNames;
Garrison Venn5fb3f662011-07-11 16:31:53 +0000197typedef std::vector<llvm::Type*> ArgTypes;
Garrison Vennf4d2f842010-02-09 23:22:43 +0000198
199//
200// Code Generation Utilities
201//
202
203/// Utility used to create a function, both declarations and definitions
204/// @param module for module instance
205/// @param retType function return type
206/// @param theArgTypes function's ordered argument types
207/// @param theArgNames function's ordered arguments needed if use of this
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000208/// function corresponds to a function definition. Use empty
Garrison Vennf4d2f842010-02-09 23:22:43 +0000209/// aggregate for function declarations.
210/// @param functName function name
211/// @param linkage function linkage
212/// @param declarationOnly for function declarations
213/// @param isVarArg function uses vararg arguments
214/// @returns function instance
Garrison Venn88bd9d62011-04-10 14:06:52 +0000215llvm::Function *createFunction(llvm::Module &module,
Chris Lattner6b8eb8c2011-07-18 04:52:09 +0000216 llvm::Type *retType,
Garrison Venn88bd9d62011-04-10 14:06:52 +0000217 const ArgTypes &theArgTypes,
218 const ArgNames &theArgNames,
219 const std::string &functName,
Garrison Vennf4d2f842010-02-09 23:22:43 +0000220 llvm::GlobalValue::LinkageTypes linkage,
221 bool declarationOnly,
222 bool isVarArg) {
Chris Lattnera868bbb2011-04-08 18:02:51 +0000223 llvm::FunctionType *functType =
224 llvm::FunctionType::get(retType, theArgTypes, isVarArg);
225 llvm::Function *ret =
226 llvm::Function::Create(functType, linkage, functName, &module);
227 if (!ret || declarationOnly)
Garrison Vennf4d2f842010-02-09 23:22:43 +0000228 return(ret);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000229
Chris Lattnera868bbb2011-04-08 18:02:51 +0000230 namedValues.clear();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000231 unsigned i = 0;
Chris Lattnera868bbb2011-04-08 18:02:51 +0000232 for (llvm::Function::arg_iterator argIndex = ret->arg_begin();
233 i != theArgNames.size();
234 ++argIndex, ++i) {
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000235
Chris Lattnera868bbb2011-04-08 18:02:51 +0000236 argIndex->setName(theArgNames[i]);
237 namedValues[theArgNames[i]] = argIndex;
238 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000239
Chris Lattnera868bbb2011-04-08 18:02:51 +0000240 return(ret);
Garrison Vennf4d2f842010-02-09 23:22:43 +0000241}
242
243
244/// Create an alloca instruction in the entry block of
245/// the parent function. This is used for mutable variables etc.
246/// @param function parent instance
247/// @param varName stack variable name
248/// @param type stack variable type
249/// @param initWith optional constant initialization value
250/// @returns AllocaInst instance
Garrison Venn88bd9d62011-04-10 14:06:52 +0000251static llvm::AllocaInst *createEntryBlockAlloca(llvm::Function &function,
Chris Lattnera868bbb2011-04-08 18:02:51 +0000252 const std::string &varName,
Chris Lattner6b8eb8c2011-07-18 04:52:09 +0000253 llvm::Type *type,
Garrison Venn88bd9d62011-04-10 14:06:52 +0000254 llvm::Constant *initWith = 0) {
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000255 llvm::BasicBlock &block = function.getEntryBlock();
Chris Lattnera868bbb2011-04-08 18:02:51 +0000256 llvm::IRBuilder<> tmp(&block, block.begin());
Garrison Venn88bd9d62011-04-10 14:06:52 +0000257 llvm::AllocaInst *ret = tmp.CreateAlloca(type, 0, varName.c_str());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000258
259 if (initWith)
Chris Lattnera868bbb2011-04-08 18:02:51 +0000260 tmp.CreateStore(initWith, ret);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000261
Chris Lattnera868bbb2011-04-08 18:02:51 +0000262 return(ret);
Garrison Vennf4d2f842010-02-09 23:22:43 +0000263}
264
265
266//
267// Code Generation Utilities End
268//
269
270//
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000271// Runtime C Library functions
Garrison Vennf4d2f842010-02-09 23:22:43 +0000272//
273
274// Note: using an extern "C" block so that static functions can be used
275extern "C" {
276
277// Note: Better ways to decide on bit width
278//
279/// Prints a 32 bit number, according to the format, to stderr.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000280/// @param intToPrint integer to print
Garrison Vennf4d2f842010-02-09 23:22:43 +0000281/// @param format printf like format to use when printing
Garrison Venn88bd9d62011-04-10 14:06:52 +0000282void print32Int(int intToPrint, const char *format) {
Chris Lattnera868bbb2011-04-08 18:02:51 +0000283 if (format) {
284 // Note: No NULL check
285 fprintf(stderr, format, intToPrint);
286 }
287 else {
288 // Note: No NULL check
289 fprintf(stderr, "::print32Int(...):NULL arg.\n");
290 }
Garrison Vennf4d2f842010-02-09 23:22:43 +0000291}
292
293
294// Note: Better ways to decide on bit width
295//
296/// Prints a 64 bit number, according to the format, to stderr.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000297/// @param intToPrint integer to print
Garrison Vennf4d2f842010-02-09 23:22:43 +0000298/// @param format printf like format to use when printing
Garrison Venn88bd9d62011-04-10 14:06:52 +0000299void print64Int(long int intToPrint, const char *format) {
Chris Lattnera868bbb2011-04-08 18:02:51 +0000300 if (format) {
301 // Note: No NULL check
302 fprintf(stderr, format, intToPrint);
303 }
304 else {
305 // Note: No NULL check
306 fprintf(stderr, "::print64Int(...):NULL arg.\n");
307 }
Garrison Vennf4d2f842010-02-09 23:22:43 +0000308}
309
310
311/// Prints a C string to stderr
312/// @param toPrint string to print
Garrison Venn88bd9d62011-04-10 14:06:52 +0000313void printStr(char *toPrint) {
Chris Lattnera868bbb2011-04-08 18:02:51 +0000314 if (toPrint) {
315 fprintf(stderr, "%s", toPrint);
316 }
317 else {
318 fprintf(stderr, "::printStr(...):NULL arg.\n");
319 }
Garrison Vennf4d2f842010-02-09 23:22:43 +0000320}
321
322
Luke Larsone7dba512015-09-18 21:15:45 +0000323/// Deletes the true previously allocated exception whose address
Garrison Vennf4d2f842010-02-09 23:22:43 +0000324/// is calculated from the supplied OurBaseException_t::unwindException
325/// member address. Handles (ignores), NULL pointers.
326/// @param expToDelete exception to delete
Garrison Venn88bd9d62011-04-10 14:06:52 +0000327void deleteOurException(OurUnwindException *expToDelete) {
Garrison Vennf4d2f842010-02-09 23:22:43 +0000328#ifdef DEBUG
Chris Lattnera868bbb2011-04-08 18:02:51 +0000329 fprintf(stderr,
330 "deleteOurException(...).\n");
Garrison Vennf4d2f842010-02-09 23:22:43 +0000331#endif
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000332
Chris Lattnera868bbb2011-04-08 18:02:51 +0000333 if (expToDelete &&
334 (expToDelete->exception_class == ourBaseExceptionClass)) {
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000335
Chris Lattnera868bbb2011-04-08 18:02:51 +0000336 free(((char*) expToDelete) + ourBaseFromUnwindOffset);
337 }
Garrison Vennf4d2f842010-02-09 23:22:43 +0000338}
339
340
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000341/// This function is the struct _Unwind_Exception API mandated delete function
342/// used by foreign exception handlers when deleting our exception
Garrison Vennf4d2f842010-02-09 23:22:43 +0000343/// (OurException), instances.
NAKAMURA Takumi1373b742013-07-29 11:03:50 +0000344/// @param reason See @link http://mentorembedded.github.com/cxx-abi/abi-eh.html
Garrison Vennf4d2f842010-02-09 23:22:43 +0000345/// @unlink
346/// @param expToDelete exception instance to delete
347void deleteFromUnwindOurException(_Unwind_Reason_Code reason,
Garrison Venn88bd9d62011-04-10 14:06:52 +0000348 OurUnwindException *expToDelete) {
Garrison Vennf4d2f842010-02-09 23:22:43 +0000349#ifdef DEBUG
Chris Lattnera868bbb2011-04-08 18:02:51 +0000350 fprintf(stderr,
351 "deleteFromUnwindOurException(...).\n");
Garrison Vennf4d2f842010-02-09 23:22:43 +0000352#endif
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000353
Chris Lattnera868bbb2011-04-08 18:02:51 +0000354 deleteOurException(expToDelete);
Garrison Vennf4d2f842010-02-09 23:22:43 +0000355}
356
357
358/// Creates (allocates on the heap), an exception (OurException instance),
359/// of the supplied type info type.
360/// @param type type info type
Garrison Venn88bd9d62011-04-10 14:06:52 +0000361OurUnwindException *createOurException(int type) {
Chris Lattnera868bbb2011-04-08 18:02:51 +0000362 size_t size = sizeof(OurException);
Garrison Venn88bd9d62011-04-10 14:06:52 +0000363 OurException *ret = (OurException*) memset(malloc(size), 0, size);
Chris Lattnera868bbb2011-04-08 18:02:51 +0000364 (ret->type).type = type;
365 (ret->unwindException).exception_class = ourBaseExceptionClass;
366 (ret->unwindException).exception_cleanup = deleteFromUnwindOurException;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000367
Chris Lattnera868bbb2011-04-08 18:02:51 +0000368 return(&(ret->unwindException));
Garrison Vennf4d2f842010-02-09 23:22:43 +0000369}
370
371
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000372/// Read a uleb128 encoded value and advance pointer
373/// See Variable Length Data in:
Garrison Vennf4d2f842010-02-09 23:22:43 +0000374/// @link http://dwarfstd.org/Dwarf3.pdf @unlink
375/// @param data reference variable holding memory pointer to decode from
376/// @returns decoded value
Garrison Venn88bd9d62011-04-10 14:06:52 +0000377static uintptr_t readULEB128(const uint8_t **data) {
Chris Lattnera868bbb2011-04-08 18:02:51 +0000378 uintptr_t result = 0;
379 uintptr_t shift = 0;
380 unsigned char byte;
Garrison Venn88bd9d62011-04-10 14:06:52 +0000381 const uint8_t *p = *data;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000382
Chris Lattnera868bbb2011-04-08 18:02:51 +0000383 do {
384 byte = *p++;
385 result |= (byte & 0x7f) << shift;
386 shift += 7;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000387 }
Chris Lattnera868bbb2011-04-08 18:02:51 +0000388 while (byte & 0x80);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000389
Chris Lattnera868bbb2011-04-08 18:02:51 +0000390 *data = p;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000391
Chris Lattnera868bbb2011-04-08 18:02:51 +0000392 return result;
Garrison Vennf4d2f842010-02-09 23:22:43 +0000393}
394
395
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000396/// Read a sleb128 encoded value and advance pointer
397/// See Variable Length Data in:
Garrison Vennf4d2f842010-02-09 23:22:43 +0000398/// @link http://dwarfstd.org/Dwarf3.pdf @unlink
399/// @param data reference variable holding memory pointer to decode from
400/// @returns decoded value
Garrison Venn88bd9d62011-04-10 14:06:52 +0000401static uintptr_t readSLEB128(const uint8_t **data) {
Chris Lattnera868bbb2011-04-08 18:02:51 +0000402 uintptr_t result = 0;
403 uintptr_t shift = 0;
404 unsigned char byte;
Garrison Venn88bd9d62011-04-10 14:06:52 +0000405 const uint8_t *p = *data;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000406
Chris Lattnera868bbb2011-04-08 18:02:51 +0000407 do {
408 byte = *p++;
409 result |= (byte & 0x7f) << shift;
410 shift += 7;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000411 }
Chris Lattnera868bbb2011-04-08 18:02:51 +0000412 while (byte & 0x80);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000413
Chris Lattnera868bbb2011-04-08 18:02:51 +0000414 *data = p;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000415
Chris Lattnera868bbb2011-04-08 18:02:51 +0000416 if ((byte & 0x40) && (shift < (sizeof(result) << 3))) {
417 result |= (~0 << shift);
418 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000419
Chris Lattnera868bbb2011-04-08 18:02:51 +0000420 return result;
Garrison Vennf4d2f842010-02-09 23:22:43 +0000421}
422
Rafael Espindola5096adf2013-05-01 21:05:05 +0000423unsigned getEncodingSize(uint8_t Encoding) {
424 if (Encoding == llvm::dwarf::DW_EH_PE_omit)
425 return 0;
426
427 switch (Encoding & 0x0F) {
428 case llvm::dwarf::DW_EH_PE_absptr:
429 return sizeof(uintptr_t);
430 case llvm::dwarf::DW_EH_PE_udata2:
431 return sizeof(uint16_t);
432 case llvm::dwarf::DW_EH_PE_udata4:
433 return sizeof(uint32_t);
434 case llvm::dwarf::DW_EH_PE_udata8:
435 return sizeof(uint64_t);
436 case llvm::dwarf::DW_EH_PE_sdata2:
437 return sizeof(int16_t);
438 case llvm::dwarf::DW_EH_PE_sdata4:
439 return sizeof(int32_t);
440 case llvm::dwarf::DW_EH_PE_sdata8:
441 return sizeof(int64_t);
442 default:
443 // not supported
444 abort();
445 }
446}
Garrison Vennf4d2f842010-02-09 23:22:43 +0000447
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000448/// Read a pointer encoded value and advance pointer
449/// See Variable Length Data in:
Garrison Vennf4d2f842010-02-09 23:22:43 +0000450/// @link http://dwarfstd.org/Dwarf3.pdf @unlink
451/// @param data reference variable holding memory pointer to decode from
452/// @param encoding dwarf encoding type
453/// @returns decoded value
Garrison Venn88bd9d62011-04-10 14:06:52 +0000454static uintptr_t readEncodedPointer(const uint8_t **data, uint8_t encoding) {
Chris Lattnera868bbb2011-04-08 18:02:51 +0000455 uintptr_t result = 0;
Garrison Venn88bd9d62011-04-10 14:06:52 +0000456 const uint8_t *p = *data;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000457
458 if (encoding == llvm::dwarf::DW_EH_PE_omit)
Chris Lattnera868bbb2011-04-08 18:02:51 +0000459 return(result);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000460
461 // first get value
Chris Lattnera868bbb2011-04-08 18:02:51 +0000462 switch (encoding & 0x0F) {
463 case llvm::dwarf::DW_EH_PE_absptr:
464 result = *((uintptr_t*)p);
465 p += sizeof(uintptr_t);
466 break;
467 case llvm::dwarf::DW_EH_PE_uleb128:
468 result = readULEB128(&p);
469 break;
470 // Note: This case has not been tested
471 case llvm::dwarf::DW_EH_PE_sleb128:
472 result = readSLEB128(&p);
473 break;
474 case llvm::dwarf::DW_EH_PE_udata2:
475 result = *((uint16_t*)p);
476 p += sizeof(uint16_t);
477 break;
478 case llvm::dwarf::DW_EH_PE_udata4:
479 result = *((uint32_t*)p);
480 p += sizeof(uint32_t);
481 break;
482 case llvm::dwarf::DW_EH_PE_udata8:
483 result = *((uint64_t*)p);
484 p += sizeof(uint64_t);
485 break;
486 case llvm::dwarf::DW_EH_PE_sdata2:
487 result = *((int16_t*)p);
488 p += sizeof(int16_t);
489 break;
490 case llvm::dwarf::DW_EH_PE_sdata4:
491 result = *((int32_t*)p);
492 p += sizeof(int32_t);
493 break;
494 case llvm::dwarf::DW_EH_PE_sdata8:
495 result = *((int64_t*)p);
496 p += sizeof(int64_t);
497 break;
498 default:
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000499 // not supported
Chris Lattnera868bbb2011-04-08 18:02:51 +0000500 abort();
501 break;
502 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000503
504 // then add relative offset
Chris Lattnera868bbb2011-04-08 18:02:51 +0000505 switch (encoding & 0x70) {
506 case llvm::dwarf::DW_EH_PE_absptr:
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000507 // do nothing
Chris Lattnera868bbb2011-04-08 18:02:51 +0000508 break;
509 case llvm::dwarf::DW_EH_PE_pcrel:
510 result += (uintptr_t)(*data);
511 break;
512 case llvm::dwarf::DW_EH_PE_textrel:
513 case llvm::dwarf::DW_EH_PE_datarel:
514 case llvm::dwarf::DW_EH_PE_funcrel:
515 case llvm::dwarf::DW_EH_PE_aligned:
516 default:
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000517 // not supported
Chris Lattnera868bbb2011-04-08 18:02:51 +0000518 abort();
519 break;
520 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000521
522 // then apply indirection
Chris Lattnera868bbb2011-04-08 18:02:51 +0000523 if (encoding & llvm::dwarf::DW_EH_PE_indirect) {
524 result = *((uintptr_t*)result);
525 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000526
Chris Lattnera868bbb2011-04-08 18:02:51 +0000527 *data = p;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000528
Chris Lattnera868bbb2011-04-08 18:02:51 +0000529 return result;
Garrison Vennf4d2f842010-02-09 23:22:43 +0000530}
531
532
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000533/// Deals with Dwarf actions matching our type infos
534/// (OurExceptionType_t instances). Returns whether or not a dwarf emitted
535/// action matches the supplied exception type. If such a match succeeds,
536/// the resultAction argument will be set with > 0 index value. Only
537/// corresponding llvm.eh.selector type info arguments, cleanup arguments
Garrison Vennf4d2f842010-02-09 23:22:43 +0000538/// are supported. Filters are not supported.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000539/// See Variable Length Data in:
Garrison Vennf4d2f842010-02-09 23:22:43 +0000540/// @link http://dwarfstd.org/Dwarf3.pdf @unlink
Dmitri Gribenko462462e2013-01-13 15:53:09 +0000541/// Also see @link http://mentorembedded.github.com/cxx-abi/abi-eh.html @unlink
Garrison Vennf4d2f842010-02-09 23:22:43 +0000542/// @param resultAction reference variable which will be set with result
543/// @param classInfo our array of type info pointers (to globals)
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000544/// @param actionEntry index into above type info array or 0 (clean up).
Garrison Vennf4d2f842010-02-09 23:22:43 +0000545/// We do not support filters.
546/// @param exceptionClass exception class (_Unwind_Exception::exception_class)
547/// of thrown exception.
548/// @param exceptionObject thrown _Unwind_Exception instance.
549/// @returns whether or not a type info was found. False is returned if only
550/// a cleanup was found
551static bool handleActionValue(int64_t *resultAction,
Rafael Espindola5096adf2013-05-01 21:05:05 +0000552 uint8_t TTypeEncoding,
553 const uint8_t *ClassInfo,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000554 uintptr_t actionEntry,
555 uint64_t exceptionClass,
Garrison Vennf4d2f842010-02-09 23:22:43 +0000556 struct _Unwind_Exception *exceptionObject) {
Chris Lattnera868bbb2011-04-08 18:02:51 +0000557 bool ret = false;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000558
559 if (!resultAction ||
560 !exceptionObject ||
Chris Lattnera868bbb2011-04-08 18:02:51 +0000561 (exceptionClass != ourBaseExceptionClass))
562 return(ret);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000563
Garrison Venn88bd9d62011-04-10 14:06:52 +0000564 struct OurBaseException_t *excp = (struct OurBaseException_t*)
Chris Lattnera868bbb2011-04-08 18:02:51 +0000565 (((char*) exceptionObject) + ourBaseFromUnwindOffset);
566 struct OurExceptionType_t *excpType = &(excp->type);
567 int type = excpType->type;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000568
Chris Lattnera868bbb2011-04-08 18:02:51 +0000569#ifdef DEBUG
570 fprintf(stderr,
571 "handleActionValue(...): exceptionObject = <%p>, "
572 "excp = <%p>.\n",
David Blaikief4057fe2015-08-14 00:31:49 +0000573 (void*)exceptionObject,
574 (void*)excp);
Chris Lattnera868bbb2011-04-08 18:02:51 +0000575#endif
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000576
Chris Lattnera868bbb2011-04-08 18:02:51 +0000577 const uint8_t *actionPos = (uint8_t*) actionEntry,
578 *tempActionPos;
579 int64_t typeOffset = 0,
580 actionOffset;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000581
Chris Lattnera868bbb2011-04-08 18:02:51 +0000582 for (int i = 0; true; ++i) {
583 // Each emitted dwarf action corresponds to a 2 tuple of
584 // type info address offset, and action offset to the next
585 // emitted action.
586 typeOffset = readSLEB128(&actionPos);
587 tempActionPos = actionPos;
588 actionOffset = readSLEB128(&tempActionPos);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000589
Garrison Vennf4d2f842010-02-09 23:22:43 +0000590#ifdef DEBUG
591 fprintf(stderr,
David Blaikief4057fe2015-08-14 00:31:49 +0000592 "handleActionValue(...):typeOffset: <%" PRIi64 ">, "
593 "actionOffset: <%" PRIi64 ">.\n",
Chris Lattnera868bbb2011-04-08 18:02:51 +0000594 typeOffset,
595 actionOffset);
Garrison Vennf4d2f842010-02-09 23:22:43 +0000596#endif
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000597 assert((typeOffset >= 0) &&
Chris Lattnera868bbb2011-04-08 18:02:51 +0000598 "handleActionValue(...):filters are not supported.");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000599
Chris Lattnera868bbb2011-04-08 18:02:51 +0000600 // Note: A typeOffset == 0 implies that a cleanup llvm.eh.selector
601 // argument has been matched.
Rafael Espindola5096adf2013-05-01 21:05:05 +0000602 if (typeOffset > 0) {
Garrison Vennf4d2f842010-02-09 23:22:43 +0000603#ifdef DEBUG
Chris Lattnera868bbb2011-04-08 18:02:51 +0000604 fprintf(stderr,
605 "handleActionValue(...):actionValue <%d> found.\n",
606 i);
Garrison Vennf4d2f842010-02-09 23:22:43 +0000607#endif
Rafael Espindola5096adf2013-05-01 21:05:05 +0000608 unsigned EncSize = getEncodingSize(TTypeEncoding);
609 const uint8_t *EntryP = ClassInfo - typeOffset * EncSize;
610 uintptr_t P = readEncodedPointer(&EntryP, TTypeEncoding);
611 struct OurExceptionType_t *ThisClassInfo =
612 reinterpret_cast<struct OurExceptionType_t *>(P);
613 if (ThisClassInfo->type == type) {
614 *resultAction = i + 1;
615 ret = true;
616 break;
617 }
Garrison Vennf4d2f842010-02-09 23:22:43 +0000618 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000619
Chris Lattnera868bbb2011-04-08 18:02:51 +0000620#ifdef DEBUG
621 fprintf(stderr,
622 "handleActionValue(...):actionValue not found.\n");
623#endif
624 if (!actionOffset)
625 break;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000626
Chris Lattnera868bbb2011-04-08 18:02:51 +0000627 actionPos += actionOffset;
628 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000629
Chris Lattnera868bbb2011-04-08 18:02:51 +0000630 return(ret);
Garrison Vennf4d2f842010-02-09 23:22:43 +0000631}
632
633
634/// Deals with the Language specific data portion of the emitted dwarf code.
Dmitri Gribenko462462e2013-01-13 15:53:09 +0000635/// See @link http://mentorembedded.github.com/cxx-abi/abi-eh.html @unlink
Garrison Vennf4d2f842010-02-09 23:22:43 +0000636/// @param version unsupported (ignored), unwind version
637/// @param lsda language specific data area
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000638/// @param _Unwind_Action actions minimally supported unwind stage
Garrison Vennf4d2f842010-02-09 23:22:43 +0000639/// (forced specifically not supported)
640/// @param exceptionClass exception class (_Unwind_Exception::exception_class)
641/// of thrown exception.
642/// @param exceptionObject thrown _Unwind_Exception instance.
643/// @param context unwind system context
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000644/// @returns minimally supported unwinding control indicator
645static _Unwind_Reason_Code handleLsda(int version,
Garrison Venn88bd9d62011-04-10 14:06:52 +0000646 const uint8_t *lsda,
Chris Lattnera868bbb2011-04-08 18:02:51 +0000647 _Unwind_Action actions,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000648 uint64_t exceptionClass,
Garrison Venn88bd9d62011-04-10 14:06:52 +0000649 struct _Unwind_Exception *exceptionObject,
Chris Lattnera868bbb2011-04-08 18:02:51 +0000650 _Unwind_Context_t context) {
651 _Unwind_Reason_Code ret = _URC_CONTINUE_UNWIND;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000652
Chris Lattnera868bbb2011-04-08 18:02:51 +0000653 if (!lsda)
654 return(ret);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000655
Garrison Vennf4d2f842010-02-09 23:22:43 +0000656#ifdef DEBUG
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000657 fprintf(stderr,
Chris Lattnera868bbb2011-04-08 18:02:51 +0000658 "handleLsda(...):lsda is non-zero.\n");
Garrison Vennf4d2f842010-02-09 23:22:43 +0000659#endif
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000660
Chris Lattnera868bbb2011-04-08 18:02:51 +0000661 // Get the current instruction pointer and offset it before next
662 // instruction in the current frame which threw the exception.
663 uintptr_t pc = _Unwind_GetIP(context)-1;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000664
665 // Get beginning current frame's code (as defined by the
Chris Lattnera868bbb2011-04-08 18:02:51 +0000666 // emitted dwarf code)
667 uintptr_t funcStart = _Unwind_GetRegionStart(context);
668 uintptr_t pcOffset = pc - funcStart;
Rafael Espindola5096adf2013-05-01 21:05:05 +0000669 const uint8_t *ClassInfo = NULL;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000670
Chris Lattnera868bbb2011-04-08 18:02:51 +0000671 // Note: See JITDwarfEmitter::EmitExceptionTable(...) for corresponding
672 // dwarf emission
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000673
Chris Lattnera868bbb2011-04-08 18:02:51 +0000674 // Parse LSDA header.
675 uint8_t lpStartEncoding = *lsda++;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000676
Chris Lattnera868bbb2011-04-08 18:02:51 +0000677 if (lpStartEncoding != llvm::dwarf::DW_EH_PE_omit) {
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000678 readEncodedPointer(&lsda, lpStartEncoding);
Chris Lattnera868bbb2011-04-08 18:02:51 +0000679 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000680
Chris Lattnera868bbb2011-04-08 18:02:51 +0000681 uint8_t ttypeEncoding = *lsda++;
682 uintptr_t classInfoOffset;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000683
Chris Lattnera868bbb2011-04-08 18:02:51 +0000684 if (ttypeEncoding != llvm::dwarf::DW_EH_PE_omit) {
685 // Calculate type info locations in emitted dwarf code which
686 // were flagged by type info arguments to llvm.eh.selector
687 // intrinsic
688 classInfoOffset = readULEB128(&lsda);
Rafael Espindola5096adf2013-05-01 21:05:05 +0000689 ClassInfo = lsda + classInfoOffset;
Chris Lattnera868bbb2011-04-08 18:02:51 +0000690 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000691
692 // Walk call-site table looking for range that
693 // includes current PC.
694
Chris Lattnera868bbb2011-04-08 18:02:51 +0000695 uint8_t callSiteEncoding = *lsda++;
696 uint32_t callSiteTableLength = readULEB128(&lsda);
Garrison Venn88bd9d62011-04-10 14:06:52 +0000697 const uint8_t *callSiteTableStart = lsda;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000698 const uint8_t *callSiteTableEnd = callSiteTableStart +
Chris Lattnera868bbb2011-04-08 18:02:51 +0000699 callSiteTableLength;
Garrison Venn88bd9d62011-04-10 14:06:52 +0000700 const uint8_t *actionTableStart = callSiteTableEnd;
701 const uint8_t *callSitePtr = callSiteTableStart;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000702
Chris Lattnera868bbb2011-04-08 18:02:51 +0000703 while (callSitePtr < callSiteTableEnd) {
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000704 uintptr_t start = readEncodedPointer(&callSitePtr,
Chris Lattnera868bbb2011-04-08 18:02:51 +0000705 callSiteEncoding);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000706 uintptr_t length = readEncodedPointer(&callSitePtr,
Chris Lattnera868bbb2011-04-08 18:02:51 +0000707 callSiteEncoding);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000708 uintptr_t landingPad = readEncodedPointer(&callSitePtr,
Garrison Vennf4d2f842010-02-09 23:22:43 +0000709 callSiteEncoding);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000710
Chris Lattnera868bbb2011-04-08 18:02:51 +0000711 // Note: Action value
712 uintptr_t actionEntry = readULEB128(&callSitePtr);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000713
Chris Lattnera868bbb2011-04-08 18:02:51 +0000714 if (exceptionClass != ourBaseExceptionClass) {
715 // We have been notified of a foreign exception being thrown,
716 // and we therefore need to execute cleanup landing pads
717 actionEntry = 0;
Chris Lattnera868bbb2011-04-08 18:02:51 +0000718 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000719
Chris Lattnera868bbb2011-04-08 18:02:51 +0000720 if (landingPad == 0) {
Garrison Vennf4d2f842010-02-09 23:22:43 +0000721#ifdef DEBUG
Chris Lattnera868bbb2011-04-08 18:02:51 +0000722 fprintf(stderr,
723 "handleLsda(...): No landing pad found.\n");
Garrison Vennf4d2f842010-02-09 23:22:43 +0000724#endif
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000725
Chris Lattnera868bbb2011-04-08 18:02:51 +0000726 continue; // no landing pad for this entry
727 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000728
Chris Lattnera868bbb2011-04-08 18:02:51 +0000729 if (actionEntry) {
730 actionEntry += ((uintptr_t) actionTableStart) - 1;
731 }
732 else {
733#ifdef DEBUG
734 fprintf(stderr,
735 "handleLsda(...):No action table found.\n");
736#endif
737 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000738
Chris Lattnera868bbb2011-04-08 18:02:51 +0000739 bool exceptionMatched = false;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000740
Chris Lattnera868bbb2011-04-08 18:02:51 +0000741 if ((start <= pcOffset) && (pcOffset < (start + length))) {
742#ifdef DEBUG
743 fprintf(stderr,
744 "handleLsda(...): Landing pad found.\n");
745#endif
746 int64_t actionValue = 0;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000747
Chris Lattnera868bbb2011-04-08 18:02:51 +0000748 if (actionEntry) {
Garrison Venn88bd9d62011-04-10 14:06:52 +0000749 exceptionMatched = handleActionValue(&actionValue,
Rafael Espindola5096adf2013-05-01 21:05:05 +0000750 ttypeEncoding,
751 ClassInfo,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000752 actionEntry,
753 exceptionClass,
Garrison Venn88bd9d62011-04-10 14:06:52 +0000754 exceptionObject);
Chris Lattnera868bbb2011-04-08 18:02:51 +0000755 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000756
Chris Lattnera868bbb2011-04-08 18:02:51 +0000757 if (!(actions & _UA_SEARCH_PHASE)) {
758#ifdef DEBUG
759 fprintf(stderr,
760 "handleLsda(...): installed landing pad "
761 "context.\n");
762#endif
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000763
Chris Lattnera868bbb2011-04-08 18:02:51 +0000764 // Found landing pad for the PC.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000765 // Set Instruction Pointer to so we re-enter function
766 // at landing pad. The landing pad is created by the
Chris Lattnera868bbb2011-04-08 18:02:51 +0000767 // compiler to take two parameters in registers.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000768 _Unwind_SetGR(context,
769 __builtin_eh_return_data_regno(0),
Chris Lattnera868bbb2011-04-08 18:02:51 +0000770 (uintptr_t)exceptionObject);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000771
Chris Lattnera868bbb2011-04-08 18:02:51 +0000772 // Note: this virtual register directly corresponds
773 // to the return of the llvm.eh.selector intrinsic
774 if (!actionEntry || !exceptionMatched) {
775 // We indicate cleanup only
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000776 _Unwind_SetGR(context,
777 __builtin_eh_return_data_regno(1),
Chris Lattnera868bbb2011-04-08 18:02:51 +0000778 0);
Garrison Vennf4d2f842010-02-09 23:22:43 +0000779 }
780 else {
Chris Lattnera868bbb2011-04-08 18:02:51 +0000781 // Matched type info index of llvm.eh.selector intrinsic
782 // passed here.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000783 _Unwind_SetGR(context,
784 __builtin_eh_return_data_regno(1),
Chris Lattnera868bbb2011-04-08 18:02:51 +0000785 actionValue);
Garrison Vennf4d2f842010-02-09 23:22:43 +0000786 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000787
Chris Lattnera868bbb2011-04-08 18:02:51 +0000788 // To execute landing pad set here
789 _Unwind_SetIP(context, funcStart + landingPad);
790 ret = _URC_INSTALL_CONTEXT;
791 }
792 else if (exceptionMatched) {
Garrison Vennf4d2f842010-02-09 23:22:43 +0000793#ifdef DEBUG
Chris Lattnera868bbb2011-04-08 18:02:51 +0000794 fprintf(stderr,
795 "handleLsda(...): setting handler found.\n");
Garrison Vennf4d2f842010-02-09 23:22:43 +0000796#endif
Chris Lattnera868bbb2011-04-08 18:02:51 +0000797 ret = _URC_HANDLER_FOUND;
798 }
799 else {
800 // Note: Only non-clean up handlers are marked as
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000801 // found. Otherwise the clean up handlers will be
802 // re-found and executed during the clean up
Chris Lattnera868bbb2011-04-08 18:02:51 +0000803 // phase.
Garrison Vennf4d2f842010-02-09 23:22:43 +0000804#ifdef DEBUG
Chris Lattnera868bbb2011-04-08 18:02:51 +0000805 fprintf(stderr,
806 "handleLsda(...): cleanup handler found.\n");
Garrison Vennf4d2f842010-02-09 23:22:43 +0000807#endif
Chris Lattnera868bbb2011-04-08 18:02:51 +0000808 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000809
Chris Lattnera868bbb2011-04-08 18:02:51 +0000810 break;
Garrison Vennf4d2f842010-02-09 23:22:43 +0000811 }
Chris Lattnera868bbb2011-04-08 18:02:51 +0000812 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000813
Chris Lattnera868bbb2011-04-08 18:02:51 +0000814 return(ret);
Garrison Vennf4d2f842010-02-09 23:22:43 +0000815}
816
817
818/// This is the personality function which is embedded (dwarf emitted), in the
819/// dwarf unwind info block. Again see: JITDwarfEmitter.cpp.
Dmitri Gribenko462462e2013-01-13 15:53:09 +0000820/// See @link http://mentorembedded.github.com/cxx-abi/abi-eh.html @unlink
Garrison Vennf4d2f842010-02-09 23:22:43 +0000821/// @param version unsupported (ignored), unwind version
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000822/// @param _Unwind_Action actions minimally supported unwind stage
Garrison Vennf4d2f842010-02-09 23:22:43 +0000823/// (forced specifically not supported)
824/// @param exceptionClass exception class (_Unwind_Exception::exception_class)
825/// of thrown exception.
826/// @param exceptionObject thrown _Unwind_Exception instance.
827/// @param context unwind system context
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000828/// @returns minimally supported unwinding control indicator
829_Unwind_Reason_Code ourPersonality(int version,
Chris Lattnera868bbb2011-04-08 18:02:51 +0000830 _Unwind_Action actions,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000831 uint64_t exceptionClass,
Garrison Venn88bd9d62011-04-10 14:06:52 +0000832 struct _Unwind_Exception *exceptionObject,
Chris Lattnera868bbb2011-04-08 18:02:51 +0000833 _Unwind_Context_t context) {
Garrison Vennf4d2f842010-02-09 23:22:43 +0000834#ifdef DEBUG
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000835 fprintf(stderr,
Chris Lattnera868bbb2011-04-08 18:02:51 +0000836 "We are in ourPersonality(...):actions is <%d>.\n",
837 actions);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000838
Chris Lattnera868bbb2011-04-08 18:02:51 +0000839 if (actions & _UA_SEARCH_PHASE) {
840 fprintf(stderr, "ourPersonality(...):In search phase.\n");
841 }
842 else {
843 fprintf(stderr, "ourPersonality(...):In non-search phase.\n");
844 }
Garrison Vennf4d2f842010-02-09 23:22:43 +0000845#endif
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000846
Garrison Venn88bd9d62011-04-10 14:06:52 +0000847 const uint8_t *lsda = _Unwind_GetLanguageSpecificData(context);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000848
Garrison Vennf4d2f842010-02-09 23:22:43 +0000849#ifdef DEBUG
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000850 fprintf(stderr,
Chris Lattnera868bbb2011-04-08 18:02:51 +0000851 "ourPersonality(...):lsda = <%p>.\n",
David Blaikief4057fe2015-08-14 00:31:49 +0000852 (void*)lsda);
Garrison Vennf4d2f842010-02-09 23:22:43 +0000853#endif
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000854
Chris Lattnera868bbb2011-04-08 18:02:51 +0000855 // The real work of the personality function is captured here
856 return(handleLsda(version,
857 lsda,
858 actions,
859 exceptionClass,
860 exceptionObject,
861 context));
Garrison Vennf4d2f842010-02-09 23:22:43 +0000862}
863
864
865/// Generates our _Unwind_Exception class from a given character array.
866/// thereby handling arbitrary lengths (not in standard), and handling
867/// embedded \0s.
Dmitri Gribenko462462e2013-01-13 15:53:09 +0000868/// See @link http://mentorembedded.github.com/cxx-abi/abi-eh.html @unlink
Garrison Vennf4d2f842010-02-09 23:22:43 +0000869/// @param classChars char array to encode. NULL values not checkedf
870/// @param classCharsSize number of chars in classChars. Value is not checked.
871/// @returns class value
872uint64_t genClass(const unsigned char classChars[], size_t classCharsSize)
873{
Chris Lattnera868bbb2011-04-08 18:02:51 +0000874 uint64_t ret = classChars[0];
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000875
Chris Lattnera868bbb2011-04-08 18:02:51 +0000876 for (unsigned i = 1; i < classCharsSize; ++i) {
877 ret <<= 8;
878 ret += classChars[i];
879 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000880
Chris Lattnera868bbb2011-04-08 18:02:51 +0000881 return(ret);
Garrison Vennf4d2f842010-02-09 23:22:43 +0000882}
883
884} // extern "C"
885
886//
887// Runtime C Library functions End
888//
889
890//
891// Code generation functions
892//
893
894/// Generates code to print given constant string
895/// @param context llvm context
896/// @param module code for module instance
897/// @param builder builder instance
898/// @param toPrint string to print
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000899/// @param useGlobal A value of true (default) indicates a GlobalValue is
900/// generated, and is used to hold the constant string. A value of
901/// false indicates that the constant string will be stored on the
Garrison Vennf4d2f842010-02-09 23:22:43 +0000902/// stack.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000903void generateStringPrint(llvm::LLVMContext &context,
Garrison Venn88bd9d62011-04-10 14:06:52 +0000904 llvm::Module &module,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000905 llvm::IRBuilder<> &builder,
Garrison Vennf4d2f842010-02-09 23:22:43 +0000906 std::string toPrint,
907 bool useGlobal = true) {
Chris Lattnera868bbb2011-04-08 18:02:51 +0000908 llvm::Function *printFunct = module.getFunction("printStr");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000909
Chris Lattnera868bbb2011-04-08 18:02:51 +0000910 llvm::Value *stringVar;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000911 llvm::Constant *stringConstant =
Peter Collingbournec4e342b2012-02-06 14:09:13 +0000912 llvm::ConstantDataArray::getString(context, toPrint);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000913
Chris Lattnera868bbb2011-04-08 18:02:51 +0000914 if (useGlobal) {
915 // Note: Does not work without allocation
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000916 stringVar =
917 new llvm::GlobalVariable(module,
Chris Lattnera868bbb2011-04-08 18:02:51 +0000918 stringConstant->getType(),
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000919 true,
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000920 llvm::GlobalValue::PrivateLinkage,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000921 stringConstant,
Chris Lattnera868bbb2011-04-08 18:02:51 +0000922 "");
923 }
924 else {
925 stringVar = builder.CreateAlloca(stringConstant->getType());
926 builder.CreateStore(stringConstant, stringVar);
927 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000928
929 llvm::Value *cast = builder.CreatePointerCast(stringVar,
Garrison Venn0a2eb8d2011-09-23 14:45:10 +0000930 builder.getInt8PtrTy());
Chris Lattnera868bbb2011-04-08 18:02:51 +0000931 builder.CreateCall(printFunct, cast);
Garrison Vennf4d2f842010-02-09 23:22:43 +0000932}
933
934
935/// Generates code to print given runtime integer according to constant
936/// string format, and a given print function.
937/// @param context llvm context
938/// @param module code for module instance
939/// @param builder builder instance
940/// @param printFunct function used to "print" integer
941/// @param toPrint string to print
942/// @param format printf like formating string for print
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000943/// @param useGlobal A value of true (default) indicates a GlobalValue is
944/// generated, and is used to hold the constant string. A value of
945/// false indicates that the constant string will be stored on the
Garrison Vennf4d2f842010-02-09 23:22:43 +0000946/// stack.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000947void generateIntegerPrint(llvm::LLVMContext &context,
Garrison Venn88bd9d62011-04-10 14:06:52 +0000948 llvm::Module &module,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000949 llvm::IRBuilder<> &builder,
Garrison Venn88bd9d62011-04-10 14:06:52 +0000950 llvm::Function &printFunct,
951 llvm::Value &toPrint,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000952 std::string format,
Garrison Vennf4d2f842010-02-09 23:22:43 +0000953 bool useGlobal = true) {
Peter Collingbournec4e342b2012-02-06 14:09:13 +0000954 llvm::Constant *stringConstant =
955 llvm::ConstantDataArray::getString(context, format);
Chris Lattnera868bbb2011-04-08 18:02:51 +0000956 llvm::Value *stringVar;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000957
Chris Lattnera868bbb2011-04-08 18:02:51 +0000958 if (useGlobal) {
959 // Note: Does not seem to work without allocation
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000960 stringVar =
961 new llvm::GlobalVariable(module,
Chris Lattnera868bbb2011-04-08 18:02:51 +0000962 stringConstant->getType(),
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000963 true,
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000964 llvm::GlobalValue::PrivateLinkage,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000965 stringConstant,
Chris Lattnera868bbb2011-04-08 18:02:51 +0000966 "");
967 }
968 else {
969 stringVar = builder.CreateAlloca(stringConstant->getType());
970 builder.CreateStore(stringConstant, stringVar);
971 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000972
973 llvm::Value *cast = builder.CreateBitCast(stringVar,
Garrison Venn0a2eb8d2011-09-23 14:45:10 +0000974 builder.getInt8PtrTy());
David Blaikie3b20b042015-08-14 00:24:56 +0000975 builder.CreateCall(&printFunct, {&toPrint, cast});
Garrison Vennf4d2f842010-02-09 23:22:43 +0000976}
977
978
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000979/// Generates code to handle finally block type semantics: always runs
980/// regardless of whether a thrown exception is passing through or the
981/// parent function is simply exiting. In addition to printing some state
982/// to stderr, this code will resume the exception handling--runs the
983/// unwind resume block, if the exception has not been previously caught
984/// by a catch clause, and will otherwise execute the end block (terminator
985/// block). In addition this function creates the corresponding function's
Garrison Vennf4d2f842010-02-09 23:22:43 +0000986/// stack storage for the exception pointer and catch flag status.
987/// @param context llvm context
988/// @param module code for module instance
989/// @param builder builder instance
990/// @param toAddTo parent function to add block to
991/// @param blockName block name of new "finally" block.
992/// @param functionId output id used for printing
993/// @param terminatorBlock terminator "end" block
994/// @param unwindResumeBlock unwind resume block
995/// @param exceptionCaughtFlag reference exception caught/thrown status storage
996/// @param exceptionStorage reference to exception pointer storage
Garrison Venn0a2eb8d2011-09-23 14:45:10 +0000997/// @param caughtResultStorage reference to landingpad result storage
Garrison Vennf4d2f842010-02-09 23:22:43 +0000998/// @returns newly created block
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000999static llvm::BasicBlock *createFinallyBlock(llvm::LLVMContext &context,
1000 llvm::Module &module,
1001 llvm::IRBuilder<> &builder,
Garrison Venn88bd9d62011-04-10 14:06:52 +00001002 llvm::Function &toAddTo,
1003 std::string &blockName,
1004 std::string &functionId,
1005 llvm::BasicBlock &terminatorBlock,
1006 llvm::BasicBlock &unwindResumeBlock,
1007 llvm::Value **exceptionCaughtFlag,
Bill Wendling383727b2012-02-04 00:29:12 +00001008 llvm::Value **exceptionStorage,
1009 llvm::Value **caughtResultStorage) {
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001010 assert(exceptionCaughtFlag &&
Chris Lattnera868bbb2011-04-08 18:02:51 +00001011 "ExceptionDemo::createFinallyBlock(...):exceptionCaughtFlag "
1012 "is NULL");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001013 assert(exceptionStorage &&
Chris Lattnera868bbb2011-04-08 18:02:51 +00001014 "ExceptionDemo::createFinallyBlock(...):exceptionStorage "
1015 "is NULL");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001016 assert(caughtResultStorage &&
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001017 "ExceptionDemo::createFinallyBlock(...):caughtResultStorage "
1018 "is NULL");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001019
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001020 *exceptionCaughtFlag = createEntryBlockAlloca(toAddTo,
1021 "exceptionCaught",
1022 ourExceptionNotThrownState->getType(),
1023 ourExceptionNotThrownState);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001024
Chris Lattner6b8eb8c2011-07-18 04:52:09 +00001025 llvm::PointerType *exceptionStorageType = builder.getInt8PtrTy();
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001026 *exceptionStorage = createEntryBlockAlloca(toAddTo,
1027 "exceptionStorage",
1028 exceptionStorageType,
1029 llvm::ConstantPointerNull::get(
1030 exceptionStorageType));
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001031 *caughtResultStorage = createEntryBlockAlloca(toAddTo,
1032 "caughtResultStorage",
1033 ourCaughtResultType,
1034 llvm::ConstantAggregateZero::get(
1035 ourCaughtResultType));
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001036
Chris Lattnera868bbb2011-04-08 18:02:51 +00001037 llvm::BasicBlock *ret = llvm::BasicBlock::Create(context,
1038 blockName,
1039 &toAddTo);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001040
Chris Lattnera868bbb2011-04-08 18:02:51 +00001041 builder.SetInsertPoint(ret);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001042
Chris Lattnera868bbb2011-04-08 18:02:51 +00001043 std::ostringstream bufferToPrint;
1044 bufferToPrint << "Gen: Executing finally block "
1045 << blockName << " in " << functionId << "\n";
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001046 generateStringPrint(context,
1047 module,
1048 builder,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001049 bufferToPrint.str(),
1050 USE_GLOBAL_STR_CONSTS);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001051
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001052 llvm::SwitchInst *theSwitch = builder.CreateSwitch(builder.CreateLoad(
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001053 *exceptionCaughtFlag),
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001054 &terminatorBlock,
1055 2);
Chris Lattnera868bbb2011-04-08 18:02:51 +00001056 theSwitch->addCase(ourExceptionCaughtState, &terminatorBlock);
1057 theSwitch->addCase(ourExceptionThrownState, &unwindResumeBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001058
Chris Lattnera868bbb2011-04-08 18:02:51 +00001059 return(ret);
Garrison Vennf4d2f842010-02-09 23:22:43 +00001060}
1061
1062
1063/// Generates catch block semantics which print a string to indicate type of
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001064/// catch executed, sets an exception caught flag, and executes passed in
Garrison Vennf4d2f842010-02-09 23:22:43 +00001065/// end block (terminator block).
1066/// @param context llvm context
1067/// @param module code for module instance
1068/// @param builder builder instance
1069/// @param toAddTo parent function to add block to
1070/// @param blockName block name of new "catch" block.
1071/// @param functionId output id used for printing
1072/// @param terminatorBlock terminator "end" block
1073/// @param exceptionCaughtFlag exception caught/thrown status
1074/// @returns newly created block
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001075static llvm::BasicBlock *createCatchBlock(llvm::LLVMContext &context,
1076 llvm::Module &module,
1077 llvm::IRBuilder<> &builder,
Garrison Venn88bd9d62011-04-10 14:06:52 +00001078 llvm::Function &toAddTo,
1079 std::string &blockName,
1080 std::string &functionId,
1081 llvm::BasicBlock &terminatorBlock,
1082 llvm::Value &exceptionCaughtFlag) {
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001083
Chris Lattnera868bbb2011-04-08 18:02:51 +00001084 llvm::BasicBlock *ret = llvm::BasicBlock::Create(context,
1085 blockName,
1086 &toAddTo);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001087
Chris Lattnera868bbb2011-04-08 18:02:51 +00001088 builder.SetInsertPoint(ret);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001089
Chris Lattnera868bbb2011-04-08 18:02:51 +00001090 std::ostringstream bufferToPrint;
1091 bufferToPrint << "Gen: Executing catch block "
1092 << blockName
1093 << " in "
1094 << functionId
1095 << std::endl;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001096 generateStringPrint(context,
1097 module,
1098 builder,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001099 bufferToPrint.str(),
1100 USE_GLOBAL_STR_CONSTS);
1101 builder.CreateStore(ourExceptionCaughtState, &exceptionCaughtFlag);
1102 builder.CreateBr(&terminatorBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001103
Chris Lattnera868bbb2011-04-08 18:02:51 +00001104 return(ret);
Garrison Vennf4d2f842010-02-09 23:22:43 +00001105}
1106
1107
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001108/// Generates a function which invokes a function (toInvoke) and, whose
1109/// unwind block will "catch" the type info types correspondingly held in the
1110/// exceptionTypesToCatch argument. If the toInvoke function throws an
1111/// exception which does not match any type info types contained in
1112/// exceptionTypesToCatch, the generated code will call _Unwind_Resume
1113/// with the raised exception. On the other hand the generated code will
Garrison Vennf4d2f842010-02-09 23:22:43 +00001114/// normally exit if the toInvoke function does not throw an exception.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001115/// The generated "finally" block is always run regardless of the cause of
Garrison Vennf4d2f842010-02-09 23:22:43 +00001116/// the generated function exit.
1117/// The generated function is returned after being verified.
1118/// @param module code for module instance
1119/// @param builder builder instance
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001120/// @param fpm a function pass manager holding optional IR to IR
Garrison Vennf4d2f842010-02-09 23:22:43 +00001121/// transformations
1122/// @param toInvoke inner function to invoke
1123/// @param ourId id used to printing purposes
1124/// @param numExceptionsToCatch length of exceptionTypesToCatch array
1125/// @param exceptionTypesToCatch array of type info types to "catch"
1126/// @returns generated function
Chandler Carruth7ecd9912015-02-13 10:21:05 +00001127static llvm::Function *createCatchWrappedInvokeFunction(
1128 llvm::Module &module, llvm::IRBuilder<> &builder,
1129 llvm::legacy::FunctionPassManager &fpm, llvm::Function &toInvoke,
1130 std::string ourId, unsigned numExceptionsToCatch,
1131 unsigned exceptionTypesToCatch[]) {
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001132
Garrison Venn88bd9d62011-04-10 14:06:52 +00001133 llvm::LLVMContext &context = module.getContext();
Chris Lattnera868bbb2011-04-08 18:02:51 +00001134 llvm::Function *toPrint32Int = module.getFunction("print32Int");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001135
Chris Lattnera868bbb2011-04-08 18:02:51 +00001136 ArgTypes argTypes;
Garrison Venn76310ac2011-07-12 15:34:42 +00001137 argTypes.push_back(builder.getInt32Ty());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001138
Chris Lattnera868bbb2011-04-08 18:02:51 +00001139 ArgNames argNames;
1140 argNames.push_back("exceptTypeToThrow");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001141
1142 llvm::Function *ret = createFunction(module,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001143 builder.getVoidTy(),
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001144 argTypes,
1145 argNames,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001146 ourId,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001147 llvm::Function::ExternalLinkage,
1148 false,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001149 false);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001150
Chris Lattnera868bbb2011-04-08 18:02:51 +00001151 // Block which calls invoke
1152 llvm::BasicBlock *entryBlock = llvm::BasicBlock::Create(context,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001153 "entry",
Chris Lattnera868bbb2011-04-08 18:02:51 +00001154 ret);
1155 // Normal block for invoke
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001156 llvm::BasicBlock *normalBlock = llvm::BasicBlock::Create(context,
1157 "normal",
Chris Lattnera868bbb2011-04-08 18:02:51 +00001158 ret);
1159 // Unwind block for invoke
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001160 llvm::BasicBlock *exceptionBlock = llvm::BasicBlock::Create(context,
1161 "exception",
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001162 ret);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001163
Chris Lattnera868bbb2011-04-08 18:02:51 +00001164 // Block which routes exception to correct catch handler block
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001165 llvm::BasicBlock *exceptionRouteBlock = llvm::BasicBlock::Create(context,
1166 "exceptionRoute",
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001167 ret);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001168
Chris Lattnera868bbb2011-04-08 18:02:51 +00001169 // Foreign exception handler
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001170 llvm::BasicBlock *externalExceptionBlock = llvm::BasicBlock::Create(context,
1171 "externalException",
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001172 ret);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001173
Chris Lattnera868bbb2011-04-08 18:02:51 +00001174 // Block which calls _Unwind_Resume
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001175 llvm::BasicBlock *unwindResumeBlock = llvm::BasicBlock::Create(context,
1176 "unwindResume",
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001177 ret);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001178
Chris Lattnera868bbb2011-04-08 18:02:51 +00001179 // Clean up block which delete exception if needed
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001180 llvm::BasicBlock *endBlock = llvm::BasicBlock::Create(context, "end", ret);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001181
Chris Lattnera868bbb2011-04-08 18:02:51 +00001182 std::string nextName;
1183 std::vector<llvm::BasicBlock*> catchBlocks(numExceptionsToCatch);
Garrison Venn88bd9d62011-04-10 14:06:52 +00001184 llvm::Value *exceptionCaughtFlag = NULL;
1185 llvm::Value *exceptionStorage = NULL;
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001186 llvm::Value *caughtResultStorage = NULL;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001187
1188 // Finally block which will branch to unwindResumeBlock if
Chris Lattnera868bbb2011-04-08 18:02:51 +00001189 // exception is not caught. Initializes/allocates stack locations.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001190 llvm::BasicBlock *finallyBlock = createFinallyBlock(context,
1191 module,
1192 builder,
1193 *ret,
1194 nextName = "finally",
Chris Lattnera868bbb2011-04-08 18:02:51 +00001195 ourId,
1196 *endBlock,
1197 *unwindResumeBlock,
1198 &exceptionCaughtFlag,
Bill Wendling383727b2012-02-04 00:29:12 +00001199 &exceptionStorage,
1200 &caughtResultStorage
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001201 );
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001202
Chris Lattnera868bbb2011-04-08 18:02:51 +00001203 for (unsigned i = 0; i < numExceptionsToCatch; ++i) {
1204 nextName = ourTypeInfoNames[exceptionTypesToCatch[i]];
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001205
Chris Lattnera868bbb2011-04-08 18:02:51 +00001206 // One catch block per type info to be caught
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001207 catchBlocks[i] = createCatchBlock(context,
1208 module,
1209 builder,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001210 *ret,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001211 nextName,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001212 ourId,
1213 *finallyBlock,
1214 *exceptionCaughtFlag);
1215 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001216
Chris Lattnera868bbb2011-04-08 18:02:51 +00001217 // Entry Block
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001218
Chris Lattnera868bbb2011-04-08 18:02:51 +00001219 builder.SetInsertPoint(entryBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001220
Chris Lattnera868bbb2011-04-08 18:02:51 +00001221 std::vector<llvm::Value*> args;
1222 args.push_back(namedValues["exceptTypeToThrow"]);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001223 builder.CreateInvoke(&toInvoke,
1224 normalBlock,
1225 exceptionBlock,
Chris Lattner6b8eb8c2011-07-18 04:52:09 +00001226 args);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001227
Chris Lattnera868bbb2011-04-08 18:02:51 +00001228 // End Block
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001229
Chris Lattnera868bbb2011-04-08 18:02:51 +00001230 builder.SetInsertPoint(endBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001231
1232 generateStringPrint(context,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001233 module,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001234 builder,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001235 "Gen: In end block: exiting in " + ourId + ".\n",
1236 USE_GLOBAL_STR_CONSTS);
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001237 llvm::Function *deleteOurException = module.getFunction("deleteOurException");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001238
Chris Lattnera868bbb2011-04-08 18:02:51 +00001239 // Note: function handles NULL exceptions
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001240 builder.CreateCall(deleteOurException,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001241 builder.CreateLoad(exceptionStorage));
1242 builder.CreateRetVoid();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001243
Chris Lattnera868bbb2011-04-08 18:02:51 +00001244 // Normal Block
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001245
Chris Lattnera868bbb2011-04-08 18:02:51 +00001246 builder.SetInsertPoint(normalBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001247
1248 generateStringPrint(context,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001249 module,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001250 builder,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001251 "Gen: No exception in " + ourId + "!\n",
1252 USE_GLOBAL_STR_CONSTS);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001253
Chris Lattnera868bbb2011-04-08 18:02:51 +00001254 // Finally block is always called
1255 builder.CreateBr(finallyBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001256
Chris Lattnera868bbb2011-04-08 18:02:51 +00001257 // Unwind Resume Block
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001258
Chris Lattnera868bbb2011-04-08 18:02:51 +00001259 builder.SetInsertPoint(unwindResumeBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001260
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001261 builder.CreateResume(builder.CreateLoad(caughtResultStorage));
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001262
Chris Lattnera868bbb2011-04-08 18:02:51 +00001263 // Exception Block
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001264
Chris Lattnera868bbb2011-04-08 18:02:51 +00001265 builder.SetInsertPoint(exceptionBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001266
Garrison Venn8cb00352011-09-22 15:45:14 +00001267 llvm::Function *personality = module.getFunction("ourPersonality");
David Blaikie08964ca2015-08-14 00:37:16 +00001268 ret->setPersonalityFn(personality);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001269
1270 llvm::LandingPadInst *caughtResult =
Garrison Venn8cb00352011-09-22 15:45:14 +00001271 builder.CreateLandingPad(ourCaughtResultType,
Garrison Venn8cb00352011-09-22 15:45:14 +00001272 numExceptionsToCatch,
1273 "landingPad");
1274
1275 caughtResult->setCleanup(true);
1276
1277 for (unsigned i = 0; i < numExceptionsToCatch; ++i) {
1278 // Set up type infos to be caught
1279 caughtResult->addClause(module.getGlobalVariable(
1280 ourTypeInfoNames[exceptionTypesToCatch[i]]));
1281 }
1282
1283 llvm::Value *unwindException = builder.CreateExtractValue(caughtResult, 0);
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001284 llvm::Value *retTypeInfoIndex = builder.CreateExtractValue(caughtResult, 1);
Garrison Venn8cb00352011-09-22 15:45:14 +00001285
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001286 // FIXME: Redundant storage which, beyond utilizing value of
1287 // caughtResultStore for unwindException storage, may be alleviated
Benjamin Kramerbde91762012-06-02 10:20:22 +00001288 // altogether with a block rearrangement
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001289 builder.CreateStore(caughtResult, caughtResultStorage);
Garrison Venn8cb00352011-09-22 15:45:14 +00001290 builder.CreateStore(unwindException, exceptionStorage);
1291 builder.CreateStore(ourExceptionThrownState, exceptionCaughtFlag);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001292
1293 // Retrieve exception_class member from thrown exception
Chris Lattnera868bbb2011-04-08 18:02:51 +00001294 // (_Unwind_Exception instance). This member tells us whether or not
1295 // the exception is foreign.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001296 llvm::Value *unwindExceptionClass =
David Blaikie83a20fa2015-04-22 04:24:43 +00001297 builder.CreateLoad(builder.CreateStructGEP(
1298 ourUnwindExceptionType,
1299 builder.CreatePointerCast(unwindException,
1300 ourUnwindExceptionType->getPointerTo()),
1301 0));
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001302
Chris Lattnera868bbb2011-04-08 18:02:51 +00001303 // Branch to the externalExceptionBlock if the exception is foreign or
1304 // to a catch router if not. Either way the finally block will be run.
1305 builder.CreateCondBr(builder.CreateICmpEQ(unwindExceptionClass,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001306 llvm::ConstantInt::get(builder.getInt64Ty(),
Chris Lattnera868bbb2011-04-08 18:02:51 +00001307 ourBaseExceptionClass)),
1308 exceptionRouteBlock,
1309 externalExceptionBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001310
Chris Lattnera868bbb2011-04-08 18:02:51 +00001311 // External Exception Block
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001312
Chris Lattnera868bbb2011-04-08 18:02:51 +00001313 builder.SetInsertPoint(externalExceptionBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001314
1315 generateStringPrint(context,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001316 module,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001317 builder,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001318 "Gen: Foreign exception received.\n",
1319 USE_GLOBAL_STR_CONSTS);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001320
Chris Lattnera868bbb2011-04-08 18:02:51 +00001321 // Branch to the finally block
1322 builder.CreateBr(finallyBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001323
Chris Lattnera868bbb2011-04-08 18:02:51 +00001324 // Exception Route Block
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001325
Chris Lattnera868bbb2011-04-08 18:02:51 +00001326 builder.SetInsertPoint(exceptionRouteBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001327
1328 // Casts exception pointer (_Unwind_Exception instance) to parent
Chris Lattnera868bbb2011-04-08 18:02:51 +00001329 // (OurException instance).
1330 //
1331 // Note: ourBaseFromUnwindOffset is usually negative
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001332 llvm::Value *typeInfoThrown = builder.CreatePointerCast(
1333 builder.CreateConstGEP1_64(unwindException,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001334 ourBaseFromUnwindOffset),
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001335 ourExceptionType->getPointerTo());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001336
Chris Lattnera868bbb2011-04-08 18:02:51 +00001337 // Retrieve thrown exception type info type
1338 //
1339 // Note: Index is not relative to pointer but instead to structure
1340 // unlike a true getelementptr (GEP) instruction
David Blaikie83a20fa2015-04-22 04:24:43 +00001341 typeInfoThrown = builder.CreateStructGEP(ourExceptionType, typeInfoThrown, 0);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001342
1343 llvm::Value *typeInfoThrownType =
David Blaikie83a20fa2015-04-22 04:24:43 +00001344 builder.CreateStructGEP(builder.getInt8PtrTy(), typeInfoThrown, 0);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001345
1346 generateIntegerPrint(context,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001347 module,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001348 builder,
1349 *toPrint32Int,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001350 *(builder.CreateLoad(typeInfoThrownType)),
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001351 "Gen: Exception type <%d> received (stack unwound) "
1352 " in " +
1353 ourId +
Chris Lattnera868bbb2011-04-08 18:02:51 +00001354 ".\n",
1355 USE_GLOBAL_STR_CONSTS);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001356
Chris Lattnera868bbb2011-04-08 18:02:51 +00001357 // Route to matched type info catch block or run cleanup finally block
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001358 llvm::SwitchInst *switchToCatchBlock = builder.CreateSwitch(retTypeInfoIndex,
1359 finallyBlock,
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001360 numExceptionsToCatch);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001361
Chris Lattnera868bbb2011-04-08 18:02:51 +00001362 unsigned nextTypeToCatch;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001363
Chris Lattnera868bbb2011-04-08 18:02:51 +00001364 for (unsigned i = 1; i <= numExceptionsToCatch; ++i) {
1365 nextTypeToCatch = i - 1;
1366 switchToCatchBlock->addCase(llvm::ConstantInt::get(
1367 llvm::Type::getInt32Ty(context), i),
1368 catchBlocks[nextTypeToCatch]);
1369 }
Garrison Venneb89d362011-09-22 14:07:50 +00001370
Chris Lattnera868bbb2011-04-08 18:02:51 +00001371 llvm::verifyFunction(*ret);
1372 fpm.run(*ret);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001373
Chris Lattnera868bbb2011-04-08 18:02:51 +00001374 return(ret);
Garrison Vennf4d2f842010-02-09 23:22:43 +00001375}
1376
1377
1378/// Generates function which throws either an exception matched to a runtime
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001379/// determined type info type (argument to generated function), or if this
1380/// runtime value matches nativeThrowType, throws a foreign exception by
Garrison Vennf4d2f842010-02-09 23:22:43 +00001381/// calling nativeThrowFunct.
1382/// @param module code for module instance
1383/// @param builder builder instance
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001384/// @param fpm a function pass manager holding optional IR to IR
Garrison Vennf4d2f842010-02-09 23:22:43 +00001385/// transformations
1386/// @param ourId id used to printing purposes
1387/// @param nativeThrowType a runtime argument of this value results in
1388/// nativeThrowFunct being called to generate/throw exception.
1389/// @param nativeThrowFunct function which will throw a foreign exception
1390/// if the above nativeThrowType matches generated function's arg.
1391/// @returns generated function
Chandler Carruth7ecd9912015-02-13 10:21:05 +00001392static llvm::Function *
1393createThrowExceptionFunction(llvm::Module &module, llvm::IRBuilder<> &builder,
1394 llvm::legacy::FunctionPassManager &fpm,
1395 std::string ourId, int32_t nativeThrowType,
1396 llvm::Function &nativeThrowFunct) {
Garrison Venn88bd9d62011-04-10 14:06:52 +00001397 llvm::LLVMContext &context = module.getContext();
Chris Lattnera868bbb2011-04-08 18:02:51 +00001398 namedValues.clear();
1399 ArgTypes unwindArgTypes;
Garrison Venn76310ac2011-07-12 15:34:42 +00001400 unwindArgTypes.push_back(builder.getInt32Ty());
Chris Lattnera868bbb2011-04-08 18:02:51 +00001401 ArgNames unwindArgNames;
1402 unwindArgNames.push_back("exceptTypeToThrow");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001403
Chris Lattnera868bbb2011-04-08 18:02:51 +00001404 llvm::Function *ret = createFunction(module,
1405 builder.getVoidTy(),
1406 unwindArgTypes,
1407 unwindArgNames,
1408 ourId,
1409 llvm::Function::ExternalLinkage,
1410 false,
1411 false);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001412
Chris Lattnera868bbb2011-04-08 18:02:51 +00001413 // Throws either one of our exception or a native C++ exception depending
1414 // on a runtime argument value containing a type info type.
1415 llvm::BasicBlock *entryBlock = llvm::BasicBlock::Create(context,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001416 "entry",
Chris Lattnera868bbb2011-04-08 18:02:51 +00001417 ret);
1418 // Throws a foreign exception
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001419 llvm::BasicBlock *nativeThrowBlock = llvm::BasicBlock::Create(context,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001420 "nativeThrow",
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001421 ret);
Chris Lattnera868bbb2011-04-08 18:02:51 +00001422 // Throws one of our Exceptions
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001423 llvm::BasicBlock *generatedThrowBlock = llvm::BasicBlock::Create(context,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001424 "generatedThrow",
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001425 ret);
Chris Lattnera868bbb2011-04-08 18:02:51 +00001426 // Retrieved runtime type info type to throw
Garrison Venn88bd9d62011-04-10 14:06:52 +00001427 llvm::Value *exceptionType = namedValues["exceptTypeToThrow"];
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001428
Chris Lattnera868bbb2011-04-08 18:02:51 +00001429 // nativeThrowBlock block
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001430
Chris Lattnera868bbb2011-04-08 18:02:51 +00001431 builder.SetInsertPoint(nativeThrowBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001432
Chris Lattnera868bbb2011-04-08 18:02:51 +00001433 // Throws foreign exception
1434 builder.CreateCall(&nativeThrowFunct, exceptionType);
1435 builder.CreateUnreachable();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001436
Chris Lattnera868bbb2011-04-08 18:02:51 +00001437 // entry block
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001438
Chris Lattnera868bbb2011-04-08 18:02:51 +00001439 builder.SetInsertPoint(entryBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001440
Chris Lattnera868bbb2011-04-08 18:02:51 +00001441 llvm::Function *toPrint32Int = module.getFunction("print32Int");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001442 generateIntegerPrint(context,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001443 module,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001444 builder,
1445 *toPrint32Int,
1446 *exceptionType,
1447 "\nGen: About to throw exception type <%d> in " +
1448 ourId +
Chris Lattnera868bbb2011-04-08 18:02:51 +00001449 ".\n",
1450 USE_GLOBAL_STR_CONSTS);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001451
Chris Lattnera868bbb2011-04-08 18:02:51 +00001452 // Switches on runtime type info type value to determine whether or not
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001453 // a foreign exception is thrown. Defaults to throwing one of our
Chris Lattnera868bbb2011-04-08 18:02:51 +00001454 // generated exceptions.
Garrison Venn88bd9d62011-04-10 14:06:52 +00001455 llvm::SwitchInst *theSwitch = builder.CreateSwitch(exceptionType,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001456 generatedThrowBlock,
1457 1);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001458
1459 theSwitch->addCase(llvm::ConstantInt::get(llvm::Type::getInt32Ty(context),
Chris Lattnera868bbb2011-04-08 18:02:51 +00001460 nativeThrowType),
1461 nativeThrowBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001462
Chris Lattnera868bbb2011-04-08 18:02:51 +00001463 // generatedThrow block
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001464
Chris Lattnera868bbb2011-04-08 18:02:51 +00001465 builder.SetInsertPoint(generatedThrowBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001466
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001467 llvm::Function *createOurException = module.getFunction("createOurException");
1468 llvm::Function *raiseOurException = module.getFunction(
1469 "_Unwind_RaiseException");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001470
Chris Lattnera868bbb2011-04-08 18:02:51 +00001471 // Creates exception to throw with runtime type info type.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001472 llvm::Value *exception = builder.CreateCall(createOurException,
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001473 namedValues["exceptTypeToThrow"]);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001474
Chris Lattnera868bbb2011-04-08 18:02:51 +00001475 // Throw generated Exception
1476 builder.CreateCall(raiseOurException, exception);
1477 builder.CreateUnreachable();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001478
Chris Lattnera868bbb2011-04-08 18:02:51 +00001479 llvm::verifyFunction(*ret);
1480 fpm.run(*ret);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001481
Chris Lattnera868bbb2011-04-08 18:02:51 +00001482 return(ret);
Garrison Vennf4d2f842010-02-09 23:22:43 +00001483}
1484
1485static void createStandardUtilityFunctions(unsigned numTypeInfos,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001486 llvm::Module &module,
Garrison Venn88bd9d62011-04-10 14:06:52 +00001487 llvm::IRBuilder<> &builder);
Garrison Vennf4d2f842010-02-09 23:22:43 +00001488
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001489/// Creates test code by generating and organizing these functions into the
Garrison Vennf4d2f842010-02-09 23:22:43 +00001490/// test case. The test case consists of an outer function setup to invoke
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001491/// an inner function within an environment having multiple catch and single
Garrison Vennf4d2f842010-02-09 23:22:43 +00001492/// finally blocks. This inner function is also setup to invoke a throw
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001493/// function within an evironment similar in nature to the outer function's
Garrison Vennf4d2f842010-02-09 23:22:43 +00001494/// catch and finally blocks. Each of these two functions catch mutually
1495/// exclusive subsets (even or odd) of the type info types configured
1496/// for this this. All generated functions have a runtime argument which
1497/// holds a type info type to throw that each function takes and passes it
1498/// to the inner one if such a inner function exists. This type info type is
1499/// looked at by the generated throw function to see whether or not it should
1500/// throw a generated exception with the same type info type, or instead call
1501/// a supplied a function which in turn will throw a foreign exception.
1502/// @param module code for module instance
1503/// @param builder builder instance
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001504/// @param fpm a function pass manager holding optional IR to IR
Garrison Vennf4d2f842010-02-09 23:22:43 +00001505/// transformations
1506/// @param nativeThrowFunctName name of external function which will throw
1507/// a foreign exception
1508/// @returns outermost generated test function.
Chandler Carruth7ecd9912015-02-13 10:21:05 +00001509llvm::Function *
1510createUnwindExceptionTest(llvm::Module &module, llvm::IRBuilder<> &builder,
1511 llvm::legacy::FunctionPassManager &fpm,
1512 std::string nativeThrowFunctName) {
Chris Lattnera868bbb2011-04-08 18:02:51 +00001513 // Number of type infos to generate
1514 unsigned numTypeInfos = 6;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001515
Chris Lattnera868bbb2011-04-08 18:02:51 +00001516 // Initialze intrisics and external functions to use along with exception
1517 // and type info globals.
1518 createStandardUtilityFunctions(numTypeInfos,
1519 module,
1520 builder);
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001521 llvm::Function *nativeThrowFunct = module.getFunction(nativeThrowFunctName);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001522
1523 // Create exception throw function using the value ~0 to cause
Chris Lattnera868bbb2011-04-08 18:02:51 +00001524 // foreign exceptions to be thrown.
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001525 llvm::Function *throwFunct = createThrowExceptionFunction(module,
1526 builder,
1527 fpm,
1528 "throwFunct",
1529 ~0,
1530 *nativeThrowFunct);
Chris Lattnera868bbb2011-04-08 18:02:51 +00001531 // Inner function will catch even type infos
1532 unsigned innerExceptionTypesToCatch[] = {6, 2, 4};
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001533 size_t numExceptionTypesToCatch = sizeof(innerExceptionTypesToCatch) /
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001534 sizeof(unsigned);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001535
Chris Lattnera868bbb2011-04-08 18:02:51 +00001536 // Generate inner function.
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001537 llvm::Function *innerCatchFunct = createCatchWrappedInvokeFunction(module,
1538 builder,
1539 fpm,
1540 *throwFunct,
1541 "innerCatchFunct",
1542 numExceptionTypesToCatch,
1543 innerExceptionTypesToCatch);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001544
Chris Lattnera868bbb2011-04-08 18:02:51 +00001545 // Outer function will catch odd type infos
1546 unsigned outerExceptionTypesToCatch[] = {3, 1, 5};
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001547 numExceptionTypesToCatch = sizeof(outerExceptionTypesToCatch) /
Chris Lattnera868bbb2011-04-08 18:02:51 +00001548 sizeof(unsigned);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001549
Chris Lattnera868bbb2011-04-08 18:02:51 +00001550 // Generate outer function
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001551 llvm::Function *outerCatchFunct = createCatchWrappedInvokeFunction(module,
1552 builder,
1553 fpm,
1554 *innerCatchFunct,
1555 "outerCatchFunct",
1556 numExceptionTypesToCatch,
1557 outerExceptionTypesToCatch);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001558
Chris Lattnera868bbb2011-04-08 18:02:51 +00001559 // Return outer function to run
1560 return(outerCatchFunct);
Garrison Vennf4d2f842010-02-09 23:22:43 +00001561}
1562
Juergen Ributzka05c5a932013-11-19 03:08:35 +00001563namespace {
Garrison Vennf4d2f842010-02-09 23:22:43 +00001564/// Represents our foreign exceptions
1565class OurCppRunException : public std::runtime_error {
1566public:
Chris Lattnera868bbb2011-04-08 18:02:51 +00001567 OurCppRunException(const std::string reason) :
1568 std::runtime_error(reason) {}
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001569
Garrison Venn88bd9d62011-04-10 14:06:52 +00001570 OurCppRunException (const OurCppRunException &toCopy) :
Chris Lattnera868bbb2011-04-08 18:02:51 +00001571 std::runtime_error(toCopy) {}
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001572
Garrison Venn88bd9d62011-04-10 14:06:52 +00001573 OurCppRunException &operator = (const OurCppRunException &toCopy) {
Chris Lattnera868bbb2011-04-08 18:02:51 +00001574 return(reinterpret_cast<OurCppRunException&>(
1575 std::runtime_error::operator=(toCopy)));
1576 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001577
Alexander Kornienkof817c1c2015-04-11 02:11:45 +00001578 ~OurCppRunException(void) throw() override {}
Garrison Vennf4d2f842010-02-09 23:22:43 +00001579};
Juergen Ributzka05c5a932013-11-19 03:08:35 +00001580} // end anonymous namespace
Garrison Vennf4d2f842010-02-09 23:22:43 +00001581
1582/// Throws foreign C++ exception.
1583/// @param ignoreIt unused parameter that allows function to match implied
1584/// generated function contract.
1585extern "C"
1586void throwCppException (int32_t ignoreIt) {
Chris Lattnera868bbb2011-04-08 18:02:51 +00001587 throw(OurCppRunException("thrown by throwCppException(...)"));
Garrison Vennf4d2f842010-02-09 23:22:43 +00001588}
1589
1590typedef void (*OurExceptionThrowFunctType) (int32_t typeToThrow);
1591
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001592/// This is a test harness which runs test by executing generated
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001593/// function with a type info type to throw. Harness wraps the execution
Garrison Vennf4d2f842010-02-09 23:22:43 +00001594/// of generated function in a C++ try catch clause.
1595/// @param engine execution engine to use for executing generated function.
1596/// This demo program expects this to be a JIT instance for demo
1597/// purposes.
1598/// @param function generated test function to run
1599/// @param typeToThrow type info type of generated exception to throw, or
1600/// indicator to cause foreign exception to be thrown.
1601static
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001602void runExceptionThrow(llvm::ExecutionEngine *engine,
1603 llvm::Function *function,
Garrison Vennf4d2f842010-02-09 23:22:43 +00001604 int32_t typeToThrow) {
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001605
Chris Lattnera868bbb2011-04-08 18:02:51 +00001606 // Find test's function pointer
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001607 OurExceptionThrowFunctType functPtr =
Chris Lattnera868bbb2011-04-08 18:02:51 +00001608 reinterpret_cast<OurExceptionThrowFunctType>(
1609 reinterpret_cast<intptr_t>(engine->getPointerToFunction(function)));
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001610
Chris Lattnera868bbb2011-04-08 18:02:51 +00001611 try {
1612 // Run test
1613 (*functPtr)(typeToThrow);
1614 }
1615 catch (OurCppRunException exc) {
1616 // Catch foreign C++ exception
1617 fprintf(stderr,
1618 "\nrunExceptionThrow(...):In C++ catch OurCppRunException "
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001619 "with reason: %s.\n",
Chris Lattnera868bbb2011-04-08 18:02:51 +00001620 exc.what());
1621 }
1622 catch (...) {
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001623 // Catch all exceptions including our generated ones. This latter
Garrison Venn56c58ce2011-09-28 10:53:56 +00001624 // functionality works according to the example in rules 1.6.4 of
Dmitri Gribenko462462e2013-01-13 15:53:09 +00001625 // http://mentorembedded.github.com/cxx-abi/abi-eh.html (v1.22),
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001626 // given that these will be exceptions foreign to C++
1627 // (the _Unwind_Exception::exception_class should be different from
Garrison Venn56c58ce2011-09-28 10:53:56 +00001628 // the one used by C++).
Chris Lattnera868bbb2011-04-08 18:02:51 +00001629 fprintf(stderr,
1630 "\nrunExceptionThrow(...):In C++ catch all.\n");
1631 }
Garrison Vennf4d2f842010-02-09 23:22:43 +00001632}
1633
1634//
1635// End test functions
1636//
1637
Garrison Venn5fb3f662011-07-11 16:31:53 +00001638typedef llvm::ArrayRef<llvm::Type*> TypeArray;
Chris Lattnerca320f52011-04-08 17:56:47 +00001639
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001640/// This initialization routine creates type info globals and
Garrison Vennf4d2f842010-02-09 23:22:43 +00001641/// adds external function declarations to module.
1642/// @param numTypeInfos number of linear type info associated type info types
1643/// to create as GlobalVariable instances, starting with the value 1.
1644/// @param module code for module instance
1645/// @param builder builder instance
1646static void createStandardUtilityFunctions(unsigned numTypeInfos,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001647 llvm::Module &module,
Garrison Venn88bd9d62011-04-10 14:06:52 +00001648 llvm::IRBuilder<> &builder) {
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001649
Garrison Venn88bd9d62011-04-10 14:06:52 +00001650 llvm::LLVMContext &context = module.getContext();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001651
Chris Lattnera868bbb2011-04-08 18:02:51 +00001652 // Exception initializations
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001653
Chris Lattnera868bbb2011-04-08 18:02:51 +00001654 // Setup exception catch state
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001655 ourExceptionNotThrownState =
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001656 llvm::ConstantInt::get(llvm::Type::getInt8Ty(context), 0),
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001657 ourExceptionThrownState =
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001658 llvm::ConstantInt::get(llvm::Type::getInt8Ty(context), 1),
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001659 ourExceptionCaughtState =
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001660 llvm::ConstantInt::get(llvm::Type::getInt8Ty(context), 2),
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001661
1662
1663
Chris Lattnera868bbb2011-04-08 18:02:51 +00001664 // Create our type info type
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001665 ourTypeInfoType = llvm::StructType::get(context,
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001666 TypeArray(builder.getInt32Ty()));
Garrison Venn8cb00352011-09-22 15:45:14 +00001667
Garrison Venn8cb00352011-09-22 15:45:14 +00001668 llvm::Type *caughtResultFieldTypes[] = {
1669 builder.getInt8PtrTy(),
1670 builder.getInt32Ty()
1671 };
1672
1673 // Create our landingpad result type
1674 ourCaughtResultType = llvm::StructType::get(context,
1675 TypeArray(caughtResultFieldTypes));
1676
Chris Lattnera868bbb2011-04-08 18:02:51 +00001677 // Create OurException type
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001678 ourExceptionType = llvm::StructType::get(context,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001679 TypeArray(ourTypeInfoType));
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001680
Chris Lattnera868bbb2011-04-08 18:02:51 +00001681 // Create portion of _Unwind_Exception type
1682 //
1683 // Note: Declaring only a portion of the _Unwind_Exception struct.
1684 // Does this cause problems?
1685 ourUnwindExceptionType =
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001686 llvm::StructType::get(context,
Garrison Venn76310ac2011-07-12 15:34:42 +00001687 TypeArray(builder.getInt64Ty()));
Garrison Venn5fb3f662011-07-11 16:31:53 +00001688
Chris Lattnera868bbb2011-04-08 18:02:51 +00001689 struct OurBaseException_t dummyException;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001690
Chris Lattnera868bbb2011-04-08 18:02:51 +00001691 // Calculate offset of OurException::unwindException member.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001692 ourBaseFromUnwindOffset = ((uintptr_t) &dummyException) -
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001693 ((uintptr_t) &(dummyException.unwindException));
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001694
Garrison Vennf4d2f842010-02-09 23:22:43 +00001695#ifdef DEBUG
Chris Lattnera868bbb2011-04-08 18:02:51 +00001696 fprintf(stderr,
1697 "createStandardUtilityFunctions(...):ourBaseFromUnwindOffset "
David Blaikief4057fe2015-08-14 00:31:49 +00001698 "= %" PRIi64 ", sizeof(struct OurBaseException_t) - "
Chris Lattnera868bbb2011-04-08 18:02:51 +00001699 "sizeof(struct _Unwind_Exception) = %lu.\n",
1700 ourBaseFromUnwindOffset,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001701 sizeof(struct OurBaseException_t) -
Chris Lattnera868bbb2011-04-08 18:02:51 +00001702 sizeof(struct _Unwind_Exception));
Garrison Vennf4d2f842010-02-09 23:22:43 +00001703#endif
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001704
Chris Lattnera868bbb2011-04-08 18:02:51 +00001705 size_t numChars = sizeof(ourBaseExcpClassChars) / sizeof(char);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001706
Chris Lattnera868bbb2011-04-08 18:02:51 +00001707 // Create our _Unwind_Exception::exception_class value
1708 ourBaseExceptionClass = genClass(ourBaseExcpClassChars, numChars);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001709
Chris Lattnera868bbb2011-04-08 18:02:51 +00001710 // Type infos
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001711
Chris Lattnera868bbb2011-04-08 18:02:51 +00001712 std::string baseStr = "typeInfo", typeInfoName;
1713 std::ostringstream typeInfoNameBuilder;
1714 std::vector<llvm::Constant*> structVals;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001715
Chris Lattnera868bbb2011-04-08 18:02:51 +00001716 llvm::Constant *nextStruct;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001717
Chris Lattnera868bbb2011-04-08 18:02:51 +00001718 // Generate each type info
1719 //
1720 // Note: First type info is not used.
1721 for (unsigned i = 0; i <= numTypeInfos; ++i) {
1722 structVals.clear();
1723 structVals.push_back(llvm::ConstantInt::get(builder.getInt32Ty(), i));
1724 nextStruct = llvm::ConstantStruct::get(ourTypeInfoType, structVals);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001725
Chris Lattnera868bbb2011-04-08 18:02:51 +00001726 typeInfoNameBuilder.str("");
1727 typeInfoNameBuilder << baseStr << i;
1728 typeInfoName = typeInfoNameBuilder.str();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001729
Chris Lattnera868bbb2011-04-08 18:02:51 +00001730 // Note: Does not seem to work without allocation
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001731 new llvm::GlobalVariable(module,
1732 ourTypeInfoType,
1733 true,
1734 llvm::GlobalValue::ExternalLinkage,
1735 nextStruct,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001736 typeInfoName);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001737
Chris Lattnera868bbb2011-04-08 18:02:51 +00001738 ourTypeInfoNames.push_back(typeInfoName);
1739 ourTypeInfoNamesIndex[i] = typeInfoName;
1740 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001741
Chris Lattnera868bbb2011-04-08 18:02:51 +00001742 ArgNames argNames;
1743 ArgTypes argTypes;
Garrison Venn88bd9d62011-04-10 14:06:52 +00001744 llvm::Function *funct = NULL;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001745
Chris Lattnera868bbb2011-04-08 18:02:51 +00001746 // print32Int
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001747
Chris Lattner6b8eb8c2011-07-18 04:52:09 +00001748 llvm::Type *retType = builder.getVoidTy();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001749
Chris Lattnera868bbb2011-04-08 18:02:51 +00001750 argTypes.clear();
Garrison Venn76310ac2011-07-12 15:34:42 +00001751 argTypes.push_back(builder.getInt32Ty());
1752 argTypes.push_back(builder.getInt8PtrTy());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001753
Chris Lattnera868bbb2011-04-08 18:02:51 +00001754 argNames.clear();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001755
1756 createFunction(module,
1757 retType,
1758 argTypes,
1759 argNames,
1760 "print32Int",
1761 llvm::Function::ExternalLinkage,
1762 true,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001763 false);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001764
Chris Lattnera868bbb2011-04-08 18:02:51 +00001765 // print64Int
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001766
Chris Lattnera868bbb2011-04-08 18:02:51 +00001767 retType = builder.getVoidTy();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001768
Chris Lattnera868bbb2011-04-08 18:02:51 +00001769 argTypes.clear();
Garrison Venn76310ac2011-07-12 15:34:42 +00001770 argTypes.push_back(builder.getInt64Ty());
1771 argTypes.push_back(builder.getInt8PtrTy());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001772
Chris Lattnera868bbb2011-04-08 18:02:51 +00001773 argNames.clear();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001774
1775 createFunction(module,
1776 retType,
1777 argTypes,
1778 argNames,
1779 "print64Int",
1780 llvm::Function::ExternalLinkage,
1781 true,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001782 false);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001783
Chris Lattnera868bbb2011-04-08 18:02:51 +00001784 // printStr
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001785
Chris Lattnera868bbb2011-04-08 18:02:51 +00001786 retType = builder.getVoidTy();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001787
Chris Lattnera868bbb2011-04-08 18:02:51 +00001788 argTypes.clear();
Garrison Venn76310ac2011-07-12 15:34:42 +00001789 argTypes.push_back(builder.getInt8PtrTy());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001790
Chris Lattnera868bbb2011-04-08 18:02:51 +00001791 argNames.clear();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001792
1793 createFunction(module,
1794 retType,
1795 argTypes,
1796 argNames,
1797 "printStr",
1798 llvm::Function::ExternalLinkage,
1799 true,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001800 false);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001801
Chris Lattnera868bbb2011-04-08 18:02:51 +00001802 // throwCppException
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001803
Chris Lattnera868bbb2011-04-08 18:02:51 +00001804 retType = builder.getVoidTy();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001805
Chris Lattnera868bbb2011-04-08 18:02:51 +00001806 argTypes.clear();
Garrison Venn76310ac2011-07-12 15:34:42 +00001807 argTypes.push_back(builder.getInt32Ty());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001808
Chris Lattnera868bbb2011-04-08 18:02:51 +00001809 argNames.clear();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001810
1811 createFunction(module,
1812 retType,
1813 argTypes,
1814 argNames,
1815 "throwCppException",
1816 llvm::Function::ExternalLinkage,
1817 true,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001818 false);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001819
Chris Lattnera868bbb2011-04-08 18:02:51 +00001820 // deleteOurException
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001821
Chris Lattnera868bbb2011-04-08 18:02:51 +00001822 retType = builder.getVoidTy();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001823
Chris Lattnera868bbb2011-04-08 18:02:51 +00001824 argTypes.clear();
Garrison Venn76310ac2011-07-12 15:34:42 +00001825 argTypes.push_back(builder.getInt8PtrTy());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001826
Chris Lattnera868bbb2011-04-08 18:02:51 +00001827 argNames.clear();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001828
1829 createFunction(module,
1830 retType,
1831 argTypes,
1832 argNames,
1833 "deleteOurException",
1834 llvm::Function::ExternalLinkage,
1835 true,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001836 false);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001837
Chris Lattnera868bbb2011-04-08 18:02:51 +00001838 // createOurException
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001839
Garrison Venn76310ac2011-07-12 15:34:42 +00001840 retType = builder.getInt8PtrTy();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001841
Chris Lattnera868bbb2011-04-08 18:02:51 +00001842 argTypes.clear();
Garrison Venn76310ac2011-07-12 15:34:42 +00001843 argTypes.push_back(builder.getInt32Ty());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001844
Chris Lattnera868bbb2011-04-08 18:02:51 +00001845 argNames.clear();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001846
1847 createFunction(module,
1848 retType,
1849 argTypes,
1850 argNames,
1851 "createOurException",
1852 llvm::Function::ExternalLinkage,
1853 true,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001854 false);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001855
Chris Lattnera868bbb2011-04-08 18:02:51 +00001856 // _Unwind_RaiseException
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001857
Chris Lattnera868bbb2011-04-08 18:02:51 +00001858 retType = builder.getInt32Ty();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001859
Chris Lattnera868bbb2011-04-08 18:02:51 +00001860 argTypes.clear();
Garrison Venn76310ac2011-07-12 15:34:42 +00001861 argTypes.push_back(builder.getInt8PtrTy());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001862
Chris Lattnera868bbb2011-04-08 18:02:51 +00001863 argNames.clear();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001864
1865 funct = createFunction(module,
1866 retType,
1867 argTypes,
1868 argNames,
1869 "_Unwind_RaiseException",
1870 llvm::Function::ExternalLinkage,
1871 true,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001872 false);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001873
NAKAMURA Takumi8489be82012-10-12 14:11:48 +00001874 funct->setDoesNotReturn();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001875
Chris Lattnera868bbb2011-04-08 18:02:51 +00001876 // _Unwind_Resume
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001877
Chris Lattnera868bbb2011-04-08 18:02:51 +00001878 retType = builder.getInt32Ty();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001879
Chris Lattnera868bbb2011-04-08 18:02:51 +00001880 argTypes.clear();
Garrison Venn76310ac2011-07-12 15:34:42 +00001881 argTypes.push_back(builder.getInt8PtrTy());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001882
Chris Lattnera868bbb2011-04-08 18:02:51 +00001883 argNames.clear();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001884
1885 funct = createFunction(module,
1886 retType,
1887 argTypes,
1888 argNames,
1889 "_Unwind_Resume",
1890 llvm::Function::ExternalLinkage,
1891 true,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001892 false);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001893
NAKAMURA Takumi8489be82012-10-12 14:11:48 +00001894 funct->setDoesNotReturn();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001895
Chris Lattnera868bbb2011-04-08 18:02:51 +00001896 // ourPersonality
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001897
Chris Lattnera868bbb2011-04-08 18:02:51 +00001898 retType = builder.getInt32Ty();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001899
Chris Lattnera868bbb2011-04-08 18:02:51 +00001900 argTypes.clear();
Garrison Venn76310ac2011-07-12 15:34:42 +00001901 argTypes.push_back(builder.getInt32Ty());
1902 argTypes.push_back(builder.getInt32Ty());
1903 argTypes.push_back(builder.getInt64Ty());
1904 argTypes.push_back(builder.getInt8PtrTy());
1905 argTypes.push_back(builder.getInt8PtrTy());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001906
Chris Lattnera868bbb2011-04-08 18:02:51 +00001907 argNames.clear();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001908
1909 createFunction(module,
1910 retType,
1911 argTypes,
1912 argNames,
1913 "ourPersonality",
1914 llvm::Function::ExternalLinkage,
1915 true,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001916 false);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001917
Chris Lattnera868bbb2011-04-08 18:02:51 +00001918 // llvm.eh.typeid.for intrinsic
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001919
Chris Lattnera868bbb2011-04-08 18:02:51 +00001920 getDeclaration(&module, llvm::Intrinsic::eh_typeid_for);
Garrison Vennf4d2f842010-02-09 23:22:43 +00001921}
1922
1923
Chris Lattnera868bbb2011-04-08 18:02:51 +00001924//===----------------------------------------------------------------------===//
Garrison Vennf4d2f842010-02-09 23:22:43 +00001925// Main test driver code.
Chris Lattnera868bbb2011-04-08 18:02:51 +00001926//===----------------------------------------------------------------------===//
Garrison Vennf4d2f842010-02-09 23:22:43 +00001927
1928/// Demo main routine which takes the type info types to throw. A test will
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001929/// be run for each given type info type. While type info types with the value
Garrison Vennf4d2f842010-02-09 23:22:43 +00001930/// of -1 will trigger a foreign C++ exception to be thrown; type info types
1931/// <= 6 and >= 1 will be caught by test functions; and type info types > 6
1932/// will result in exceptions which pass through to the test harness. All other
1933/// type info types are not supported and could cause a crash.
Garrison Venn88bd9d62011-04-10 14:06:52 +00001934int main(int argc, char *argv[]) {
Chris Lattnera868bbb2011-04-08 18:02:51 +00001935 if (argc == 1) {
1936 fprintf(stderr,
1937 "\nUsage: ExceptionDemo <exception type to throw> "
1938 "[<type 2>...<type n>].\n"
1939 " Each type must have the value of 1 - 6 for "
1940 "generated exceptions to be caught;\n"
1941 " the value -1 for foreign C++ exceptions to be "
1942 "generated and thrown;\n"
1943 " or the values > 6 for exceptions to be ignored.\n"
1944 "\nTry: ExceptionDemo 2 3 7 -1\n"
1945 " for a full test.\n\n");
1946 return(0);
1947 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001948
Chris Lattnera868bbb2011-04-08 18:02:51 +00001949 // If not set, exception handling will not be turned on
Peter Collingbournedff24782011-12-07 23:58:57 +00001950 llvm::TargetOptions Opts;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001951
Chris Lattnera868bbb2011-04-08 18:02:51 +00001952 llvm::InitializeNativeTarget();
Rafael Espindolae93dc3b2013-05-05 20:57:58 +00001953 llvm::InitializeNativeTargetAsmPrinter();
Garrison Venn88bd9d62011-04-10 14:06:52 +00001954 llvm::LLVMContext &context = llvm::getGlobalContext();
Chris Lattnera868bbb2011-04-08 18:02:51 +00001955 llvm::IRBuilder<> theBuilder(context);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001956
Chris Lattnera868bbb2011-04-08 18:02:51 +00001957 // Make the module, which holds all the code.
Rafael Espindola2a8a2792014-08-19 04:04:25 +00001958 std::unique_ptr<llvm::Module> Owner =
1959 llvm::make_unique<llvm::Module>("my cool jit", context);
1960 llvm::Module *module = Owner.get();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001961
NAKAMURA Takumi4c71cc12014-12-03 02:05:51 +00001962 std::unique_ptr<llvm::RTDyldMemoryManager> MemMgr(new llvm::SectionMemoryManager());
Rafael Espindolae93dc3b2013-05-05 20:57:58 +00001963
Chris Lattnera868bbb2011-04-08 18:02:51 +00001964 // Build engine with JIT
Rafael Espindola2a8a2792014-08-19 04:04:25 +00001965 llvm::EngineBuilder factory(std::move(Owner));
Chris Lattnera868bbb2011-04-08 18:02:51 +00001966 factory.setEngineKind(llvm::EngineKind::JIT);
Peter Collingbournedff24782011-12-07 23:58:57 +00001967 factory.setTargetOptions(Opts);
NAKAMURA Takumi4c71cc12014-12-03 02:05:51 +00001968 factory.setMCJITMemoryManager(std::move(MemMgr));
Garrison Venn88bd9d62011-04-10 14:06:52 +00001969 llvm::ExecutionEngine *executionEngine = factory.create();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001970
Chris Lattnera868bbb2011-04-08 18:02:51 +00001971 {
Chandler Carruth7ecd9912015-02-13 10:21:05 +00001972 llvm::legacy::FunctionPassManager fpm(module);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001973
1974 // Set up the optimizer pipeline.
Chris Lattnera868bbb2011-04-08 18:02:51 +00001975 // Start with registering info about how the
1976 // target lays out data structures.
David Blaikie3b20b042015-08-14 00:24:56 +00001977 module->setDataLayout(executionEngine->getDataLayout());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001978
Chris Lattnera868bbb2011-04-08 18:02:51 +00001979 // Optimizations turned on
1980#ifdef ADD_OPT_PASSES
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001981
Chris Lattnera868bbb2011-04-08 18:02:51 +00001982 // Basic AliasAnslysis support for GVN.
1983 fpm.add(llvm::createBasicAliasAnalysisPass());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001984
Chris Lattnera868bbb2011-04-08 18:02:51 +00001985 // Promote allocas to registers.
1986 fpm.add(llvm::createPromoteMemoryToRegisterPass());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001987
Chris Lattnera868bbb2011-04-08 18:02:51 +00001988 // Do simple "peephole" optimizations and bit-twiddling optzns.
1989 fpm.add(llvm::createInstructionCombiningPass());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001990
Chris Lattnera868bbb2011-04-08 18:02:51 +00001991 // Reassociate expressions.
1992 fpm.add(llvm::createReassociatePass());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001993
Chris Lattnera868bbb2011-04-08 18:02:51 +00001994 // Eliminate Common SubExpressions.
1995 fpm.add(llvm::createGVNPass());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001996
1997 // Simplify the control flow graph (deleting unreachable
Chris Lattnera868bbb2011-04-08 18:02:51 +00001998 // blocks, etc).
1999 fpm.add(llvm::createCFGSimplificationPass());
2000#endif // ADD_OPT_PASSES
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00002001
Chris Lattnera868bbb2011-04-08 18:02:51 +00002002 fpm.doInitialization();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00002003
Chris Lattnera868bbb2011-04-08 18:02:51 +00002004 // Generate test code using function throwCppException(...) as
2005 // the function which throws foreign exceptions.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00002006 llvm::Function *toRun =
2007 createUnwindExceptionTest(*module,
2008 theBuilder,
Garrison Venn8cb00352011-09-22 15:45:14 +00002009 fpm,
2010 "throwCppException");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00002011
Rafael Espindolae93dc3b2013-05-05 20:57:58 +00002012 executionEngine->finalizeObject();
2013
Chris Lattnera868bbb2011-04-08 18:02:51 +00002014 fprintf(stderr, "\nBegin module dump:\n\n");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00002015
Chris Lattnera868bbb2011-04-08 18:02:51 +00002016 module->dump();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00002017
Chris Lattnera868bbb2011-04-08 18:02:51 +00002018 fprintf(stderr, "\nEnd module dump:\n");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00002019
Chris Lattnera868bbb2011-04-08 18:02:51 +00002020 fprintf(stderr, "\n\nBegin Test:\n");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00002021
Chris Lattnera868bbb2011-04-08 18:02:51 +00002022 for (int i = 1; i < argc; ++i) {
2023 // Run test for each argument whose value is the exception
2024 // type to throw.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00002025 runExceptionThrow(executionEngine,
2026 toRun,
Chris Lattnera868bbb2011-04-08 18:02:51 +00002027 (unsigned) strtoul(argv[i], NULL, 10));
2028 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00002029
Chris Lattnera868bbb2011-04-08 18:02:51 +00002030 fprintf(stderr, "\nEnd Test:\n\n");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00002031 }
2032
Chris Lattnera868bbb2011-04-08 18:02:51 +00002033 delete executionEngine;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00002034
Chris Lattnera868bbb2011-04-08 18:02:51 +00002035 return 0;
Garrison Vennf4d2f842010-02-09 23:22:43 +00002036}