1 |
|
|
#include "gwion_util.h" |
2 |
|
|
#include "gwion_ast.h" |
3 |
|
|
#include "gwion_env.h" |
4 |
|
|
#include "vm.h" |
5 |
|
|
#include "instr.h" |
6 |
|
|
#include "object.h" |
7 |
|
|
#include "array.h" |
8 |
|
|
#include "memoize.h" |
9 |
|
|
#include "gwion.h" |
10 |
|
|
#include "operator.h" |
11 |
|
|
#include "import.h" |
12 |
|
|
|
13 |
|
842 |
ANN void free_code_instr(const Vector v, const Gwion gwion) { |
14 |
✓✓ |
21372 |
for(m_uint i = vector_size(v) + 1; --i;) { |
15 |
|
19688 |
const Instr instr = (Instr)vector_at(v, i - 1); |
16 |
✓✓ |
39331 |
const f_freearg f = (f_freearg)(map_get(&gwion->data->freearg, instr->opcode) ?: |
17 |
|
19643 |
map_get(&gwion->data->freearg, (vtype)instr->execute)); |
18 |
✓✓ |
19688 |
if(f) |
19 |
|
150 |
f(instr, gwion); |
20 |
|
19688 |
mp_free(gwion->mp, Instr, instr); |
21 |
|
|
} |
22 |
|
842 |
} |
23 |
|
|
|
24 |
|
840 |
ANN static void _free_code_instr(const Vector v, const Gwion gwion) { |
25 |
|
840 |
free_code_instr(v, gwion); |
26 |
|
840 |
free_vector(gwion->mp, v); |
27 |
|
840 |
} |
28 |
|
|
|
29 |
|
59791 |
ANN static void free_vm_code(VM_Code a, Gwion gwion) { |
30 |
✓✓ |
59791 |
if(a->memoize) |
31 |
|
1 |
memoize_end(gwion->mp, a->memoize); |
32 |
✓✓ |
59791 |
if(!GET_FLAG(a, builtin)) { |
33 |
|
840 |
_mp_free(gwion->mp, vector_size(a->instr) * SZ_INT, a->bytecode); |
34 |
|
840 |
_free_code_instr(a->instr, gwion); |
35 |
|
|
} |
36 |
|
59791 |
free_mstr(gwion->mp, a->name); |
37 |
|
59791 |
mp_free(gwion->mp , VM_Code, a); |
38 |
|
59791 |
} |
39 |
|
|
|
40 |
|
844 |
ANN static m_bit* tobytecode(MemPool p, const VM_Code code) { |
41 |
|
844 |
const Vector v = code->instr; |
42 |
|
844 |
const m_uint sz = vector_size(v); |
43 |
|
844 |
m_bit *ptr = _mp_malloc(p, sz * BYTECODE_SZ); |
44 |
✓✓ |
20596 |
for(m_uint i= 0; i < sz; ++i) { |
45 |
|
19752 |
const Instr instr = (Instr)vector_at(v, i); |
46 |
✓✓ |
19752 |
if(instr->opcode < eOP_MAX) |
47 |
|
19454 |
memcpy(ptr + i*BYTECODE_SZ, instr, BYTECODE_SZ); |
48 |
|
|
else { |
49 |
|
298 |
*(m_bit*)(ptr + (i*BYTECODE_SZ)) = instr->opcode; |
50 |
|
|
// *(m_bit*)(ptr + (i*BYTECODE_SZ)) = eOP_MAX; |
51 |
|
298 |
*(Instr*)(ptr + (i*BYTECODE_SZ) + SZ_INT) = instr; |
52 |
|
298 |
*(f_instr*)(ptr + (i*BYTECODE_SZ) + SZ_INT*2) = instr->execute; |
53 |
|
|
} |
54 |
|
19752 |
*(unsigned short*)(ptr + (i*BYTECODE_SZ) + 2) = i; |
55 |
|
|
} |
56 |
|
844 |
return ptr; |
57 |
|
|
} |
58 |
|
|
|
59 |
|
|
|
60 |
|
69832 |
VM_Code new_vm_code(MemPool p, const Vector instr, const m_uint stack_depth, |
61 |
|
|
const ae_flag flag, const m_str name) { |
62 |
|
69832 |
VM_Code code = mp_calloc(p, VM_Code); |
63 |
✓✓ |
69832 |
if(instr) { |
64 |
|
844 |
code->instr = vector_copy(p, instr); |
65 |
|
844 |
code->bytecode = tobytecode(p, code); |
66 |
|
|
} |
67 |
|
69832 |
code->name = mstrdup(p, name); |
68 |
|
69832 |
code->stack_depth = stack_depth; |
69 |
|
69832 |
code->flag = flag; |
70 |
|
69832 |
code->ref = new_refcount(p, free_vm_code); |
71 |
|
69832 |
return code; |
72 |
|
|
} |
73 |
|
|
|