GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/emit/memoize.c Lines: 34 34 100.0 %
Date: 2020-08-07 19:15:19 Branches: 6 6 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 "emit.h"
7
#include "gwion.h"
8
#include "memoize.h"
9
10
struct Memoize_ {
11
  struct Vector_ v;
12
  m_uint arg_sz;
13
  m_uint ret_sz;
14
  size_t limit;
15
  size_t curr;
16
};
17
18
1
Memoize memoize_ini(const Emitter emit, const Func f) {
19
1
  Memoize m = mp_calloc(emit->gwion->mp, Memoize);
20
1
  vector_init(&m->v);
21
1
  m->ret_sz = f->def->base->ret_type->size;
22
1
  m->arg_sz = f->def->stack_depth;
23
1
  m->limit = emit->info->memoize;
24
1
  return m;
25
}
26
27
1
void memoize_end(MemPool p, Memoize m) {
28
56
  for(m_uint i = 0; i < vector_size(&m->v); ++i)
29
55
    mp_free2(p, m->arg_sz + m->arg_sz, (void*)vector_at(&m->v, i));
30
1
  vector_release(&m->v);
31
1
  mp_free(p, Memoize, m);
32
1
}
33
34
53
ANN static inline m_bit* get_data(MemPool mp, Memoize m) {
35
53
  if(vector_size(&m->v) >= m->limit)
36
51
    return (m_bit*)vector_at(&m->v, m->curr++ % m->limit);
37
2
  m_bit* data = mp_calloc2(mp, m->arg_sz + m->ret_sz);
38
2
  vector_add(&m->v, (vtype)data);
39
2
  return data;
40
}
41
42
53
INSTR(MemoizeStore) {
43
53
  const Memoize m = shred->code->memoize;
44
53
  m_bit* data = get_data(shred->info->vm->gwion->mp, m);
45
53
  memcpy(data, shred->mem, m->arg_sz);
46
53
  memcpy(data + m->arg_sz, shred->reg - m->ret_sz, m->ret_sz);
47
53
  vector_add(&m->v, (vtype)data);
48
53
}
49
50
1319
INSTR(MemoizeIni) {
51
1319
  const m_uint idx = *(m_uint*)MEM(instr->m_val);
52
1319
  const Memoize m = shred->code->memoize;
53
1319
  if(idx < VLEN(&m->v)) {
54
1266
    const m_bit* data = (m_bit*)vector_at(&m->v, idx);
55
1266
    memcpy(MEM(instr->m_val + SZ_INT *2), data, instr->m_val2);
56
  } else
57
53
    shred->pc = *(m_uint*)MEM(instr->m_val + SZ_INT);
58
1319
}