aboutsummaryrefslogtreecommitdiffstats
path: root/src/viz.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/viz.c')
-rw-r--r--src/viz.c93
1 files changed, 93 insertions, 0 deletions
diff --git a/src/viz.c b/src/viz.c
index ea8a817..eb5631b 100644
--- a/src/viz.c
+++ b/src/viz.c
@@ -235,3 +235,96 @@ viz_symtables(Scope **scopes) {
235 } 235 }
236 printf("}\n"); 236 printf("}\n");
237} 237}
238
239static const char* basm_op_str[] = {
240 [OP_ADD] = "add ",
241 [OP_SUB] = "sub ",
242 [OP_MUL] = "mul ",
243 [OP_DIV] = "div ",
244 [OP_MOD] = "mod ",
245 [OP_LD8] = "ld8 ",
246 [OP_LD16] = "ld16 ",
247 [OP_LD32] = "ld32 ",
248 [OP_LD64] = "ld64 ",
249 [OP_ST8] = "st8 ",
250 [OP_ST16] = "st16 ",
251 [OP_ST32] = "st32 ",
252 [OP_ST64] = "st64 ",
253 [OP_CP8] = "cp8 ",
254 [OP_CP16] = "cp16 ",
255 [OP_CP32] = "cp32 ",
256 [OP_CP64] = "cp64 ",
257 [OP_NOT] = "not ",
258 [OP_AND] = "and ",
259 [OP_OR] = "or ",
260 [OP_XOR] = "xor ",
261 [OP_LSHIFT] = "lshift",
262 [OP_RSHIFT] = "rshift",
263 [OP_LROT] = "lrot ",
264 [OP_RROT] = "rrot ",
265 [OP_LABEL] = "lab ",
266 [OP_JMP] = "jmp ",
267 [OP_JMP_EQ] = "jeq ",
268 [OP_JMP_NEQ] = "jne ",
269 [OP_JMP_GT] = "jgt ",
270 [OP_JMP_LT] = "jlt ",
271 [OP_JMP_GE] = "jge ",
272 [OP_JMP_LE] = "jle ",
273};
274
275void
276viz_operand(Operand op) {
277 switch (op.type) {
278 case OP_TYPE_LABEL: {
279 printf("l%zu", op.id);
280 } break;
281 case OP_TYPE_REG: {
282 printf("r%zu", op.id);
283 } break;
284 case OP_TYPE_CONST: {
285 printf("%lld", op.constant.uval);
286 } break;
287 }
288}
289
290void
291viz_instruction(Instruction *inst, LineInfo *line) {
292 printf("[%6ld:%-6ld] ", line->line, line->col);
293 printf("%s", basm_op_str[inst->op]);
294 switch (inst->op) {
295 case OP_ST8:
296 case OP_ST16:
297 case OP_ST32:
298 case OP_ST64:
299 case OP_LD8:
300 case OP_LD16:
301 case OP_LD32:
302 case OP_LD64: {
303 printf(" ");
304 viz_operand(inst->dst);
305 printf(", ");
306 viz_operand(inst->src_a);
307 if (inst->src_a.type != OP_TYPE_CONST) {
308 printf(", ");
309 viz_operand(inst->src_b);
310 }
311 } break;
312 default: {
313 printf(" ");
314 viz_operand(inst->dst);
315 printf(", ");
316 viz_operand(inst->src_a);
317 printf(", ");
318 viz_operand(inst->src_b);
319 } break;
320 }
321 printf("\n");
322}
323
324void
325viz_basm(ProgramBASM *program) {
326 for (size_t i = 0; i < array_size(program->inst); ++i) {
327 viz_instruction(&program->inst[i], &program->lines[i]);
328 }
329
330}