Back to index

glibc  2.9
switch.c
Go to the documentation of this file.
00001 /* Copyright (C) 1991, 1992, 1997 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <signal.h>
00020 #include <68881-sw.h>
00021 
00022 
00023 /* The signal that is sent when a 68881 instruction
00024    is executed and there is no 68881.  */
00025 #ifndef       TRAPSIG
00026 #define       TRAPSIG       SIGILL
00027 #endif
00028 
00029 /* Zero if no 68881, one if we have a 68881, or -1 if we don't know yet.  */
00030 static int have_fpu = -1;
00031 
00032 
00033 /* Signal handler for the trap that happens if we don't have a 68881.  */
00034 static void
00035 trap (sig)
00036      int sig;
00037 {
00038   have_fpu = 0;
00039 }
00040 
00041 /* This function is called by functions that want to switch.
00042    The calling function must be a `struct switch_caller' in data space.
00043    It determines whether a 68881 is present, and modifies its caller
00044    to be a static jump to either the 68881 version or the soft version.
00045    It then returns into the function it has chosen to do the work.  */
00046 void
00047 __68881_switch (dummy)
00048      int dummy;
00049 {
00050   void **return_address_location = &((void **) &dummy)[-1];
00051   struct switch_caller *const caller
00052     = (struct switch_caller *) (((short int *) *return_address_location) - 1);
00053 
00054   if (have_fpu < 0)
00055     {
00056       /* Figure out whether or not we have a 68881.  */
00057       __sighandler_t handler = signal (TRAPSIG, trap);
00058       if (handler == SIG_ERR)
00059        /* We can't figure it out, so assume we don't have a 68881.
00060           This assumption will never cause us any problems other than
00061           lost performance, while the reverse assumption could cause
00062           the program to crash.  */
00063        have_fpu = 0;
00064       else
00065        {
00066          /* We set `have_fpu' to nonzero, and then execute a 68881
00067             no-op instruction.  If we have a 68881, this will do nothing.
00068             If we don't have one, this will trap and the signal handler
00069             will clear `have_fpu'.  */
00070          have_fpu = 1;
00071          asm ("fnop");
00072 
00073          /* Restore the old signal handler.  */
00074          (void) signal (TRAPSIG, handler);
00075        }
00076     }
00077 
00078   /* Modify the caller to be a jump to the appropriate address.  */
00079   caller->insn = JMP;
00080   caller->target = have_fpu ? caller->fpu : caller->soft;
00081 
00082   /* Make the address we will return to be the target we have chosen.
00083      Our return will match the `jsr' done by the caller we have
00084      just modified, and it will be just as if that had instead
00085      been a `jmp' to the new target.  */
00086   *return_address_location = caller->target;
00087 }