[WinEH] Add 32-bit SEH state table emission prototype
[oota-llvm.git] / test / CodeGen / X86 / seh-safe-div-win32.ll
1 ; RUN: llc -mtriple i686-pc-windows-msvc < %s | FileCheck %s
2
3 ; This test case is also intended to be run manually as a complete functional
4 ; test. It should link, print something, and exit zero rather than crashing.
5 ; It is the hypothetical lowering of a C source program that looks like:
6 ;
7 ;   int safe_div(int *n, int *d) {
8 ;     int r;
9 ;     __try {
10 ;       __try {
11 ;         r = *n / *d;
12 ;       } __except(GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION) {
13 ;         puts("EXCEPTION_ACCESS_VIOLATION");
14 ;         r = -1;
15 ;       }
16 ;     } __except(GetExceptionCode() == EXCEPTION_INT_DIVIDE_BY_ZERO) {
17 ;       puts("EXCEPTION_INT_DIVIDE_BY_ZERO");
18 ;       r = -2;
19 ;     }
20 ;     return r;
21 ;   }
22
23 @str1 = internal constant [27 x i8] c"EXCEPTION_ACCESS_VIOLATION\00"
24 @str2 = internal constant [29 x i8] c"EXCEPTION_INT_DIVIDE_BY_ZERO\00"
25
26 define i32 @safe_div(i32* %n, i32* %d) {
27 entry:
28   %r = alloca i32, align 4
29   store i32 42, i32* %r
30   invoke void @try_body(i32* %r, i32* %n, i32* %d)
31           to label %__try.cont unwind label %lpad
32
33 lpad:
34   %vals = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @_except_handler3 to i8*)
35           catch i8* bitcast (i32 ()* @safe_div_filt0 to i8*)
36           catch i8* bitcast (i32 ()* @safe_div_filt1 to i8*)
37   %ehptr = extractvalue { i8*, i32 } %vals, 0
38   %sel = extractvalue { i8*, i32 } %vals, 1
39   %filt0_val = call i32 @llvm.eh.typeid.for(i8* bitcast (i32 ()* @safe_div_filt0 to i8*))
40   %is_filt0 = icmp eq i32 %sel, %filt0_val
41   br i1 %is_filt0, label %handler0, label %eh.dispatch1
42
43 eh.dispatch1:
44   %filt1_val = call i32 @llvm.eh.typeid.for(i8* bitcast (i32 ()* @safe_div_filt1 to i8*))
45   %is_filt1 = icmp eq i32 %sel, %filt1_val
46   br i1 %is_filt1, label %handler1, label %eh.resume
47
48 handler0:
49   call void @puts(i8* getelementptr ([27 x i8], [27 x i8]* @str1, i32 0, i32 0))
50   store i32 -1, i32* %r, align 4
51   br label %__try.cont
52
53 handler1:
54   call void @puts(i8* getelementptr ([29 x i8], [29 x i8]* @str2, i32 0, i32 0))
55   store i32 -2, i32* %r, align 4
56   br label %__try.cont
57
58 eh.resume:
59   resume { i8*, i32 } %vals
60
61 __try.cont:
62   %safe_ret = load i32, i32* %r, align 4
63   ret i32 %safe_ret
64 }
65
66 ; Normal path code
67
68 ; CHECK: {{^}}_safe_div:
69 ; CHECK: movl $42, [[rloc:.*\(%ebp\)]]
70 ; CHECK: leal [[rloc]],
71 ; CHECK: calll _try_body
72 ; CHECK: [[cont_bb:LBB0_[0-9]+]]:
73 ; CHECK: movl [[rloc]], %eax
74 ; CHECK: retl
75
76 ; Landing pad code
77
78 ; CHECK: [[handler0:Ltmp[0-9]+]]: # Block address taken
79 ; CHECK: # %handler0
80 ; CHECK: calll _puts
81 ; CHECK: jmp [[cont_bb]]
82
83 ; CHECK: [[handler1:Ltmp[0-9]+]]: # Block address taken
84 ; CHECK: # %handler1
85 ; CHECK: calll _puts
86 ; CHECK: jmp [[cont_bb]]
87
88 ; CHECK: .section .xdata,"dr"
89 ; CHECK-NEXT: L__ehtable$safe_div:
90 ; CHECK-NEXT: .long -1
91 ; CHECK-NEXT: .long _safe_div_filt0
92 ; CHECK-NEXT: .long [[handler0]]
93 ; CHECK-NEXT: .long 0
94 ; CHECK-NEXT: .long _safe_div_filt1
95 ; CHECK-NEXT: .long [[handler1]]
96
97 define void @try_body(i32* %r, i32* %n, i32* %d) {
98 entry:
99   %0 = load i32, i32* %n, align 4
100   %1 = load i32, i32* %d, align 4
101   %div = sdiv i32 %0, %1
102   store i32 %div, i32* %r, align 4
103   ret void
104 }
105
106 ; The prototype of these filter functions is:
107 ; int filter(EXCEPTION_POINTERS *eh_ptrs, void *rbp);
108
109 ; The definition of EXCEPTION_POINTERS is:
110 ;   typedef struct _EXCEPTION_POINTERS {
111 ;     EXCEPTION_RECORD *ExceptionRecord;
112 ;     CONTEXT          *ContextRecord;
113 ;   } EXCEPTION_POINTERS;
114
115 ; The definition of EXCEPTION_RECORD is:
116 ;   typedef struct _EXCEPTION_RECORD {
117 ;     DWORD ExceptionCode;
118 ;     ...
119 ;   } EXCEPTION_RECORD;
120
121 ; FIXME: Use llvm.eh.exceptioninfo for this.
122 declare i32 @safe_div_filt0()
123 declare i32 @safe_div_filt1()
124 ; define i32 @safe_div_filt0() {
125 ;   %eh_ptrs_c = bitcast i8* %eh_ptrs to i32**
126 ;   %eh_rec = load i32*, i32** %eh_ptrs_c
127 ;   %eh_code = load i32, i32* %eh_rec
128 ;   ; EXCEPTION_ACCESS_VIOLATION = 0xC0000005
129 ;   %cmp = icmp eq i32 %eh_code, 3221225477
130 ;   %filt.res = zext i1 %cmp to i32
131 ;   ret i32 %filt.res
132 ; }
133 ; define i32 @safe_div_filt1() {
134 ;   %eh_ptrs_c = bitcast i8* %eh_ptrs to i32**
135 ;   %eh_rec = load i32*, i32** %eh_ptrs_c
136 ;   %eh_code = load i32, i32* %eh_rec
137 ;   ; EXCEPTION_INT_DIVIDE_BY_ZERO = 0xC0000094
138 ;   %cmp = icmp eq i32 %eh_code, 3221225620
139 ;   %filt.res = zext i1 %cmp to i32
140 ;   ret i32 %filt.res
141 ; }
142
143 @str_result = internal constant [21 x i8] c"safe_div result: %d\0A\00"
144
145 define i32 @main() {
146   %d.addr = alloca i32, align 4
147   %n.addr = alloca i32, align 4
148
149   store i32 10, i32* %n.addr, align 4
150   store i32 2, i32* %d.addr, align 4
151   %r1 = call i32 @safe_div(i32* %n.addr, i32* %d.addr)
152   call void (i8*, ...) @printf(i8* getelementptr ([21 x i8], [21 x i8]* @str_result, i32 0, i32 0), i32 %r1)
153
154   store i32 10, i32* %n.addr, align 4
155   store i32 0, i32* %d.addr, align 4
156   %r2 = call i32 @safe_div(i32* %n.addr, i32* %d.addr)
157   call void (i8*, ...) @printf(i8* getelementptr ([21 x i8], [21 x i8]* @str_result, i32 0, i32 0), i32 %r2)
158
159   %r3 = call i32 @safe_div(i32* %n.addr, i32* null)
160   call void (i8*, ...) @printf(i8* getelementptr ([21 x i8], [21 x i8]* @str_result, i32 0, i32 0), i32 %r3)
161   ret i32 0
162 }
163
164 declare i32 @_except_handler3(...)
165 declare i32 @llvm.eh.typeid.for(i8*) readnone nounwind
166 declare void @puts(i8*)
167 declare void @printf(i8*, ...)
168 declare void @abort()