blob: d50c7769526d91f7d6783baf9ecc5abdcacceef3 [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"
59#include "llvm/IR/Module.h"
Garrison Vennf4d2f842010-02-09 23:22:43 +000060#include "llvm/PassManager.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
1125static
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001126llvm::Function *createCatchWrappedInvokeFunction(llvm::Module &module,
1127 llvm::IRBuilder<> &builder,
Garrison Venn88bd9d62011-04-10 14:06:52 +00001128 llvm::FunctionPassManager &fpm,
1129 llvm::Function &toInvoke,
1130 std::string ourId,
1131 unsigned numExceptionsToCatch,
1132 unsigned exceptionTypesToCatch[]) {
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001133
Garrison Venn88bd9d62011-04-10 14:06:52 +00001134 llvm::LLVMContext &context = module.getContext();
Chris Lattnera868bbb2011-04-08 18:02:51 +00001135 llvm::Function *toPrint32Int = module.getFunction("print32Int");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001136
Chris Lattnera868bbb2011-04-08 18:02:51 +00001137 ArgTypes argTypes;
Garrison Venn76310ac2011-07-12 15:34:42 +00001138 argTypes.push_back(builder.getInt32Ty());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001139
Chris Lattnera868bbb2011-04-08 18:02:51 +00001140 ArgNames argNames;
1141 argNames.push_back("exceptTypeToThrow");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001142
1143 llvm::Function *ret = createFunction(module,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001144 builder.getVoidTy(),
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001145 argTypes,
1146 argNames,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001147 ourId,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001148 llvm::Function::ExternalLinkage,
1149 false,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001150 false);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001151
Chris Lattnera868bbb2011-04-08 18:02:51 +00001152 // Block which calls invoke
1153 llvm::BasicBlock *entryBlock = llvm::BasicBlock::Create(context,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001154 "entry",
Chris Lattnera868bbb2011-04-08 18:02:51 +00001155 ret);
1156 // Normal block for invoke
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001157 llvm::BasicBlock *normalBlock = llvm::BasicBlock::Create(context,
1158 "normal",
Chris Lattnera868bbb2011-04-08 18:02:51 +00001159 ret);
1160 // Unwind block for invoke
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001161 llvm::BasicBlock *exceptionBlock = llvm::BasicBlock::Create(context,
1162 "exception",
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001163 ret);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001164
Chris Lattnera868bbb2011-04-08 18:02:51 +00001165 // Block which routes exception to correct catch handler block
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001166 llvm::BasicBlock *exceptionRouteBlock = llvm::BasicBlock::Create(context,
1167 "exceptionRoute",
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001168 ret);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001169
Chris Lattnera868bbb2011-04-08 18:02:51 +00001170 // Foreign exception handler
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001171 llvm::BasicBlock *externalExceptionBlock = llvm::BasicBlock::Create(context,
1172 "externalException",
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001173 ret);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001174
Chris Lattnera868bbb2011-04-08 18:02:51 +00001175 // Block which calls _Unwind_Resume
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001176 llvm::BasicBlock *unwindResumeBlock = llvm::BasicBlock::Create(context,
1177 "unwindResume",
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001178 ret);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001179
Chris Lattnera868bbb2011-04-08 18:02:51 +00001180 // Clean up block which delete exception if needed
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001181 llvm::BasicBlock *endBlock = llvm::BasicBlock::Create(context, "end", ret);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001182
Chris Lattnera868bbb2011-04-08 18:02:51 +00001183 std::string nextName;
1184 std::vector<llvm::BasicBlock*> catchBlocks(numExceptionsToCatch);
Garrison Venn88bd9d62011-04-10 14:06:52 +00001185 llvm::Value *exceptionCaughtFlag = NULL;
1186 llvm::Value *exceptionStorage = NULL;
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001187 llvm::Value *caughtResultStorage = NULL;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001188
1189 // Finally block which will branch to unwindResumeBlock if
Chris Lattnera868bbb2011-04-08 18:02:51 +00001190 // exception is not caught. Initializes/allocates stack locations.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001191 llvm::BasicBlock *finallyBlock = createFinallyBlock(context,
1192 module,
1193 builder,
1194 *ret,
1195 nextName = "finally",
Chris Lattnera868bbb2011-04-08 18:02:51 +00001196 ourId,
1197 *endBlock,
1198 *unwindResumeBlock,
1199 &exceptionCaughtFlag,
Bill Wendling383727b2012-02-04 00:29:12 +00001200 &exceptionStorage,
1201 &caughtResultStorage
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001202 );
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001203
Chris Lattnera868bbb2011-04-08 18:02:51 +00001204 for (unsigned i = 0; i < numExceptionsToCatch; ++i) {
1205 nextName = ourTypeInfoNames[exceptionTypesToCatch[i]];
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001206
Chris Lattnera868bbb2011-04-08 18:02:51 +00001207 // One catch block per type info to be caught
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001208 catchBlocks[i] = createCatchBlock(context,
1209 module,
1210 builder,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001211 *ret,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001212 nextName,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001213 ourId,
1214 *finallyBlock,
1215 *exceptionCaughtFlag);
1216 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001217
Chris Lattnera868bbb2011-04-08 18:02:51 +00001218 // Entry Block
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001219
Chris Lattnera868bbb2011-04-08 18:02:51 +00001220 builder.SetInsertPoint(entryBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001221
Chris Lattnera868bbb2011-04-08 18:02:51 +00001222 std::vector<llvm::Value*> args;
1223 args.push_back(namedValues["exceptTypeToThrow"]);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001224 builder.CreateInvoke(&toInvoke,
1225 normalBlock,
1226 exceptionBlock,
Chris Lattner6b8eb8c2011-07-18 04:52:09 +00001227 args);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001228
Chris Lattnera868bbb2011-04-08 18:02:51 +00001229 // End Block
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001230
Chris Lattnera868bbb2011-04-08 18:02:51 +00001231 builder.SetInsertPoint(endBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001232
1233 generateStringPrint(context,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001234 module,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001235 builder,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001236 "Gen: In end block: exiting in " + ourId + ".\n",
1237 USE_GLOBAL_STR_CONSTS);
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001238 llvm::Function *deleteOurException = module.getFunction("deleteOurException");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001239
Chris Lattnera868bbb2011-04-08 18:02:51 +00001240 // Note: function handles NULL exceptions
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001241 builder.CreateCall(deleteOurException,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001242 builder.CreateLoad(exceptionStorage));
1243 builder.CreateRetVoid();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001244
Chris Lattnera868bbb2011-04-08 18:02:51 +00001245 // Normal Block
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001246
Chris Lattnera868bbb2011-04-08 18:02:51 +00001247 builder.SetInsertPoint(normalBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001248
1249 generateStringPrint(context,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001250 module,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001251 builder,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001252 "Gen: No exception in " + ourId + "!\n",
1253 USE_GLOBAL_STR_CONSTS);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001254
Chris Lattnera868bbb2011-04-08 18:02:51 +00001255 // Finally block is always called
1256 builder.CreateBr(finallyBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001257
Chris Lattnera868bbb2011-04-08 18:02:51 +00001258 // Unwind Resume Block
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001259
Chris Lattnera868bbb2011-04-08 18:02:51 +00001260 builder.SetInsertPoint(unwindResumeBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001261
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001262 builder.CreateResume(builder.CreateLoad(caughtResultStorage));
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001263
Chris Lattnera868bbb2011-04-08 18:02:51 +00001264 // Exception Block
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001265
Chris Lattnera868bbb2011-04-08 18:02:51 +00001266 builder.SetInsertPoint(exceptionBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001267
Garrison Venn8cb00352011-09-22 15:45:14 +00001268 llvm::Function *personality = module.getFunction("ourPersonality");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001269
1270 llvm::LandingPadInst *caughtResult =
Garrison Venn8cb00352011-09-22 15:45:14 +00001271 builder.CreateLandingPad(ourCaughtResultType,
1272 personality,
1273 numExceptionsToCatch,
1274 "landingPad");
1275
1276 caughtResult->setCleanup(true);
1277
1278 for (unsigned i = 0; i < numExceptionsToCatch; ++i) {
1279 // Set up type infos to be caught
1280 caughtResult->addClause(module.getGlobalVariable(
1281 ourTypeInfoNames[exceptionTypesToCatch[i]]));
1282 }
1283
1284 llvm::Value *unwindException = builder.CreateExtractValue(caughtResult, 0);
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001285 llvm::Value *retTypeInfoIndex = builder.CreateExtractValue(caughtResult, 1);
Garrison Venn8cb00352011-09-22 15:45:14 +00001286
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001287 // FIXME: Redundant storage which, beyond utilizing value of
1288 // caughtResultStore for unwindException storage, may be alleviated
Benjamin Kramerbde91762012-06-02 10:20:22 +00001289 // altogether with a block rearrangement
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001290 builder.CreateStore(caughtResult, caughtResultStorage);
Garrison Venn8cb00352011-09-22 15:45:14 +00001291 builder.CreateStore(unwindException, exceptionStorage);
1292 builder.CreateStore(ourExceptionThrownState, exceptionCaughtFlag);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001293
1294 // Retrieve exception_class member from thrown exception
Chris Lattnera868bbb2011-04-08 18:02:51 +00001295 // (_Unwind_Exception instance). This member tells us whether or not
1296 // the exception is foreign.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001297 llvm::Value *unwindExceptionClass =
Chris Lattnera868bbb2011-04-08 18:02:51 +00001298 builder.CreateLoad(builder.CreateStructGEP(
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001299 builder.CreatePointerCast(unwindException,
Micah Villmow51e72462012-10-24 17:25:11 +00001300 ourUnwindExceptionType->getPointerTo()),
Chris Lattnera868bbb2011-04-08 18:02:51 +00001301 0));
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001302
Chris Lattnera868bbb2011-04-08 18:02:51 +00001303 // Branch to the externalExceptionBlock if the exception is foreign or
1304 // to a catch router if not. Either way the finally block will be run.
1305 builder.CreateCondBr(builder.CreateICmpEQ(unwindExceptionClass,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001306 llvm::ConstantInt::get(builder.getInt64Ty(),
Chris Lattnera868bbb2011-04-08 18:02:51 +00001307 ourBaseExceptionClass)),
1308 exceptionRouteBlock,
1309 externalExceptionBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001310
Chris Lattnera868bbb2011-04-08 18:02:51 +00001311 // External Exception Block
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001312
Chris Lattnera868bbb2011-04-08 18:02:51 +00001313 builder.SetInsertPoint(externalExceptionBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001314
1315 generateStringPrint(context,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001316 module,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001317 builder,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001318 "Gen: Foreign exception received.\n",
1319 USE_GLOBAL_STR_CONSTS);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001320
Chris Lattnera868bbb2011-04-08 18:02:51 +00001321 // Branch to the finally block
1322 builder.CreateBr(finallyBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001323
Chris Lattnera868bbb2011-04-08 18:02:51 +00001324 // Exception Route Block
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001325
Chris Lattnera868bbb2011-04-08 18:02:51 +00001326 builder.SetInsertPoint(exceptionRouteBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001327
1328 // Casts exception pointer (_Unwind_Exception instance) to parent
Chris Lattnera868bbb2011-04-08 18:02:51 +00001329 // (OurException instance).
1330 //
1331 // Note: ourBaseFromUnwindOffset is usually negative
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001332 llvm::Value *typeInfoThrown = builder.CreatePointerCast(
1333 builder.CreateConstGEP1_64(unwindException,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001334 ourBaseFromUnwindOffset),
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001335 ourExceptionType->getPointerTo());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001336
Chris Lattnera868bbb2011-04-08 18:02:51 +00001337 // Retrieve thrown exception type info type
1338 //
1339 // Note: Index is not relative to pointer but instead to structure
1340 // unlike a true getelementptr (GEP) instruction
1341 typeInfoThrown = builder.CreateStructGEP(typeInfoThrown, 0);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001342
1343 llvm::Value *typeInfoThrownType =
Chris Lattnera868bbb2011-04-08 18:02:51 +00001344 builder.CreateStructGEP(typeInfoThrown, 0);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001345
1346 generateIntegerPrint(context,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001347 module,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001348 builder,
1349 *toPrint32Int,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001350 *(builder.CreateLoad(typeInfoThrownType)),
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001351 "Gen: Exception type <%d> received (stack unwound) "
1352 " in " +
1353 ourId +
Chris Lattnera868bbb2011-04-08 18:02:51 +00001354 ".\n",
1355 USE_GLOBAL_STR_CONSTS);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001356
Chris Lattnera868bbb2011-04-08 18:02:51 +00001357 // Route to matched type info catch block or run cleanup finally block
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001358 llvm::SwitchInst *switchToCatchBlock = builder.CreateSwitch(retTypeInfoIndex,
1359 finallyBlock,
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001360 numExceptionsToCatch);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001361
Chris Lattnera868bbb2011-04-08 18:02:51 +00001362 unsigned nextTypeToCatch;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001363
Chris Lattnera868bbb2011-04-08 18:02:51 +00001364 for (unsigned i = 1; i <= numExceptionsToCatch; ++i) {
1365 nextTypeToCatch = i - 1;
1366 switchToCatchBlock->addCase(llvm::ConstantInt::get(
1367 llvm::Type::getInt32Ty(context), i),
1368 catchBlocks[nextTypeToCatch]);
1369 }
Garrison Venneb89d362011-09-22 14:07:50 +00001370
Chris Lattnera868bbb2011-04-08 18:02:51 +00001371 llvm::verifyFunction(*ret);
1372 fpm.run(*ret);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001373
Chris Lattnera868bbb2011-04-08 18:02:51 +00001374 return(ret);
Garrison Vennf4d2f842010-02-09 23:22:43 +00001375}
1376
1377
1378/// Generates function which throws either an exception matched to a runtime
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001379/// determined type info type (argument to generated function), or if this
1380/// runtime value matches nativeThrowType, throws a foreign exception by
Garrison Vennf4d2f842010-02-09 23:22:43 +00001381/// calling nativeThrowFunct.
1382/// @param module code for module instance
1383/// @param builder builder instance
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001384/// @param fpm a function pass manager holding optional IR to IR
Garrison Vennf4d2f842010-02-09 23:22:43 +00001385/// transformations
1386/// @param ourId id used to printing purposes
1387/// @param nativeThrowType a runtime argument of this value results in
1388/// nativeThrowFunct being called to generate/throw exception.
1389/// @param nativeThrowFunct function which will throw a foreign exception
1390/// if the above nativeThrowType matches generated function's arg.
1391/// @returns generated function
1392static
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001393llvm::Function *createThrowExceptionFunction(llvm::Module &module,
1394 llvm::IRBuilder<> &builder,
Garrison Venn88bd9d62011-04-10 14:06:52 +00001395 llvm::FunctionPassManager &fpm,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001396 std::string ourId,
1397 int32_t nativeThrowType,
Garrison Venn88bd9d62011-04-10 14:06:52 +00001398 llvm::Function &nativeThrowFunct) {
1399 llvm::LLVMContext &context = module.getContext();
Chris Lattnera868bbb2011-04-08 18:02:51 +00001400 namedValues.clear();
1401 ArgTypes unwindArgTypes;
Garrison Venn76310ac2011-07-12 15:34:42 +00001402 unwindArgTypes.push_back(builder.getInt32Ty());
Chris Lattnera868bbb2011-04-08 18:02:51 +00001403 ArgNames unwindArgNames;
1404 unwindArgNames.push_back("exceptTypeToThrow");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001405
Chris Lattnera868bbb2011-04-08 18:02:51 +00001406 llvm::Function *ret = createFunction(module,
1407 builder.getVoidTy(),
1408 unwindArgTypes,
1409 unwindArgNames,
1410 ourId,
1411 llvm::Function::ExternalLinkage,
1412 false,
1413 false);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001414
Chris Lattnera868bbb2011-04-08 18:02:51 +00001415 // Throws either one of our exception or a native C++ exception depending
1416 // on a runtime argument value containing a type info type.
1417 llvm::BasicBlock *entryBlock = llvm::BasicBlock::Create(context,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001418 "entry",
Chris Lattnera868bbb2011-04-08 18:02:51 +00001419 ret);
1420 // Throws a foreign exception
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001421 llvm::BasicBlock *nativeThrowBlock = llvm::BasicBlock::Create(context,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001422 "nativeThrow",
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001423 ret);
Chris Lattnera868bbb2011-04-08 18:02:51 +00001424 // Throws one of our Exceptions
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001425 llvm::BasicBlock *generatedThrowBlock = llvm::BasicBlock::Create(context,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001426 "generatedThrow",
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001427 ret);
Chris Lattnera868bbb2011-04-08 18:02:51 +00001428 // Retrieved runtime type info type to throw
Garrison Venn88bd9d62011-04-10 14:06:52 +00001429 llvm::Value *exceptionType = namedValues["exceptTypeToThrow"];
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001430
Chris Lattnera868bbb2011-04-08 18:02:51 +00001431 // nativeThrowBlock block
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001432
Chris Lattnera868bbb2011-04-08 18:02:51 +00001433 builder.SetInsertPoint(nativeThrowBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001434
Chris Lattnera868bbb2011-04-08 18:02:51 +00001435 // Throws foreign exception
1436 builder.CreateCall(&nativeThrowFunct, exceptionType);
1437 builder.CreateUnreachable();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001438
Chris Lattnera868bbb2011-04-08 18:02:51 +00001439 // entry block
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001440
Chris Lattnera868bbb2011-04-08 18:02:51 +00001441 builder.SetInsertPoint(entryBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001442
Chris Lattnera868bbb2011-04-08 18:02:51 +00001443 llvm::Function *toPrint32Int = module.getFunction("print32Int");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001444 generateIntegerPrint(context,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001445 module,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001446 builder,
1447 *toPrint32Int,
1448 *exceptionType,
1449 "\nGen: About to throw exception type <%d> in " +
1450 ourId +
Chris Lattnera868bbb2011-04-08 18:02:51 +00001451 ".\n",
1452 USE_GLOBAL_STR_CONSTS);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001453
Chris Lattnera868bbb2011-04-08 18:02:51 +00001454 // Switches on runtime type info type value to determine whether or not
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001455 // a foreign exception is thrown. Defaults to throwing one of our
Chris Lattnera868bbb2011-04-08 18:02:51 +00001456 // generated exceptions.
Garrison Venn88bd9d62011-04-10 14:06:52 +00001457 llvm::SwitchInst *theSwitch = builder.CreateSwitch(exceptionType,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001458 generatedThrowBlock,
1459 1);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001460
1461 theSwitch->addCase(llvm::ConstantInt::get(llvm::Type::getInt32Ty(context),
Chris Lattnera868bbb2011-04-08 18:02:51 +00001462 nativeThrowType),
1463 nativeThrowBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001464
Chris Lattnera868bbb2011-04-08 18:02:51 +00001465 // generatedThrow block
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001466
Chris Lattnera868bbb2011-04-08 18:02:51 +00001467 builder.SetInsertPoint(generatedThrowBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001468
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001469 llvm::Function *createOurException = module.getFunction("createOurException");
1470 llvm::Function *raiseOurException = module.getFunction(
1471 "_Unwind_RaiseException");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001472
Chris Lattnera868bbb2011-04-08 18:02:51 +00001473 // Creates exception to throw with runtime type info type.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001474 llvm::Value *exception = builder.CreateCall(createOurException,
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001475 namedValues["exceptTypeToThrow"]);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001476
Chris Lattnera868bbb2011-04-08 18:02:51 +00001477 // Throw generated Exception
1478 builder.CreateCall(raiseOurException, exception);
1479 builder.CreateUnreachable();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001480
Chris Lattnera868bbb2011-04-08 18:02:51 +00001481 llvm::verifyFunction(*ret);
1482 fpm.run(*ret);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001483
Chris Lattnera868bbb2011-04-08 18:02:51 +00001484 return(ret);
Garrison Vennf4d2f842010-02-09 23:22:43 +00001485}
1486
1487static void createStandardUtilityFunctions(unsigned numTypeInfos,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001488 llvm::Module &module,
Garrison Venn88bd9d62011-04-10 14:06:52 +00001489 llvm::IRBuilder<> &builder);
Garrison Vennf4d2f842010-02-09 23:22:43 +00001490
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001491/// Creates test code by generating and organizing these functions into the
Garrison Vennf4d2f842010-02-09 23:22:43 +00001492/// test case. The test case consists of an outer function setup to invoke
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001493/// an inner function within an environment having multiple catch and single
Garrison Vennf4d2f842010-02-09 23:22:43 +00001494/// finally blocks. This inner function is also setup to invoke a throw
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001495/// function within an evironment similar in nature to the outer function's
Garrison Vennf4d2f842010-02-09 23:22:43 +00001496/// catch and finally blocks. Each of these two functions catch mutually
1497/// exclusive subsets (even or odd) of the type info types configured
1498/// for this this. All generated functions have a runtime argument which
1499/// holds a type info type to throw that each function takes and passes it
1500/// to the inner one if such a inner function exists. This type info type is
1501/// looked at by the generated throw function to see whether or not it should
1502/// throw a generated exception with the same type info type, or instead call
1503/// a supplied a function which in turn will throw a foreign exception.
1504/// @param module code for module instance
1505/// @param builder builder instance
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001506/// @param fpm a function pass manager holding optional IR to IR
Garrison Vennf4d2f842010-02-09 23:22:43 +00001507/// transformations
1508/// @param nativeThrowFunctName name of external function which will throw
1509/// a foreign exception
1510/// @returns outermost generated test function.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001511llvm::Function *createUnwindExceptionTest(llvm::Module &module,
1512 llvm::IRBuilder<> &builder,
Garrison Venn88bd9d62011-04-10 14:06:52 +00001513 llvm::FunctionPassManager &fpm,
Garrison Vennf4d2f842010-02-09 23:22:43 +00001514 std::string nativeThrowFunctName) {
Chris Lattnera868bbb2011-04-08 18:02:51 +00001515 // Number of type infos to generate
1516 unsigned numTypeInfos = 6;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001517
Chris Lattnera868bbb2011-04-08 18:02:51 +00001518 // Initialze intrisics and external functions to use along with exception
1519 // and type info globals.
1520 createStandardUtilityFunctions(numTypeInfos,
1521 module,
1522 builder);
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001523 llvm::Function *nativeThrowFunct = module.getFunction(nativeThrowFunctName);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001524
1525 // Create exception throw function using the value ~0 to cause
Chris Lattnera868bbb2011-04-08 18:02:51 +00001526 // foreign exceptions to be thrown.
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001527 llvm::Function *throwFunct = createThrowExceptionFunction(module,
1528 builder,
1529 fpm,
1530 "throwFunct",
1531 ~0,
1532 *nativeThrowFunct);
Chris Lattnera868bbb2011-04-08 18:02:51 +00001533 // Inner function will catch even type infos
1534 unsigned innerExceptionTypesToCatch[] = {6, 2, 4};
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001535 size_t numExceptionTypesToCatch = sizeof(innerExceptionTypesToCatch) /
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001536 sizeof(unsigned);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001537
Chris Lattnera868bbb2011-04-08 18:02:51 +00001538 // Generate inner function.
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001539 llvm::Function *innerCatchFunct = createCatchWrappedInvokeFunction(module,
1540 builder,
1541 fpm,
1542 *throwFunct,
1543 "innerCatchFunct",
1544 numExceptionTypesToCatch,
1545 innerExceptionTypesToCatch);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001546
Chris Lattnera868bbb2011-04-08 18:02:51 +00001547 // Outer function will catch odd type infos
1548 unsigned outerExceptionTypesToCatch[] = {3, 1, 5};
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001549 numExceptionTypesToCatch = sizeof(outerExceptionTypesToCatch) /
Chris Lattnera868bbb2011-04-08 18:02:51 +00001550 sizeof(unsigned);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001551
Chris Lattnera868bbb2011-04-08 18:02:51 +00001552 // Generate outer function
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001553 llvm::Function *outerCatchFunct = createCatchWrappedInvokeFunction(module,
1554 builder,
1555 fpm,
1556 *innerCatchFunct,
1557 "outerCatchFunct",
1558 numExceptionTypesToCatch,
1559 outerExceptionTypesToCatch);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001560
Chris Lattnera868bbb2011-04-08 18:02:51 +00001561 // Return outer function to run
1562 return(outerCatchFunct);
Garrison Vennf4d2f842010-02-09 23:22:43 +00001563}
1564
Juergen Ributzka05c5a932013-11-19 03:08:35 +00001565namespace {
Garrison Vennf4d2f842010-02-09 23:22:43 +00001566/// Represents our foreign exceptions
1567class OurCppRunException : public std::runtime_error {
1568public:
Chris Lattnera868bbb2011-04-08 18:02:51 +00001569 OurCppRunException(const std::string reason) :
1570 std::runtime_error(reason) {}
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001571
Garrison Venn88bd9d62011-04-10 14:06:52 +00001572 OurCppRunException (const OurCppRunException &toCopy) :
Chris Lattnera868bbb2011-04-08 18:02:51 +00001573 std::runtime_error(toCopy) {}
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001574
Garrison Venn88bd9d62011-04-10 14:06:52 +00001575 OurCppRunException &operator = (const OurCppRunException &toCopy) {
Chris Lattnera868bbb2011-04-08 18:02:51 +00001576 return(reinterpret_cast<OurCppRunException&>(
1577 std::runtime_error::operator=(toCopy)));
1578 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001579
Juergen Ributzka05c5a932013-11-19 03:08:35 +00001580 virtual ~OurCppRunException (void) throw () {}
Garrison Vennf4d2f842010-02-09 23:22:43 +00001581};
Juergen Ributzka05c5a932013-11-19 03:08:35 +00001582} // end anonymous namespace
Garrison Vennf4d2f842010-02-09 23:22:43 +00001583
1584/// Throws foreign C++ exception.
1585/// @param ignoreIt unused parameter that allows function to match implied
1586/// generated function contract.
1587extern "C"
1588void throwCppException (int32_t ignoreIt) {
Chris Lattnera868bbb2011-04-08 18:02:51 +00001589 throw(OurCppRunException("thrown by throwCppException(...)"));
Garrison Vennf4d2f842010-02-09 23:22:43 +00001590}
1591
1592typedef void (*OurExceptionThrowFunctType) (int32_t typeToThrow);
1593
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001594/// This is a test harness which runs test by executing generated
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001595/// function with a type info type to throw. Harness wraps the execution
Garrison Vennf4d2f842010-02-09 23:22:43 +00001596/// of generated function in a C++ try catch clause.
1597/// @param engine execution engine to use for executing generated function.
1598/// This demo program expects this to be a JIT instance for demo
1599/// purposes.
1600/// @param function generated test function to run
1601/// @param typeToThrow type info type of generated exception to throw, or
1602/// indicator to cause foreign exception to be thrown.
1603static
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001604void runExceptionThrow(llvm::ExecutionEngine *engine,
1605 llvm::Function *function,
Garrison Vennf4d2f842010-02-09 23:22:43 +00001606 int32_t typeToThrow) {
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001607
Chris Lattnera868bbb2011-04-08 18:02:51 +00001608 // Find test's function pointer
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001609 OurExceptionThrowFunctType functPtr =
Chris Lattnera868bbb2011-04-08 18:02:51 +00001610 reinterpret_cast<OurExceptionThrowFunctType>(
1611 reinterpret_cast<intptr_t>(engine->getPointerToFunction(function)));
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001612
Chris Lattnera868bbb2011-04-08 18:02:51 +00001613 try {
1614 // Run test
1615 (*functPtr)(typeToThrow);
1616 }
1617 catch (OurCppRunException exc) {
1618 // Catch foreign C++ exception
1619 fprintf(stderr,
1620 "\nrunExceptionThrow(...):In C++ catch OurCppRunException "
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001621 "with reason: %s.\n",
Chris Lattnera868bbb2011-04-08 18:02:51 +00001622 exc.what());
1623 }
1624 catch (...) {
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001625 // Catch all exceptions including our generated ones. This latter
Garrison Venn56c58ce2011-09-28 10:53:56 +00001626 // functionality works according to the example in rules 1.6.4 of
Dmitri Gribenko462462e2013-01-13 15:53:09 +00001627 // http://mentorembedded.github.com/cxx-abi/abi-eh.html (v1.22),
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001628 // given that these will be exceptions foreign to C++
1629 // (the _Unwind_Exception::exception_class should be different from
Garrison Venn56c58ce2011-09-28 10:53:56 +00001630 // the one used by C++).
Chris Lattnera868bbb2011-04-08 18:02:51 +00001631 fprintf(stderr,
1632 "\nrunExceptionThrow(...):In C++ catch all.\n");
1633 }
Garrison Vennf4d2f842010-02-09 23:22:43 +00001634}
1635
1636//
1637// End test functions
1638//
1639
Garrison Venn5fb3f662011-07-11 16:31:53 +00001640typedef llvm::ArrayRef<llvm::Type*> TypeArray;
Chris Lattnerca320f52011-04-08 17:56:47 +00001641
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001642/// This initialization routine creates type info globals and
Garrison Vennf4d2f842010-02-09 23:22:43 +00001643/// adds external function declarations to module.
1644/// @param numTypeInfos number of linear type info associated type info types
1645/// to create as GlobalVariable instances, starting with the value 1.
1646/// @param module code for module instance
1647/// @param builder builder instance
1648static void createStandardUtilityFunctions(unsigned numTypeInfos,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001649 llvm::Module &module,
Garrison Venn88bd9d62011-04-10 14:06:52 +00001650 llvm::IRBuilder<> &builder) {
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001651
Garrison Venn88bd9d62011-04-10 14:06:52 +00001652 llvm::LLVMContext &context = module.getContext();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001653
Chris Lattnera868bbb2011-04-08 18:02:51 +00001654 // Exception initializations
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001655
Chris Lattnera868bbb2011-04-08 18:02:51 +00001656 // Setup exception catch state
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001657 ourExceptionNotThrownState =
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001658 llvm::ConstantInt::get(llvm::Type::getInt8Ty(context), 0),
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001659 ourExceptionThrownState =
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001660 llvm::ConstantInt::get(llvm::Type::getInt8Ty(context), 1),
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001661 ourExceptionCaughtState =
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001662 llvm::ConstantInt::get(llvm::Type::getInt8Ty(context), 2),
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001663
1664
1665
Chris Lattnera868bbb2011-04-08 18:02:51 +00001666 // Create our type info type
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001667 ourTypeInfoType = llvm::StructType::get(context,
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001668 TypeArray(builder.getInt32Ty()));
Garrison Venn8cb00352011-09-22 15:45:14 +00001669
Garrison Venn8cb00352011-09-22 15:45:14 +00001670 llvm::Type *caughtResultFieldTypes[] = {
1671 builder.getInt8PtrTy(),
1672 builder.getInt32Ty()
1673 };
1674
1675 // Create our landingpad result type
1676 ourCaughtResultType = llvm::StructType::get(context,
1677 TypeArray(caughtResultFieldTypes));
1678
Chris Lattnera868bbb2011-04-08 18:02:51 +00001679 // Create OurException type
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001680 ourExceptionType = llvm::StructType::get(context,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001681 TypeArray(ourTypeInfoType));
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001682
Chris Lattnera868bbb2011-04-08 18:02:51 +00001683 // Create portion of _Unwind_Exception type
1684 //
1685 // Note: Declaring only a portion of the _Unwind_Exception struct.
1686 // Does this cause problems?
1687 ourUnwindExceptionType =
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001688 llvm::StructType::get(context,
Garrison Venn76310ac2011-07-12 15:34:42 +00001689 TypeArray(builder.getInt64Ty()));
Garrison Venn5fb3f662011-07-11 16:31:53 +00001690
Chris Lattnera868bbb2011-04-08 18:02:51 +00001691 struct OurBaseException_t dummyException;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001692
Chris Lattnera868bbb2011-04-08 18:02:51 +00001693 // Calculate offset of OurException::unwindException member.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001694 ourBaseFromUnwindOffset = ((uintptr_t) &dummyException) -
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001695 ((uintptr_t) &(dummyException.unwindException));
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001696
Garrison Vennf4d2f842010-02-09 23:22:43 +00001697#ifdef DEBUG
Chris Lattnera868bbb2011-04-08 18:02:51 +00001698 fprintf(stderr,
1699 "createStandardUtilityFunctions(...):ourBaseFromUnwindOffset "
1700 "= %lld, sizeof(struct OurBaseException_t) - "
1701 "sizeof(struct _Unwind_Exception) = %lu.\n",
1702 ourBaseFromUnwindOffset,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001703 sizeof(struct OurBaseException_t) -
Chris Lattnera868bbb2011-04-08 18:02:51 +00001704 sizeof(struct _Unwind_Exception));
Garrison Vennf4d2f842010-02-09 23:22:43 +00001705#endif
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001706
Chris Lattnera868bbb2011-04-08 18:02:51 +00001707 size_t numChars = sizeof(ourBaseExcpClassChars) / sizeof(char);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001708
Chris Lattnera868bbb2011-04-08 18:02:51 +00001709 // Create our _Unwind_Exception::exception_class value
1710 ourBaseExceptionClass = genClass(ourBaseExcpClassChars, numChars);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001711
Chris Lattnera868bbb2011-04-08 18:02:51 +00001712 // Type infos
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001713
Chris Lattnera868bbb2011-04-08 18:02:51 +00001714 std::string baseStr = "typeInfo", typeInfoName;
1715 std::ostringstream typeInfoNameBuilder;
1716 std::vector<llvm::Constant*> structVals;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001717
Chris Lattnera868bbb2011-04-08 18:02:51 +00001718 llvm::Constant *nextStruct;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001719
Chris Lattnera868bbb2011-04-08 18:02:51 +00001720 // Generate each type info
1721 //
1722 // Note: First type info is not used.
1723 for (unsigned i = 0; i <= numTypeInfos; ++i) {
1724 structVals.clear();
1725 structVals.push_back(llvm::ConstantInt::get(builder.getInt32Ty(), i));
1726 nextStruct = llvm::ConstantStruct::get(ourTypeInfoType, structVals);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001727
Chris Lattnera868bbb2011-04-08 18:02:51 +00001728 typeInfoNameBuilder.str("");
1729 typeInfoNameBuilder << baseStr << i;
1730 typeInfoName = typeInfoNameBuilder.str();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001731
Chris Lattnera868bbb2011-04-08 18:02:51 +00001732 // Note: Does not seem to work without allocation
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001733 new llvm::GlobalVariable(module,
1734 ourTypeInfoType,
1735 true,
1736 llvm::GlobalValue::ExternalLinkage,
1737 nextStruct,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001738 typeInfoName);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001739
Chris Lattnera868bbb2011-04-08 18:02:51 +00001740 ourTypeInfoNames.push_back(typeInfoName);
1741 ourTypeInfoNamesIndex[i] = typeInfoName;
1742 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001743
Chris Lattnera868bbb2011-04-08 18:02:51 +00001744 ArgNames argNames;
1745 ArgTypes argTypes;
Garrison Venn88bd9d62011-04-10 14:06:52 +00001746 llvm::Function *funct = NULL;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001747
Chris Lattnera868bbb2011-04-08 18:02:51 +00001748 // print32Int
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001749
Chris Lattner6b8eb8c2011-07-18 04:52:09 +00001750 llvm::Type *retType = builder.getVoidTy();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001751
Chris Lattnera868bbb2011-04-08 18:02:51 +00001752 argTypes.clear();
Garrison Venn76310ac2011-07-12 15:34:42 +00001753 argTypes.push_back(builder.getInt32Ty());
1754 argTypes.push_back(builder.getInt8PtrTy());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001755
Chris Lattnera868bbb2011-04-08 18:02:51 +00001756 argNames.clear();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001757
1758 createFunction(module,
1759 retType,
1760 argTypes,
1761 argNames,
1762 "print32Int",
1763 llvm::Function::ExternalLinkage,
1764 true,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001765 false);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001766
Chris Lattnera868bbb2011-04-08 18:02:51 +00001767 // print64Int
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001768
Chris Lattnera868bbb2011-04-08 18:02:51 +00001769 retType = builder.getVoidTy();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001770
Chris Lattnera868bbb2011-04-08 18:02:51 +00001771 argTypes.clear();
Garrison Venn76310ac2011-07-12 15:34:42 +00001772 argTypes.push_back(builder.getInt64Ty());
1773 argTypes.push_back(builder.getInt8PtrTy());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001774
Chris Lattnera868bbb2011-04-08 18:02:51 +00001775 argNames.clear();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001776
1777 createFunction(module,
1778 retType,
1779 argTypes,
1780 argNames,
1781 "print64Int",
1782 llvm::Function::ExternalLinkage,
1783 true,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001784 false);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001785
Chris Lattnera868bbb2011-04-08 18:02:51 +00001786 // printStr
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001787
Chris Lattnera868bbb2011-04-08 18:02:51 +00001788 retType = builder.getVoidTy();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001789
Chris Lattnera868bbb2011-04-08 18:02:51 +00001790 argTypes.clear();
Garrison Venn76310ac2011-07-12 15:34:42 +00001791 argTypes.push_back(builder.getInt8PtrTy());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001792
Chris Lattnera868bbb2011-04-08 18:02:51 +00001793 argNames.clear();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001794
1795 createFunction(module,
1796 retType,
1797 argTypes,
1798 argNames,
1799 "printStr",
1800 llvm::Function::ExternalLinkage,
1801 true,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001802 false);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001803
Chris Lattnera868bbb2011-04-08 18:02:51 +00001804 // throwCppException
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001805
Chris Lattnera868bbb2011-04-08 18:02:51 +00001806 retType = builder.getVoidTy();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001807
Chris Lattnera868bbb2011-04-08 18:02:51 +00001808 argTypes.clear();
Garrison Venn76310ac2011-07-12 15:34:42 +00001809 argTypes.push_back(builder.getInt32Ty());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001810
Chris Lattnera868bbb2011-04-08 18:02:51 +00001811 argNames.clear();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001812
1813 createFunction(module,
1814 retType,
1815 argTypes,
1816 argNames,
1817 "throwCppException",
1818 llvm::Function::ExternalLinkage,
1819 true,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001820 false);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001821
Chris Lattnera868bbb2011-04-08 18:02:51 +00001822 // deleteOurException
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001823
Chris Lattnera868bbb2011-04-08 18:02:51 +00001824 retType = builder.getVoidTy();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001825
Chris Lattnera868bbb2011-04-08 18:02:51 +00001826 argTypes.clear();
Garrison Venn76310ac2011-07-12 15:34:42 +00001827 argTypes.push_back(builder.getInt8PtrTy());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001828
Chris Lattnera868bbb2011-04-08 18:02:51 +00001829 argNames.clear();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001830
1831 createFunction(module,
1832 retType,
1833 argTypes,
1834 argNames,
1835 "deleteOurException",
1836 llvm::Function::ExternalLinkage,
1837 true,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001838 false);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001839
Chris Lattnera868bbb2011-04-08 18:02:51 +00001840 // createOurException
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001841
Garrison Venn76310ac2011-07-12 15:34:42 +00001842 retType = builder.getInt8PtrTy();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001843
Chris Lattnera868bbb2011-04-08 18:02:51 +00001844 argTypes.clear();
Garrison Venn76310ac2011-07-12 15:34:42 +00001845 argTypes.push_back(builder.getInt32Ty());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001846
Chris Lattnera868bbb2011-04-08 18:02:51 +00001847 argNames.clear();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001848
1849 createFunction(module,
1850 retType,
1851 argTypes,
1852 argNames,
1853 "createOurException",
1854 llvm::Function::ExternalLinkage,
1855 true,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001856 false);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001857
Chris Lattnera868bbb2011-04-08 18:02:51 +00001858 // _Unwind_RaiseException
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001859
Chris Lattnera868bbb2011-04-08 18:02:51 +00001860 retType = builder.getInt32Ty();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001861
Chris Lattnera868bbb2011-04-08 18:02:51 +00001862 argTypes.clear();
Garrison Venn76310ac2011-07-12 15:34:42 +00001863 argTypes.push_back(builder.getInt8PtrTy());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001864
Chris Lattnera868bbb2011-04-08 18:02:51 +00001865 argNames.clear();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001866
1867 funct = createFunction(module,
1868 retType,
1869 argTypes,
1870 argNames,
1871 "_Unwind_RaiseException",
1872 llvm::Function::ExternalLinkage,
1873 true,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001874 false);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001875
NAKAMURA Takumi8489be82012-10-12 14:11:48 +00001876 funct->setDoesNotReturn();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001877
Chris Lattnera868bbb2011-04-08 18:02:51 +00001878 // _Unwind_Resume
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001879
Chris Lattnera868bbb2011-04-08 18:02:51 +00001880 retType = builder.getInt32Ty();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001881
Chris Lattnera868bbb2011-04-08 18:02:51 +00001882 argTypes.clear();
Garrison Venn76310ac2011-07-12 15:34:42 +00001883 argTypes.push_back(builder.getInt8PtrTy());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001884
Chris Lattnera868bbb2011-04-08 18:02:51 +00001885 argNames.clear();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001886
1887 funct = createFunction(module,
1888 retType,
1889 argTypes,
1890 argNames,
1891 "_Unwind_Resume",
1892 llvm::Function::ExternalLinkage,
1893 true,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001894 false);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001895
NAKAMURA Takumi8489be82012-10-12 14:11:48 +00001896 funct->setDoesNotReturn();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001897
Chris Lattnera868bbb2011-04-08 18:02:51 +00001898 // ourPersonality
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001899
Chris Lattnera868bbb2011-04-08 18:02:51 +00001900 retType = builder.getInt32Ty();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001901
Chris Lattnera868bbb2011-04-08 18:02:51 +00001902 argTypes.clear();
Garrison Venn76310ac2011-07-12 15:34:42 +00001903 argTypes.push_back(builder.getInt32Ty());
1904 argTypes.push_back(builder.getInt32Ty());
1905 argTypes.push_back(builder.getInt64Ty());
1906 argTypes.push_back(builder.getInt8PtrTy());
1907 argTypes.push_back(builder.getInt8PtrTy());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001908
Chris Lattnera868bbb2011-04-08 18:02:51 +00001909 argNames.clear();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001910
1911 createFunction(module,
1912 retType,
1913 argTypes,
1914 argNames,
1915 "ourPersonality",
1916 llvm::Function::ExternalLinkage,
1917 true,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001918 false);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001919
Chris Lattnera868bbb2011-04-08 18:02:51 +00001920 // llvm.eh.typeid.for intrinsic
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001921
Chris Lattnera868bbb2011-04-08 18:02:51 +00001922 getDeclaration(&module, llvm::Intrinsic::eh_typeid_for);
Garrison Vennf4d2f842010-02-09 23:22:43 +00001923}
1924
1925
Chris Lattnera868bbb2011-04-08 18:02:51 +00001926//===----------------------------------------------------------------------===//
Garrison Vennf4d2f842010-02-09 23:22:43 +00001927// Main test driver code.
Chris Lattnera868bbb2011-04-08 18:02:51 +00001928//===----------------------------------------------------------------------===//
Garrison Vennf4d2f842010-02-09 23:22:43 +00001929
1930/// Demo main routine which takes the type info types to throw. A test will
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001931/// be run for each given type info type. While type info types with the value
Garrison Vennf4d2f842010-02-09 23:22:43 +00001932/// of -1 will trigger a foreign C++ exception to be thrown; type info types
1933/// <= 6 and >= 1 will be caught by test functions; and type info types > 6
1934/// will result in exceptions which pass through to the test harness. All other
1935/// type info types are not supported and could cause a crash.
Garrison Venn88bd9d62011-04-10 14:06:52 +00001936int main(int argc, char *argv[]) {
Chris Lattnera868bbb2011-04-08 18:02:51 +00001937 if (argc == 1) {
1938 fprintf(stderr,
1939 "\nUsage: ExceptionDemo <exception type to throw> "
1940 "[<type 2>...<type n>].\n"
1941 " Each type must have the value of 1 - 6 for "
1942 "generated exceptions to be caught;\n"
1943 " the value -1 for foreign C++ exceptions to be "
1944 "generated and thrown;\n"
1945 " or the values > 6 for exceptions to be ignored.\n"
1946 "\nTry: ExceptionDemo 2 3 7 -1\n"
1947 " for a full test.\n\n");
1948 return(0);
1949 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001950
Chris Lattnera868bbb2011-04-08 18:02:51 +00001951 // If not set, exception handling will not be turned on
Peter Collingbournedff24782011-12-07 23:58:57 +00001952 llvm::TargetOptions Opts;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001953
Chris Lattnera868bbb2011-04-08 18:02:51 +00001954 llvm::InitializeNativeTarget();
Rafael Espindolae93dc3b2013-05-05 20:57:58 +00001955 llvm::InitializeNativeTargetAsmPrinter();
Garrison Venn88bd9d62011-04-10 14:06:52 +00001956 llvm::LLVMContext &context = llvm::getGlobalContext();
Chris Lattnera868bbb2011-04-08 18:02:51 +00001957 llvm::IRBuilder<> theBuilder(context);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001958
Chris Lattnera868bbb2011-04-08 18:02:51 +00001959 // Make the module, which holds all the code.
Rafael Espindola2a8a2792014-08-19 04:04:25 +00001960 std::unique_ptr<llvm::Module> Owner =
1961 llvm::make_unique<llvm::Module>("my cool jit", context);
1962 llvm::Module *module = Owner.get();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001963
NAKAMURA Takumi4c71cc12014-12-03 02:05:51 +00001964 std::unique_ptr<llvm::RTDyldMemoryManager> MemMgr(new llvm::SectionMemoryManager());
Rafael Espindolae93dc3b2013-05-05 20:57:58 +00001965
Chris Lattnera868bbb2011-04-08 18:02:51 +00001966 // Build engine with JIT
Rafael Espindola2a8a2792014-08-19 04:04:25 +00001967 llvm::EngineBuilder factory(std::move(Owner));
Chris Lattnera868bbb2011-04-08 18:02:51 +00001968 factory.setEngineKind(llvm::EngineKind::JIT);
Peter Collingbournedff24782011-12-07 23:58:57 +00001969 factory.setTargetOptions(Opts);
NAKAMURA Takumi4c71cc12014-12-03 02:05:51 +00001970 factory.setMCJITMemoryManager(std::move(MemMgr));
Garrison Venn88bd9d62011-04-10 14:06:52 +00001971 llvm::ExecutionEngine *executionEngine = factory.create();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001972
Chris Lattnera868bbb2011-04-08 18:02:51 +00001973 {
1974 llvm::FunctionPassManager fpm(module);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001975
1976 // Set up the optimizer pipeline.
Chris Lattnera868bbb2011-04-08 18:02:51 +00001977 // Start with registering info about how the
1978 // target lays out data structures.
Rafael Espindola339430f2014-02-25 23:25:17 +00001979 module->setDataLayout(executionEngine->getDataLayout());
Rafael Espindolac435adc2014-09-10 21:27:43 +00001980 fpm.add(new llvm::DataLayoutPass());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001981
Chris Lattnera868bbb2011-04-08 18:02:51 +00001982 // Optimizations turned on
1983#ifdef ADD_OPT_PASSES
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001984
Chris Lattnera868bbb2011-04-08 18:02:51 +00001985 // Basic AliasAnslysis support for GVN.
1986 fpm.add(llvm::createBasicAliasAnalysisPass());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001987
Chris Lattnera868bbb2011-04-08 18:02:51 +00001988 // Promote allocas to registers.
1989 fpm.add(llvm::createPromoteMemoryToRegisterPass());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001990
Chris Lattnera868bbb2011-04-08 18:02:51 +00001991 // Do simple "peephole" optimizations and bit-twiddling optzns.
1992 fpm.add(llvm::createInstructionCombiningPass());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001993
Chris Lattnera868bbb2011-04-08 18:02:51 +00001994 // Reassociate expressions.
1995 fpm.add(llvm::createReassociatePass());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001996
Chris Lattnera868bbb2011-04-08 18:02:51 +00001997 // Eliminate Common SubExpressions.
1998 fpm.add(llvm::createGVNPass());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001999
2000 // Simplify the control flow graph (deleting unreachable
Chris Lattnera868bbb2011-04-08 18:02:51 +00002001 // blocks, etc).
2002 fpm.add(llvm::createCFGSimplificationPass());
2003#endif // ADD_OPT_PASSES
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00002004
Chris Lattnera868bbb2011-04-08 18:02:51 +00002005 fpm.doInitialization();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00002006
Chris Lattnera868bbb2011-04-08 18:02:51 +00002007 // Generate test code using function throwCppException(...) as
2008 // the function which throws foreign exceptions.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00002009 llvm::Function *toRun =
2010 createUnwindExceptionTest(*module,
2011 theBuilder,
Garrison Venn8cb00352011-09-22 15:45:14 +00002012 fpm,
2013 "throwCppException");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00002014
Rafael Espindolae93dc3b2013-05-05 20:57:58 +00002015 executionEngine->finalizeObject();
2016
Chris Lattnera868bbb2011-04-08 18:02:51 +00002017 fprintf(stderr, "\nBegin module dump:\n\n");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00002018
Chris Lattnera868bbb2011-04-08 18:02:51 +00002019 module->dump();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00002020
Chris Lattnera868bbb2011-04-08 18:02:51 +00002021 fprintf(stderr, "\nEnd module dump:\n");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00002022
Chris Lattnera868bbb2011-04-08 18:02:51 +00002023 fprintf(stderr, "\n\nBegin Test:\n");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00002024
Chris Lattnera868bbb2011-04-08 18:02:51 +00002025 for (int i = 1; i < argc; ++i) {
2026 // Run test for each argument whose value is the exception
2027 // type to throw.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00002028 runExceptionThrow(executionEngine,
2029 toRun,
Chris Lattnera868bbb2011-04-08 18:02:51 +00002030 (unsigned) strtoul(argv[i], NULL, 10));
2031 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00002032
Chris Lattnera868bbb2011-04-08 18:02:51 +00002033 fprintf(stderr, "\nEnd Test:\n\n");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00002034 }
2035
Chris Lattnera868bbb2011-04-08 18:02:51 +00002036 delete executionEngine;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00002037
Chris Lattnera868bbb2011-04-08 18:02:51 +00002038 return 0;
Garrison Vennf4d2f842010-02-09 23:22:43 +00002039}