1995-08-09 08:06:35 -07:00
|
|
|
/***********************************************************************/
|
|
|
|
/* */
|
1996-04-30 07:53:58 -07:00
|
|
|
/* Objective Caml */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* */
|
|
|
|
/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
|
|
|
|
/* */
|
1996-04-30 07:53:58 -07:00
|
|
|
/* Copyright 1996 Institut National de Recherche en Informatique et */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* Automatique. Distributed only by permission. */
|
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
1995-07-10 02:48:27 -07:00
|
|
|
/* Raising exceptions from C. */
|
|
|
|
|
1996-02-01 07:02:04 -08:00
|
|
|
#include <signal.h>
|
1995-07-10 02:48:27 -07:00
|
|
|
#include "alloc.h"
|
|
|
|
#include "fail.h"
|
|
|
|
#include "gc.h"
|
|
|
|
#include "memory.h"
|
|
|
|
#include "mlvalues.h"
|
|
|
|
#include "roots.h"
|
|
|
|
#include "signals.h"
|
1995-12-20 02:40:34 -08:00
|
|
|
#include "stack.h"
|
1995-07-10 02:48:27 -07:00
|
|
|
|
|
|
|
/* The globals holding predefined exceptions */
|
|
|
|
|
1995-07-18 02:45:16 -07:00
|
|
|
typedef char caml_generated_constant[256];
|
|
|
|
/* We claim these constants are big so that e.g. the Mips compiler
|
|
|
|
will not assume that they are in the .sdata section */
|
|
|
|
|
|
|
|
extern caml_generated_constant Out_of_memory, Sys_error, Failure,
|
|
|
|
Invalid_argument, End_of_file, Division_by_zero, Not_found, Match_failure;
|
1995-07-10 02:48:27 -07:00
|
|
|
|
|
|
|
/* Exception raising */
|
|
|
|
|
|
|
|
extern void raise_caml_exception P((value bucket)) Noreturn;
|
|
|
|
|
1995-12-20 02:40:34 -08:00
|
|
|
extern char * caml_exception_pointer;
|
|
|
|
|
1995-07-10 02:48:27 -07:00
|
|
|
void mlraise(v)
|
|
|
|
value v;
|
|
|
|
{
|
1996-02-05 07:28:51 -08:00
|
|
|
#ifdef POSIX_SIGNALS
|
1996-02-01 07:02:04 -08:00
|
|
|
sigset_t mask;
|
|
|
|
sigemptyset(&mask);
|
|
|
|
sigprocmask(SIG_SETMASK, &mask, NULL);
|
1996-02-05 07:28:51 -08:00
|
|
|
#else
|
1996-02-21 02:49:46 -08:00
|
|
|
#ifdef HAS_SIGSETMASK
|
1996-02-05 07:28:51 -08:00
|
|
|
sigsetmask(0);
|
1996-02-21 02:49:46 -08:00
|
|
|
#endif
|
1996-02-05 07:28:51 -08:00
|
|
|
#endif
|
|
|
|
leave_blocking_section();
|
1995-12-20 02:40:34 -08:00
|
|
|
#ifndef Stack_grows_upwards
|
|
|
|
while (local_roots != NULL &&
|
|
|
|
(char *) local_roots < caml_exception_pointer) {
|
|
|
|
#else
|
|
|
|
while (local_roots != NULL &&
|
|
|
|
(char *) local_roots > caml_exception_pointer) {
|
|
|
|
#endif
|
|
|
|
local_roots = (value *) local_roots[1];
|
|
|
|
}
|
1995-07-10 02:48:27 -07:00
|
|
|
raise_caml_exception(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
void raise_constant(tag)
|
|
|
|
value tag;
|
|
|
|
{
|
|
|
|
value bucket;
|
|
|
|
Push_roots (a, 1);
|
|
|
|
a[0] = tag;
|
|
|
|
bucket = alloc (1, 0);
|
|
|
|
Field(bucket, 0) = a[0];
|
|
|
|
Pop_roots ();
|
|
|
|
mlraise(bucket);
|
|
|
|
}
|
|
|
|
|
|
|
|
void raise_with_arg(tag, arg)
|
|
|
|
value tag;
|
|
|
|
value arg;
|
|
|
|
{
|
|
|
|
value bucket;
|
|
|
|
Push_roots (a, 2);
|
|
|
|
a[0] = tag;
|
|
|
|
a[1] = arg;
|
|
|
|
bucket = alloc (2, 0);
|
|
|
|
Field(bucket, 0) = a[0];
|
|
|
|
Field(bucket, 1) = a[1];
|
|
|
|
Pop_roots ();
|
|
|
|
mlraise(bucket);
|
|
|
|
}
|
|
|
|
|
|
|
|
void raise_with_string(tag, msg)
|
|
|
|
value tag;
|
|
|
|
char * msg;
|
|
|
|
{
|
|
|
|
raise_with_arg(tag, copy_string(msg));
|
|
|
|
}
|
|
|
|
|
|
|
|
void failwith (msg)
|
|
|
|
char * msg;
|
|
|
|
{
|
1995-07-18 02:45:16 -07:00
|
|
|
raise_with_string((value) Failure, msg);
|
1995-07-10 02:48:27 -07:00
|
|
|
}
|
|
|
|
|
1995-07-24 05:44:52 -07:00
|
|
|
/* We chose to abort the program if a C primitive raises Invalid_argument.
|
|
|
|
Rationale: nobody should trap Invalid_argument, and we're not running
|
|
|
|
under a toplevel, so this will provide the same feedback to the user.
|
|
|
|
Moreover, divisions by zero or out-of-bounds accesses also abort the
|
|
|
|
program, and there's no way we can turn them into exceptions.
|
|
|
|
Finally, this allows a number of C primitives to be declared "noalloc",
|
|
|
|
and this makes calling them much more efficient. */
|
|
|
|
|
1995-07-10 02:48:27 -07:00
|
|
|
void invalid_argument (msg)
|
|
|
|
char * msg;
|
|
|
|
{
|
1995-07-24 05:44:52 -07:00
|
|
|
fatal_error_arg("Fatal_error: Invalid_argument \"%s\"\n", msg);
|
1995-07-10 02:48:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* To raise Out_of_memory, we can't use raise_constant,
|
|
|
|
because it allocates and we're out of memory...
|
|
|
|
We therefore build the bucket by hand.
|
|
|
|
This works OK because the exception value for Out_of_memory is also
|
|
|
|
statically allocated out of the heap. */
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
header_t hdr;
|
|
|
|
value exn;
|
|
|
|
} out_of_memory_bucket;
|
|
|
|
|
|
|
|
void raise_out_of_memory()
|
|
|
|
{
|
|
|
|
out_of_memory_bucket.hdr = Make_header(1, 0, White);
|
1995-07-18 02:45:16 -07:00
|
|
|
out_of_memory_bucket.exn = (value) Out_of_memory;
|
1995-07-10 02:48:27 -07:00
|
|
|
mlraise((value) &(out_of_memory_bucket.exn));
|
|
|
|
}
|
|
|
|
|
|
|
|
void raise_sys_error(msg)
|
|
|
|
value msg;
|
|
|
|
{
|
1995-07-18 02:45:16 -07:00
|
|
|
raise_with_arg((value) Sys_error, msg);
|
1995-07-10 02:48:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void raise_end_of_file()
|
|
|
|
{
|
1995-07-18 02:45:16 -07:00
|
|
|
raise_constant((value) End_of_file);
|
1995-07-10 02:48:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void raise_zero_divide()
|
|
|
|
{
|
1995-07-18 02:45:16 -07:00
|
|
|
raise_constant((value) Division_by_zero);
|
1995-07-10 02:48:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void raise_not_found()
|
|
|
|
{
|
1995-07-18 02:45:16 -07:00
|
|
|
raise_constant((value) Not_found);
|
1995-07-10 02:48:27 -07:00
|
|
|
}
|
|
|
|
|