aboutsummaryrefslogtreecommitdiffstats
path: root/src/bytecode/debug.h
blob: 3d08d8f53b9a2f90cfee5d4c58a8e22c07673479 (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
#ifndef BDL_DEBUG_H
#define BDL_DEBUG_H

void disassemble_chunk(u8 *chunk, const char *name);
size_t disassemble_instruction(u8 *chunk, size_t offset);

void
disassemble_chunk(u8 *chunk, const char *name) {
    printf("== %s ==\n", name);
    size_t offset = 0;
    while (offset < array_size(chunk)) {
        offset = disassemble_instruction(chunk, offset);
    }
}

size_t
disassemble_instruction(u8 *chunk, size_t offset) {
    printf("%04ld ", offset);
    u8 instruction = chunk[offset];
    switch (instruction) {
        case OP_RETURN: {
            printf("OP_RETURN\n");
            return offset + 1;
        } break;
        default: {
            printf("Unknown OP: %d\n", instruction);
            return offset + 1;
        } break;
    }
}

#endif // BDL_DEBUG_H