Skip to content

Commit 462d19f

Browse files
committed
wip
1 parent 4e85433 commit 462d19f

File tree

9 files changed

+67
-75
lines changed

9 files changed

+67
-75
lines changed

ocaml/runtime/amd64.S

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,9 @@ CFI_STARTPROC
727727
movq C_ARG_2, %rbx /* closure */
728728
movq 0(C_ARG_3), %rax /* argument */
729729
movq 0(%rbx), %r12 /* code pointer */
730+
LBL(120):
731+
cmp $1, C_ARG_4
732+
je LBL(caml_start_program_async_exn)
730733
jmp LBL(caml_start_program)
731734
CFI_ENDPROC
732735
ENDFUNCTION(G(caml_callback_asm))
@@ -741,7 +744,7 @@ CFI_STARTPROC
741744
movq 0(C_ARG_3), %rax /* first argument */
742745
movq 8(C_ARG_3), %rbx /* second argument */
743746
LEA_VAR(caml_apply2, %r12) /* code pointer */
744-
jmp LBL(caml_start_program)
747+
jmp LBL(120)
745748
CFI_ENDPROC
746749
ENDFUNCTION(G(caml_callback2_asm))
747750

@@ -756,24 +759,10 @@ CFI_STARTPROC
756759
movq C_ARG_2, %rsi /* closure */
757760
movq 16(C_ARG_3), %rdi /* third argument */
758761
LEA_VAR(caml_apply3, %r12) /* code pointer */
759-
jmp LBL(caml_start_program)
762+
jmp LBL(120)
760763
CFI_ENDPROC
761764
ENDFUNCTION(G(caml_callback3_asm))
762765

763-
/* Variant of caml_callback_asm that installs an async exn trap frame. */
764-
FUNCTION(G(caml_callback_asm_async_exn))
765-
CFI_STARTPROC
766-
/* Save callee-save registers */
767-
PUSH_CALLEE_SAVE_REGS
768-
/* Initial loading of arguments */
769-
movq C_ARG_1, %r14 /* Caml_state */
770-
movq C_ARG_2, %rbx /* closure */
771-
movq 0(C_ARG_3), %rax /* argument */
772-
movq 0(%rbx), %r12 /* code pointer */
773-
jmp LBL(caml_start_program_async_exn)
774-
CFI_ENDPROC
775-
ENDFUNCTION(G(caml_callback_asm_async_exn))
776-
777766
FUNCTION(G(caml_ml_array_bound_error))
778767
CFI_STARTPROC
779768
LEA_VAR(caml_array_bound_error, %rax)

ocaml/runtime/callback.c

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -51,26 +51,11 @@ static void init_callback_code(void)
5151
callback_code_inited = 1;
5252
}
5353

54-
static int is_async_exn(value exn)
55-
{
56-
const value *break_exn;
57-
58-
if (caml_global_data == 0) return 0;
59-
60-
if (exn == Field(caml_global_data, STACK_OVERFLOW_EXN)) return 1;
61-
62-
/* "Sys.Break" must match stdlib/sys.mlp. */
63-
break_exn = caml_named_value("Sys.Break");
64-
if (break_exn != NULL && exn == *break_exn) return 1;
65-
66-
return 0;
67-
}
68-
6954
static value caml_callbackN_exn0(value closure, int narg, value args[],
70-
int catch_async_exns)
55+
int *returning_async_exn)
7156
{
72-
int i;
73-
value res, exn;
57+
int i, returning_async_exn;
58+
value res;
7459

7560
CAMLassert(narg + 4 <= 256);
7661

@@ -83,26 +68,16 @@ static value caml_callbackN_exn0(value closure, int narg, value args[],
8368
if (!callback_code_inited) init_callback_code();
8469
callback_code[1] = narg + 3;
8570
callback_code[3] = narg;
86-
res = caml_interprete(callback_code, sizeof(callback_code));
71+
res = caml_interprete(callback_code, sizeof(callback_code),
72+
returning_async_exn);
8773
if (Is_exception_result(res)) Caml_state->extern_sp += narg + 4; /* PR#3419 */
8874

89-
if (!Is_exception_result(res)) return res;
90-
91-
exn = Extract_exception(res);
92-
93-
/* When not called from [Sys.with_async_exns], any asynchronous exceptions
94-
must be reraised here, rather than being returned as the result of one
95-
of the [caml_callback*] functions. This will cause them to arrive only
96-
at any [Sys.with_async_exns] and toplevel uncaught exception handler
97-
sites. */
98-
if (!catch_async_exns && is_async_exn(exn)) caml_raise_async(exn);
99-
10075
return res;
10176
}
10277

10378
CAMLexport value caml_callbackN_exn(value closure, int narg, value args[])
10479
{
105-
return caml_callbackN_exn0(closure, narg, args, 0);
80+
return caml_callbackN_exn0(closure, narg, args, NULL);
10681
}
10782

10883
CAMLexport value caml_callback_exn(value closure, value arg1)
@@ -117,25 +92,59 @@ CAMLexport value caml_callback2_exn(value closure, value arg1, value arg2)
11792
value arg[2];
11893
arg[0] = arg1;
11994
arg[1] = arg2;
120-
return caml_callbackN_exn(closure, 2, arg);
95+
return caml_callbackN_exn0(closure, 2, arg);
12196
}
12297

