exception.cc 4.73 KB
Newer Older
Tom Tromey committed
1 2
// Functions for Exception Support for Java.

3
/* Copyright (C) 1998, 1999  Free Software Foundation
Tom Tromey committed
4 5 6 7 8 9 10 11 12 13

   This file is part of libgcj.

This software is copyrighted work licensed under the terms of the
Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
details.  */

#include <config.h>

#include <stddef.h>
14
#include <stdlib.h>
Tom Tromey committed
15 16 17

#include <java/lang/Class.h>
#include <java/lang/NullPointerException.h>
Tom Tromey committed
18
#include <gcj/cni.h>
Tom Tromey committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
#include <jvm.h>

// eh-common.h needs gansidecl.h.
#include "gansidecl.h"
#include "eh-common.h"

typedef struct {
  __eh_info eh_info;
  void *value;
} java_eh_info;


/* Language-specific EH info pointer, throw routine, and language/version
   info routines. All defined in libgcc2. */

extern "C" java_eh_info **__get_eh_info (); 
extern "C" void __throw () __attribute__ ((__noreturn__));
Tom Tromey committed
36
extern "C" void __sjthrow () __attribute__ ((__noreturn__));
Tom Tromey committed
37 38
extern "C" short __get_eh_table_version (void *table);
extern "C" short __get_eh_table_language (void *table);
Tom Tromey committed
39
extern "C" void *__get_eh_context ();
Tom Tromey committed
40 41 42 43 44

extern "C" void *
_Jv_type_matcher (java_eh_info *info, void* match_info, 
		  void *exception_table)
{
Tom Tromey committed
45 46 47 48
#ifndef SJLJ_EXCEPTIONS
  /* No exception table implies the old style mechanism, so don't check. */
  if (exception_table != NULL
      && __get_eh_table_language (exception_table) != EH_LANG_Java)
Tom Tromey committed
49
    return NULL;
Tom Tromey committed
50
#endif
Tom Tromey committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102

  /* we don't worry about version info yet, there is only one version! */
  
  if (match_info != NULL)
    {
      // The match_info is either a (java::lang::Class*) or
      // match_info is one more than a (Utf8Const*).
      if (sizeof(void*) != sizeof(size_t))
	abort();
      size_t mi = (size_t) match_info;
      if ((mi & 1) != 0)
	match_info = _Jv_FindClass ((Utf8Const*) (mi - 1), NULL);
      if (! _Jv_IsInstanceOf ((jobject) info->value, (jclass) match_info))
	return NULL;
    }

  return info->value;
}

/* Compiler hook to return a pointer to java exception object. The value
   is cleared, so if the exception needs to be rethrown, it should be set 
   again */

extern "C" void *
_Jv_exception_info (void)
{
  java_eh_info *info = *(__get_eh_info ());
  void *ptr;

  if (info == NULL)
    abort ();

  ptr = info->value;

  /* clear the value so another throw is an error */
  info->value = NULL;

  return ptr;
}



/* Allocate an exception info structure for java. Called the first time
   an exception is thrown. */

extern "C" void
_Jv_eh_alloc ()
{
  /* FIXME: we should use _Jv_AllocBytes here.  However, libgcc2
     apparently can sometimes free() this value itself.  */
  java_eh_info *p = (java_eh_info *) malloc (sizeof (java_eh_info));
  if (p == 0)
103
    abort ();
Tom Tromey committed
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135

  p->value = 0;
  java_eh_info ** info_ptr = __get_eh_info ();

  /* There should NOT be an exception info pointer already. */
  if (*info_ptr != NULL)
    abort ();

  *info_ptr = p;
}

/* Deallocate the current exception info structure. Called at shutdown time. */

extern "C" void
_Jv_eh_free ()
{
  java_eh_info ** info_ptr = __get_eh_info ();
  if (*info_ptr == NULL)
    abort ();
  
  /* FIXME: ideally we should just let the GC handle this.  */
  free (*info_ptr);
  *info_ptr = NULL;
}

/* Initialize an __eh_info structure with this libraries matching info. */

extern "C" void
_Jv_setup_eh_info (__eh_info *)
{
}

Tom Tromey committed
136 137 138
/* Perform a throw, Java style. Throw will unwind through this call,
   so there better not be any handlers or exception thrown here. */

Tom Tromey committed
139 140 141 142 143 144 145 146 147 148 149
extern "C" void
_Jv_Throw (void *value)
{
  if (value == NULL)
    value = (void *) new java::lang::NullPointerException ();
  java_eh_info *ehinfo = *(__get_eh_info ());
  if (ehinfo == NULL)
    {
      _Jv_eh_alloc ();
      ehinfo = *(__get_eh_info ());
    }
150
  ehinfo->eh_info.match_function = (__eh_matcher) _Jv_type_matcher;
Tom Tromey committed
151 152 153
  ehinfo->eh_info.language = EH_LANG_Java;
  ehinfo->eh_info.version = 1;
  ehinfo->value = value;
Tom Tromey committed
154 155 156 157 158 159 160 161

/* We're happy with setjmp/longjmp exceptions or region-based
   exception handlers: entry points are provided here for both.  */
#ifdef SJLJ_EXCEPTIONS
  __sjthrow ();
#else
  __throw ();
#endif
Tom Tromey committed
162
}
Tom Tromey committed
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195

#ifdef USE_WIN32_SIGNALLING

// This is a mangled version of _Jv_Throw and __sjthrow except
// rather than calling longjmp, it returns a pointer to the jmp buffer

extern "C" int *
win32_get_restart_frame (void *value)
{
  struct eh_context *eh = (struct eh_context *)__get_eh_context ();
  void ***dhc = &eh->dynamic_handler_chain;
 
  java_eh_info *ehinfo = *(__get_eh_info ());
  if (ehinfo == NULL)
    {
      _Jv_eh_alloc ();
      ehinfo = *(__get_eh_info ());
    }
  ehinfo->eh_info.match_function = (__eh_matcher) _Jv_type_matcher;
  ehinfo->eh_info.language = EH_LANG_Java;
  ehinfo->eh_info.version = 1;
  ehinfo->value = value;

  // FIXME: Run clean ups?

  int *jmpbuf = (int*)&(*dhc)[2];

  *dhc = (void**)(*dhc)[0];

  return  jmpbuf;
}

#endif /* USE_WIN32_SIGNALLING */