blob: 75140967fa8050d761a685fb4939417037c1404a [file] [log] [blame]
Chris Lattnera868bbb2011-04-08 18:02:51 +00001//===-- ExceptionDemo.cpp - An example using llvm Exceptions --------------===//
Garrison Vennf4d2f842010-02-09 23:22:43 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
Chris Lattnera868bbb2011-04-08 18:02:51 +00008//===----------------------------------------------------------------------===//
Garrison Vennf4d2f842010-02-09 23:22:43 +00009//
10// Demo program which implements an example LLVM exception implementation, and
11// shows several test cases including the handling of foreign exceptions.
12// It is run with type info types arguments to throw. A test will
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +000013// be run for each given type info type. While type info types with the value
Garrison Vennf4d2f842010-02-09 23:22:43 +000014// of -1 will trigger a foreign C++ exception to be thrown; type info types
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +000015// <= 6 and >= 1 will cause the associated generated exceptions to be thrown
Garrison Vennf4d2f842010-02-09 23:22:43 +000016// and caught by generated test functions; and type info types > 6
17// will result in exceptions which pass through to the test harness. All other
18// type info types are not supported and could cause a crash. In all cases,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +000019// the "finally" blocks of every generated test functions will executed
Garrison Vennf4d2f842010-02-09 23:22:43 +000020// regardless of whether or not that test function ignores or catches the
21// thrown exception.
22//
23// examples:
24//
25// ExceptionDemo
26//
27// causes a usage to be printed to stderr
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +000028//
Garrison Vennf4d2f842010-02-09 23:22:43 +000029// ExceptionDemo 2 3 7 -1
30//
31// results in the following cases:
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +000032// - Value 2 causes an exception with a type info type of 2 to be
Garrison Vennf4d2f842010-02-09 23:22:43 +000033// thrown and caught by an inner generated test function.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +000034// - Value 3 causes an exception with a type info type of 3 to be
Garrison Vennf4d2f842010-02-09 23:22:43 +000035// thrown and caught by an outer generated test function.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +000036// - Value 7 causes an exception with a type info type of 7 to be
Garrison Vennf4d2f842010-02-09 23:22:43 +000037// thrown and NOT be caught by any generated function.
38// - Value -1 causes a foreign C++ exception to be thrown and not be
39// caught by any generated function
40//
41// Cases -1 and 7 are caught by a C++ test harness where the validity of
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +000042// of a C++ catch(...) clause catching a generated exception with a
43// type info type of 7 is explained by: example in rules 1.6.4 in
Dmitri Gribenko462462e2013-01-13 15:53:09 +000044// http://mentorembedded.github.com/cxx-abi/abi-eh.html (v1.22)
Garrison Vennf4d2f842010-02-09 23:22:43 +000045//
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +000046// This code uses code from the llvm compiler-rt project and the llvm
Garrison Vennf4d2f842010-02-09 23:22:43 +000047// Kaleidoscope project.
48//
Chris Lattnera868bbb2011-04-08 18:02:51 +000049//===----------------------------------------------------------------------===//
Garrison Vennf4d2f842010-02-09 23:22:43 +000050
NAKAMURA Takumi85c9bac2015-03-02 01:04:34 +000051#include "llvm/ADT/STLExtras.h"
Chandler Carruthd7cd9ac92014-01-13 09:53:45 +000052#include "llvm/IR/Verifier.h"
Rafael Espindolae93dc3b2013-05-05 20:57:58 +000053#include "llvm/ExecutionEngine/MCJIT.h"
54#include "llvm/ExecutionEngine/SectionMemoryManager.h"
Chandler Carruth005f27a2013-01-02 11:56:33 +000055#include "llvm/IR/DataLayout.h"
56#include "llvm/IR/DerivedTypes.h"
57#include "llvm/IR/IRBuilder.h"
58#include "llvm/IR/Intrinsics.h"
59#include "llvm/IR/LLVMContext.h"
Chandler Carruth30d69c22015-02-13 10:01:29 +000060#include "llvm/IR/LegacyPassManager.h"
Chandler Carruth005f27a2013-01-02 11:56:33 +000061#include "llvm/IR/Module.h"
Garrison Vennf4d2f842010-02-09 23:22:43 +000062#include "llvm/Support/Dwarf.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000063#include "llvm/Support/TargetSelect.h"
Chandler Carruth605e30e2012-12-04 10:16:57 +000064#include "llvm/Target/TargetOptions.h"
65#include "llvm/Transforms/Scalar.h"
Garrison Vennf4d2f842010-02-09 23:22:43 +000066
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +000067// FIXME: Although all systems tested with (Linux, OS X), do not need this
68// header file included. A user on ubuntu reported, undefined symbols
Garrison Venna0f6ecb2011-04-12 12:30:10 +000069// for stderr, and fprintf, and the addition of this include fixed the
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +000070// issue for them. Given that LLVM's best practices include the goal
71// of reducing the number of redundant header files included, the
72// correct solution would be to find out why these symbols are not
Garrison Venna0f6ecb2011-04-12 12:30:10 +000073// defined for the system in question, and fix the issue by finding out
74// which LLVM header file, if any, would include these symbols.
Garrison Venn56c5ca22011-04-11 19:52:49 +000075#include <cstdio>
Garrison Venna0f6ecb2011-04-12 12:30:10 +000076
Garrison Vennf4d2f842010-02-09 23:22:43 +000077#include <sstream>
Garrison Vennf4d2f842010-02-09 23:22:43 +000078#include <stdexcept>
79
80
81#ifndef USE_GLOBAL_STR_CONSTS
82#define USE_GLOBAL_STR_CONSTS true
83#endif
84
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +000085// System C++ ABI unwind types from:
Dmitri Gribenko462462e2013-01-13 15:53:09 +000086// http://mentorembedded.github.com/cxx-abi/abi-eh.html (v1.22)
Garrison Vennf4d2f842010-02-09 23:22:43 +000087
88extern "C" {
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +000089
Chris Lattnera868bbb2011-04-08 18:02:51 +000090 typedef enum {
Garrison Vennf4d2f842010-02-09 23:22:43 +000091 _URC_NO_REASON = 0,
92 _URC_FOREIGN_EXCEPTION_CAUGHT = 1,
93 _URC_FATAL_PHASE2_ERROR = 2,
94 _URC_FATAL_PHASE1_ERROR = 3,
95 _URC_NORMAL_STOP = 4,
96 _URC_END_OF_STACK = 5,
97 _URC_HANDLER_FOUND = 6,
98 _URC_INSTALL_CONTEXT = 7,
99 _URC_CONTINUE_UNWIND = 8
Chris Lattnera868bbb2011-04-08 18:02:51 +0000100 } _Unwind_Reason_Code;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000101
Chris Lattnera868bbb2011-04-08 18:02:51 +0000102 typedef enum {
Garrison Vennf4d2f842010-02-09 23:22:43 +0000103 _UA_SEARCH_PHASE = 1,
104 _UA_CLEANUP_PHASE = 2,
105 _UA_HANDLER_FRAME = 4,
106 _UA_FORCE_UNWIND = 8,
107 _UA_END_OF_STACK = 16
Chris Lattnera868bbb2011-04-08 18:02:51 +0000108 } _Unwind_Action;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000109
Chris Lattnera868bbb2011-04-08 18:02:51 +0000110 struct _Unwind_Exception;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000111
Chris Lattnera868bbb2011-04-08 18:02:51 +0000112 typedef void (*_Unwind_Exception_Cleanup_Fn) (_Unwind_Reason_Code,
113 struct _Unwind_Exception *);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000114
Chris Lattnera868bbb2011-04-08 18:02:51 +0000115 struct _Unwind_Exception {
Garrison Vennf4d2f842010-02-09 23:22:43 +0000116 uint64_t exception_class;
117 _Unwind_Exception_Cleanup_Fn exception_cleanup;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000118
119 uintptr_t private_1;
120 uintptr_t private_2;
121
Garrison Vennf4d2f842010-02-09 23:22:43 +0000122 // @@@ The IA-64 ABI says that this structure must be double-word aligned.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000123 // Taking that literally does not make much sense generically. Instead
Garrison Vennf4d2f842010-02-09 23:22:43 +0000124 // we provide the maximum alignment required by any type for the machine.
Chris Lattnera868bbb2011-04-08 18:02:51 +0000125 } __attribute__((__aligned__));
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000126
Chris Lattnera868bbb2011-04-08 18:02:51 +0000127 struct _Unwind_Context;
Garrison Venn88bd9d62011-04-10 14:06:52 +0000128 typedef struct _Unwind_Context *_Unwind_Context_t;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000129
Garrison Venn88bd9d62011-04-10 14:06:52 +0000130 extern const uint8_t *_Unwind_GetLanguageSpecificData (_Unwind_Context_t c);
Chris Lattnera868bbb2011-04-08 18:02:51 +0000131 extern uintptr_t _Unwind_GetGR (_Unwind_Context_t c, int i);
132 extern void _Unwind_SetGR (_Unwind_Context_t c, int i, uintptr_t n);
133 extern void _Unwind_SetIP (_Unwind_Context_t, uintptr_t new_value);
134 extern uintptr_t _Unwind_GetIP (_Unwind_Context_t context);
135 extern uintptr_t _Unwind_GetRegionStart (_Unwind_Context_t context);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000136
Garrison Vennf4d2f842010-02-09 23:22:43 +0000137} // extern "C"
138
139//
140// Example types
141//
142
143/// This is our simplistic type info
144struct OurExceptionType_t {
Chris Lattnera868bbb2011-04-08 18:02:51 +0000145 /// type info type
146 int type;
Garrison Vennf4d2f842010-02-09 23:22:43 +0000147};
148
149
150/// This is our Exception class which relies on a negative offset to calculate
151/// pointers to its instances from pointers to its unwindException member.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000152///
Garrison Vennf4d2f842010-02-09 23:22:43 +0000153/// Note: The above unwind.h defines struct _Unwind_Exception to be aligned
154/// on a double word boundary. This is necessary to match the standard:
Dmitri Gribenko462462e2013-01-13 15:53:09 +0000155/// http://mentorembedded.github.com/cxx-abi/abi-eh.html
Garrison Vennf4d2f842010-02-09 23:22:43 +0000156struct OurBaseException_t {
Chris Lattnera868bbb2011-04-08 18:02:51 +0000157 struct OurExceptionType_t type;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000158
Chris Lattnera868bbb2011-04-08 18:02:51 +0000159 // Note: This is properly aligned in unwind.h
160 struct _Unwind_Exception unwindException;
Garrison Vennf4d2f842010-02-09 23:22:43 +0000161};
162
163
164// Note: Not needed since we are C++
165typedef struct OurBaseException_t OurException;
166typedef struct _Unwind_Exception OurUnwindException;
167
168//
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000169// Various globals used to support typeinfo and generatted exceptions in
Garrison Vennf4d2f842010-02-09 23:22:43 +0000170// general
171//
172
173static std::map<std::string, llvm::Value*> namedValues;
174
175int64_t ourBaseFromUnwindOffset;
176
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000177const unsigned char ourBaseExcpClassChars[] =
Chris Lattnera868bbb2011-04-08 18:02:51 +0000178{'o', 'b', 'j', '\0', 'b', 'a', 's', '\0'};
Garrison Vennf4d2f842010-02-09 23:22:43 +0000179
180
181static uint64_t ourBaseExceptionClass = 0;
182
183static std::vector<std::string> ourTypeInfoNames;
184static std::map<int, std::string> ourTypeInfoNamesIndex;
185
Garrison Venn88bd9d62011-04-10 14:06:52 +0000186static llvm::StructType *ourTypeInfoType;
Garrison Venn8cb00352011-09-22 15:45:14 +0000187static llvm::StructType *ourCaughtResultType;
Garrison Venn88bd9d62011-04-10 14:06:52 +0000188static llvm::StructType *ourExceptionType;
189static llvm::StructType *ourUnwindExceptionType;
Garrison Vennf4d2f842010-02-09 23:22:43 +0000190
Garrison Venn88bd9d62011-04-10 14:06:52 +0000191static llvm::ConstantInt *ourExceptionNotThrownState;
192static llvm::ConstantInt *ourExceptionThrownState;
193static llvm::ConstantInt *ourExceptionCaughtState;
Garrison Vennf4d2f842010-02-09 23:22:43 +0000194
195typedef std::vector<std::string> ArgNames;
Garrison Venn5fb3f662011-07-11 16:31:53 +0000196typedef std::vector<llvm::Type*> ArgTypes;
Garrison Vennf4d2f842010-02-09 23:22:43 +0000197
198//
199// Code Generation Utilities
200//
201
202/// Utility used to create a function, both declarations and definitions
203/// @param module for module instance
204/// @param retType function return type
205/// @param theArgTypes function's ordered argument types
206/// @param theArgNames function's ordered arguments needed if use of this
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000207/// function corresponds to a function definition. Use empty
Garrison Vennf4d2f842010-02-09 23:22:43 +0000208/// aggregate for function declarations.
209/// @param functName function name
210/// @param linkage function linkage
211/// @param declarationOnly for function declarations
212/// @param isVarArg function uses vararg arguments
213/// @returns function instance
Garrison Venn88bd9d62011-04-10 14:06:52 +0000214llvm::Function *createFunction(llvm::Module &module,
Chris Lattner6b8eb8c2011-07-18 04:52:09 +0000215 llvm::Type *retType,
Garrison Venn88bd9d62011-04-10 14:06:52 +0000216 const ArgTypes &theArgTypes,
217 const ArgNames &theArgNames,
218 const std::string &functName,
Garrison Vennf4d2f842010-02-09 23:22:43 +0000219 llvm::GlobalValue::LinkageTypes linkage,
220 bool declarationOnly,
221 bool isVarArg) {
Chris Lattnera868bbb2011-04-08 18:02:51 +0000222 llvm::FunctionType *functType =
223 llvm::FunctionType::get(retType, theArgTypes, isVarArg);
224 llvm::Function *ret =
225 llvm::Function::Create(functType, linkage, functName, &module);
226 if (!ret || declarationOnly)
Garrison Vennf4d2f842010-02-09 23:22:43 +0000227 return(ret);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000228
Chris Lattnera868bbb2011-04-08 18:02:51 +0000229 namedValues.clear();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000230 unsigned i = 0;
Chris Lattnera868bbb2011-04-08 18:02:51 +0000231 for (llvm::Function::arg_iterator argIndex = ret->arg_begin();
232 i != theArgNames.size();
233 ++argIndex, ++i) {
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000234
Chris Lattnera868bbb2011-04-08 18:02:51 +0000235 argIndex->setName(theArgNames[i]);
236 namedValues[theArgNames[i]] = argIndex;
237 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000238
Chris Lattnera868bbb2011-04-08 18:02:51 +0000239 return(ret);
Garrison Vennf4d2f842010-02-09 23:22:43 +0000240}
241
242
243/// Create an alloca instruction in the entry block of
244/// the parent function. This is used for mutable variables etc.
245/// @param function parent instance
246/// @param varName stack variable name
247/// @param type stack variable type
248/// @param initWith optional constant initialization value
249/// @returns AllocaInst instance
Garrison Venn88bd9d62011-04-10 14:06:52 +0000250static llvm::AllocaInst *createEntryBlockAlloca(llvm::Function &function,
Chris Lattnera868bbb2011-04-08 18:02:51 +0000251 const std::string &varName,
Chris Lattner6b8eb8c2011-07-18 04:52:09 +0000252 llvm::Type *type,
Garrison Venn88bd9d62011-04-10 14:06:52 +0000253 llvm::Constant *initWith = 0) {
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000254 llvm::BasicBlock &block = function.getEntryBlock();
Chris Lattnera868bbb2011-04-08 18:02:51 +0000255 llvm::IRBuilder<> tmp(&block, block.begin());
Garrison Venn88bd9d62011-04-10 14:06:52 +0000256 llvm::AllocaInst *ret = tmp.CreateAlloca(type, 0, varName.c_str());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000257
258 if (initWith)
Chris Lattnera868bbb2011-04-08 18:02:51 +0000259 tmp.CreateStore(initWith, ret);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000260
Chris Lattnera868bbb2011-04-08 18:02:51 +0000261 return(ret);
Garrison Vennf4d2f842010-02-09 23:22:43 +0000262}
263
264
265//
266// Code Generation Utilities End
267//
268
269//
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000270// Runtime C Library functions
Garrison Vennf4d2f842010-02-09 23:22:43 +0000271//
272
273// Note: using an extern "C" block so that static functions can be used
274extern "C" {
275
276// Note: Better ways to decide on bit width
277//
278/// Prints a 32 bit number, according to the format, to stderr.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000279/// @param intToPrint integer to print
Garrison Vennf4d2f842010-02-09 23:22:43 +0000280/// @param format printf like format to use when printing
Garrison Venn88bd9d62011-04-10 14:06:52 +0000281void print32Int(int intToPrint, const char *format) {
Chris Lattnera868bbb2011-04-08 18:02:51 +0000282 if (format) {
283 // Note: No NULL check
284 fprintf(stderr, format, intToPrint);
285 }
286 else {
287 // Note: No NULL check
288 fprintf(stderr, "::print32Int(...):NULL arg.\n");
289 }
Garrison Vennf4d2f842010-02-09 23:22:43 +0000290}
291
292
293// Note: Better ways to decide on bit width
294//
295/// Prints a 64 bit number, according to the format, to stderr.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000296/// @param intToPrint integer to print
Garrison Vennf4d2f842010-02-09 23:22:43 +0000297/// @param format printf like format to use when printing
Garrison Venn88bd9d62011-04-10 14:06:52 +0000298void print64Int(long int intToPrint, const char *format) {
Chris Lattnera868bbb2011-04-08 18:02:51 +0000299 if (format) {
300 // Note: No NULL check
301 fprintf(stderr, format, intToPrint);
302 }
303 else {
304 // Note: No NULL check
305 fprintf(stderr, "::print64Int(...):NULL arg.\n");
306 }
Garrison Vennf4d2f842010-02-09 23:22:43 +0000307}
308
309
310/// Prints a C string to stderr
311/// @param toPrint string to print
Garrison Venn88bd9d62011-04-10 14:06:52 +0000312void printStr(char *toPrint) {
Chris Lattnera868bbb2011-04-08 18:02:51 +0000313 if (toPrint) {
314 fprintf(stderr, "%s", toPrint);
315 }
316 else {
317 fprintf(stderr, "::printStr(...):NULL arg.\n");
318 }
Garrison Vennf4d2f842010-02-09 23:22:43 +0000319}
320
321
322/// Deletes the true previosly allocated exception whose address
323/// is calculated from the supplied OurBaseException_t::unwindException
324/// member address. Handles (ignores), NULL pointers.
325/// @param expToDelete exception to delete
Garrison Venn88bd9d62011-04-10 14:06:52 +0000326void deleteOurException(OurUnwindException *expToDelete) {
Garrison Vennf4d2f842010-02-09 23:22:43 +0000327#ifdef DEBUG
Chris Lattnera868bbb2011-04-08 18:02:51 +0000328 fprintf(stderr,
329 "deleteOurException(...).\n");
Garrison Vennf4d2f842010-02-09 23:22:43 +0000330#endif
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000331
Chris Lattnera868bbb2011-04-08 18:02:51 +0000332 if (expToDelete &&
333 (expToDelete->exception_class == ourBaseExceptionClass)) {
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000334
Chris Lattnera868bbb2011-04-08 18:02:51 +0000335 free(((char*) expToDelete) + ourBaseFromUnwindOffset);
336 }
Garrison Vennf4d2f842010-02-09 23:22:43 +0000337}
338
339
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000340/// This function is the struct _Unwind_Exception API mandated delete function
341/// used by foreign exception handlers when deleting our exception
Garrison Vennf4d2f842010-02-09 23:22:43 +0000342/// (OurException), instances.
NAKAMURA Takumi1373b742013-07-29 11:03:50 +0000343/// @param reason See @link http://mentorembedded.github.com/cxx-abi/abi-eh.html
Garrison Vennf4d2f842010-02-09 23:22:43 +0000344/// @unlink
345/// @param expToDelete exception instance to delete
346void deleteFromUnwindOurException(_Unwind_Reason_Code reason,
Garrison Venn88bd9d62011-04-10 14:06:52 +0000347 OurUnwindException *expToDelete) {
Garrison Vennf4d2f842010-02-09 23:22:43 +0000348#ifdef DEBUG
Chris Lattnera868bbb2011-04-08 18:02:51 +0000349 fprintf(stderr,
350 "deleteFromUnwindOurException(...).\n");
Garrison Vennf4d2f842010-02-09 23:22:43 +0000351#endif
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000352
Chris Lattnera868bbb2011-04-08 18:02:51 +0000353 deleteOurException(expToDelete);
Garrison Vennf4d2f842010-02-09 23:22:43 +0000354}
355
356
357/// Creates (allocates on the heap), an exception (OurException instance),
358/// of the supplied type info type.
359/// @param type type info type
Garrison Venn88bd9d62011-04-10 14:06:52 +0000360OurUnwindException *createOurException(int type) {
Chris Lattnera868bbb2011-04-08 18:02:51 +0000361 size_t size = sizeof(OurException);
Garrison Venn88bd9d62011-04-10 14:06:52 +0000362 OurException *ret = (OurException*) memset(malloc(size), 0, size);
Chris Lattnera868bbb2011-04-08 18:02:51 +0000363 (ret->type).type = type;
364 (ret->unwindException).exception_class = ourBaseExceptionClass;
365 (ret->unwindException).exception_cleanup = deleteFromUnwindOurException;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000366
Chris Lattnera868bbb2011-04-08 18:02:51 +0000367 return(&(ret->unwindException));
Garrison Vennf4d2f842010-02-09 23:22:43 +0000368}
369
370
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000371/// Read a uleb128 encoded value and advance pointer
372/// See Variable Length Data in:
Garrison Vennf4d2f842010-02-09 23:22:43 +0000373/// @link http://dwarfstd.org/Dwarf3.pdf @unlink
374/// @param data reference variable holding memory pointer to decode from
375/// @returns decoded value
Garrison Venn88bd9d62011-04-10 14:06:52 +0000376static uintptr_t readULEB128(const uint8_t **data) {
Chris Lattnera868bbb2011-04-08 18:02:51 +0000377 uintptr_t result = 0;
378 uintptr_t shift = 0;
379 unsigned char byte;
Garrison Venn88bd9d62011-04-10 14:06:52 +0000380 const uint8_t *p = *data;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000381
Chris Lattnera868bbb2011-04-08 18:02:51 +0000382 do {
383 byte = *p++;
384 result |= (byte & 0x7f) << shift;
385 shift += 7;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000386 }
Chris Lattnera868bbb2011-04-08 18:02:51 +0000387 while (byte & 0x80);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000388
Chris Lattnera868bbb2011-04-08 18:02:51 +0000389 *data = p;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000390
Chris Lattnera868bbb2011-04-08 18:02:51 +0000391 return result;
Garrison Vennf4d2f842010-02-09 23:22:43 +0000392}
393
394
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000395/// Read a sleb128 encoded value and advance pointer
396/// See Variable Length Data in:
Garrison Vennf4d2f842010-02-09 23:22:43 +0000397/// @link http://dwarfstd.org/Dwarf3.pdf @unlink
398/// @param data reference variable holding memory pointer to decode from
399/// @returns decoded value
Garrison Venn88bd9d62011-04-10 14:06:52 +0000400static uintptr_t readSLEB128(const uint8_t **data) {
Chris Lattnera868bbb2011-04-08 18:02:51 +0000401 uintptr_t result = 0;
402 uintptr_t shift = 0;
403 unsigned char byte;
Garrison Venn88bd9d62011-04-10 14:06:52 +0000404 const uint8_t *p = *data;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000405
Chris Lattnera868bbb2011-04-08 18:02:51 +0000406 do {
407 byte = *p++;
408 result |= (byte & 0x7f) << shift;
409 shift += 7;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000410 }
Chris Lattnera868bbb2011-04-08 18:02:51 +0000411 while (byte & 0x80);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000412
Chris Lattnera868bbb2011-04-08 18:02:51 +0000413 *data = p;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000414
Chris Lattnera868bbb2011-04-08 18:02:51 +0000415 if ((byte & 0x40) && (shift < (sizeof(result) << 3))) {
416 result |= (~0 << shift);
417 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000418
Chris Lattnera868bbb2011-04-08 18:02:51 +0000419 return result;
Garrison Vennf4d2f842010-02-09 23:22:43 +0000420}
421
Rafael Espindola5096adf2013-05-01 21:05:05 +0000422unsigned getEncodingSize(uint8_t Encoding) {
423 if (Encoding == llvm::dwarf::DW_EH_PE_omit)
424 return 0;
425
426 switch (Encoding & 0x0F) {
427 case llvm::dwarf::DW_EH_PE_absptr:
428 return sizeof(uintptr_t);
429 case llvm::dwarf::DW_EH_PE_udata2:
430 return sizeof(uint16_t);
431 case llvm::dwarf::DW_EH_PE_udata4:
432 return sizeof(uint32_t);
433 case llvm::dwarf::DW_EH_PE_udata8:
434 return sizeof(uint64_t);
435 case llvm::dwarf::DW_EH_PE_sdata2:
436 return sizeof(int16_t);
437 case llvm::dwarf::DW_EH_PE_sdata4:
438 return sizeof(int32_t);
439 case llvm::dwarf::DW_EH_PE_sdata8:
440 return sizeof(int64_t);
441 default:
442 // not supported
443 abort();
444 }
445}
Garrison Vennf4d2f842010-02-09 23:22:43 +0000446
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000447/// Read a pointer encoded value and advance pointer
448/// See Variable Length Data in:
Garrison Vennf4d2f842010-02-09 23:22:43 +0000449/// @link http://dwarfstd.org/Dwarf3.pdf @unlink
450/// @param data reference variable holding memory pointer to decode from
451/// @param encoding dwarf encoding type
452/// @returns decoded value
Garrison Venn88bd9d62011-04-10 14:06:52 +0000453static uintptr_t readEncodedPointer(const uint8_t **data, uint8_t encoding) {
Chris Lattnera868bbb2011-04-08 18:02:51 +0000454 uintptr_t result = 0;
Garrison Venn88bd9d62011-04-10 14:06:52 +0000455 const uint8_t *p = *data;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000456
457 if (encoding == llvm::dwarf::DW_EH_PE_omit)
Chris Lattnera868bbb2011-04-08 18:02:51 +0000458 return(result);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000459
460 // first get value
Chris Lattnera868bbb2011-04-08 18:02:51 +0000461 switch (encoding & 0x0F) {
462 case llvm::dwarf::DW_EH_PE_absptr:
463 result = *((uintptr_t*)p);
464 p += sizeof(uintptr_t);
465 break;
466 case llvm::dwarf::DW_EH_PE_uleb128:
467 result = readULEB128(&p);
468 break;
469 // Note: This case has not been tested
470 case llvm::dwarf::DW_EH_PE_sleb128:
471 result = readSLEB128(&p);
472 break;
473 case llvm::dwarf::DW_EH_PE_udata2:
474 result = *((uint16_t*)p);
475 p += sizeof(uint16_t);
476 break;
477 case llvm::dwarf::DW_EH_PE_udata4:
478 result = *((uint32_t*)p);
479 p += sizeof(uint32_t);
480 break;
481 case llvm::dwarf::DW_EH_PE_udata8:
482 result = *((uint64_t*)p);
483 p += sizeof(uint64_t);
484 break;
485 case llvm::dwarf::DW_EH_PE_sdata2:
486 result = *((int16_t*)p);
487 p += sizeof(int16_t);
488 break;
489 case llvm::dwarf::DW_EH_PE_sdata4:
490 result = *((int32_t*)p);
491 p += sizeof(int32_t);
492 break;
493 case llvm::dwarf::DW_EH_PE_sdata8:
494 result = *((int64_t*)p);
495 p += sizeof(int64_t);
496 break;
497 default:
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000498 // not supported
Chris Lattnera868bbb2011-04-08 18:02:51 +0000499 abort();
500 break;
501 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000502
503 // then add relative offset
Chris Lattnera868bbb2011-04-08 18:02:51 +0000504 switch (encoding & 0x70) {
505 case llvm::dwarf::DW_EH_PE_absptr:
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000506 // do nothing
Chris Lattnera868bbb2011-04-08 18:02:51 +0000507 break;
508 case llvm::dwarf::DW_EH_PE_pcrel:
509 result += (uintptr_t)(*data);
510 break;
511 case llvm::dwarf::DW_EH_PE_textrel:
512 case llvm::dwarf::DW_EH_PE_datarel:
513 case llvm::dwarf::DW_EH_PE_funcrel:
514 case llvm::dwarf::DW_EH_PE_aligned:
515 default:
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000516 // not supported
Chris Lattnera868bbb2011-04-08 18:02:51 +0000517 abort();
518 break;
519 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000520
521 // then apply indirection
Chris Lattnera868bbb2011-04-08 18:02:51 +0000522 if (encoding & llvm::dwarf::DW_EH_PE_indirect) {
523 result = *((uintptr_t*)result);
524 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000525
Chris Lattnera868bbb2011-04-08 18:02:51 +0000526 *data = p;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000527
Chris Lattnera868bbb2011-04-08 18:02:51 +0000528 return result;
Garrison Vennf4d2f842010-02-09 23:22:43 +0000529}
530
531
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000532/// Deals with Dwarf actions matching our type infos
533/// (OurExceptionType_t instances). Returns whether or not a dwarf emitted
534/// action matches the supplied exception type. If such a match succeeds,
535/// the resultAction argument will be set with > 0 index value. Only
536/// corresponding llvm.eh.selector type info arguments, cleanup arguments
Garrison Vennf4d2f842010-02-09 23:22:43 +0000537/// are supported. Filters are not supported.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000538/// See Variable Length Data in:
Garrison Vennf4d2f842010-02-09 23:22:43 +0000539/// @link http://dwarfstd.org/Dwarf3.pdf @unlink
Dmitri Gribenko462462e2013-01-13 15:53:09 +0000540/// Also see @link http://mentorembedded.github.com/cxx-abi/abi-eh.html @unlink
Garrison Vennf4d2f842010-02-09 23:22:43 +0000541/// @param resultAction reference variable which will be set with result
542/// @param classInfo our array of type info pointers (to globals)
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000543/// @param actionEntry index into above type info array or 0 (clean up).
Garrison Vennf4d2f842010-02-09 23:22:43 +0000544/// We do not support filters.
545/// @param exceptionClass exception class (_Unwind_Exception::exception_class)
546/// of thrown exception.
547/// @param exceptionObject thrown _Unwind_Exception instance.
548/// @returns whether or not a type info was found. False is returned if only
549/// a cleanup was found
550static bool handleActionValue(int64_t *resultAction,
Rafael Espindola5096adf2013-05-01 21:05:05 +0000551 uint8_t TTypeEncoding,
552 const uint8_t *ClassInfo,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000553 uintptr_t actionEntry,
554 uint64_t exceptionClass,
Garrison Vennf4d2f842010-02-09 23:22:43 +0000555 struct _Unwind_Exception *exceptionObject) {
Chris Lattnera868bbb2011-04-08 18:02:51 +0000556 bool ret = false;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000557
558 if (!resultAction ||
559 !exceptionObject ||
Chris Lattnera868bbb2011-04-08 18:02:51 +0000560 (exceptionClass != ourBaseExceptionClass))
561 return(ret);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000562
Garrison Venn88bd9d62011-04-10 14:06:52 +0000563 struct OurBaseException_t *excp = (struct OurBaseException_t*)
Chris Lattnera868bbb2011-04-08 18:02:51 +0000564 (((char*) exceptionObject) + ourBaseFromUnwindOffset);
565 struct OurExceptionType_t *excpType = &(excp->type);
566 int type = excpType->type;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000567
Chris Lattnera868bbb2011-04-08 18:02:51 +0000568#ifdef DEBUG
569 fprintf(stderr,
570 "handleActionValue(...): exceptionObject = <%p>, "
571 "excp = <%p>.\n",
572 exceptionObject,
573 excp);
574#endif
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000575
Chris Lattnera868bbb2011-04-08 18:02:51 +0000576 const uint8_t *actionPos = (uint8_t*) actionEntry,
577 *tempActionPos;
578 int64_t typeOffset = 0,
579 actionOffset;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000580
Chris Lattnera868bbb2011-04-08 18:02:51 +0000581 for (int i = 0; true; ++i) {
582 // Each emitted dwarf action corresponds to a 2 tuple of
583 // type info address offset, and action offset to the next
584 // emitted action.
585 typeOffset = readSLEB128(&actionPos);
586 tempActionPos = actionPos;
587 actionOffset = readSLEB128(&tempActionPos);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000588
Garrison Vennf4d2f842010-02-09 23:22:43 +0000589#ifdef DEBUG
590 fprintf(stderr,
Chris Lattnera868bbb2011-04-08 18:02:51 +0000591 "handleActionValue(...):typeOffset: <%lld>, "
592 "actionOffset: <%lld>.\n",
593 typeOffset,
594 actionOffset);
Garrison Vennf4d2f842010-02-09 23:22:43 +0000595#endif
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000596 assert((typeOffset >= 0) &&
Chris Lattnera868bbb2011-04-08 18:02:51 +0000597 "handleActionValue(...):filters are not supported.");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000598
Chris Lattnera868bbb2011-04-08 18:02:51 +0000599 // Note: A typeOffset == 0 implies that a cleanup llvm.eh.selector
600 // argument has been matched.
Rafael Espindola5096adf2013-05-01 21:05:05 +0000601 if (typeOffset > 0) {
Garrison Vennf4d2f842010-02-09 23:22:43 +0000602#ifdef DEBUG
Chris Lattnera868bbb2011-04-08 18:02:51 +0000603 fprintf(stderr,
604 "handleActionValue(...):actionValue <%d> found.\n",
605 i);
Garrison Vennf4d2f842010-02-09 23:22:43 +0000606#endif
Rafael Espindola5096adf2013-05-01 21:05:05 +0000607 unsigned EncSize = getEncodingSize(TTypeEncoding);
608 const uint8_t *EntryP = ClassInfo - typeOffset * EncSize;
609 uintptr_t P = readEncodedPointer(&EntryP, TTypeEncoding);
610 struct OurExceptionType_t *ThisClassInfo =
611 reinterpret_cast<struct OurExceptionType_t *>(P);
612 if (ThisClassInfo->type == type) {
613 *resultAction = i + 1;
614 ret = true;
615 break;
616 }
Garrison Vennf4d2f842010-02-09 23:22:43 +0000617 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000618
Chris Lattnera868bbb2011-04-08 18:02:51 +0000619#ifdef DEBUG
620 fprintf(stderr,
621 "handleActionValue(...):actionValue not found.\n");
622#endif
623 if (!actionOffset)
624 break;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000625
Chris Lattnera868bbb2011-04-08 18:02:51 +0000626 actionPos += actionOffset;
627 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000628
Chris Lattnera868bbb2011-04-08 18:02:51 +0000629 return(ret);
Garrison Vennf4d2f842010-02-09 23:22:43 +0000630}
631
632
633/// Deals with the Language specific data portion of the emitted dwarf code.
Dmitri Gribenko462462e2013-01-13 15:53:09 +0000634/// See @link http://mentorembedded.github.com/cxx-abi/abi-eh.html @unlink
Garrison Vennf4d2f842010-02-09 23:22:43 +0000635/// @param version unsupported (ignored), unwind version
636/// @param lsda language specific data area
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000637/// @param _Unwind_Action actions minimally supported unwind stage
Garrison Vennf4d2f842010-02-09 23:22:43 +0000638/// (forced specifically not supported)
639/// @param exceptionClass exception class (_Unwind_Exception::exception_class)
640/// of thrown exception.
641/// @param exceptionObject thrown _Unwind_Exception instance.
642/// @param context unwind system context
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000643/// @returns minimally supported unwinding control indicator
644static _Unwind_Reason_Code handleLsda(int version,
Garrison Venn88bd9d62011-04-10 14:06:52 +0000645 const uint8_t *lsda,
Chris Lattnera868bbb2011-04-08 18:02:51 +0000646 _Unwind_Action actions,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000647 uint64_t exceptionClass,
Garrison Venn88bd9d62011-04-10 14:06:52 +0000648 struct _Unwind_Exception *exceptionObject,
Chris Lattnera868bbb2011-04-08 18:02:51 +0000649 _Unwind_Context_t context) {
650 _Unwind_Reason_Code ret = _URC_CONTINUE_UNWIND;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000651
Chris Lattnera868bbb2011-04-08 18:02:51 +0000652 if (!lsda)
653 return(ret);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000654
Garrison Vennf4d2f842010-02-09 23:22:43 +0000655#ifdef DEBUG
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000656 fprintf(stderr,
Chris Lattnera868bbb2011-04-08 18:02:51 +0000657 "handleLsda(...):lsda is non-zero.\n");
Garrison Vennf4d2f842010-02-09 23:22:43 +0000658#endif
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000659
Chris Lattnera868bbb2011-04-08 18:02:51 +0000660 // Get the current instruction pointer and offset it before next
661 // instruction in the current frame which threw the exception.
662 uintptr_t pc = _Unwind_GetIP(context)-1;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000663
664 // Get beginning current frame's code (as defined by the
Chris Lattnera868bbb2011-04-08 18:02:51 +0000665 // emitted dwarf code)
666 uintptr_t funcStart = _Unwind_GetRegionStart(context);
667 uintptr_t pcOffset = pc - funcStart;
Rafael Espindola5096adf2013-05-01 21:05:05 +0000668 const uint8_t *ClassInfo = NULL;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000669
Chris Lattnera868bbb2011-04-08 18:02:51 +0000670 // Note: See JITDwarfEmitter::EmitExceptionTable(...) for corresponding
671 // dwarf emission
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000672
Chris Lattnera868bbb2011-04-08 18:02:51 +0000673 // Parse LSDA header.
674 uint8_t lpStartEncoding = *lsda++;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000675
Chris Lattnera868bbb2011-04-08 18:02:51 +0000676 if (lpStartEncoding != llvm::dwarf::DW_EH_PE_omit) {
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000677 readEncodedPointer(&lsda, lpStartEncoding);
Chris Lattnera868bbb2011-04-08 18:02:51 +0000678 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000679
Chris Lattnera868bbb2011-04-08 18:02:51 +0000680 uint8_t ttypeEncoding = *lsda++;
681 uintptr_t classInfoOffset;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000682
Chris Lattnera868bbb2011-04-08 18:02:51 +0000683 if (ttypeEncoding != llvm::dwarf::DW_EH_PE_omit) {
684 // Calculate type info locations in emitted dwarf code which
685 // were flagged by type info arguments to llvm.eh.selector
686 // intrinsic
687 classInfoOffset = readULEB128(&lsda);
Rafael Espindola5096adf2013-05-01 21:05:05 +0000688 ClassInfo = lsda + classInfoOffset;
Chris Lattnera868bbb2011-04-08 18:02:51 +0000689 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000690
691 // Walk call-site table looking for range that
692 // includes current PC.
693
Chris Lattnera868bbb2011-04-08 18:02:51 +0000694 uint8_t callSiteEncoding = *lsda++;
695 uint32_t callSiteTableLength = readULEB128(&lsda);
Garrison Venn88bd9d62011-04-10 14:06:52 +0000696 const uint8_t *callSiteTableStart = lsda;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000697 const uint8_t *callSiteTableEnd = callSiteTableStart +
Chris Lattnera868bbb2011-04-08 18:02:51 +0000698 callSiteTableLength;
Garrison Venn88bd9d62011-04-10 14:06:52 +0000699 const uint8_t *actionTableStart = callSiteTableEnd;
700 const uint8_t *callSitePtr = callSiteTableStart;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000701
Chris Lattnera868bbb2011-04-08 18:02:51 +0000702 while (callSitePtr < callSiteTableEnd) {
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000703 uintptr_t start = readEncodedPointer(&callSitePtr,
Chris Lattnera868bbb2011-04-08 18:02:51 +0000704 callSiteEncoding);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000705 uintptr_t length = readEncodedPointer(&callSitePtr,
Chris Lattnera868bbb2011-04-08 18:02:51 +0000706 callSiteEncoding);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000707 uintptr_t landingPad = readEncodedPointer(&callSitePtr,
Garrison Vennf4d2f842010-02-09 23:22:43 +0000708 callSiteEncoding);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000709
Chris Lattnera868bbb2011-04-08 18:02:51 +0000710 // Note: Action value
711 uintptr_t actionEntry = readULEB128(&callSitePtr);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000712
Chris Lattnera868bbb2011-04-08 18:02:51 +0000713 if (exceptionClass != ourBaseExceptionClass) {
714 // We have been notified of a foreign exception being thrown,
715 // and we therefore need to execute cleanup landing pads
716 actionEntry = 0;
Chris Lattnera868bbb2011-04-08 18:02:51 +0000717 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000718
Chris Lattnera868bbb2011-04-08 18:02:51 +0000719 if (landingPad == 0) {
Garrison Vennf4d2f842010-02-09 23:22:43 +0000720#ifdef DEBUG
Chris Lattnera868bbb2011-04-08 18:02:51 +0000721 fprintf(stderr,
722 "handleLsda(...): No landing pad found.\n");
Garrison Vennf4d2f842010-02-09 23:22:43 +0000723#endif
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000724
Chris Lattnera868bbb2011-04-08 18:02:51 +0000725 continue; // no landing pad for this entry
726 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000727
Chris Lattnera868bbb2011-04-08 18:02:51 +0000728 if (actionEntry) {
729 actionEntry += ((uintptr_t) actionTableStart) - 1;
730 }
731 else {
732#ifdef DEBUG
733 fprintf(stderr,
734 "handleLsda(...):No action table found.\n");
735#endif
736 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000737
Chris Lattnera868bbb2011-04-08 18:02:51 +0000738 bool exceptionMatched = false;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000739
Chris Lattnera868bbb2011-04-08 18:02:51 +0000740 if ((start <= pcOffset) && (pcOffset < (start + length))) {
741#ifdef DEBUG
742 fprintf(stderr,
743 "handleLsda(...): Landing pad found.\n");
744#endif
745 int64_t actionValue = 0;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000746
Chris Lattnera868bbb2011-04-08 18:02:51 +0000747 if (actionEntry) {
Garrison Venn88bd9d62011-04-10 14:06:52 +0000748 exceptionMatched = handleActionValue(&actionValue,
Rafael Espindola5096adf2013-05-01 21:05:05 +0000749 ttypeEncoding,
750 ClassInfo,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000751 actionEntry,
752 exceptionClass,
Garrison Venn88bd9d62011-04-10 14:06:52 +0000753 exceptionObject);
Chris Lattnera868bbb2011-04-08 18:02:51 +0000754 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000755
Chris Lattnera868bbb2011-04-08 18:02:51 +0000756 if (!(actions & _UA_SEARCH_PHASE)) {
757#ifdef DEBUG
758 fprintf(stderr,
759 "handleLsda(...): installed landing pad "
760 "context.\n");
761#endif
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000762
Chris Lattnera868bbb2011-04-08 18:02:51 +0000763 // Found landing pad for the PC.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000764 // Set Instruction Pointer to so we re-enter function
765 // at landing pad. The landing pad is created by the
Chris Lattnera868bbb2011-04-08 18:02:51 +0000766 // compiler to take two parameters in registers.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000767 _Unwind_SetGR(context,
768 __builtin_eh_return_data_regno(0),
Chris Lattnera868bbb2011-04-08 18:02:51 +0000769 (uintptr_t)exceptionObject);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000770
Chris Lattnera868bbb2011-04-08 18:02:51 +0000771 // Note: this virtual register directly corresponds
772 // to the return of the llvm.eh.selector intrinsic
773 if (!actionEntry || !exceptionMatched) {
774 // We indicate cleanup only
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000775 _Unwind_SetGR(context,
776 __builtin_eh_return_data_regno(1),
Chris Lattnera868bbb2011-04-08 18:02:51 +0000777 0);
Garrison Vennf4d2f842010-02-09 23:22:43 +0000778 }
779 else {
Chris Lattnera868bbb2011-04-08 18:02:51 +0000780 // Matched type info index of llvm.eh.selector intrinsic
781 // passed here.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000782 _Unwind_SetGR(context,
783 __builtin_eh_return_data_regno(1),
Chris Lattnera868bbb2011-04-08 18:02:51 +0000784 actionValue);
Garrison Vennf4d2f842010-02-09 23:22:43 +0000785 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000786
Chris Lattnera868bbb2011-04-08 18:02:51 +0000787 // To execute landing pad set here
788 _Unwind_SetIP(context, funcStart + landingPad);
789 ret = _URC_INSTALL_CONTEXT;
790 }
791 else if (exceptionMatched) {
Garrison Vennf4d2f842010-02-09 23:22:43 +0000792#ifdef DEBUG
Chris Lattnera868bbb2011-04-08 18:02:51 +0000793 fprintf(stderr,
794 "handleLsda(...): setting handler found.\n");
Garrison Vennf4d2f842010-02-09 23:22:43 +0000795#endif
Chris Lattnera868bbb2011-04-08 18:02:51 +0000796 ret = _URC_HANDLER_FOUND;
797 }
798 else {
799 // Note: Only non-clean up handlers are marked as
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000800 // found. Otherwise the clean up handlers will be
801 // re-found and executed during the clean up
Chris Lattnera868bbb2011-04-08 18:02:51 +0000802 // phase.
Garrison Vennf4d2f842010-02-09 23:22:43 +0000803#ifdef DEBUG
Chris Lattnera868bbb2011-04-08 18:02:51 +0000804 fprintf(stderr,
805 "handleLsda(...): cleanup handler found.\n");
Garrison Vennf4d2f842010-02-09 23:22:43 +0000806#endif
Chris Lattnera868bbb2011-04-08 18:02:51 +0000807 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000808
Chris Lattnera868bbb2011-04-08 18:02:51 +0000809 break;
Garrison Vennf4d2f842010-02-09 23:22:43 +0000810 }
Chris Lattnera868bbb2011-04-08 18:02:51 +0000811 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000812
Chris Lattnera868bbb2011-04-08 18:02:51 +0000813 return(ret);
Garrison Vennf4d2f842010-02-09 23:22:43 +0000814}
815
816
817/// This is the personality function which is embedded (dwarf emitted), in the
818/// dwarf unwind info block. Again see: JITDwarfEmitter.cpp.
Dmitri Gribenko462462e2013-01-13 15:53:09 +0000819/// See @link http://mentorembedded.github.com/cxx-abi/abi-eh.html @unlink
Garrison Vennf4d2f842010-02-09 23:22:43 +0000820/// @param version unsupported (ignored), unwind version
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000821/// @param _Unwind_Action actions minimally supported unwind stage
Garrison Vennf4d2f842010-02-09 23:22:43 +0000822/// (forced specifically not supported)
823/// @param exceptionClass exception class (_Unwind_Exception::exception_class)
824/// of thrown exception.
825/// @param exceptionObject thrown _Unwind_Exception instance.
826/// @param context unwind system context
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000827/// @returns minimally supported unwinding control indicator
828_Unwind_Reason_Code ourPersonality(int version,
Chris Lattnera868bbb2011-04-08 18:02:51 +0000829 _Unwind_Action actions,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000830 uint64_t exceptionClass,
Garrison Venn88bd9d62011-04-10 14:06:52 +0000831 struct _Unwind_Exception *exceptionObject,
Chris Lattnera868bbb2011-04-08 18:02:51 +0000832 _Unwind_Context_t context) {
Garrison Vennf4d2f842010-02-09 23:22:43 +0000833#ifdef DEBUG
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000834 fprintf(stderr,
Chris Lattnera868bbb2011-04-08 18:02:51 +0000835 "We are in ourPersonality(...):actions is <%d>.\n",
836 actions);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000837
Chris Lattnera868bbb2011-04-08 18:02:51 +0000838 if (actions & _UA_SEARCH_PHASE) {
839 fprintf(stderr, "ourPersonality(...):In search phase.\n");
840 }
841 else {
842 fprintf(stderr, "ourPersonality(...):In non-search phase.\n");
843 }
Garrison Vennf4d2f842010-02-09 23:22:43 +0000844#endif
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000845
Garrison Venn88bd9d62011-04-10 14:06:52 +0000846 const uint8_t *lsda = _Unwind_GetLanguageSpecificData(context);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000847
Garrison Vennf4d2f842010-02-09 23:22:43 +0000848#ifdef DEBUG
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000849 fprintf(stderr,
Chris Lattnera868bbb2011-04-08 18:02:51 +0000850 "ourPersonality(...):lsda = <%p>.\n",
851 lsda);
Garrison Vennf4d2f842010-02-09 23:22:43 +0000852#endif
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000853
Chris Lattnera868bbb2011-04-08 18:02:51 +0000854 // The real work of the personality function is captured here
855 return(handleLsda(version,
856 lsda,
857 actions,
858 exceptionClass,
859 exceptionObject,
860 context));
Garrison Vennf4d2f842010-02-09 23:22:43 +0000861}
862
863
864/// Generates our _Unwind_Exception class from a given character array.
865/// thereby handling arbitrary lengths (not in standard), and handling
866/// embedded \0s.
Dmitri Gribenko462462e2013-01-13 15:53:09 +0000867/// See @link http://mentorembedded.github.com/cxx-abi/abi-eh.html @unlink
Garrison Vennf4d2f842010-02-09 23:22:43 +0000868/// @param classChars char array to encode. NULL values not checkedf
869/// @param classCharsSize number of chars in classChars. Value is not checked.
870/// @returns class value
871uint64_t genClass(const unsigned char classChars[], size_t classCharsSize)
872{
Chris Lattnera868bbb2011-04-08 18:02:51 +0000873 uint64_t ret = classChars[0];
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000874
Chris Lattnera868bbb2011-04-08 18:02:51 +0000875 for (unsigned i = 1; i < classCharsSize; ++i) {
876 ret <<= 8;
877 ret += classChars[i];
878 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000879
Chris Lattnera868bbb2011-04-08 18:02:51 +0000880 return(ret);
Garrison Vennf4d2f842010-02-09 23:22:43 +0000881}
882
883} // extern "C"
884
885//
886// Runtime C Library functions End
887//
888
889//
890// Code generation functions
891//
892
893/// Generates code to print given constant string
894/// @param context llvm context
895/// @param module code for module instance
896/// @param builder builder instance
897/// @param toPrint string to print
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000898/// @param useGlobal A value of true (default) indicates a GlobalValue is
899/// generated, and is used to hold the constant string. A value of
900/// false indicates that the constant string will be stored on the
Garrison Vennf4d2f842010-02-09 23:22:43 +0000901/// stack.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000902void generateStringPrint(llvm::LLVMContext &context,
Garrison Venn88bd9d62011-04-10 14:06:52 +0000903 llvm::Module &module,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000904 llvm::IRBuilder<> &builder,
Garrison Vennf4d2f842010-02-09 23:22:43 +0000905 std::string toPrint,
906 bool useGlobal = true) {
Chris Lattnera868bbb2011-04-08 18:02:51 +0000907 llvm::Function *printFunct = module.getFunction("printStr");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000908
Chris Lattnera868bbb2011-04-08 18:02:51 +0000909 llvm::Value *stringVar;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000910 llvm::Constant *stringConstant =
Peter Collingbournec4e342b2012-02-06 14:09:13 +0000911 llvm::ConstantDataArray::getString(context, toPrint);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000912
Chris Lattnera868bbb2011-04-08 18:02:51 +0000913 if (useGlobal) {
914 // Note: Does not work without allocation
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000915 stringVar =
916 new llvm::GlobalVariable(module,
Chris Lattnera868bbb2011-04-08 18:02:51 +0000917 stringConstant->getType(),
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000918 true,
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000919 llvm::GlobalValue::PrivateLinkage,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000920 stringConstant,
Chris Lattnera868bbb2011-04-08 18:02:51 +0000921 "");
922 }
923 else {
924 stringVar = builder.CreateAlloca(stringConstant->getType());
925 builder.CreateStore(stringConstant, stringVar);
926 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000927
928 llvm::Value *cast = builder.CreatePointerCast(stringVar,
Garrison Venn0a2eb8d2011-09-23 14:45:10 +0000929 builder.getInt8PtrTy());
Chris Lattnera868bbb2011-04-08 18:02:51 +0000930 builder.CreateCall(printFunct, cast);
Garrison Vennf4d2f842010-02-09 23:22:43 +0000931}
932
933
934/// Generates code to print given runtime integer according to constant
935/// string format, and a given print function.
936/// @param context llvm context
937/// @param module code for module instance
938/// @param builder builder instance
939/// @param printFunct function used to "print" integer
940/// @param toPrint string to print
941/// @param format printf like formating string for print
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000942/// @param useGlobal A value of true (default) indicates a GlobalValue is
943/// generated, and is used to hold the constant string. A value of
944/// false indicates that the constant string will be stored on the
Garrison Vennf4d2f842010-02-09 23:22:43 +0000945/// stack.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000946void generateIntegerPrint(llvm::LLVMContext &context,
Garrison Venn88bd9d62011-04-10 14:06:52 +0000947 llvm::Module &module,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000948 llvm::IRBuilder<> &builder,
Garrison Venn88bd9d62011-04-10 14:06:52 +0000949 llvm::Function &printFunct,
950 llvm::Value &toPrint,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000951 std::string format,
Garrison Vennf4d2f842010-02-09 23:22:43 +0000952 bool useGlobal = true) {
Peter Collingbournec4e342b2012-02-06 14:09:13 +0000953 llvm::Constant *stringConstant =
954 llvm::ConstantDataArray::getString(context, format);
Chris Lattnera868bbb2011-04-08 18:02:51 +0000955 llvm::Value *stringVar;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000956
Chris Lattnera868bbb2011-04-08 18:02:51 +0000957 if (useGlobal) {
958 // Note: Does not seem to work without allocation
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000959 stringVar =
960 new llvm::GlobalVariable(module,
Chris Lattnera868bbb2011-04-08 18:02:51 +0000961 stringConstant->getType(),
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000962 true,
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000963 llvm::GlobalValue::PrivateLinkage,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000964 stringConstant,
Chris Lattnera868bbb2011-04-08 18:02:51 +0000965 "");
966 }
967 else {
968 stringVar = builder.CreateAlloca(stringConstant->getType());
969 builder.CreateStore(stringConstant, stringVar);
970 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000971
972 llvm::Value *cast = builder.CreateBitCast(stringVar,
Garrison Venn0a2eb8d2011-09-23 14:45:10 +0000973 builder.getInt8PtrTy());
David Blaikie3b20b042015-08-14 00:24:56 +0000974 builder.CreateCall(&printFunct, {&toPrint, cast});
Garrison Vennf4d2f842010-02-09 23:22:43 +0000975}
976
977
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000978/// Generates code to handle finally block type semantics: always runs
979/// regardless of whether a thrown exception is passing through or the
980/// parent function is simply exiting. In addition to printing some state
981/// to stderr, this code will resume the exception handling--runs the
982/// unwind resume block, if the exception has not been previously caught
983/// by a catch clause, and will otherwise execute the end block (terminator
984/// block). In addition this function creates the corresponding function's
Garrison Vennf4d2f842010-02-09 23:22:43 +0000985/// stack storage for the exception pointer and catch flag status.
986/// @param context llvm context
987/// @param module code for module instance
988/// @param builder builder instance
989/// @param toAddTo parent function to add block to
990/// @param blockName block name of new "finally" block.
991/// @param functionId output id used for printing
992/// @param terminatorBlock terminator "end" block
993/// @param unwindResumeBlock unwind resume block
994/// @param exceptionCaughtFlag reference exception caught/thrown status storage
995/// @param exceptionStorage reference to exception pointer storage
Garrison Venn0a2eb8d2011-09-23 14:45:10 +0000996/// @param caughtResultStorage reference to landingpad result storage
Garrison Vennf4d2f842010-02-09 23:22:43 +0000997/// @returns newly created block
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +0000998static llvm::BasicBlock *createFinallyBlock(llvm::LLVMContext &context,
999 llvm::Module &module,
1000 llvm::IRBuilder<> &builder,
Garrison Venn88bd9d62011-04-10 14:06:52 +00001001 llvm::Function &toAddTo,
1002 std::string &blockName,
1003 std::string &functionId,
1004 llvm::BasicBlock &terminatorBlock,
1005 llvm::BasicBlock &unwindResumeBlock,
1006 llvm::Value **exceptionCaughtFlag,
Bill Wendling383727b2012-02-04 00:29:12 +00001007 llvm::Value **exceptionStorage,
1008 llvm::Value **caughtResultStorage) {
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001009 assert(exceptionCaughtFlag &&
Chris Lattnera868bbb2011-04-08 18:02:51 +00001010 "ExceptionDemo::createFinallyBlock(...):exceptionCaughtFlag "
1011 "is NULL");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001012 assert(exceptionStorage &&
Chris Lattnera868bbb2011-04-08 18:02:51 +00001013 "ExceptionDemo::createFinallyBlock(...):exceptionStorage "
1014 "is NULL");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001015 assert(caughtResultStorage &&
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001016 "ExceptionDemo::createFinallyBlock(...):caughtResultStorage "
1017 "is NULL");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001018
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001019 *exceptionCaughtFlag = createEntryBlockAlloca(toAddTo,
1020 "exceptionCaught",
1021 ourExceptionNotThrownState->getType(),
1022 ourExceptionNotThrownState);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001023
Chris Lattner6b8eb8c2011-07-18 04:52:09 +00001024 llvm::PointerType *exceptionStorageType = builder.getInt8PtrTy();
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001025 *exceptionStorage = createEntryBlockAlloca(toAddTo,
1026 "exceptionStorage",
1027 exceptionStorageType,
1028 llvm::ConstantPointerNull::get(
1029 exceptionStorageType));
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001030 *caughtResultStorage = createEntryBlockAlloca(toAddTo,
1031 "caughtResultStorage",
1032 ourCaughtResultType,
1033 llvm::ConstantAggregateZero::get(
1034 ourCaughtResultType));
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001035
Chris Lattnera868bbb2011-04-08 18:02:51 +00001036 llvm::BasicBlock *ret = llvm::BasicBlock::Create(context,
1037 blockName,
1038 &toAddTo);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001039
Chris Lattnera868bbb2011-04-08 18:02:51 +00001040 builder.SetInsertPoint(ret);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001041
Chris Lattnera868bbb2011-04-08 18:02:51 +00001042 std::ostringstream bufferToPrint;
1043 bufferToPrint << "Gen: Executing finally block "
1044 << blockName << " in " << functionId << "\n";
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001045 generateStringPrint(context,
1046 module,
1047 builder,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001048 bufferToPrint.str(),
1049 USE_GLOBAL_STR_CONSTS);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001050
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001051 llvm::SwitchInst *theSwitch = builder.CreateSwitch(builder.CreateLoad(
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001052 *exceptionCaughtFlag),
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001053 &terminatorBlock,
1054 2);
Chris Lattnera868bbb2011-04-08 18:02:51 +00001055 theSwitch->addCase(ourExceptionCaughtState, &terminatorBlock);
1056 theSwitch->addCase(ourExceptionThrownState, &unwindResumeBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001057
Chris Lattnera868bbb2011-04-08 18:02:51 +00001058 return(ret);
Garrison Vennf4d2f842010-02-09 23:22:43 +00001059}
1060
1061
1062/// Generates catch block semantics which print a string to indicate type of
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001063/// catch executed, sets an exception caught flag, and executes passed in
Garrison Vennf4d2f842010-02-09 23:22:43 +00001064/// end block (terminator block).
1065/// @param context llvm context
1066/// @param module code for module instance
1067/// @param builder builder instance
1068/// @param toAddTo parent function to add block to
1069/// @param blockName block name of new "catch" block.
1070/// @param functionId output id used for printing
1071/// @param terminatorBlock terminator "end" block
1072/// @param exceptionCaughtFlag exception caught/thrown status
1073/// @returns newly created block
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001074static llvm::BasicBlock *createCatchBlock(llvm::LLVMContext &context,
1075 llvm::Module &module,
1076 llvm::IRBuilder<> &builder,
Garrison Venn88bd9d62011-04-10 14:06:52 +00001077 llvm::Function &toAddTo,
1078 std::string &blockName,
1079 std::string &functionId,
1080 llvm::BasicBlock &terminatorBlock,
1081 llvm::Value &exceptionCaughtFlag) {
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001082
Chris Lattnera868bbb2011-04-08 18:02:51 +00001083 llvm::BasicBlock *ret = llvm::BasicBlock::Create(context,
1084 blockName,
1085 &toAddTo);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001086
Chris Lattnera868bbb2011-04-08 18:02:51 +00001087 builder.SetInsertPoint(ret);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001088
Chris Lattnera868bbb2011-04-08 18:02:51 +00001089 std::ostringstream bufferToPrint;
1090 bufferToPrint << "Gen: Executing catch block "
1091 << blockName
1092 << " in "
1093 << functionId
1094 << std::endl;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001095 generateStringPrint(context,
1096 module,
1097 builder,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001098 bufferToPrint.str(),
1099 USE_GLOBAL_STR_CONSTS);
1100 builder.CreateStore(ourExceptionCaughtState, &exceptionCaughtFlag);
1101 builder.CreateBr(&terminatorBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001102
Chris Lattnera868bbb2011-04-08 18:02:51 +00001103 return(ret);
Garrison Vennf4d2f842010-02-09 23:22:43 +00001104}
1105
1106
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001107/// Generates a function which invokes a function (toInvoke) and, whose
1108/// unwind block will "catch" the type info types correspondingly held in the
1109/// exceptionTypesToCatch argument. If the toInvoke function throws an
1110/// exception which does not match any type info types contained in
1111/// exceptionTypesToCatch, the generated code will call _Unwind_Resume
1112/// with the raised exception. On the other hand the generated code will
Garrison Vennf4d2f842010-02-09 23:22:43 +00001113/// normally exit if the toInvoke function does not throw an exception.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001114/// The generated "finally" block is always run regardless of the cause of
Garrison Vennf4d2f842010-02-09 23:22:43 +00001115/// the generated function exit.
1116/// The generated function is returned after being verified.
1117/// @param module code for module instance
1118/// @param builder builder instance
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001119/// @param fpm a function pass manager holding optional IR to IR
Garrison Vennf4d2f842010-02-09 23:22:43 +00001120/// transformations
1121/// @param toInvoke inner function to invoke
1122/// @param ourId id used to printing purposes
1123/// @param numExceptionsToCatch length of exceptionTypesToCatch array
1124/// @param exceptionTypesToCatch array of type info types to "catch"
1125/// @returns generated function
Chandler Carruth7ecd9912015-02-13 10:21:05 +00001126static llvm::Function *createCatchWrappedInvokeFunction(
1127 llvm::Module &module, llvm::IRBuilder<> &builder,
1128 llvm::legacy::FunctionPassManager &fpm, llvm::Function &toInvoke,
1129 std::string ourId, unsigned numExceptionsToCatch,
1130 unsigned exceptionTypesToCatch[]) {
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001131
Garrison Venn88bd9d62011-04-10 14:06:52 +00001132 llvm::LLVMContext &context = module.getContext();
Chris Lattnera868bbb2011-04-08 18:02:51 +00001133 llvm::Function *toPrint32Int = module.getFunction("print32Int");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001134
Chris Lattnera868bbb2011-04-08 18:02:51 +00001135 ArgTypes argTypes;
Garrison Venn76310ac2011-07-12 15:34:42 +00001136 argTypes.push_back(builder.getInt32Ty());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001137
Chris Lattnera868bbb2011-04-08 18:02:51 +00001138 ArgNames argNames;
1139 argNames.push_back("exceptTypeToThrow");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001140
1141 llvm::Function *ret = createFunction(module,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001142 builder.getVoidTy(),
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001143 argTypes,
1144 argNames,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001145 ourId,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001146 llvm::Function::ExternalLinkage,
1147 false,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001148 false);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001149
Chris Lattnera868bbb2011-04-08 18:02:51 +00001150 // Block which calls invoke
1151 llvm::BasicBlock *entryBlock = llvm::BasicBlock::Create(context,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001152 "entry",
Chris Lattnera868bbb2011-04-08 18:02:51 +00001153 ret);
1154 // Normal block for invoke
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001155 llvm::BasicBlock *normalBlock = llvm::BasicBlock::Create(context,
1156 "normal",
Chris Lattnera868bbb2011-04-08 18:02:51 +00001157 ret);
1158 // Unwind block for invoke
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001159 llvm::BasicBlock *exceptionBlock = llvm::BasicBlock::Create(context,
1160 "exception",
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001161 ret);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001162
Chris Lattnera868bbb2011-04-08 18:02:51 +00001163 // Block which routes exception to correct catch handler block
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001164 llvm::BasicBlock *exceptionRouteBlock = llvm::BasicBlock::Create(context,
1165 "exceptionRoute",
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001166 ret);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001167
Chris Lattnera868bbb2011-04-08 18:02:51 +00001168 // Foreign exception handler
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001169 llvm::BasicBlock *externalExceptionBlock = llvm::BasicBlock::Create(context,
1170 "externalException",
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001171 ret);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001172
Chris Lattnera868bbb2011-04-08 18:02:51 +00001173 // Block which calls _Unwind_Resume
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001174 llvm::BasicBlock *unwindResumeBlock = llvm::BasicBlock::Create(context,
1175 "unwindResume",
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001176 ret);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001177
Chris Lattnera868bbb2011-04-08 18:02:51 +00001178 // Clean up block which delete exception if needed
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001179 llvm::BasicBlock *endBlock = llvm::BasicBlock::Create(context, "end", ret);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001180
Chris Lattnera868bbb2011-04-08 18:02:51 +00001181 std::string nextName;
1182 std::vector<llvm::BasicBlock*> catchBlocks(numExceptionsToCatch);
Garrison Venn88bd9d62011-04-10 14:06:52 +00001183 llvm::Value *exceptionCaughtFlag = NULL;
1184 llvm::Value *exceptionStorage = NULL;
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001185 llvm::Value *caughtResultStorage = NULL;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001186
1187 // Finally block which will branch to unwindResumeBlock if
Chris Lattnera868bbb2011-04-08 18:02:51 +00001188 // exception is not caught. Initializes/allocates stack locations.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001189 llvm::BasicBlock *finallyBlock = createFinallyBlock(context,
1190 module,
1191 builder,
1192 *ret,
1193 nextName = "finally",
Chris Lattnera868bbb2011-04-08 18:02:51 +00001194 ourId,
1195 *endBlock,
1196 *unwindResumeBlock,
1197 &exceptionCaughtFlag,
Bill Wendling383727b2012-02-04 00:29:12 +00001198 &exceptionStorage,
1199 &caughtResultStorage
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001200 );
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001201
Chris Lattnera868bbb2011-04-08 18:02:51 +00001202 for (unsigned i = 0; i < numExceptionsToCatch; ++i) {
1203 nextName = ourTypeInfoNames[exceptionTypesToCatch[i]];
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001204
Chris Lattnera868bbb2011-04-08 18:02:51 +00001205 // One catch block per type info to be caught
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001206 catchBlocks[i] = createCatchBlock(context,
1207 module,
1208 builder,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001209 *ret,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001210 nextName,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001211 ourId,
1212 *finallyBlock,
1213 *exceptionCaughtFlag);
1214 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001215
Chris Lattnera868bbb2011-04-08 18:02:51 +00001216 // Entry Block
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001217
Chris Lattnera868bbb2011-04-08 18:02:51 +00001218 builder.SetInsertPoint(entryBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001219
Chris Lattnera868bbb2011-04-08 18:02:51 +00001220 std::vector<llvm::Value*> args;
1221 args.push_back(namedValues["exceptTypeToThrow"]);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001222 builder.CreateInvoke(&toInvoke,
1223 normalBlock,
1224 exceptionBlock,
Chris Lattner6b8eb8c2011-07-18 04:52:09 +00001225 args);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001226
Chris Lattnera868bbb2011-04-08 18:02:51 +00001227 // End Block
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001228
Chris Lattnera868bbb2011-04-08 18:02:51 +00001229 builder.SetInsertPoint(endBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001230
1231 generateStringPrint(context,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001232 module,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001233 builder,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001234 "Gen: In end block: exiting in " + ourId + ".\n",
1235 USE_GLOBAL_STR_CONSTS);
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001236 llvm::Function *deleteOurException = module.getFunction("deleteOurException");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001237
Chris Lattnera868bbb2011-04-08 18:02:51 +00001238 // Note: function handles NULL exceptions
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001239 builder.CreateCall(deleteOurException,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001240 builder.CreateLoad(exceptionStorage));
1241 builder.CreateRetVoid();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001242
Chris Lattnera868bbb2011-04-08 18:02:51 +00001243 // Normal Block
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001244
Chris Lattnera868bbb2011-04-08 18:02:51 +00001245 builder.SetInsertPoint(normalBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001246
1247 generateStringPrint(context,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001248 module,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001249 builder,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001250 "Gen: No exception in " + ourId + "!\n",
1251 USE_GLOBAL_STR_CONSTS);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001252
Chris Lattnera868bbb2011-04-08 18:02:51 +00001253 // Finally block is always called
1254 builder.CreateBr(finallyBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001255
Chris Lattnera868bbb2011-04-08 18:02:51 +00001256 // Unwind Resume Block
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001257
Chris Lattnera868bbb2011-04-08 18:02:51 +00001258 builder.SetInsertPoint(unwindResumeBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001259
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001260 builder.CreateResume(builder.CreateLoad(caughtResultStorage));
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001261
Chris Lattnera868bbb2011-04-08 18:02:51 +00001262 // Exception Block
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001263
Chris Lattnera868bbb2011-04-08 18:02:51 +00001264 builder.SetInsertPoint(exceptionBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001265
Garrison Venn8cb00352011-09-22 15:45:14 +00001266 llvm::Function *personality = module.getFunction("ourPersonality");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001267
1268 llvm::LandingPadInst *caughtResult =
Garrison Venn8cb00352011-09-22 15:45:14 +00001269 builder.CreateLandingPad(ourCaughtResultType,
1270 personality,
1271 numExceptionsToCatch,
1272 "landingPad");
1273
1274 caughtResult->setCleanup(true);
1275
1276 for (unsigned i = 0; i < numExceptionsToCatch; ++i) {
1277 // Set up type infos to be caught
1278 caughtResult->addClause(module.getGlobalVariable(
1279 ourTypeInfoNames[exceptionTypesToCatch[i]]));
1280 }
1281
1282 llvm::Value *unwindException = builder.CreateExtractValue(caughtResult, 0);
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001283 llvm::Value *retTypeInfoIndex = builder.CreateExtractValue(caughtResult, 1);
Garrison Venn8cb00352011-09-22 15:45:14 +00001284
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001285 // FIXME: Redundant storage which, beyond utilizing value of
1286 // caughtResultStore for unwindException storage, may be alleviated
Benjamin Kramerbde91762012-06-02 10:20:22 +00001287 // altogether with a block rearrangement
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001288 builder.CreateStore(caughtResult, caughtResultStorage);
Garrison Venn8cb00352011-09-22 15:45:14 +00001289 builder.CreateStore(unwindException, exceptionStorage);
1290 builder.CreateStore(ourExceptionThrownState, exceptionCaughtFlag);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001291
1292 // Retrieve exception_class member from thrown exception
Chris Lattnera868bbb2011-04-08 18:02:51 +00001293 // (_Unwind_Exception instance). This member tells us whether or not
1294 // the exception is foreign.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001295 llvm::Value *unwindExceptionClass =
David Blaikie83a20fa2015-04-22 04:24:43 +00001296 builder.CreateLoad(builder.CreateStructGEP(
1297 ourUnwindExceptionType,
1298 builder.CreatePointerCast(unwindException,
1299 ourUnwindExceptionType->getPointerTo()),
1300 0));
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001301
Chris Lattnera868bbb2011-04-08 18:02:51 +00001302 // Branch to the externalExceptionBlock if the exception is foreign or
1303 // to a catch router if not. Either way the finally block will be run.
1304 builder.CreateCondBr(builder.CreateICmpEQ(unwindExceptionClass,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001305 llvm::ConstantInt::get(builder.getInt64Ty(),
Chris Lattnera868bbb2011-04-08 18:02:51 +00001306 ourBaseExceptionClass)),
1307 exceptionRouteBlock,
1308 externalExceptionBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001309
Chris Lattnera868bbb2011-04-08 18:02:51 +00001310 // External Exception Block
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001311
Chris Lattnera868bbb2011-04-08 18:02:51 +00001312 builder.SetInsertPoint(externalExceptionBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001313
1314 generateStringPrint(context,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001315 module,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001316 builder,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001317 "Gen: Foreign exception received.\n",
1318 USE_GLOBAL_STR_CONSTS);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001319
Chris Lattnera868bbb2011-04-08 18:02:51 +00001320 // Branch to the finally block
1321 builder.CreateBr(finallyBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001322
Chris Lattnera868bbb2011-04-08 18:02:51 +00001323 // Exception Route Block
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001324
Chris Lattnera868bbb2011-04-08 18:02:51 +00001325 builder.SetInsertPoint(exceptionRouteBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001326
1327 // Casts exception pointer (_Unwind_Exception instance) to parent
Chris Lattnera868bbb2011-04-08 18:02:51 +00001328 // (OurException instance).
1329 //
1330 // Note: ourBaseFromUnwindOffset is usually negative
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001331 llvm::Value *typeInfoThrown = builder.CreatePointerCast(
1332 builder.CreateConstGEP1_64(unwindException,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001333 ourBaseFromUnwindOffset),
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001334 ourExceptionType->getPointerTo());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001335
Chris Lattnera868bbb2011-04-08 18:02:51 +00001336 // Retrieve thrown exception type info type
1337 //
1338 // Note: Index is not relative to pointer but instead to structure
1339 // unlike a true getelementptr (GEP) instruction
David Blaikie83a20fa2015-04-22 04:24:43 +00001340 typeInfoThrown = builder.CreateStructGEP(ourExceptionType, typeInfoThrown, 0);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001341
1342 llvm::Value *typeInfoThrownType =
David Blaikie83a20fa2015-04-22 04:24:43 +00001343 builder.CreateStructGEP(builder.getInt8PtrTy(), typeInfoThrown, 0);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001344
1345 generateIntegerPrint(context,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001346 module,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001347 builder,
1348 *toPrint32Int,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001349 *(builder.CreateLoad(typeInfoThrownType)),
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001350 "Gen: Exception type <%d> received (stack unwound) "
1351 " in " +
1352 ourId +
Chris Lattnera868bbb2011-04-08 18:02:51 +00001353 ".\n",
1354 USE_GLOBAL_STR_CONSTS);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001355
Chris Lattnera868bbb2011-04-08 18:02:51 +00001356 // Route to matched type info catch block or run cleanup finally block
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001357 llvm::SwitchInst *switchToCatchBlock = builder.CreateSwitch(retTypeInfoIndex,
1358 finallyBlock,
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001359 numExceptionsToCatch);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001360
Chris Lattnera868bbb2011-04-08 18:02:51 +00001361 unsigned nextTypeToCatch;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001362
Chris Lattnera868bbb2011-04-08 18:02:51 +00001363 for (unsigned i = 1; i <= numExceptionsToCatch; ++i) {
1364 nextTypeToCatch = i - 1;
1365 switchToCatchBlock->addCase(llvm::ConstantInt::get(
1366 llvm::Type::getInt32Ty(context), i),
1367 catchBlocks[nextTypeToCatch]);
1368 }
Garrison Venneb89d362011-09-22 14:07:50 +00001369
Chris Lattnera868bbb2011-04-08 18:02:51 +00001370 llvm::verifyFunction(*ret);
1371 fpm.run(*ret);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001372
Chris Lattnera868bbb2011-04-08 18:02:51 +00001373 return(ret);
Garrison Vennf4d2f842010-02-09 23:22:43 +00001374}
1375
1376
1377/// Generates function which throws either an exception matched to a runtime
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001378/// determined type info type (argument to generated function), or if this
1379/// runtime value matches nativeThrowType, throws a foreign exception by
Garrison Vennf4d2f842010-02-09 23:22:43 +00001380/// calling nativeThrowFunct.
1381/// @param module code for module instance
1382/// @param builder builder instance
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001383/// @param fpm a function pass manager holding optional IR to IR
Garrison Vennf4d2f842010-02-09 23:22:43 +00001384/// transformations
1385/// @param ourId id used to printing purposes
1386/// @param nativeThrowType a runtime argument of this value results in
1387/// nativeThrowFunct being called to generate/throw exception.
1388/// @param nativeThrowFunct function which will throw a foreign exception
1389/// if the above nativeThrowType matches generated function's arg.
1390/// @returns generated function
Chandler Carruth7ecd9912015-02-13 10:21:05 +00001391static llvm::Function *
1392createThrowExceptionFunction(llvm::Module &module, llvm::IRBuilder<> &builder,
1393 llvm::legacy::FunctionPassManager &fpm,
1394 std::string ourId, int32_t nativeThrowType,
1395 llvm::Function &nativeThrowFunct) {
Garrison Venn88bd9d62011-04-10 14:06:52 +00001396 llvm::LLVMContext &context = module.getContext();
Chris Lattnera868bbb2011-04-08 18:02:51 +00001397 namedValues.clear();
1398 ArgTypes unwindArgTypes;
Garrison Venn76310ac2011-07-12 15:34:42 +00001399 unwindArgTypes.push_back(builder.getInt32Ty());
Chris Lattnera868bbb2011-04-08 18:02:51 +00001400 ArgNames unwindArgNames;
1401 unwindArgNames.push_back("exceptTypeToThrow");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001402
Chris Lattnera868bbb2011-04-08 18:02:51 +00001403 llvm::Function *ret = createFunction(module,
1404 builder.getVoidTy(),
1405 unwindArgTypes,
1406 unwindArgNames,
1407 ourId,
1408 llvm::Function::ExternalLinkage,
1409 false,
1410 false);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001411
Chris Lattnera868bbb2011-04-08 18:02:51 +00001412 // Throws either one of our exception or a native C++ exception depending
1413 // on a runtime argument value containing a type info type.
1414 llvm::BasicBlock *entryBlock = llvm::BasicBlock::Create(context,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001415 "entry",
Chris Lattnera868bbb2011-04-08 18:02:51 +00001416 ret);
1417 // Throws a foreign exception
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001418 llvm::BasicBlock *nativeThrowBlock = llvm::BasicBlock::Create(context,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001419 "nativeThrow",
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001420 ret);
Chris Lattnera868bbb2011-04-08 18:02:51 +00001421 // Throws one of our Exceptions
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001422 llvm::BasicBlock *generatedThrowBlock = llvm::BasicBlock::Create(context,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001423 "generatedThrow",
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001424 ret);
Chris Lattnera868bbb2011-04-08 18:02:51 +00001425 // Retrieved runtime type info type to throw
Garrison Venn88bd9d62011-04-10 14:06:52 +00001426 llvm::Value *exceptionType = namedValues["exceptTypeToThrow"];
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001427
Chris Lattnera868bbb2011-04-08 18:02:51 +00001428 // nativeThrowBlock block
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001429
Chris Lattnera868bbb2011-04-08 18:02:51 +00001430 builder.SetInsertPoint(nativeThrowBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001431
Chris Lattnera868bbb2011-04-08 18:02:51 +00001432 // Throws foreign exception
1433 builder.CreateCall(&nativeThrowFunct, exceptionType);
1434 builder.CreateUnreachable();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001435
Chris Lattnera868bbb2011-04-08 18:02:51 +00001436 // entry block
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001437
Chris Lattnera868bbb2011-04-08 18:02:51 +00001438 builder.SetInsertPoint(entryBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001439
Chris Lattnera868bbb2011-04-08 18:02:51 +00001440 llvm::Function *toPrint32Int = module.getFunction("print32Int");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001441 generateIntegerPrint(context,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001442 module,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001443 builder,
1444 *toPrint32Int,
1445 *exceptionType,
1446 "\nGen: About to throw exception type <%d> in " +
1447 ourId +
Chris Lattnera868bbb2011-04-08 18:02:51 +00001448 ".\n",
1449 USE_GLOBAL_STR_CONSTS);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001450
Chris Lattnera868bbb2011-04-08 18:02:51 +00001451 // Switches on runtime type info type value to determine whether or not
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001452 // a foreign exception is thrown. Defaults to throwing one of our
Chris Lattnera868bbb2011-04-08 18:02:51 +00001453 // generated exceptions.
Garrison Venn88bd9d62011-04-10 14:06:52 +00001454 llvm::SwitchInst *theSwitch = builder.CreateSwitch(exceptionType,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001455 generatedThrowBlock,
1456 1);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001457
1458 theSwitch->addCase(llvm::ConstantInt::get(llvm::Type::getInt32Ty(context),
Chris Lattnera868bbb2011-04-08 18:02:51 +00001459 nativeThrowType),
1460 nativeThrowBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001461
Chris Lattnera868bbb2011-04-08 18:02:51 +00001462 // generatedThrow block
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001463
Chris Lattnera868bbb2011-04-08 18:02:51 +00001464 builder.SetInsertPoint(generatedThrowBlock);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001465
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001466 llvm::Function *createOurException = module.getFunction("createOurException");
1467 llvm::Function *raiseOurException = module.getFunction(
1468 "_Unwind_RaiseException");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001469
Chris Lattnera868bbb2011-04-08 18:02:51 +00001470 // Creates exception to throw with runtime type info type.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001471 llvm::Value *exception = builder.CreateCall(createOurException,
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001472 namedValues["exceptTypeToThrow"]);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001473
Chris Lattnera868bbb2011-04-08 18:02:51 +00001474 // Throw generated Exception
1475 builder.CreateCall(raiseOurException, exception);
1476 builder.CreateUnreachable();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001477
Chris Lattnera868bbb2011-04-08 18:02:51 +00001478 llvm::verifyFunction(*ret);
1479 fpm.run(*ret);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001480
Chris Lattnera868bbb2011-04-08 18:02:51 +00001481 return(ret);
Garrison Vennf4d2f842010-02-09 23:22:43 +00001482}
1483
1484static void createStandardUtilityFunctions(unsigned numTypeInfos,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001485 llvm::Module &module,
Garrison Venn88bd9d62011-04-10 14:06:52 +00001486 llvm::IRBuilder<> &builder);
Garrison Vennf4d2f842010-02-09 23:22:43 +00001487
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001488/// Creates test code by generating and organizing these functions into the
Garrison Vennf4d2f842010-02-09 23:22:43 +00001489/// test case. The test case consists of an outer function setup to invoke
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001490/// an inner function within an environment having multiple catch and single
Garrison Vennf4d2f842010-02-09 23:22:43 +00001491/// finally blocks. This inner function is also setup to invoke a throw
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001492/// function within an evironment similar in nature to the outer function's
Garrison Vennf4d2f842010-02-09 23:22:43 +00001493/// catch and finally blocks. Each of these two functions catch mutually
1494/// exclusive subsets (even or odd) of the type info types configured
1495/// for this this. All generated functions have a runtime argument which
1496/// holds a type info type to throw that each function takes and passes it
1497/// to the inner one if such a inner function exists. This type info type is
1498/// looked at by the generated throw function to see whether or not it should
1499/// throw a generated exception with the same type info type, or instead call
1500/// a supplied a function which in turn will throw a foreign exception.
1501/// @param module code for module instance
1502/// @param builder builder instance
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001503/// @param fpm a function pass manager holding optional IR to IR
Garrison Vennf4d2f842010-02-09 23:22:43 +00001504/// transformations
1505/// @param nativeThrowFunctName name of external function which will throw
1506/// a foreign exception
1507/// @returns outermost generated test function.
Chandler Carruth7ecd9912015-02-13 10:21:05 +00001508llvm::Function *
1509createUnwindExceptionTest(llvm::Module &module, llvm::IRBuilder<> &builder,
1510 llvm::legacy::FunctionPassManager &fpm,
1511 std::string nativeThrowFunctName) {
Chris Lattnera868bbb2011-04-08 18:02:51 +00001512 // Number of type infos to generate
1513 unsigned numTypeInfos = 6;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001514
Chris Lattnera868bbb2011-04-08 18:02:51 +00001515 // Initialze intrisics and external functions to use along with exception
1516 // and type info globals.
1517 createStandardUtilityFunctions(numTypeInfos,
1518 module,
1519 builder);
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001520 llvm::Function *nativeThrowFunct = module.getFunction(nativeThrowFunctName);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001521
1522 // Create exception throw function using the value ~0 to cause
Chris Lattnera868bbb2011-04-08 18:02:51 +00001523 // foreign exceptions to be thrown.
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001524 llvm::Function *throwFunct = createThrowExceptionFunction(module,
1525 builder,
1526 fpm,
1527 "throwFunct",
1528 ~0,
1529 *nativeThrowFunct);
Chris Lattnera868bbb2011-04-08 18:02:51 +00001530 // Inner function will catch even type infos
1531 unsigned innerExceptionTypesToCatch[] = {6, 2, 4};
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001532 size_t numExceptionTypesToCatch = sizeof(innerExceptionTypesToCatch) /
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001533 sizeof(unsigned);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001534
Chris Lattnera868bbb2011-04-08 18:02:51 +00001535 // Generate inner function.
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001536 llvm::Function *innerCatchFunct = createCatchWrappedInvokeFunction(module,
1537 builder,
1538 fpm,
1539 *throwFunct,
1540 "innerCatchFunct",
1541 numExceptionTypesToCatch,
1542 innerExceptionTypesToCatch);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001543
Chris Lattnera868bbb2011-04-08 18:02:51 +00001544 // Outer function will catch odd type infos
1545 unsigned outerExceptionTypesToCatch[] = {3, 1, 5};
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001546 numExceptionTypesToCatch = sizeof(outerExceptionTypesToCatch) /
Chris Lattnera868bbb2011-04-08 18:02:51 +00001547 sizeof(unsigned);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001548
Chris Lattnera868bbb2011-04-08 18:02:51 +00001549 // Generate outer function
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001550 llvm::Function *outerCatchFunct = createCatchWrappedInvokeFunction(module,
1551 builder,
1552 fpm,
1553 *innerCatchFunct,
1554 "outerCatchFunct",
1555 numExceptionTypesToCatch,
1556 outerExceptionTypesToCatch);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001557
Chris Lattnera868bbb2011-04-08 18:02:51 +00001558 // Return outer function to run
1559 return(outerCatchFunct);
Garrison Vennf4d2f842010-02-09 23:22:43 +00001560}
1561
Juergen Ributzka05c5a932013-11-19 03:08:35 +00001562namespace {
Garrison Vennf4d2f842010-02-09 23:22:43 +00001563/// Represents our foreign exceptions
1564class OurCppRunException : public std::runtime_error {
1565public:
Chris Lattnera868bbb2011-04-08 18:02:51 +00001566 OurCppRunException(const std::string reason) :
1567 std::runtime_error(reason) {}
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001568
Garrison Venn88bd9d62011-04-10 14:06:52 +00001569 OurCppRunException (const OurCppRunException &toCopy) :
Chris Lattnera868bbb2011-04-08 18:02:51 +00001570 std::runtime_error(toCopy) {}
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001571
Garrison Venn88bd9d62011-04-10 14:06:52 +00001572 OurCppRunException &operator = (const OurCppRunException &toCopy) {
Chris Lattnera868bbb2011-04-08 18:02:51 +00001573 return(reinterpret_cast<OurCppRunException&>(
1574 std::runtime_error::operator=(toCopy)));
1575 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001576
Alexander Kornienkof817c1c2015-04-11 02:11:45 +00001577 ~OurCppRunException(void) throw() override {}
Garrison Vennf4d2f842010-02-09 23:22:43 +00001578};
Juergen Ributzka05c5a932013-11-19 03:08:35 +00001579} // end anonymous namespace
Garrison Vennf4d2f842010-02-09 23:22:43 +00001580
1581/// Throws foreign C++ exception.
1582/// @param ignoreIt unused parameter that allows function to match implied
1583/// generated function contract.
1584extern "C"
1585void throwCppException (int32_t ignoreIt) {
Chris Lattnera868bbb2011-04-08 18:02:51 +00001586 throw(OurCppRunException("thrown by throwCppException(...)"));
Garrison Vennf4d2f842010-02-09 23:22:43 +00001587}
1588
1589typedef void (*OurExceptionThrowFunctType) (int32_t typeToThrow);
1590
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001591/// This is a test harness which runs test by executing generated
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001592/// function with a type info type to throw. Harness wraps the execution
Garrison Vennf4d2f842010-02-09 23:22:43 +00001593/// of generated function in a C++ try catch clause.
1594/// @param engine execution engine to use for executing generated function.
1595/// This demo program expects this to be a JIT instance for demo
1596/// purposes.
1597/// @param function generated test function to run
1598/// @param typeToThrow type info type of generated exception to throw, or
1599/// indicator to cause foreign exception to be thrown.
1600static
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001601void runExceptionThrow(llvm::ExecutionEngine *engine,
1602 llvm::Function *function,
Garrison Vennf4d2f842010-02-09 23:22:43 +00001603 int32_t typeToThrow) {
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001604
Chris Lattnera868bbb2011-04-08 18:02:51 +00001605 // Find test's function pointer
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001606 OurExceptionThrowFunctType functPtr =
Chris Lattnera868bbb2011-04-08 18:02:51 +00001607 reinterpret_cast<OurExceptionThrowFunctType>(
1608 reinterpret_cast<intptr_t>(engine->getPointerToFunction(function)));
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001609
Chris Lattnera868bbb2011-04-08 18:02:51 +00001610 try {
1611 // Run test
1612 (*functPtr)(typeToThrow);
1613 }
1614 catch (OurCppRunException exc) {
1615 // Catch foreign C++ exception
1616 fprintf(stderr,
1617 "\nrunExceptionThrow(...):In C++ catch OurCppRunException "
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001618 "with reason: %s.\n",
Chris Lattnera868bbb2011-04-08 18:02:51 +00001619 exc.what());
1620 }
1621 catch (...) {
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001622 // Catch all exceptions including our generated ones. This latter
Garrison Venn56c58ce2011-09-28 10:53:56 +00001623 // functionality works according to the example in rules 1.6.4 of
Dmitri Gribenko462462e2013-01-13 15:53:09 +00001624 // http://mentorembedded.github.com/cxx-abi/abi-eh.html (v1.22),
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001625 // given that these will be exceptions foreign to C++
1626 // (the _Unwind_Exception::exception_class should be different from
Garrison Venn56c58ce2011-09-28 10:53:56 +00001627 // the one used by C++).
Chris Lattnera868bbb2011-04-08 18:02:51 +00001628 fprintf(stderr,
1629 "\nrunExceptionThrow(...):In C++ catch all.\n");
1630 }
Garrison Vennf4d2f842010-02-09 23:22:43 +00001631}
1632
1633//
1634// End test functions
1635//
1636
Garrison Venn5fb3f662011-07-11 16:31:53 +00001637typedef llvm::ArrayRef<llvm::Type*> TypeArray;
Chris Lattnerca320f52011-04-08 17:56:47 +00001638
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001639/// This initialization routine creates type info globals and
Garrison Vennf4d2f842010-02-09 23:22:43 +00001640/// adds external function declarations to module.
1641/// @param numTypeInfos number of linear type info associated type info types
1642/// to create as GlobalVariable instances, starting with the value 1.
1643/// @param module code for module instance
1644/// @param builder builder instance
1645static void createStandardUtilityFunctions(unsigned numTypeInfos,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001646 llvm::Module &module,
Garrison Venn88bd9d62011-04-10 14:06:52 +00001647 llvm::IRBuilder<> &builder) {
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001648
Garrison Venn88bd9d62011-04-10 14:06:52 +00001649 llvm::LLVMContext &context = module.getContext();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001650
Chris Lattnera868bbb2011-04-08 18:02:51 +00001651 // Exception initializations
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001652
Chris Lattnera868bbb2011-04-08 18:02:51 +00001653 // Setup exception catch state
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001654 ourExceptionNotThrownState =
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001655 llvm::ConstantInt::get(llvm::Type::getInt8Ty(context), 0),
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001656 ourExceptionThrownState =
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001657 llvm::ConstantInt::get(llvm::Type::getInt8Ty(context), 1),
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001658 ourExceptionCaughtState =
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001659 llvm::ConstantInt::get(llvm::Type::getInt8Ty(context), 2),
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001660
1661
1662
Chris Lattnera868bbb2011-04-08 18:02:51 +00001663 // Create our type info type
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001664 ourTypeInfoType = llvm::StructType::get(context,
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001665 TypeArray(builder.getInt32Ty()));
Garrison Venn8cb00352011-09-22 15:45:14 +00001666
Garrison Venn8cb00352011-09-22 15:45:14 +00001667 llvm::Type *caughtResultFieldTypes[] = {
1668 builder.getInt8PtrTy(),
1669 builder.getInt32Ty()
1670 };
1671
1672 // Create our landingpad result type
1673 ourCaughtResultType = llvm::StructType::get(context,
1674 TypeArray(caughtResultFieldTypes));
1675
Chris Lattnera868bbb2011-04-08 18:02:51 +00001676 // Create OurException type
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001677 ourExceptionType = llvm::StructType::get(context,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001678 TypeArray(ourTypeInfoType));
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001679
Chris Lattnera868bbb2011-04-08 18:02:51 +00001680 // Create portion of _Unwind_Exception type
1681 //
1682 // Note: Declaring only a portion of the _Unwind_Exception struct.
1683 // Does this cause problems?
1684 ourUnwindExceptionType =
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001685 llvm::StructType::get(context,
Garrison Venn76310ac2011-07-12 15:34:42 +00001686 TypeArray(builder.getInt64Ty()));
Garrison Venn5fb3f662011-07-11 16:31:53 +00001687
Chris Lattnera868bbb2011-04-08 18:02:51 +00001688 struct OurBaseException_t dummyException;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001689
Chris Lattnera868bbb2011-04-08 18:02:51 +00001690 // Calculate offset of OurException::unwindException member.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001691 ourBaseFromUnwindOffset = ((uintptr_t) &dummyException) -
Garrison Venn0a2eb8d2011-09-23 14:45:10 +00001692 ((uintptr_t) &(dummyException.unwindException));
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001693
Garrison Vennf4d2f842010-02-09 23:22:43 +00001694#ifdef DEBUG
Chris Lattnera868bbb2011-04-08 18:02:51 +00001695 fprintf(stderr,
1696 "createStandardUtilityFunctions(...):ourBaseFromUnwindOffset "
1697 "= %lld, sizeof(struct OurBaseException_t) - "
1698 "sizeof(struct _Unwind_Exception) = %lu.\n",
1699 ourBaseFromUnwindOffset,
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001700 sizeof(struct OurBaseException_t) -
Chris Lattnera868bbb2011-04-08 18:02:51 +00001701 sizeof(struct _Unwind_Exception));
Garrison Vennf4d2f842010-02-09 23:22:43 +00001702#endif
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001703
Chris Lattnera868bbb2011-04-08 18:02:51 +00001704 size_t numChars = sizeof(ourBaseExcpClassChars) / sizeof(char);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001705
Chris Lattnera868bbb2011-04-08 18:02:51 +00001706 // Create our _Unwind_Exception::exception_class value
1707 ourBaseExceptionClass = genClass(ourBaseExcpClassChars, numChars);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001708
Chris Lattnera868bbb2011-04-08 18:02:51 +00001709 // Type infos
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001710
Chris Lattnera868bbb2011-04-08 18:02:51 +00001711 std::string baseStr = "typeInfo", typeInfoName;
1712 std::ostringstream typeInfoNameBuilder;
1713 std::vector<llvm::Constant*> structVals;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001714
Chris Lattnera868bbb2011-04-08 18:02:51 +00001715 llvm::Constant *nextStruct;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001716
Chris Lattnera868bbb2011-04-08 18:02:51 +00001717 // Generate each type info
1718 //
1719 // Note: First type info is not used.
1720 for (unsigned i = 0; i <= numTypeInfos; ++i) {
1721 structVals.clear();
1722 structVals.push_back(llvm::ConstantInt::get(builder.getInt32Ty(), i));
1723 nextStruct = llvm::ConstantStruct::get(ourTypeInfoType, structVals);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001724
Chris Lattnera868bbb2011-04-08 18:02:51 +00001725 typeInfoNameBuilder.str("");
1726 typeInfoNameBuilder << baseStr << i;
1727 typeInfoName = typeInfoNameBuilder.str();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001728
Chris Lattnera868bbb2011-04-08 18:02:51 +00001729 // Note: Does not seem to work without allocation
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001730 new llvm::GlobalVariable(module,
1731 ourTypeInfoType,
1732 true,
1733 llvm::GlobalValue::ExternalLinkage,
1734 nextStruct,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001735 typeInfoName);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001736
Chris Lattnera868bbb2011-04-08 18:02:51 +00001737 ourTypeInfoNames.push_back(typeInfoName);
1738 ourTypeInfoNamesIndex[i] = typeInfoName;
1739 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001740
Chris Lattnera868bbb2011-04-08 18:02:51 +00001741 ArgNames argNames;
1742 ArgTypes argTypes;
Garrison Venn88bd9d62011-04-10 14:06:52 +00001743 llvm::Function *funct = NULL;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001744
Chris Lattnera868bbb2011-04-08 18:02:51 +00001745 // print32Int
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001746
Chris Lattner6b8eb8c2011-07-18 04:52:09 +00001747 llvm::Type *retType = builder.getVoidTy();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001748
Chris Lattnera868bbb2011-04-08 18:02:51 +00001749 argTypes.clear();
Garrison Venn76310ac2011-07-12 15:34:42 +00001750 argTypes.push_back(builder.getInt32Ty());
1751 argTypes.push_back(builder.getInt8PtrTy());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001752
Chris Lattnera868bbb2011-04-08 18:02:51 +00001753 argNames.clear();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001754
1755 createFunction(module,
1756 retType,
1757 argTypes,
1758 argNames,
1759 "print32Int",
1760 llvm::Function::ExternalLinkage,
1761 true,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001762 false);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001763
Chris Lattnera868bbb2011-04-08 18:02:51 +00001764 // print64Int
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001765
Chris Lattnera868bbb2011-04-08 18:02:51 +00001766 retType = builder.getVoidTy();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001767
Chris Lattnera868bbb2011-04-08 18:02:51 +00001768 argTypes.clear();
Garrison Venn76310ac2011-07-12 15:34:42 +00001769 argTypes.push_back(builder.getInt64Ty());
1770 argTypes.push_back(builder.getInt8PtrTy());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001771
Chris Lattnera868bbb2011-04-08 18:02:51 +00001772 argNames.clear();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001773
1774 createFunction(module,
1775 retType,
1776 argTypes,
1777 argNames,
1778 "print64Int",
1779 llvm::Function::ExternalLinkage,
1780 true,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001781 false);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001782
Chris Lattnera868bbb2011-04-08 18:02:51 +00001783 // printStr
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001784
Chris Lattnera868bbb2011-04-08 18:02:51 +00001785 retType = builder.getVoidTy();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001786
Chris Lattnera868bbb2011-04-08 18:02:51 +00001787 argTypes.clear();
Garrison Venn76310ac2011-07-12 15:34:42 +00001788 argTypes.push_back(builder.getInt8PtrTy());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001789
Chris Lattnera868bbb2011-04-08 18:02:51 +00001790 argNames.clear();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001791
1792 createFunction(module,
1793 retType,
1794 argTypes,
1795 argNames,
1796 "printStr",
1797 llvm::Function::ExternalLinkage,
1798 true,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001799 false);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001800
Chris Lattnera868bbb2011-04-08 18:02:51 +00001801 // throwCppException
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001802
Chris Lattnera868bbb2011-04-08 18:02:51 +00001803 retType = builder.getVoidTy();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001804
Chris Lattnera868bbb2011-04-08 18:02:51 +00001805 argTypes.clear();
Garrison Venn76310ac2011-07-12 15:34:42 +00001806 argTypes.push_back(builder.getInt32Ty());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001807
Chris Lattnera868bbb2011-04-08 18:02:51 +00001808 argNames.clear();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001809
1810 createFunction(module,
1811 retType,
1812 argTypes,
1813 argNames,
1814 "throwCppException",
1815 llvm::Function::ExternalLinkage,
1816 true,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001817 false);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001818
Chris Lattnera868bbb2011-04-08 18:02:51 +00001819 // deleteOurException
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001820
Chris Lattnera868bbb2011-04-08 18:02:51 +00001821 retType = builder.getVoidTy();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001822
Chris Lattnera868bbb2011-04-08 18:02:51 +00001823 argTypes.clear();
Garrison Venn76310ac2011-07-12 15:34:42 +00001824 argTypes.push_back(builder.getInt8PtrTy());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001825
Chris Lattnera868bbb2011-04-08 18:02:51 +00001826 argNames.clear();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001827
1828 createFunction(module,
1829 retType,
1830 argTypes,
1831 argNames,
1832 "deleteOurException",
1833 llvm::Function::ExternalLinkage,
1834 true,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001835 false);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001836
Chris Lattnera868bbb2011-04-08 18:02:51 +00001837 // createOurException
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001838
Garrison Venn76310ac2011-07-12 15:34:42 +00001839 retType = builder.getInt8PtrTy();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001840
Chris Lattnera868bbb2011-04-08 18:02:51 +00001841 argTypes.clear();
Garrison Venn76310ac2011-07-12 15:34:42 +00001842 argTypes.push_back(builder.getInt32Ty());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001843
Chris Lattnera868bbb2011-04-08 18:02:51 +00001844 argNames.clear();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001845
1846 createFunction(module,
1847 retType,
1848 argTypes,
1849 argNames,
1850 "createOurException",
1851 llvm::Function::ExternalLinkage,
1852 true,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001853 false);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001854
Chris Lattnera868bbb2011-04-08 18:02:51 +00001855 // _Unwind_RaiseException
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001856
Chris Lattnera868bbb2011-04-08 18:02:51 +00001857 retType = builder.getInt32Ty();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001858
Chris Lattnera868bbb2011-04-08 18:02:51 +00001859 argTypes.clear();
Garrison Venn76310ac2011-07-12 15:34:42 +00001860 argTypes.push_back(builder.getInt8PtrTy());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001861
Chris Lattnera868bbb2011-04-08 18:02:51 +00001862 argNames.clear();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001863
1864 funct = createFunction(module,
1865 retType,
1866 argTypes,
1867 argNames,
1868 "_Unwind_RaiseException",
1869 llvm::Function::ExternalLinkage,
1870 true,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001871 false);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001872
NAKAMURA Takumi8489be82012-10-12 14:11:48 +00001873 funct->setDoesNotReturn();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001874
Chris Lattnera868bbb2011-04-08 18:02:51 +00001875 // _Unwind_Resume
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001876
Chris Lattnera868bbb2011-04-08 18:02:51 +00001877 retType = builder.getInt32Ty();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001878
Chris Lattnera868bbb2011-04-08 18:02:51 +00001879 argTypes.clear();
Garrison Venn76310ac2011-07-12 15:34:42 +00001880 argTypes.push_back(builder.getInt8PtrTy());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001881
Chris Lattnera868bbb2011-04-08 18:02:51 +00001882 argNames.clear();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001883
1884 funct = createFunction(module,
1885 retType,
1886 argTypes,
1887 argNames,
1888 "_Unwind_Resume",
1889 llvm::Function::ExternalLinkage,
1890 true,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001891 false);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001892
NAKAMURA Takumi8489be82012-10-12 14:11:48 +00001893 funct->setDoesNotReturn();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001894
Chris Lattnera868bbb2011-04-08 18:02:51 +00001895 // ourPersonality
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001896
Chris Lattnera868bbb2011-04-08 18:02:51 +00001897 retType = builder.getInt32Ty();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001898
Chris Lattnera868bbb2011-04-08 18:02:51 +00001899 argTypes.clear();
Garrison Venn76310ac2011-07-12 15:34:42 +00001900 argTypes.push_back(builder.getInt32Ty());
1901 argTypes.push_back(builder.getInt32Ty());
1902 argTypes.push_back(builder.getInt64Ty());
1903 argTypes.push_back(builder.getInt8PtrTy());
1904 argTypes.push_back(builder.getInt8PtrTy());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001905
Chris Lattnera868bbb2011-04-08 18:02:51 +00001906 argNames.clear();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001907
1908 createFunction(module,
1909 retType,
1910 argTypes,
1911 argNames,
1912 "ourPersonality",
1913 llvm::Function::ExternalLinkage,
1914 true,
Chris Lattnera868bbb2011-04-08 18:02:51 +00001915 false);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001916
Chris Lattnera868bbb2011-04-08 18:02:51 +00001917 // llvm.eh.typeid.for intrinsic
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001918
Chris Lattnera868bbb2011-04-08 18:02:51 +00001919 getDeclaration(&module, llvm::Intrinsic::eh_typeid_for);
Garrison Vennf4d2f842010-02-09 23:22:43 +00001920}
1921
1922
Chris Lattnera868bbb2011-04-08 18:02:51 +00001923//===----------------------------------------------------------------------===//
Garrison Vennf4d2f842010-02-09 23:22:43 +00001924// Main test driver code.
Chris Lattnera868bbb2011-04-08 18:02:51 +00001925//===----------------------------------------------------------------------===//
Garrison Vennf4d2f842010-02-09 23:22:43 +00001926
1927/// Demo main routine which takes the type info types to throw. A test will
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001928/// be run for each given type info type. While type info types with the value
Garrison Vennf4d2f842010-02-09 23:22:43 +00001929/// of -1 will trigger a foreign C++ exception to be thrown; type info types
1930/// <= 6 and >= 1 will be caught by test functions; and type info types > 6
1931/// will result in exceptions which pass through to the test harness. All other
1932/// type info types are not supported and could cause a crash.
Garrison Venn88bd9d62011-04-10 14:06:52 +00001933int main(int argc, char *argv[]) {
Chris Lattnera868bbb2011-04-08 18:02:51 +00001934 if (argc == 1) {
1935 fprintf(stderr,
1936 "\nUsage: ExceptionDemo <exception type to throw> "
1937 "[<type 2>...<type n>].\n"
1938 " Each type must have the value of 1 - 6 for "
1939 "generated exceptions to be caught;\n"
1940 " the value -1 for foreign C++ exceptions to be "
1941 "generated and thrown;\n"
1942 " or the values > 6 for exceptions to be ignored.\n"
1943 "\nTry: ExceptionDemo 2 3 7 -1\n"
1944 " for a full test.\n\n");
1945 return(0);
1946 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001947
Chris Lattnera868bbb2011-04-08 18:02:51 +00001948 // If not set, exception handling will not be turned on
Peter Collingbournedff24782011-12-07 23:58:57 +00001949 llvm::TargetOptions Opts;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001950
Chris Lattnera868bbb2011-04-08 18:02:51 +00001951 llvm::InitializeNativeTarget();
Rafael Espindolae93dc3b2013-05-05 20:57:58 +00001952 llvm::InitializeNativeTargetAsmPrinter();
Garrison Venn88bd9d62011-04-10 14:06:52 +00001953 llvm::LLVMContext &context = llvm::getGlobalContext();
Chris Lattnera868bbb2011-04-08 18:02:51 +00001954 llvm::IRBuilder<> theBuilder(context);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001955
Chris Lattnera868bbb2011-04-08 18:02:51 +00001956 // Make the module, which holds all the code.
Rafael Espindola2a8a2792014-08-19 04:04:25 +00001957 std::unique_ptr<llvm::Module> Owner =
1958 llvm::make_unique<llvm::Module>("my cool jit", context);
1959 llvm::Module *module = Owner.get();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001960
NAKAMURA Takumi4c71cc12014-12-03 02:05:51 +00001961 std::unique_ptr<llvm::RTDyldMemoryManager> MemMgr(new llvm::SectionMemoryManager());
Rafael Espindolae93dc3b2013-05-05 20:57:58 +00001962
Chris Lattnera868bbb2011-04-08 18:02:51 +00001963 // Build engine with JIT
Rafael Espindola2a8a2792014-08-19 04:04:25 +00001964 llvm::EngineBuilder factory(std::move(Owner));
Chris Lattnera868bbb2011-04-08 18:02:51 +00001965 factory.setEngineKind(llvm::EngineKind::JIT);
Peter Collingbournedff24782011-12-07 23:58:57 +00001966 factory.setTargetOptions(Opts);
NAKAMURA Takumi4c71cc12014-12-03 02:05:51 +00001967 factory.setMCJITMemoryManager(std::move(MemMgr));
Garrison Venn88bd9d62011-04-10 14:06:52 +00001968 llvm::ExecutionEngine *executionEngine = factory.create();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001969
Chris Lattnera868bbb2011-04-08 18:02:51 +00001970 {
Chandler Carruth7ecd9912015-02-13 10:21:05 +00001971 llvm::legacy::FunctionPassManager fpm(module);
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001972
1973 // Set up the optimizer pipeline.
Chris Lattnera868bbb2011-04-08 18:02:51 +00001974 // Start with registering info about how the
1975 // target lays out data structures.
David Blaikie3b20b042015-08-14 00:24:56 +00001976 module->setDataLayout(executionEngine->getDataLayout());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001977
Chris Lattnera868bbb2011-04-08 18:02:51 +00001978 // Optimizations turned on
1979#ifdef ADD_OPT_PASSES
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001980
Chris Lattnera868bbb2011-04-08 18:02:51 +00001981 // Basic AliasAnslysis support for GVN.
1982 fpm.add(llvm::createBasicAliasAnalysisPass());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001983
Chris Lattnera868bbb2011-04-08 18:02:51 +00001984 // Promote allocas to registers.
1985 fpm.add(llvm::createPromoteMemoryToRegisterPass());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001986
Chris Lattnera868bbb2011-04-08 18:02:51 +00001987 // Do simple "peephole" optimizations and bit-twiddling optzns.
1988 fpm.add(llvm::createInstructionCombiningPass());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001989
Chris Lattnera868bbb2011-04-08 18:02:51 +00001990 // Reassociate expressions.
1991 fpm.add(llvm::createReassociatePass());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001992
Chris Lattnera868bbb2011-04-08 18:02:51 +00001993 // Eliminate Common SubExpressions.
1994 fpm.add(llvm::createGVNPass());
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00001995
1996 // Simplify the control flow graph (deleting unreachable
Chris Lattnera868bbb2011-04-08 18:02:51 +00001997 // blocks, etc).
1998 fpm.add(llvm::createCFGSimplificationPass());
1999#endif // ADD_OPT_PASSES
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00002000
Chris Lattnera868bbb2011-04-08 18:02:51 +00002001 fpm.doInitialization();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00002002
Chris Lattnera868bbb2011-04-08 18:02:51 +00002003 // Generate test code using function throwCppException(...) as
2004 // the function which throws foreign exceptions.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00002005 llvm::Function *toRun =
2006 createUnwindExceptionTest(*module,
2007 theBuilder,
Garrison Venn8cb00352011-09-22 15:45:14 +00002008 fpm,
2009 "throwCppException");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00002010
Rafael Espindolae93dc3b2013-05-05 20:57:58 +00002011 executionEngine->finalizeObject();
2012
Chris Lattnera868bbb2011-04-08 18:02:51 +00002013 fprintf(stderr, "\nBegin module dump:\n\n");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00002014
Chris Lattnera868bbb2011-04-08 18:02:51 +00002015 module->dump();
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00002016
Chris Lattnera868bbb2011-04-08 18:02:51 +00002017 fprintf(stderr, "\nEnd module dump:\n");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00002018
Chris Lattnera868bbb2011-04-08 18:02:51 +00002019 fprintf(stderr, "\n\nBegin Test:\n");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00002020
Chris Lattnera868bbb2011-04-08 18:02:51 +00002021 for (int i = 1; i < argc; ++i) {
2022 // Run test for each argument whose value is the exception
2023 // type to throw.
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00002024 runExceptionThrow(executionEngine,
2025 toRun,
Chris Lattnera868bbb2011-04-08 18:02:51 +00002026 (unsigned) strtoul(argv[i], NULL, 10));
2027 }
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00002028
Chris Lattnera868bbb2011-04-08 18:02:51 +00002029 fprintf(stderr, "\nEnd Test:\n\n");
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00002030 }
2031
Chris Lattnera868bbb2011-04-08 18:02:51 +00002032 delete executionEngine;
NAKAMURA Takumi8e70dd52012-10-12 14:11:43 +00002033
Chris Lattnera868bbb2011-04-08 18:02:51 +00002034 return 0;
Garrison Vennf4d2f842010-02-09 23:22:43 +00002035}