| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
#include <stdio.h> |
| 12 |
#include <string.h> |
| 13 |
|
| 14 |
#define CAML_INTERNALS |
| 15 |
#include <caml/alloc.h> |
| 16 |
#include <caml/address_class.h> |
| 17 |
#include <caml/roots.h> |
| 18 |
#include <caml/version.h> |
| 19 |
|
| 20 |
#if OCAML_VERSION >= 50000 |
| 21 |
#include <caml/shared_heap.h> |
| 22 |
#endif |
| 23 |
|
| 24 |
#include "rocq_instruct.h" |
| 25 |
#include "rocq_fix_code.h" |
| 26 |
#include "rocq_memory.h" |
| 27 |
#include "rocq_interp.h" |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
value * rocq_stack_low; |
| 32 |
value * rocq_stack_high; |
| 33 |
value * rocq_stack_threshold; |
| 34 |
asize_t rocq_max_stack_size = Rocq_max_stack_size; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
long rocq_saved_sp_offset; |
| 39 |
value * rocq_sp; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
void rocq_stat_free (void * blk) |
| 44 |
{ |
| 45 |
free (blk); |
| 46 |
} |
| 47 |
|
| 48 |
value rocq_static_alloc(value size) |
| 49 |
{ |
| 50 |
return (value) rocq_stat_alloc((asize_t) Long_val(size)); |
| 51 |
} |
| 52 |
|
| 53 |
#if OCAML_VERSION < 50000 |
| 54 |
static void (*rocq_prev_scan_roots_hook) (scanning_action); |
| 55 |
|
| 56 |
static void rocq_scan_roots(scanning_action action) |
| 57 |
{ |
| 58 |
register value * i; |
| 59 |
|
| 60 |
for (i = rocq_sp; i < rocq_stack_high; i++) { |
| 61 |
if (!Is_block(*i)) continue; |
| 62 |
(*action) (*i, i); |
| 63 |
}; |
| 64 |
|
| 65 |
if (rocq_prev_scan_roots_hook != NULL) (*rocq_prev_scan_roots_hook)(action); |
| 66 |
} |
| 67 |
#else |
| 68 |
static void (*rocq_prev_scan_roots_hook) (scanning_action, scanning_action_flags, void *, caml_domain_state *); |
| 69 |
|
| 70 |
static void rocq_scan_roots(scanning_action action, scanning_action_flags flags, void *ctx, caml_domain_state *state) |
| 71 |
{ |
| 72 |
register value * i; |
| 73 |
|
| 74 |
for (i = rocq_sp; i < rocq_stack_high; i++) { |
| 75 |
if (!Is_block(*i)) continue; |
| 76 |
(*action) (ctx, *i, i); |
| 77 |
}; |
| 78 |
|
| 79 |
if (rocq_prev_scan_roots_hook != NULL) (*rocq_prev_scan_roots_hook)(action, flags, ctx, state); |
| 80 |
} |
| 81 |
#endif |
| 82 |
|
| 83 |
static int rocq_vm_initialized = 0; |
| 84 |
|
| 85 |
value init_rocq_vm(value unit) |
| 86 |
{ |
| 87 |
if (rocq_vm_initialized) { |
| 88 |
fprintf(stderr, "already open\n"); |
| 89 |
fflush(stderr); |
| 90 |
return Val_unit; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
rocq_stack_low = (value *) rocq_stat_alloc(Rocq_stack_size); |
| 95 |
rocq_stack_high = rocq_stack_low + Rocq_stack_size / sizeof (value); |
| 96 |
rocq_stack_threshold = rocq_stack_low + Rocq_stack_threshold / sizeof(value); |
| 97 |
rocq_max_stack_size = Rocq_max_stack_size; |
| 98 |
|
| 99 |
|
| 100 |
rocq_sp = rocq_stack_high; |
| 101 |
rocq_interprete(NULL, Val_unit, Atom(0), Atom(0), Val_unit, 0); |
| 102 |
|
| 103 |
|
| 104 |
if (rocq_prev_scan_roots_hook == NULL) |
| 105 |
rocq_prev_scan_roots_hook = caml_scan_roots_hook; |
| 106 |
caml_scan_roots_hook = rocq_scan_roots; |
| 107 |
rocq_vm_initialized = 1; |
| 108 |
|
| 109 |
return Val_unit;; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
void realloc_rocq_stack(asize_t required_space) |
| 114 |
{ |
| 115 |
asize_t size; |
| 116 |
value * new_low, * new_high, * new_sp; |
| 117 |
size = rocq_stack_high - rocq_stack_low; |
| 118 |
do { |
| 119 |
size *= 2; |
| 120 |
} while (size < rocq_stack_high - rocq_sp + required_space); |
| 121 |
new_low = (value *) rocq_stat_alloc(size * sizeof(value)); |
| 122 |
new_high = new_low + size; |
| 123 |
|
| 124 |
#define shift(ptr) \ |
| 125 |
((char *) new_high - ((char *) rocq_stack_high - (char *) (ptr))) |
| 126 |
|
| 127 |
new_sp = (value *) shift(rocq_sp); |
| 128 |
memmove((char *) new_sp, |
| 129 |
(char *) rocq_sp, |
| 130 |
(rocq_stack_high - rocq_sp) * sizeof(value)); |
| 131 |
rocq_stat_free(rocq_stack_low); |
| 132 |
rocq_stack_low = new_low; |
| 133 |
rocq_stack_high = new_high; |
| 134 |
rocq_stack_threshold = rocq_stack_low + Rocq_stack_threshold / sizeof(value); |
| 135 |
rocq_sp = new_sp; |
| 136 |
#undef shift |
| 137 |
} |