aboutsummaryrefslogtreecommitdiffstats
path: root/src/ir.c
blob: 4c85792afa90b82caa5ceaada7e2fc580bcbf031 (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
typedef enum Operator {
    // Arithmetic ops.
    OP_ADD,
    OP_SUB,
    OP_MUL,
    OP_DIV,
    OP_MOD,

    // Load/store/copy operations.
    OP_LD8,
    OP_LD16,
    OP_LD32,
    OP_LD64,
    OP_ST8,
    OP_ST16,
    OP_ST32,
    OP_ST64,
    OP_CP8,
    OP_CP16,
    OP_CP32,
    OP_CP64,

    // Bit fiddling operations.
    OP_NOT,
    OP_AND,
    OP_OR,
    OP_XOR,
    OP_LSHIFT,
    OP_RSHIFT,
    OP_LROT,
    OP_RROT,

    // (Un)conditional jump operations.
    OP_LABEL,
    OP_JMP,
    OP_JMP_EQ,
    OP_JMP_NEQ,
    OP_JMP_GT,
    OP_JMP_LT,
    OP_JMP_GE,
    OP_JMP_LE,
} Operator;

typedef enum OperandType {
    OP_TYPE_REG,
    OP_TYPE_CONST,
    OP_TYPE_LABEL,
} OperandType;

typedef struct Operand {
    OperandType type;
    union {
        struct {
            size_t num;
        } reg;

        struct {
            size_t num;
        } label;

        struct {
            union {
                u64 uval;
                s64 sval;
            };
        } const;
    };
} Operand;

typedef struct Instruction {
    Operator op;
    Operand dst;
    Operand src_a;
    Operand src_b;
} Instruction;

typedef struct LineInfo {
    size_t line;
    size_t col;
} LineInfo;

typedef struct ProgramBASM {
    Instruction *inst;
    LineInfo *lines;
} ProgramBASM;