GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/vm/vm_code.c Lines: 45 45 100.0 %
Date: 2020-09-22 13:02:15 Branches: 16 16 100.0 %

Line Branch Exec Source
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
856
ANN void free_code_instr(const Vector v, const Gwion gwion) {
14
21598
  for(m_uint i = vector_size(v) + 1; --i;) {
15
19886
    const Instr instr = (Instr)vector_at(v, i - 1);
16
39727
    const f_freearg f = (f_freearg)(map_get(&gwion->data->freearg, instr->opcode) ?:
17
19841
       map_get(&gwion->data->freearg, (vtype)instr->execute));
18
19886
    if(f)
19
152
      f(instr, gwion);
20
19886
    mp_free(gwion->mp, Instr, instr);
21
  }
22
856
}
23
24
854
ANN static void _free_code_instr(const Vector v, const Gwion gwion) {
25
854
  free_code_instr(v, gwion);
26
854
  free_vector(gwion->mp, v);
27
854
}
28
29
59888
ANN static void free_vm_code(VM_Code a, Gwion gwion) {
30
59888
  if(a->memoize)
31
1
    memoize_end(gwion->mp, a->memoize);
32
59888
  if(!GET_FLAG(a, builtin)) {
33
854
    _mp_free(gwion->mp, vector_size(a->instr) * SZ_INT, a->bytecode);
34
854
    _free_code_instr(a->instr, gwion);
35
  }
36
59888
  free_mstr(gwion->mp, a->name);
37
59888
  mp_free(gwion->mp , VM_Code, a);
38
59888
}
39
40
856
ANN static m_bit* tobytecode(MemPool p, const VM_Code code) {
41
856
  const Vector v = code->instr;
42
856
  const m_uint sz = vector_size(v);
43
856
  m_bit *ptr = _mp_malloc(p, sz * BYTECODE_SZ);
44
20776
  for(m_uint i= 0; i < sz; ++i) {
45
19920
    const Instr instr = (Instr)vector_at(v, i);
46
19920
    if(instr->opcode < eOP_MAX)
47
19614
      memcpy(ptr + i*BYTECODE_SZ, instr, BYTECODE_SZ);
48
    else {
49
306
      *(m_bit*)(ptr + (i*BYTECODE_SZ)) = instr->opcode;
50
//      *(m_bit*)(ptr + (i*BYTECODE_SZ)) = eOP_MAX;
51
306
      *(Instr*)(ptr + (i*BYTECODE_SZ) + SZ_INT) = instr;
52
306
      *(f_instr*)(ptr + (i*BYTECODE_SZ) + SZ_INT*2) = instr->execute;
53
    }
54
19920
    *(unsigned short*)(ptr + (i*BYTECODE_SZ) + 2) = i;
55
  }
56
856
  return ptr;
57
}
58
59
60
69941
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
69941
  VM_Code code           = mp_calloc(p, VM_Code);
63
69941
  if(instr) {
64
856
    code->instr            = vector_copy(p, instr);
65
856
    code->bytecode = tobytecode(p, code);
66
  }
67
69941
  code->name             = mstrdup(p, name);
68
69941
  code->stack_depth      = stack_depth;
69
69941
  code->flag = flag;
70
69941
  code->ref = new_refcount(p, free_vm_code);
71
69941
  return code;
72
}
73