blob: a3182d2087fc05601dc1ed8933dd7e1fee8df5b0 [file] [log] [blame]
Chris Lattner626ab1c2011-04-08 18:02:51 +00001//===-- ExceptionDemo.cpp - An example using llvm Exceptions --------------===//
Garrison Venna2c2f1a2010-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 Lattner626ab1c2011-04-08 18:02:51 +00008//===----------------------------------------------------------------------===//
Garrison Venna2c2f1a2010-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 Takumi9f469a02012-10-12 14:11:43 +000013// be run for each given type info type. While type info types with the value
Garrison Venna2c2f1a2010-02-09 23:22:43 +000014// of -1 will trigger a foreign C++ exception to be thrown; type info types
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +000015// <= 6 and >= 1 will cause the associated generated exceptions to be thrown
Garrison Venna2c2f1a2010-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 Takumi9f469a02012-10-12 14:11:43 +000019// the "finally" blocks of every generated test functions will executed
Garrison Venna2c2f1a2010-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 Takumi9f469a02012-10-12 14:11:43 +000028//
Garrison Venna2c2f1a2010-02-09 23:22:43 +000029// ExceptionDemo 2 3 7 -1
30//
31// results in the following cases:
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +000032// - Value 2 causes an exception with a type info type of 2 to be
Garrison Venna2c2f1a2010-02-09 23:22:43 +000033// thrown and caught by an inner generated test function.
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +000034// - Value 3 causes an exception with a type info type of 3 to be
Garrison Venna2c2f1a2010-02-09 23:22:43 +000035// thrown and caught by an outer generated test function.
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +000036// - Value 7 causes an exception with a type info type of 7 to be
Garrison Venna2c2f1a2010-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 Takumi9f469a02012-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
Vlad Tsyrklevich30b47382017-09-12 00:19:11 +000044// http://itanium-cxx-abi.github.io/cxx-abi/abi-eh.html (v1.22)
Garrison Venna2c2f1a2010-02-09 23:22:43 +000045//
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +000046// This code uses code from the llvm compiler-rt project and the llvm
Garrison Venna2c2f1a2010-02-09 23:22:43 +000047// Kaleidoscope project.
48//
Chris Lattner626ab1c2011-04-08 18:02:51 +000049//===----------------------------------------------------------------------===//
Garrison Venna2c2f1a2010-02-09 23:22:43 +000050
NAKAMURA Takumia757af92015-03-02 01:04:34 +000051#include "llvm/ADT/STLExtras.h"
Zachary Turner19ca2b02017-06-07 03:48:56 +000052#include "llvm/BinaryFormat/Dwarf.h"
Rafael Espindolac8b95512013-05-05 20:57:58 +000053#include "llvm/ExecutionEngine/MCJIT.h"
54#include "llvm/ExecutionEngine/SectionMemoryManager.h"
Chandler Carruth0a084602013-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 Carruth417c5c12015-02-13 10:01:29 +000060#include "llvm/IR/LegacyPassManager.h"
Chandler Carruth0a084602013-01-02 11:56:33 +000061#include "llvm/IR/Module.h"
Zachary Turner19ca2b02017-06-07 03:48:56 +000062#include "llvm/IR/Verifier.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000063#include "llvm/Support/TargetSelect.h"
Chandler Carruth4ca7e092012-12-04 10:16:57 +000064#include "llvm/Target/TargetOptions.h"
65#include "llvm/Transforms/Scalar.h"
Garrison Venna2c2f1a2010-02-09 23:22:43 +000066
NAKAMURA Takumi9f469a02012-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 Venn18bba842011-04-12 12:30:10 +000069// for stderr, and fprintf, and the addition of this include fixed the
NAKAMURA Takumi9f469a02012-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 Venn18bba842011-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 Venn2a7d4ad2011-04-11 19:52:49 +000075#include <cstdio>
Garrison Venn18bba842011-04-12 12:30:10 +000076
Garrison Venna2c2f1a2010-02-09 23:22:43 +000077#include <sstream>
Garrison Venna2c2f1a2010-02-09 23:22:43 +000078#include <stdexcept>
79
David Blaikie54b3a032015-08-14 00:31:49 +000080#include <inttypes.h>
Garrison Venna2c2f1a2010-02-09 23:22:43 +000081
Saleem Abdulrasool308b0462016-11-20 02:36:36 +000082#include <unwind.h>
83
Garrison Venna2c2f1a2010-02-09 23:22:43 +000084#ifndef USE_GLOBAL_STR_CONSTS
85#define USE_GLOBAL_STR_CONSTS true
86#endif
87
Garrison Venna2c2f1a2010-02-09 23:22:43 +000088//
89// Example types
90//
91
92/// This is our simplistic type info
93struct OurExceptionType_t {
Chris Lattner626ab1c2011-04-08 18:02:51 +000094 /// type info type
95 int type;
Garrison Venna2c2f1a2010-02-09 23:22:43 +000096};
97
98
99/// This is our Exception class which relies on a negative offset to calculate
100/// pointers to its instances from pointers to its unwindException member.
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000101///
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000102/// Note: The above unwind.h defines struct _Unwind_Exception to be aligned
103/// on a double word boundary. This is necessary to match the standard:
Vlad Tsyrklevich30b47382017-09-12 00:19:11 +0000104/// http://itanium-cxx-abi.github.io/cxx-abi/abi-eh.html
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000105struct OurBaseException_t {
Chris Lattner626ab1c2011-04-08 18:02:51 +0000106 struct OurExceptionType_t type;
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000107
Chris Lattner626ab1c2011-04-08 18:02:51 +0000108 // Note: This is properly aligned in unwind.h
109 struct _Unwind_Exception unwindException;
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000110};
111
112
113// Note: Not needed since we are C++
114typedef struct OurBaseException_t OurException;
115typedef struct _Unwind_Exception OurUnwindException;
116
117//
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000118// Various globals used to support typeinfo and generatted exceptions in
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000119// general
120//
121
122static std::map<std::string, llvm::Value*> namedValues;
123
124int64_t ourBaseFromUnwindOffset;
125
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000126const unsigned char ourBaseExcpClassChars[] =
Chris Lattner626ab1c2011-04-08 18:02:51 +0000127{'o', 'b', 'j', '\0', 'b', 'a', 's', '\0'};
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000128
129
130static uint64_t ourBaseExceptionClass = 0;
131
132static std::vector<std::string> ourTypeInfoNames;
133static std::map<int, std::string> ourTypeInfoNamesIndex;
134
Garrison Venn64cfcef2011-04-10 14:06:52 +0000135static llvm::StructType *ourTypeInfoType;
Garrison Venn85500712011-09-22 15:45:14 +0000136static llvm::StructType *ourCaughtResultType;
Garrison Venn64cfcef2011-04-10 14:06:52 +0000137static llvm::StructType *ourExceptionType;
138static llvm::StructType *ourUnwindExceptionType;
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000139
Garrison Venn64cfcef2011-04-10 14:06:52 +0000140static llvm::ConstantInt *ourExceptionNotThrownState;
141static llvm::ConstantInt *ourExceptionThrownState;
142static llvm::ConstantInt *ourExceptionCaughtState;
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000143
144typedef std::vector<std::string> ArgNames;
Garrison Venn6e6cdd02011-07-11 16:31:53 +0000145typedef std::vector<llvm::Type*> ArgTypes;
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000146
147//
148// Code Generation Utilities
149//
150
151/// Utility used to create a function, both declarations and definitions
152/// @param module for module instance
153/// @param retType function return type
154/// @param theArgTypes function's ordered argument types
155/// @param theArgNames function's ordered arguments needed if use of this
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000156/// function corresponds to a function definition. Use empty
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000157/// aggregate for function declarations.
158/// @param functName function name
159/// @param linkage function linkage
160/// @param declarationOnly for function declarations
161/// @param isVarArg function uses vararg arguments
162/// @returns function instance
Garrison Venn64cfcef2011-04-10 14:06:52 +0000163llvm::Function *createFunction(llvm::Module &module,
Chris Lattner77613d42011-07-18 04:52:09 +0000164 llvm::Type *retType,
Garrison Venn64cfcef2011-04-10 14:06:52 +0000165 const ArgTypes &theArgTypes,
166 const ArgNames &theArgNames,
167 const std::string &functName,
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000168 llvm::GlobalValue::LinkageTypes linkage,
169 bool declarationOnly,
170 bool isVarArg) {
Chris Lattner626ab1c2011-04-08 18:02:51 +0000171 llvm::FunctionType *functType =
172 llvm::FunctionType::get(retType, theArgTypes, isVarArg);
173 llvm::Function *ret =
174 llvm::Function::Create(functType, linkage, functName, &module);
175 if (!ret || declarationOnly)
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000176 return(ret);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000177
Chris Lattner626ab1c2011-04-08 18:02:51 +0000178 namedValues.clear();
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000179 unsigned i = 0;
Chris Lattner626ab1c2011-04-08 18:02:51 +0000180 for (llvm::Function::arg_iterator argIndex = ret->arg_begin();
181 i != theArgNames.size();
182 ++argIndex, ++i) {
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000183
Chris Lattner626ab1c2011-04-08 18:02:51 +0000184 argIndex->setName(theArgNames[i]);
185 namedValues[theArgNames[i]] = argIndex;
186 }
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000187
Chris Lattner626ab1c2011-04-08 18:02:51 +0000188 return(ret);
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000189}
190
191
192/// Create an alloca instruction in the entry block of
193/// the parent function. This is used for mutable variables etc.
194/// @param function parent instance
195/// @param varName stack variable name
196/// @param type stack variable type
197/// @param initWith optional constant initialization value
198/// @returns AllocaInst instance
Garrison Venn64cfcef2011-04-10 14:06:52 +0000199static llvm::AllocaInst *createEntryBlockAlloca(llvm::Function &function,
Chris Lattner626ab1c2011-04-08 18:02:51 +0000200 const std::string &varName,
Chris Lattner77613d42011-07-18 04:52:09 +0000201 llvm::Type *type,
Garrison Venn64cfcef2011-04-10 14:06:52 +0000202 llvm::Constant *initWith = 0) {
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000203 llvm::BasicBlock &block = function.getEntryBlock();
Chris Lattner626ab1c2011-04-08 18:02:51 +0000204 llvm::IRBuilder<> tmp(&block, block.begin());
Malcolm Parsons4c127322016-11-02 16:43:50 +0000205 llvm::AllocaInst *ret = tmp.CreateAlloca(type, 0, varName);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000206
207 if (initWith)
Chris Lattner626ab1c2011-04-08 18:02:51 +0000208 tmp.CreateStore(initWith, ret);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000209
Chris Lattner626ab1c2011-04-08 18:02:51 +0000210 return(ret);
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000211}
212
213
214//
215// Code Generation Utilities End
216//
217
218//
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000219// Runtime C Library functions
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000220//
221
Saleem Abdulrasool802a1be2016-11-20 02:36:38 +0000222namespace {
223template <typename Type_>
224uintptr_t ReadType(const uint8_t *&p) {
225 Type_ value;
226 memcpy(&value, p, sizeof(Type_));
227 p += sizeof(Type_);
228 return static_cast<uintptr_t>(value);
229}
230}
231
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000232// Note: using an extern "C" block so that static functions can be used
233extern "C" {
234
235// Note: Better ways to decide on bit width
236//
237/// Prints a 32 bit number, according to the format, to stderr.
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000238/// @param intToPrint integer to print
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000239/// @param format printf like format to use when printing
Garrison Venn64cfcef2011-04-10 14:06:52 +0000240void print32Int(int intToPrint, const char *format) {
Chris Lattner626ab1c2011-04-08 18:02:51 +0000241 if (format) {
242 // Note: No NULL check
243 fprintf(stderr, format, intToPrint);
244 }
245 else {
246 // Note: No NULL check
247 fprintf(stderr, "::print32Int(...):NULL arg.\n");
248 }
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000249}
250
251
252// Note: Better ways to decide on bit width
253//
254/// Prints a 64 bit number, according to the format, to stderr.
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000255/// @param intToPrint integer to print
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000256/// @param format printf like format to use when printing
Garrison Venn64cfcef2011-04-10 14:06:52 +0000257void print64Int(long int intToPrint, const char *format) {
Chris Lattner626ab1c2011-04-08 18:02:51 +0000258 if (format) {
259 // Note: No NULL check
260 fprintf(stderr, format, intToPrint);
261 }
262 else {
263 // Note: No NULL check
264 fprintf(stderr, "::print64Int(...):NULL arg.\n");
265 }
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000266}
267
268
269/// Prints a C string to stderr
270/// @param toPrint string to print
Garrison Venn64cfcef2011-04-10 14:06:52 +0000271void printStr(char *toPrint) {
Chris Lattner626ab1c2011-04-08 18:02:51 +0000272 if (toPrint) {
273 fprintf(stderr, "%s", toPrint);
274 }
275 else {
276 fprintf(stderr, "::printStr(...):NULL arg.\n");
277 }
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000278}
279
280
Luke Larson204847a2015-09-18 21:15:45 +0000281/// Deletes the true previously allocated exception whose address
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000282/// is calculated from the supplied OurBaseException_t::unwindException
283/// member address. Handles (ignores), NULL pointers.
284/// @param expToDelete exception to delete
Garrison Venn64cfcef2011-04-10 14:06:52 +0000285void deleteOurException(OurUnwindException *expToDelete) {
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000286#ifdef DEBUG
Chris Lattner626ab1c2011-04-08 18:02:51 +0000287 fprintf(stderr,
288 "deleteOurException(...).\n");
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000289#endif
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000290
Chris Lattner626ab1c2011-04-08 18:02:51 +0000291 if (expToDelete &&
292 (expToDelete->exception_class == ourBaseExceptionClass)) {
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000293
Chris Lattner626ab1c2011-04-08 18:02:51 +0000294 free(((char*) expToDelete) + ourBaseFromUnwindOffset);
295 }
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000296}
297
298
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000299/// This function is the struct _Unwind_Exception API mandated delete function
300/// used by foreign exception handlers when deleting our exception
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000301/// (OurException), instances.
Vlad Tsyrklevich30b47382017-09-12 00:19:11 +0000302/// @param reason See @link http://itanium-cxx-abi.github.io/cxx-abi/abi-eh.html
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000303/// @unlink
304/// @param expToDelete exception instance to delete
305void deleteFromUnwindOurException(_Unwind_Reason_Code reason,
Garrison Venn64cfcef2011-04-10 14:06:52 +0000306 OurUnwindException *expToDelete) {
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000307#ifdef DEBUG
Chris Lattner626ab1c2011-04-08 18:02:51 +0000308 fprintf(stderr,
309 "deleteFromUnwindOurException(...).\n");
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000310#endif
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000311
Chris Lattner626ab1c2011-04-08 18:02:51 +0000312 deleteOurException(expToDelete);
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000313}
314
315
316/// Creates (allocates on the heap), an exception (OurException instance),
317/// of the supplied type info type.
318/// @param type type info type
Garrison Venn64cfcef2011-04-10 14:06:52 +0000319OurUnwindException *createOurException(int type) {
Chris Lattner626ab1c2011-04-08 18:02:51 +0000320 size_t size = sizeof(OurException);
Garrison Venn64cfcef2011-04-10 14:06:52 +0000321 OurException *ret = (OurException*) memset(malloc(size), 0, size);
Chris Lattner626ab1c2011-04-08 18:02:51 +0000322 (ret->type).type = type;
323 (ret->unwindException).exception_class = ourBaseExceptionClass;
324 (ret->unwindException).exception_cleanup = deleteFromUnwindOurException;
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000325
Chris Lattner626ab1c2011-04-08 18:02:51 +0000326 return(&(ret->unwindException));
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000327}
328
329
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000330/// Read a uleb128 encoded value and advance pointer
331/// See Variable Length Data in:
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000332/// @link http://dwarfstd.org/Dwarf3.pdf @unlink
333/// @param data reference variable holding memory pointer to decode from
334/// @returns decoded value
Garrison Venn64cfcef2011-04-10 14:06:52 +0000335static uintptr_t readULEB128(const uint8_t **data) {
Chris Lattner626ab1c2011-04-08 18:02:51 +0000336 uintptr_t result = 0;
337 uintptr_t shift = 0;
338 unsigned char byte;
Garrison Venn64cfcef2011-04-10 14:06:52 +0000339 const uint8_t *p = *data;
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000340
Chris Lattner626ab1c2011-04-08 18:02:51 +0000341 do {
342 byte = *p++;
343 result |= (byte & 0x7f) << shift;
344 shift += 7;
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000345 }
Chris Lattner626ab1c2011-04-08 18:02:51 +0000346 while (byte & 0x80);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000347
Chris Lattner626ab1c2011-04-08 18:02:51 +0000348 *data = p;
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000349
Chris Lattner626ab1c2011-04-08 18:02:51 +0000350 return result;
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000351}
352
353
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000354/// Read a sleb128 encoded value and advance pointer
355/// See Variable Length Data in:
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000356/// @link http://dwarfstd.org/Dwarf3.pdf @unlink
357/// @param data reference variable holding memory pointer to decode from
358/// @returns decoded value
Garrison Venn64cfcef2011-04-10 14:06:52 +0000359static uintptr_t readSLEB128(const uint8_t **data) {
Chris Lattner626ab1c2011-04-08 18:02:51 +0000360 uintptr_t result = 0;
361 uintptr_t shift = 0;
362 unsigned char byte;
Garrison Venn64cfcef2011-04-10 14:06:52 +0000363 const uint8_t *p = *data;
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000364
Chris Lattner626ab1c2011-04-08 18:02:51 +0000365 do {
366 byte = *p++;
367 result |= (byte & 0x7f) << shift;
368 shift += 7;
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000369 }
Chris Lattner626ab1c2011-04-08 18:02:51 +0000370 while (byte & 0x80);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000371
Chris Lattner626ab1c2011-04-08 18:02:51 +0000372 *data = p;
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000373
Chris Lattner626ab1c2011-04-08 18:02:51 +0000374 if ((byte & 0x40) && (shift < (sizeof(result) << 3))) {
375 result |= (~0 << shift);
376 }
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000377
Chris Lattner626ab1c2011-04-08 18:02:51 +0000378 return result;
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000379}
380
Rafael Espindola58c3aa22013-05-01 21:05:05 +0000381unsigned getEncodingSize(uint8_t Encoding) {
382 if (Encoding == llvm::dwarf::DW_EH_PE_omit)
383 return 0;
384
385 switch (Encoding & 0x0F) {
386 case llvm::dwarf::DW_EH_PE_absptr:
387 return sizeof(uintptr_t);
388 case llvm::dwarf::DW_EH_PE_udata2:
389 return sizeof(uint16_t);
390 case llvm::dwarf::DW_EH_PE_udata4:
391 return sizeof(uint32_t);
392 case llvm::dwarf::DW_EH_PE_udata8:
393 return sizeof(uint64_t);
394 case llvm::dwarf::DW_EH_PE_sdata2:
395 return sizeof(int16_t);
396 case llvm::dwarf::DW_EH_PE_sdata4:
397 return sizeof(int32_t);
398 case llvm::dwarf::DW_EH_PE_sdata8:
399 return sizeof(int64_t);
400 default:
401 // not supported
402 abort();
403 }
404}
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000405
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000406/// Read a pointer encoded value and advance pointer
407/// See Variable Length Data in:
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000408/// @link http://dwarfstd.org/Dwarf3.pdf @unlink
409/// @param data reference variable holding memory pointer to decode from
410/// @param encoding dwarf encoding type
411/// @returns decoded value
Garrison Venn64cfcef2011-04-10 14:06:52 +0000412static uintptr_t readEncodedPointer(const uint8_t **data, uint8_t encoding) {
Chris Lattner626ab1c2011-04-08 18:02:51 +0000413 uintptr_t result = 0;
Garrison Venn64cfcef2011-04-10 14:06:52 +0000414 const uint8_t *p = *data;
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000415
416 if (encoding == llvm::dwarf::DW_EH_PE_omit)
Chris Lattner626ab1c2011-04-08 18:02:51 +0000417 return(result);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000418
419 // first get value
Chris Lattner626ab1c2011-04-08 18:02:51 +0000420 switch (encoding & 0x0F) {
421 case llvm::dwarf::DW_EH_PE_absptr:
Saleem Abdulrasool802a1be2016-11-20 02:36:38 +0000422 result = ReadType<uintptr_t>(p);
Chris Lattner626ab1c2011-04-08 18:02:51 +0000423 break;
424 case llvm::dwarf::DW_EH_PE_uleb128:
425 result = readULEB128(&p);
426 break;
427 // Note: This case has not been tested
428 case llvm::dwarf::DW_EH_PE_sleb128:
429 result = readSLEB128(&p);
430 break;
431 case llvm::dwarf::DW_EH_PE_udata2:
Saleem Abdulrasool802a1be2016-11-20 02:36:38 +0000432 result = ReadType<uint16_t>(p);
Chris Lattner626ab1c2011-04-08 18:02:51 +0000433 break;
434 case llvm::dwarf::DW_EH_PE_udata4:
Saleem Abdulrasool802a1be2016-11-20 02:36:38 +0000435 result = ReadType<uint32_t>(p);
Chris Lattner626ab1c2011-04-08 18:02:51 +0000436 break;
437 case llvm::dwarf::DW_EH_PE_udata8:
Saleem Abdulrasool802a1be2016-11-20 02:36:38 +0000438 result = ReadType<uint64_t>(p);
Chris Lattner626ab1c2011-04-08 18:02:51 +0000439 break;
440 case llvm::dwarf::DW_EH_PE_sdata2:
Saleem Abdulrasool802a1be2016-11-20 02:36:38 +0000441 result = ReadType<int16_t>(p);
Chris Lattner626ab1c2011-04-08 18:02:51 +0000442 break;
443 case llvm::dwarf::DW_EH_PE_sdata4:
Saleem Abdulrasool802a1be2016-11-20 02:36:38 +0000444 result = ReadType<int32_t>(p);
Chris Lattner626ab1c2011-04-08 18:02:51 +0000445 break;
446 case llvm::dwarf::DW_EH_PE_sdata8:
Saleem Abdulrasool802a1be2016-11-20 02:36:38 +0000447 result = ReadType<int64_t>(p);
Chris Lattner626ab1c2011-04-08 18:02:51 +0000448 break;
449 default:
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000450 // not supported
Chris Lattner626ab1c2011-04-08 18:02:51 +0000451 abort();
452 break;
453 }
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000454
455 // then add relative offset
Chris Lattner626ab1c2011-04-08 18:02:51 +0000456 switch (encoding & 0x70) {
457 case llvm::dwarf::DW_EH_PE_absptr:
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000458 // do nothing
Chris Lattner626ab1c2011-04-08 18:02:51 +0000459 break;
460 case llvm::dwarf::DW_EH_PE_pcrel:
461 result += (uintptr_t)(*data);
462 break;
463 case llvm::dwarf::DW_EH_PE_textrel:
464 case llvm::dwarf::DW_EH_PE_datarel:
465 case llvm::dwarf::DW_EH_PE_funcrel:
466 case llvm::dwarf::DW_EH_PE_aligned:
467 default:
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000468 // not supported
Chris Lattner626ab1c2011-04-08 18:02:51 +0000469 abort();
470 break;
471 }
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000472
473 // then apply indirection
Chris Lattner626ab1c2011-04-08 18:02:51 +0000474 if (encoding & llvm::dwarf::DW_EH_PE_indirect) {
475 result = *((uintptr_t*)result);
476 }
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000477
Chris Lattner626ab1c2011-04-08 18:02:51 +0000478 *data = p;
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000479
Chris Lattner626ab1c2011-04-08 18:02:51 +0000480 return result;
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000481}
482
483
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000484/// Deals with Dwarf actions matching our type infos
485/// (OurExceptionType_t instances). Returns whether or not a dwarf emitted
486/// action matches the supplied exception type. If such a match succeeds,
487/// the resultAction argument will be set with > 0 index value. Only
488/// corresponding llvm.eh.selector type info arguments, cleanup arguments
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000489/// are supported. Filters are not supported.
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000490/// See Variable Length Data in:
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000491/// @link http://dwarfstd.org/Dwarf3.pdf @unlink
Vlad Tsyrklevich30b47382017-09-12 00:19:11 +0000492/// Also see @link http://itanium-cxx-abi.github.io/cxx-abi/abi-eh.html @unlink
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000493/// @param resultAction reference variable which will be set with result
494/// @param classInfo our array of type info pointers (to globals)
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000495/// @param actionEntry index into above type info array or 0 (clean up).
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000496/// We do not support filters.
497/// @param exceptionClass exception class (_Unwind_Exception::exception_class)
498/// of thrown exception.
499/// @param exceptionObject thrown _Unwind_Exception instance.
500/// @returns whether or not a type info was found. False is returned if only
501/// a cleanup was found
502static bool handleActionValue(int64_t *resultAction,
Rafael Espindola58c3aa22013-05-01 21:05:05 +0000503 uint8_t TTypeEncoding,
504 const uint8_t *ClassInfo,
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000505 uintptr_t actionEntry,
506 uint64_t exceptionClass,
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000507 struct _Unwind_Exception *exceptionObject) {
Chris Lattner626ab1c2011-04-08 18:02:51 +0000508 bool ret = false;
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000509
510 if (!resultAction ||
511 !exceptionObject ||
Chris Lattner626ab1c2011-04-08 18:02:51 +0000512 (exceptionClass != ourBaseExceptionClass))
513 return(ret);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000514
Garrison Venn64cfcef2011-04-10 14:06:52 +0000515 struct OurBaseException_t *excp = (struct OurBaseException_t*)
Chris Lattner626ab1c2011-04-08 18:02:51 +0000516 (((char*) exceptionObject) + ourBaseFromUnwindOffset);
517 struct OurExceptionType_t *excpType = &(excp->type);
518 int type = excpType->type;
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000519
Chris Lattner626ab1c2011-04-08 18:02:51 +0000520#ifdef DEBUG
521 fprintf(stderr,
522 "handleActionValue(...): exceptionObject = <%p>, "
523 "excp = <%p>.\n",
David Blaikie54b3a032015-08-14 00:31:49 +0000524 (void*)exceptionObject,
525 (void*)excp);
Chris Lattner626ab1c2011-04-08 18:02:51 +0000526#endif
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000527
Chris Lattner626ab1c2011-04-08 18:02:51 +0000528 const uint8_t *actionPos = (uint8_t*) actionEntry,
529 *tempActionPos;
530 int64_t typeOffset = 0,
531 actionOffset;
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000532
Chris Lattner626ab1c2011-04-08 18:02:51 +0000533 for (int i = 0; true; ++i) {
534 // Each emitted dwarf action corresponds to a 2 tuple of
535 // type info address offset, and action offset to the next
536 // emitted action.
537 typeOffset = readSLEB128(&actionPos);
538 tempActionPos = actionPos;
539 actionOffset = readSLEB128(&tempActionPos);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000540
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000541#ifdef DEBUG
542 fprintf(stderr,
David Blaikie54b3a032015-08-14 00:31:49 +0000543 "handleActionValue(...):typeOffset: <%" PRIi64 ">, "
544 "actionOffset: <%" PRIi64 ">.\n",
Chris Lattner626ab1c2011-04-08 18:02:51 +0000545 typeOffset,
546 actionOffset);
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000547#endif
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000548 assert((typeOffset >= 0) &&
Chris Lattner626ab1c2011-04-08 18:02:51 +0000549 "handleActionValue(...):filters are not supported.");
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000550
Chris Lattner626ab1c2011-04-08 18:02:51 +0000551 // Note: A typeOffset == 0 implies that a cleanup llvm.eh.selector
552 // argument has been matched.
Rafael Espindola58c3aa22013-05-01 21:05:05 +0000553 if (typeOffset > 0) {
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000554#ifdef DEBUG
Chris Lattner626ab1c2011-04-08 18:02:51 +0000555 fprintf(stderr,
556 "handleActionValue(...):actionValue <%d> found.\n",
557 i);
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000558#endif
Rafael Espindola58c3aa22013-05-01 21:05:05 +0000559 unsigned EncSize = getEncodingSize(TTypeEncoding);
560 const uint8_t *EntryP = ClassInfo - typeOffset * EncSize;
561 uintptr_t P = readEncodedPointer(&EntryP, TTypeEncoding);
562 struct OurExceptionType_t *ThisClassInfo =
563 reinterpret_cast<struct OurExceptionType_t *>(P);
564 if (ThisClassInfo->type == type) {
565 *resultAction = i + 1;
566 ret = true;
567 break;
568 }
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000569 }
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000570
Chris Lattner626ab1c2011-04-08 18:02:51 +0000571#ifdef DEBUG
572 fprintf(stderr,
573 "handleActionValue(...):actionValue not found.\n");
574#endif
575 if (!actionOffset)
576 break;
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000577
Chris Lattner626ab1c2011-04-08 18:02:51 +0000578 actionPos += actionOffset;
579 }
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000580
Chris Lattner626ab1c2011-04-08 18:02:51 +0000581 return(ret);
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000582}
583
584
585/// Deals with the Language specific data portion of the emitted dwarf code.
Vlad Tsyrklevich30b47382017-09-12 00:19:11 +0000586/// See @link http://itanium-cxx-abi.github.io/cxx-abi/abi-eh.html @unlink
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000587/// @param version unsupported (ignored), unwind version
588/// @param lsda language specific data area
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000589/// @param _Unwind_Action actions minimally supported unwind stage
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000590/// (forced specifically not supported)
591/// @param exceptionClass exception class (_Unwind_Exception::exception_class)
592/// of thrown exception.
593/// @param exceptionObject thrown _Unwind_Exception instance.
594/// @param context unwind system context
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000595/// @returns minimally supported unwinding control indicator
Saleem Abdulrasool308b0462016-11-20 02:36:36 +0000596static _Unwind_Reason_Code handleLsda(int version, const uint8_t *lsda,
Chris Lattner626ab1c2011-04-08 18:02:51 +0000597 _Unwind_Action actions,
Saleem Abdulrasool308b0462016-11-20 02:36:36 +0000598 _Unwind_Exception_Class exceptionClass,
599 struct _Unwind_Exception *exceptionObject,
600 struct _Unwind_Context *context) {
Chris Lattner626ab1c2011-04-08 18:02:51 +0000601 _Unwind_Reason_Code ret = _URC_CONTINUE_UNWIND;
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000602
Chris Lattner626ab1c2011-04-08 18:02:51 +0000603 if (!lsda)
604 return(ret);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000605
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000606#ifdef DEBUG
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000607 fprintf(stderr,
Chris Lattner626ab1c2011-04-08 18:02:51 +0000608 "handleLsda(...):lsda is non-zero.\n");
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000609#endif
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000610
Chris Lattner626ab1c2011-04-08 18:02:51 +0000611 // Get the current instruction pointer and offset it before next
612 // instruction in the current frame which threw the exception.
613 uintptr_t pc = _Unwind_GetIP(context)-1;
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000614
615 // Get beginning current frame's code (as defined by the
Chris Lattner626ab1c2011-04-08 18:02:51 +0000616 // emitted dwarf code)
617 uintptr_t funcStart = _Unwind_GetRegionStart(context);
618 uintptr_t pcOffset = pc - funcStart;
Rafael Espindola58c3aa22013-05-01 21:05:05 +0000619 const uint8_t *ClassInfo = NULL;
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000620
Chris Lattner626ab1c2011-04-08 18:02:51 +0000621 // Note: See JITDwarfEmitter::EmitExceptionTable(...) for corresponding
622 // dwarf emission
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000623
Chris Lattner626ab1c2011-04-08 18:02:51 +0000624 // Parse LSDA header.
625 uint8_t lpStartEncoding = *lsda++;
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000626
Chris Lattner626ab1c2011-04-08 18:02:51 +0000627 if (lpStartEncoding != llvm::dwarf::DW_EH_PE_omit) {
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000628 readEncodedPointer(&lsda, lpStartEncoding);
Chris Lattner626ab1c2011-04-08 18:02:51 +0000629 }
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000630
Chris Lattner626ab1c2011-04-08 18:02:51 +0000631 uint8_t ttypeEncoding = *lsda++;
632 uintptr_t classInfoOffset;
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000633
Chris Lattner626ab1c2011-04-08 18:02:51 +0000634 if (ttypeEncoding != llvm::dwarf::DW_EH_PE_omit) {
635 // Calculate type info locations in emitted dwarf code which
636 // were flagged by type info arguments to llvm.eh.selector
637 // intrinsic
638 classInfoOffset = readULEB128(&lsda);
Rafael Espindola58c3aa22013-05-01 21:05:05 +0000639 ClassInfo = lsda + classInfoOffset;
Chris Lattner626ab1c2011-04-08 18:02:51 +0000640 }
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000641
642 // Walk call-site table looking for range that
643 // includes current PC.
644
Chris Lattner626ab1c2011-04-08 18:02:51 +0000645 uint8_t callSiteEncoding = *lsda++;
646 uint32_t callSiteTableLength = readULEB128(&lsda);
Garrison Venn64cfcef2011-04-10 14:06:52 +0000647 const uint8_t *callSiteTableStart = lsda;
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000648 const uint8_t *callSiteTableEnd = callSiteTableStart +
Chris Lattner626ab1c2011-04-08 18:02:51 +0000649 callSiteTableLength;
Garrison Venn64cfcef2011-04-10 14:06:52 +0000650 const uint8_t *actionTableStart = callSiteTableEnd;
651 const uint8_t *callSitePtr = callSiteTableStart;
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000652
Chris Lattner626ab1c2011-04-08 18:02:51 +0000653 while (callSitePtr < callSiteTableEnd) {
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000654 uintptr_t start = readEncodedPointer(&callSitePtr,
Chris Lattner626ab1c2011-04-08 18:02:51 +0000655 callSiteEncoding);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000656 uintptr_t length = readEncodedPointer(&callSitePtr,
Chris Lattner626ab1c2011-04-08 18:02:51 +0000657 callSiteEncoding);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000658 uintptr_t landingPad = readEncodedPointer(&callSitePtr,
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000659 callSiteEncoding);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000660
Chris Lattner626ab1c2011-04-08 18:02:51 +0000661 // Note: Action value
662 uintptr_t actionEntry = readULEB128(&callSitePtr);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000663
Chris Lattner626ab1c2011-04-08 18:02:51 +0000664 if (exceptionClass != ourBaseExceptionClass) {
665 // We have been notified of a foreign exception being thrown,
666 // and we therefore need to execute cleanup landing pads
667 actionEntry = 0;
Chris Lattner626ab1c2011-04-08 18:02:51 +0000668 }
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000669
Chris Lattner626ab1c2011-04-08 18:02:51 +0000670 if (landingPad == 0) {
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000671#ifdef DEBUG
Chris Lattner626ab1c2011-04-08 18:02:51 +0000672 fprintf(stderr,
673 "handleLsda(...): No landing pad found.\n");
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000674#endif
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000675
Chris Lattner626ab1c2011-04-08 18:02:51 +0000676 continue; // no landing pad for this entry
677 }
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000678
Chris Lattner626ab1c2011-04-08 18:02:51 +0000679 if (actionEntry) {
680 actionEntry += ((uintptr_t) actionTableStart) - 1;
681 }
682 else {
683#ifdef DEBUG
684 fprintf(stderr,
685 "handleLsda(...):No action table found.\n");
686#endif
687 }
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000688
Chris Lattner626ab1c2011-04-08 18:02:51 +0000689 bool exceptionMatched = false;
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000690
Chris Lattner626ab1c2011-04-08 18:02:51 +0000691 if ((start <= pcOffset) && (pcOffset < (start + length))) {
692#ifdef DEBUG
693 fprintf(stderr,
694 "handleLsda(...): Landing pad found.\n");
695#endif
696 int64_t actionValue = 0;
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000697
Chris Lattner626ab1c2011-04-08 18:02:51 +0000698 if (actionEntry) {
Garrison Venn64cfcef2011-04-10 14:06:52 +0000699 exceptionMatched = handleActionValue(&actionValue,
Rafael Espindola58c3aa22013-05-01 21:05:05 +0000700 ttypeEncoding,
701 ClassInfo,
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000702 actionEntry,
703 exceptionClass,
Garrison Venn64cfcef2011-04-10 14:06:52 +0000704 exceptionObject);
Chris Lattner626ab1c2011-04-08 18:02:51 +0000705 }
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000706
Chris Lattner626ab1c2011-04-08 18:02:51 +0000707 if (!(actions & _UA_SEARCH_PHASE)) {
708#ifdef DEBUG
709 fprintf(stderr,
710 "handleLsda(...): installed landing pad "
711 "context.\n");
712#endif
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000713
Chris Lattner626ab1c2011-04-08 18:02:51 +0000714 // Found landing pad for the PC.
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000715 // Set Instruction Pointer to so we re-enter function
716 // at landing pad. The landing pad is created by the
Chris Lattner626ab1c2011-04-08 18:02:51 +0000717 // compiler to take two parameters in registers.
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000718 _Unwind_SetGR(context,
719 __builtin_eh_return_data_regno(0),
Chris Lattner626ab1c2011-04-08 18:02:51 +0000720 (uintptr_t)exceptionObject);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000721
Chris Lattner626ab1c2011-04-08 18:02:51 +0000722 // Note: this virtual register directly corresponds
723 // to the return of the llvm.eh.selector intrinsic
724 if (!actionEntry || !exceptionMatched) {
725 // We indicate cleanup only
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000726 _Unwind_SetGR(context,
727 __builtin_eh_return_data_regno(1),
Chris Lattner626ab1c2011-04-08 18:02:51 +0000728 0);
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000729 }
730 else {
Chris Lattner626ab1c2011-04-08 18:02:51 +0000731 // Matched type info index of llvm.eh.selector intrinsic
732 // passed here.
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000733 _Unwind_SetGR(context,
734 __builtin_eh_return_data_regno(1),
Chris Lattner626ab1c2011-04-08 18:02:51 +0000735 actionValue);
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000736 }
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000737
Chris Lattner626ab1c2011-04-08 18:02:51 +0000738 // To execute landing pad set here
739 _Unwind_SetIP(context, funcStart + landingPad);
740 ret = _URC_INSTALL_CONTEXT;
741 }
742 else if (exceptionMatched) {
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000743#ifdef DEBUG
Chris Lattner626ab1c2011-04-08 18:02:51 +0000744 fprintf(stderr,
745 "handleLsda(...): setting handler found.\n");
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000746#endif
Chris Lattner626ab1c2011-04-08 18:02:51 +0000747 ret = _URC_HANDLER_FOUND;
748 }
749 else {
750 // Note: Only non-clean up handlers are marked as
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000751 // found. Otherwise the clean up handlers will be
752 // re-found and executed during the clean up
Chris Lattner626ab1c2011-04-08 18:02:51 +0000753 // phase.
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000754#ifdef DEBUG
Chris Lattner626ab1c2011-04-08 18:02:51 +0000755 fprintf(stderr,
756 "handleLsda(...): cleanup handler found.\n");
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000757#endif
Chris Lattner626ab1c2011-04-08 18:02:51 +0000758 }
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000759
Chris Lattner626ab1c2011-04-08 18:02:51 +0000760 break;
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000761 }
Chris Lattner626ab1c2011-04-08 18:02:51 +0000762 }
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000763
Chris Lattner626ab1c2011-04-08 18:02:51 +0000764 return(ret);
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000765}
766
767
768/// This is the personality function which is embedded (dwarf emitted), in the
769/// dwarf unwind info block. Again see: JITDwarfEmitter.cpp.
Vlad Tsyrklevich30b47382017-09-12 00:19:11 +0000770/// See @link http://itanium-cxx-abi.github.io/cxx-abi/abi-eh.html @unlink
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000771/// @param version unsupported (ignored), unwind version
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000772/// @param _Unwind_Action actions minimally supported unwind stage
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000773/// (forced specifically not supported)
774/// @param exceptionClass exception class (_Unwind_Exception::exception_class)
775/// of thrown exception.
776/// @param exceptionObject thrown _Unwind_Exception instance.
777/// @param context unwind system context
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000778/// @returns minimally supported unwinding control indicator
Saleem Abdulrasool308b0462016-11-20 02:36:36 +0000779_Unwind_Reason_Code ourPersonality(int version, _Unwind_Action actions,
780 _Unwind_Exception_Class exceptionClass,
Garrison Venn64cfcef2011-04-10 14:06:52 +0000781 struct _Unwind_Exception *exceptionObject,
Saleem Abdulrasool308b0462016-11-20 02:36:36 +0000782 struct _Unwind_Context *context) {
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000783#ifdef DEBUG
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000784 fprintf(stderr,
Chris Lattner626ab1c2011-04-08 18:02:51 +0000785 "We are in ourPersonality(...):actions is <%d>.\n",
786 actions);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000787
Chris Lattner626ab1c2011-04-08 18:02:51 +0000788 if (actions & _UA_SEARCH_PHASE) {
789 fprintf(stderr, "ourPersonality(...):In search phase.\n");
790 }
791 else {
792 fprintf(stderr, "ourPersonality(...):In non-search phase.\n");
793 }
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000794#endif
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000795
Garrison Venn64cfcef2011-04-10 14:06:52 +0000796 const uint8_t *lsda = _Unwind_GetLanguageSpecificData(context);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000797
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000798#ifdef DEBUG
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000799 fprintf(stderr,
Chris Lattner626ab1c2011-04-08 18:02:51 +0000800 "ourPersonality(...):lsda = <%p>.\n",
David Blaikie54b3a032015-08-14 00:31:49 +0000801 (void*)lsda);
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000802#endif
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000803
Chris Lattner626ab1c2011-04-08 18:02:51 +0000804 // The real work of the personality function is captured here
805 return(handleLsda(version,
806 lsda,
807 actions,
808 exceptionClass,
809 exceptionObject,
810 context));
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000811}
812
813
814/// Generates our _Unwind_Exception class from a given character array.
815/// thereby handling arbitrary lengths (not in standard), and handling
816/// embedded \0s.
Vlad Tsyrklevich30b47382017-09-12 00:19:11 +0000817/// See @link http://itanium-cxx-abi.github.io/cxx-abi/abi-eh.html @unlink
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000818/// @param classChars char array to encode. NULL values not checkedf
819/// @param classCharsSize number of chars in classChars. Value is not checked.
820/// @returns class value
821uint64_t genClass(const unsigned char classChars[], size_t classCharsSize)
822{
Chris Lattner626ab1c2011-04-08 18:02:51 +0000823 uint64_t ret = classChars[0];
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000824
Chris Lattner626ab1c2011-04-08 18:02:51 +0000825 for (unsigned i = 1; i < classCharsSize; ++i) {
826 ret <<= 8;
827 ret += classChars[i];
828 }
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000829
Chris Lattner626ab1c2011-04-08 18:02:51 +0000830 return(ret);
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000831}
832
833} // extern "C"
834
835//
836// Runtime C Library functions End
837//
838
839//
840// Code generation functions
841//
842
843/// Generates code to print given constant string
844/// @param context llvm context
845/// @param module code for module instance
846/// @param builder builder instance
847/// @param toPrint string to print
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000848/// @param useGlobal A value of true (default) indicates a GlobalValue is
849/// generated, and is used to hold the constant string. A value of
850/// false indicates that the constant string will be stored on the
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000851/// stack.
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000852void generateStringPrint(llvm::LLVMContext &context,
Garrison Venn64cfcef2011-04-10 14:06:52 +0000853 llvm::Module &module,
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000854 llvm::IRBuilder<> &builder,
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000855 std::string toPrint,
856 bool useGlobal = true) {
Chris Lattner626ab1c2011-04-08 18:02:51 +0000857 llvm::Function *printFunct = module.getFunction("printStr");
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000858
Chris Lattner626ab1c2011-04-08 18:02:51 +0000859 llvm::Value *stringVar;
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000860 llvm::Constant *stringConstant =
Peter Collingbourne793a32d2012-02-06 14:09:13 +0000861 llvm::ConstantDataArray::getString(context, toPrint);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000862
Chris Lattner626ab1c2011-04-08 18:02:51 +0000863 if (useGlobal) {
864 // Note: Does not work without allocation
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000865 stringVar =
866 new llvm::GlobalVariable(module,
Chris Lattner626ab1c2011-04-08 18:02:51 +0000867 stringConstant->getType(),
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000868 true,
Rafael Espindola737c9f62014-02-19 17:23:20 +0000869 llvm::GlobalValue::PrivateLinkage,
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000870 stringConstant,
Chris Lattner626ab1c2011-04-08 18:02:51 +0000871 "");
872 }
873 else {
874 stringVar = builder.CreateAlloca(stringConstant->getType());
875 builder.CreateStore(stringConstant, stringVar);
876 }
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000877
878 llvm::Value *cast = builder.CreatePointerCast(stringVar,
Garrison Venn9cb50862011-09-23 14:45:10 +0000879 builder.getInt8PtrTy());
Chris Lattner626ab1c2011-04-08 18:02:51 +0000880 builder.CreateCall(printFunct, cast);
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000881}
882
883
884/// Generates code to print given runtime integer according to constant
885/// string format, and a given print function.
886/// @param context llvm context
887/// @param module code for module instance
888/// @param builder builder instance
889/// @param printFunct function used to "print" integer
890/// @param toPrint string to print
891/// @param format printf like formating string for print
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000892/// @param useGlobal A value of true (default) indicates a GlobalValue is
893/// generated, and is used to hold the constant string. A value of
894/// false indicates that the constant string will be stored on the
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000895/// stack.
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000896void generateIntegerPrint(llvm::LLVMContext &context,
Garrison Venn64cfcef2011-04-10 14:06:52 +0000897 llvm::Module &module,
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000898 llvm::IRBuilder<> &builder,
Garrison Venn64cfcef2011-04-10 14:06:52 +0000899 llvm::Function &printFunct,
900 llvm::Value &toPrint,
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000901 std::string format,
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000902 bool useGlobal = true) {
Peter Collingbourne793a32d2012-02-06 14:09:13 +0000903 llvm::Constant *stringConstant =
904 llvm::ConstantDataArray::getString(context, format);
Chris Lattner626ab1c2011-04-08 18:02:51 +0000905 llvm::Value *stringVar;
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000906
Chris Lattner626ab1c2011-04-08 18:02:51 +0000907 if (useGlobal) {
908 // Note: Does not seem to work without allocation
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000909 stringVar =
910 new llvm::GlobalVariable(module,
Chris Lattner626ab1c2011-04-08 18:02:51 +0000911 stringConstant->getType(),
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000912 true,
Rafael Espindola737c9f62014-02-19 17:23:20 +0000913 llvm::GlobalValue::PrivateLinkage,
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000914 stringConstant,
Chris Lattner626ab1c2011-04-08 18:02:51 +0000915 "");
916 }
917 else {
918 stringVar = builder.CreateAlloca(stringConstant->getType());
919 builder.CreateStore(stringConstant, stringVar);
920 }
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000921
922 llvm::Value *cast = builder.CreateBitCast(stringVar,
Garrison Venn9cb50862011-09-23 14:45:10 +0000923 builder.getInt8PtrTy());
David Blaikie00c293b2015-08-14 00:24:56 +0000924 builder.CreateCall(&printFunct, {&toPrint, cast});
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000925}
926
927
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000928/// Generates code to handle finally block type semantics: always runs
929/// regardless of whether a thrown exception is passing through or the
930/// parent function is simply exiting. In addition to printing some state
931/// to stderr, this code will resume the exception handling--runs the
932/// unwind resume block, if the exception has not been previously caught
933/// by a catch clause, and will otherwise execute the end block (terminator
934/// block). In addition this function creates the corresponding function's
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000935/// stack storage for the exception pointer and catch flag status.
936/// @param context llvm context
937/// @param module code for module instance
938/// @param builder builder instance
939/// @param toAddTo parent function to add block to
940/// @param blockName block name of new "finally" block.
941/// @param functionId output id used for printing
942/// @param terminatorBlock terminator "end" block
943/// @param unwindResumeBlock unwind resume block
944/// @param exceptionCaughtFlag reference exception caught/thrown status storage
945/// @param exceptionStorage reference to exception pointer storage
Garrison Venn9cb50862011-09-23 14:45:10 +0000946/// @param caughtResultStorage reference to landingpad result storage
Garrison Venna2c2f1a2010-02-09 23:22:43 +0000947/// @returns newly created block
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000948static llvm::BasicBlock *createFinallyBlock(llvm::LLVMContext &context,
949 llvm::Module &module,
950 llvm::IRBuilder<> &builder,
Garrison Venn64cfcef2011-04-10 14:06:52 +0000951 llvm::Function &toAddTo,
952 std::string &blockName,
953 std::string &functionId,
954 llvm::BasicBlock &terminatorBlock,
955 llvm::BasicBlock &unwindResumeBlock,
956 llvm::Value **exceptionCaughtFlag,
Bill Wendling08e8db42012-02-04 00:29:12 +0000957 llvm::Value **exceptionStorage,
958 llvm::Value **caughtResultStorage) {
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000959 assert(exceptionCaughtFlag &&
Chris Lattner626ab1c2011-04-08 18:02:51 +0000960 "ExceptionDemo::createFinallyBlock(...):exceptionCaughtFlag "
961 "is NULL");
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000962 assert(exceptionStorage &&
Chris Lattner626ab1c2011-04-08 18:02:51 +0000963 "ExceptionDemo::createFinallyBlock(...):exceptionStorage "
964 "is NULL");
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000965 assert(caughtResultStorage &&
Garrison Venn9cb50862011-09-23 14:45:10 +0000966 "ExceptionDemo::createFinallyBlock(...):caughtResultStorage "
967 "is NULL");
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000968
Garrison Venn9cb50862011-09-23 14:45:10 +0000969 *exceptionCaughtFlag = createEntryBlockAlloca(toAddTo,
970 "exceptionCaught",
971 ourExceptionNotThrownState->getType(),
972 ourExceptionNotThrownState);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000973
Chris Lattner77613d42011-07-18 04:52:09 +0000974 llvm::PointerType *exceptionStorageType = builder.getInt8PtrTy();
Garrison Venn9cb50862011-09-23 14:45:10 +0000975 *exceptionStorage = createEntryBlockAlloca(toAddTo,
976 "exceptionStorage",
977 exceptionStorageType,
978 llvm::ConstantPointerNull::get(
979 exceptionStorageType));
Garrison Venn9cb50862011-09-23 14:45:10 +0000980 *caughtResultStorage = createEntryBlockAlloca(toAddTo,
981 "caughtResultStorage",
982 ourCaughtResultType,
983 llvm::ConstantAggregateZero::get(
984 ourCaughtResultType));
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000985
Chris Lattner626ab1c2011-04-08 18:02:51 +0000986 llvm::BasicBlock *ret = llvm::BasicBlock::Create(context,
987 blockName,
988 &toAddTo);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000989
Chris Lattner626ab1c2011-04-08 18:02:51 +0000990 builder.SetInsertPoint(ret);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000991
Chris Lattner626ab1c2011-04-08 18:02:51 +0000992 std::ostringstream bufferToPrint;
993 bufferToPrint << "Gen: Executing finally block "
994 << blockName << " in " << functionId << "\n";
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +0000995 generateStringPrint(context,
996 module,
997 builder,
Chris Lattner626ab1c2011-04-08 18:02:51 +0000998 bufferToPrint.str(),
999 USE_GLOBAL_STR_CONSTS);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001000
Garrison Venn9cb50862011-09-23 14:45:10 +00001001 llvm::SwitchInst *theSwitch = builder.CreateSwitch(builder.CreateLoad(
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001002 *exceptionCaughtFlag),
Garrison Venn9cb50862011-09-23 14:45:10 +00001003 &terminatorBlock,
1004 2);
Chris Lattner626ab1c2011-04-08 18:02:51 +00001005 theSwitch->addCase(ourExceptionCaughtState, &terminatorBlock);
1006 theSwitch->addCase(ourExceptionThrownState, &unwindResumeBlock);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001007
Chris Lattner626ab1c2011-04-08 18:02:51 +00001008 return(ret);
Garrison Venna2c2f1a2010-02-09 23:22:43 +00001009}
1010
1011
1012/// Generates catch block semantics which print a string to indicate type of
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001013/// catch executed, sets an exception caught flag, and executes passed in
Garrison Venna2c2f1a2010-02-09 23:22:43 +00001014/// end block (terminator block).
1015/// @param context llvm context
1016/// @param module code for module instance
1017/// @param builder builder instance
1018/// @param toAddTo parent function to add block to
1019/// @param blockName block name of new "catch" block.
1020/// @param functionId output id used for printing
1021/// @param terminatorBlock terminator "end" block
1022/// @param exceptionCaughtFlag exception caught/thrown status
1023/// @returns newly created block
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001024static llvm::BasicBlock *createCatchBlock(llvm::LLVMContext &context,
1025 llvm::Module &module,
1026 llvm::IRBuilder<> &builder,
Garrison Venn64cfcef2011-04-10 14:06:52 +00001027 llvm::Function &toAddTo,
1028 std::string &blockName,
1029 std::string &functionId,
1030 llvm::BasicBlock &terminatorBlock,
1031 llvm::Value &exceptionCaughtFlag) {
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001032
Chris Lattner626ab1c2011-04-08 18:02:51 +00001033 llvm::BasicBlock *ret = llvm::BasicBlock::Create(context,
1034 blockName,
1035 &toAddTo);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001036
Chris Lattner626ab1c2011-04-08 18:02:51 +00001037 builder.SetInsertPoint(ret);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001038
Chris Lattner626ab1c2011-04-08 18:02:51 +00001039 std::ostringstream bufferToPrint;
1040 bufferToPrint << "Gen: Executing catch block "
1041 << blockName
1042 << " in "
1043 << functionId
1044 << std::endl;
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001045 generateStringPrint(context,
1046 module,
1047 builder,
Chris Lattner626ab1c2011-04-08 18:02:51 +00001048 bufferToPrint.str(),
1049 USE_GLOBAL_STR_CONSTS);
1050 builder.CreateStore(ourExceptionCaughtState, &exceptionCaughtFlag);
1051 builder.CreateBr(&terminatorBlock);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001052
Chris Lattner626ab1c2011-04-08 18:02:51 +00001053 return(ret);
Garrison Venna2c2f1a2010-02-09 23:22:43 +00001054}
1055
1056
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001057/// Generates a function which invokes a function (toInvoke) and, whose
1058/// unwind block will "catch" the type info types correspondingly held in the
1059/// exceptionTypesToCatch argument. If the toInvoke function throws an
1060/// exception which does not match any type info types contained in
1061/// exceptionTypesToCatch, the generated code will call _Unwind_Resume
1062/// with the raised exception. On the other hand the generated code will
Garrison Venna2c2f1a2010-02-09 23:22:43 +00001063/// normally exit if the toInvoke function does not throw an exception.
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001064/// The generated "finally" block is always run regardless of the cause of
Garrison Venna2c2f1a2010-02-09 23:22:43 +00001065/// the generated function exit.
1066/// The generated function is returned after being verified.
1067/// @param module code for module instance
1068/// @param builder builder instance
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001069/// @param fpm a function pass manager holding optional IR to IR
Garrison Venna2c2f1a2010-02-09 23:22:43 +00001070/// transformations
1071/// @param toInvoke inner function to invoke
1072/// @param ourId id used to printing purposes
1073/// @param numExceptionsToCatch length of exceptionTypesToCatch array
1074/// @param exceptionTypesToCatch array of type info types to "catch"
1075/// @returns generated function
Chandler Carruth7ea33e52015-02-13 10:21:05 +00001076static llvm::Function *createCatchWrappedInvokeFunction(
1077 llvm::Module &module, llvm::IRBuilder<> &builder,
1078 llvm::legacy::FunctionPassManager &fpm, llvm::Function &toInvoke,
1079 std::string ourId, unsigned numExceptionsToCatch,
1080 unsigned exceptionTypesToCatch[]) {
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001081
Garrison Venn64cfcef2011-04-10 14:06:52 +00001082 llvm::LLVMContext &context = module.getContext();
Chris Lattner626ab1c2011-04-08 18:02:51 +00001083 llvm::Function *toPrint32Int = module.getFunction("print32Int");
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001084
Chris Lattner626ab1c2011-04-08 18:02:51 +00001085 ArgTypes argTypes;
Garrison Vennc0f33cb2011-07-12 15:34:42 +00001086 argTypes.push_back(builder.getInt32Ty());
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001087
Chris Lattner626ab1c2011-04-08 18:02:51 +00001088 ArgNames argNames;
1089 argNames.push_back("exceptTypeToThrow");
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001090
1091 llvm::Function *ret = createFunction(module,
Chris Lattner626ab1c2011-04-08 18:02:51 +00001092 builder.getVoidTy(),
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001093 argTypes,
1094 argNames,
Chris Lattner626ab1c2011-04-08 18:02:51 +00001095 ourId,
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001096 llvm::Function::ExternalLinkage,
1097 false,
Chris Lattner626ab1c2011-04-08 18:02:51 +00001098 false);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001099
Chris Lattner626ab1c2011-04-08 18:02:51 +00001100 // Block which calls invoke
1101 llvm::BasicBlock *entryBlock = llvm::BasicBlock::Create(context,
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001102 "entry",
Chris Lattner626ab1c2011-04-08 18:02:51 +00001103 ret);
1104 // Normal block for invoke
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001105 llvm::BasicBlock *normalBlock = llvm::BasicBlock::Create(context,
1106 "normal",
Chris Lattner626ab1c2011-04-08 18:02:51 +00001107 ret);
1108 // Unwind block for invoke
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001109 llvm::BasicBlock *exceptionBlock = llvm::BasicBlock::Create(context,
1110 "exception",
Garrison Venn9cb50862011-09-23 14:45:10 +00001111 ret);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001112
Chris Lattner626ab1c2011-04-08 18:02:51 +00001113 // Block which routes exception to correct catch handler block
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001114 llvm::BasicBlock *exceptionRouteBlock = llvm::BasicBlock::Create(context,
1115 "exceptionRoute",
Garrison Venn9cb50862011-09-23 14:45:10 +00001116 ret);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001117
Chris Lattner626ab1c2011-04-08 18:02:51 +00001118 // Foreign exception handler
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001119 llvm::BasicBlock *externalExceptionBlock = llvm::BasicBlock::Create(context,
1120 "externalException",
Garrison Venn9cb50862011-09-23 14:45:10 +00001121 ret);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001122
Chris Lattner626ab1c2011-04-08 18:02:51 +00001123 // Block which calls _Unwind_Resume
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001124 llvm::BasicBlock *unwindResumeBlock = llvm::BasicBlock::Create(context,
1125 "unwindResume",
Garrison Venn9cb50862011-09-23 14:45:10 +00001126 ret);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001127
Chris Lattner626ab1c2011-04-08 18:02:51 +00001128 // Clean up block which delete exception if needed
Garrison Venn9cb50862011-09-23 14:45:10 +00001129 llvm::BasicBlock *endBlock = llvm::BasicBlock::Create(context, "end", ret);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001130
Chris Lattner626ab1c2011-04-08 18:02:51 +00001131 std::string nextName;
1132 std::vector<llvm::BasicBlock*> catchBlocks(numExceptionsToCatch);
Garrison Venn64cfcef2011-04-10 14:06:52 +00001133 llvm::Value *exceptionCaughtFlag = NULL;
1134 llvm::Value *exceptionStorage = NULL;
Garrison Venn9cb50862011-09-23 14:45:10 +00001135 llvm::Value *caughtResultStorage = NULL;
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001136
1137 // Finally block which will branch to unwindResumeBlock if
Chris Lattner626ab1c2011-04-08 18:02:51 +00001138 // exception is not caught. Initializes/allocates stack locations.
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001139 llvm::BasicBlock *finallyBlock = createFinallyBlock(context,
1140 module,
1141 builder,
1142 *ret,
1143 nextName = "finally",
Chris Lattner626ab1c2011-04-08 18:02:51 +00001144 ourId,
1145 *endBlock,
1146 *unwindResumeBlock,
1147 &exceptionCaughtFlag,
Bill Wendling08e8db42012-02-04 00:29:12 +00001148 &exceptionStorage,
1149 &caughtResultStorage
Garrison Venn9cb50862011-09-23 14:45:10 +00001150 );
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001151
Chris Lattner626ab1c2011-04-08 18:02:51 +00001152 for (unsigned i = 0; i < numExceptionsToCatch; ++i) {
1153 nextName = ourTypeInfoNames[exceptionTypesToCatch[i]];
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001154
Chris Lattner626ab1c2011-04-08 18:02:51 +00001155 // One catch block per type info to be caught
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001156 catchBlocks[i] = createCatchBlock(context,
1157 module,
1158 builder,
Chris Lattner626ab1c2011-04-08 18:02:51 +00001159 *ret,
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001160 nextName,
Chris Lattner626ab1c2011-04-08 18:02:51 +00001161 ourId,
1162 *finallyBlock,
1163 *exceptionCaughtFlag);
1164 }
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001165
Chris Lattner626ab1c2011-04-08 18:02:51 +00001166 // Entry Block
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001167
Chris Lattner626ab1c2011-04-08 18:02:51 +00001168 builder.SetInsertPoint(entryBlock);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001169
Chris Lattner626ab1c2011-04-08 18:02:51 +00001170 std::vector<llvm::Value*> args;
1171 args.push_back(namedValues["exceptTypeToThrow"]);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001172 builder.CreateInvoke(&toInvoke,
1173 normalBlock,
1174 exceptionBlock,
Chris Lattner77613d42011-07-18 04:52:09 +00001175 args);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001176
Chris Lattner626ab1c2011-04-08 18:02:51 +00001177 // End Block
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001178
Chris Lattner626ab1c2011-04-08 18:02:51 +00001179 builder.SetInsertPoint(endBlock);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001180
1181 generateStringPrint(context,
Chris Lattner626ab1c2011-04-08 18:02:51 +00001182 module,
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001183 builder,
Chris Lattner626ab1c2011-04-08 18:02:51 +00001184 "Gen: In end block: exiting in " + ourId + ".\n",
1185 USE_GLOBAL_STR_CONSTS);
Garrison Venn9cb50862011-09-23 14:45:10 +00001186 llvm::Function *deleteOurException = module.getFunction("deleteOurException");
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001187
Chris Lattner626ab1c2011-04-08 18:02:51 +00001188 // Note: function handles NULL exceptions
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001189 builder.CreateCall(deleteOurException,
Chris Lattner626ab1c2011-04-08 18:02:51 +00001190 builder.CreateLoad(exceptionStorage));
1191 builder.CreateRetVoid();
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001192
Chris Lattner626ab1c2011-04-08 18:02:51 +00001193 // Normal Block
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001194
Chris Lattner626ab1c2011-04-08 18:02:51 +00001195 builder.SetInsertPoint(normalBlock);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001196
1197 generateStringPrint(context,
Chris Lattner626ab1c2011-04-08 18:02:51 +00001198 module,
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001199 builder,
Chris Lattner626ab1c2011-04-08 18:02:51 +00001200 "Gen: No exception in " + ourId + "!\n",
1201 USE_GLOBAL_STR_CONSTS);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001202
Chris Lattner626ab1c2011-04-08 18:02:51 +00001203 // Finally block is always called
1204 builder.CreateBr(finallyBlock);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001205
Chris Lattner626ab1c2011-04-08 18:02:51 +00001206 // Unwind Resume Block
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001207
Chris Lattner626ab1c2011-04-08 18:02:51 +00001208 builder.SetInsertPoint(unwindResumeBlock);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001209
Garrison Venn9cb50862011-09-23 14:45:10 +00001210 builder.CreateResume(builder.CreateLoad(caughtResultStorage));
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001211
Chris Lattner626ab1c2011-04-08 18:02:51 +00001212 // Exception Block
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001213
Chris Lattner626ab1c2011-04-08 18:02:51 +00001214 builder.SetInsertPoint(exceptionBlock);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001215
Garrison Venn85500712011-09-22 15:45:14 +00001216 llvm::Function *personality = module.getFunction("ourPersonality");
David Blaikie329f9592015-08-14 00:37:16 +00001217 ret->setPersonalityFn(personality);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001218
1219 llvm::LandingPadInst *caughtResult =
Garrison Venn85500712011-09-22 15:45:14 +00001220 builder.CreateLandingPad(ourCaughtResultType,
Garrison Venn85500712011-09-22 15:45:14 +00001221 numExceptionsToCatch,
1222 "landingPad");
1223
1224 caughtResult->setCleanup(true);
1225
1226 for (unsigned i = 0; i < numExceptionsToCatch; ++i) {
1227 // Set up type infos to be caught
1228 caughtResult->addClause(module.getGlobalVariable(
1229 ourTypeInfoNames[exceptionTypesToCatch[i]]));
1230 }
1231
1232 llvm::Value *unwindException = builder.CreateExtractValue(caughtResult, 0);
Garrison Venn9cb50862011-09-23 14:45:10 +00001233 llvm::Value *retTypeInfoIndex = builder.CreateExtractValue(caughtResult, 1);
Garrison Venn85500712011-09-22 15:45:14 +00001234
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001235 // FIXME: Redundant storage which, beyond utilizing value of
1236 // caughtResultStore for unwindException storage, may be alleviated
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00001237 // altogether with a block rearrangement
Garrison Venn9cb50862011-09-23 14:45:10 +00001238 builder.CreateStore(caughtResult, caughtResultStorage);
Garrison Venn85500712011-09-22 15:45:14 +00001239 builder.CreateStore(unwindException, exceptionStorage);
1240 builder.CreateStore(ourExceptionThrownState, exceptionCaughtFlag);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001241
1242 // Retrieve exception_class member from thrown exception
Chris Lattner626ab1c2011-04-08 18:02:51 +00001243 // (_Unwind_Exception instance). This member tells us whether or not
1244 // the exception is foreign.
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001245 llvm::Value *unwindExceptionClass =
David Blaikie337b35a2015-04-22 04:24:43 +00001246 builder.CreateLoad(builder.CreateStructGEP(
1247 ourUnwindExceptionType,
1248 builder.CreatePointerCast(unwindException,
1249 ourUnwindExceptionType->getPointerTo()),
1250 0));
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001251
Chris Lattner626ab1c2011-04-08 18:02:51 +00001252 // Branch to the externalExceptionBlock if the exception is foreign or
1253 // to a catch router if not. Either way the finally block will be run.
1254 builder.CreateCondBr(builder.CreateICmpEQ(unwindExceptionClass,
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001255 llvm::ConstantInt::get(builder.getInt64Ty(),
Chris Lattner626ab1c2011-04-08 18:02:51 +00001256 ourBaseExceptionClass)),
1257 exceptionRouteBlock,
1258 externalExceptionBlock);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001259
Chris Lattner626ab1c2011-04-08 18:02:51 +00001260 // External Exception Block
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001261
Chris Lattner626ab1c2011-04-08 18:02:51 +00001262 builder.SetInsertPoint(externalExceptionBlock);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001263
1264 generateStringPrint(context,
Chris Lattner626ab1c2011-04-08 18:02:51 +00001265 module,
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001266 builder,
Chris Lattner626ab1c2011-04-08 18:02:51 +00001267 "Gen: Foreign exception received.\n",
1268 USE_GLOBAL_STR_CONSTS);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001269
Chris Lattner626ab1c2011-04-08 18:02:51 +00001270 // Branch to the finally block
1271 builder.CreateBr(finallyBlock);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001272
Chris Lattner626ab1c2011-04-08 18:02:51 +00001273 // Exception Route Block
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001274
Chris Lattner626ab1c2011-04-08 18:02:51 +00001275 builder.SetInsertPoint(exceptionRouteBlock);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001276
1277 // Casts exception pointer (_Unwind_Exception instance) to parent
Chris Lattner626ab1c2011-04-08 18:02:51 +00001278 // (OurException instance).
1279 //
1280 // Note: ourBaseFromUnwindOffset is usually negative
Garrison Venn9cb50862011-09-23 14:45:10 +00001281 llvm::Value *typeInfoThrown = builder.CreatePointerCast(
1282 builder.CreateConstGEP1_64(unwindException,
Chris Lattner626ab1c2011-04-08 18:02:51 +00001283 ourBaseFromUnwindOffset),
Garrison Venn9cb50862011-09-23 14:45:10 +00001284 ourExceptionType->getPointerTo());
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001285
Chris Lattner626ab1c2011-04-08 18:02:51 +00001286 // Retrieve thrown exception type info type
1287 //
1288 // Note: Index is not relative to pointer but instead to structure
1289 // unlike a true getelementptr (GEP) instruction
David Blaikie337b35a2015-04-22 04:24:43 +00001290 typeInfoThrown = builder.CreateStructGEP(ourExceptionType, typeInfoThrown, 0);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001291
1292 llvm::Value *typeInfoThrownType =
David Blaikie337b35a2015-04-22 04:24:43 +00001293 builder.CreateStructGEP(builder.getInt8PtrTy(), typeInfoThrown, 0);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001294
1295 generateIntegerPrint(context,
Chris Lattner626ab1c2011-04-08 18:02:51 +00001296 module,
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001297 builder,
1298 *toPrint32Int,
Chris Lattner626ab1c2011-04-08 18:02:51 +00001299 *(builder.CreateLoad(typeInfoThrownType)),
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001300 "Gen: Exception type <%d> received (stack unwound) "
1301 " in " +
1302 ourId +
Chris Lattner626ab1c2011-04-08 18:02:51 +00001303 ".\n",
1304 USE_GLOBAL_STR_CONSTS);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001305
Chris Lattner626ab1c2011-04-08 18:02:51 +00001306 // Route to matched type info catch block or run cleanup finally block
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001307 llvm::SwitchInst *switchToCatchBlock = builder.CreateSwitch(retTypeInfoIndex,
1308 finallyBlock,
Garrison Venn9cb50862011-09-23 14:45:10 +00001309 numExceptionsToCatch);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001310
Chris Lattner626ab1c2011-04-08 18:02:51 +00001311 unsigned nextTypeToCatch;
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001312
Chris Lattner626ab1c2011-04-08 18:02:51 +00001313 for (unsigned i = 1; i <= numExceptionsToCatch; ++i) {
1314 nextTypeToCatch = i - 1;
1315 switchToCatchBlock->addCase(llvm::ConstantInt::get(
1316 llvm::Type::getInt32Ty(context), i),
1317 catchBlocks[nextTypeToCatch]);
1318 }
Garrison Vennaae66fa2011-09-22 14:07:50 +00001319
Chris Lattner626ab1c2011-04-08 18:02:51 +00001320 llvm::verifyFunction(*ret);
1321 fpm.run(*ret);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001322
Chris Lattner626ab1c2011-04-08 18:02:51 +00001323 return(ret);
Garrison Venna2c2f1a2010-02-09 23:22:43 +00001324}
1325
1326
1327/// Generates function which throws either an exception matched to a runtime
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001328/// determined type info type (argument to generated function), or if this
1329/// runtime value matches nativeThrowType, throws a foreign exception by
Garrison Venna2c2f1a2010-02-09 23:22:43 +00001330/// calling nativeThrowFunct.
1331/// @param module code for module instance
1332/// @param builder builder instance
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001333/// @param fpm a function pass manager holding optional IR to IR
Garrison Venna2c2f1a2010-02-09 23:22:43 +00001334/// transformations
1335/// @param ourId id used to printing purposes
1336/// @param nativeThrowType a runtime argument of this value results in
1337/// nativeThrowFunct being called to generate/throw exception.
1338/// @param nativeThrowFunct function which will throw a foreign exception
1339/// if the above nativeThrowType matches generated function's arg.
1340/// @returns generated function
Chandler Carruth7ea33e52015-02-13 10:21:05 +00001341static llvm::Function *
1342createThrowExceptionFunction(llvm::Module &module, llvm::IRBuilder<> &builder,
1343 llvm::legacy::FunctionPassManager &fpm,
1344 std::string ourId, int32_t nativeThrowType,
1345 llvm::Function &nativeThrowFunct) {
Garrison Venn64cfcef2011-04-10 14:06:52 +00001346 llvm::LLVMContext &context = module.getContext();
Chris Lattner626ab1c2011-04-08 18:02:51 +00001347 namedValues.clear();
1348 ArgTypes unwindArgTypes;
Garrison Vennc0f33cb2011-07-12 15:34:42 +00001349 unwindArgTypes.push_back(builder.getInt32Ty());
Chris Lattner626ab1c2011-04-08 18:02:51 +00001350 ArgNames unwindArgNames;
1351 unwindArgNames.push_back("exceptTypeToThrow");
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001352
Chris Lattner626ab1c2011-04-08 18:02:51 +00001353 llvm::Function *ret = createFunction(module,
1354 builder.getVoidTy(),
1355 unwindArgTypes,
1356 unwindArgNames,
1357 ourId,
1358 llvm::Function::ExternalLinkage,
1359 false,
1360 false);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001361
Chris Lattner626ab1c2011-04-08 18:02:51 +00001362 // Throws either one of our exception or a native C++ exception depending
1363 // on a runtime argument value containing a type info type.
1364 llvm::BasicBlock *entryBlock = llvm::BasicBlock::Create(context,
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001365 "entry",
Chris Lattner626ab1c2011-04-08 18:02:51 +00001366 ret);
1367 // Throws a foreign exception
Garrison Venn9cb50862011-09-23 14:45:10 +00001368 llvm::BasicBlock *nativeThrowBlock = llvm::BasicBlock::Create(context,
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001369 "nativeThrow",
Garrison Venn9cb50862011-09-23 14:45:10 +00001370 ret);
Chris Lattner626ab1c2011-04-08 18:02:51 +00001371 // Throws one of our Exceptions
Garrison Venn9cb50862011-09-23 14:45:10 +00001372 llvm::BasicBlock *generatedThrowBlock = llvm::BasicBlock::Create(context,
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001373 "generatedThrow",
Garrison Venn9cb50862011-09-23 14:45:10 +00001374 ret);
Chris Lattner626ab1c2011-04-08 18:02:51 +00001375 // Retrieved runtime type info type to throw
Garrison Venn64cfcef2011-04-10 14:06:52 +00001376 llvm::Value *exceptionType = namedValues["exceptTypeToThrow"];
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001377
Chris Lattner626ab1c2011-04-08 18:02:51 +00001378 // nativeThrowBlock block
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001379
Chris Lattner626ab1c2011-04-08 18:02:51 +00001380 builder.SetInsertPoint(nativeThrowBlock);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001381
Chris Lattner626ab1c2011-04-08 18:02:51 +00001382 // Throws foreign exception
1383 builder.CreateCall(&nativeThrowFunct, exceptionType);
1384 builder.CreateUnreachable();
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001385
Chris Lattner626ab1c2011-04-08 18:02:51 +00001386 // entry block
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001387
Chris Lattner626ab1c2011-04-08 18:02:51 +00001388 builder.SetInsertPoint(entryBlock);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001389
Chris Lattner626ab1c2011-04-08 18:02:51 +00001390 llvm::Function *toPrint32Int = module.getFunction("print32Int");
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001391 generateIntegerPrint(context,
Chris Lattner626ab1c2011-04-08 18:02:51 +00001392 module,
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001393 builder,
1394 *toPrint32Int,
1395 *exceptionType,
1396 "\nGen: About to throw exception type <%d> in " +
1397 ourId +
Chris Lattner626ab1c2011-04-08 18:02:51 +00001398 ".\n",
1399 USE_GLOBAL_STR_CONSTS);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001400
Chris Lattner626ab1c2011-04-08 18:02:51 +00001401 // Switches on runtime type info type value to determine whether or not
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001402 // a foreign exception is thrown. Defaults to throwing one of our
Chris Lattner626ab1c2011-04-08 18:02:51 +00001403 // generated exceptions.
Garrison Venn64cfcef2011-04-10 14:06:52 +00001404 llvm::SwitchInst *theSwitch = builder.CreateSwitch(exceptionType,
Chris Lattner626ab1c2011-04-08 18:02:51 +00001405 generatedThrowBlock,
1406 1);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001407
1408 theSwitch->addCase(llvm::ConstantInt::get(llvm::Type::getInt32Ty(context),
Chris Lattner626ab1c2011-04-08 18:02:51 +00001409 nativeThrowType),
1410 nativeThrowBlock);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001411
Chris Lattner626ab1c2011-04-08 18:02:51 +00001412 // generatedThrow block
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001413
Chris Lattner626ab1c2011-04-08 18:02:51 +00001414 builder.SetInsertPoint(generatedThrowBlock);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001415
Garrison Venn9cb50862011-09-23 14:45:10 +00001416 llvm::Function *createOurException = module.getFunction("createOurException");
1417 llvm::Function *raiseOurException = module.getFunction(
1418 "_Unwind_RaiseException");
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001419
Chris Lattner626ab1c2011-04-08 18:02:51 +00001420 // Creates exception to throw with runtime type info type.
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001421 llvm::Value *exception = builder.CreateCall(createOurException,
Garrison Venn9cb50862011-09-23 14:45:10 +00001422 namedValues["exceptTypeToThrow"]);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001423
Chris Lattner626ab1c2011-04-08 18:02:51 +00001424 // Throw generated Exception
1425 builder.CreateCall(raiseOurException, exception);
1426 builder.CreateUnreachable();
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001427
Chris Lattner626ab1c2011-04-08 18:02:51 +00001428 llvm::verifyFunction(*ret);
1429 fpm.run(*ret);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001430
Chris Lattner626ab1c2011-04-08 18:02:51 +00001431 return(ret);
Garrison Venna2c2f1a2010-02-09 23:22:43 +00001432}
1433
1434static void createStandardUtilityFunctions(unsigned numTypeInfos,
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001435 llvm::Module &module,
Garrison Venn64cfcef2011-04-10 14:06:52 +00001436 llvm::IRBuilder<> &builder);
Garrison Venna2c2f1a2010-02-09 23:22:43 +00001437
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001438/// Creates test code by generating and organizing these functions into the
Garrison Venna2c2f1a2010-02-09 23:22:43 +00001439/// test case. The test case consists of an outer function setup to invoke
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001440/// an inner function within an environment having multiple catch and single
Garrison Venna2c2f1a2010-02-09 23:22:43 +00001441/// finally blocks. This inner function is also setup to invoke a throw
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001442/// function within an evironment similar in nature to the outer function's
Garrison Venna2c2f1a2010-02-09 23:22:43 +00001443/// catch and finally blocks. Each of these two functions catch mutually
1444/// exclusive subsets (even or odd) of the type info types configured
1445/// for this this. All generated functions have a runtime argument which
1446/// holds a type info type to throw that each function takes and passes it
1447/// to the inner one if such a inner function exists. This type info type is
1448/// looked at by the generated throw function to see whether or not it should
1449/// throw a generated exception with the same type info type, or instead call
1450/// a supplied a function which in turn will throw a foreign exception.
1451/// @param module code for module instance
1452/// @param builder builder instance
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001453/// @param fpm a function pass manager holding optional IR to IR
Garrison Venna2c2f1a2010-02-09 23:22:43 +00001454/// transformations
1455/// @param nativeThrowFunctName name of external function which will throw
1456/// a foreign exception
1457/// @returns outermost generated test function.
Chandler Carruth7ea33e52015-02-13 10:21:05 +00001458llvm::Function *
1459createUnwindExceptionTest(llvm::Module &module, llvm::IRBuilder<> &builder,
1460 llvm::legacy::FunctionPassManager &fpm,
1461 std::string nativeThrowFunctName) {
Chris Lattner626ab1c2011-04-08 18:02:51 +00001462 // Number of type infos to generate
1463 unsigned numTypeInfos = 6;
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001464
Chris Lattner626ab1c2011-04-08 18:02:51 +00001465 // Initialze intrisics and external functions to use along with exception
1466 // and type info globals.
1467 createStandardUtilityFunctions(numTypeInfos,
1468 module,
1469 builder);
Garrison Venn9cb50862011-09-23 14:45:10 +00001470 llvm::Function *nativeThrowFunct = module.getFunction(nativeThrowFunctName);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001471
1472 // Create exception throw function using the value ~0 to cause
Chris Lattner626ab1c2011-04-08 18:02:51 +00001473 // foreign exceptions to be thrown.
Garrison Venn9cb50862011-09-23 14:45:10 +00001474 llvm::Function *throwFunct = createThrowExceptionFunction(module,
1475 builder,
1476 fpm,
1477 "throwFunct",
1478 ~0,
1479 *nativeThrowFunct);
Chris Lattner626ab1c2011-04-08 18:02:51 +00001480 // Inner function will catch even type infos
1481 unsigned innerExceptionTypesToCatch[] = {6, 2, 4};
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001482 size_t numExceptionTypesToCatch = sizeof(innerExceptionTypesToCatch) /
Garrison Venn9cb50862011-09-23 14:45:10 +00001483 sizeof(unsigned);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001484
Chris Lattner626ab1c2011-04-08 18:02:51 +00001485 // Generate inner function.
Garrison Venn9cb50862011-09-23 14:45:10 +00001486 llvm::Function *innerCatchFunct = createCatchWrappedInvokeFunction(module,
1487 builder,
1488 fpm,
1489 *throwFunct,
1490 "innerCatchFunct",
1491 numExceptionTypesToCatch,
1492 innerExceptionTypesToCatch);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001493
Chris Lattner626ab1c2011-04-08 18:02:51 +00001494 // Outer function will catch odd type infos
1495 unsigned outerExceptionTypesToCatch[] = {3, 1, 5};
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001496 numExceptionTypesToCatch = sizeof(outerExceptionTypesToCatch) /
Chris Lattner626ab1c2011-04-08 18:02:51 +00001497 sizeof(unsigned);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001498
Chris Lattner626ab1c2011-04-08 18:02:51 +00001499 // Generate outer function
Garrison Venn9cb50862011-09-23 14:45:10 +00001500 llvm::Function *outerCatchFunct = createCatchWrappedInvokeFunction(module,
1501 builder,
1502 fpm,
1503 *innerCatchFunct,
1504 "outerCatchFunct",
1505 numExceptionTypesToCatch,
1506 outerExceptionTypesToCatch);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001507
Chris Lattner626ab1c2011-04-08 18:02:51 +00001508 // Return outer function to run
1509 return(outerCatchFunct);
Garrison Venna2c2f1a2010-02-09 23:22:43 +00001510}
1511
Juergen Ributzkaba0f9912013-11-19 03:08:35 +00001512namespace {
Garrison Venna2c2f1a2010-02-09 23:22:43 +00001513/// Represents our foreign exceptions
1514class OurCppRunException : public std::runtime_error {
1515public:
Chris Lattner626ab1c2011-04-08 18:02:51 +00001516 OurCppRunException(const std::string reason) :
1517 std::runtime_error(reason) {}
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001518
Garrison Venn64cfcef2011-04-10 14:06:52 +00001519 OurCppRunException (const OurCppRunException &toCopy) :
Chris Lattner626ab1c2011-04-08 18:02:51 +00001520 std::runtime_error(toCopy) {}
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001521
Garrison Venn64cfcef2011-04-10 14:06:52 +00001522 OurCppRunException &operator = (const OurCppRunException &toCopy) {
Chris Lattner626ab1c2011-04-08 18:02:51 +00001523 return(reinterpret_cast<OurCppRunException&>(
1524 std::runtime_error::operator=(toCopy)));
1525 }
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001526
Alexander Kornienkoc16fc542015-04-11 02:11:45 +00001527 ~OurCppRunException(void) throw() override {}
Garrison Venna2c2f1a2010-02-09 23:22:43 +00001528};
Juergen Ributzkaba0f9912013-11-19 03:08:35 +00001529} // end anonymous namespace
Garrison Venna2c2f1a2010-02-09 23:22:43 +00001530
1531/// Throws foreign C++ exception.
1532/// @param ignoreIt unused parameter that allows function to match implied
1533/// generated function contract.
1534extern "C"
1535void throwCppException (int32_t ignoreIt) {
Chris Lattner626ab1c2011-04-08 18:02:51 +00001536 throw(OurCppRunException("thrown by throwCppException(...)"));
Garrison Venna2c2f1a2010-02-09 23:22:43 +00001537}
1538
1539typedef void (*OurExceptionThrowFunctType) (int32_t typeToThrow);
1540
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001541/// This is a test harness which runs test by executing generated
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001542/// function with a type info type to throw. Harness wraps the execution
Garrison Venna2c2f1a2010-02-09 23:22:43 +00001543/// of generated function in a C++ try catch clause.
1544/// @param engine execution engine to use for executing generated function.
1545/// This demo program expects this to be a JIT instance for demo
1546/// purposes.
1547/// @param function generated test function to run
1548/// @param typeToThrow type info type of generated exception to throw, or
1549/// indicator to cause foreign exception to be thrown.
1550static
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001551void runExceptionThrow(llvm::ExecutionEngine *engine,
1552 llvm::Function *function,
Garrison Venna2c2f1a2010-02-09 23:22:43 +00001553 int32_t typeToThrow) {
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001554
Chris Lattner626ab1c2011-04-08 18:02:51 +00001555 // Find test's function pointer
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001556 OurExceptionThrowFunctType functPtr =
Chris Lattner626ab1c2011-04-08 18:02:51 +00001557 reinterpret_cast<OurExceptionThrowFunctType>(
1558 reinterpret_cast<intptr_t>(engine->getPointerToFunction(function)));
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001559
Chris Lattner626ab1c2011-04-08 18:02:51 +00001560 try {
1561 // Run test
1562 (*functPtr)(typeToThrow);
1563 }
1564 catch (OurCppRunException exc) {
1565 // Catch foreign C++ exception
1566 fprintf(stderr,
1567 "\nrunExceptionThrow(...):In C++ catch OurCppRunException "
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001568 "with reason: %s.\n",
Chris Lattner626ab1c2011-04-08 18:02:51 +00001569 exc.what());
1570 }
1571 catch (...) {
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001572 // Catch all exceptions including our generated ones. This latter
Garrison Venn113aa862011-09-28 10:53:56 +00001573 // functionality works according to the example in rules 1.6.4 of
Vlad Tsyrklevich30b47382017-09-12 00:19:11 +00001574 // http://itanium-cxx-abi.github.io/cxx-abi/abi-eh.html (v1.22),
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001575 // given that these will be exceptions foreign to C++
1576 // (the _Unwind_Exception::exception_class should be different from
Garrison Venn113aa862011-09-28 10:53:56 +00001577 // the one used by C++).
Chris Lattner626ab1c2011-04-08 18:02:51 +00001578 fprintf(stderr,
1579 "\nrunExceptionThrow(...):In C++ catch all.\n");
1580 }
Garrison Venna2c2f1a2010-02-09 23:22:43 +00001581}
1582
1583//
1584// End test functions
1585//
1586
Garrison Venn6e6cdd02011-07-11 16:31:53 +00001587typedef llvm::ArrayRef<llvm::Type*> TypeArray;
Chris Lattnercad3f772011-04-08 17:56:47 +00001588
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001589/// This initialization routine creates type info globals and
Garrison Venna2c2f1a2010-02-09 23:22:43 +00001590/// adds external function declarations to module.
1591/// @param numTypeInfos number of linear type info associated type info types
1592/// to create as GlobalVariable instances, starting with the value 1.
1593/// @param module code for module instance
1594/// @param builder builder instance
1595static void createStandardUtilityFunctions(unsigned numTypeInfos,
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001596 llvm::Module &module,
Garrison Venn64cfcef2011-04-10 14:06:52 +00001597 llvm::IRBuilder<> &builder) {
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001598
Garrison Venn64cfcef2011-04-10 14:06:52 +00001599 llvm::LLVMContext &context = module.getContext();
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001600
Chris Lattner626ab1c2011-04-08 18:02:51 +00001601 // Exception initializations
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001602
Chris Lattner626ab1c2011-04-08 18:02:51 +00001603 // Setup exception catch state
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001604 ourExceptionNotThrownState =
Garrison Venn9cb50862011-09-23 14:45:10 +00001605 llvm::ConstantInt::get(llvm::Type::getInt8Ty(context), 0),
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001606 ourExceptionThrownState =
Garrison Venn9cb50862011-09-23 14:45:10 +00001607 llvm::ConstantInt::get(llvm::Type::getInt8Ty(context), 1),
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001608 ourExceptionCaughtState =
Garrison Venn9cb50862011-09-23 14:45:10 +00001609 llvm::ConstantInt::get(llvm::Type::getInt8Ty(context), 2),
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001610
1611
1612
Chris Lattner626ab1c2011-04-08 18:02:51 +00001613 // Create our type info type
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001614 ourTypeInfoType = llvm::StructType::get(context,
Garrison Venn9cb50862011-09-23 14:45:10 +00001615 TypeArray(builder.getInt32Ty()));
Garrison Venn85500712011-09-22 15:45:14 +00001616
Garrison Venn85500712011-09-22 15:45:14 +00001617 llvm::Type *caughtResultFieldTypes[] = {
1618 builder.getInt8PtrTy(),
1619 builder.getInt32Ty()
1620 };
1621
1622 // Create our landingpad result type
1623 ourCaughtResultType = llvm::StructType::get(context,
1624 TypeArray(caughtResultFieldTypes));
1625
Chris Lattner626ab1c2011-04-08 18:02:51 +00001626 // Create OurException type
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001627 ourExceptionType = llvm::StructType::get(context,
Chris Lattner626ab1c2011-04-08 18:02:51 +00001628 TypeArray(ourTypeInfoType));
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001629
Chris Lattner626ab1c2011-04-08 18:02:51 +00001630 // Create portion of _Unwind_Exception type
1631 //
1632 // Note: Declaring only a portion of the _Unwind_Exception struct.
1633 // Does this cause problems?
1634 ourUnwindExceptionType =
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001635 llvm::StructType::get(context,
Garrison Vennc0f33cb2011-07-12 15:34:42 +00001636 TypeArray(builder.getInt64Ty()));
Garrison Venn6e6cdd02011-07-11 16:31:53 +00001637
Chris Lattner626ab1c2011-04-08 18:02:51 +00001638 struct OurBaseException_t dummyException;
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001639
Chris Lattner626ab1c2011-04-08 18:02:51 +00001640 // Calculate offset of OurException::unwindException member.
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001641 ourBaseFromUnwindOffset = ((uintptr_t) &dummyException) -
Garrison Venn9cb50862011-09-23 14:45:10 +00001642 ((uintptr_t) &(dummyException.unwindException));
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001643
Garrison Venna2c2f1a2010-02-09 23:22:43 +00001644#ifdef DEBUG
Chris Lattner626ab1c2011-04-08 18:02:51 +00001645 fprintf(stderr,
1646 "createStandardUtilityFunctions(...):ourBaseFromUnwindOffset "
David Blaikie54b3a032015-08-14 00:31:49 +00001647 "= %" PRIi64 ", sizeof(struct OurBaseException_t) - "
Chris Lattner626ab1c2011-04-08 18:02:51 +00001648 "sizeof(struct _Unwind_Exception) = %lu.\n",
1649 ourBaseFromUnwindOffset,
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001650 sizeof(struct OurBaseException_t) -
Chris Lattner626ab1c2011-04-08 18:02:51 +00001651 sizeof(struct _Unwind_Exception));
Garrison Venna2c2f1a2010-02-09 23:22:43 +00001652#endif
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001653
Chris Lattner626ab1c2011-04-08 18:02:51 +00001654 size_t numChars = sizeof(ourBaseExcpClassChars) / sizeof(char);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001655
Chris Lattner626ab1c2011-04-08 18:02:51 +00001656 // Create our _Unwind_Exception::exception_class value
1657 ourBaseExceptionClass = genClass(ourBaseExcpClassChars, numChars);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001658
Chris Lattner626ab1c2011-04-08 18:02:51 +00001659 // Type infos
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001660
Chris Lattner626ab1c2011-04-08 18:02:51 +00001661 std::string baseStr = "typeInfo", typeInfoName;
1662 std::ostringstream typeInfoNameBuilder;
1663 std::vector<llvm::Constant*> structVals;
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001664
Chris Lattner626ab1c2011-04-08 18:02:51 +00001665 llvm::Constant *nextStruct;
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001666
Chris Lattner626ab1c2011-04-08 18:02:51 +00001667 // Generate each type info
1668 //
1669 // Note: First type info is not used.
1670 for (unsigned i = 0; i <= numTypeInfos; ++i) {
1671 structVals.clear();
1672 structVals.push_back(llvm::ConstantInt::get(builder.getInt32Ty(), i));
1673 nextStruct = llvm::ConstantStruct::get(ourTypeInfoType, structVals);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001674
Chris Lattner626ab1c2011-04-08 18:02:51 +00001675 typeInfoNameBuilder.str("");
1676 typeInfoNameBuilder << baseStr << i;
1677 typeInfoName = typeInfoNameBuilder.str();
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001678
Chris Lattner626ab1c2011-04-08 18:02:51 +00001679 // Note: Does not seem to work without allocation
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001680 new llvm::GlobalVariable(module,
1681 ourTypeInfoType,
1682 true,
1683 llvm::GlobalValue::ExternalLinkage,
1684 nextStruct,
Chris Lattner626ab1c2011-04-08 18:02:51 +00001685 typeInfoName);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001686
Chris Lattner626ab1c2011-04-08 18:02:51 +00001687 ourTypeInfoNames.push_back(typeInfoName);
1688 ourTypeInfoNamesIndex[i] = typeInfoName;
1689 }
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001690
Chris Lattner626ab1c2011-04-08 18:02:51 +00001691 ArgNames argNames;
1692 ArgTypes argTypes;
Garrison Venn64cfcef2011-04-10 14:06:52 +00001693 llvm::Function *funct = NULL;
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001694
Chris Lattner626ab1c2011-04-08 18:02:51 +00001695 // print32Int
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001696
Chris Lattner77613d42011-07-18 04:52:09 +00001697 llvm::Type *retType = builder.getVoidTy();
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001698
Chris Lattner626ab1c2011-04-08 18:02:51 +00001699 argTypes.clear();
Garrison Vennc0f33cb2011-07-12 15:34:42 +00001700 argTypes.push_back(builder.getInt32Ty());
1701 argTypes.push_back(builder.getInt8PtrTy());
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001702
Chris Lattner626ab1c2011-04-08 18:02:51 +00001703 argNames.clear();
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001704
1705 createFunction(module,
1706 retType,
1707 argTypes,
1708 argNames,
1709 "print32Int",
1710 llvm::Function::ExternalLinkage,
1711 true,
Chris Lattner626ab1c2011-04-08 18:02:51 +00001712 false);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001713
Chris Lattner626ab1c2011-04-08 18:02:51 +00001714 // print64Int
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001715
Chris Lattner626ab1c2011-04-08 18:02:51 +00001716 retType = builder.getVoidTy();
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001717
Chris Lattner626ab1c2011-04-08 18:02:51 +00001718 argTypes.clear();
Garrison Vennc0f33cb2011-07-12 15:34:42 +00001719 argTypes.push_back(builder.getInt64Ty());
1720 argTypes.push_back(builder.getInt8PtrTy());
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001721
Chris Lattner626ab1c2011-04-08 18:02:51 +00001722 argNames.clear();
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001723
1724 createFunction(module,
1725 retType,
1726 argTypes,
1727 argNames,
1728 "print64Int",
1729 llvm::Function::ExternalLinkage,
1730 true,
Chris Lattner626ab1c2011-04-08 18:02:51 +00001731 false);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001732
Chris Lattner626ab1c2011-04-08 18:02:51 +00001733 // printStr
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001734
Chris Lattner626ab1c2011-04-08 18:02:51 +00001735 retType = builder.getVoidTy();
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001736
Chris Lattner626ab1c2011-04-08 18:02:51 +00001737 argTypes.clear();
Garrison Vennc0f33cb2011-07-12 15:34:42 +00001738 argTypes.push_back(builder.getInt8PtrTy());
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001739
Chris Lattner626ab1c2011-04-08 18:02:51 +00001740 argNames.clear();
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001741
1742 createFunction(module,
1743 retType,
1744 argTypes,
1745 argNames,
1746 "printStr",
1747 llvm::Function::ExternalLinkage,
1748 true,
Chris Lattner626ab1c2011-04-08 18:02:51 +00001749 false);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001750
Chris Lattner626ab1c2011-04-08 18:02:51 +00001751 // throwCppException
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001752
Chris Lattner626ab1c2011-04-08 18:02:51 +00001753 retType = builder.getVoidTy();
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001754
Chris Lattner626ab1c2011-04-08 18:02:51 +00001755 argTypes.clear();
Garrison Vennc0f33cb2011-07-12 15:34:42 +00001756 argTypes.push_back(builder.getInt32Ty());
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001757
Chris Lattner626ab1c2011-04-08 18:02:51 +00001758 argNames.clear();
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001759
1760 createFunction(module,
1761 retType,
1762 argTypes,
1763 argNames,
1764 "throwCppException",
1765 llvm::Function::ExternalLinkage,
1766 true,
Chris Lattner626ab1c2011-04-08 18:02:51 +00001767 false);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001768
Chris Lattner626ab1c2011-04-08 18:02:51 +00001769 // deleteOurException
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001770
Chris Lattner626ab1c2011-04-08 18:02:51 +00001771 retType = builder.getVoidTy();
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001772
Chris Lattner626ab1c2011-04-08 18:02:51 +00001773 argTypes.clear();
Garrison Vennc0f33cb2011-07-12 15:34:42 +00001774 argTypes.push_back(builder.getInt8PtrTy());
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001775
Chris Lattner626ab1c2011-04-08 18:02:51 +00001776 argNames.clear();
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001777
1778 createFunction(module,
1779 retType,
1780 argTypes,
1781 argNames,
1782 "deleteOurException",
1783 llvm::Function::ExternalLinkage,
1784 true,
Chris Lattner626ab1c2011-04-08 18:02:51 +00001785 false);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001786
Chris Lattner626ab1c2011-04-08 18:02:51 +00001787 // createOurException
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001788
Garrison Vennc0f33cb2011-07-12 15:34:42 +00001789 retType = builder.getInt8PtrTy();
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001790
Chris Lattner626ab1c2011-04-08 18:02:51 +00001791 argTypes.clear();
Garrison Vennc0f33cb2011-07-12 15:34:42 +00001792 argTypes.push_back(builder.getInt32Ty());
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001793
Chris Lattner626ab1c2011-04-08 18:02:51 +00001794 argNames.clear();
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001795
1796 createFunction(module,
1797 retType,
1798 argTypes,
1799 argNames,
1800 "createOurException",
1801 llvm::Function::ExternalLinkage,
1802 true,
Chris Lattner626ab1c2011-04-08 18:02:51 +00001803 false);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001804
Chris Lattner626ab1c2011-04-08 18:02:51 +00001805 // _Unwind_RaiseException
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001806
Chris Lattner626ab1c2011-04-08 18:02:51 +00001807 retType = builder.getInt32Ty();
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001808
Chris Lattner626ab1c2011-04-08 18:02:51 +00001809 argTypes.clear();
Garrison Vennc0f33cb2011-07-12 15:34:42 +00001810 argTypes.push_back(builder.getInt8PtrTy());
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001811
Chris Lattner626ab1c2011-04-08 18:02:51 +00001812 argNames.clear();
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001813
1814 funct = createFunction(module,
1815 retType,
1816 argTypes,
1817 argNames,
1818 "_Unwind_RaiseException",
1819 llvm::Function::ExternalLinkage,
1820 true,
Chris Lattner626ab1c2011-04-08 18:02:51 +00001821 false);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001822
NAKAMURA Takumi4c856ee2012-10-12 14:11:48 +00001823 funct->setDoesNotReturn();
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001824
Chris Lattner626ab1c2011-04-08 18:02:51 +00001825 // _Unwind_Resume
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001826
Chris Lattner626ab1c2011-04-08 18:02:51 +00001827 retType = builder.getInt32Ty();
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001828
Chris Lattner626ab1c2011-04-08 18:02:51 +00001829 argTypes.clear();
Garrison Vennc0f33cb2011-07-12 15:34:42 +00001830 argTypes.push_back(builder.getInt8PtrTy());
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001831
Chris Lattner626ab1c2011-04-08 18:02:51 +00001832 argNames.clear();
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001833
1834 funct = createFunction(module,
1835 retType,
1836 argTypes,
1837 argNames,
1838 "_Unwind_Resume",
1839 llvm::Function::ExternalLinkage,
1840 true,
Chris Lattner626ab1c2011-04-08 18:02:51 +00001841 false);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001842
NAKAMURA Takumi4c856ee2012-10-12 14:11:48 +00001843 funct->setDoesNotReturn();
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001844
Chris Lattner626ab1c2011-04-08 18:02:51 +00001845 // ourPersonality
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001846
Chris Lattner626ab1c2011-04-08 18:02:51 +00001847 retType = builder.getInt32Ty();
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001848
Chris Lattner626ab1c2011-04-08 18:02:51 +00001849 argTypes.clear();
Garrison Vennc0f33cb2011-07-12 15:34:42 +00001850 argTypes.push_back(builder.getInt32Ty());
1851 argTypes.push_back(builder.getInt32Ty());
1852 argTypes.push_back(builder.getInt64Ty());
1853 argTypes.push_back(builder.getInt8PtrTy());
1854 argTypes.push_back(builder.getInt8PtrTy());
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001855
Chris Lattner626ab1c2011-04-08 18:02:51 +00001856 argNames.clear();
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001857
1858 createFunction(module,
1859 retType,
1860 argTypes,
1861 argNames,
1862 "ourPersonality",
1863 llvm::Function::ExternalLinkage,
1864 true,
Chris Lattner626ab1c2011-04-08 18:02:51 +00001865 false);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001866
Chris Lattner626ab1c2011-04-08 18:02:51 +00001867 // llvm.eh.typeid.for intrinsic
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001868
Chris Lattner626ab1c2011-04-08 18:02:51 +00001869 getDeclaration(&module, llvm::Intrinsic::eh_typeid_for);
Garrison Venna2c2f1a2010-02-09 23:22:43 +00001870}
1871
1872
Chris Lattner626ab1c2011-04-08 18:02:51 +00001873//===----------------------------------------------------------------------===//
Garrison Venna2c2f1a2010-02-09 23:22:43 +00001874// Main test driver code.
Chris Lattner626ab1c2011-04-08 18:02:51 +00001875//===----------------------------------------------------------------------===//
Garrison Venna2c2f1a2010-02-09 23:22:43 +00001876
1877/// Demo main routine which takes the type info types to throw. A test will
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001878/// be run for each given type info type. While type info types with the value
Garrison Venna2c2f1a2010-02-09 23:22:43 +00001879/// of -1 will trigger a foreign C++ exception to be thrown; type info types
1880/// <= 6 and >= 1 will be caught by test functions; and type info types > 6
1881/// will result in exceptions which pass through to the test harness. All other
1882/// type info types are not supported and could cause a crash.
Garrison Venn64cfcef2011-04-10 14:06:52 +00001883int main(int argc, char *argv[]) {
Chris Lattner626ab1c2011-04-08 18:02:51 +00001884 if (argc == 1) {
1885 fprintf(stderr,
1886 "\nUsage: ExceptionDemo <exception type to throw> "
1887 "[<type 2>...<type n>].\n"
1888 " Each type must have the value of 1 - 6 for "
1889 "generated exceptions to be caught;\n"
1890 " the value -1 for foreign C++ exceptions to be "
1891 "generated and thrown;\n"
1892 " or the values > 6 for exceptions to be ignored.\n"
1893 "\nTry: ExceptionDemo 2 3 7 -1\n"
1894 " for a full test.\n\n");
1895 return(0);
1896 }
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001897
Chris Lattner626ab1c2011-04-08 18:02:51 +00001898 // If not set, exception handling will not be turned on
Peter Collingbourned40e1032011-12-07 23:58:57 +00001899 llvm::TargetOptions Opts;
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001900
Chris Lattner626ab1c2011-04-08 18:02:51 +00001901 llvm::InitializeNativeTarget();
Rafael Espindolac8b95512013-05-05 20:57:58 +00001902 llvm::InitializeNativeTargetAsmPrinter();
Mehdi Amini8be77072016-04-14 21:59:01 +00001903 llvm::LLVMContext Context;
1904 llvm::IRBuilder<> theBuilder(Context);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001905
Chris Lattner626ab1c2011-04-08 18:02:51 +00001906 // Make the module, which holds all the code.
Rafael Espindola3f4ed322014-08-19 04:04:25 +00001907 std::unique_ptr<llvm::Module> Owner =
Mehdi Amini8be77072016-04-14 21:59:01 +00001908 llvm::make_unique<llvm::Module>("my cool jit", Context);
Rafael Espindola3f4ed322014-08-19 04:04:25 +00001909 llvm::Module *module = Owner.get();
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001910
NAKAMURA Takumi8cc929e2014-12-03 02:05:51 +00001911 std::unique_ptr<llvm::RTDyldMemoryManager> MemMgr(new llvm::SectionMemoryManager());
Rafael Espindolac8b95512013-05-05 20:57:58 +00001912
Chris Lattner626ab1c2011-04-08 18:02:51 +00001913 // Build engine with JIT
Rafael Espindola3f4ed322014-08-19 04:04:25 +00001914 llvm::EngineBuilder factory(std::move(Owner));
Chris Lattner626ab1c2011-04-08 18:02:51 +00001915 factory.setEngineKind(llvm::EngineKind::JIT);
Peter Collingbourned40e1032011-12-07 23:58:57 +00001916 factory.setTargetOptions(Opts);
NAKAMURA Takumi8cc929e2014-12-03 02:05:51 +00001917 factory.setMCJITMemoryManager(std::move(MemMgr));
Garrison Venn64cfcef2011-04-10 14:06:52 +00001918 llvm::ExecutionEngine *executionEngine = factory.create();
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001919
Chris Lattner626ab1c2011-04-08 18:02:51 +00001920 {
Chandler Carruth7ea33e52015-02-13 10:21:05 +00001921 llvm::legacy::FunctionPassManager fpm(module);
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001922
1923 // Set up the optimizer pipeline.
Chris Lattner626ab1c2011-04-08 18:02:51 +00001924 // Start with registering info about how the
1925 // target lays out data structures.
David Blaikie00c293b2015-08-14 00:24:56 +00001926 module->setDataLayout(executionEngine->getDataLayout());
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001927
Chris Lattner626ab1c2011-04-08 18:02:51 +00001928 // Optimizations turned on
1929#ifdef ADD_OPT_PASSES
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001930
Chris Lattner626ab1c2011-04-08 18:02:51 +00001931 // Basic AliasAnslysis support for GVN.
1932 fpm.add(llvm::createBasicAliasAnalysisPass());
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001933
Chris Lattner626ab1c2011-04-08 18:02:51 +00001934 // Promote allocas to registers.
1935 fpm.add(llvm::createPromoteMemoryToRegisterPass());
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001936
Chris Lattner626ab1c2011-04-08 18:02:51 +00001937 // Do simple "peephole" optimizations and bit-twiddling optzns.
1938 fpm.add(llvm::createInstructionCombiningPass());
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001939
Chris Lattner626ab1c2011-04-08 18:02:51 +00001940 // Reassociate expressions.
1941 fpm.add(llvm::createReassociatePass());
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001942
Chris Lattner626ab1c2011-04-08 18:02:51 +00001943 // Eliminate Common SubExpressions.
1944 fpm.add(llvm::createGVNPass());
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001945
1946 // Simplify the control flow graph (deleting unreachable
Chris Lattner626ab1c2011-04-08 18:02:51 +00001947 // blocks, etc).
1948 fpm.add(llvm::createCFGSimplificationPass());
1949#endif // ADD_OPT_PASSES
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001950
Chris Lattner626ab1c2011-04-08 18:02:51 +00001951 fpm.doInitialization();
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001952
Chris Lattner626ab1c2011-04-08 18:02:51 +00001953 // Generate test code using function throwCppException(...) as
1954 // the function which throws foreign exceptions.
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001955 llvm::Function *toRun =
1956 createUnwindExceptionTest(*module,
1957 theBuilder,
Garrison Venn85500712011-09-22 15:45:14 +00001958 fpm,
1959 "throwCppException");
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001960
Rafael Espindolac8b95512013-05-05 20:57:58 +00001961 executionEngine->finalizeObject();
1962
Chris Lattner626ab1c2011-04-08 18:02:51 +00001963 fprintf(stderr, "\nBegin module dump:\n\n");
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001964
Chris Lattner626ab1c2011-04-08 18:02:51 +00001965 module->dump();
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001966
Chris Lattner626ab1c2011-04-08 18:02:51 +00001967 fprintf(stderr, "\nEnd module dump:\n");
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001968
Chris Lattner626ab1c2011-04-08 18:02:51 +00001969 fprintf(stderr, "\n\nBegin Test:\n");
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001970
Chris Lattner626ab1c2011-04-08 18:02:51 +00001971 for (int i = 1; i < argc; ++i) {
1972 // Run test for each argument whose value is the exception
1973 // type to throw.
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001974 runExceptionThrow(executionEngine,
1975 toRun,
Chris Lattner626ab1c2011-04-08 18:02:51 +00001976 (unsigned) strtoul(argv[i], NULL, 10));
1977 }
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001978
Chris Lattner626ab1c2011-04-08 18:02:51 +00001979 fprintf(stderr, "\nEnd Test:\n\n");
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001980 }
1981
Chris Lattner626ab1c2011-04-08 18:02:51 +00001982 delete executionEngine;
NAKAMURA Takumi9f469a02012-10-12 14:11:43 +00001983
Chris Lattner626ab1c2011-04-08 18:02:51 +00001984 return 0;
Garrison Venna2c2f1a2010-02-09 23:22:43 +00001985}