blob: 8bc13e7659f923e673fb9fbb97c758a83ae99e13 [file] [log] [blame]
Chris Lattner7b3092d2003-08-30 23:31:08 +00001//===- SJLJ-Exception.cpp - SetJmp/LongJmp Exception Handling -------------===//
2//
3// This file implements the API used by the Setjmp/Longjmp exception handling
4// runtime library.
5//
6//===----------------------------------------------------------------------===//
Chris Lattner214191f2003-08-30 23:29:22 +00007
8#include "SJLJ-Exception.h"
9#include <cstdlib>
10#include <cassert>
11
Chris Lattner7b3092d2003-08-30 23:31:08 +000012// get_sjlj_exception - Adjust the llvm_exception pointer to be an appropriate
13// llvm_sjlj_exception pointer.
Chris Lattner214191f2003-08-30 23:29:22 +000014inline llvm_sjlj_exception *get_sjlj_exception(llvm_exception *E) {
15 assert(E->ExceptionType == SJLJException);
16 return (llvm_sjlj_exception*)(E+1) - 1;
17}
18
19// SetJmpMapEntry - One entry in a linked list of setjmps for the current
20// function.
21struct SetJmpMapEntry {
22 void *JmpBuf;
23 unsigned SetJmpID;
24 SetJmpMapEntry *Next;
25};
26
Chris Lattner7b3092d2003-08-30 23:31:08 +000027// SJLJDestructor - This function is used to free the exception when
28// language-indent code needs to destroy the exception without knowing exactly
29// what type it is.
Chris Lattner214191f2003-08-30 23:29:22 +000030static void SJLJDestructor(llvm_exception *E) {
31 free(get_sjlj_exception(E));
32}
33
34
35// __llvm_sjljeh_throw_longjmp - This function creates the longjmp exception and
36// returns. It takes care of mapping the longjmp value from 0 -> 1 as
37// appropriate. The caller should immediately call llvm.unwind after this
38// function call.
39void __llvm_sjljeh_throw_longjmp(void *JmpBuffer, int Val) throw() {
40 llvm_sjlj_exception *E =
41 (llvm_sjlj_exception *)malloc(sizeof(llvm_sjlj_exception));
42 E->BaseException.ExceptionDestructor = SJLJDestructor;
43 E->BaseException.ExceptionType = SJLJException;
44 E->BaseException.HandlerCount = 0;
45 E->BaseException.isRethrown = 0;
46 E->JmpBuffer = JmpBuffer;
47 E->LongJmpValue = Val ? Val : 1;
48
49 __llvm_eh_add_uncaught_exception(&E->BaseException);
50}
51
52// __llvm_sjljeh_init_setjmpmap - This funciton initializes the pointer provided
53// to an empty setjmp map, and should be called on entry to a function which
54// calls setjmp.
55void __llvm_sjljeh_init_setjmpmap(void **SetJmpMap) throw() {
56 *SetJmpMap = 0;
57}
58
59// __llvm_sjljeh_destroy_setjmpmap - This function frees all memory associated
60// with the specified setjmpmap structure. It should be called on all exits
61// (returns or unwinds) from the function which calls ...init_setjmpmap.
62void __llvm_sjljeh_destroy_setjmpmap(void **SetJmpMap) throw() {
63 SetJmpMapEntry *Next;
64 for (SetJmpMapEntry *SJE = *(SetJmpMapEntry**)SetJmpMap; SJE; SJE = Next) {
65 Next = SJE->Next;
66 free(SJE);
67 }
68}
69
70// __llvm_sjljeh_add_setjmp_to_map - This function adds or updates an entry to
71// the map, to indicate which setjmp should be returned to if a longjmp happens.
72void __llvm_sjljeh_add_setjmp_to_map(void **SetJmpMap, void *JmpBuf,
73 unsigned SetJmpID) throw() {
74 SetJmpMapEntry **SJE = (SetJmpMapEntry**)SetJmpMap;
75
76 // Scan for a pre-existing entry...
77 for (; *SJE; SJE = &(*SJE)->Next)
78 if ((*SJE)->JmpBuf == JmpBuf) {
79 (*SJE)->SetJmpID = SetJmpID;
80 return;
81 }
82
83 // No prexisting entry found, append to the end of the list...
84 SetJmpMapEntry *New = (SetJmpMapEntry *)malloc(sizeof(SetJmpMapEntry));
85 *SJE = New;
86 New->JmpBuf = JmpBuf;
87 New->SetJmpID = SetJmpID;
88 New->Next = 0;
89}
90
91// __llvm_sjljeh_is_longjmp_exception - This function returns true if the
92// current uncaught exception is a longjmp exception. This is the first step of
93// catching a sjlj exception.
94bool __llvm_sjljeh_is_longjmp_exception() throw() {
95 return __llvm_eh_current_uncaught_exception_type(SJLJException) != 0;
96}
97
98// __llvm_sjljeh_get_longjmp_value - This function returns the value that the
99// setjmp call should "return". This requires that the current uncaught
100// exception be a sjlj exception, though it does not require the exception to be
101// caught by this function.
102int __llvm_sjljeh_get_longjmp_value() throw() {
103 llvm_sjlj_exception *E =
104 get_sjlj_exception(__llvm_eh_get_uncaught_exception());
105 return E->LongJmpValue;
106}
107
108// __llvm_sjljeh_try_catching_longjmp_exception - This function checks to see if
109// the current uncaught longjmp exception matches any of the setjmps collected
110// in the setjmpmap structure. If so, it catches and destroys the exception,
111// returning the index of the setjmp which caught the exception. If not, it
112// leaves the exception uncaught and returns a value of ~0.
113unsigned __llvm_sjljeh_try_catching_longjmp_exception(void **SetJmpMap) throw(){
114 llvm_sjlj_exception *E =
115 get_sjlj_exception(__llvm_eh_get_uncaught_exception());
116
117 // Scan for a matching entry in the SetJmpMap...
118 SetJmpMapEntry *SJE = *(SetJmpMapEntry**)SetJmpMap;
119 for (; SJE; SJE = SJE->Next)
120 if (SJE->JmpBuf == E->JmpBuffer) {
121 // "Catch" and destroy the exception...
122 __llvm_eh_pop_from_uncaught_stack();
123
124 // We know it's a longjmp exception, so we can just free it instead of
125 // calling the destructor.
126 free(E);
127
128 // Return the setjmp ID which we should branch to...
129 return SJE->SetJmpID;
130 }
131
132 // No setjmp in this function catches the exception!
133 return ~0;
134}