170 lines · plain
1; RUN: llc -mtriple x86_64-pc-windows-msvc < %s | FileCheck %s2 3; This test case is also intended to be run manually as a complete functional4; 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 26define i32 @safe_div(ptr %n, ptr %d) personality ptr @__C_specific_handler {27entry:28 %r = alloca i32, align 429 invoke void @try_body(ptr %r, ptr %n, ptr %d)30 to label %__try.cont unwind label %lpad031 32lpad0:33 %cs0 = catchswitch within none [label %handler0] unwind label %lpad134 35handler0:36 %p0 = catchpad within %cs0 [ptr @safe_div_filt0]37 call void @puts(ptr @str1) [ "funclet"(token %p0) ]38 store i32 -1, ptr %r, align 439 catchret from %p0 to label %__try.cont40 41lpad1:42 %cs1 = catchswitch within none [label %handler1] unwind to caller43 44handler1:45 %p1 = catchpad within %cs1 [ptr @safe_div_filt1]46 call void @puts(ptr @str2) [ "funclet"(token %p1) ]47 store i32 -2, ptr %r, align 448 catchret from %p1 to label %__try.cont49 50__try.cont:51 %safe_ret = load i32, ptr %r, align 452 ret i32 %safe_ret53}54 55; Normal path code56 57; CHECK: {{^}}safe_div:58; CHECK: .seh_proc safe_div59; CHECK: .seh_handler __C_specific_handler, @unwind, @except60; CHECK: .Ltmp0:61; CHECK: leaq [[rloc:.*\(%rbp\)]], %rcx62; CHECK: callq try_body63; CHECK: nop64; CHECK-NEXT: .Ltmp165; CHECK: [[cont_bb:\.LBB0_[0-9]+]]:66; CHECK: movl [[rloc]], %eax67; CHECK: retq68 69; Landing pad code70 71; CHECK: [[handler1:\.LBB0_[0-9]+]]: # %handler172; CHECK: callq puts73; CHECK: movl $-2, [[rloc]]74; CHECK: jmp [[cont_bb]]75 76; CHECK: [[handler0:\.LBB0_[0-9]+]]: # %handler077; CHECK: callq puts78; CHECK: movl $-1, [[rloc]]79; CHECK: jmp [[cont_bb]]80 81; CHECK: .seh_handlerdata82; CHECK-NEXT: .Lsafe_div$parent_frame_offset83; CHECK-NEXT: .long (.Llsda_end0-.Llsda_begin0)/1684; CHECK-NEXT: .Llsda_begin0:85; CHECK-NEXT: .long .Ltmp0@IMGREL86; CHECK-NEXT: .long .Ltmp1@IMGREL87; CHECK-NEXT: .long safe_div_filt0@IMGREL88; CHECK-NEXT: .long [[handler0]]@IMGREL89; CHECK-NEXT: .long .Ltmp0@IMGREL90; CHECK-NEXT: .long .Ltmp1@IMGREL91; CHECK-NEXT: .long safe_div_filt1@IMGREL92; CHECK-NEXT: .long [[handler1]]@IMGREL93; CHECK-NEXT: .Llsda_end0:94; CHECK: .text95; CHECK: .seh_endproc96 97define void @try_body(ptr %r, ptr %n, ptr %d) {98entry:99 %0 = load i32, ptr %n, align 4100 %1 = load i32, ptr %d, align 4101 %div = sdiv i32 %0, %1102 store i32 %div, ptr %r, align 4103 ret void104}105 106; The prototype of these filter functions is:107; int filter(EXCEPTION_POINTERS *eh_ptrs, ptr 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; The exception code can be retreived with two loads, one for the record122; pointer and one for the code. The values of local variables can be123; accessed via rbp, but that would require additional not yet implemented LLVM124; support.125 126define i32 @safe_div_filt0(ptr %eh_ptrs, ptr %rbp) {127 %eh_rec = load ptr, ptr %eh_ptrs128 %eh_code = load i32, ptr %eh_rec129 ; EXCEPTION_ACCESS_VIOLATION = 0xC0000005130 %cmp = icmp eq i32 %eh_code, 3221225477131 %filt.res = zext i1 %cmp to i32132 ret i32 %filt.res133}134 135define i32 @safe_div_filt1(ptr %eh_ptrs, ptr %rbp) {136 %eh_rec = load ptr, ptr %eh_ptrs137 %eh_code = load i32, ptr %eh_rec138 ; EXCEPTION_INT_DIVIDE_BY_ZERO = 0xC0000094139 %cmp = icmp eq i32 %eh_code, 3221225620140 %filt.res = zext i1 %cmp to i32141 ret i32 %filt.res142}143 144@str_result = internal constant [21 x i8] c"safe_div result: %d\0A\00"145 146define i32 @main() {147 %d.addr = alloca i32, align 4148 %n.addr = alloca i32, align 4149 150 store i32 10, ptr %n.addr, align 4151 store i32 2, ptr %d.addr, align 4152 %r1 = call i32 @safe_div(ptr %n.addr, ptr %d.addr)153 call void (ptr, ...) @printf(ptr @str_result, i32 %r1)154 155 store i32 10, ptr %n.addr, align 4156 store i32 0, ptr %d.addr, align 4157 %r2 = call i32 @safe_div(ptr %n.addr, ptr %d.addr)158 call void (ptr, ...) @printf(ptr @str_result, i32 %r2)159 160 %r3 = call i32 @safe_div(ptr %n.addr, ptr null)161 call void (ptr, ...) @printf(ptr @str_result, i32 %r3)162 ret i32 0163}164 165declare i32 @__C_specific_handler(...)166declare i32 @llvm.eh.typeid.for(ptr) readnone nounwind167declare void @puts(ptr)168declare void @printf(ptr, ...)169declare void @abort()170