wined3d: Merge wined3d_surface_upload_data() into texture2d_upload_data().
[wine.git] / dlls / msvcrt / except_i386.c
blob317a9616bb237e9230067dbd4e05813e5fa0cce7
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, cxx_exception_frame *frame,
404 const cxx_function_descr *descr, int nested_trylevel,
405 EXCEPTION_REGISTRATION_RECORD *catch_frame,
406 cxx_exception_type *info )
408 UINT i;
409 int j;
410 void *addr, *object = (void *)rec->ExceptionInformation[1];
411 struct catch_func_nested_frame nested_frame;
412 int trylevel = frame->trylevel;
413 DWORD save_esp = ((DWORD*)frame)[-1];
414 thread_data_t *data;
416 for (i = 0; i < descr->tryblock_count; i++)
418 const tryblock_info *tryblock = &descr->tryblock[i];
420 /* only handle try blocks inside current catch block */
421 if (catch_frame && nested_trylevel > tryblock->start_level) continue;
423 if (trylevel < tryblock->start_level) continue;
424 if (trylevel > tryblock->end_level) continue;
426 /* got a try block */
427 for (j = 0; j < tryblock->catchblock_count; j++)
429 const catchblock_info *catchblock = &tryblock->catchblock[j];
430 if(info)
432 const cxx_type_info *type = find_caught_type( info,
433 catchblock->type_info, catchblock->flags );
434 if (!type) continue;
436 TRACE( "matched type %p in tryblock %d catchblock %d\n", type, i, j );
438 /* copy the exception to its destination on the stack */
439 copy_exception( object, frame, catchblock, type );
441 else
443 /* no CXX_EXCEPTION only proceed with a catch(...) block*/
444 if(catchblock->type_info)
445 continue;
446 TRACE("found catch(...) block\n");
449 /* Add frame info here so exception is not freed inside RtlUnwind call */
450 _CreateFrameInfo(&nested_frame.frame_info.frame_info,
451 (void*)rec->ExceptionInformation[1]);
453 /* unwind the stack */
454 RtlUnwind( catch_frame ? catch_frame : &frame->frame, 0, rec, 0 );
455 cxx_local_unwind( frame, descr, tryblock->start_level );
456 frame->trylevel = tryblock->end_level + 1;
458 data = msvcrt_get_thread_data();
459 nested_frame.frame_info.rec = data->exc_record;
460 data->exc_record = rec;
462 /* call the catch block */
463 TRACE( "calling catch block %p addr %p ebp %p\n",
464 catchblock, catchblock->handler, &frame->ebp );
466 /* setup an exception block for nested exceptions */
467 nested_frame.frame.Handler = catch_function_nested_handler;
468 nested_frame.cxx_frame = frame;
469 nested_frame.descr = descr;
470 nested_frame.trylevel = nested_trylevel + 1;
472 __wine_push_frame( &nested_frame.frame );
473 addr = call_ebp_func( catchblock->handler, &frame->ebp );
474 __wine_pop_frame( &nested_frame.frame );
476 ((DWORD*)frame)[-1] = save_esp;
477 __CxxUnregisterExceptionObject(&nested_frame.frame_info, FALSE);
478 TRACE( "done, continuing at %p\n", addr );
480 continue_after_catch( frame, addr );
485 /*********************************************************************
486 * __CxxExceptionFilter (MSVCRT.@)
488 int CDECL __CxxExceptionFilter( PEXCEPTION_POINTERS ptrs,
489 const type_info *ti, int flags, void **copy)
491 const cxx_type_info *type;
492 PEXCEPTION_RECORD rec;
494 TRACE( "%p %p %x %p\n", ptrs, ti, flags, copy );
496 if (!ptrs) return EXCEPTION_CONTINUE_SEARCH;
498 /* handle catch(...) */
499 if (!ti) return EXCEPTION_EXECUTE_HANDLER;
501 rec = ptrs->ExceptionRecord;
502 if (rec->ExceptionCode != CXX_EXCEPTION || rec->NumberParameters != 3 ||
503 rec->ExceptionInformation[0] < CXX_FRAME_MAGIC_VC6 ||
504 rec->ExceptionInformation[0] > CXX_FRAME_MAGIC_VC8)
505 return EXCEPTION_CONTINUE_SEARCH;
507 if (rec->ExceptionInformation[1] == 0 && rec->ExceptionInformation[2] == 0)
509 rec = msvcrt_get_thread_data()->exc_record;
510 if (!rec) return EXCEPTION_CONTINUE_SEARCH;
513 type = find_caught_type( (cxx_exception_type*)rec->ExceptionInformation[2], ti, flags );
514 if (!type) return EXCEPTION_CONTINUE_SEARCH;
516 if (copy)
518 void *object = (void *)rec->ExceptionInformation[1];
520 if (flags & TYPE_FLAG_REFERENCE)
522 *copy = get_this_pointer( &type->offsets, object );
524 else if (type->flags & CLASS_IS_SIMPLE_TYPE)
526 memmove( copy, object, type->size );
527 /* if it is a pointer, adjust it */
528 if (type->size == sizeof(void*)) *copy = get_this_pointer( &type->offsets, *copy );
530 else /* copy the object */
532 if (type->copy_ctor)
533 call_copy_ctor( type->copy_ctor, copy, get_this_pointer(&type->offsets,object),
534 (type->flags & CLASS_HAS_VIRTUAL_BASE_CLASS) );
535 else
536 memmove( copy, get_this_pointer(&type->offsets,object), type->size );
539 return EXCEPTION_EXECUTE_HANDLER;
542 static LONG CALLBACK se_translation_filter( EXCEPTION_POINTERS *ep, void *c )
544 se_translator_ctx *ctx = (se_translator_ctx *)c;
545 EXCEPTION_RECORD *rec = ep->ExceptionRecord;
546 cxx_exception_type *exc_type;
548 if (rec->ExceptionCode != CXX_EXCEPTION)
550 TRACE( "non-c++ exception thrown in SEH handler: %x\n", rec->ExceptionCode );
551 MSVCRT_terminate();
554 exc_type = (cxx_exception_type *)rec->ExceptionInformation[2];
555 call_catch_block( rec, ctx->frame, ctx->descr,
556 ctx->frame->trylevel, ctx->nested_frame, exc_type );
558 __DestructExceptionObject( rec );
559 return ExceptionContinueSearch;
562 /*********************************************************************
563 * cxx_frame_handler
565 * Implementation of __CxxFrameHandler.
567 DWORD CDECL cxx_frame_handler( PEXCEPTION_RECORD rec, cxx_exception_frame* frame,
568 PCONTEXT context, EXCEPTION_REGISTRATION_RECORD** dispatch,
569 const cxx_function_descr *descr,
570 EXCEPTION_REGISTRATION_RECORD* nested_frame,
571 int nested_trylevel )
573 cxx_exception_type *exc_type;
575 if (descr->magic < CXX_FRAME_MAGIC_VC6 || descr->magic > CXX_FRAME_MAGIC_VC8)
577 ERR( "invalid frame magic %x\n", descr->magic );
578 return ExceptionContinueSearch;
580 if (descr->magic >= CXX_FRAME_MAGIC_VC8 &&
581 (descr->flags & FUNC_DESCR_SYNCHRONOUS) &&
582 (rec->ExceptionCode != CXX_EXCEPTION))
583 return ExceptionContinueSearch; /* handle only c++ exceptions */
585 if (rec->ExceptionFlags & (EH_UNWINDING|EH_EXIT_UNWIND))
587 if (descr->unwind_count && !nested_trylevel) cxx_local_unwind( frame, descr, -1 );
588 return ExceptionContinueSearch;
590 if (!descr->tryblock_count) return ExceptionContinueSearch;
592 if(rec->ExceptionCode == CXX_EXCEPTION &&
593 rec->ExceptionInformation[1] == 0 && rec->ExceptionInformation[2] == 0)
595 *rec = *msvcrt_get_thread_data()->exc_record;
596 rec->ExceptionFlags &= ~EH_UNWINDING;
597 if(TRACE_ON(seh)) {
598 TRACE("detect rethrow: exception code: %x\n", rec->ExceptionCode);
599 if(rec->ExceptionCode == CXX_EXCEPTION)
600 TRACE("re-propage: obj: %lx, type: %lx\n",
601 rec->ExceptionInformation[1], rec->ExceptionInformation[2]);
605 if(rec->ExceptionCode == CXX_EXCEPTION)
607 exc_type = (cxx_exception_type *)rec->ExceptionInformation[2];
609 if (rec->ExceptionInformation[0] > CXX_FRAME_MAGIC_VC8 &&
610 exc_type->custom_handler)
612 return exc_type->custom_handler( rec, frame, context, dispatch,
613 descr, nested_trylevel, nested_frame, 0 );
616 if (TRACE_ON(seh))
618 TRACE("handling C++ exception rec %p frame %p trylevel %d descr %p nested_frame %p\n",
619 rec, frame, frame->trylevel, descr, nested_frame );
620 dump_exception_type( exc_type );
621 dump_function_descr( descr );
624 else
626 thread_data_t *data = msvcrt_get_thread_data();
628 exc_type = NULL;
629 TRACE("handling C exception code %x rec %p frame %p trylevel %d descr %p nested_frame %p\n",
630 rec->ExceptionCode, rec, frame, frame->trylevel, descr, nested_frame );
632 if (data->se_translator) {
633 EXCEPTION_POINTERS except_ptrs;
634 se_translator_ctx ctx;
636 ctx.frame = frame;
637 ctx.descr = descr;
638 ctx.nested_frame = nested_frame;
639 __TRY
641 except_ptrs.ExceptionRecord = rec;
642 except_ptrs.ContextRecord = context;
643 data->se_translator( rec->ExceptionCode, &except_ptrs );
645 __EXCEPT_CTX(se_translation_filter, &ctx)
648 __ENDTRY
652 call_catch_block( rec, frame, descr, frame->trylevel, nested_frame, exc_type );
653 return ExceptionContinueSearch;
657 /*********************************************************************
658 * __CxxFrameHandler (MSVCRT.@)
660 extern DWORD CDECL __CxxFrameHandler( PEXCEPTION_RECORD rec, EXCEPTION_REGISTRATION_RECORD* frame,
661 PCONTEXT context, EXCEPTION_REGISTRATION_RECORD** dispatch );
662 __ASM_GLOBAL_FUNC( __CxxFrameHandler,
663 "pushl $0\n\t" /* nested_trylevel */
664 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
665 "pushl $0\n\t" /* nested_frame */
666 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
667 "pushl %eax\n\t" /* descr */
668 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
669 "pushl 28(%esp)\n\t" /* dispatch */
670 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
671 "pushl 28(%esp)\n\t" /* context */
672 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
673 "pushl 28(%esp)\n\t" /* frame */
674 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
675 "pushl 28(%esp)\n\t" /* rec */
676 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
677 "call " __ASM_NAME("cxx_frame_handler") "\n\t"
678 "add $28,%esp\n\t"
679 __ASM_CFI(".cfi_adjust_cfa_offset -28\n\t")
680 "ret" )
683 /*********************************************************************
684 * __CxxLongjmpUnwind (MSVCRT.@)
686 * Callback meant to be used as UnwindFunc for setjmp/longjmp.
688 void __stdcall __CxxLongjmpUnwind( const struct MSVCRT___JUMP_BUFFER *buf )
690 cxx_exception_frame *frame = (cxx_exception_frame *)buf->Registration;
691 const cxx_function_descr *descr = (const cxx_function_descr *)buf->UnwindData[0];
693 TRACE( "unwinding frame %p descr %p trylevel %ld\n", frame, descr, buf->TryLevel );
694 cxx_local_unwind( frame, descr, buf->TryLevel );
697 /*********************************************************************
698 * __CppXcptFilter (MSVCRT.@)
700 int CDECL __CppXcptFilter(NTSTATUS ex, PEXCEPTION_POINTERS ptr)
702 /* only filter c++ exceptions */
703 if (ex != CXX_EXCEPTION) return EXCEPTION_CONTINUE_SEARCH;
704 return _XcptFilter( ex, ptr );
707 /*********************************************************************
708 * __CxxDetectRethrow (MSVCRT.@)
710 BOOL CDECL __CxxDetectRethrow(PEXCEPTION_POINTERS ptrs)
712 PEXCEPTION_RECORD rec;
714 if (!ptrs)
715 return FALSE;
717 rec = ptrs->ExceptionRecord;
719 if (rec->ExceptionCode == CXX_EXCEPTION &&
720 rec->NumberParameters == 3 &&
721 rec->ExceptionInformation[0] == CXX_FRAME_MAGIC_VC6 &&
722 rec->ExceptionInformation[2])
724 ptrs->ExceptionRecord = msvcrt_get_thread_data()->exc_record;
725 return TRUE;
727 return (msvcrt_get_thread_data()->exc_record == rec);
730 /*********************************************************************
731 * __CxxQueryExceptionSize (MSVCRT.@)
733 unsigned int CDECL __CxxQueryExceptionSize(void)
735 return sizeof(cxx_exception_type);
739 /*********************************************************************
740 * _EH_prolog (MSVCRT.@)
743 /* Provided for VC++ binary compatibility only */
744 __ASM_GLOBAL_FUNC(_EH_prolog,
745 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") /* skip ret addr */
746 "pushl $-1\n\t"
747 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
748 "pushl %eax\n\t"
749 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
750 "pushl %fs:0\n\t"
751 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
752 "movl %esp, %fs:0\n\t"
753 "movl 12(%esp), %eax\n\t"
754 "movl %ebp, 12(%esp)\n\t"
755 "leal 12(%esp), %ebp\n\t"
756 "pushl %eax\n\t"
757 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
758 "ret")
760 static const SCOPETABLE_V4 *get_scopetable_v4( MSVCRT_EXCEPTION_FRAME *frame, ULONG_PTR cookie )
762 return (const SCOPETABLE_V4 *)((ULONG_PTR)frame->scopetable ^ cookie);
765 static DWORD MSVCRT_nested_handler(PEXCEPTION_RECORD rec,
766 EXCEPTION_REGISTRATION_RECORD* frame,
767 PCONTEXT context,
768 EXCEPTION_REGISTRATION_RECORD** dispatch)
770 if (!(rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
771 return ExceptionContinueSearch;
772 *dispatch = frame;
773 return ExceptionCollidedUnwind;
776 static void msvcrt_local_unwind2(MSVCRT_EXCEPTION_FRAME* frame, int trylevel, void *ebp)
778 EXCEPTION_REGISTRATION_RECORD reg;
780 TRACE("(%p,%d,%d)\n",frame, frame->trylevel, trylevel);
782 /* Register a handler in case of a nested exception */
783 reg.Handler = MSVCRT_nested_handler;
784 reg.Prev = NtCurrentTeb()->Tib.ExceptionList;
785 __wine_push_frame(&reg);
787 while (frame->trylevel != TRYLEVEL_END && frame->trylevel != trylevel)
789 int level = frame->trylevel;
790 frame->trylevel = frame->scopetable[level].previousTryLevel;
791 if (!frame->scopetable[level].lpfnFilter)
793 TRACE( "__try block cleanup level %d handler %p ebp %p\n",
794 level, frame->scopetable[level].lpfnHandler, ebp );
795 call_unwind_func( frame->scopetable[level].lpfnHandler, ebp );
798 __wine_pop_frame(&reg);
799 TRACE("unwound OK\n");
802 static void msvcrt_local_unwind4( ULONG *cookie, MSVCRT_EXCEPTION_FRAME* frame, int trylevel, void *ebp )
804 EXCEPTION_REGISTRATION_RECORD reg;
805 const SCOPETABLE_V4 *scopetable = get_scopetable_v4( frame, *cookie );
807 TRACE("(%p,%d,%d)\n",frame, frame->trylevel, trylevel);
809 /* Register a handler in case of a nested exception */
810 reg.Handler = MSVCRT_nested_handler;
811 reg.Prev = NtCurrentTeb()->Tib.ExceptionList;
812 __wine_push_frame(&reg);
814 while (frame->trylevel != -2 && frame->trylevel != trylevel)
816 int level = frame->trylevel;
817 frame->trylevel = scopetable->entries[level].previousTryLevel;
818 if (!scopetable->entries[level].lpfnFilter)
820 TRACE( "__try block cleanup level %d handler %p ebp %p\n",
821 level, scopetable->entries[level].lpfnHandler, ebp );
822 call_unwind_func( scopetable->entries[level].lpfnHandler, ebp );
825 __wine_pop_frame(&reg);
826 TRACE("unwound OK\n");
829 /*******************************************************************
830 * _local_unwind2 (MSVCRT.@)
832 void CDECL _local_unwind2(MSVCRT_EXCEPTION_FRAME* frame, int trylevel)
834 msvcrt_local_unwind2( frame, trylevel, &frame->_ebp );
837 /*******************************************************************
838 * _local_unwind4 (MSVCRT.@)
840 void CDECL _local_unwind4( ULONG *cookie, MSVCRT_EXCEPTION_FRAME* frame, int trylevel )
842 msvcrt_local_unwind4( cookie, frame, trylevel, &frame->_ebp );
845 /*******************************************************************
846 * _global_unwind2 (MSVCRT.@)
848 void CDECL _global_unwind2(EXCEPTION_REGISTRATION_RECORD* frame)
850 TRACE("(%p)\n",frame);
851 RtlUnwind( frame, 0, 0, 0 );
854 /*********************************************************************
855 * _except_handler2 (MSVCRT.@)
857 int CDECL _except_handler2(PEXCEPTION_RECORD rec,
858 EXCEPTION_REGISTRATION_RECORD* frame,
859 PCONTEXT context,
860 EXCEPTION_REGISTRATION_RECORD** dispatcher)
862 FIXME("exception %x flags=%x at %p handler=%p %p %p stub\n",
863 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
864 frame->Handler, context, dispatcher);
865 return ExceptionContinueSearch;
868 /*********************************************************************
869 * _except_handler3 (MSVCRT.@)
871 int CDECL _except_handler3(PEXCEPTION_RECORD rec,
872 MSVCRT_EXCEPTION_FRAME* frame,
873 PCONTEXT context, void* dispatcher)
875 int retval, trylevel;
876 EXCEPTION_POINTERS exceptPtrs;
877 PSCOPETABLE pScopeTable;
879 TRACE("exception %x flags=%x at %p handler=%p %p %p semi-stub\n",
880 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
881 frame->handler, context, dispatcher);
883 __asm__ __volatile__ ("cld");
885 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
887 /* Unwinding the current frame */
888 msvcrt_local_unwind2(frame, TRYLEVEL_END, &frame->_ebp);
889 TRACE("unwound current frame, returning ExceptionContinueSearch\n");
890 return ExceptionContinueSearch;
892 else
894 /* Hunting for handler */
895 exceptPtrs.ExceptionRecord = rec;
896 exceptPtrs.ContextRecord = context;
897 *((DWORD *)frame-1) = (DWORD)&exceptPtrs;
898 trylevel = frame->trylevel;
899 pScopeTable = frame->scopetable;
901 while (trylevel != TRYLEVEL_END)
903 TRACE( "level %d prev %d filter %p\n", trylevel, pScopeTable[trylevel].previousTryLevel,
904 pScopeTable[trylevel].lpfnFilter );
905 if (pScopeTable[trylevel].lpfnFilter)
907 retval = call_filter( pScopeTable[trylevel].lpfnFilter, &exceptPtrs, &frame->_ebp );
909 TRACE("filter returned %s\n", retval == EXCEPTION_CONTINUE_EXECUTION ?
910 "CONTINUE_EXECUTION" : retval == EXCEPTION_EXECUTE_HANDLER ?
911 "EXECUTE_HANDLER" : "CONTINUE_SEARCH");
913 if (retval == EXCEPTION_CONTINUE_EXECUTION)
914 return ExceptionContinueExecution;
916 if (retval == EXCEPTION_EXECUTE_HANDLER)
918 /* Unwind all higher frames, this one will handle the exception */
919 _global_unwind2((EXCEPTION_REGISTRATION_RECORD*)frame);
920 msvcrt_local_unwind2(frame, trylevel, &frame->_ebp);
922 /* Set our trylevel to the enclosing block, and call the __finally
923 * code, which won't return
925 frame->trylevel = pScopeTable[trylevel].previousTryLevel;
926 TRACE("__finally block %p\n",pScopeTable[trylevel].lpfnHandler);
927 call_finally_block(pScopeTable[trylevel].lpfnHandler, &frame->_ebp);
928 ERR("Returned from __finally block - expect crash!\n");
931 trylevel = pScopeTable[trylevel].previousTryLevel;
934 TRACE("reached TRYLEVEL_END, returning ExceptionContinueSearch\n");
935 return ExceptionContinueSearch;
938 /*********************************************************************
939 * _except_handler4_common (MSVCRT.@)
941 int CDECL _except_handler4_common( ULONG *cookie, void (*check_cookie)(void),
942 EXCEPTION_RECORD *rec, MSVCRT_EXCEPTION_FRAME *frame,
943 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
945 int retval, trylevel;
946 EXCEPTION_POINTERS exceptPtrs;
947 const SCOPETABLE_V4 *scope_table = get_scopetable_v4( frame, *cookie );
949 TRACE( "exception %x flags=%x at %p handler=%p %p %p cookie=%x scope table=%p cookies=%d/%x,%d/%x\n",
950 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
951 frame->handler, context, dispatcher, *cookie, scope_table,
952 scope_table->gs_cookie_offset, scope_table->gs_cookie_xor,
953 scope_table->eh_cookie_offset, scope_table->eh_cookie_xor );
955 /* FIXME: no cookie validation yet */
957 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
959 /* Unwinding the current frame */
960 msvcrt_local_unwind4( cookie, frame, -2, &frame->_ebp );
961 TRACE("unwound current frame, returning ExceptionContinueSearch\n");
962 return ExceptionContinueSearch;
964 else
966 /* Hunting for handler */
967 exceptPtrs.ExceptionRecord = rec;
968 exceptPtrs.ContextRecord = context;
969 *((DWORD *)frame-1) = (DWORD)&exceptPtrs;
970 trylevel = frame->trylevel;
972 while (trylevel != -2)
974 TRACE( "level %d prev %d filter %p\n", trylevel,
975 scope_table->entries[trylevel].previousTryLevel,
976 scope_table->entries[trylevel].lpfnFilter );
977 if (scope_table->entries[trylevel].lpfnFilter)
979 retval = call_filter( scope_table->entries[trylevel].lpfnFilter, &exceptPtrs, &frame->_ebp );
981 TRACE("filter returned %s\n", retval == EXCEPTION_CONTINUE_EXECUTION ?
982 "CONTINUE_EXECUTION" : retval == EXCEPTION_EXECUTE_HANDLER ?
983 "EXECUTE_HANDLER" : "CONTINUE_SEARCH");
985 if (retval == EXCEPTION_CONTINUE_EXECUTION)
986 return ExceptionContinueExecution;
988 if (retval == EXCEPTION_EXECUTE_HANDLER)
990 __DestructExceptionObject(rec);
992 /* Unwind all higher frames, this one will handle the exception */
993 _global_unwind2((EXCEPTION_REGISTRATION_RECORD*)frame);
994 msvcrt_local_unwind4( cookie, frame, trylevel, &frame->_ebp );
996 /* Set our trylevel to the enclosing block, and call the __finally
997 * code, which won't return
999 frame->trylevel = scope_table->entries[trylevel].previousTryLevel;
1000 TRACE("__finally block %p\n",scope_table->entries[trylevel].lpfnHandler);
1001 call_finally_block(scope_table->entries[trylevel].lpfnHandler, &frame->_ebp);
1002 ERR("Returned from __finally block - expect crash!\n");
1005 trylevel = scope_table->entries[trylevel].previousTryLevel;
1008 TRACE("reached -2, returning ExceptionContinueSearch\n");
1009 return ExceptionContinueSearch;
1014 * setjmp/longjmp implementation
1017 #define MSVCRT_JMP_MAGIC 0x56433230 /* ID value for new jump structure */
1018 typedef void (__stdcall *MSVCRT_unwind_function)(const struct MSVCRT___JUMP_BUFFER *);
1020 /* define an entrypoint for setjmp/setjmp3 that stores the registers in the jmp buf */
1021 /* and then jumps to the C backend function */
1022 #define DEFINE_SETJMP_ENTRYPOINT(name) \
1023 __ASM_GLOBAL_FUNC( name, \
1024 "movl 4(%esp),%ecx\n\t" /* jmp_buf */ \
1025 "movl %ebp,0(%ecx)\n\t" /* jmp_buf.Ebp */ \
1026 "movl %ebx,4(%ecx)\n\t" /* jmp_buf.Ebx */ \
1027 "movl %edi,8(%ecx)\n\t" /* jmp_buf.Edi */ \
1028 "movl %esi,12(%ecx)\n\t" /* jmp_buf.Esi */ \
1029 "movl %esp,16(%ecx)\n\t" /* jmp_buf.Esp */ \
1030 "movl 0(%esp),%eax\n\t" \
1031 "movl %eax,20(%ecx)\n\t" /* jmp_buf.Eip */ \
1032 "jmp " __ASM_NAME("__regs_") # name )
1034 /* restore the registers from the jmp buf upon longjmp */
1035 extern void DECLSPEC_NORETURN longjmp_set_regs( struct MSVCRT___JUMP_BUFFER *jmp, int retval );
1036 __ASM_GLOBAL_FUNC( longjmp_set_regs,
1037 "movl 4(%esp),%ecx\n\t" /* jmp_buf */
1038 "movl 8(%esp),%eax\n\t" /* retval */
1039 "movl 0(%ecx),%ebp\n\t" /* jmp_buf.Ebp */
1040 "movl 4(%ecx),%ebx\n\t" /* jmp_buf.Ebx */
1041 "movl 8(%ecx),%edi\n\t" /* jmp_buf.Edi */
1042 "movl 12(%ecx),%esi\n\t" /* jmp_buf.Esi */
1043 "movl 16(%ecx),%esp\n\t" /* jmp_buf.Esp */
1044 "addl $4,%esp\n\t" /* get rid of return address */
1045 "jmp *20(%ecx)\n\t" /* jmp_buf.Eip */ )
1048 * The signatures of the setjmp/longjmp functions do not match that
1049 * declared in the setjmp header so they don't follow the regular naming
1050 * convention to avoid conflicts.
1053 /*******************************************************************
1054 * _setjmp (MSVCRT.@)
1056 DEFINE_SETJMP_ENTRYPOINT(MSVCRT__setjmp)
1057 int CDECL DECLSPEC_HIDDEN __regs_MSVCRT__setjmp(struct MSVCRT___JUMP_BUFFER *jmp)
1059 jmp->Registration = (unsigned long)NtCurrentTeb()->Tib.ExceptionList;
1060 if (jmp->Registration == ~0UL)
1061 jmp->TryLevel = TRYLEVEL_END;
1062 else
1063 jmp->TryLevel = ((MSVCRT_EXCEPTION_FRAME*)jmp->Registration)->trylevel;
1065 TRACE("buf=%p ebx=%08lx esi=%08lx edi=%08lx ebp=%08lx esp=%08lx eip=%08lx frame=%08lx\n",
1066 jmp, jmp->Ebx, jmp->Esi, jmp->Edi, jmp->Ebp, jmp->Esp, jmp->Eip, jmp->Registration );
1067 return 0;
1070 /*******************************************************************
1071 * _setjmp3 (MSVCRT.@)
1073 DEFINE_SETJMP_ENTRYPOINT( MSVCRT__setjmp3 )
1074 int WINAPIV DECLSPEC_HIDDEN __regs_MSVCRT__setjmp3(struct MSVCRT___JUMP_BUFFER *jmp, int nb_args, ...)
1076 jmp->Cookie = MSVCRT_JMP_MAGIC;
1077 jmp->UnwindFunc = 0;
1078 jmp->Registration = (unsigned long)NtCurrentTeb()->Tib.ExceptionList;
1079 if (jmp->Registration == ~0UL)
1081 jmp->TryLevel = TRYLEVEL_END;
1083 else
1085 int i;
1086 va_list args;
1088 va_start( args, nb_args );
1089 if (nb_args > 0) jmp->UnwindFunc = va_arg( args, unsigned long );
1090 if (nb_args > 1) jmp->TryLevel = va_arg( args, unsigned long );
1091 else jmp->TryLevel = ((MSVCRT_EXCEPTION_FRAME*)jmp->Registration)->trylevel;
1092 for (i = 0; i < 6 && i < nb_args - 2; i++)
1093 jmp->UnwindData[i] = va_arg( args, unsigned long );
1094 va_end( args );
1097 TRACE("buf=%p ebx=%08lx esi=%08lx edi=%08lx ebp=%08lx esp=%08lx eip=%08lx frame=%08lx\n",
1098 jmp, jmp->Ebx, jmp->Esi, jmp->Edi, jmp->Ebp, jmp->Esp, jmp->Eip, jmp->Registration );
1099 return 0;
1102 /*********************************************************************
1103 * longjmp (MSVCRT.@)
1105 void CDECL MSVCRT_longjmp(struct MSVCRT___JUMP_BUFFER *jmp, int retval)
1107 unsigned long cur_frame = 0;
1109 TRACE("buf=%p ebx=%08lx esi=%08lx edi=%08lx ebp=%08lx esp=%08lx eip=%08lx frame=%08lx retval=%08x\n",
1110 jmp, jmp->Ebx, jmp->Esi, jmp->Edi, jmp->Ebp, jmp->Esp, jmp->Eip, jmp->Registration, retval );
1112 cur_frame=(unsigned long)NtCurrentTeb()->Tib.ExceptionList;
1113 TRACE("cur_frame=%lx\n",cur_frame);
1115 if (cur_frame != jmp->Registration)
1116 _global_unwind2((EXCEPTION_REGISTRATION_RECORD*)jmp->Registration);
1118 if (jmp->Registration)
1120 if (IsBadReadPtr(&jmp->Cookie, sizeof(long)) || jmp->Cookie != MSVCRT_JMP_MAGIC)
1122 msvcrt_local_unwind2((MSVCRT_EXCEPTION_FRAME*)jmp->Registration,
1123 jmp->TryLevel, (void *)jmp->Ebp);
1125 else if(jmp->UnwindFunc)
1127 MSVCRT_unwind_function unwind_func;
1129 unwind_func=(MSVCRT_unwind_function)jmp->UnwindFunc;
1130 unwind_func(jmp);
1134 if (!retval)
1135 retval = 1;
1137 longjmp_set_regs( jmp, retval );
1140 /*********************************************************************
1141 * _seh_longjmp_unwind (MSVCRT.@)
1143 void __stdcall _seh_longjmp_unwind(struct MSVCRT___JUMP_BUFFER *jmp)
1145 msvcrt_local_unwind2( (MSVCRT_EXCEPTION_FRAME *)jmp->Registration, jmp->TryLevel, (void *)jmp->Ebp );
1148 /*********************************************************************
1149 * _seh_longjmp_unwind4 (MSVCRT.@)
1151 void __stdcall _seh_longjmp_unwind4(struct MSVCRT___JUMP_BUFFER *jmp)
1153 msvcrt_local_unwind4( (ULONG *)&jmp->Cookie, (MSVCRT_EXCEPTION_FRAME *)jmp->Registration,
1154 jmp->TryLevel, (void *)jmp->Ebp );
1157 /*********************************************************************
1158 * _fpieee_flt (MSVCRT.@)
1160 int __cdecl _fpieee_flt(ULONG exception_code, EXCEPTION_POINTERS *ep,
1161 int (__cdecl *handler)(_FPIEEE_RECORD*))
1163 FLOATING_SAVE_AREA *ctx = &ep->ContextRecord->FloatSave;
1164 _FPIEEE_RECORD rec;
1165 int ret;
1167 TRACE("(%x %p %p)\n", exception_code, ep, handler);
1169 switch(exception_code) {
1170 case STATUS_FLOAT_DIVIDE_BY_ZERO:
1171 case STATUS_FLOAT_INEXACT_RESULT:
1172 case STATUS_FLOAT_INVALID_OPERATION:
1173 case STATUS_FLOAT_OVERFLOW:
1174 case STATUS_FLOAT_UNDERFLOW:
1175 break;
1176 default:
1177 return EXCEPTION_CONTINUE_SEARCH;
1180 memset(&rec, 0, sizeof(rec));
1181 rec.RoundingMode = ctx->ControlWord >> 10;
1182 switch((ctx->ControlWord >> 8) & 0x3) {
1183 case 0: rec.Precision = 2; break;
1184 case 1: rec.Precision = 3; break;
1185 case 2: rec.Precision = 1; break;
1186 case 3: rec.Precision = 0; break;
1188 rec.Status.InvalidOperation = ctx->StatusWord & 0x1;
1189 rec.Status.ZeroDivide = ((ctx->StatusWord & 0x4) != 0);
1190 rec.Status.Overflow = ((ctx->StatusWord & 0x8) != 0);
1191 rec.Status.Underflow = ((ctx->StatusWord & 0x10) != 0);
1192 rec.Status.Inexact = ((ctx->StatusWord & 0x20) != 0);
1193 rec.Enable.InvalidOperation = ((ctx->ControlWord & 0x1) == 0);
1194 rec.Enable.ZeroDivide = ((ctx->ControlWord & 0x4) == 0);
1195 rec.Enable.Overflow = ((ctx->ControlWord & 0x8) == 0);
1196 rec.Enable.Underflow = ((ctx->ControlWord & 0x10) == 0);
1197 rec.Enable.Inexact = ((ctx->ControlWord & 0x20) == 0);
1198 rec.Cause.InvalidOperation = rec.Enable.InvalidOperation & rec.Status.InvalidOperation;
1199 rec.Cause.ZeroDivide = rec.Enable.ZeroDivide & rec.Status.ZeroDivide;
1200 rec.Cause.Overflow = rec.Enable.Overflow & rec.Status.Overflow;
1201 rec.Cause.Underflow = rec.Enable.Underflow & rec.Status.Underflow;
1202 rec.Cause.Inexact = rec.Enable.Inexact & rec.Status.Inexact;
1204 TRACE("opcode: %x\n", *(ULONG*)ep->ContextRecord->FloatSave.ErrorOffset);
1206 if(*(WORD*)ctx->ErrorOffset == 0x35dc) { /* fdiv m64fp */
1207 if(exception_code==STATUS_FLOAT_DIVIDE_BY_ZERO || exception_code==STATUS_FLOAT_INVALID_OPERATION) {
1208 rec.Operand1.OperandValid = 1;
1209 rec.Result.OperandValid = 0;
1210 } else {
1211 rec.Operand1.OperandValid = 0;
1212 rec.Result.OperandValid = 1;
1214 rec.Operand2.OperandValid = 1;
1215 rec.Operation = _FpCodeDivide;
1216 rec.Operand1.Format = _FpFormatFp80;
1217 memcpy(&rec.Operand1.Value.Fp80Value, ctx->RegisterArea, sizeof(rec.Operand1.Value.Fp80Value));
1218 rec.Operand2.Format = _FpFormatFp64;
1219 rec.Operand2.Value.Fp64Value = *(double*)ctx->DataOffset;
1220 rec.Result.Format = _FpFormatFp80;
1221 memcpy(&rec.Result.Value.Fp80Value, ctx->RegisterArea, sizeof(rec.Operand1.Value.Fp80Value));
1223 ret = handler(&rec);
1225 if(ret == EXCEPTION_CONTINUE_EXECUTION)
1226 memcpy(ctx->RegisterArea, &rec.Result.Value.Fp80Value, sizeof(rec.Operand1.Value.Fp80Value));
1227 return ret;
1230 FIXME("unsupported opcode: %x\n", *(ULONG*)ep->ContextRecord->FloatSave.ErrorOffset);
1231 return EXCEPTION_CONTINUE_SEARCH;
1234 #endif /* __i386__ */