tinycc-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: [Tinycc-devel] tcc and fp example


From: Yann Bourrigault
Subject: RE: [Tinycc-devel] tcc and fp example
Date: Tue, 19 Oct 2010 09:52:29 +0100

FYi, here's a part of code extracted from the MinGW crt startup that handles 
the signals correctly on Windows. You can execute it at the beginning of your 
code or add it to the tcc crt startup.

#include <signal.h>
#include <windows.h>

/* This function will be called when a trap occurs. Thanks to Jacob
   Navia for his contribution. */
static CALLBACK long
_gnu_exception_handler (EXCEPTION_POINTERS * exception_data)
{
  void (*old_handler) (int);
  long action = EXCEPTION_CONTINUE_SEARCH;
  int reset_fpu = 0;

  switch (exception_data->ExceptionRecord->ExceptionCode)
    {
    case EXCEPTION_ACCESS_VIOLATION:
      /* test if the user has set SIGSEGV */
      old_handler = signal (SIGSEGV, SIG_DFL);
      if (old_handler == SIG_IGN)
        {
          /* this is undefined if the signal was raised by anything other
             than raise ().  */
          signal (SIGSEGV, SIG_IGN);
          action = EXCEPTION_CONTINUE_EXECUTION;
        }
      else if (old_handler != SIG_DFL)
        {
          /* This means 'old' is a user defined function. Call it */
          (*old_handler) (SIGSEGV);
          action = EXCEPTION_CONTINUE_EXECUTION;
        }
      break;

    case EXCEPTION_ILLEGAL_INSTRUCTION:
    case EXCEPTION_PRIV_INSTRUCTION:
      /* test if the user has set SIGILL */
      old_handler = signal (SIGILL, SIG_DFL);
      if (old_handler == SIG_IGN)
        {
          /* this is undefined if the signal was raised by anything other
             than raise ().  */
          signal (SIGILL, SIG_IGN);
          action = EXCEPTION_CONTINUE_EXECUTION;
        }
      else if (old_handler != SIG_DFL)
        {
          /* This means 'old' is a user defined function. Call it */
          (*old_handler) (SIGILL);
          action = EXCEPTION_CONTINUE_EXECUTION;
        }
      break;

    case EXCEPTION_FLT_INVALID_OPERATION:
    case EXCEPTION_FLT_DIVIDE_BY_ZERO:
    case EXCEPTION_FLT_DENORMAL_OPERAND:
    case EXCEPTION_FLT_OVERFLOW:
    case EXCEPTION_FLT_UNDERFLOW:
    case EXCEPTION_FLT_INEXACT_RESULT:
      reset_fpu = 1;
      /* fall through. */

    case EXCEPTION_INT_DIVIDE_BY_ZERO:
      /* test if the user has set SIGFPE */
      old_handler = signal (SIGFPE, SIG_DFL);
      if (old_handler == SIG_IGN)
        {
          signal (SIGFPE, SIG_IGN);
          if (reset_fpu)
            _fpreset ();
          action = EXCEPTION_CONTINUE_EXECUTION;
        }
      else if (old_handler != SIG_DFL)
        {
          /* This means 'old' is a user defined function. Call it */
          (*old_handler) (SIGFPE);
          action = EXCEPTION_CONTINUE_EXECUTION;
        }
      break;

    default:
      break;
    }
  return action;
}

void main() {
        SetUnhandledExceptionFilter (_gnu_exception_handler);
}

Yann.

-----Original Message-----
From: address@hidden [mailto:address@hidden On Behalf Of grischka
Sent: Sunday, September 26, 2010 2:31 PM
To: address@hidden
Subject: Re: [Tinycc-devel] tcc and fp example

Lee wrote:
> Hi all,
> 
> I am trying to polish my C programming knowledge. Attached here is an 
> example ( slightly modified) from the MS website about FP Exceptions.
> Compiled on Devcpp (gcc 3.4.2 mingw) the result is different than tcc, 
> still not correct. Using tcc, FP exceptions are not caught. Any advice?
> (Is there a tcc-users mailing list? sorry if I am not in the right place).
> 
> Thanks, Lee

Support for SEH is completely non-present in the tinycc startup code
(crt1.c),  in particular to support the signal() from msvcrt correctly,
_except_handler3() (from msvcrt too) needs to be registered in the
SEH chain.  (Someone needs to get inspired from the startup code of
a program compiled with msvc :))

