blob: 317a326cb9f2abb44f9378a277224b27f220e325 [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
Chandler Carruthd7cd9ac92014-01-13 09:53:45 +000051#include "llvm/IR/Verifier.h"
Rafael Espindolae93dc3b2013-05-05 20:57:58 +000052#include "llvm/ExecutionEngine/MCJIT.h"
53#include "llvm/ExecutionEngine/SectionMemoryManager.h"
Chandler Carruth005f27a2013-01-02 11:56:33 +000054#include "llvm/IR/DataLayout.h"
55#include "llvm/IR/DerivedTypes.h"
56#include "llvm/IR/IRBuilder.h"
57#include "llvm/IR/Intrinsics.h"
58#include "llvm/IR/LLVMContext.h"
Chandler Carruth30d69c22015-02-13 10:01:29 +000059#include "llvm/IR/LegacyPassManager.h"
Chandler Carruth005f27a2013-01-02 11:56:33 +000060#include "llvm/IR/Module.h"
Garrison Vennf4d2f842010-02-09 23:22:43 +000061#include "llvm/Support/Dwarf.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000062#include "llvm/Support/TargetSelect.h"
Chandler Carruth605e30e2012-12-04 10:16:57 +000063#include "llvm/Target/TargetOptions.h"
64#include "llvm/Transforms/Scalar.h"
Garrison Vennf4d2f842010-02-09 23:22:43 +000065
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +000066// FIXME: Although all systems tested with (Linux, OS X), do not need this
67// header file included. A user on ubuntu reported, undefined symbols
Garrison Venna0f6ecb2011-04-12 12:30:10 +000068// for stderr, and fprintf, and the addition of this include fixed the
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +000069// issue for them. Given that LLVM's best practices include the goal
70// of reducing the number of redundant header files included, the
71// correct solution would be to find out why these symbols are not
Garrison Venna0f6ecb2011-04-12 12:30:10 +000072// defined for the system in question, and fix the issue by finding out
73// which LLVM header file, if any, would include these symbols.
Garrison Venn56c5ca22011-04-11 19:52:49 +000074#include <cstdio>
Garrison Venna0f6ecb2011-04-12 12:30:10 +000075
Garrison Vennf4d2f842010-02-09 23:22:43 +000076#include <sstream>
Garrison Vennf4d2f842010-02-09 23:22:43 +000077#include <stdexcept>
78
79
80#ifndef USE_GLOBAL_STR_CONSTS
81#define USE_GLOBAL_STR_CONSTS true
82#endif
83
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +000084// System C++ ABI unwind types from:
Dmitri Gribenko462462e2013-01-13 15:53:09 +000085// http://mentorembedded.github.com/cxx-abi/abi-eh.html (v1.22)
Garrison Vennf4d2f842010-02-09 23:22:43 +000086
87extern "C" {
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +000088
Chris Lattnera868bbb2011-04-08 18:02:51 +000089 typedef enum {
Garrison Vennf4d2f842010-02-09 23:22:43 +000090 _URC_NO_REASON = 0,
91 _URC_FOREIGN_EXCEPTION_CAUGHT = 1,
92 _URC_FATAL_PHASE2_ERROR = 2,
93 _URC_FATAL_PHASE1_ERROR = 3,
94 _URC_NORMAL_STOP = 4,
95 _URC_END_OF_STACK = 5,
96 _URC_HANDLER_FOUND = 6,
97 _URC_INSTALL_CONTEXT = 7,
98 _URC_CONTINUE_UNWIND = 8
Chris Lattnera868bbb2011-04-08 18:02:51 +000099 } _Unwind_Reason_Code;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000100
Chris Lattnera868bbb2011-04-08 18:02:51 +0000101 typedef enum {
Garrison Vennf4d2f842010-02-09 23:22:43 +0000102 _UA_SEARCH_PHASE = 1,
103 _UA_CLEANUP_PHASE = 2,
104 _UA_HANDLER_FRAME = 4,
105 _UA_FORCE_UNWIND = 8,
106 _UA_END_OF_STACK = 16
Chris Lattnera868bbb2011-04-08 18:02:51 +0000107 } _Unwind_Action;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000108
Chris Lattnera868bbb2011-04-08 18:02:51 +0000109 struct _Unwind_Exception;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000110
Chris Lattnera868bbb2011-04-08 18:02:51 +0000111 typedef void (*_Unwind_Exception_Cleanup_Fn) (_Unwind_Reason_Code,
112 struct _Unwind_Exception *);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000113
Chris Lattnera868bbb2011-04-08 18:02:51 +0000114 struct _Unwind_Exception {
Garrison Vennf4d2f842010-02-09 23:22:43 +0000115 uint64_t exception_class;
116 _Unwind_Exception_Cleanup_Fn exception_cleanup;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000117
118 uintptr_t private_1;
119 uintptr_t private_2;
120
Garrison Vennf4d2f842010-02-09 23:22:43 +0000121 // @@@ The IA-64 ABI says that this structure must be double-word aligned.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000122 // Taking that literally does not make much sense generically. Instead
Garrison Vennf4d2f842010-02-09 23:22:43 +0000123 // we provide the maximum alignment required by any type for the machine.
Chris Lattnera868bbb2011-04-08 18:02:51 +0000124 } __attribute__((__aligned__));
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000125
Chris Lattnera868bbb2011-04-08 18:02:51 +0000126 struct _Unwind_Context;
Garrison Venn88bd9d62011-04-10 14:06:52 +0000127 typedef struct _Unwind_Context *_Unwind_Context_t;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000128
Garrison Venn88bd9d62011-04-10 14:06:52 +0000129 extern const uint8_t *_Unwind_GetLanguageSpecificData (_Unwind_Context_t c);
Chris Lattnera868bbb2011-04-08 18:02:51 +0000130 extern uintptr_t _Unwind_GetGR (_Unwind_Context_t c, int i);
131 extern void _Unwind_SetGR (_Unwind_Context_t c, int i, uintptr_t n);
132 extern void _Unwind_SetIP (_Unwind_Context_t, uintptr_t new_value);
133 extern uintptr_t _Unwind_GetIP (_Unwind_Context_t context);
134 extern uintptr_t _Unwind_GetRegionStart (_Unwind_Context_t context);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000135
Garrison Vennf4d2f842010-02-09 23:22:43 +0000136} // extern "C"
137
138//
139// Example types
140//
141
142/// This is our simplistic type info
143struct OurExceptionType_t {
Chris Lattnera868bbb2011-04-08 18:02:51 +0000144 /// type info type
145 int type;
Garrison Vennf4d2f842010-02-09 23:22:43 +0000146};
147
148
149/// This is our Exception class which relies on a negative offset to calculate
150/// pointers to its instances from pointers to its unwindException member.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000151///
Garrison Vennf4d2f842010-02-09 23:22:43 +0000152/// Note: The above unwind.h defines struct _Unwind_Exception to be aligned
153/// on a double word boundary. This is necessary to match the standard:
Dmitri Gribenko462462e2013-01-13 15:53:09 +0000154/// http://mentorembedded.github.com/cxx-abi/abi-eh.html
Garrison Vennf4d2f842010-02-09 23:22:43 +0000155struct OurBaseException_t {
Chris Lattnera868bbb2011-04-08 18:02:51 +0000156 struct OurExceptionType_t type;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000157
Chris Lattnera868bbb2011-04-08 18:02:51 +0000158 // Note: This is properly aligned in unwind.h
159 struct _Unwind_Exception unwindException;
Garrison Vennf4d2f842010-02-09 23:22:43 +0000160};
161
162
163// Note: Not needed since we are C++
164typedef struct OurBaseException_t OurException;
165typedef struct _Unwind_Exception OurUnwindException;
166
167//
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000168// Various globals used to support typeinfo and generatted exceptions in
Garrison Vennf4d2f842010-02-09 23:22:43 +0000169// general
170//
171
172static std::map<std::string, llvm::Value*> namedValues;
173
174int64_t ourBaseFromUnwindOffset;
175
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000176const unsigned char ourBaseExcpClassChars[] =
Chris Lattnera868bbb2011-04-08 18:02:51 +0000177{'o', 'b', 'j', '\0', 'b', 'a', 's', '\0'};
Garrison Vennf4d2f842010-02-09 23:22:43 +0000178
179
180static uint64_t ourBaseExceptionClass = 0;
181
182static std::vector<std::string> ourTypeInfoNames;
183static std::map<int, std::string> ourTypeInfoNamesIndex;
184
Garrison Venn88bd9d62011-04-10 14:06:52 +0000185static llvm::StructType *ourTypeInfoType;
Garrison Venn8cb00352011-09-22 15:45:14 +0000186static llvm::StructType *ourCaughtResultType;
Garrison Venn88bd9d62011-04-10 14:06:52 +0000187static llvm::StructType *ourExceptionType;
188static llvm::StructType *ourUnwindExceptionType;
Garrison Vennf4d2f842010-02-09 23:22:43 +0000189
Garrison Venn88bd9d62011-04-10 14:06:52 +0000190static llvm::ConstantInt *ourExceptionNotThrownState;
191static llvm::ConstantInt *ourExceptionThrownState;
192static llvm::ConstantInt *ourExceptionCaughtState;
Garrison Vennf4d2f842010-02-09 23:22:43 +0000193
194typedef std::vector<std::string> ArgNames;
Garrison Venn5fb3f662011-07-11 16:31:53 +0000195typedef std::vector<llvm::Type*> ArgTypes;
Garrison Vennf4d2f842010-02-09 23:22:43 +0000196
197//
198// Code Generation Utilities
199//
200
201/// Utility used to create a function, both declarations and definitions
202/// @param module for module instance
203/// @param retType function return type
204/// @param theArgTypes function's ordered argument types
205/// @param theArgNames function's ordered arguments needed if use of this
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000206/// function corresponds to a function definition. Use empty
Garrison Vennf4d2f842010-02-09 23:22:43 +0000207/// aggregate for function declarations.
208/// @param functName function name
209/// @param linkage function linkage
210/// @param declarationOnly for function declarations
211/// @param isVarArg function uses vararg arguments
212/// @returns function instance
Garrison Venn88bd9d62011-04-10 14:06:52 +0000213llvm::Function *createFunction(llvm::Module &module,
Chris Lattner6b8eb8c2011-07-18 04:52:09 +0000214 llvm::Type *retType,
Garrison Venn88bd9d62011-04-10 14:06:52 +0000215 const ArgTypes &theArgTypes,
216 const ArgNames &theArgNames,
217 const std::string &functName,
Garrison Vennf4d2f842010-02-09 23:22:43 +0000218 llvm::GlobalValue::LinkageTypes linkage,
219 bool declarationOnly,
220 bool isVarArg) {
Chris Lattnera868bbb2011-04-08 18:02:51 +0000221 llvm::FunctionType *functType =
222 llvm::FunctionType::get(retType, theArgTypes, isVarArg);
223 llvm::Function *ret =
224 llvm::Function::Create(functType, linkage, functName, &module);
225 if (!ret || declarationOnly)
Garrison Vennf4d2f842010-02-09 23:22:43 +0000226 return(ret);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000227
Chris Lattnera868bbb2011-04-08 18:02:51 +0000228 namedValues.clear();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000229 unsigned i = 0;
Chris Lattnera868bbb2011-04-08 18:02:51 +0000230 for (llvm::Function::arg_iterator argIndex = ret->arg_begin();
231 i != theArgNames.size();
232 ++argIndex, ++i) {
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000233
Chris Lattnera868bbb2011-04-08 18:02:51 +0000234 argIndex->setName(theArgNames[i]);
235 namedValues[theArgNames[i]] = argIndex;
236 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000237
Chris Lattnera868bbb2011-04-08 18:02:51 +0000238 return(ret);
Garrison Vennf4d2f842010-02-09 23:22:43 +0000239}
240
241
242/// Create an alloca instruction in the entry block of
243/// the parent function. This is used for mutable variables etc.
244/// @param function parent instance
245/// @param varName stack variable name
246/// @param type stack variable type
247/// @param initWith optional constant initialization value
248/// @returns AllocaInst instance
Garrison Venn88bd9d62011-04-10 14:06:52 +0000249static llvm::AllocaInst *createEntryBlockAlloca(llvm::Function &function,
Chris Lattnera868bbb2011-04-08 18:02:51 +0000250 const std::string &varName,
Chris Lattner6b8eb8c2011-07-18 04:52:09 +0000251 llvm::Type *type,
Garrison Venn88bd9d62011-04-10 14:06:52 +0000252 llvm::Constant *initWith = 0) {
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000253 llvm::BasicBlock &block = function.getEntryBlock();
Chris Lattnera868bbb2011-04-08 18:02:51 +0000254 llvm::IRBuilder<> tmp(&block, block.begin());
Garrison Venn88bd9d62011-04-10 14:06:52 +0000255 llvm::AllocaInst *ret = tmp.CreateAlloca(type, 0, varName.c_str());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000256
257 if (initWith)
Chris Lattnera868bbb2011-04-08 18:02:51 +0000258 tmp.CreateStore(initWith, ret);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000259
Chris Lattnera868bbb2011-04-08 18:02:51 +0000260 return(ret);
Garrison Vennf4d2f842010-02-09 23:22:43 +0000261}
262
263
264//
265// Code Generation Utilities End
266//
267
268//
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000269// Runtime C Library functions
Garrison Vennf4d2f842010-02-09 23:22:43 +0000270//
271
272// Note: using an extern "C" block so that static functions can be used
273extern "C" {
274
275// Note: Better ways to decide on bit width
276//
277/// Prints a 32 bit number, according to the format, to stderr.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000278/// @param intToPrint integer to print
Garrison Vennf4d2f842010-02-09 23:22:43 +0000279/// @param format printf like format to use when printing
Garrison Venn88bd9d62011-04-10 14:06:52 +0000280void print32Int(int intToPrint, const char *format) {
Chris Lattnera868bbb2011-04-08 18:02:51 +0000281 if (format) {
282 // Note: No NULL check
283 fprintf(stderr, format, intToPrint);
284 }
285 else {
286 // Note: No NULL check
287 fprintf(stderr, "::print32Int(...):NULL arg.\n");
288 }
Garrison Vennf4d2f842010-02-09 23:22:43 +0000289}
290
291
292// Note: Better ways to decide on bit width
293//
294/// Prints a 64 bit number, according to the format, to stderr.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000295/// @param intToPrint integer to print
Garrison Vennf4d2f842010-02-09 23:22:43 +0000296/// @param format printf like format to use when printing
Garrison Venn88bd9d62011-04-10 14:06:52 +0000297void print64Int(long int intToPrint, const char *format) {
Chris Lattnera868bbb2011-04-08 18:02:51 +0000298 if (format) {
299 // Note: No NULL check
300 fprintf(stderr, format, intToPrint);
301 }
302 else {
303 // Note: No NULL check
304 fprintf(stderr, "::print64Int(...):NULL arg.\n");
305 }
Garrison Vennf4d2f842010-02-09 23:22:43 +0000306}
307
308
309/// Prints a C string to stderr
310/// @param toPrint string to print
Garrison Venn88bd9d62011-04-10 14:06:52 +0000311void printStr(char *toPrint) {
Chris Lattnera868bbb2011-04-08 18:02:51 +0000312 if (toPrint) {
313 fprintf(stderr, "%s", toPrint);
314 }
315 else {
316 fprintf(stderr, "::printStr(...):NULL arg.\n");
317 }
Garrison Vennf4d2f842010-02-09 23:22:43 +0000318}
319
320
321/// Deletes the true previosly allocated exception whose address
322/// is calculated from the supplied OurBaseException_t::unwindException
323/// member address. Handles (ignores), NULL pointers.
324/// @param expToDelete exception to delete
Garrison Venn88bd9d62011-04-10 14:06:52 +0000325void deleteOurException(OurUnwindException *expToDelete) {
Garrison Vennf4d2f842010-02-09 23:22:43 +0000326#ifdef DEBUG
Chris Lattnera868bbb2011-04-08 18:02:51 +0000327 fprintf(stderr,
328 "deleteOurException(...).\n");
Garrison Vennf4d2f842010-02-09 23:22:43 +0000329#endif
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000330
Chris Lattnera868bbb2011-04-08 18:02:51 +0000331 if (expToDelete &&
332 (expToDelete->exception_class == ourBaseExceptionClass)) {
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000333
Chris Lattnera868bbb2011-04-08 18:02:51 +0000334 free(((char*) expToDelete) + ourBaseFromUnwindOffset);
335 }
Garrison Vennf4d2f842010-02-09 23:22:43 +0000336}
337
338
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000339/// This function is the struct _Unwind_Exception API mandated delete function
340/// used by foreign exception handlers when deleting our exception
Garrison Vennf4d2f842010-02-09 23:22:43 +0000341/// (OurException), instances.
NAKAMURA Takumi1373b742013-07-29 11:03:50 +0000342/// @param reason See @link http://mentorembedded.github.com/cxx-abi/abi-eh.html
Garrison Vennf4d2f842010-02-09 23:22:43 +0000343/// @unlink
344/// @param expToDelete exception instance to delete
345void deleteFromUnwindOurException(_Unwind_Reason_Code reason,
Garrison Venn88bd9d62011-04-10 14:06:52 +0000346 OurUnwindException *expToDelete) {
Garrison Vennf4d2f842010-02-09 23:22:43 +0000347#ifdef DEBUG
Chris Lattnera868bbb2011-04-08 18:02:51 +0000348 fprintf(stderr,
349 "deleteFromUnwindOurException(...).\n");
Garrison Vennf4d2f842010-02-09 23:22:43 +0000350#endif
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000351
Chris Lattnera868bbb2011-04-08 18:02:51 +0000352 deleteOurException(expToDelete);
Garrison Vennf4d2f842010-02-09 23:22:43 +0000353}
354
355
356/// Creates (allocates on the heap), an exception (OurException instance),
357/// of the supplied type info type.
358/// @param type type info type
Garrison Venn88bd9d62011-04-10 14:06:52 +0000359OurUnwindException *createOurException(int type) {
Chris Lattnera868bbb2011-04-08 18:02:51 +0000360 size_t size = sizeof(OurException);
Garrison Venn88bd9d62011-04-10 14:06:52 +0000361 OurException *ret = (OurException*) memset(malloc(size), 0, size);
Chris Lattnera868bbb2011-04-08 18:02:51 +0000362 (ret->type).type = type;
363 (ret->unwindException).exception_class = ourBaseExceptionClass;
364 (ret->unwindException).exception_cleanup = deleteFromUnwindOurException;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000365
Chris Lattnera868bbb2011-04-08 18:02:51 +0000366 return(&(ret->unwindException));
Garrison Vennf4d2f842010-02-09 23:22:43 +0000367}
368
369
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000370/// Read a uleb128 encoded value and advance pointer
371/// See Variable Length Data in:
Garrison Vennf4d2f842010-02-09 23:22:43 +0000372/// @link http://dwarfstd.org/Dwarf3.pdf @unlink
373/// @param data reference variable holding memory pointer to decode from
374/// @returns decoded value
Garrison Venn88bd9d62011-04-10 14:06:52 +0000375static uintptr_t readULEB128(const uint8_t **data) {
Chris Lattnera868bbb2011-04-08 18:02:51 +0000376 uintptr_t result = 0;
377 uintptr_t shift = 0;
378 unsigned char byte;
Garrison Venn88bd9d62011-04-10 14:06:52 +0000379 const uint8_t *p = *data;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000380
Chris Lattnera868bbb2011-04-08 18:02:51 +0000381 do {
382 byte = *p++;
383 result |= (byte & 0x7f) << shift;
384 shift += 7;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000385 }
Chris Lattnera868bbb2011-04-08 18:02:51 +0000386 while (byte & 0x80);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000387
Chris Lattnera868bbb2011-04-08 18:02:51 +0000388 *data = p;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000389
Chris Lattnera868bbb2011-04-08 18:02:51 +0000390 return result;
Garrison Vennf4d2f842010-02-09 23:22:43 +0000391}
392
393
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000394/// Read a sleb128 encoded value and advance pointer
395/// See Variable Length Data in:
Garrison Vennf4d2f842010-02-09 23:22:43 +0000396/// @link http://dwarfstd.org/Dwarf3.pdf @unlink
397/// @param data reference variable holding memory pointer to decode from
398/// @returns decoded value
Garrison Venn88bd9d62011-04-10 14:06:52 +0000399static uintptr_t readSLEB128(const uint8_t **data) {
Chris Lattnera868bbb2011-04-08 18:02:51 +0000400 uintptr_t result = 0;
401 uintptr_t shift = 0;
402 unsigned char byte;
Garrison Venn88bd9d62011-04-10 14:06:52 +0000403 const uint8_t *p = *data;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000404
Chris Lattnera868bbb2011-04-08 18:02:51 +0000405 do {
406 byte = *p++;
407 result |= (byte & 0x7f) << shift;
408 shift += 7;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000409 }
Chris Lattnera868bbb2011-04-08 18:02:51 +0000410 while (byte & 0x80);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000411
Chris Lattnera868bbb2011-04-08 18:02:51 +0000412 *data = p;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000413
Chris Lattnera868bbb2011-04-08 18:02:51 +0000414 if ((byte & 0x40) && (shift < (sizeof(result) << 3))) {
415 result |= (~0 << shift);
416 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000417
Chris Lattnera868bbb2011-04-08 18:02:51 +0000418 return result;
Garrison Vennf4d2f842010-02-09 23:22:43 +0000419}
420
Rafael Espindola5096adf2013-05-01 21:05:05 +0000421unsigned getEncodingSize(uint8_t Encoding) {
422 if (Encoding == llvm::dwarf::DW_EH_PE_omit)
423 return 0;
424
425 switch (Encoding & 0x0F) {
426 case llvm::dwarf::DW_EH_PE_absptr:
427 return sizeof(uintptr_t);
428 case llvm::dwarf::DW_EH_PE_udata2:
429 return sizeof(uint16_t);
430 case llvm::dwarf::DW_EH_PE_udata4:
431 return sizeof(uint32_t);
432 case llvm::dwarf::DW_EH_PE_udata8:
433 return sizeof(uint64_t);
434 case llvm::dwarf::DW_EH_PE_sdata2:
435 return sizeof(int16_t);
436 case llvm::dwarf::DW_EH_PE_sdata4:
437 return sizeof(int32_t);
438 case llvm::dwarf::DW_EH_PE_sdata8:
439 return sizeof(int64_t);
440 default:
441 // not supported
442 abort();
443 }
444}
Garrison Vennf4d2f842010-02-09 23:22:43 +0000445
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000446/// Read a pointer encoded value and advance pointer
447/// See Variable Length Data in:
Garrison Vennf4d2f842010-02-09 23:22:43 +0000448/// @link http://dwarfstd.org/Dwarf3.pdf @unlink
449/// @param data reference variable holding memory pointer to decode from
450/// @param encoding dwarf encoding type
451/// @returns decoded value
Garrison Venn88bd9d62011-04-10 14:06:52 +0000452static uintptr_t readEncodedPointer(const uint8_t **data, uint8_t encoding) {
Chris Lattnera868bbb2011-04-08 18:02:51 +0000453 uintptr_t result = 0;
Garrison Venn88bd9d62011-04-10 14:06:52 +0000454 const uint8_t *p = *data;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000455
456 if (encoding == llvm::dwarf::DW_EH_PE_omit)
Chris Lattnera868bbb2011-04-08 18:02:51 +0000457 return(result);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000458
459 // first get value
Chris Lattnera868bbb2011-04-08 18:02:51 +0000460 switch (encoding & 0x0F) {
461 case llvm::dwarf::DW_EH_PE_absptr:
462 result = *((uintptr_t*)p);
463 p += sizeof(uintptr_t);
464 break;
465 case llvm::dwarf::DW_EH_PE_uleb128:
466 result = readULEB128(&p);
467 break;
468 // Note: This case has not been tested
469 case llvm::dwarf::DW_EH_PE_sleb128:
470 result = readSLEB128(&p);
471 break;
472 case llvm::dwarf::DW_EH_PE_udata2:
473 result = *((uint16_t*)p);
474 p += sizeof(uint16_t);
475 break;
476 case llvm::dwarf::DW_EH_PE_udata4:
477 result = *((uint32_t*)p);
478 p += sizeof(uint32_t);
479 break;
480 case llvm::dwarf::DW_EH_PE_udata8:
481 result = *((uint64_t*)p);
482 p += sizeof(uint64_t);
483 break;
484 case llvm::dwarf::DW_EH_PE_sdata2:
485 result = *((int16_t*)p);
486 p += sizeof(int16_t);
487 break;
488 case llvm::dwarf::DW_EH_PE_sdata4:
489 result = *((int32_t*)p);
490 p += sizeof(int32_t);
491 break;
492 case llvm::dwarf::DW_EH_PE_sdata8:
493 result = *((int64_t*)p);
494 p += sizeof(int64_t);
495 break;
496 default:
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000497 // not supported
Chris Lattnera868bbb2011-04-08 18:02:51 +0000498 abort();
499 break;
500 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000501
502 // then add relative offset
Chris Lattnera868bbb2011-04-08 18:02:51 +0000503 switch (encoding & 0x70) {
504 case llvm::dwarf::DW_EH_PE_absptr:
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000505 // do nothing
Chris Lattnera868bbb2011-04-08 18:02:51 +0000506 break;
507 case llvm::dwarf::DW_EH_PE_pcrel:
508 result += (uintptr_t)(*data);
509 break;
510 case llvm::dwarf::DW_EH_PE_textrel:
511 case llvm::dwarf::DW_EH_PE_datarel:
512 case llvm::dwarf::DW_EH_PE_funcrel:
513 case llvm::dwarf::DW_EH_PE_aligned:
514 default:
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000515 // not supported
Chris Lattnera868bbb2011-04-08 18:02:51 +0000516 abort();
517 break;
518 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000519
520 // then apply indirection
Chris Lattnera868bbb2011-04-08 18:02:51 +0000521 if (encoding & llvm::dwarf::DW_EH_PE_indirect) {
522 result = *((uintptr_t*)result);
523 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000524
Chris Lattnera868bbb2011-04-08 18:02:51 +0000525 *data = p;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000526
Chris Lattnera868bbb2011-04-08 18:02:51 +0000527 return result;
Garrison Vennf4d2f842010-02-09 23:22:43 +0000528}
529
530
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000531/// Deals with Dwarf actions matching our type infos
532/// (OurExceptionType_t instances). Returns whether or not a dwarf emitted
533/// action matches the supplied exception type. If such a match succeeds,
534/// the resultAction argument will be set with > 0 index value. Only
535/// corresponding llvm.eh.selector type info arguments, cleanup arguments
Garrison Vennf4d2f842010-02-09 23:22:43 +0000536/// are supported. Filters are not supported.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000537/// See Variable Length Data in:
Garrison Vennf4d2f842010-02-09 23:22:43 +0000538/// @link http://dwarfstd.org/Dwarf3.pdf @unlink
Dmitri Gribenko462462e2013-01-13 15:53:09 +0000539/// Also see @link http://mentorembedded.github.com/cxx-abi/abi-eh.html @unlink
Garrison Vennf4d2f842010-02-09 23:22:43 +0000540/// @param resultAction reference variable which will be set with result
541/// @param classInfo our array of type info pointers (to globals)
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000542/// @param actionEntry index into above type info array or 0 (clean up).
Garrison Vennf4d2f842010-02-09 23:22:43 +0000543/// We do not support filters.
544/// @param exceptionClass exception class (_Unwind_Exception::exception_class)
545/// of thrown exception.
546/// @param exceptionObject thrown _Unwind_Exception instance.
547/// @returns whether or not a type info was found. False is returned if only
548/// a cleanup was found
549static bool handleActionValue(int64_t *resultAction,
Rafael Espindola5096adf2013-05-01 21:05:05 +0000550 uint8_t TTypeEncoding,
551 const uint8_t *ClassInfo,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000552 uintptr_t actionEntry,
553 uint64_t exceptionClass,
Garrison Vennf4d2f842010-02-09 23:22:43 +0000554 struct _Unwind_Exception *exceptionObject) {
Chris Lattnera868bbb2011-04-08 18:02:51 +0000555 bool ret = false;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000556
557 if (!resultAction ||
558 !exceptionObject ||
Chris Lattnera868bbb2011-04-08 18:02:51 +0000559 (exceptionClass != ourBaseExceptionClass))
560 return(ret);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000561
Garrison Venn88bd9d62011-04-10 14:06:52 +0000562 struct OurBaseException_t *excp = (struct OurBaseException_t*)
Chris Lattnera868bbb2011-04-08 18:02:51 +0000563 (((char*) exceptionObject) + ourBaseFromUnwindOffset);
564 struct OurExceptionType_t *excpType = &(excp->type);
565 int type = excpType->type;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000566
Chris Lattnera868bbb2011-04-08 18:02:51 +0000567#ifdef DEBUG
568 fprintf(stderr,
569 "handleActionValue(...): exceptionObject = <%p>, "
570 "excp = <%p>.\n",
571 exceptionObject,
572 excp);
573#endif
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000574
Chris Lattnera868bbb2011-04-08 18:02:51 +0000575 const uint8_t *actionPos = (uint8_t*) actionEntry,
576 *tempActionPos;
577 int64_t typeOffset = 0,
578 actionOffset;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000579
Chris Lattnera868bbb2011-04-08 18:02:51 +0000580 for (int i = 0; true; ++i) {
581 // Each emitted dwarf action corresponds to a 2 tuple of
582 // type info address offset, and action offset to the next
583 // emitted action.
584 typeOffset = readSLEB128(&actionPos);
585 tempActionPos = actionPos;
586 actionOffset = readSLEB128(&tempActionPos);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000587
Garrison Vennf4d2f842010-02-09 23:22:43 +0000588#ifdef DEBUG
589 fprintf(stderr,
Chris Lattnera868bbb2011-04-08 18:02:51 +0000590 "handleActionValue(...):typeOffset: <%lld>, "
591 "actionOffset: <%lld>.\n",
592 typeOffset,
593 actionOffset);
Garrison Vennf4d2f842010-02-09 23:22:43 +0000594#endif
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000595 assert((typeOffset >= 0) &&
Chris Lattnera868bbb2011-04-08 18:02:51 +0000596 "handleActionValue(...):filters are not supported.");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000597
Chris Lattnera868bbb2011-04-08 18:02:51 +0000598 // Note: A typeOffset == 0 implies that a cleanup llvm.eh.selector
599 // argument has been matched.
Rafael Espindola5096adf2013-05-01 21:05:05 +0000600 if (typeOffset > 0) {
Garrison Vennf4d2f842010-02-09 23:22:43 +0000601#ifdef DEBUG
Chris Lattnera868bbb2011-04-08 18:02:51 +0000602 fprintf(stderr,
603 "handleActionValue(...):actionValue <%d> found.\n",
604 i);
Garrison Vennf4d2f842010-02-09 23:22:43 +0000605#endif
Rafael Espindola5096adf2013-05-01 21:05:05 +0000606 unsigned EncSize = getEncodingSize(TTypeEncoding);
607 const uint8_t *EntryP = ClassInfo - typeOffset * EncSize;
608 uintptr_t P = readEncodedPointer(&EntryP, TTypeEncoding);
609 struct OurExceptionType_t *ThisClassInfo =
610 reinterpret_cast<struct OurExceptionType_t *>(P);
611 if (ThisClassInfo->type == type) {
612 *resultAction = i + 1;
613 ret = true;
614 break;
615 }
Garrison Vennf4d2f842010-02-09 23:22:43 +0000616 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000617
Chris Lattnera868bbb2011-04-08 18:02:51 +0000618#ifdef DEBUG
619 fprintf(stderr,
620 "handleActionValue(...):actionValue not found.\n");
621#endif
622 if (!actionOffset)
623 break;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000624
Chris Lattnera868bbb2011-04-08 18:02:51 +0000625 actionPos += actionOffset;
626 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000627
Chris Lattnera868bbb2011-04-08 18:02:51 +0000628 return(ret);
Garrison Vennf4d2f842010-02-09 23:22:43 +0000629}
630
631
632/// Deals with the Language specific data portion of the emitted dwarf code.
Dmitri Gribenko462462e2013-01-13 15:53:09 +0000633/// See @link http://mentorembedded.github.com/cxx-abi/abi-eh.html @unlink
Garrison Vennf4d2f842010-02-09 23:22:43 +0000634/// @param version unsupported (ignored), unwind version
635/// @param lsda language specific data area
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000636/// @param _Unwind_Action actions minimally supported unwind stage
Garrison Vennf4d2f842010-02-09 23:22:43 +0000637/// (forced specifically not supported)
638/// @param exceptionClass exception class (_Unwind_Exception::exception_class)
639/// of thrown exception.
640/// @param exceptionObject thrown _Unwind_Exception instance.
641/// @param context unwind system context
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000642/// @returns minimally supported unwinding control indicator
643static _Unwind_Reason_Code handleLsda(int version,
Garrison Venn88bd9d62011-04-10 14:06:52 +0000644 const uint8_t *lsda,
Chris Lattnera868bbb2011-04-08 18:02:51 +0000645 _Unwind_Action actions,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000646 uint64_t exceptionClass,
Garrison Venn88bd9d62011-04-10 14:06:52 +0000647 struct _Unwind_Exception *exceptionObject,
Chris Lattnera868bbb2011-04-08 18:02:51 +0000648 _Unwind_Context_t context) {
649 _Unwind_Reason_Code ret = _URC_CONTINUE_UNWIND;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000650
Chris Lattnera868bbb2011-04-08 18:02:51 +0000651 if (!lsda)
652 return(ret);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000653
Garrison Vennf4d2f842010-02-09 23:22:43 +0000654#ifdef DEBUG
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000655 fprintf(stderr,
Chris Lattnera868bbb2011-04-08 18:02:51 +0000656 "handleLsda(...):lsda is non-zero.\n");
Garrison Vennf4d2f842010-02-09 23:22:43 +0000657#endif
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000658
Chris Lattnera868bbb2011-04-08 18:02:51 +0000659 // Get the current instruction pointer and offset it before next
660 // instruction in the current frame which threw the exception.
661 uintptr_t pc = _Unwind_GetIP(context)-1;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000662
663 // Get beginning current frame's code (as defined by the
Chris Lattnera868bbb2011-04-08 18:02:51 +0000664 // emitted dwarf code)
665 uintptr_t funcStart = _Unwind_GetRegionStart(context);
666 uintptr_t pcOffset = pc - funcStart;
Rafael Espindola5096adf2013-05-01 21:05:05 +0000667 const uint8_t *ClassInfo = NULL;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000668
Chris Lattnera868bbb2011-04-08 18:02:51 +0000669 // Note: See JITDwarfEmitter::EmitExceptionTable(...) for corresponding
670 // dwarf emission
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000671
Chris Lattnera868bbb2011-04-08 18:02:51 +0000672 // Parse LSDA header.
673 uint8_t lpStartEncoding = *lsda++;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000674
Chris Lattnera868bbb2011-04-08 18:02:51 +0000675 if (lpStartEncoding != llvm::dwarf::DW_EH_PE_omit) {
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000676 readEncodedPointer(&lsda, lpStartEncoding);
Chris Lattnera868bbb2011-04-08 18:02:51 +0000677 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000678
Chris Lattnera868bbb2011-04-08 18:02:51 +0000679 uint8_t ttypeEncoding = *lsda++;
680 uintptr_t classInfoOffset;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000681
Chris Lattnera868bbb2011-04-08 18:02:51 +0000682 if (ttypeEncoding != llvm::dwarf::DW_EH_PE_omit) {
683 // Calculate type info locations in emitted dwarf code which
684 // were flagged by type info arguments to llvm.eh.selector
685 // intrinsic
686 classInfoOffset = readULEB128(&lsda);
Rafael Espindola5096adf2013-05-01 21:05:05 +0000687 ClassInfo = lsda + classInfoOffset;
Chris Lattnera868bbb2011-04-08 18:02:51 +0000688 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000689
690 // Walk call-site table looking for range that
691 // includes current PC.
692
Chris Lattnera868bbb2011-04-08 18:02:51 +0000693 uint8_t callSiteEncoding = *lsda++;
694 uint32_t callSiteTableLength = readULEB128(&lsda);
Garrison Venn88bd9d62011-04-10 14:06:52 +0000695 const uint8_t *callSiteTableStart = lsda;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000696 const uint8_t *callSiteTableEnd = callSiteTableStart +
Chris Lattnera868bbb2011-04-08 18:02:51 +0000697 callSiteTableLength;
Garrison Venn88bd9d62011-04-10 14:06:52 +0000698 const uint8_t *actionTableStart = callSiteTableEnd;
699 const uint8_t *callSitePtr = callSiteTableStart;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000700
Chris Lattnera868bbb2011-04-08 18:02:51 +0000701 while (callSitePtr < callSiteTableEnd) {
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000702 uintptr_t start = readEncodedPointer(&callSitePtr,
Chris Lattnera868bbb2011-04-08 18:02:51 +0000703 callSiteEncoding);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000704 uintptr_t length = readEncodedPointer(&callSitePtr,
Chris Lattnera868bbb2011-04-08 18:02:51 +0000705 callSiteEncoding);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000706 uintptr_t landingPad = readEncodedPointer(&callSitePtr,
Garrison Vennf4d2f842010-02-09 23:22:43 +0000707 callSiteEncoding);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000708
Chris Lattnera868bbb2011-04-08 18:02:51 +0000709 // Note: Action value
710 uintptr_t actionEntry = readULEB128(&callSitePtr);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000711
Chris Lattnera868bbb2011-04-08 18:02:51 +0000712 if (exceptionClass != ourBaseExceptionClass) {
713 // We have been notified of a foreign exception being thrown,
714 // and we therefore need to execute cleanup landing pads
715 actionEntry = 0;
Chris Lattnera868bbb2011-04-08 18:02:51 +0000716 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000717
Chris Lattnera868bbb2011-04-08 18:02:51 +0000718 if (landingPad == 0) {
Garrison Vennf4d2f842010-02-09 23:22:43 +0000719#ifdef DEBUG
Chris Lattnera868bbb2011-04-08 18:02:51 +0000720 fprintf(stderr,
721 "handleLsda(...): No landing pad found.\n");
Garrison Vennf4d2f842010-02-09 23:22:43 +0000722#endif
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000723
Chris Lattnera868bbb2011-04-08 18:02:51 +0000724 continue; // no landing pad for this entry
725 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000726
Chris Lattnera868bbb2011-04-08 18:02:51 +0000727 if (actionEntry) {
728 actionEntry += ((uintptr_t) actionTableStart) - 1;
729 }
730 else {
731#ifdef DEBUG
732 fprintf(stderr,
733 "handleLsda(...):No action table found.\n");
734#endif
735 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000736
Chris Lattnera868bbb2011-04-08 18:02:51 +0000737 bool exceptionMatched = false;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000738
Chris Lattnera868bbb2011-04-08 18:02:51 +0000739 if ((start <= pcOffset) && (pcOffset < (start + length))) {
740#ifdef DEBUG
741 fprintf(stderr,
742 "handleLsda(...): Landing pad found.\n");
743#endif
744 int64_t actionValue = 0;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000745
Chris Lattnera868bbb2011-04-08 18:02:51 +0000746 if (actionEntry) {
Garrison Venn88bd9d62011-04-10 14:06:52 +0000747 exceptionMatched = handleActionValue(&actionValue,
Rafael Espindola5096adf2013-05-01 21:05:05 +0000748 ttypeEncoding,
749 ClassInfo,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000750 actionEntry,
751 exceptionClass,
Garrison Venn88bd9d62011-04-10 14:06:52 +0000752 exceptionObject);
Chris Lattnera868bbb2011-04-08 18:02:51 +0000753 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000754
Chris Lattnera868bbb2011-04-08 18:02:51 +0000755 if (!(actions & _UA_SEARCH_PHASE)) {
756#ifdef DEBUG
757 fprintf(stderr,
758 "handleLsda(...): installed landing pad "
759 "context.\n");
760#endif
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000761
Chris Lattnera868bbb2011-04-08 18:02:51 +0000762 // Found landing pad for the PC.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000763 // Set Instruction Pointer to so we re-enter function
764 // at landing pad. The landing pad is created by the
Chris Lattnera868bbb2011-04-08 18:02:51 +0000765 // compiler to take two parameters in registers.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000766 _Unwind_SetGR(context,
767 __builtin_eh_return_data_regno(0),
Chris Lattnera868bbb2011-04-08 18:02:51 +0000768 (uintptr_t)exceptionObject);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000769
Chris Lattnera868bbb2011-04-08 18:02:51 +0000770 // Note: this virtual register directly corresponds
771 // to the return of the llvm.eh.selector intrinsic
772 if (!actionEntry || !exceptionMatched) {
773 // We indicate cleanup only
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000774 _Unwind_SetGR(context,
775 __builtin_eh_return_data_regno(1),
Chris Lattnera868bbb2011-04-08 18:02:51 +0000776 0);
Garrison Vennf4d2f842010-02-09 23:22:43 +0000777 }
778 else {
Chris Lattnera868bbb2011-04-08 18:02:51 +0000779 // Matched type info index of llvm.eh.selector intrinsic
780 // passed here.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000781 _Unwind_SetGR(context,
782 __builtin_eh_return_data_regno(1),
Chris Lattnera868bbb2011-04-08 18:02:51 +0000783 actionValue);
Garrison Vennf4d2f842010-02-09 23:22:43 +0000784 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000785
Chris Lattnera868bbb2011-04-08 18:02:51 +0000786 // To execute landing pad set here
787 _Unwind_SetIP(context, funcStart + landingPad);
788 ret = _URC_INSTALL_CONTEXT;
789 }
790 else if (exceptionMatched) {
Garrison Vennf4d2f842010-02-09 23:22:43 +0000791#ifdef DEBUG
Chris Lattnera868bbb2011-04-08 18:02:51 +0000792 fprintf(stderr,
793 "handleLsda(...): setting handler found.\n");
Garrison Vennf4d2f842010-02-09 23:22:43 +0000794#endif
Chris Lattnera868bbb2011-04-08 18:02:51 +0000795 ret = _URC_HANDLER_FOUND;
796 }
797 else {
798 // Note: Only non-clean up handlers are marked as
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000799 // found. Otherwise the clean up handlers will be
800 // re-found and executed during the clean up
Chris Lattnera868bbb2011-04-08 18:02:51 +0000801 // phase.
Garrison Vennf4d2f842010-02-09 23:22:43 +0000802#ifdef DEBUG
Chris Lattnera868bbb2011-04-08 18:02:51 +0000803 fprintf(stderr,
804 "handleLsda(...): cleanup handler found.\n");
Garrison Vennf4d2f842010-02-09 23:22:43 +0000805#endif
Chris Lattnera868bbb2011-04-08 18:02:51 +0000806 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000807
Chris Lattnera868bbb2011-04-08 18:02:51 +0000808 break;
Garrison Vennf4d2f842010-02-09 23:22:43 +0000809 }
Chris Lattnera868bbb2011-04-08 18:02:51 +0000810 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000811
Chris Lattnera868bbb2011-04-08 18:02:51 +0000812 return(ret);
Garrison Vennf4d2f842010-02-09 23:22:43 +0000813}
814
815
816/// This is the personality function which is embedded (dwarf emitted), in the
817/// dwarf unwind info block. Again see: JITDwarfEmitter.cpp.
Dmitri Gribenko462462e2013-01-13 15:53:09 +0000818/// See @link http://mentorembedded.github.com/cxx-abi/abi-eh.html @unlink
Garrison Vennf4d2f842010-02-09 23:22:43 +0000819/// @param version unsupported (ignored), unwind version
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000820/// @param _Unwind_Action actions minimally supported unwind stage
Garrison Vennf4d2f842010-02-09 23:22:43 +0000821/// (forced specifically not supported)
822/// @param exceptionClass exception class (_Unwind_Exception::exception_class)
823/// of thrown exception.
824/// @param exceptionObject thrown _Unwind_Exception instance.
825/// @param context unwind system context
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000826/// @returns minimally supported unwinding control indicator
827_Unwind_Reason_Code ourPersonality(int version,
Chris Lattnera868bbb2011-04-08 18:02:51 +0000828 _Unwind_Action actions,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000829 uint64_t exceptionClass,
Garrison Venn88bd9d62011-04-10 14:06:52 +0000830 struct _Unwind_Exception *exceptionObject,
Chris Lattnera868bbb2011-04-08 18:02:51 +0000831 _Unwind_Context_t context) {
Garrison Vennf4d2f842010-02-09 23:22:43 +0000832#ifdef DEBUG
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000833 fprintf(stderr,
Chris Lattnera868bbb2011-04-08 18:02:51 +0000834 "We are in ourPersonality(...):actions is <%d>.\n",
835 actions);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000836
Chris Lattnera868bbb2011-04-08 18:02:51 +0000837 if (actions & _UA_SEARCH_PHASE) {
838 fprintf(stderr, "ourPersonality(...):In search phase.\n");
839 }
840 else {
841 fprintf(stderr, "ourPersonality(...):In non-search phase.\n");
842 }
Garrison Vennf4d2f842010-02-09 23:22:43 +0000843#endif
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000844
Garrison Venn88bd9d62011-04-10 14:06:52 +0000845 const uint8_t *lsda = _Unwind_GetLanguageSpecificData(context);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000846
Garrison Vennf4d2f842010-02-09 23:22:43 +0000847#ifdef DEBUG
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000848 fprintf(stderr,
Chris Lattnera868bbb2011-04-08 18:02:51 +0000849 "ourPersonality(...):lsda = <%p>.\n",
850 lsda);
Garrison Vennf4d2f842010-02-09 23:22:43 +0000851#endif
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000852
Chris Lattnera868bbb2011-04-08 18:02:51 +0000853 // The real work of the personality function is captured here
854 return(handleLsda(version,
855 lsda,
856 actions,
857 exceptionClass,
858 exceptionObject,
859 context));
Garrison Vennf4d2f842010-02-09 23:22:43 +0000860}
861
862
863/// Generates our _Unwind_Exception class from a given character array.
864/// thereby handling arbitrary lengths (not in standard), and handling
865/// embedded \0s.
Dmitri Gribenko462462e2013-01-13 15:53:09 +0000866/// See @link http://mentorembedded.github.com/cxx-abi/abi-eh.html @unlink
Garrison Vennf4d2f842010-02-09 23:22:43 +0000867/// @param classChars char array to encode. NULL values not checkedf
868/// @param classCharsSize number of chars in classChars. Value is not checked.
869/// @returns class value
870uint64_t genClass(const unsigned char classChars[], size_t classCharsSize)
871{
Chris Lattnera868bbb2011-04-08 18:02:51 +0000872 uint64_t ret = classChars[0];
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000873
Chris Lattnera868bbb2011-04-08 18:02:51 +0000874 for (unsigned i = 1; i < classCharsSize; ++i) {
875 ret <<= 8;
876 ret += classChars[i];
877 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000878
Chris Lattnera868bbb2011-04-08 18:02:51 +0000879 return(ret);
Garrison Vennf4d2f842010-02-09 23:22:43 +0000880}
881
882} // extern "C"
883
884//
885// Runtime C Library functions End
886//
887
888//
889// Code generation functions
890//
891
892/// Generates code to print given constant string
893/// @param context llvm context
894/// @param module code for module instance
895/// @param builder builder instance
896/// @param toPrint string to print
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000897/// @param useGlobal A value of true (default) indicates a GlobalValue is
898/// generated, and is used to hold the constant string. A value of
899/// false indicates that the constant string will be stored on the
Garrison Vennf4d2f842010-02-09 23:22:43 +0000900/// stack.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000901void generateStringPrint(llvm::LLVMContext &context,
Garrison Venn88bd9d62011-04-10 14:06:52 +0000902 llvm::Module &module,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000903 llvm::IRBuilder<> &builder,
Garrison Vennf4d2f842010-02-09 23:22:43 +0000904 std::string toPrint,
905 bool useGlobal = true) {
Chris Lattnera868bbb2011-04-08 18:02:51 +0000906 llvm::Function *printFunct = module.getFunction("printStr");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000907
Chris Lattnera868bbb2011-04-08 18:02:51 +0000908 llvm::Value *stringVar;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000909 llvm::Constant *stringConstant =
Peter Collingbournec4e342b2012-02-06 14:09:13 +0000910 llvm::ConstantDataArray::getString(context, toPrint);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000911
Chris Lattnera868bbb2011-04-08 18:02:51 +0000912 if (useGlobal) {
913 // Note: Does not work without allocation
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000914 stringVar =
915 new llvm::GlobalVariable(module,
Chris Lattnera868bbb2011-04-08 18:02:51 +0000916 stringConstant->getType(),
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000917 true,
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000918 llvm::GlobalValue::PrivateLinkage,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000919 stringConstant,
Chris Lattnera868bbb2011-04-08 18:02:51 +0000920 "");
921 }
922 else {
923 stringVar = builder.CreateAlloca(stringConstant->getType());
924 builder.CreateStore(stringConstant, stringVar);
925 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000926
927 llvm::Value *cast = builder.CreatePointerCast(stringVar,
Garrison Venn0a2eb8d2011-09-23 14:45:10 +0000928 builder.getInt8PtrTy());
Chris Lattnera868bbb2011-04-08 18:02:51 +0000929 builder.CreateCall(printFunct, cast);
Garrison Vennf4d2f842010-02-09 23:22:43 +0000930}
931
932
933/// Generates code to print given runtime integer according to constant
934/// string format, and a given print function.
935/// @param context llvm context
936/// @param module code for module instance
937/// @param builder builder instance
938/// @param printFunct function used to "print" integer
939/// @param toPrint string to print
940/// @param format printf like formating string for print
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000941/// @param useGlobal A value of true (default) indicates a GlobalValue is
942/// generated, and is used to hold the constant string. A value of
943/// false indicates that the constant string will be stored on the
Garrison Vennf4d2f842010-02-09 23:22:43 +0000944/// stack.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000945void generateIntegerPrint(llvm::LLVMContext &context,
Garrison Venn88bd9d62011-04-10 14:06:52 +0000946 llvm::Module &module,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000947 llvm::IRBuilder<> &builder,
Garrison Venn88bd9d62011-04-10 14:06:52 +0000948 llvm::Function &printFunct,
949 llvm::Value &toPrint,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000950 std::string format,
Garrison Vennf4d2f842010-02-09 23:22:43 +0000951 bool useGlobal = true) {
Peter Collingbournec4e342b2012-02-06 14:09:13 +0000952 llvm::Constant *stringConstant =
953 llvm::ConstantDataArray::getString(context, format);
Chris Lattnera868bbb2011-04-08 18:02:51 +0000954 llvm::Value *stringVar;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000955
Chris Lattnera868bbb2011-04-08 18:02:51 +0000956 if (useGlobal) {
957 // Note: Does not seem to work without allocation
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000958 stringVar =
959 new llvm::GlobalVariable(module,
Chris Lattnera868bbb2011-04-08 18:02:51 +0000960 stringConstant->getType(),
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000961 true,
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000962 llvm::GlobalValue::PrivateLinkage,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000963 stringConstant,
Chris Lattnera868bbb2011-04-08 18:02:51 +0000964 "");
965 }
966 else {
967 stringVar = builder.CreateAlloca(stringConstant->getType());
968 builder.CreateStore(stringConstant, stringVar);
969 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000970
971 llvm::Value *cast = builder.CreateBitCast(stringVar,
Garrison Venn0a2eb8d2011-09-23 14:45:10 +0000972 builder.getInt8PtrTy());
Chris Lattnera868bbb2011-04-08 18:02:51 +0000973 builder.CreateCall2(&printFunct, &toPrint, cast);
Garrison Vennf4d2f842010-02-09 23:22:43 +0000974}
975
976
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000977/// Generates code to handle finally block type semantics: always runs
978/// regardless of whether a thrown exception is passing through or the
979/// parent function is simply exiting. In addition to printing some state
980/// to stderr, this code will resume the exception handling--runs the
981/// unwind resume block, if the exception has not been previously caught
982/// by a catch clause, and will otherwise execute the end block (terminator
983/// block). In addition this function creates the corresponding function's
Garrison Vennf4d2f842010-02-09 23:22:43 +0000984/// stack storage for the exception pointer and catch flag status.
985/// @param context llvm context
986/// @param module code for module instance
987/// @param builder builder instance
988/// @param toAddTo parent function to add block to
989/// @param blockName block name of new "finally" block.
990/// @param functionId output id used for printing
991/// @param terminatorBlock terminator "end" block
992/// @param unwindResumeBlock unwind resume block
993/// @param exceptionCaughtFlag reference exception caught/thrown status storage
994/// @param exceptionStorage reference to exception pointer storage
Garrison Venn0a2eb8d2011-09-23 14:45:10 +0000995/// @param caughtResultStorage reference to landingpad result storage
Garrison Vennf4d2f842010-02-09 23:22:43 +0000996/// @returns newly created block
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000997static llvm::BasicBlock *createFinallyBlock(llvm::LLVMContext &context,
998 llvm::Module &module,
999 llvm::IRBuilder<> &builder,
Garrison Venn88bd9d62011-04-10 14:06:52 +00001000 llvm::Function &toAddTo,
1001 std::string &blockName,
1002 std::string &functionId,
1003 llvm::BasicBlock &terminatorBlock,
1004 llvm::BasicBlock &unwindResumeBlock,
1005 llvm::Value **exceptionCaughtFlag,
Bill Wendling383727b2012-02-04 00:29:12 +00001006 llvm::Value **exceptionStorage,
1007 llvm::Value **caughtResultStorage) {
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001008 assert(exceptionCaughtFlag &&
Chris Lattnera868bbb2011-04-08 18:02:51 +00001009 "ExceptionDemo::createFinallyBlock(...):exceptionCaughtFlag "
1010 "is NULL");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001011 assert(exceptionStorage &&
Chris Lattnera868bbb2011-04-08 18:02:51 +00001012 "ExceptionDemo::createFinallyBlock(...):exceptionStorage "
1013 "is NULL");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001014 assert(caughtResultStorage &&
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001015 "ExceptionDemo::createFinallyBlock(...):caughtResultStorage "
1016 "is NULL");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001017
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001018 *exceptionCaughtFlag = createEntryBlockAlloca(toAddTo,
1019 "exceptionCaught",
1020 ourExceptionNotThrownState->getType(),
1021 ourExceptionNotThrownState);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001022
Chris Lattner6b8eb8c2011-07-18 04:52:09 +00001023 llvm::PointerType *exceptionStorageType = builder.getInt8PtrTy();
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001024 *exceptionStorage = createEntryBlockAlloca(toAddTo,
1025 "exceptionStorage",
1026 exceptionStorageType,
1027 llvm::ConstantPointerNull::get(
1028 exceptionStorageType));
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001029 *caughtResultStorage = createEntryBlockAlloca(toAddTo,
1030 "caughtResultStorage",
1031 ourCaughtResultType,
1032 llvm::ConstantAggregateZero::get(
1033 ourCaughtResultType));
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001034
Chris Lattnera868bbb2011-04-08 18:02:51 +00001035 llvm::BasicBlock *ret = llvm::BasicBlock::Create(context,
1036 blockName,
1037 &toAddTo);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001038
Chris Lattnera868bbb2011-04-08 18:02:51 +00001039 builder.SetInsertPoint(ret);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001040
Chris Lattnera868bbb2011-04-08 18:02:51 +00001041 std::ostringstream bufferToPrint;
1042 bufferToPrint << "Gen: Executing finally block "
1043 << blockName << " in " << functionId << "\n";
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001044 generateStringPrint(context,
1045 module,
1046 builder,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001047 bufferToPrint.str(),
1048 USE_GLOBAL_STR_CONSTS);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001049
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001050 llvm::SwitchInst *theSwitch = builder.CreateSwitch(builder.CreateLoad(
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001051 *exceptionCaughtFlag),
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001052 &terminatorBlock,
1053 2);
Chris Lattnera868bbb2011-04-08 18:02:51 +00001054 theSwitch->addCase(ourExceptionCaughtState, &terminatorBlock);
1055 theSwitch->addCase(ourExceptionThrownState, &unwindResumeBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001056
Chris Lattnera868bbb2011-04-08 18:02:51 +00001057 return(ret);
Garrison Vennf4d2f842010-02-09 23:22:43 +00001058}
1059
1060
1061/// Generates catch block semantics which print a string to indicate type of
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001062/// catch executed, sets an exception caught flag, and executes passed in
Garrison Vennf4d2f842010-02-09 23:22:43 +00001063/// end block (terminator block).
1064/// @param context llvm context
1065/// @param module code for module instance
1066/// @param builder builder instance
1067/// @param toAddTo parent function to add block to
1068/// @param blockName block name of new "catch" block.
1069/// @param functionId output id used for printing
1070/// @param terminatorBlock terminator "end" block
1071/// @param exceptionCaughtFlag exception caught/thrown status
1072/// @returns newly created block
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001073static llvm::BasicBlock *createCatchBlock(llvm::LLVMContext &context,
1074 llvm::Module &module,
1075 llvm::IRBuilder<> &builder,
Garrison Venn88bd9d62011-04-10 14:06:52 +00001076 llvm::Function &toAddTo,
1077 std::string &blockName,
1078 std::string &functionId,
1079 llvm::BasicBlock &terminatorBlock,
1080 llvm::Value &exceptionCaughtFlag) {
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001081
Chris Lattnera868bbb2011-04-08 18:02:51 +00001082 llvm::BasicBlock *ret = llvm::BasicBlock::Create(context,
1083 blockName,
1084 &toAddTo);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001085
Chris Lattnera868bbb2011-04-08 18:02:51 +00001086 builder.SetInsertPoint(ret);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001087
Chris Lattnera868bbb2011-04-08 18:02:51 +00001088 std::ostringstream bufferToPrint;
1089 bufferToPrint << "Gen: Executing catch block "
1090 << blockName
1091 << " in "
1092 << functionId
1093 << std::endl;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001094 generateStringPrint(context,
1095 module,
1096 builder,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001097 bufferToPrint.str(),
1098 USE_GLOBAL_STR_CONSTS);
1099 builder.CreateStore(ourExceptionCaughtState, &exceptionCaughtFlag);
1100 builder.CreateBr(&terminatorBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001101
Chris Lattnera868bbb2011-04-08 18:02:51 +00001102 return(ret);
Garrison Vennf4d2f842010-02-09 23:22:43 +00001103}
1104
1105
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001106/// Generates a function which invokes a function (toInvoke) and, whose
1107/// unwind block will "catch" the type info types correspondingly held in the
1108/// exceptionTypesToCatch argument. If the toInvoke function throws an
1109/// exception which does not match any type info types contained in
1110/// exceptionTypesToCatch, the generated code will call _Unwind_Resume
1111/// with the raised exception. On the other hand the generated code will
Garrison Vennf4d2f842010-02-09 23:22:43 +00001112/// normally exit if the toInvoke function does not throw an exception.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001113/// The generated "finally" block is always run regardless of the cause of
Garrison Vennf4d2f842010-02-09 23:22:43 +00001114/// the generated function exit.
1115/// The generated function is returned after being verified.
1116/// @param module code for module instance
1117/// @param builder builder instance
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001118/// @param fpm a function pass manager holding optional IR to IR
Garrison Vennf4d2f842010-02-09 23:22:43 +00001119/// transformations
1120/// @param toInvoke inner function to invoke
1121/// @param ourId id used to printing purposes
1122/// @param numExceptionsToCatch length of exceptionTypesToCatch array
1123/// @param exceptionTypesToCatch array of type info types to "catch"
1124/// @returns generated function
Chandler Carruth7ecd9912015-02-13 10:21:05 +00001125static llvm::Function *createCatchWrappedInvokeFunction(
1126 llvm::Module &module, llvm::IRBuilder<> &builder,
1127 llvm::legacy::FunctionPassManager &fpm, llvm::Function &toInvoke,
1128 std::string ourId, unsigned numExceptionsToCatch,
1129 unsigned exceptionTypesToCatch[]) {
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001130
Garrison Venn88bd9d62011-04-10 14:06:52 +00001131 llvm::LLVMContext &context = module.getContext();
Chris Lattnera868bbb2011-04-08 18:02:51 +00001132 llvm::Function *toPrint32Int = module.getFunction("print32Int");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001133
Chris Lattnera868bbb2011-04-08 18:02:51 +00001134 ArgTypes argTypes;
Garrison Venn76310ac2011-07-12 15:34:42 +00001135 argTypes.push_back(builder.getInt32Ty());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001136
Chris Lattnera868bbb2011-04-08 18:02:51 +00001137 ArgNames argNames;
1138 argNames.push_back("exceptTypeToThrow");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001139
1140 llvm::Function *ret = createFunction(module,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001141 builder.getVoidTy(),
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001142 argTypes,
1143 argNames,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001144 ourId,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001145 llvm::Function::ExternalLinkage,
1146 false,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001147 false);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001148
Chris Lattnera868bbb2011-04-08 18:02:51 +00001149 // Block which calls invoke
1150 llvm::BasicBlock *entryBlock = llvm::BasicBlock::Create(context,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001151 "entry",
Chris Lattnera868bbb2011-04-08 18:02:51 +00001152 ret);
1153 // Normal block for invoke
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001154 llvm::BasicBlock *normalBlock = llvm::BasicBlock::Create(context,
1155 "normal",
Chris Lattnera868bbb2011-04-08 18:02:51 +00001156 ret);
1157 // Unwind block for invoke
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001158 llvm::BasicBlock *exceptionBlock = llvm::BasicBlock::Create(context,
1159 "exception",
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001160 ret);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001161
Chris Lattnera868bbb2011-04-08 18:02:51 +00001162 // Block which routes exception to correct catch handler block
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001163 llvm::BasicBlock *exceptionRouteBlock = llvm::BasicBlock::Create(context,
1164 "exceptionRoute",
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001165 ret);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001166
Chris Lattnera868bbb2011-04-08 18:02:51 +00001167 // Foreign exception handler
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001168 llvm::BasicBlock *externalExceptionBlock = llvm::BasicBlock::Create(context,
1169 "externalException",
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001170 ret);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001171
Chris Lattnera868bbb2011-04-08 18:02:51 +00001172 // Block which calls _Unwind_Resume
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001173 llvm::BasicBlock *unwindResumeBlock = llvm::BasicBlock::Create(context,
1174 "unwindResume",
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001175 ret);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001176
Chris Lattnera868bbb2011-04-08 18:02:51 +00001177 // Clean up block which delete exception if needed
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001178 llvm::BasicBlock *endBlock = llvm::BasicBlock::Create(context, "end", ret);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001179
Chris Lattnera868bbb2011-04-08 18:02:51 +00001180 std::string nextName;
1181 std::vector<llvm::BasicBlock*> catchBlocks(numExceptionsToCatch);
Garrison Venn88bd9d62011-04-10 14:06:52 +00001182 llvm::Value *exceptionCaughtFlag = NULL;
1183 llvm::Value *exceptionStorage = NULL;
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001184 llvm::Value *caughtResultStorage = NULL;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001185
1186 // Finally block which will branch to unwindResumeBlock if
Chris Lattnera868bbb2011-04-08 18:02:51 +00001187 // exception is not caught. Initializes/allocates stack locations.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001188 llvm::BasicBlock *finallyBlock = createFinallyBlock(context,
1189 module,
1190 builder,
1191 *ret,
1192 nextName = "finally",
Chris Lattnera868bbb2011-04-08 18:02:51 +00001193 ourId,
1194 *endBlock,
1195 *unwindResumeBlock,
1196 &exceptionCaughtFlag,
Bill Wendling383727b2012-02-04 00:29:12 +00001197 &exceptionStorage,
1198 &caughtResultStorage
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001199 );
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001200
Chris Lattnera868bbb2011-04-08 18:02:51 +00001201 for (unsigned i = 0; i < numExceptionsToCatch; ++i) {
1202 nextName = ourTypeInfoNames[exceptionTypesToCatch[i]];
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001203
Chris Lattnera868bbb2011-04-08 18:02:51 +00001204 // One catch block per type info to be caught
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001205 catchBlocks[i] = createCatchBlock(context,
1206 module,
1207 builder,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001208 *ret,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001209 nextName,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001210 ourId,
1211 *finallyBlock,
1212 *exceptionCaughtFlag);
1213 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001214
Chris Lattnera868bbb2011-04-08 18:02:51 +00001215 // Entry Block
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001216
Chris Lattnera868bbb2011-04-08 18:02:51 +00001217 builder.SetInsertPoint(entryBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001218
Chris Lattnera868bbb2011-04-08 18:02:51 +00001219 std::vector<llvm::Value*> args;
1220 args.push_back(namedValues["exceptTypeToThrow"]);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001221 builder.CreateInvoke(&toInvoke,
1222 normalBlock,
1223 exceptionBlock,
Chris Lattner6b8eb8c2011-07-18 04:52:09 +00001224 args);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001225
Chris Lattnera868bbb2011-04-08 18:02:51 +00001226 // End Block
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001227
Chris Lattnera868bbb2011-04-08 18:02:51 +00001228 builder.SetInsertPoint(endBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001229
1230 generateStringPrint(context,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001231 module,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001232 builder,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001233 "Gen: In end block: exiting in " + ourId + ".\n",
1234 USE_GLOBAL_STR_CONSTS);
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001235 llvm::Function *deleteOurException = module.getFunction("deleteOurException");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001236
Chris Lattnera868bbb2011-04-08 18:02:51 +00001237 // Note: function handles NULL exceptions
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001238 builder.CreateCall(deleteOurException,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001239 builder.CreateLoad(exceptionStorage));
1240 builder.CreateRetVoid();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001241
Chris Lattnera868bbb2011-04-08 18:02:51 +00001242 // Normal Block
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001243
Chris Lattnera868bbb2011-04-08 18:02:51 +00001244 builder.SetInsertPoint(normalBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001245
1246 generateStringPrint(context,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001247 module,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001248 builder,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001249 "Gen: No exception in " + ourId + "!\n",
1250 USE_GLOBAL_STR_CONSTS);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001251
Chris Lattnera868bbb2011-04-08 18:02:51 +00001252 // Finally block is always called
1253 builder.CreateBr(finallyBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001254
Chris Lattnera868bbb2011-04-08 18:02:51 +00001255 // Unwind Resume Block
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001256
Chris Lattnera868bbb2011-04-08 18:02:51 +00001257 builder.SetInsertPoint(unwindResumeBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001258
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001259 builder.CreateResume(builder.CreateLoad(caughtResultStorage));
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001260
Chris Lattnera868bbb2011-04-08 18:02:51 +00001261 // Exception Block
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001262
Chris Lattnera868bbb2011-04-08 18:02:51 +00001263 builder.SetInsertPoint(exceptionBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001264
Garrison Venn8cb00352011-09-22 15:45:14 +00001265 llvm::Function *personality = module.getFunction("ourPersonality");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001266
1267 llvm::LandingPadInst *caughtResult =
Garrison Venn8cb00352011-09-22 15:45:14 +00001268 builder.CreateLandingPad(ourCaughtResultType,
1269 personality,
1270 numExceptionsToCatch,
1271 "landingPad");
1272
1273 caughtResult->setCleanup(true);
1274
1275 for (unsigned i = 0; i < numExceptionsToCatch; ++i) {
1276 // Set up type infos to be caught
1277 caughtResult->addClause(module.getGlobalVariable(
1278 ourTypeInfoNames[exceptionTypesToCatch[i]]));
1279 }
1280
1281 llvm::Value *unwindException = builder.CreateExtractValue(caughtResult, 0);
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001282 llvm::Value *retTypeInfoIndex = builder.CreateExtractValue(caughtResult, 1);
Garrison Venn8cb00352011-09-22 15:45:14 +00001283
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001284 // FIXME: Redundant storage which, beyond utilizing value of
1285 // caughtResultStore for unwindException storage, may be alleviated
Benjamin Kramerbde91762012-06-02 10:20:22 +00001286 // altogether with a block rearrangement
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001287 builder.CreateStore(caughtResult, caughtResultStorage);
Garrison Venn8cb00352011-09-22 15:45:14 +00001288 builder.CreateStore(unwindException, exceptionStorage);
1289 builder.CreateStore(ourExceptionThrownState, exceptionCaughtFlag);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001290
1291 // Retrieve exception_class member from thrown exception
Chris Lattnera868bbb2011-04-08 18:02:51 +00001292 // (_Unwind_Exception instance). This member tells us whether or not
1293 // the exception is foreign.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001294 llvm::Value *unwindExceptionClass =
Chris Lattnera868bbb2011-04-08 18:02:51 +00001295 builder.CreateLoad(builder.CreateStructGEP(
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001296 builder.CreatePointerCast(unwindException,
Micah Villmow51e72462012-10-24 17:25:11 +00001297 ourUnwindExceptionType->getPointerTo()),
Chris Lattnera868bbb2011-04-08 18:02:51 +00001298 0));
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001299
Chris Lattnera868bbb2011-04-08 18:02:51 +00001300 // Branch to the externalExceptionBlock if the exception is foreign or
1301 // to a catch router if not. Either way the finally block will be run.
1302 builder.CreateCondBr(builder.CreateICmpEQ(unwindExceptionClass,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001303 llvm::ConstantInt::get(builder.getInt64Ty(),
Chris Lattnera868bbb2011-04-08 18:02:51 +00001304 ourBaseExceptionClass)),
1305 exceptionRouteBlock,
1306 externalExceptionBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001307
Chris Lattnera868bbb2011-04-08 18:02:51 +00001308 // External Exception Block
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001309
Chris Lattnera868bbb2011-04-08 18:02:51 +00001310 builder.SetInsertPoint(externalExceptionBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001311
1312 generateStringPrint(context,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001313 module,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001314 builder,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001315 "Gen: Foreign exception received.\n",
1316 USE_GLOBAL_STR_CONSTS);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001317
Chris Lattnera868bbb2011-04-08 18:02:51 +00001318 // Branch to the finally block
1319 builder.CreateBr(finallyBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001320
Chris Lattnera868bbb2011-04-08 18:02:51 +00001321 // Exception Route Block
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001322
Chris Lattnera868bbb2011-04-08 18:02:51 +00001323 builder.SetInsertPoint(exceptionRouteBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001324
1325 // Casts exception pointer (_Unwind_Exception instance) to parent
Chris Lattnera868bbb2011-04-08 18:02:51 +00001326 // (OurException instance).
1327 //
1328 // Note: ourBaseFromUnwindOffset is usually negative
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001329 llvm::Value *typeInfoThrown = builder.CreatePointerCast(
1330 builder.CreateConstGEP1_64(unwindException,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001331 ourBaseFromUnwindOffset),
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001332 ourExceptionType->getPointerTo());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001333
Chris Lattnera868bbb2011-04-08 18:02:51 +00001334 // Retrieve thrown exception type info type
1335 //
1336 // Note: Index is not relative to pointer but instead to structure
1337 // unlike a true getelementptr (GEP) instruction
1338 typeInfoThrown = builder.CreateStructGEP(typeInfoThrown, 0);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001339
1340 llvm::Value *typeInfoThrownType =
Chris Lattnera868bbb2011-04-08 18:02:51 +00001341 builder.CreateStructGEP(typeInfoThrown, 0);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001342
1343 generateIntegerPrint(context,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001344 module,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001345 builder,
1346 *toPrint32Int,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001347 *(builder.CreateLoad(typeInfoThrownType)),
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001348 "Gen: Exception type <%d> received (stack unwound) "
1349 " in " +
1350 ourId +
Chris Lattnera868bbb2011-04-08 18:02:51 +00001351 ".\n",
1352 USE_GLOBAL_STR_CONSTS);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001353
Chris Lattnera868bbb2011-04-08 18:02:51 +00001354 // Route to matched type info catch block or run cleanup finally block
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001355 llvm::SwitchInst *switchToCatchBlock = builder.CreateSwitch(retTypeInfoIndex,
1356 finallyBlock,
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001357 numExceptionsToCatch);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001358
Chris Lattnera868bbb2011-04-08 18:02:51 +00001359 unsigned nextTypeToCatch;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001360
Chris Lattnera868bbb2011-04-08 18:02:51 +00001361 for (unsigned i = 1; i <= numExceptionsToCatch; ++i) {
1362 nextTypeToCatch = i - 1;
1363 switchToCatchBlock->addCase(llvm::ConstantInt::get(
1364 llvm::Type::getInt32Ty(context), i),
1365 catchBlocks[nextTypeToCatch]);
1366 }
Garrison Venneb89d362011-09-22 14:07:50 +00001367
Chris Lattnera868bbb2011-04-08 18:02:51 +00001368 llvm::verifyFunction(*ret);
1369 fpm.run(*ret);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001370
Chris Lattnera868bbb2011-04-08 18:02:51 +00001371 return(ret);
Garrison Vennf4d2f842010-02-09 23:22:43 +00001372}
1373
1374
1375/// Generates function which throws either an exception matched to a runtime
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001376/// determined type info type (argument to generated function), or if this
1377/// runtime value matches nativeThrowType, throws a foreign exception by
Garrison Vennf4d2f842010-02-09 23:22:43 +00001378/// calling nativeThrowFunct.
1379/// @param module code for module instance
1380/// @param builder builder instance
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001381/// @param fpm a function pass manager holding optional IR to IR
Garrison Vennf4d2f842010-02-09 23:22:43 +00001382/// transformations
1383/// @param ourId id used to printing purposes
1384/// @param nativeThrowType a runtime argument of this value results in
1385/// nativeThrowFunct being called to generate/throw exception.
1386/// @param nativeThrowFunct function which will throw a foreign exception
1387/// if the above nativeThrowType matches generated function's arg.
1388/// @returns generated function
Chandler Carruth7ecd9912015-02-13 10:21:05 +00001389static llvm::Function *
1390createThrowExceptionFunction(llvm::Module &module, llvm::IRBuilder<> &builder,
1391 llvm::legacy::FunctionPassManager &fpm,
1392 std::string ourId, int32_t nativeThrowType,
1393 llvm::Function &nativeThrowFunct) {
Garrison Venn88bd9d62011-04-10 14:06:52 +00001394 llvm::LLVMContext &context = module.getContext();
Chris Lattnera868bbb2011-04-08 18:02:51 +00001395 namedValues.clear();
1396 ArgTypes unwindArgTypes;
Garrison Venn76310ac2011-07-12 15:34:42 +00001397 unwindArgTypes.push_back(builder.getInt32Ty());
Chris Lattnera868bbb2011-04-08 18:02:51 +00001398 ArgNames unwindArgNames;
1399 unwindArgNames.push_back("exceptTypeToThrow");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001400
Chris Lattnera868bbb2011-04-08 18:02:51 +00001401 llvm::Function *ret = createFunction(module,
1402 builder.getVoidTy(),
1403 unwindArgTypes,
1404 unwindArgNames,
1405 ourId,
1406 llvm::Function::ExternalLinkage,
1407 false,
1408 false);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001409
Chris Lattnera868bbb2011-04-08 18:02:51 +00001410 // Throws either one of our exception or a native C++ exception depending
1411 // on a runtime argument value containing a type info type.
1412 llvm::BasicBlock *entryBlock = llvm::BasicBlock::Create(context,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001413 "entry",
Chris Lattnera868bbb2011-04-08 18:02:51 +00001414 ret);
1415 // Throws a foreign exception
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001416 llvm::BasicBlock *nativeThrowBlock = llvm::BasicBlock::Create(context,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001417 "nativeThrow",
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001418 ret);
Chris Lattnera868bbb2011-04-08 18:02:51 +00001419 // Throws one of our Exceptions
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001420 llvm::BasicBlock *generatedThrowBlock = llvm::BasicBlock::Create(context,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001421 "generatedThrow",
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001422 ret);
Chris Lattnera868bbb2011-04-08 18:02:51 +00001423 // Retrieved runtime type info type to throw
Garrison Venn88bd9d62011-04-10 14:06:52 +00001424 llvm::Value *exceptionType = namedValues["exceptTypeToThrow"];
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001425
Chris Lattnera868bbb2011-04-08 18:02:51 +00001426 // nativeThrowBlock block
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001427
Chris Lattnera868bbb2011-04-08 18:02:51 +00001428 builder.SetInsertPoint(nativeThrowBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001429
Chris Lattnera868bbb2011-04-08 18:02:51 +00001430 // Throws foreign exception
1431 builder.CreateCall(&nativeThrowFunct, exceptionType);
1432 builder.CreateUnreachable();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001433
Chris Lattnera868bbb2011-04-08 18:02:51 +00001434 // entry block
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001435
Chris Lattnera868bbb2011-04-08 18:02:51 +00001436 builder.SetInsertPoint(entryBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001437
Chris Lattnera868bbb2011-04-08 18:02:51 +00001438 llvm::Function *toPrint32Int = module.getFunction("print32Int");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001439 generateIntegerPrint(context,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001440 module,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001441 builder,
1442 *toPrint32Int,
1443 *exceptionType,
1444 "\nGen: About to throw exception type <%d> in " +
1445 ourId +
Chris Lattnera868bbb2011-04-08 18:02:51 +00001446 ".\n",
1447 USE_GLOBAL_STR_CONSTS);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001448
Chris Lattnera868bbb2011-04-08 18:02:51 +00001449 // Switches on runtime type info type value to determine whether or not
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001450 // a foreign exception is thrown. Defaults to throwing one of our
Chris Lattnera868bbb2011-04-08 18:02:51 +00001451 // generated exceptions.
Garrison Venn88bd9d62011-04-10 14:06:52 +00001452 llvm::SwitchInst *theSwitch = builder.CreateSwitch(exceptionType,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001453 generatedThrowBlock,
1454 1);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001455
1456 theSwitch->addCase(llvm::ConstantInt::get(llvm::Type::getInt32Ty(context),
Chris Lattnera868bbb2011-04-08 18:02:51 +00001457 nativeThrowType),
1458 nativeThrowBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001459
Chris Lattnera868bbb2011-04-08 18:02:51 +00001460 // generatedThrow block
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001461
Chris Lattnera868bbb2011-04-08 18:02:51 +00001462 builder.SetInsertPoint(generatedThrowBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001463
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001464 llvm::Function *createOurException = module.getFunction("createOurException");
1465 llvm::Function *raiseOurException = module.getFunction(
1466 "_Unwind_RaiseException");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001467
Chris Lattnera868bbb2011-04-08 18:02:51 +00001468 // Creates exception to throw with runtime type info type.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001469 llvm::Value *exception = builder.CreateCall(createOurException,
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001470 namedValues["exceptTypeToThrow"]);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001471
Chris Lattnera868bbb2011-04-08 18:02:51 +00001472 // Throw generated Exception
1473 builder.CreateCall(raiseOurException, exception);
1474 builder.CreateUnreachable();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001475
Chris Lattnera868bbb2011-04-08 18:02:51 +00001476 llvm::verifyFunction(*ret);
1477 fpm.run(*ret);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001478
Chris Lattnera868bbb2011-04-08 18:02:51 +00001479 return(ret);
Garrison Vennf4d2f842010-02-09 23:22:43 +00001480}
1481
1482static void createStandardUtilityFunctions(unsigned numTypeInfos,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001483 llvm::Module &module,
Garrison Venn88bd9d62011-04-10 14:06:52 +00001484 llvm::IRBuilder<> &builder);
Garrison Vennf4d2f842010-02-09 23:22:43 +00001485
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001486/// Creates test code by generating and organizing these functions into the
Garrison Vennf4d2f842010-02-09 23:22:43 +00001487/// test case. The test case consists of an outer function setup to invoke
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001488/// an inner function within an environment having multiple catch and single
Garrison Vennf4d2f842010-02-09 23:22:43 +00001489/// finally blocks. This inner function is also setup to invoke a throw
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001490/// function within an evironment similar in nature to the outer function's
Garrison Vennf4d2f842010-02-09 23:22:43 +00001491/// catch and finally blocks. Each of these two functions catch mutually
1492/// exclusive subsets (even or odd) of the type info types configured
1493/// for this this. All generated functions have a runtime argument which
1494/// holds a type info type to throw that each function takes and passes it
1495/// to the inner one if such a inner function exists. This type info type is
1496/// looked at by the generated throw function to see whether or not it should
1497/// throw a generated exception with the same type info type, or instead call
1498/// a supplied a function which in turn will throw a foreign exception.
1499/// @param module code for module instance
1500/// @param builder builder instance
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001501/// @param fpm a function pass manager holding optional IR to IR
Garrison Vennf4d2f842010-02-09 23:22:43 +00001502/// transformations
1503/// @param nativeThrowFunctName name of external function which will throw
1504/// a foreign exception
1505/// @returns outermost generated test function.
Chandler Carruth7ecd9912015-02-13 10:21:05 +00001506llvm::Function *
1507createUnwindExceptionTest(llvm::Module &module, llvm::IRBuilder<> &builder,
1508 llvm::legacy::FunctionPassManager &fpm,
1509 std::string nativeThrowFunctName) {
Chris Lattnera868bbb2011-04-08 18:02:51 +00001510 // Number of type infos to generate
1511 unsigned numTypeInfos = 6;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001512
Chris Lattnera868bbb2011-04-08 18:02:51 +00001513 // Initialze intrisics and external functions to use along with exception
1514 // and type info globals.
1515 createStandardUtilityFunctions(numTypeInfos,
1516 module,
1517 builder);
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001518 llvm::Function *nativeThrowFunct = module.getFunction(nativeThrowFunctName);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001519
1520 // Create exception throw function using the value ~0 to cause
Chris Lattnera868bbb2011-04-08 18:02:51 +00001521 // foreign exceptions to be thrown.
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001522 llvm::Function *throwFunct = createThrowExceptionFunction(module,
1523 builder,
1524 fpm,
1525 "throwFunct",
1526 ~0,
1527 *nativeThrowFunct);
Chris Lattnera868bbb2011-04-08 18:02:51 +00001528 // Inner function will catch even type infos
1529 unsigned innerExceptionTypesToCatch[] = {6, 2, 4};
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001530 size_t numExceptionTypesToCatch = sizeof(innerExceptionTypesToCatch) /
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001531 sizeof(unsigned);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001532
Chris Lattnera868bbb2011-04-08 18:02:51 +00001533 // Generate inner function.
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001534 llvm::Function *innerCatchFunct = createCatchWrappedInvokeFunction(module,
1535 builder,
1536 fpm,
1537 *throwFunct,
1538 "innerCatchFunct",
1539 numExceptionTypesToCatch,
1540 innerExceptionTypesToCatch);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001541
Chris Lattnera868bbb2011-04-08 18:02:51 +00001542 // Outer function will catch odd type infos
1543 unsigned outerExceptionTypesToCatch[] = {3, 1, 5};
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001544 numExceptionTypesToCatch = sizeof(outerExceptionTypesToCatch) /
Chris Lattnera868bbb2011-04-08 18:02:51 +00001545 sizeof(unsigned);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001546
Chris Lattnera868bbb2011-04-08 18:02:51 +00001547 // Generate outer function
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001548 llvm::Function *outerCatchFunct = createCatchWrappedInvokeFunction(module,
1549 builder,
1550 fpm,
1551 *innerCatchFunct,
1552 "outerCatchFunct",
1553 numExceptionTypesToCatch,
1554 outerExceptionTypesToCatch);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001555
Chris Lattnera868bbb2011-04-08 18:02:51 +00001556 // Return outer function to run
1557 return(outerCatchFunct);
Garrison Vennf4d2f842010-02-09 23:22:43 +00001558}
1559
Juergen Ributzka05c5a932013-11-19 03:08:35 +00001560namespace {
Garrison Vennf4d2f842010-02-09 23:22:43 +00001561/// Represents our foreign exceptions
1562class OurCppRunException : public std::runtime_error {
1563public:
Chris Lattnera868bbb2011-04-08 18:02:51 +00001564 OurCppRunException(const std::string reason) :
1565 std::runtime_error(reason) {}
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001566
Garrison Venn88bd9d62011-04-10 14:06:52 +00001567 OurCppRunException (const OurCppRunException &toCopy) :
Chris Lattnera868bbb2011-04-08 18:02:51 +00001568 std::runtime_error(toCopy) {}
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001569
Garrison Venn88bd9d62011-04-10 14:06:52 +00001570 OurCppRunException &operator = (const OurCppRunException &toCopy) {
Chris Lattnera868bbb2011-04-08 18:02:51 +00001571 return(reinterpret_cast<OurCppRunException&>(
1572 std::runtime_error::operator=(toCopy)));
1573 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001574
Juergen Ributzka05c5a932013-11-19 03:08:35 +00001575 virtual ~OurCppRunException (void) throw () {}
Garrison Vennf4d2f842010-02-09 23:22:43 +00001576};
Juergen Ributzka05c5a932013-11-19 03:08:35 +00001577} // end anonymous namespace
Garrison Vennf4d2f842010-02-09 23:22:43 +00001578
1579/// Throws foreign C++ exception.
1580/// @param ignoreIt unused parameter that allows function to match implied
1581/// generated function contract.
1582extern "C"
1583void throwCppException (int32_t ignoreIt) {
Chris Lattnera868bbb2011-04-08 18:02:51 +00001584 throw(OurCppRunException("thrown by throwCppException(...)"));
Garrison Vennf4d2f842010-02-09 23:22:43 +00001585}
1586
1587typedef void (*OurExceptionThrowFunctType) (int32_t typeToThrow);
1588
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001589/// This is a test harness which runs test by executing generated
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001590/// function with a type info type to throw. Harness wraps the execution
Garrison Vennf4d2f842010-02-09 23:22:43 +00001591/// of generated function in a C++ try catch clause.
1592/// @param engine execution engine to use for executing generated function.
1593/// This demo program expects this to be a JIT instance for demo
1594/// purposes.
1595/// @param function generated test function to run
1596/// @param typeToThrow type info type of generated exception to throw, or
1597/// indicator to cause foreign exception to be thrown.
1598static
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001599void runExceptionThrow(llvm::ExecutionEngine *engine,
1600 llvm::Function *function,
Garrison Vennf4d2f842010-02-09 23:22:43 +00001601 int32_t typeToThrow) {
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001602
Chris Lattnera868bbb2011-04-08 18:02:51 +00001603 // Find test's function pointer
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001604 OurExceptionThrowFunctType functPtr =
Chris Lattnera868bbb2011-04-08 18:02:51 +00001605 reinterpret_cast<OurExceptionThrowFunctType>(
1606 reinterpret_cast<intptr_t>(engine->getPointerToFunction(function)));
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001607
Chris Lattnera868bbb2011-04-08 18:02:51 +00001608 try {
1609 // Run test
1610 (*functPtr)(typeToThrow);
1611 }
1612 catch (OurCppRunException exc) {
1613 // Catch foreign C++ exception
1614 fprintf(stderr,
1615 "\nrunExceptionThrow(...):In C++ catch OurCppRunException "
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001616 "with reason: %s.\n",
Chris Lattnera868bbb2011-04-08 18:02:51 +00001617 exc.what());
1618 }
1619 catch (...) {
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001620 // Catch all exceptions including our generated ones. This latter
Garrison Venn56c58ce2011-09-28 10:53:56 +00001621 // functionality works according to the example in rules 1.6.4 of
Dmitri Gribenko462462e2013-01-13 15:53:09 +00001622 // http://mentorembedded.github.com/cxx-abi/abi-eh.html (v1.22),
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001623 // given that these will be exceptions foreign to C++
1624 // (the _Unwind_Exception::exception_class should be different from
Garrison Venn56c58ce2011-09-28 10:53:56 +00001625 // the one used by C++).
Chris Lattnera868bbb2011-04-08 18:02:51 +00001626 fprintf(stderr,
1627 "\nrunExceptionThrow(...):In C++ catch all.\n");
1628 }
Garrison Vennf4d2f842010-02-09 23:22:43 +00001629}
1630
1631//
1632// End test functions
1633//
1634
Garrison Venn5fb3f662011-07-11 16:31:53 +00001635typedef llvm::ArrayRef<llvm::Type*> TypeArray;
Chris Lattnerca320f52011-04-08 17:56:47 +00001636
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001637/// This initialization routine creates type info globals and
Garrison Vennf4d2f842010-02-09 23:22:43 +00001638/// adds external function declarations to module.
1639/// @param numTypeInfos number of linear type info associated type info types
1640/// to create as GlobalVariable instances, starting with the value 1.
1641/// @param module code for module instance
1642/// @param builder builder instance
1643static void createStandardUtilityFunctions(unsigned numTypeInfos,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001644 llvm::Module &module,
Garrison Venn88bd9d62011-04-10 14:06:52 +00001645 llvm::IRBuilder<> &builder) {
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001646
Garrison Venn88bd9d62011-04-10 14:06:52 +00001647 llvm::LLVMContext &context = module.getContext();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001648
Chris Lattnera868bbb2011-04-08 18:02:51 +00001649 // Exception initializations
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001650
Chris Lattnera868bbb2011-04-08 18:02:51 +00001651 // Setup exception catch state
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001652 ourExceptionNotThrownState =
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001653 llvm::ConstantInt::get(llvm::Type::getInt8Ty(context), 0),
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001654 ourExceptionThrownState =
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001655 llvm::ConstantInt::get(llvm::Type::getInt8Ty(context), 1),
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001656 ourExceptionCaughtState =
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001657 llvm::ConstantInt::get(llvm::Type::getInt8Ty(context), 2),
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001658
1659
1660
Chris Lattnera868bbb2011-04-08 18:02:51 +00001661 // Create our type info type
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001662 ourTypeInfoType = llvm::StructType::get(context,
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001663 TypeArray(builder.getInt32Ty()));
Garrison Venn8cb00352011-09-22 15:45:14 +00001664
Garrison Venn8cb00352011-09-22 15:45:14 +00001665 llvm::Type *caughtResultFieldTypes[] = {
1666 builder.getInt8PtrTy(),
1667 builder.getInt32Ty()
1668 };
1669
1670 // Create our landingpad result type
1671 ourCaughtResultType = llvm::StructType::get(context,
1672 TypeArray(caughtResultFieldTypes));
1673
Chris Lattnera868bbb2011-04-08 18:02:51 +00001674 // Create OurException type
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001675 ourExceptionType = llvm::StructType::get(context,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001676 TypeArray(ourTypeInfoType));
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001677
Chris Lattnera868bbb2011-04-08 18:02:51 +00001678 // Create portion of _Unwind_Exception type
1679 //
1680 // Note: Declaring only a portion of the _Unwind_Exception struct.
1681 // Does this cause problems?
1682 ourUnwindExceptionType =
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001683 llvm::StructType::get(context,
Garrison Venn76310ac2011-07-12 15:34:42 +00001684 TypeArray(builder.getInt64Ty()));
Garrison Venn5fb3f662011-07-11 16:31:53 +00001685
Chris Lattnera868bbb2011-04-08 18:02:51 +00001686 struct OurBaseException_t dummyException;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001687
Chris Lattnera868bbb2011-04-08 18:02:51 +00001688 // Calculate offset of OurException::unwindException member.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001689 ourBaseFromUnwindOffset = ((uintptr_t) &dummyException) -
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001690 ((uintptr_t) &(dummyException.unwindException));
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001691
Garrison Vennf4d2f842010-02-09 23:22:43 +00001692#ifdef DEBUG
Chris Lattnera868bbb2011-04-08 18:02:51 +00001693 fprintf(stderr,
1694 "createStandardUtilityFunctions(...):ourBaseFromUnwindOffset "
1695 "= %lld, sizeof(struct OurBaseException_t) - "
1696 "sizeof(struct _Unwind_Exception) = %lu.\n",
1697 ourBaseFromUnwindOffset,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001698 sizeof(struct OurBaseException_t) -
Chris Lattnera868bbb2011-04-08 18:02:51 +00001699 sizeof(struct _Unwind_Exception));
Garrison Vennf4d2f842010-02-09 23:22:43 +00001700#endif
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001701
Chris Lattnera868bbb2011-04-08 18:02:51 +00001702 size_t numChars = sizeof(ourBaseExcpClassChars) / sizeof(char);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001703
Chris Lattnera868bbb2011-04-08 18:02:51 +00001704 // Create our _Unwind_Exception::exception_class value
1705 ourBaseExceptionClass = genClass(ourBaseExcpClassChars, numChars);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001706
Chris Lattnera868bbb2011-04-08 18:02:51 +00001707 // Type infos
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001708
Chris Lattnera868bbb2011-04-08 18:02:51 +00001709 std::string baseStr = "typeInfo", typeInfoName;
1710 std::ostringstream typeInfoNameBuilder;
1711 std::vector<llvm::Constant*> structVals;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001712
Chris Lattnera868bbb2011-04-08 18:02:51 +00001713 llvm::Constant *nextStruct;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001714
Chris Lattnera868bbb2011-04-08 18:02:51 +00001715 // Generate each type info
1716 //
1717 // Note: First type info is not used.
1718 for (unsigned i = 0; i <= numTypeInfos; ++i) {
1719 structVals.clear();
1720 structVals.push_back(llvm::ConstantInt::get(builder.getInt32Ty(), i));
1721 nextStruct = llvm::ConstantStruct::get(ourTypeInfoType, structVals);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001722
Chris Lattnera868bbb2011-04-08 18:02:51 +00001723 typeInfoNameBuilder.str("");
1724 typeInfoNameBuilder << baseStr << i;
1725 typeInfoName = typeInfoNameBuilder.str();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001726
Chris Lattnera868bbb2011-04-08 18:02:51 +00001727 // Note: Does not seem to work without allocation
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001728 new llvm::GlobalVariable(module,
1729 ourTypeInfoType,
1730 true,
1731 llvm::GlobalValue::ExternalLinkage,
1732 nextStruct,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001733 typeInfoName);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001734
Chris Lattnera868bbb2011-04-08 18:02:51 +00001735 ourTypeInfoNames.push_back(typeInfoName);
1736 ourTypeInfoNamesIndex[i] = typeInfoName;
1737 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001738
Chris Lattnera868bbb2011-04-08 18:02:51 +00001739 ArgNames argNames;
1740 ArgTypes argTypes;
Garrison Venn88bd9d62011-04-10 14:06:52 +00001741 llvm::Function *funct = NULL;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001742
Chris Lattnera868bbb2011-04-08 18:02:51 +00001743 // print32Int
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001744
Chris Lattner6b8eb8c2011-07-18 04:52:09 +00001745 llvm::Type *retType = builder.getVoidTy();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001746
Chris Lattnera868bbb2011-04-08 18:02:51 +00001747 argTypes.clear();
Garrison Venn76310ac2011-07-12 15:34:42 +00001748 argTypes.push_back(builder.getInt32Ty());
1749 argTypes.push_back(builder.getInt8PtrTy());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001750
Chris Lattnera868bbb2011-04-08 18:02:51 +00001751 argNames.clear();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001752
1753 createFunction(module,
1754 retType,
1755 argTypes,
1756 argNames,
1757 "print32Int",
1758 llvm::Function::ExternalLinkage,
1759 true,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001760 false);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001761
Chris Lattnera868bbb2011-04-08 18:02:51 +00001762 // print64Int
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001763
Chris Lattnera868bbb2011-04-08 18:02:51 +00001764 retType = builder.getVoidTy();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001765
Chris Lattnera868bbb2011-04-08 18:02:51 +00001766 argTypes.clear();
Garrison Venn76310ac2011-07-12 15:34:42 +00001767 argTypes.push_back(builder.getInt64Ty());
1768 argTypes.push_back(builder.getInt8PtrTy());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001769
Chris Lattnera868bbb2011-04-08 18:02:51 +00001770 argNames.clear();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001771
1772 createFunction(module,
1773 retType,
1774 argTypes,
1775 argNames,
1776 "print64Int",
1777 llvm::Function::ExternalLinkage,
1778 true,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001779 false);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001780
Chris Lattnera868bbb2011-04-08 18:02:51 +00001781 // printStr
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001782
Chris Lattnera868bbb2011-04-08 18:02:51 +00001783 retType = builder.getVoidTy();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001784
Chris Lattnera868bbb2011-04-08 18:02:51 +00001785 argTypes.clear();
Garrison Venn76310ac2011-07-12 15:34:42 +00001786 argTypes.push_back(builder.getInt8PtrTy());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001787
Chris Lattnera868bbb2011-04-08 18:02:51 +00001788 argNames.clear();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001789
1790 createFunction(module,
1791 retType,
1792 argTypes,
1793 argNames,
1794 "printStr",
1795 llvm::Function::ExternalLinkage,
1796 true,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001797 false);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001798
Chris Lattnera868bbb2011-04-08 18:02:51 +00001799 // throwCppException
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001800
Chris Lattnera868bbb2011-04-08 18:02:51 +00001801 retType = builder.getVoidTy();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001802
Chris Lattnera868bbb2011-04-08 18:02:51 +00001803 argTypes.clear();
Garrison Venn76310ac2011-07-12 15:34:42 +00001804 argTypes.push_back(builder.getInt32Ty());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001805
Chris Lattnera868bbb2011-04-08 18:02:51 +00001806 argNames.clear();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001807
1808 createFunction(module,
1809 retType,
1810 argTypes,
1811 argNames,
1812 "throwCppException",
1813 llvm::Function::ExternalLinkage,
1814 true,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001815 false);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001816
Chris Lattnera868bbb2011-04-08 18:02:51 +00001817 // deleteOurException
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001818
Chris Lattnera868bbb2011-04-08 18:02:51 +00001819 retType = builder.getVoidTy();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001820
Chris Lattnera868bbb2011-04-08 18:02:51 +00001821 argTypes.clear();
Garrison Venn76310ac2011-07-12 15:34:42 +00001822 argTypes.push_back(builder.getInt8PtrTy());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001823
Chris Lattnera868bbb2011-04-08 18:02:51 +00001824 argNames.clear();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001825
1826 createFunction(module,
1827 retType,
1828 argTypes,
1829 argNames,
1830 "deleteOurException",
1831 llvm::Function::ExternalLinkage,
1832 true,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001833 false);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001834
Chris Lattnera868bbb2011-04-08 18:02:51 +00001835 // createOurException
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001836
Garrison Venn76310ac2011-07-12 15:34:42 +00001837 retType = builder.getInt8PtrTy();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001838
Chris Lattnera868bbb2011-04-08 18:02:51 +00001839 argTypes.clear();
Garrison Venn76310ac2011-07-12 15:34:42 +00001840 argTypes.push_back(builder.getInt32Ty());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001841
Chris Lattnera868bbb2011-04-08 18:02:51 +00001842 argNames.clear();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001843
1844 createFunction(module,
1845 retType,
1846 argTypes,
1847 argNames,
1848 "createOurException",
1849 llvm::Function::ExternalLinkage,
1850 true,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001851 false);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001852
Chris Lattnera868bbb2011-04-08 18:02:51 +00001853 // _Unwind_RaiseException
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001854
Chris Lattnera868bbb2011-04-08 18:02:51 +00001855 retType = builder.getInt32Ty();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001856
Chris Lattnera868bbb2011-04-08 18:02:51 +00001857 argTypes.clear();
Garrison Venn76310ac2011-07-12 15:34:42 +00001858 argTypes.push_back(builder.getInt8PtrTy());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001859
Chris Lattnera868bbb2011-04-08 18:02:51 +00001860 argNames.clear();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001861
1862 funct = createFunction(module,
1863 retType,
1864 argTypes,
1865 argNames,
1866 "_Unwind_RaiseException",
1867 llvm::Function::ExternalLinkage,
1868 true,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001869 false);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001870
NAKAMURA Takumi8489be82012-10-12 14:11:48 +00001871 funct->setDoesNotReturn();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001872
Chris Lattnera868bbb2011-04-08 18:02:51 +00001873 // _Unwind_Resume
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001874
Chris Lattnera868bbb2011-04-08 18:02:51 +00001875 retType = builder.getInt32Ty();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001876
Chris Lattnera868bbb2011-04-08 18:02:51 +00001877 argTypes.clear();
Garrison Venn76310ac2011-07-12 15:34:42 +00001878 argTypes.push_back(builder.getInt8PtrTy());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001879
Chris Lattnera868bbb2011-04-08 18:02:51 +00001880 argNames.clear();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001881
1882 funct = createFunction(module,
1883 retType,
1884 argTypes,
1885 argNames,
1886 "_Unwind_Resume",
1887 llvm::Function::ExternalLinkage,
1888 true,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001889 false);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001890
NAKAMURA Takumi8489be82012-10-12 14:11:48 +00001891 funct->setDoesNotReturn();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001892
Chris Lattnera868bbb2011-04-08 18:02:51 +00001893 // ourPersonality
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001894
Chris Lattnera868bbb2011-04-08 18:02:51 +00001895 retType = builder.getInt32Ty();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001896
Chris Lattnera868bbb2011-04-08 18:02:51 +00001897 argTypes.clear();
Garrison Venn76310ac2011-07-12 15:34:42 +00001898 argTypes.push_back(builder.getInt32Ty());
1899 argTypes.push_back(builder.getInt32Ty());
1900 argTypes.push_back(builder.getInt64Ty());
1901 argTypes.push_back(builder.getInt8PtrTy());
1902 argTypes.push_back(builder.getInt8PtrTy());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001903
Chris Lattnera868bbb2011-04-08 18:02:51 +00001904 argNames.clear();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001905
1906 createFunction(module,
1907 retType,
1908 argTypes,
1909 argNames,
1910 "ourPersonality",
1911 llvm::Function::ExternalLinkage,
1912 true,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001913 false);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001914
Chris Lattnera868bbb2011-04-08 18:02:51 +00001915 // llvm.eh.typeid.for intrinsic
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001916
Chris Lattnera868bbb2011-04-08 18:02:51 +00001917 getDeclaration(&module, llvm::Intrinsic::eh_typeid_for);
Garrison Vennf4d2f842010-02-09 23:22:43 +00001918}
1919
1920
Chris Lattnera868bbb2011-04-08 18:02:51 +00001921//===----------------------------------------------------------------------===//
Garrison Vennf4d2f842010-02-09 23:22:43 +00001922// Main test driver code.
Chris Lattnera868bbb2011-04-08 18:02:51 +00001923//===----------------------------------------------------------------------===//
Garrison Vennf4d2f842010-02-09 23:22:43 +00001924
1925/// Demo main routine which takes the type info types to throw. A test will
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001926/// be run for each given type info type. While type info types with the value
Garrison Vennf4d2f842010-02-09 23:22:43 +00001927/// of -1 will trigger a foreign C++ exception to be thrown; type info types
1928/// <= 6 and >= 1 will be caught by test functions; and type info types > 6
1929/// will result in exceptions which pass through to the test harness. All other
1930/// type info types are not supported and could cause a crash.
Garrison Venn88bd9d62011-04-10 14:06:52 +00001931int main(int argc, char *argv[]) {
Chris Lattnera868bbb2011-04-08 18:02:51 +00001932 if (argc == 1) {
1933 fprintf(stderr,
1934 "\nUsage: ExceptionDemo <exception type to throw> "
1935 "[<type 2>...<type n>].\n"
1936 " Each type must have the value of 1 - 6 for "
1937 "generated exceptions to be caught;\n"
1938 " the value -1 for foreign C++ exceptions to be "
1939 "generated and thrown;\n"
1940 " or the values > 6 for exceptions to be ignored.\n"
1941 "\nTry: ExceptionDemo 2 3 7 -1\n"
1942 " for a full test.\n\n");
1943 return(0);
1944 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001945
Chris Lattnera868bbb2011-04-08 18:02:51 +00001946 // If not set, exception handling will not be turned on
Peter Collingbournedff24782011-12-07 23:58:57 +00001947 llvm::TargetOptions Opts;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001948
Chris Lattnera868bbb2011-04-08 18:02:51 +00001949 llvm::InitializeNativeTarget();
Rafael Espindolae93dc3b2013-05-05 20:57:58 +00001950 llvm::InitializeNativeTargetAsmPrinter();
Garrison Venn88bd9d62011-04-10 14:06:52 +00001951 llvm::LLVMContext &context = llvm::getGlobalContext();
Chris Lattnera868bbb2011-04-08 18:02:51 +00001952 llvm::IRBuilder<> theBuilder(context);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001953
Chris Lattnera868bbb2011-04-08 18:02:51 +00001954 // Make the module, which holds all the code.
Rafael Espindola2a8a2792014-08-19 04:04:25 +00001955 std::unique_ptr<llvm::Module> Owner =
1956 llvm::make_unique<llvm::Module>("my cool jit", context);
1957 llvm::Module *module = Owner.get();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001958
NAKAMURA Takumi4c71cc12014-12-03 02:05:51 +00001959 std::unique_ptr<llvm::RTDyldMemoryManager> MemMgr(new llvm::SectionMemoryManager());
Rafael Espindolae93dc3b2013-05-05 20:57:58 +00001960
Chris Lattnera868bbb2011-04-08 18:02:51 +00001961 // Build engine with JIT
Rafael Espindola2a8a2792014-08-19 04:04:25 +00001962 llvm::EngineBuilder factory(std::move(Owner));
Chris Lattnera868bbb2011-04-08 18:02:51 +00001963 factory.setEngineKind(llvm::EngineKind::JIT);
Peter Collingbournedff24782011-12-07 23:58:57 +00001964 factory.setTargetOptions(Opts);
NAKAMURA Takumi4c71cc12014-12-03 02:05:51 +00001965 factory.setMCJITMemoryManager(std::move(MemMgr));
Garrison Venn88bd9d62011-04-10 14:06:52 +00001966 llvm::ExecutionEngine *executionEngine = factory.create();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001967
Chris Lattnera868bbb2011-04-08 18:02:51 +00001968 {
Chandler Carruth7ecd9912015-02-13 10:21:05 +00001969 llvm::legacy::FunctionPassManager fpm(module);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001970
1971 // Set up the optimizer pipeline.
Chris Lattnera868bbb2011-04-08 18:02:51 +00001972 // Start with registering info about how the
1973 // target lays out data structures.
Rafael Espindola339430f2014-02-25 23:25:17 +00001974 module->setDataLayout(executionEngine->getDataLayout());
Rafael Espindolac435adc2014-09-10 21:27:43 +00001975 fpm.add(new llvm::DataLayoutPass());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001976
Chris Lattnera868bbb2011-04-08 18:02:51 +00001977 // Optimizations turned on
1978#ifdef ADD_OPT_PASSES
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001979
Chris Lattnera868bbb2011-04-08 18:02:51 +00001980 // Basic AliasAnslysis support for GVN.
1981 fpm.add(llvm::createBasicAliasAnalysisPass());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001982
Chris Lattnera868bbb2011-04-08 18:02:51 +00001983 // Promote allocas to registers.
1984 fpm.add(llvm::createPromoteMemoryToRegisterPass());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001985
Chris Lattnera868bbb2011-04-08 18:02:51 +00001986 // Do simple "peephole" optimizations and bit-twiddling optzns.
1987 fpm.add(llvm::createInstructionCombiningPass());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001988
Chris Lattnera868bbb2011-04-08 18:02:51 +00001989 // Reassociate expressions.
1990 fpm.add(llvm::createReassociatePass());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001991
Chris Lattnera868bbb2011-04-08 18:02:51 +00001992 // Eliminate Common SubExpressions.
1993 fpm.add(llvm::createGVNPass());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001994
1995 // Simplify the control flow graph (deleting unreachable
Chris Lattnera868bbb2011-04-08 18:02:51 +00001996 // blocks, etc).
1997 fpm.add(llvm::createCFGSimplificationPass());
1998#endif // ADD_OPT_PASSES
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001999
Chris Lattnera868bbb2011-04-08 18:02:51 +00002000 fpm.doInitialization();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00002001
Chris Lattnera868bbb2011-04-08 18:02:51 +00002002 // Generate test code using function throwCppException(...) as
2003 // the function which throws foreign exceptions.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00002004 llvm::Function *toRun =
2005 createUnwindExceptionTest(*module,
2006 theBuilder,
Garrison Venn8cb00352011-09-22 15:45:14 +00002007 fpm,
2008 "throwCppException");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00002009
Rafael Espindolae93dc3b2013-05-05 20:57:58 +00002010 executionEngine->finalizeObject();
2011
Chris Lattnera868bbb2011-04-08 18:02:51 +00002012 fprintf(stderr, "\nBegin module dump:\n\n");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00002013
Chris Lattnera868bbb2011-04-08 18:02:51 +00002014 module->dump();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00002015
Chris Lattnera868bbb2011-04-08 18:02:51 +00002016 fprintf(stderr, "\nEnd module dump:\n");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00002017
Chris Lattnera868bbb2011-04-08 18:02:51 +00002018 fprintf(stderr, "\n\nBegin Test:\n");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00002019
Chris Lattnera868bbb2011-04-08 18:02:51 +00002020 for (int i = 1; i < argc; ++i) {
2021 // Run test for each argument whose value is the exception
2022 // type to throw.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00002023 runExceptionThrow(executionEngine,
2024 toRun,
Chris Lattnera868bbb2011-04-08 18:02:51 +00002025 (unsigned) strtoul(argv[i], NULL, 10));
2026 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00002027
Chris Lattnera868bbb2011-04-08 18:02:51 +00002028 fprintf(stderr, "\nEnd Test:\n\n");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00002029 }
2030
Chris Lattnera868bbb2011-04-08 18:02:51 +00002031 delete executionEngine;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00002032
Chris Lattnera868bbb2011-04-08 18:02:51 +00002033 return 0;
Garrison Vennf4d2f842010-02-09 23:22:43 +00002034}