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 */
|
1999-11-17 10:59:06 -08:00
|
|
|
/* en Automatique. All rights reserved. This file is distributed */
|
|
|
|
/* under the terms of the GNU Library General Public License. */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
|
|
|
/* $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"
|
1997-08-29 08:37:22 -07:00
|
|
|
#include "io.h"
|
1995-07-10 02:48:27 -07:00
|
|
|
#include "gc.h"
|
|
|
|
#include "memory.h"
|
|
|
|
#include "mlvalues.h"
|
|
|
|
#include "signals.h"
|
1995-12-20 02:40:34 -08:00
|
|
|
#include "stack.h"
|
1997-11-21 05:46:23 -08:00
|
|
|
#include "roots.h"
|
1995-07-10 02:48:27 -07:00
|
|
|
|
|
|
|
/* The globals holding predefined exceptions */
|
|
|
|
|
1998-11-18 10:10:53 -08:00
|
|
|
typedef value caml_generated_constant[1];
|
1995-07-18 02:45:16 -07:00
|
|
|
|
|
|
|
extern caml_generated_constant Out_of_memory, Sys_error, Failure,
|
1998-11-20 07:38:09 -08:00
|
|
|
Invalid_argument, End_of_file, Division_by_zero, Not_found,
|
|
|
|
Match_failure, Sys_blocked_io;
|
1995-07-10 02:48:27 -07:00
|
|
|
|
1998-11-02 07:08:40 -08:00
|
|
|
/* Exception raising */
|
|
|
|
|
|
|
|
extern void raise_caml_exception (value bucket) Noreturn;
|
|
|
|
|
|
|
|
char * caml_exception_pointer = NULL;
|
|
|
|
|
|
|
|
void mlraise(value v)
|
|
|
|
{
|
1997-08-29 08:37:22 -07:00
|
|
|
Unlock_exn();
|
1997-03-17 02:17:32 -08:00
|
|
|
if (caml_exception_pointer == NULL) fatal_uncaught_exception(v);
|
1997-05-26 10:16:31 -07:00
|
|
|
|
1995-12-20 02:40:34 -08:00
|
|
|
#ifndef Stack_grows_upwards
|
1997-05-26 10:16:31 -07:00
|
|
|
#define PUSHED_AFTER <
|
1995-12-20 02:40:34 -08:00
|
|
|
#else
|
1997-05-26 10:16:31 -07:00
|
|
|
#define PUSHED_AFTER >
|
1995-12-20 02:40:34 -08:00
|
|
|
#endif
|
1997-05-26 10:16:31 -07:00
|
|
|
while (local_roots != NULL &&
|
|
|
|
(char *) local_roots PUSHED_AFTER caml_exception_pointer) {
|
1997-06-01 10:15:19 -07:00
|
|
|
local_roots = local_roots->next;
|
1997-05-26 10:16:31 -07:00
|
|
|
}
|
|
|
|
#undef PUSHED_AFTER
|
|
|
|
|
1995-07-10 02:48:27 -07:00
|
|
|
raise_caml_exception(v);
|
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
void raise_constant(value tag)
|
1995-07-10 02:48:27 -07:00
|
|
|
{
|
|
|
|
value bucket;
|
1997-05-26 10:16:31 -07:00
|
|
|
Begin_root (tag);
|
1998-10-26 11:19:32 -08:00
|
|
|
bucket = alloc_small (1, 0);
|
1997-05-26 10:16:31 -07:00
|
|
|
Field(bucket, 0) = tag;
|
|
|
|
End_roots ();
|
1995-07-10 02:48:27 -07:00
|
|
|
mlraise(bucket);
|
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
void raise_with_arg(value tag, value arg)
|
1995-07-10 02:48:27 -07:00
|
|
|
{
|
|
|
|
value bucket;
|
1997-05-26 10:16:31 -07:00
|
|
|
Begin_roots2 (tag, arg);
|
1998-10-26 11:19:32 -08:00
|
|
|
bucket = alloc_small (2, 0);
|
1997-05-26 10:16:31 -07:00
|
|
|
Field(bucket, 0) = tag;
|
|
|
|
Field(bucket, 1) = arg;
|
|
|
|
End_roots ();
|
1995-07-10 02:48:27 -07:00
|
|
|
mlraise(bucket);
|
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
void raise_with_string(value tag, char *msg)
|
1995-07-10 02:48:27 -07:00
|
|
|
{
|
|
|
|
raise_with_arg(tag, copy_string(msg));
|
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
void failwith (char *msg)
|
1995-07-10 02:48:27 -07:00
|
|
|
{
|
1995-07-18 02:45:16 -07:00
|
|
|
raise_with_string((value) Failure, msg);
|
1995-07-10 02:48:27 -07:00
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
void invalid_argument (char *msg)
|
1995-07-10 02:48:27 -07:00
|
|
|
{
|
1998-11-11 07:35:48 -08:00
|
|
|
raise_with_string((value) Invalid_argument, 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;
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
void raise_out_of_memory(void)
|
1995-07-10 02:48:27 -07:00
|
|
|
{
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
void raise_sys_error(value msg)
|
1995-07-10 02:48:27 -07:00
|
|
|
{
|
1995-07-18 02:45:16 -07:00
|
|
|
raise_with_arg((value) Sys_error, msg);
|
1995-07-10 02:48:27 -07:00
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
void raise_end_of_file(void)
|
1995-07-10 02:48:27 -07:00
|
|
|
{
|
1995-07-18 02:45:16 -07:00
|
|
|
raise_constant((value) End_of_file);
|
1995-07-10 02:48:27 -07:00
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
void raise_zero_divide(void)
|
1995-07-10 02:48:27 -07:00
|
|
|
{
|
1995-07-18 02:45:16 -07:00
|
|
|
raise_constant((value) Division_by_zero);
|
1995-07-10 02:48:27 -07:00
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
void raise_not_found(void)
|
1995-07-10 02:48:27 -07:00
|
|
|
{
|
1995-07-18 02:45:16 -07:00
|
|
|
raise_constant((value) Not_found);
|
1995-07-10 02:48:27 -07:00
|
|
|
}
|
1998-11-11 07:35:48 -08:00
|
|
|
|
1998-11-20 07:38:09 -08:00
|
|
|
void raise_sys_blocked_io(void)
|
|
|
|
{
|
1998-11-26 02:00:51 -08:00
|
|
|
raise_constant((value) Sys_blocked_io);
|
1998-11-20 07:38:09 -08:00
|
|
|
}
|
|
|
|
|
1998-11-11 07:35:48 -08:00
|
|
|
/* We allocate statically the bucket for the exception because we can't
|
|
|
|
do a GC before the exception is raised (lack of stack descriptors
|
|
|
|
for the ccall to array_bound_error */
|
|
|
|
|
|
|
|
#define BOUND_MSG "out-of-bound array or string access"
|
|
|
|
#define BOUND_MSG_LEN (sizeof(BOUND_MSG) - 1)
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
header_t hdr;
|
|
|
|
value exn;
|
|
|
|
value arg;
|
|
|
|
} array_bound_error_bucket;
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
header_t hdr;
|
|
|
|
char data[BOUND_MSG_LEN + sizeof(value)];
|
|
|
|
} array_bound_error_msg = { 0, BOUND_MSG };
|
|
|
|
|
|
|
|
void array_bound_error(void)
|
|
|
|
{
|
|
|
|
mlsize_t wosize = (BOUND_MSG_LEN + sizeof(value)) / sizeof(value);
|
|
|
|
mlsize_t offset_index = Bsize_wsize(wosize) - 1;
|
|
|
|
array_bound_error_msg.hdr = Make_header(wosize, String_tag, White);
|
|
|
|
array_bound_error_msg.data[offset_index] = offset_index - BOUND_MSG_LEN;
|
|
|
|
array_bound_error_bucket.hdr = Make_header(2, 0, White);
|
|
|
|
array_bound_error_bucket.exn = (value) Invalid_argument;
|
|
|
|
array_bound_error_bucket.arg = (value) array_bound_error_msg.data;
|
|
|
|
mlraise((value) &array_bound_error_bucket.exn);
|
|
|
|
}
|