--- grischka

> 
> /* FPRESET.C: This program uses signal to set up a
>  * routine for handling floating-point errors.
>  */
> 
> #include <stdio.h>
> #include <signal.h>
> #include <setjmp.h>
> #include <stdlib.h>
> #include <float.h>
> #include <math.h>
> #include <string.h>
> 
> jmp_buf mark;              /* Address for long jump to jump to */
> int     fperr;             /* Global error number */
> 
> void __cdecl fphandler( int sig, int num );   /* Prototypes */
> void fpcheck( void );
> 
> int main( void )
> {
>    double n1, n2, r;
>    int jmpret;
>    int ch;
>   
>    /* Unmask all floating-point exceptions. */
>     _control87( 0, _MCW_EM );
>    /* Set up floating-point error handler. The compiler
>     * will generate a warning because it expects
>     * signal-handling functions to take only one argument.
>     */
> //   if( signal( SIGFPE, fphandler ) == SIG_ERR )
>       if( signal( SIGFPE, (void (__cdecl *)(int)) fphandler ) == SIG_ERR )
> 
>    {
>       fprintf( stderr, "Couldn't set SIGFPE\n" );
>       abort();   }
> 
>    /* Save stack environment for return in case of error. First
>     * time through, jmpret is 0, so true conditional is executed.
>     * If an error occurs, jmpret will be set to -1 and false
>     * conditional will be executed.
>     */
>    jmpret = setjmp( mark );
> //   printf( "\n%lf\n" , jmpret );
>    if( jmpret == 0 )
>    {
>       printf( "Test for invalid operation - enter two numbers: \n" );
>       printf( "First nr: \n");
>       scanf( "%lf", &n1 );
>       printf( "Second nr: \n");
>       scanf( "%lf", &n2 );
> 
>       r = n1 / n2;
>       /* This won't be reached if error occurs. */
>       printf( "\n\n%4.3g / %4.3g = %4.3g\n", n1, n2, r );
> 
>       r = n1 * n2;
>       /* This won't be reached if error occurs. */
>       printf( "\n\n%4.3g * %4.3g = %4.3g\n", n1, n2, r );
>    }
>    else
>       fpcheck();
>       ch = getchar();     
>       ch = getchar();
> }
> /* fphandler handles SIGFPE (floating-point error) interrupt. Note
>  * that this prototype accepts two arguments and that the
>  * prototype for signal in the run-time library expects a signal
>  * handler to have only one argument.
>  *
>  * The second argument in this signal handler allows processing of
>  * _FPE_INVALID, _FPE_OVERFLOW, _FPE_UNDERFLOW, and
>  * _FPE_ZERODIVIDE, all of which are Microsoft-specific symbols
>  * that augment the information provided by SIGFPE. The compiler
>  * will generate a warning, which is harmless and expected.
> 
>  */
> void fphandler( int sig, int num )
> {
>    /* Set global for outside check since we don't want
>     * to do I/O in the handler.
>     */
>    fperr = num;
>    printf( "Error %d::: \n", fperr );
>    /* Initialize floating-point package. */
>    _fpreset();
>    /* Restore calling environment and jump back to setjmp. Return
>     * -1 so that setjmp will return false for conditional test.
>     */
>    longjmp( mark, -1 );
> }
> void fpcheck( void )
> {
>    char fpstr[30];
>   
>    switch( fperr )
>    {
>    case _FPE_INVALID:
>        strcpy( fpstr, "Invalid number" );
>        break;
>    case _FPE_OVERFLOW:
>        strcpy( fpstr, "Overflow" );
> 
>        break;
>    case _FPE_UNDERFLOW:
>        strcpy( fpstr, "Underflow" );
>        break;
>    case _FPE_ZERODIVIDE:
>        strcpy( fpstr, "Divide by zero" );
>        break;
>    default:
>        strcpy( fpstr, "Other floating point error" );
>        break;
>    }
>    printf( "Error %d: %s\n", fperr, fpstr );
> 
> }

_______________________________________________
Tinycc-devel mailing list
address@hidden
http://lists.nongnu.org/mailman/listinfo/tinycc-devel

reply via email to

[Prev in Thread] Current Thread [Next in Thread]