GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/vm/vm_code.c Lines: 45 45 100.0 %
Date: 2020-10-03 09:50:08 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
854
ANN void free_code_instr(const Vector v, const Gwion gwion) {
14
21564
  for(m_uint i = vector_size(v) + 1; --i;) {
15
19856
    const Instr instr = (Instr)vector_at(v, i - 1);
16
39667
    const f_freearg f = (f_freearg)(map_get(&gwion->data->freearg, instr->opcode) ?:
17
19811
       map_get(&gwion->data->freearg, (vtype)instr->execute));
18
19856
    if(f)
19
152
      f(instr, gwion);
20
19856
    mp_free(gwion->mp, Instr, instr);
21
  }
22
854
}
23
24
852
ANN static void _free_code_instr(const Vector v, const Gwion gwion) {
25
852
  free_code_instr(v, gwion);
26
852
  free_vector(gwion->mp, v);
27
852
}
28
29
59969
ANN static void free_vm_code(VM_Code a, Gwion gwion) {
30
59969
  if(a->memoize)
31
1
    memoize_end(gwion->mp, a->memoize);
32
59969
  if(!GET_FLAG(a, builtin)) {
33
852
    _mp_free(gwion->mp, vector_size(a->instr) * SZ_INT, a->bytecode);
34
852
    _free_code_instr(a->instr, gwion);
35
  }
36
59969
  free_mstr(gwion->mp, a->name);
37
59969
  mp_free(gwion->mp , VM_Code, a);
38
59969
}
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
70038
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
70038
  VM_Code code           = mp_calloc(p, VM_Code);
63
70038
  if(instr) {
64
856
    code->instr            = vector_copy(p, instr);
65
856
    code->bytecode = tobytecode(p, code);
66
  }
67
70038
  code->name             = mstrdup(p, name);
68
70038
  code->stack_depth      = stack_depth;
69
70038
  code->flag = flag;
70
70038
  code->ref = new_refcount(p, free_vm_code);
71
70038
  return code;
72
}
73