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 "pass.h" |
9 |
|
|
|
10 |
|
|
static const m_str default_passes_name[] = { "check", "emit" }; |
11 |
|
|
static const compilation_pass default_passes[][2] = { { type_engine_check_prog, type_engine_clean_prog }, { emit_ast, NULL } }; |
12 |
|
|
#define NPASS sizeof(default_passes)/sizeof(default_passes[0]) |
13 |
|
|
|
14 |
|
1437 |
ANN void pass_register(const Gwion gwion, const m_str name, const compilation_pass pass[2]) { |
15 |
|
1437 |
compilation_pass *passes = mp_malloc2(gwion->mp, sizeof(compilation_pass)*2); |
16 |
|
1437 |
passes[0] = pass[0]; |
17 |
|
1437 |
passes[1] = pass[1]; |
18 |
|
1437 |
const Symbol sym = insert_symbol(gwion->st, name); |
19 |
|
1437 |
map_set(&gwion->data->passes->map, (vtype)sym, (vtype)pass); |
20 |
|
1437 |
map_set(&gwion->data->passes->map, (vtype)sym, (vtype)passes); |
21 |
|
1437 |
} |
22 |
|
|
|
23 |
|
720 |
ANN m_bool pass_set(const Gwion gwion, const Vector passes) { |
24 |
|
720 |
const Vector v = &gwion->data->passes->vec; |
25 |
|
720 |
vector_clear(v); |
26 |
✓✓ |
2156 |
for(m_uint i = 0; i < vector_size(passes); ++i) { |
27 |
|
1438 |
const m_str name = (m_str)vector_at(passes, i); |
28 |
|
1438 |
const Symbol sym = insert_symbol(gwion->st, name); |
29 |
|
1438 |
const compilation_pass pass = (compilation_pass)map_get(&gwion->data->passes->map, (vtype)sym); |
30 |
✓✓ |
1438 |
if(!pass) { |
31 |
|
2 |
gw_err("Failed to set compilation passes, back to default\n"); |
32 |
|
2 |
pass_default(gwion); |
33 |
|
2 |
return GW_ERROR; |
34 |
|
|
} |
35 |
|
1436 |
vector_add(v, (vtype)pass); |
36 |
|
|
} |
37 |
|
718 |
return GW_OK; |
38 |
|
|
} |
39 |
|
|
|
40 |
|
718 |
ANN void pass_default(const Gwion gwion) { |
41 |
|
|
struct Vector_ v; |
42 |
|
718 |
vector_init(&v); |
43 |
✓✓ |
2154 |
for(m_uint i = 0; i < NPASS; ++i) { |
44 |
|
1436 |
vector_add(&v, (vtype)default_passes_name[i]); |
45 |
|
1436 |
pass_register(gwion, default_passes_name[i], default_passes[i]); |
46 |
|
|
} |
47 |
|
718 |
pass_set(gwion, &v); |
48 |
|
718 |
vector_release(&v); |
49 |
|
718 |
} |
50 |
|
|
|
51 |
|
|
|
52 |
|
716 |
ANEW ANN struct Passes_* new_passes(MemPool mp) { |
53 |
|
716 |
struct Passes_ *a = mp_calloc(mp, Passes); |
54 |
|
716 |
map_init(&a->map); |
55 |
|
716 |
vector_init(&a->vec); |
56 |
|
716 |
return a; |
57 |
|
|
} |
58 |
|
|
|
59 |
|
715 |
ANN void free_passes(const MemPool mp, struct Passes_ *a) { |
60 |
|
715 |
map_release(&a->map); |
61 |
✓✓ |
2145 |
for(m_uint i = 0; i < vector_size(&a->vec); ++i) { |
62 |
|
1430 |
compilation_pass *passes = (compilation_pass *)vector_at(&a->vec, i); |
63 |
|
1430 |
mp_free2(mp, sizeof(compilation_pass)*2, passes); |
64 |
|
|
} |
65 |
|
715 |
vector_release(&a->vec); |
66 |
|
715 |
} |