| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
#include <stdio.h> |
| 15 |
#include <stdlib.h> |
| 16 |
#include <stdint.h> |
| 17 |
#include <caml/config.h> |
| 18 |
#include <caml/misc.h> |
| 19 |
#include <caml/mlvalues.h> |
| 20 |
#include <caml/fail.h> |
| 21 |
#include <caml/alloc.h> |
| 22 |
#include <caml/memory.h> |
| 23 |
#include "rocq_instruct.h" |
| 24 |
#include "rocq_arity.h" |
| 25 |
#include "rocq_fix_code.h" |
| 26 |
|
| 27 |
#ifdef THREADED_CODE |
| 28 |
|
| 29 |
static char ** rocq_instr_table; |
| 30 |
static char * rocq_instr_base; |
| 31 |
#define VALINSTR(instr) ((opcode_t)(rocq_instr_table[instr] - rocq_instr_base)) |
| 32 |
|
| 33 |
void rocq_init_thread_code(void ** instr_table, void * instr_base) |
| 34 |
{ |
| 35 |
rocq_instr_table = (char **) instr_table; |
| 36 |
rocq_instr_base = (char *) instr_base; |
| 37 |
} |
| 38 |
|
| 39 |
#else |
| 40 |
#define VALINSTR(instr) instr |
| 41 |
#endif |
| 42 |
|
| 43 |
int rocq_is_instruction(opcode_t instr1, opcode_t instr2) |
| 44 |
{ |
| 45 |
return instr1 == VALINSTR(instr2); |
| 46 |
} |
| 47 |
|
| 48 |
void * rocq_stat_alloc (asize_t sz) |
| 49 |
{ |
| 50 |
void * result = malloc (sz); |
| 51 |
if (result == NULL) caml_raise_out_of_memory (); |
| 52 |
return result; |
| 53 |
} |
| 54 |
|
| 55 |
static opcode_t accu_instr; |
| 56 |
code_t accumulate = &accu_instr; |
| 57 |
|
| 58 |
value rocq_accumulate(value unit) |
| 59 |
{ |
| 60 |
CAMLparam1(unit); |
| 61 |
CAMLlocal1(res); |
| 62 |
accu_instr = VALINSTR(ACCUMULATE); |
| 63 |
res = caml_alloc_small(1, Abstract_tag); |
| 64 |
Code_val(res) = accumulate; |
| 65 |
CAMLreturn(res); |
| 66 |
} |
| 67 |
|
| 68 |
value rocq_makeaccu (value i) { |
| 69 |
CAMLparam1(i); |
| 70 |
CAMLlocal1(res); |
| 71 |
code_t q = rocq_stat_alloc(2 * sizeof(opcode_t)); |
| 72 |
res = caml_alloc_small(1, Abstract_tag); |
| 73 |
Code_val(res) = q; |
| 74 |
*q++ = VALINSTR(MAKEACCU); |
| 75 |
*q = (opcode_t)Int_val(i); |
| 76 |
CAMLreturn(res); |
| 77 |
} |
| 78 |
|
| 79 |
value rocq_pushpop (value i) { |
| 80 |
CAMLparam1(i); |
| 81 |
CAMLlocal1(res); |
| 82 |
code_t q; |
| 83 |
res = caml_alloc_small(1, Abstract_tag); |
| 84 |
int n = Int_val(i); |
| 85 |
if (n == 0) { |
| 86 |
q = rocq_stat_alloc(sizeof(opcode_t)); |
| 87 |
Code_val(res) = q; |
| 88 |
*q = VALINSTR(STOP); |
| 89 |
CAMLreturn(res); |
| 90 |
} |
| 91 |
else { |
| 92 |
q = rocq_stat_alloc(3 * sizeof(opcode_t)); |
| 93 |
Code_val(res) = q; |
| 94 |
*q++ = VALINSTR(POP); |
| 95 |
*q++ = (opcode_t)n; |
| 96 |
*q = VALINSTR(STOP); |
| 97 |
CAMLreturn(res); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
#ifdef ARCH_BIG_ENDIAN |
| 102 |
#define Reverse_32(dst,src) { \ |
| 103 |
char * _p, * _q; \ |
| 104 |
char _a, _b; \ |
| 105 |
_p = (char *) (src); \ |
| 106 |
_q = (char *) (dst); \ |
| 107 |
_a = _p[0]; \ |
| 108 |
_b = _p[1]; \ |
| 109 |
_q[0] = _p[3]; \ |
| 110 |
_q[1] = _p[2]; \ |
| 111 |
_q[3] = _a; \ |
| 112 |
_q[2] = _b; \ |
| 113 |
} |
| 114 |
#define COPY32(dst,src) Reverse_32(dst,src) |
| 115 |
#else |
| 116 |
#define COPY32(dst,src) (*dst=*src) |
| 117 |
#endif |
| 118 |
|
| 119 |
value rocq_tcode_of_code (value code) { |
| 120 |
CAMLparam1 (code); |
| 121 |
CAMLlocal1 (res); |
| 122 |
code_t p, q; |
| 123 |
asize_t len = (asize_t) caml_string_length(code); |
| 124 |
res = caml_alloc_small(1, Abstract_tag); |
| 125 |
q = rocq_stat_alloc(len); |
| 126 |
Code_val(res) = q; |
| 127 |
len /= sizeof(opcode_t); |
| 128 |
for (p = (code_t)code; p < (code_t)code + len; ) { |
| 129 |
opcode_t instr; |
| 130 |
COPY32(&instr,p); |
| 131 |
p++; |
| 132 |
if (instr < 0 || instr > STOP) abort(); |
| 133 |
*q++ = VALINSTR(instr); |
| 134 |
if (instr == SWITCH) { |
| 135 |
uint32_t i, sizes, const_size, block_size; |
| 136 |
COPY32(q,p); p++; |
| 137 |
sizes=*q++; |
| 138 |
const_size = sizes >> 8; |
| 139 |
block_size = sizes & 0xFF; |
| 140 |
sizes = const_size + block_size; |
| 141 |
for(i=0; i<sizes; i++) { COPY32(q,p); p++; q++; }; |
| 142 |
} else if (instr == CLOSUREREC || instr==CLOSURECOFIX) { |
| 143 |
uint32_t i, n; |
| 144 |
COPY32(q,p); p++; |
| 145 |
n = 3 + 2*(*q); |
| 146 |
q++; |
| 147 |
for(i=1; i<n; i++) { COPY32(q,p); p++; q++; }; |
| 148 |
} else { |
| 149 |
int i, ar; |
| 150 |
ar = arity[instr]; |
| 151 |
if (ar < 0) abort(); |
| 152 |
for(i=0; i<ar; i++) { COPY32(q,p); p++; q++; }; |
| 153 |
} |
| 154 |
} |
| 155 |
CAMLreturn(res); |
| 156 |
} |