12398
CAMLexport value caml_callback3_exn(value closure,
124-
value arg1, value arg2, value arg3)
99+
value arg1, value arg2, value arg3)
125100
{
126101
value arg[3];
127102
arg[0] = arg1;
128103
arg[1] = arg2;
129104
arg[2] = arg3;
130-
return caml_callbackN_exn(closure, 3, arg);
105+
return caml_callbackN_exn0(closure, 3, arg);
131106
}
132107

133-
CAMLexport value caml_callback_async_exn(value closure, value arg1)
108+
/* Exception-propagating variants of the above */
109+
110+
CAMLexport value caml_callbackN(value closure, int narg, value args[])
111+
{
112+
value res;
113+
int returning_async_exn;
114+
115+
res = caml_callbackN_exn0(closure, narg, args, &returning_async_exn);
116+
if (Is_exception_result(res)) {
117+
value exn = Extract_exception(res);
118+
if (returning_async_exn) caml_raise_async(exn);
119+
else caml_raise(exn);
120+
}
121+
122+
return res;
123+
}
124+
125+
CAMLexport value caml_callback_exn(value closure, value arg1)
134126
{
135127
value arg[1];
136128
arg[0] = arg1;
129+
return caml_callbackN(closure, 1, arg);
130+
}
137131

138-
return caml_callbackN_exn0(closure, 1, arg, 1);
132+
CAMLexport value caml_callback2_exn(value closure, value arg1, value arg2)
133+
{
134+
value arg[2];
135+
arg[0] = arg1;
136+
arg[1] = arg2;
137+
return caml_callbackN(closure, 2, arg);
138+
}
139+
140+
CAMLexport value caml_callback3_exn(value closure,
141+
value arg1, value arg2, value arg3)
142+
{
143+
value arg[3];
144+
arg[0] = arg1;
145+
arg[1] = arg2;
146+
arg[2] = arg3;
147+
return caml_callbackN(closure, 3, arg);
139148
}
140149

141150
#else
@@ -165,7 +174,6 @@ CAMLexport value caml_callback3_exn(value closure,
165174
return caml_callback3_asm(Caml_state, closure, args);
166175
}
167176

168-
169177
CAMLexport value caml_callbackN_exn(value closure, int narg, value args[])
170178
{
171179
CAMLparam1 (closure);
@@ -197,16 +205,6 @@ CAMLexport value caml_callbackN_exn(value closure, int narg, value args[])
197205
CAMLreturn (res);
198206
}
199207

200-
extern value (caml_callback_asm_async_exn)
201-
(caml_domain_state* state, value closure, value* args);
202-
203-
CAMLexport value caml_callback_async_exn(value closure, value arg)
204-
{
205-
return caml_callback_asm_async_exn(Caml_state, closure, &arg);
206-
}
207-
208-
#endif
209-
210208
/* Exception-propagating variants of the above */
211209

212210
CAMLexport value caml_callback (value closure, value arg)
@@ -230,6 +228,8 @@ CAMLexport value caml_callbackN (value closure, int narg, value args[])
230228
return caml_raise_if_exception(caml_callbackN_exn(closure, narg, args));
231229
}
232230

231+
#endif
232+
233233
/* Naming of OCaml values */
234234

