aboutsummaryrefslogtreecommitdiff
path: root/ssa.h
blob: 75d166d36f079505805c42b19a0a9867c010b8c1 (plain) (blame)
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
#ifndef SSA_H
#define SSA_H
#include "const.h"
#include "semantics.h"
typedef struct COpr COpr;
typedef COpr *COpr_t;
struct COpr {
    enum {
        VAR,
        TMP,
        IMM,
        IMMS
    } kind;
    union {
        CVar_t var;
        int imm;
        char *str;
    } info;
};

typedef struct CInst CInst;
typedef CInst *CInst_t;
struct CInst {
    enum {
        MOVE,
        BEQZ,   /* conditional jump */
        BNEZ,
        GOTO,   /* unconditional jump */
        ARR,    /* displacement */
        WARR,
        PUSH,   /* push to stack top */
        CALL,   /* call function */
        RET,    /* return */
        MUL, DIV, MOD, ADD, SUB,
        SHL, SHR, AND, XOR, OR,
        LOR, LAND, NEG, NOR, SEQ,
        EQ, NE, LT, GT, LE, GE
    } op;
    COpr_t dest, src1, src2;
    CInst_t next, prev;
};

typedef struct COprList COprList;
typedef COprList *COprList_t;
struct COprList {
    COpr opr;
    COprList_t next;
};
typedef struct CPhi CPhi;
typedef CPhi *CPhi_t;
struct CPhi {
    COpr dest;
    COprList_t params;
};

typedef struct CBlock CBlock;
typedef CBlock *CBlock_t;
struct CBlock {
    CPhi_t phis;
    CInst_t insts;  /* instructions */
    CBlock_t next, prev;
    int id;
    int ref;        /* if referenced by any gotos */
};

CBlock_t cblock_create();
void cblock_append(CBlock_t cblk, CInst_t inst);
CInst_t cblock_getback(CBlock_t cblk);
int cblock_isempty(CBlock_t cblk);

typedef struct CEdge CEdge;
typedef struct CGraph {
    struct CEdge {
        CBlock *to;
        CEdge *next;
    } *head[MAX_BLOCK], *rhead[MAX_BLOCK];
} CGraph;

void ssa_generate(CScope_t scope);
#endif