Skip to content

Commit

Permalink
callback.c: register the bytecode fragment used for callbacks
Browse files Browse the repository at this point in the history
So that the return address pushed on the bytecode interpreter stack
is correctly recognized as a code pointer.
  • Loading branch information
xavierleroy committed Jun 14, 2020
1 parent 82be456 commit 9aff4e6
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions runtime/callback.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

/* Bytecode callbacks */

#include "caml/codefrag.h"
#include "caml/interp.h"
#include "caml/instruct.h"
#include "caml/fix_code.h"
Expand All @@ -37,25 +38,20 @@ CAMLexport int caml_callback_depth = 0;

#ifndef LOCAL_CALLBACK_BYTECODE
static opcode_t callback_code[] = { ACC, 0, APPLY, 0, POP, 1, STOP };
#endif


#ifdef THREADED_CODE
static int callback_code_inited = 0;

static int callback_code_threaded = 0;

static void thread_callback(void)
static void init_callback_code(void)
{
caml_register_code_fragment((char *) callback_code,
(char *) callback_code + sizeof(callback_code),
DIGEST_IGNORE, NULL);
#ifdef THREADED_CODE
caml_thread_code(callback_code, sizeof(callback_code));
callback_code_threaded = 1;
#endif
callback_code_inited = 1;
}

#define Init_callback() if (!callback_code_threaded) thread_callback()

#else

#define Init_callback()

#endif

CAMLexport value caml_callbackN_exn(value closure, int narg, value args[])
Expand All @@ -79,7 +75,7 @@ CAMLexport value caml_callbackN_exn(value closure, int narg, value args[])
Caml_state->extern_sp[narg + 1] = Val_unit; /* environment */
Caml_state->extern_sp[narg + 2] = Val_long(0); /* extra args */
Caml_state->extern_sp[narg + 3] = closure;
Init_callback();
if (!callback_code_inited) init_callback_code();
callback_code[1] = narg + 3;
callback_code[3] = narg;
res = caml_interprete(callback_code, sizeof(callback_code));
Expand All @@ -96,6 +92,8 @@ CAMLexport value caml_callbackN_exn(value closure, int narg, value args[])
local_callback_code[4] = POP;
local_callback_code[5] = 1;
local_callback_code[6] = STOP;
/* Not registering the code fragment, as code fragment management
would need to be revised thoroughly for an hypothetical JIT */
#ifdef THREADED_CODE
caml_thread_code(local_callback_code, sizeof(local_callback_code));
#endif /*THREADED_CODE*/
Expand Down

0 comments on commit 9aff4e6

Please sign in to comment.