ucrtbase: Add __processing_throw implementation.
[wine.git] / dlls / msvcrt / except_i386.c
blob512130197ab6ec91056e54f5f121238e6f828810
1 /*
2 * msvcrt C++ exception handling
4 * Copyright 2000 Jon Griffiths
5 * Copyright 2002 Alexandre Julliard
6 * Copyright 2005 Juan Lang
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 * NOTES
23 * A good reference is the article "How a C++ compiler implements
24 * exception handling" by Vishal Kochhar, available on
25 * www.thecodeproject.com.
28 #include "config.h"
29 #include "wine/port.h"
31 #ifdef __i386__
33 #include <stdarg.h>
35 #include "windef.h"
36 #include "winbase.h"
37 #include "winternl.h"
38 #include "msvcrt.h"
39 #include "wine/exception.h"
40 #include "excpt.h"
41 #include "wine/debug.h"
43 #include "cppexcept.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(seh);
48 /* the exception frame used by CxxFrameHandler */
49 typedef struct __cxx_exception_frame
51 EXCEPTION_REGISTRATION_RECORD frame; /* the standard exception frame */
52 int trylevel;
53 DWORD ebp;
54 } cxx_exception_frame;
56 /* info about a single catch {} block */
57 typedef struct __catchblock_info
59 UINT flags; /* flags (see below) */
60 const type_info *type_info; /* C++ type caught by this block */
61 int offset; /* stack offset to copy exception object to */
62 void (*handler)(void);/* catch block handler code */
63 } catchblock_info;
64 #define TYPE_FLAG_CONST 1
65 #define TYPE_FLAG_VOLATILE 2
66 #define TYPE_FLAG_REFERENCE 8
68 /* info about a single try {} block */
69 typedef struct __tryblock_info
71 int start_level; /* start trylevel of that block */
72 int end_level; /* end trylevel of that block */
73 int catch_level; /* initial trylevel of the catch block */
74 int catchblock_count; /* count of catch blocks in array */
75 const catchblock_info *catchblock; /* array of catch blocks */
76 } tryblock_info;
78 /* info about the unwind handler for a given trylevel */
79 typedef struct __unwind_info
81 int prev; /* prev trylevel unwind handler, to run after this one */
82 void (*handler)(void);/* unwind handler */
83 } unwind_info;
85 /* descriptor of all try blocks of a given function */
86 typedef struct __cxx_function_descr
88 UINT magic; /* must be CXX_FRAME_MAGIC */
89 UINT unwind_count; /* number of unwind handlers */
90 const unwind_info *unwind_table; /* array of unwind handlers */
91 UINT tryblock_count; /* number of try blocks */
92 const tryblock_info *tryblock; /* array of try blocks */
93 UINT ipmap_count;
94 const void *ipmap;
95 const void *expect_list; /* expected exceptions list when magic >= VC7 */
96 UINT flags; /* flags when magic >= VC8 */
97 } cxx_function_descr;
99 typedef struct
101 cxx_exception_frame *frame;
102 const cxx_function_descr *descr;
103 EXCEPTION_REGISTRATION_RECORD *nested_frame;
104 } se_translator_ctx;
106 typedef struct _SCOPETABLE
108 int previousTryLevel;
109 int (*lpfnFilter)(PEXCEPTION_POINTERS);
110 int (*lpfnHandler)(void);
111 } SCOPETABLE, *PSCOPETABLE;
113 typedef struct _MSVCRT_EXCEPTION_FRAME
115 EXCEPTION_REGISTRATION_RECORD *prev;
116 void (*handler)(PEXCEPTION_RECORD, EXCEPTION_REGISTRATION_RECORD*,
117 PCONTEXT, PEXCEPTION_RECORD);
118 PSCOPETABLE scopetable;
119 int trylevel;
120 int _ebp;
121 PEXCEPTION_POINTERS xpointers;
122 } MSVCRT_EXCEPTION_FRAME;
124 typedef struct
126 int gs_cookie_offset;
127 ULONG gs_cookie_xor;
128 int eh_cookie_offset;
129 ULONG eh_cookie_xor;
130 SCOPETABLE entries[1];
131 } SCOPETABLE_V4;
133 #define TRYLEVEL_END (-1) /* End of trylevel list */
135 DWORD CDECL cxx_frame_handler( PEXCEPTION_RECORD rec, cxx_exception_frame* frame,
136 PCONTEXT context, EXCEPTION_REGISTRATION_RECORD** dispatch,
137 const cxx_function_descr *descr,
138 EXCEPTION_REGISTRATION_RECORD* nested_frame, int nested_trylevel ) DECLSPEC_HIDDEN;
140 /* call a function with a given ebp */
141 static inline void *call_ebp_func( void *func, void *ebp )
143 void *ret;
144 int dummy;
145 __asm__ __volatile__ ("pushl %%ebx\n\t"
146 "pushl %%ebp\n\t"
147 "movl %4,%%ebp\n\t"
148 "call *%%eax\n\t"
149 "popl %%ebp\n\t"
150 "popl %%ebx"
151 : "=a" (ret), "=S" (dummy), "=D" (dummy)
152 : "0" (func), "1" (ebp) : "ecx", "edx", "memory" );
153 return ret;
156 /* call a copy constructor */
157 static inline void call_copy_ctor( void *func, void *this, void *src, int has_vbase )
159 TRACE( "calling copy ctor %p object %p src %p\n", func, this, src );
160 if (has_vbase)
161 /* in that case copy ctor takes an extra bool indicating whether to copy the base class */
162 __asm__ __volatile__("pushl $1; pushl %2; call *%0"
163 : : "r" (func), "c" (this), "r" (src) : "eax", "edx", "memory" );
164 else
165 __asm__ __volatile__("pushl %2; call *%0"
166 : : "r" (func), "c" (this), "r" (src) : "eax", "edx", "memory" );
169 /* continue execution to the specified address after exception is caught */
170 static inline void DECLSPEC_NORETURN continue_after_catch( cxx_exception_frame* frame, void *addr )
172 __asm__ __volatile__("movl -4(%0),%%esp; leal 12(%0),%%ebp; jmp *%1"
173 : : "r" (frame), "a" (addr) );
174 for (;;) ; /* unreached */
177 static inline void call_finally_block( void *code_block, void *base_ptr )
179 __asm__ __volatile__ ("movl %1,%%ebp; call *%%eax"
180 : : "a" (code_block), "g" (base_ptr));
183 static inline int call_filter( int (*func)(PEXCEPTION_POINTERS), void *arg, void *ebp )
185 int ret;
186 __asm__ __volatile__ ("pushl %%ebp; pushl %3; movl %2,%%ebp; call *%%eax; popl %%ebp; popl %%ebp"
187 : "=a" (ret)
188 : "0" (func), "r" (ebp), "r" (arg)
189 : "ecx", "edx", "memory" );
190 return ret;
193 static inline int call_unwind_func( int (*func)(void), void *ebp )
195 int ret;
196 __asm__ __volatile__ ("pushl %%ebp\n\t"
197 "pushl %%ebx\n\t"
198 "pushl %%esi\n\t"
199 "pushl %%edi\n\t"
200 "movl %2,%%ebp\n\t"
201 "call *%0\n\t"
202 "popl %%edi\n\t"
203 "popl %%esi\n\t"
204 "popl %%ebx\n\t"
205 "popl %%ebp"
206 : "=a" (ret)
207 : "0" (func), "r" (ebp)
208 : "ecx", "edx", "memory" );
209 return ret;
212 static inline void dump_type( const cxx_type_info *type )
214 TRACE( "flags %x type %p %s offsets %d,%d,%d size %d copy ctor %p\n",
215 type->flags, type->type_info, dbgstr_type_info(type->type_info),
216 type->offsets.this_offset, type->offsets.vbase_descr, type->offsets.vbase_offset,
217 type->size, type->copy_ctor );
220 static void dump_exception_type( const cxx_exception_type *type )
222 UINT i;
224 TRACE( "flags %x destr %p handler %p type info %p\n",
225 type->flags, type->destructor, type->custom_handler, type->type_info_table );
226 for (i = 0; i < type->type_info_table->count; i++)
228 TRACE( " %d: ", i );
229 dump_type( type->type_info_table->info[i] );
233 static void dump_function_descr( const cxx_function_descr *descr )
235 UINT i;
236 int j;
238 TRACE( "magic %x\n", descr->magic );
239 TRACE( "unwind table: %p %d\n", descr->unwind_table, descr->unwind_count );
240 for (i = 0; i < descr->unwind_count; i++)
242 TRACE( " %d: prev %d func %p\n", i,
243 descr->unwind_table[i].prev, descr->unwind_table[i].handler );
245 TRACE( "try table: %p %d\n", descr->tryblock, descr->tryblock_count );
246 for (i = 0; i < descr->tryblock_count; i++)
248 TRACE( " %d: start %d end %d catchlevel %d catch %p %d\n", i,
249 descr->tryblock[i].start_level, descr->tryblock[i].end_level,
250 descr->tryblock[i].catch_level, descr->tryblock[i].catchblock,
251 descr->tryblock[i].catchblock_count );
252 for (j = 0; j < descr->tryblock[i].catchblock_count; j++)
254 const catchblock_info *ptr = &descr->tryblock[i].catchblock[j];
255 TRACE( " %d: flags %x offset %d handler %p type %p %s\n",
256 j, ptr->flags, ptr->offset, ptr->handler,
257 ptr->type_info, dbgstr_type_info( ptr->type_info ) );
260 if (descr->magic <= CXX_FRAME_MAGIC_VC6) return;
261 TRACE( "expect list: %p\n", descr->expect_list );
262 if (descr->magic <= CXX_FRAME_MAGIC_VC7) return;
263 TRACE( "flags: %08x\n", descr->flags );
266 /* check if the exception type is caught by a given catch block, and return the type that matched */
267 static const cxx_type_info *find_caught_type( cxx_exception_type *exc_type,
268 const type_info *catch_ti, UINT catch_flags )
270 UINT i;
272 for (i = 0; i < exc_type->type_info_table->count; i++)
274 const cxx_type_info *type = exc_type->type_info_table->info[i];
276 if (!catch_ti) return type; /* catch(...) matches any type */
277 if (catch_ti != type->type_info)
279 if (strcmp( catch_ti->mangled, type->type_info->mangled )) continue;
281 /* type is the same, now check the flags */
282 if ((exc_type->flags & TYPE_FLAG_CONST) &&
283 !(catch_flags & TYPE_FLAG_CONST)) continue;
284 if ((exc_type->flags & TYPE_FLAG_VOLATILE) &&
285 !(catch_flags & TYPE_FLAG_VOLATILE)) continue;
286 return type; /* it matched */
288 return NULL;
292 /* copy the exception object where the catch block wants it */
293 static void copy_exception( void *object, cxx_exception_frame *frame,
294 const catchblock_info *catchblock, const cxx_type_info *type )
296 void **dest_ptr;
298 if (!catchblock->type_info || !catchblock->type_info->mangled[0]) return;
299 if (!catchblock->offset) return;
300 dest_ptr = (void **)((char *)&frame->ebp + catchblock->offset);
302 if (catchblock->flags & TYPE_FLAG_REFERENCE)
304 *dest_ptr = get_this_pointer( &type->offsets, object );
306 else if (type->flags & CLASS_IS_SIMPLE_TYPE)
308 memmove( dest_ptr, object, type->size );
309 /* if it is a pointer, adjust it */
310 if (type->size == sizeof(void *)) *dest_ptr = get_this_pointer( &type->offsets, *dest_ptr );
312 else /* copy the object */
314 if (type->copy_ctor)
315 call_copy_ctor( type->copy_ctor, dest_ptr, get_this_pointer(&type->offsets,object),
316 (type->flags & CLASS_HAS_VIRTUAL_BASE_CLASS) );
317 else
318 memmove( dest_ptr, get_this_pointer(&type->offsets,object), type->size );
322 /* unwind the local function up to a given trylevel */
323 static void cxx_local_unwind( cxx_exception_frame* frame, const cxx_function_descr *descr, int last_level)
325 void (*handler)(void);
326 int trylevel = frame->trylevel;
328 while (trylevel != last_level)
330 if (trylevel < 0 || trylevel >= descr->unwind_count)
332 ERR( "invalid trylevel %d\n", trylevel );
333 MSVCRT_terminate();
335 handler = descr->unwind_table[trylevel].handler;
336 if (handler)
338 TRACE( "calling unwind handler %p trylevel %d last %d ebp %p\n",
339 handler, trylevel, last_level, &frame->ebp );
340 call_ebp_func( handler, &frame->ebp );
342 trylevel = descr->unwind_table[trylevel].prev;
344 frame->trylevel = last_level;
347 /* exception frame for nested exceptions in catch block */
348 struct catch_func_nested_frame
350 EXCEPTION_REGISTRATION_RECORD frame; /* standard exception frame */
351 cxx_exception_frame *cxx_frame; /* frame of parent exception */
352 const cxx_function_descr *descr; /* descriptor of parent exception */
353 int trylevel; /* current try level */
354 cxx_frame_info frame_info;
357 /* handler for exceptions happening while calling a catch function */
358 static DWORD catch_function_nested_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
359 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
361 struct catch_func_nested_frame *nested_frame = (struct catch_func_nested_frame *)frame;
363 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
365 __CxxUnregisterExceptionObject(&nested_frame->frame_info, FALSE);
366 return ExceptionContinueSearch;
369 TRACE( "got nested exception in catch function\n" );
371 if(rec->ExceptionCode == CXX_EXCEPTION)
373 PEXCEPTION_RECORD prev_rec = msvcrt_get_thread_data()->exc_record;
375 if((rec->ExceptionInformation[1] == 0 && rec->ExceptionInformation[2] == 0) ||
376 (prev_rec->ExceptionCode == CXX_EXCEPTION &&
377 rec->ExceptionInformation[1] == prev_rec->ExceptionInformation[1] &&
378 rec->ExceptionInformation[2] == prev_rec->ExceptionInformation[2]))
380 /* exception was rethrown */
381 *rec = *prev_rec;
382 rec->ExceptionFlags &= ~EH_UNWINDING;
383 if(TRACE_ON(seh)) {
384 TRACE("detect rethrow: exception code: %x\n", rec->ExceptionCode);
385 if(rec->ExceptionCode == CXX_EXCEPTION)
386 TRACE("re-propage: obj: %lx, type: %lx\n",
387 rec->ExceptionInformation[1], rec->ExceptionInformation[2]);
390 else
392 TRACE("detect threw new exception in catch block\n");
396 return cxx_frame_handler( rec, nested_frame->cxx_frame, context,
397 NULL, nested_frame->descr, &nested_frame->frame,
398 nested_frame->trylevel );
401 /* find and call the appropriate catch block for an exception */
402 /* returns the address to continue execution to after the catch block was called */
403 static inline void call_catch_block( PEXCEPTION_RECORD rec, CONTEXT *context,
404 cxx_exception_frame *frame,
405 const cxx_function_descr *descr, int nested_trylevel,
406 EXCEPTION_REGISTRATION_RECORD *catch_frame,
407 cxx_exception_type *info )
409 UINT i;
410 int j;
411 void *addr, *object = (void *)rec->ExceptionInformation[1];
412 struct catch_func_nested_frame nested_frame;
413 int trylevel = frame->trylevel;
414 DWORD save_esp = ((DWORD*)frame)[-1];
415 thread_data_t *data = msvcrt_get_thread_data();
417 data->processing_throw++;
418 for (i = 0; i < descr->tryblock_count; i++)
420 const tryblock_info *tryblock = &descr->tryblock[i];
422 /* only handle try blocks inside current catch block */
423 if (catch_frame && nested_trylevel > tryblock->start_level) continue;
425 if (trylevel < tryblock->start_level) continue;
426 if (trylevel > tryblock->end_level) continue;
428 /* got a try block */
429 for (j = 0; j < tryblock->catchblock_count; j++)
431 const catchblock_info *catchblock = &tryblock->catchblock[j];
432 if(info)
434 const cxx_type_info *type = find_caught_type( info,
435 catchblock->type_info, catchblock->flags );
436 if (!type) continue;
438 TRACE( "matched type %p in tryblock %d catchblock %d\n", type, i, j );
440 /* copy the exception to its destination on the stack */
441 copy_exception( object, frame, catchblock, type );
443 else
445 /* no CXX_EXCEPTION only proceed with a catch(...) block*/
446 if(catchblock->type_info)
447 continue;
448 TRACE("found catch(...) block\n");
451 /* Add frame info here so exception is not freed inside RtlUnwind call */
452 _CreateFrameInfo(&nested_frame.frame_info.frame_info,
453 (void*)rec->ExceptionInformation[1]);
455 /* unwind the stack */
456 RtlUnwind( catch_frame ? catch_frame : &frame->frame, 0, rec, 0 );
457 cxx_local_unwind( frame, descr, tryblock->start_level );
458 frame->trylevel = tryblock->end_level + 1;
460 nested_frame.frame_info.rec = data->exc_record;
461 nested_frame.frame_info.context = data->ctx_record;
462 data->exc_record = rec;
463 data->ctx_record = context;
464 data->processing_throw--;
466 /* call the catch block */
467 TRACE( "calling catch block %p addr %p ebp %p\n",
468 catchblock, catchblock->handler, &frame->ebp );
470 /* setup an exception block for nested exceptions */
471 nested_frame.frame.Handler = catch_function_nested_handler;
472 nested_frame.cxx_frame = frame;
473 nested_frame.descr = descr;
474 nested_frame.trylevel = nested_trylevel + 1;
476 __wine_push_frame( &nested_frame.frame );
477 addr = call_ebp_func( catchblock->handler, &frame->ebp );
478 __wine_pop_frame( &nested_frame.frame );
480 ((DWORD*)frame)[-1] = save_esp;
481 __CxxUnregisterExceptionObject(&nested_frame.frame_info, FALSE);
482 TRACE( "done, continuing at %p\n", addr );
484 continue_after_catch( frame, addr );
487 data->processing_throw--;
490 /*********************************************************************
491 * __CxxExceptionFilter (MSVCRT.@)
493 int CDECL __CxxExceptionFilter( PEXCEPTION_POINTERS ptrs,
494 const type_info *ti, int flags, void **copy)
496 const cxx_type_info *type;
497 PEXCEPTION_RECORD rec;
499 TRACE( "%p %p %x %p\n", ptrs, ti, flags, copy );
501 if (!ptrs) return EXCEPTION_CONTINUE_SEARCH;
503 /* handle catch(...) */
504 if (!ti) return EXCEPTION_EXECUTE_HANDLER;
506 rec = ptrs->ExceptionRecord;
507 if (rec->ExceptionCode != CXX_EXCEPTION || rec->NumberParameters != 3 ||
508 rec->ExceptionInformation[0] < CXX_FRAME_MAGIC_VC6 ||
509 rec->ExceptionInformation[0] > CXX_FRAME_MAGIC_VC8)
510 return EXCEPTION_CONTINUE_SEARCH;
512 if (rec->ExceptionInformation[1] == 0 && rec->ExceptionInformation[2] == 0)
514 rec = msvcrt_get_thread_data()->exc_record;
515 if (!rec) return EXCEPTION_CONTINUE_SEARCH;
518 type = find_caught_type( (cxx_exception_type*)rec->ExceptionInformation[2], ti, flags );
519 if (!type) return EXCEPTION_CONTINUE_SEARCH;
521 if (copy)
523 void *object = (void *)rec->ExceptionInformation[1];
525 if (flags & TYPE_FLAG_REFERENCE)
527 *copy = get_this_pointer( &type->offsets, object );
529 else if (type->flags & CLASS_IS_SIMPLE_TYPE)
531 memmove( copy, object, type->size );
532 /* if it is a pointer, adjust it */
533 if (type->size == sizeof(void*)) *copy = get_this_pointer( &type->offsets, *copy );
535 else /* copy the object */
537 if (type->copy_ctor)
538 call_copy_ctor( type->copy_ctor, copy, get_this_pointer(&type->offsets,object),
539 (type->flags & CLASS_HAS_VIRTUAL_BASE_CLASS) );
540 else
541 memmove( copy, get_this_pointer(&type->offsets,object), type->size );
544 return EXCEPTION_EXECUTE_HANDLER;
547 static LONG CALLBACK se_translation_filter( EXCEPTION_POINTERS *ep, void *c )
549 se_translator_ctx *ctx = (se_translator_ctx *)c;
550 EXCEPTION_RECORD *rec = ep->ExceptionRecord;
551 cxx_exception_type *exc_type;
553 if (rec->ExceptionCode != CXX_EXCEPTION)
555 TRACE( "non-c++ exception thrown in SEH handler: %x\n", rec->ExceptionCode );
556 MSVCRT_terminate();
559 exc_type = (cxx_exception_type *)rec->ExceptionInformation[2];
560 call_catch_block( rec, ep->ContextRecord, ctx->frame, ctx->descr,
561 ctx->frame->trylevel, ctx->nested_frame, exc_type );
563 __DestructExceptionObject( rec );
564 return ExceptionContinueSearch;
567 /*********************************************************************
568 * cxx_frame_handler
570 * Implementation of __CxxFrameHandler.
572 DWORD CDECL cxx_frame_handler( PEXCEPTION_RECORD rec, cxx_exception_frame* frame,
573 PCONTEXT context, EXCEPTION_REGISTRATION_RECORD** dispatch,
574 const cxx_function_descr *descr,
575 EXCEPTION_REGISTRATION_RECORD* nested_frame,
576 int nested_trylevel )
578 cxx_exception_type *exc_type;
580 if (descr->magic < CXX_FRAME_MAGIC_VC6 || descr->magic > CXX_FRAME_MAGIC_VC8)
582 ERR( "invalid frame magic %x\n", descr->magic );
583 return ExceptionContinueSearch;
585 if (descr->magic >= CXX_FRAME_MAGIC_VC8 &&
586 (descr->flags & FUNC_DESCR_SYNCHRONOUS) &&
587 (rec->ExceptionCode != CXX_EXCEPTION))
588 return ExceptionContinueSearch; /* handle only c++ exceptions */
590 if (rec->ExceptionFlags & (EH_UNWINDING|EH_EXIT_UNWIND))
592 if (descr->unwind_count && !nested_trylevel) cxx_local_unwind( frame, descr, -1 );
593 return ExceptionContinueSearch;
595 if (!descr->tryblock_count) return ExceptionContinueSearch;
597 if(rec->ExceptionCode == CXX_EXCEPTION &&
598 rec->ExceptionInformation[1] == 0 && rec->ExceptionInformation[2] == 0)
600 *rec = *msvcrt_get_thread_data()->exc_record;
601 rec->ExceptionFlags &= ~EH_UNWINDING;
602 if(TRACE_ON(seh)) {
603 TRACE("detect rethrow: exception code: %x\n", rec->ExceptionCode);
604 if(rec->ExceptionCode == CXX_EXCEPTION)
605 TRACE("re-propage: obj: %lx, type: %lx\n",
606 rec->ExceptionInformation[1], rec->ExceptionInformation[2]);
610 if(rec->ExceptionCode == CXX_EXCEPTION)
612 exc_type = (cxx_exception_type *)rec->ExceptionInformation[2];
614 if (rec->ExceptionInformation[0] > CXX_FRAME_MAGIC_VC8 &&
615 exc_type->custom_handler)
617 return exc_type->custom_handler( rec, frame, context, dispatch,
618 descr, nested_trylevel, nested_frame, 0 );
621 if (TRACE_ON(seh))
623 TRACE("handling C++ exception rec %p frame %p trylevel %d descr %p nested_frame %p\n",
624 rec, frame, frame->trylevel, descr, nested_frame );
625 dump_exception_type( exc_type );
626 dump_function_descr( descr );
629 else
631 thread_data_t *data = msvcrt_get_thread_data();
633 exc_type = NULL;
634 TRACE("handling C exception code %x rec %p frame %p trylevel %d descr %p nested_frame %p\n",
635 rec->ExceptionCode, rec, frame, frame->trylevel, descr, nested_frame );
637 if (data->se_translator) {
638 EXCEPTION_POINTERS except_ptrs;
639 se_translator_ctx ctx;
641 ctx.frame = frame;
642 ctx.descr = descr;
643 ctx.nested_frame = nested_frame;
644 __TRY
646 except_ptrs.ExceptionRecord = rec;
647 except_ptrs.ContextRecord = context;
648 data->se_translator( rec->ExceptionCode, &except_ptrs );
650 __EXCEPT_CTX(se_translation_filter, &ctx)
653 __ENDTRY
657 call_catch_block( rec, context, frame, descr,
658 frame->trylevel, nested_frame, exc_type );
659 return ExceptionContinueSearch;
663 /*********************************************************************
664 * __CxxFrameHandler (MSVCRT.@)
666 extern DWORD CDECL __CxxFrameHandler( PEXCEPTION_RECORD rec, EXCEPTION_REGISTRATION_RECORD* frame,
667 PCONTEXT context, EXCEPTION_REGISTRATION_RECORD** dispatch );
668 __ASM_GLOBAL_FUNC( __CxxFrameHandler,
669 "pushl $0\n\t" /* nested_trylevel */
670 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
671 "pushl $0\n\t" /* nested_frame */
672 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
673 "pushl %eax\n\t" /* descr */
674 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
675 "pushl 28(%esp)\n\t" /* dispatch */
676 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
677 "pushl 28(%esp)\n\t" /* context */
678 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
679 "pushl 28(%esp)\n\t" /* frame */
680 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
681 "pushl 28(%esp)\n\t" /* rec */
682 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
683 "call " __ASM_NAME("cxx_frame_handler") "\n\t"
684 "add $28,%esp\n\t"
685 __ASM_CFI(".cfi_adjust_cfa_offset -28\n\t")
686 "ret" )
689 /*********************************************************************
690 * __CxxLongjmpUnwind (MSVCRT.@)
692 * Callback meant to be used as UnwindFunc for setjmp/longjmp.
694 void __stdcall __CxxLongjmpUnwind( const struct MSVCRT___JUMP_BUFFER *buf )
696 cxx_exception_frame *frame = (cxx_exception_frame *)buf->Registration;
697 const cxx_function_descr *descr = (const cxx_function_descr *)buf->UnwindData[0];
699 TRACE( "unwinding frame %p descr %p trylevel %ld\n", frame, descr, buf->TryLevel );
700 cxx_local_unwind( frame, descr, buf->TryLevel );
703 /*********************************************************************
704 * __CppXcptFilter (MSVCRT.@)
706 int CDECL __CppXcptFilter(NTSTATUS ex, PEXCEPTION_POINTERS ptr)
708 /* only filter c++ exceptions */
709 if (ex != CXX_EXCEPTION) return EXCEPTION_CONTINUE_SEARCH;
710 return _XcptFilter( ex, ptr );
713 /*********************************************************************
714 * __CxxDetectRethrow (MSVCRT.@)
716 BOOL CDECL __CxxDetectRethrow(PEXCEPTION_POINTERS ptrs)
718 PEXCEPTION_RECORD rec;
720 if (!ptrs)
721 return FALSE;
723 rec = ptrs->ExceptionRecord;
725 if (rec->ExceptionCode == CXX_EXCEPTION &&
726 rec->NumberParameters == 3 &&
727 rec->ExceptionInformation[0] == CXX_FRAME_MAGIC_VC6 &&
728 rec->ExceptionInformation[2])
730 ptrs->ExceptionRecord = msvcrt_get_thread_data()->exc_record;
731 return TRUE;
733 return (msvcrt_get_thread_data()->exc_record == rec);
736 /*********************************************************************
737 * __CxxQueryExceptionSize (MSVCRT.@)
739 unsigned int CDECL __CxxQueryExceptionSize(void)
741 return sizeof(cxx_exception_type);
745 /*********************************************************************
746 * _EH_prolog (MSVCRT.@)
749 /* Provided for VC++ binary compatibility only */
750 __ASM_GLOBAL_FUNC(_EH_prolog,
751 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") /* skip ret addr */
752 "pushl $-1\n\t"
753 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
754 "pushl %eax\n\t"
755 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
756 "pushl %fs:0\n\t"
757 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
758 "movl %esp, %fs:0\n\t"
759 "movl 12(%esp), %eax\n\t"
760 "movl %ebp, 12(%esp)\n\t"
761 "leal 12(%esp), %ebp\n\t"
762 "pushl %eax\n\t"
763 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
764 "ret")
766 static const SCOPETABLE_V4 *get_scopetable_v4( MSVCRT_EXCEPTION_FRAME *frame, ULONG_PTR cookie )
768 return (const SCOPETABLE_V4 *)((ULONG_PTR)frame->scopetable ^ cookie);
771 static DWORD MSVCRT_nested_handler(PEXCEPTION_RECORD rec,
772 EXCEPTION_REGISTRATION_RECORD* frame,
773 PCONTEXT context,
774 EXCEPTION_REGISTRATION_RECORD** dispatch)
776 if (!(rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
777 return ExceptionContinueSearch;
778 *dispatch = frame;
779 return ExceptionCollidedUnwind;
782 static void msvcrt_local_unwind2(MSVCRT_EXCEPTION_FRAME* frame, int trylevel, void *ebp)
784 EXCEPTION_REGISTRATION_RECORD reg;
786 TRACE("(%p,%d,%d)\n",frame, frame->trylevel, trylevel);
788 /* Register a handler in case of a nested exception */
789 reg.Handler = MSVCRT_nested_handler;
790 reg.Prev = NtCurrentTeb()->Tib.ExceptionList;
791 __wine_push_frame(&reg);
793 while (frame->trylevel != TRYLEVEL_END && frame->trylevel != trylevel)
795 int level = frame->trylevel;
796 frame->trylevel = frame->scopetable[level].previousTryLevel;
797 if (!frame->scopetable[level].lpfnFilter)
799 TRACE( "__try block cleanup level %d handler %p ebp %p\n",
800 level, frame->scopetable[level].lpfnHandler, ebp );
801 call_unwind_func( frame->scopetable[level].lpfnHandler, ebp );
804 __wine_pop_frame(&reg);
805 TRACE("unwound OK\n");
808 static void msvcrt_local_unwind4( ULONG *cookie, MSVCRT_EXCEPTION_FRAME* frame, int trylevel, void *ebp )
810 EXCEPTION_REGISTRATION_RECORD reg;
811 const SCOPETABLE_V4 *scopetable = get_scopetable_v4( frame, *cookie );
813 TRACE("(%p,%d,%d)\n",frame, frame->trylevel, trylevel);
815 /* Register a handler in case of a nested exception */
816 reg.Handler = MSVCRT_nested_handler;
817 reg.Prev = NtCurrentTeb()->Tib.ExceptionList;
818 __wine_push_frame(&reg);
820 while (frame->trylevel != -2 && frame->trylevel != trylevel)
822 int level = frame->trylevel;
823 frame->trylevel = scopetable->entries[level].previousTryLevel;
824 if (!scopetable->entries[level].lpfnFilter)
826 TRACE( "__try block cleanup level %d handler %p ebp %p\n",
827 level, scopetable->entries[level].lpfnHandler, ebp );
828 call_unwind_func( scopetable->entries[level].lpfnHandler, ebp );
831 __wine_pop_frame(&reg);
832 TRACE("unwound OK\n");
835 /*******************************************************************
836 * _local_unwind2 (MSVCRT.@)
838 void CDECL _local_unwind2(MSVCRT_EXCEPTION_FRAME* frame, int trylevel)
840 msvcrt_local_unwind2( frame, trylevel, &frame->_ebp );
843 /*******************************************************************
844 * _local_unwind4 (MSVCRT.@)
846 void CDECL _local_unwind4( ULONG *cookie, MSVCRT_EXCEPTION_FRAME* frame, int trylevel )
848 msvcrt_local_unwind4( cookie, frame, trylevel, &frame->_ebp );
851 /*******************************************************************
852 * _global_unwind2 (MSVCRT.@)
854 void CDECL _global_unwind2(EXCEPTION_REGISTRATION_RECORD* frame)
856 TRACE("(%p)\n",frame);
857 RtlUnwind( frame, 0, 0, 0 );
860 /*********************************************************************
861 * _except_handler2 (MSVCRT.@)
863 int CDECL _except_handler2(PEXCEPTION_RECORD rec,
864 EXCEPTION_REGISTRATION_RECORD* frame,
865 PCONTEXT context,
866 EXCEPTION_REGISTRATION_RECORD** dispatcher)
868 FIXME("exception %x flags=%x at %p handler=%p %p %p stub\n",
869 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
870 frame->Handler, context, dispatcher);
871 return ExceptionContinueSearch;
874 /*********************************************************************
875 * _except_handler3 (MSVCRT.@)
877 int CDECL _except_handler3(PEXCEPTION_RECORD rec,
878 MSVCRT_EXCEPTION_FRAME* frame,
879 PCONTEXT context, void* dispatcher)
881 int retval, trylevel;
882 EXCEPTION_POINTERS exceptPtrs;
883 PSCOPETABLE pScopeTable;
885 TRACE("exception %x flags=%x at %p handler=%p %p %p semi-stub\n",
886 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
887 frame->handler, context, dispatcher);
889 __asm__ __volatile__ ("cld");
891 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
893 /* Unwinding the current frame */
894 msvcrt_local_unwind2(frame, TRYLEVEL_END, &frame->_ebp);
895 TRACE("unwound current frame, returning ExceptionContinueSearch\n");
896 return ExceptionContinueSearch;
898 else
900 /* Hunting for handler */
901 exceptPtrs.ExceptionRecord = rec;
902 exceptPtrs.ContextRecord = context;
903 *((DWORD *)frame-1) = (DWORD)&exceptPtrs;
904 trylevel = frame->trylevel;
905 pScopeTable = frame->scopetable;
907 while (trylevel != TRYLEVEL_END)
909 TRACE( "level %d prev %d filter %p\n", trylevel, pScopeTable[trylevel].previousTryLevel,
910 pScopeTable[trylevel].lpfnFilter );
911 if (pScopeTable[trylevel].lpfnFilter)
913 retval = call_filter( pScopeTable[trylevel].lpfnFilter, &exceptPtrs, &frame->_ebp );
915 TRACE("filter returned %s\n", retval == EXCEPTION_CONTINUE_EXECUTION ?
916 "CONTINUE_EXECUTION" : retval == EXCEPTION_EXECUTE_HANDLER ?
917 "EXECUTE_HANDLER" : "CONTINUE_SEARCH");
919 if (retval == EXCEPTION_CONTINUE_EXECUTION)
920 return ExceptionContinueExecution;
922 if (retval == EXCEPTION_EXECUTE_HANDLER)
924 /* Unwind all higher frames, this one will handle the exception */
925 _global_unwind2((EXCEPTION_REGISTRATION_RECORD*)frame);
926 msvcrt_local_unwind2(frame, trylevel, &frame->_ebp);
928 /* Set our trylevel to the enclosing block, and call the __finally
929 * code, which won't return
931 frame->trylevel = pScopeTable[trylevel].previousTryLevel;
932 TRACE("__finally block %p\n",pScopeTable[trylevel].lpfnHandler);
933 call_finally_block(pScopeTable[trylevel].lpfnHandler, &frame->_ebp);
934 ERR("Returned from __finally block - expect crash!\n");
937 trylevel = pScopeTable[trylevel].previousTryLevel;
940 TRACE("reached TRYLEVEL_END, returning ExceptionContinueSearch\n");
941 return ExceptionContinueSearch;
944 /*********************************************************************
945 * _except_handler4_common (MSVCRT.@)
947 int CDECL _except_handler4_common( ULONG *cookie, void (*check_cookie)(void),
948 EXCEPTION_RECORD *rec, MSVCRT_EXCEPTION_FRAME *frame,
949 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
951 int retval, trylevel;
952 EXCEPTION_POINTERS exceptPtrs;
953 const SCOPETABLE_V4 *scope_table = get_scopetable_v4( frame, *cookie );
955 TRACE( "exception %x flags=%x at %p handler=%p %p %p cookie=%x scope table=%p cookies=%d/%x,%d/%x\n",
956 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
957 frame->handler, context, dispatcher, *cookie, scope_table,
958 scope_table->gs_cookie_offset, scope_table->gs_cookie_xor,
959 scope_table->eh_cookie_offset, scope_table->eh_cookie_xor );
961 /* FIXME: no cookie validation yet */
963 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
965 /* Unwinding the current frame */
966 msvcrt_local_unwind4( cookie, frame, -2, &frame->_ebp );
967 TRACE("unwound current frame, returning ExceptionContinueSearch\n");
968 return ExceptionContinueSearch;
970 else
972 /* Hunting for handler */
973 exceptPtrs.ExceptionRecord = rec;
974 exceptPtrs.ContextRecord = context;
975 *((DWORD *)frame-1) = (DWORD)&exceptPtrs;
976 trylevel = frame->trylevel;
978 while (trylevel != -2)
980 TRACE( "level %d prev %d filter %p\n", trylevel,
981 scope_table->entries[trylevel].previousTryLevel,
982 scope_table->entries[trylevel].lpfnFilter );
983 if (scope_table->entries[trylevel].lpfnFilter)
985 retval = call_filter( scope_table->entries[trylevel].lpfnFilter, &exceptPtrs, &frame->_ebp );
987 TRACE("filter returned %s\n", retval == EXCEPTION_CONTINUE_EXECUTION ?
988 "CONTINUE_EXECUTION" : retval == EXCEPTION_EXECUTE_HANDLER ?
989 "EXECUTE_HANDLER" : "CONTINUE_SEARCH");
991 if (retval == EXCEPTION_CONTINUE_EXECUTION)
992 return ExceptionContinueExecution;
994 if (retval == EXCEPTION_EXECUTE_HANDLER)
996 __DestructExceptionObject(rec);
998 /* Unwind all higher frames, this one will handle the exception */
999 _global_unwind2((EXCEPTION_REGISTRATION_RECORD*)frame);
1000 msvcrt_local_unwind4( cookie, frame, trylevel, &frame->_ebp );
1002 /* Set our trylevel to the enclosing block, and call the __finally
1003 * code, which won't return
1005 frame->trylevel = scope_table->entries[trylevel].previousTryLevel;
1006 TRACE("__finally block %p\n",scope_table->entries[trylevel].lpfnHandler);
1007 call_finally_block(scope_table->entries[trylevel].lpfnHandler, &frame->_ebp);
1008 ERR("Returned from __finally block - expect crash!\n");
1011 trylevel = scope_table->entries[trylevel].previousTryLevel;
1014 TRACE("reached -2, returning ExceptionContinueSearch\n");
1015 return ExceptionContinueSearch;
1020 * setjmp/longjmp implementation
1023 #define MSVCRT_JMP_MAGIC 0x56433230 /* ID value for new jump structure */
1024 typedef void (__stdcall *MSVCRT_unwind_function)(const struct MSVCRT___JUMP_BUFFER *);
1026 /* define an entrypoint for setjmp/setjmp3 that stores the registers in the jmp buf */
1027 /* and then jumps to the C backend function */
1028 #define DEFINE_SETJMP_ENTRYPOINT(name) \
1029 __ASM_GLOBAL_FUNC( name, \
1030 "movl 4(%esp),%ecx\n\t" /* jmp_buf */ \
1031 "movl %ebp,0(%ecx)\n\t" /* jmp_buf.Ebp */ \
1032 "movl %ebx,4(%ecx)\n\t" /* jmp_buf.Ebx */ \
1033 "movl %edi,8(%ecx)\n\t" /* jmp_buf.Edi */ \
1034 "movl %esi,12(%ecx)\n\t" /* jmp_buf.Esi */ \
1035 "movl %esp,16(%ecx)\n\t" /* jmp_buf.Esp */ \
1036 "movl 0(%esp),%eax\n\t" \
1037 "movl %eax,20(%ecx)\n\t" /* jmp_buf.Eip */ \
1038 "jmp " __ASM_NAME("__regs_") # name )
1040 /* restore the registers from the jmp buf upon longjmp */
1041 extern void DECLSPEC_NORETURN longjmp_set_regs( struct MSVCRT___JUMP_BUFFER *jmp, int retval );
1042 __ASM_GLOBAL_FUNC( longjmp_set_regs,
1043 "movl 4(%esp),%ecx\n\t" /* jmp_buf */
1044 "movl 8(%esp),%eax\n\t" /* retval */
1045 "movl 0(%ecx),%ebp\n\t" /* jmp_buf.Ebp */
1046 "movl 4(%ecx),%ebx\n\t" /* jmp_buf.Ebx */
1047 "movl 8(%ecx),%edi\n\t" /* jmp_buf.Edi */
1048 "movl 12(%ecx),%esi\n\t" /* jmp_buf.Esi */
1049 "movl 16(%ecx),%esp\n\t" /* jmp_buf.Esp */
1050 "addl $4,%esp\n\t" /* get rid of return address */
1051 "jmp *20(%ecx)\n\t" /* jmp_buf.Eip */ )
1054 * The signatures of the setjmp/longjmp functions do not match that
1055 * declared in the setjmp header so they don't follow the regular naming
1056 * convention to avoid conflicts.
1059 /*******************************************************************
1060 * _setjmp (MSVCRT.@)
1062 DEFINE_SETJMP_ENTRYPOINT(MSVCRT__setjmp)
1063 int CDECL DECLSPEC_HIDDEN __regs_MSVCRT__setjmp(struct MSVCRT___JUMP_BUFFER *jmp)
1065 jmp->Registration = (unsigned long)NtCurrentTeb()->Tib.ExceptionList;
1066 if (jmp->Registration == ~0UL)
1067 jmp->TryLevel = TRYLEVEL_END;
1068 else
1069 jmp->TryLevel = ((MSVCRT_EXCEPTION_FRAME*)jmp->Registration)->trylevel;
1071 TRACE("buf=%p ebx=%08lx esi=%08lx edi=%08lx ebp=%08lx esp=%08lx eip=%08lx frame=%08lx\n",
1072 jmp, jmp->Ebx, jmp->Esi, jmp->Edi, jmp->Ebp, jmp->Esp, jmp->Eip, jmp->Registration );
1073 return 0;
1076 /*******************************************************************
1077 * _setjmp3 (MSVCRT.@)
1079 DEFINE_SETJMP_ENTRYPOINT( MSVCRT__setjmp3 )
1080 int WINAPIV DECLSPEC_HIDDEN __regs_MSVCRT__setjmp3(struct MSVCRT___JUMP_BUFFER *jmp, int nb_args, ...)
1082 jmp->Cookie = MSVCRT_JMP_MAGIC;
1083 jmp->UnwindFunc = 0;
1084 jmp->Registration = (unsigned long)NtCurrentTeb()->Tib.ExceptionList;
1085 if (jmp->Registration == ~0UL)
1087 jmp->TryLevel = TRYLEVEL_END;
1089 else
1091 int i;
1092 va_list args;
1094 va_start( args, nb_args );
1095 if (nb_args > 0) jmp->UnwindFunc = va_arg( args, unsigned long );
1096 if (nb_args > 1) jmp->TryLevel = va_arg( args, unsigned long );
1097 else jmp->TryLevel = ((MSVCRT_EXCEPTION_FRAME*)jmp->Registration)->trylevel;
1098 for (i = 0; i < 6 && i < nb_args - 2; i++)
1099 jmp->UnwindData[i] = va_arg( args, unsigned long );
1100 va_end( args );
1103 TRACE("buf=%p ebx=%08lx esi=%08lx edi=%08lx ebp=%08lx esp=%08lx eip=%08lx frame=%08lx\n",
1104 jmp, jmp->Ebx, jmp->Esi, jmp->Edi, jmp->Ebp, jmp->Esp, jmp->Eip, jmp->Registration );
1105 return 0;
1108 /*********************************************************************
1109 * longjmp (MSVCRT.@)
1111 void CDECL MSVCRT_longjmp(struct MSVCRT___JUMP_BUFFER *jmp, int retval)
1113 unsigned long cur_frame = 0;
1115 TRACE("buf=%p ebx=%08lx esi=%08lx edi=%08lx ebp=%08lx esp=%08lx eip=%08lx frame=%08lx retval=%08x\n",
1116 jmp, jmp->Ebx, jmp->Esi, jmp->Edi, jmp->Ebp, jmp->Esp, jmp->Eip, jmp->Registration, retval );
1118 cur_frame=(unsigned long)NtCurrentTeb()->Tib.ExceptionList;
1119 TRACE("cur_frame=%lx\n",cur_frame);
1121 if (cur_frame != jmp->Registration)
1122 _global_unwind2((EXCEPTION_REGISTRATION_RECORD*)jmp->Registration);
1124 if (jmp->Registration)
1126 if (IsBadReadPtr(&jmp->Cookie, sizeof(long)) || jmp->Cookie != MSVCRT_JMP_MAGIC)
1128 msvcrt_local_unwind2((MSVCRT_EXCEPTION_FRAME*)jmp->Registration,
1129 jmp->TryLevel, (void *)jmp->Ebp);
1131 else if(jmp->UnwindFunc)
1133 MSVCRT_unwind_function unwind_func;
1135 unwind_func=(MSVCRT_unwind_function)jmp->UnwindFunc;
1136 unwind_func(jmp);
1140 if (!retval)
1141 retval = 1;
1143 longjmp_set_regs( jmp, retval );
1146 /*********************************************************************
1147 * _seh_longjmp_unwind (MSVCRT.@)
1149 void __stdcall _seh_longjmp_unwind(struct MSVCRT___JUMP_BUFFER *jmp)
1151 msvcrt_local_unwind2( (MSVCRT_EXCEPTION_FRAME *)jmp->Registration, jmp->TryLevel, (void *)jmp->Ebp );
1154 /*********************************************************************
1155 * _seh_longjmp_unwind4 (MSVCRT.@)
1157 void __stdcall _seh_longjmp_unwind4(struct MSVCRT___JUMP_BUFFER *jmp)
1159 msvcrt_local_unwind4( (ULONG *)&jmp->Cookie, (MSVCRT_EXCEPTION_FRAME *)jmp->Registration,
1160 jmp->TryLevel, (void *)jmp->Ebp );
1163 /*********************************************************************
1164 * _fpieee_flt (MSVCRT.@)
1166 int __cdecl _fpieee_flt(ULONG exception_code, EXCEPTION_POINTERS *ep,
1167 int (__cdecl *handler)(_FPIEEE_RECORD*))
1169 FLOATING_SAVE_AREA *ctx = &ep->ContextRecord->FloatSave;
1170 _FPIEEE_RECORD rec;
1171 int ret;
1173 TRACE("(%x %p %p)\n", exception_code, ep, handler);
1175 switch(exception_code) {
1176 case STATUS_FLOAT_DIVIDE_BY_ZERO:
1177 case STATUS_FLOAT_INEXACT_RESULT:
1178 case STATUS_FLOAT_INVALID_OPERATION:
1179 case STATUS_FLOAT_OVERFLOW:
1180 case STATUS_FLOAT_UNDERFLOW:
1181 break;
1182 default:
1183 return EXCEPTION_CONTINUE_SEARCH;
1186 memset(&rec, 0, sizeof(rec));
1187 rec.RoundingMode = ctx->ControlWord >> 10;
1188 switch((ctx->ControlWord >> 8) & 0x3) {
1189 case 0: rec.Precision = 2; break;
1190 case 1: rec.Precision = 3; break;
1191 case 2: rec.Precision = 1; break;
1192 case 3: rec.Precision = 0; break;
1194 rec.Status.InvalidOperation = ctx->StatusWord & 0x1;
1195 rec.Status.ZeroDivide = ((ctx->StatusWord & 0x4) != 0);
1196 rec.Status.Overflow = ((ctx->StatusWord & 0x8) != 0);
1197 rec.Status.Underflow = ((ctx->StatusWord & 0x10) != 0);
1198 rec.Status.Inexact = ((ctx->StatusWord & 0x20) != 0);
1199 rec.Enable.InvalidOperation = ((ctx->ControlWord & 0x1) == 0);
1200 rec.Enable.ZeroDivide = ((ctx->ControlWord & 0x4) == 0);
1201 rec.Enable.Overflow = ((ctx->ControlWord & 0x8) == 0);
1202 rec.Enable.Underflow = ((ctx->ControlWord & 0x10) == 0);
1203 rec.Enable.Inexact = ((ctx->ControlWord & 0x20) == 0);
1204 rec.Cause.InvalidOperation = rec.Enable.InvalidOperation & rec.Status.InvalidOperation;
1205 rec.Cause.ZeroDivide = rec.Enable.ZeroDivide & rec.Status.ZeroDivide;
1206 rec.Cause.Overflow = rec.Enable.Overflow & rec.Status.Overflow;
1207 rec.Cause.Underflow = rec.Enable.Underflow & rec.Status.Underflow;
1208 rec.Cause.Inexact = rec.Enable.Inexact & rec.Status.Inexact;
1210 TRACE("opcode: %x\n", *(ULONG*)ep->ContextRecord->FloatSave.ErrorOffset);
1212 if(*(WORD*)ctx->ErrorOffset == 0x35dc) { /* fdiv m64fp */
1213 if(exception_code==STATUS_FLOAT_DIVIDE_BY_ZERO || exception_code==STATUS_FLOAT_INVALID_OPERATION) {
1214 rec.Operand1.OperandValid = 1;
1215 rec.Result.OperandValid = 0;
1216 } else {
1217 rec.Operand1.OperandValid = 0;
1218 rec.Result.OperandValid = 1;
1220 rec.Operand2.OperandValid = 1;
1221 rec.Operation = _FpCodeDivide;
1222 rec.Operand1.Format = _FpFormatFp80;
1223 memcpy(&rec.Operand1.Value.Fp80Value, ctx->RegisterArea, sizeof(rec.Operand1.Value.Fp80Value));
1224 rec.Operand2.Format = _FpFormatFp64;
1225 rec.Operand2.Value.Fp64Value = *(double*)ctx->DataOffset;
1226 rec.Result.Format = _FpFormatFp80;
1227 memcpy(&rec.Result.Value.Fp80Value, ctx->RegisterArea, sizeof(rec.Operand1.Value.Fp80Value));
1229 ret = handler(&rec);
1231 if(ret == EXCEPTION_CONTINUE_EXECUTION)
1232 memcpy(ctx->RegisterArea, &rec.Result.Value.Fp80Value, sizeof(rec.Operand1.Value.Fp80Value));
1233 return ret;
1236 FIXME("unsupported opcode: %x\n", *(ULONG*)ep->ContextRecord->FloatSave.ErrorOffset);
1237 return EXCEPTION_CONTINUE_SEARCH;
1240 #endif /* __i386__ */