aboutsummaryrefslogtreecommitdiffstats
path: root/src/viz.c
blob: 8e3138770379bba0923baa6eff8f13b02ac97d10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
static const char* node_str[] = {
    [NODE_BUILTIN] = "BUILTIN",
    [NODE_NUMBER]  = "NUMBER",
    [NODE_BOOL]    = "BOOL",
    [NODE_STRING]  = "STRING",
    [NODE_SYMBOL]  = "SYMBOL",
    [NODE_TYPE]    = "TYPE",
    [NODE_DEF]     = "DEF",
    [NODE_SET]     = "SET",
    [NODE_FUN]     = "FUN",
    [NODE_BLOCK]   = "BLOCK",
    [NODE_IF]      = "IF",
    [NODE_FUNCALL] = "FUNCALL",
};

void
viz_node(Node *node) {
    if (node == NULL) {
        return;
    }
    printf("%zu [width=2.5,shape=Mrecord,label=\"", node->id);
    printf("<top> %s -- [%4ld:%-4ld] ", node_str[node->type], node->line, node->col);
    if (node->expr_type != NULL) {
        printf("| T: ");
        sv_write(&node->expr_type->name);
    }
    switch (node->type) {
        case NODE_NUMBER: {
            printf("| Value: ");
            if (node->number.negative) {
                printf("-");
            }
            if (node->number.fractional != 0) {
                printf("%zu.%zu", node->number.integral, node->number.fractional);
            } else {
                printf("%zu", node->number.integral);
            }
            printf("\"];\n");
        } break;
        case NODE_SYMBOL:
        case NODE_TYPE:
        case NODE_STRING: {
            printf(" | Value: ");
            sv_write(&node->string);
            printf("\"];\n");
        } break;
        case NODE_BOOL: {
            printf(" | Value: ");
            if (node->boolean) {
                printf("true");
            } else {
                printf("false");
            }
            printf("\"];\n");
        } break;
        case NODE_BUILTIN: {
            printf(" | Name: %s", token_str[node->builtin.type]);
            printf(" | <args> Args: ");
            printf("\"];\n");
            size_t n_args = array_size(node->builtin.args);
            for (size_t i = 0; i < n_args; ++i) {
                viz_node(node->builtin.args[i]);
                printf("%zu:args:e->%zu:top:w;\n", node->id, node->builtin.args[i]->id);
            }
        } break;
        case NODE_DEF: {
            printf(" | Name: ");
            sv_write(&node->def.symbol->string);
            printf(" | Type: ");
            sv_write(&node->def.type->string);
            printf(" | <val> Expr: ");
            printf("\"];\n");
            viz_node(node->def.value);
            printf("%zu:val:e->%zu:top:w;\n", node->id, node->def.value->id);
        } break;
        case NODE_SET: {
            printf(" | Name: ");
            sv_write(&node->set.symbol->string);
            printf(" | <val> Expr: ");
            printf("\"];\n");
            viz_node(node->set.value);
            printf("%zu:val:e->%zu:top:w;\n", node->id, node->def.value->id);
        } break;
        case NODE_BLOCK: {
            printf(" | <expr> Exprs: ");
            printf("\"];\n");
            size_t n_expr = array_size(node->block.expr);
            for (size_t i = 0; i < n_expr; ++i) {
                viz_node(node->block.expr[i]);
                printf("%zu:expr:e->%zu:top:w;\n", node->id, node->block.expr[i]->id);
            }
        } break;
        case NODE_IF: {
            printf(" | <cond> Cond: ");
            printf(" | <t> ExprTrue:");
            if (node->ifexpr.expr_false != NULL) {
                printf(" | <f> ExprFalse: ");
            }
            printf("\"];\n");
            viz_node(node->ifexpr.cond);
            printf("%zu:cond:e->%zu:top:w;\n", node->id, node->ifexpr.cond->id);
            viz_node(node->ifexpr.expr_true);
            printf("%zu:t:e->%zu:top:w;\n", node->id, node->ifexpr.expr_true->id);
            if (node->ifexpr.expr_false != NULL) {
                viz_node(node->ifexpr.expr_false);
                printf("%zu:f:e->%zu:top:w;\n", node->id, node->ifexpr.expr_false->id);
            }
        } break;
        case NODE_FUN: {
            printf(" | Name: ");
            sv_write(&node->fun.name->string);
            printf(" | Type: ");
            printf("(");
            size_t n_params = array_size(node->fun.param_names);
            for (size_t i = 0; i < n_params; ++i) {
                printf(":");
                sv_write(&node->fun.param_types[i]->string);
                if (i < n_params - 1) {
                    printf(" ");
                }
            }
            printf("):");
            sv_write(&node->fun.return_type->string);
            if (n_params > 0) {
                printf(" | Parameters: (");
                for (size_t i = 0; i < n_params; ++i) {
                    sv_write(&node->fun.param_names[i]->string);
                    if (i < n_params - 1) {
                        printf(" ");
                    }
                }
                printf(")");
            }
            printf(" | <bod> Body: ");
            printf("\"];\n");
            viz_node(node->fun.body);
            printf("%zu:bod:e->%zu:top:w;\n", node->id, node->fun.body->id);
        } break;
        case NODE_FUNCALL: {
            printf(" | Name: ");
            sv_write(&node->funcall.name->string);
            printf(" | <args> Args: ");
            printf("\"];\n");
            size_t n_args = array_size(node->funcall.args);
            for (size_t i = 0; i < n_args; ++i) {
                viz_node(node->funcall.args[i]);
                printf("%zu:args:e->%zu:top:w;\n", node->id,
                    node->funcall.args[i]->id);
            }
        } break;
    }
}

