forked from ocaml-flambda/flambda-backend
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstartup_nat.c
207 lines (181 loc) · 6.65 KB
/
startup_nat.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
103
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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
196
197
198
199
200
201
202
203
204
205
206
207
/**************************************************************************/
/* */
/* OCaml */
/* */
/* Xavier Leroy and Damien Doligez, INRIA Rocquencourt */
/* */
/* Copyright 1996 Institut National de Recherche en Informatique et */
/* en Automatique. */
/* */
/* All rights reserved. This file is distributed under the terms of */
/* the GNU Lesser General Public License version 2.1, with the */
/* special exception on linking described in the file LICENSE. */
/* */
/**************************************************************************/
#define CAML_INTERNALS
/* Start-up code */
#include <stdio.h>
#include <stdlib.h>
#include "caml/callback.h"
#include "caml/backtrace.h"
#include "caml/custom.h"
#include "caml/codefrag.h"
#include "caml/debugger.h"
#include "caml/domain.h"
#include "caml/eventlog.h"
#include "caml/fail.h"
#include "caml/freelist.h"
#include "caml/gc.h"
#include "caml/gc_ctrl.h"
#include "caml/intext.h"
#include "caml/memory.h"
#include "caml/misc.h"
#include "caml/mlvalues.h"
#include "caml/osdeps.h"
#include "caml/printexc.h"
#include "caml/signals.h"
#include "caml/stack.h"
#include "caml/startup_aux.h"
#include "caml/sys.h"
#ifdef HAS_UI
#include "caml/ui.h"
#endif
extern int caml_parser_trace;
extern char caml_system__code_begin, caml_system__code_end;
extern uintnat caml_prelinking_in_use;
/* Initialize the atom table and the static data and code area limits. */
struct segment { char * begin; char * end; };
static void init_static(void)
{
extern struct segment caml_data_segments[], caml_code_segments[];
char * caml_code_area_start, * caml_code_area_end;
int i;
caml_init_atom_table ();
for (i = 0; caml_data_segments[i].begin != 0; i++) {
/* 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. */
if (caml_page_table_add(In_static_data,
caml_data_segments[i].begin,
caml_data_segments[i].end + sizeof(value)) != 0)
caml_fatal_error("not enough memory for initial page table");
}
if (caml_prelinking_in_use) {
/* Register each segment as a separate code fragment */
for (i = 0; caml_code_segments[i].begin != 0; i++) {
caml_register_code_fragment(caml_code_segments[i].begin,
caml_code_segments[i].end,
DIGEST_LATER, NULL);
}
} else {
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;
}
/* Register the code in the table of code fragments */
caml_register_code_fragment(caml_code_area_start,
caml_code_area_end,
DIGEST_LATER, NULL);
}
/* Also register the glue code written in assembly */
caml_register_code_fragment(&caml_system__code_begin,
&caml_system__code_end,
DIGEST_IGNORE, NULL);
}
/* These are termination hooks used by the systhreads library */
struct longjmp_buffer caml_termination_jmpbuf;
void (*caml_termination_hook)(void *) = NULL;
extern value caml_start_program (caml_domain_state*);
#ifdef _WIN32
extern void caml_win32_overflow_detection (void);
#endif
#if defined(_MSC_VER) && __STDC_SECURE_LIB__ >= 200411L
/* PR 4887: avoid crash box of windows runtime on some system calls */
extern void caml_install_invalid_parameter_handler();
#endif
value caml_startup_common(char_os **argv, int pooling)
{
char_os * exe_name, * proc_self_exe;
char tos;
value res;
/* Initialize the domain */
caml_init_domain();
/* Determine options */
#ifdef DEBUG
caml_verb_gc = 0x3F;
#endif
caml_parse_ocamlrunparam();
CAML_EVENTLOG_INIT();
#ifdef DEBUG
caml_gc_message (-1, "### OCaml runtime: debug mode ###\n");
#endif
if (caml_cleanup_on_exit)
pooling = 1;
if (!caml_startup_aux(pooling))
return Val_unit;
caml_init_frame_descriptors();
caml_init_locale();
#if defined(_MSC_VER) && __STDC_SECURE_LIB__ >= 200411L
caml_install_invalid_parameter_handler();
#endif
caml_init_custom_operations();
Caml_state->top_of_stack = &tos;
caml_init_gc (caml_init_minor_heap_wsz, caml_init_heap_wsz,
caml_init_heap_chunk_sz, caml_init_percent_free,
caml_init_max_percent_free, caml_init_major_window,
caml_init_custom_major_ratio, caml_init_custom_minor_ratio,
caml_init_custom_minor_max_bsz, caml_init_policy);
init_static();
caml_init_signals();
#ifdef _WIN32
caml_win32_overflow_detection();
#endif
caml_init_backtrace();
caml_debugger_init (); /* force debugger.o stub to be linked */
exe_name = argv[0];
if (exe_name == NULL) exe_name = T("");
proc_self_exe = caml_executable_name();
if (proc_self_exe != NULL)
exe_name = proc_self_exe;
else
exe_name = caml_search_exe_in_path(exe_name);
caml_sys_init(exe_name, argv);
if (sigsetjmp(caml_termination_jmpbuf.buf, 0)) {
caml_terminate_signals();
if (caml_termination_hook != NULL) caml_termination_hook(NULL);
return Val_unit;
}
res = caml_start_program(Caml_state);
/* ignore distinction between async and normal,
it's an uncaught exception either way */
Caml_state->raising_async_exn = 0;
caml_terminate_signals();
return res;
}
value caml_startup_exn(char_os **argv)
{
return caml_startup_common(argv, /* pooling */ 0);
}
void caml_startup(char_os **argv)
{
value res = caml_startup_exn(argv);
if (Is_exception_result(res))
caml_fatal_uncaught_exception(Extract_exception(res));
}
void caml_main(char_os **argv)
{
caml_startup(argv);
}
value caml_startup_pooled_exn(char_os **argv)
{
return caml_startup_common(argv, /* pooling */ 1);
}
void caml_startup_pooled(char_os **argv)
{
value res = caml_startup_pooled_exn(argv);
if (Is_exception_result(res))
caml_fatal_uncaught_exception(Extract_exception(res));
}