1995-08-09 08:06:35 -07:00
|
|
|
/***********************************************************************/
|
|
|
|
/* */
|
2011-07-27 07:17:02 -07:00
|
|
|
/* OCaml */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* */
|
|
|
|
/* Xavier Leroy and Damien Doligez, 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 */
|
2001-12-07 05:41:02 -08:00
|
|
|
/* under the terms of the GNU Library General Public License, with */
|
|
|
|
/* the special exception on linking described in file ../LICENSE. */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
1995-07-10 02:48:27 -07:00
|
|
|
/* Start-up code */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
1999-02-14 08:48:25 -08:00
|
|
|
#include "callback.h"
|
2007-01-29 04:11:18 -08:00
|
|
|
#include "backtrace.h"
|
2000-02-11 07:09:27 -08:00
|
|
|
#include "custom.h"
|
2012-02-05 01:56:23 -08:00
|
|
|
#include "debugger.h"
|
1996-11-07 05:12:16 -08:00
|
|
|
#include "fail.h"
|
2008-12-03 10:09:09 -08:00
|
|
|
#include "freelist.h"
|
1995-07-10 02:48:27 -07:00
|
|
|
#include "gc.h"
|
|
|
|
#include "gc_ctrl.h"
|
2012-03-13 07:50:41 -07:00
|
|
|
#include "intext.h"
|
2008-01-03 01:37:10 -08:00
|
|
|
#include "memory.h"
|
1995-07-10 02:48:27 -07:00
|
|
|
#include "misc.h"
|
|
|
|
#include "mlvalues.h"
|
2002-09-03 06:56:36 -07:00
|
|
|
#include "osdeps.h"
|
2001-06-15 07:22:49 -07:00
|
|
|
#include "printexc.h"
|
2010-04-27 00:55:08 -07:00
|
|
|
#include "stack.h"
|
1995-07-10 02:48:27 -07:00
|
|
|
#include "sys.h"
|
1996-11-07 05:12:16 -08:00
|
|
|
#ifdef HAS_UI
|
|
|
|
#include "ui.h"
|
|
|
|
#endif
|
1995-07-10 02:48:27 -07:00
|
|
|
|
2004-01-01 08:42:43 -08:00
|
|
|
extern int caml_parser_trace;
|
2007-11-06 07:16:56 -08:00
|
|
|
CAMLexport header_t caml_atom_table[256];
|
2004-01-02 11:23:29 -08:00
|
|
|
char * caml_code_area_start, * caml_code_area_end;
|
1995-07-10 02:48:27 -07:00
|
|
|
|
1997-07-02 11:16:15 -07:00
|
|
|
/* Initialize the atom table and the static data and code area limits. */
|
1996-11-07 02:55:49 -08:00
|
|
|
|
1997-07-02 11:16:15 -07:00
|
|
|
struct segment { char * begin; char * end; };
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
static void init_atoms(void)
|
1995-07-10 02:48:27 -07:00
|
|
|
{
|
1997-07-02 11:16:15 -07:00
|
|
|
extern struct segment caml_data_segments[], caml_code_segments[];
|
2008-01-03 01:37:10 -08:00
|
|
|
int i;
|
2012-03-13 07:50:41 -07:00
|
|
|
struct code_fragment * cf;
|
1996-02-20 02:59:35 -08:00
|
|
|
|
2008-01-03 01:37:10 -08:00
|
|
|
for (i = 0; i < 256; i++) {
|
|
|
|
caml_atom_table[i] = Make_header(0, i, Caml_white);
|
|
|
|
}
|
|
|
|
if (caml_page_table_add(In_static_data,
|
|
|
|
caml_atom_table, caml_atom_table + 256) != 0)
|
2013-03-09 14:38:52 -08:00
|
|
|
caml_fatal_error("Fatal error: not enough memory for initial page table");
|
2008-01-03 01:37:10 -08:00
|
|
|
|
|
|
|
for (i = 0; caml_data_segments[i].begin != 0; i++) {
|
2012-02-18 08:56:29 -08:00
|
|
|
/* PR#5509: we must include the zero word at end of data segment,
|
|
|
|
because pointers equal to caml_data_segments[i].end are static data. */
|
2008-12-03 10:09:09 -08:00
|
|
|
if (caml_page_table_add(In_static_data,
|
2008-01-03 01:37:10 -08:00
|
|
|
caml_data_segments[i].begin,
|
2012-02-18 08:56:29 -08:00
|
|
|
caml_data_segments[i].end + sizeof(value)) != 0)
|
2013-03-09 14:38:52 -08:00
|
|
|
caml_fatal_error("Fatal error: not enough memory for initial page table");
|
2008-01-03 01:37:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
caml_code_area_start = caml_code_segments[0].begin;
|
|
|
|
caml_code_area_end = caml_code_segments[0].end;
|
|
|
|
for (i = 1; caml_code_segments[i].begin != 0; i++) {
|
|
|
|
if (caml_code_segments[i].begin < caml_code_area_start)
|
|
|
|
caml_code_area_start = caml_code_segments[i].begin;
|
|
|
|
if (caml_code_segments[i].end > caml_code_area_end)
|
|
|
|
caml_code_area_end = caml_code_segments[i].end;
|
|
|
|
}
|
2012-03-13 07:50:41 -07:00
|
|
|
/* Register the code in the table of code fragments */
|
|
|
|
cf = caml_stat_alloc(sizeof(struct code_fragment));
|
|
|
|
cf->code_start = caml_code_area_start;
|
|
|
|
cf->code_end = caml_code_area_end;
|
|
|
|
cf->digest_computed = 0;
|
|
|
|
caml_ext_table_init(&caml_code_fragments_table, 8);
|
|
|
|
caml_ext_table_add(&caml_code_fragments_table, cf);
|
1995-07-10 02:48:27 -07:00
|
|
|
}
|
|
|
|
|
1997-05-13 07:45:38 -07:00
|
|
|
/* Configuration parameters and flags */
|
1995-07-10 02:48:27 -07:00
|
|
|
|
2005-09-22 07:21:50 -07:00
|
|
|
static uintnat percent_free_init = Percent_free_def;
|
|
|
|
static uintnat max_percent_free_init = Max_percent_free_def;
|
|
|
|
static uintnat minor_heap_init = Minor_heap_def;
|
|
|
|
static uintnat heap_chunk_init = Heap_chunk_def;
|
|
|
|
static uintnat heap_size_init = Init_heap_def;
|
|
|
|
static uintnat max_stack_init = Max_stack_def;
|
1995-07-10 02:48:27 -07:00
|
|
|
|
1996-11-07 02:55:49 -08:00
|
|
|
/* Parse the CAMLRUNPARAM variable */
|
|
|
|
/* The option letter for each runtime option is the first letter of the
|
1997-05-13 07:45:38 -07:00
|
|
|
last word of the ML name of the option (see [stdlib/gc.mli]).
|
|
|
|
Except for l (maximum stack size) and h (initial heap size).
|
|
|
|
*/
|
|
|
|
/* Note: option l is irrelevant to the native-code runtime. */
|
|
|
|
|
2002-06-05 05:26:08 -07:00
|
|
|
/* If you change these functions, see also their copy in byterun/startup.c */
|
|
|
|
|
2005-09-22 07:21:50 -07:00
|
|
|
static void scanmult (char *opt, uintnat *var)
|
1997-05-13 07:45:38 -07:00
|
|
|
{
|
|
|
|
char mult = ' ';
|
2005-09-22 07:21:50 -07:00
|
|
|
int val;
|
|
|
|
sscanf (opt, "=%u%c", &val, &mult);
|
|
|
|
sscanf (opt, "=0x%x%c", &val, &mult);
|
|
|
|
switch (mult) {
|
|
|
|
case 'k': *var = (uintnat) val * 1024; break;
|
|
|
|
case 'M': *var = (uintnat) val * 1024 * 1024; break;
|
|
|
|
case 'G': *var = (uintnat) val * 1024 * 1024 * 1024; break;
|
|
|
|
default: *var = (uintnat) val; break;
|
|
|
|
}
|
1997-05-13 07:45:38 -07:00
|
|
|
}
|
1996-11-07 02:55:49 -08:00
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
static void parse_camlrunparam(void)
|
1996-11-07 02:55:49 -08:00
|
|
|
{
|
1999-11-18 09:57:48 -08:00
|
|
|
char *opt = getenv ("OCAMLRUNPARAM");
|
2008-12-03 10:09:09 -08:00
|
|
|
uintnat p;
|
1999-11-18 09:57:48 -08:00
|
|
|
|
|
|
|
if (opt == NULL) opt = getenv ("CAMLRUNPARAM");
|
|
|
|
|
1995-07-10 02:48:27 -07:00
|
|
|
if (opt != NULL){
|
|
|
|
while (*opt != '\0'){
|
|
|
|
switch (*opt++){
|
1997-05-19 08:42:21 -07:00
|
|
|
case 's': scanmult (opt, &minor_heap_init); break;
|
1997-05-13 07:45:38 -07:00
|
|
|
case 'i': scanmult (opt, &heap_chunk_init); break;
|
|
|
|
case 'h': scanmult (opt, &heap_size_init); break;
|
|
|
|
case 'l': scanmult (opt, &max_stack_init); break;
|
1997-05-21 08:26:09 -07:00
|
|
|
case 'o': scanmult (opt, &percent_free_init); break;
|
|
|
|
case 'O': scanmult (opt, &max_percent_free_init); break;
|
2003-12-29 14:15:02 -08:00
|
|
|
case 'v': scanmult (opt, &caml_verb_gc); break;
|
2008-03-14 06:47:24 -07:00
|
|
|
case 'b': caml_record_backtrace(Val_true); break;
|
2004-01-01 08:42:43 -08:00
|
|
|
case 'p': caml_parser_trace = 1; break;
|
2008-12-03 10:09:09 -08:00
|
|
|
case 'a': scanmult (opt, &p); caml_set_allocation_policy (p); break;
|
1995-07-10 02:48:27 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1996-11-07 02:55:49 -08:00
|
|
|
}
|
|
|
|
|
2003-06-16 05:31:14 -07:00
|
|
|
/* These are termination hooks used by the systhreads library */
|
|
|
|
struct longjmp_buffer caml_termination_jmpbuf;
|
|
|
|
void (*caml_termination_hook)(void *) = NULL;
|
|
|
|
|
1999-02-14 08:48:25 -08:00
|
|
|
extern value caml_start_program (void);
|
2004-01-02 11:23:29 -08:00
|
|
|
extern void caml_init_ieee_floats (void);
|
2004-01-01 08:42:43 -08:00
|
|
|
extern void caml_init_signals (void);
|
1996-11-07 02:55:49 -08:00
|
|
|
|
2013-01-17 01:04:53 -08:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
|
|
|
|
/* PR 4887: avoid crash box of windows runtime on some system calls */
|
|
|
|
extern void caml_install_invalid_parameter_handler();
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
void caml_main(char **argv)
|
1996-11-07 02:55:49 -08:00
|
|
|
{
|
2002-02-11 05:51:40 -08:00
|
|
|
char * exe_name;
|
|
|
|
static char proc_self_exe[256];
|
1999-02-14 08:48:25 -08:00
|
|
|
value res;
|
2010-04-27 00:55:08 -07:00
|
|
|
char tos;
|
1999-02-14 08:48:25 -08:00
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
caml_init_ieee_floats();
|
2013-01-17 01:04:53 -08:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
caml_install_invalid_parameter_handler();
|
|
|
|
#endif
|
2004-01-01 08:42:43 -08:00
|
|
|
caml_init_custom_operations();
|
1996-11-07 02:55:49 -08:00
|
|
|
#ifdef DEBUG
|
2003-12-29 14:15:02 -08:00
|
|
|
caml_verb_gc = 63;
|
1996-11-07 02:55:49 -08:00
|
|
|
#endif
|
2010-04-27 00:55:08 -07:00
|
|
|
caml_top_of_stack = &tos;
|
1996-11-07 02:55:49 -08:00
|
|
|
parse_camlrunparam();
|
2004-01-02 11:23:29 -08:00
|
|
|
caml_init_gc (minor_heap_init, heap_size_init, heap_chunk_init,
|
|
|
|
percent_free_init, max_percent_free_init);
|
1995-07-10 02:48:27 -07:00
|
|
|
init_atoms();
|
2004-01-01 08:42:43 -08:00
|
|
|
caml_init_signals();
|
2010-04-22 08:41:16 -07:00
|
|
|
caml_debugger_init (); /* force debugger.o stub to be linked */
|
2002-02-11 05:51:40 -08:00
|
|
|
exe_name = argv[0];
|
2005-03-24 09:20:54 -08:00
|
|
|
if (exe_name == NULL) exe_name = "";
|
2004-01-01 10:32:36 -08:00
|
|
|
if (caml_executable_name(proc_self_exe, sizeof(proc_self_exe)) == 0)
|
2002-02-11 05:51:40 -08:00
|
|
|
exe_name = proc_self_exe;
|
2005-03-24 09:20:54 -08:00
|
|
|
else
|
|
|
|
exe_name = caml_search_exe_in_path(exe_name);
|
2003-12-16 10:09:44 -08:00
|
|
|
caml_sys_init(exe_name, argv);
|
2003-06-16 05:31:14 -07:00
|
|
|
if (sigsetjmp(caml_termination_jmpbuf.buf, 0)) {
|
|
|
|
if (caml_termination_hook != NULL) caml_termination_hook(NULL);
|
|
|
|
return;
|
|
|
|
}
|
1999-02-14 08:48:25 -08:00
|
|
|
res = caml_start_program();
|
|
|
|
if (Is_exception_result(res))
|
2004-01-01 08:42:43 -08:00
|
|
|
caml_fatal_uncaught_exception(Extract_exception(res));
|
1995-07-10 02:48:27 -07:00
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
void caml_startup(char **argv)
|
1996-11-07 02:55:49 -08:00
|
|
|
{
|
|
|
|
caml_main(argv);
|
|
|
|
}
|