void
viz_ast(Root *roots) {
    if (roots == NULL) {
        return;
    }
    printf("digraph ast {\n");
    printf("rankdir=LR;\n");
    printf("ranksep=\"0.95 equally\";\n");
    printf("nodesep=\"0.5 equally\";\n");
    printf("overlap=scale;\n");
    printf("bgcolor=\"transparent\";\n");
    for (size_t i = 0; i < array_size(roots); ++i) {
        printf("subgraph %zu {\n", i);
        Node *root = roots[array_size(roots) - 1 - i];
        viz_node(root);
        printf("}\n");
    }
    printf("}\n");
}

void
viz_symtables(Scope **scopes) {
    printf("digraph symtables {\n");
    printf("rankdir=RL;\n");
    printf("bgcolor=\"transparent\";\n");
    for (size_t i = 0; i < array_size(scopes); ++i) {
        Scope *scope = scopes[i];
        if (array_size(scope->symbols->pairs) == 0) {
            continue;
        }
        printf("%zu [shape=none,label=<", scope->id);
        printf("<table border=\"1\" cellborder=\"0\">\n");
        printf("<tr><td>NAME</td><td>KIND</td><td>TYPE</td></tr>");
        for (size_t j = 0; j < array_cap(scope->symbols->pairs); ++j) {
            HashTablePair pair = scope->symbols->pairs[j];
            if (pair.key == NULL) {
                continue;
            }
            Symbol *sym = pair.value;
            printf("<tr>");
            printf("<td>");
            print_node(sym->name);
            printf("</td>");
            switch (sym->type) {
                case SYMBOL_PAR: {
                    printf("<td>par</td>");
                    printf("<td>");
                    print_node(sym->var.type);
                    printf("</td>");
                } break;
                case SYMBOL_VAR: {
                    printf("<td>var</td>");
                    printf("<td>");
                    print_node(sym->var.type);
                    printf("</td>");
                } break;
                case SYMBOL_FUN: {
                    printf("<td>fun</td>");
                    printf("<td>");
                    printf("(");
                    size_t n_params = array_size(sym->fun.param_types);
                    for (size_t k = 0; k < n_params; ++k) {
                        print_node(sym->fun.param_types[k]);
                        if (k < n_params - 1) {
                            printf(" ");
                        }
                    }
                    printf(")");
                    printf(":");
                    print_node(sym->fun.return_type);
                    printf("</td>");
                } break;
            }
            printf("</tr>\n");
        }
        printf("</table>\n");
        printf(">];\n");

        if (scope->parent != NULL) {
            printf("%zu -> %zu\n", scope->id, scope->parent->id);
        }
    }
    printf("}\n");
}