235235
struct named_value {

ocaml/runtime/caml/callback.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ CAMLextern value caml_callback3_exn (value closure,
3939
value arg1, value arg2, value arg3);
4040
CAMLextern value caml_callbackN_exn (value closure, int narg, value args[]);
4141

42-
CAMLextern value caml_callback_async_exn (value closure, value arg);
43-
4442
CAMLextern const value * caml_named_value (char const * name);
4543
typedef void (*caml_named_action) (const value*, char *);
4644
CAMLextern void caml_iterate_named_values(caml_named_action f);

ocaml/runtime/caml/interp.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
#include "mlvalues.h"
2525

2626
/* interpret a bytecode */
27-
value caml_interprete (code_t prog, asize_t prog_size);
27+
value caml_interprete (code_t prog, asize_t prog_size,
28+
int *returning_async_exn);
2829

2930
#endif /* CAML_INTERNALS */
3031

ocaml/runtime/fail_byt.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ CAMLexport value caml_check_async_exn(value res, const char *msg)
248248
CAMLprim value caml_with_async_exns(value body_callback)
249249
{
250250
value exn;
251-
value result = caml_callback_async_exn(body_callback, Val_unit);
251+
value result = caml_callback_exn(body_callback, Val_unit);
252252

253253
if (!Is_exception_result(result))
254254
return result;

ocaml/runtime/fail_nat.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ CAMLexport value caml_check_async_exn(value res, const char *msg)
260260
CAMLprim value caml_with_async_exns(value body_callback)
261261
{
262262
value exn;
263-
value result = caml_callback_async_exn(body_callback, Val_unit);
263+
value result = caml_callback_exn(body_callback, Val_unit);
264264

265265
if (!Is_exception_result(result))
266266
return result;

ocaml/runtime/interp.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ static intnat caml_bcodcount;
211211

212212
/* The interpreter itself */
213213

214-
value caml_interprete(code_t prog, asize_t prog_size)
214+
value caml_interprete(code_t prog, asize_t prog_size, int *returning_async_exn)
215215
{
216216
#ifdef PC_REG
217217
register code_t pc PC_REG;
@@ -250,6 +250,8 @@ value caml_interprete(code_t prog, asize_t prog_size)
250250
};
251251
#endif
252252

253+
if (returning_async_exn != NULL) *returning_async_exn = 0;
254+
253255
if (prog == NULL) { /* Interpreter is initializing */
254256
#ifdef THREADED_CODE
255257
caml_instr_table = (char **) jumptable;
@@ -306,6 +308,8 @@ value caml_interprete(code_t prog, asize_t prog_size)
306308
Caml_state->trapsp = (value *) ((char *) Caml_state->stack_high
307309
- initial_trapsp_offset);
308310

311+
if (returning_async_exn != NULL) *returning_async_exn = 1;
312+
309313
goto raise_notrace;
310314
}
311315
Caml_state->external_raise_async = &raise_async_buf;

ocaml/runtime/printexc.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,9 @@ static void default_fatal_uncaught_exception(value exn, const char *msg2)
122122
saved_backtrace_pos = Caml_state->backtrace_pos;
123123
Caml_state->backtrace_active = 0;
124124
at_exit = caml_named_value("Pervasives.do_at_exit");
125-
/* We use [caml_callback_async_exn] to ensure that, in the event of an
126-
asynchronous exception occurring, it still gets caught here. */
127-
if (at_exit != NULL) caml_callback_async_exn(*at_exit, Val_unit);
125+
/* In the event of an asynchronous exception occurring, it will still get
126+
caught here, because of the semantics of [caml_callback_exn]. */
127+
if (at_exit != NULL) caml_callback_exn(*at_exit, Val_unit);
128128
Caml_state->backtrace_active = saved_backtrace_active;
129129
Caml_state->backtrace_pos = saved_backtrace_pos;
130130
/* Display the uncaught exception */

ocaml/runtime/startup_byt.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ CAMLexport void caml_main(char_os **argv)
466466
#endif
467467
/* Execute the program */
468468
caml_debugger(PROGRAM_START, Val_unit);
469-
res = caml_interprete(caml_start_code, caml_code_size);
469+
res = caml_interprete(caml_start_code, caml_code_size, NULL);
470470
if (Is_exception_result(res)) {
471471
Caml_state->exn_bucket = Extract_exception(res);
472472
if (caml_debugger_in_use) {
@@ -527,7 +527,7 @@ CAMLexport value caml_startup_code_exn(
527527
caml_init_atom_table();
528528
caml_init_backtrace();
529529
/* Initialize the interpreter */
530-
caml_interprete(NULL, 0);
530+
caml_interprete(NULL, 0, NULL);
531531
/* Initialize the debugger, if needed */
532532
caml_debugger_init();
533533
/* Load the code */
@@ -557,7 +557,7 @@ CAMLexport value caml_startup_code_exn(
557557
caml_load_main_debug_info();
558558
/* Execute the program */
559559
caml_debugger(PROGRAM_START, Val_unit);
560-
return caml_interprete(caml_start_code, caml_code_size);
560+
return caml_interprete(caml_start_code, caml_code_size, NULL);
561561
}
562562

563563
CAMLexport void caml_startup_code(

0 commit comments

Comments
 (0)