static const char* basm_op_str[] = {
    [OP_ADD]        = "add   ",
    [OP_SUB]        = "sub   ",
    [OP_MUL]        = "mul   ",
    [OP_DIV]        = "div   ",
    [OP_MOD]        = "mod   ",
    [OP_LD8]        = "ld8   ",
    [OP_LD16]       = "ld16  ",
    [OP_LD32]       = "ld32  ",
    [OP_LD64]       = "ld64  ",
    [OP_ST8]        = "st8   ",
    [OP_ST16]       = "st16  ",
    [OP_ST32]       = "st32  ",
    [OP_ST64]       = "st64  ",
    [OP_CP8]        = "cp8   ",
    [OP_CP16]       = "cp16  ",
    [OP_CP32]       = "cp32  ",
    [OP_CP64]       = "cp64  ",
    [OP_NOT]        = "not   ",
    [OP_AND]        = "and   ",
    [OP_OR]         = "or    ",
    [OP_XOR]        = "xor   ",
    [OP_LSHIFT]     = "lshift",
    [OP_RSHIFT]     = "rshift",
    [OP_LROT]       = "lrot  ",
    [OP_RROT]       = "rrot  ",
    [OP_LABEL]      = "lab   ",
    [OP_JMP]        = "jmp   ",
    [OP_JMP_TRUE]   = "jt    ",
    [OP_JMP_FALSE]  = "jf    ",
    [OP_JMP_EQ]     = "jeq   ",
    [OP_JMP_NEQ]    = "jne   ",
    [OP_JMP_GT]     = "jgt   ",
    [OP_JMP_LT]     = "jlt   ",
    [OP_JMP_GE]     = "jge   ",
    [OP_JMP_LE]     = "jle   ",
};

void
viz_operand(Operand op) {
    switch (op.type) {
        case OP_TYPE_LABEL: {
            printf("l%zu", op.id);
        } break;
        case OP_TYPE_REG: {
            printf("r%zu", op.id);
        } break;
        case OP_TYPE_CONST: {
            printf("%lld", op.constant.uval);
        } break;
    }
}

void
viz_instruction(Instruction *inst, LineInfo *line) {
    printf("[%6ld:%-6ld] ", line->line, line->col);
    printf("%s", basm_op_str[inst->op]);
    switch (inst->op) {
        case OP_ST8:
        case OP_ST16:
        case OP_ST32:
        case OP_ST64:
        case OP_LD8:
        case OP_LD16:
        case OP_LD32:
        case OP_LD64: {
            printf(" ");
            viz_operand(inst->dst);
            printf(", ");
            viz_operand(inst->src_a);
            if (inst->src_a.type != OP_TYPE_CONST) {
                printf(", ");
                viz_operand(inst->src_b);
            }
        } break;
        case OP_JMP:
        case OP_LABEL: {
            printf(" ");
            viz_operand(inst->dst);
        } break;
        case OP_JMP_TRUE:
        case OP_JMP_FALSE: {
            printf(" ");
            viz_operand(inst->dst);
            printf(", ");
            viz_operand(inst->src_a);
        } break;
        default: {
            printf(" ");
            viz_operand(inst->dst);
            printf(", ");
            viz_operand(inst->src_a);
            printf(", ");
            viz_operand(inst->src_b);
        } break;
    }
    printf("\n");
}

void
viz_basm(ProgramBASM *program) {
    for (size_t i = 0; i < array_size(program->inst); ++i) {
        viz_instruction(&program->inst[i], &program->lines[i]);
    }

}