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
|
#ifndef IR_H
#define IR_H
#include "arena.h"
#include "dynarr.h"
#include "lex.h" /* for error reporting only */
#include "str.h"
/* types */
typedef enum {
T_TOP, /* may or may not be a constant */
T_CONST, /* known compile-time constant */
T_BOT, /* known not a constant */
T_CTRL, /* control flow bottom */
T_XCTRL, /* control flow top (dead) */
} TypeLevel;
typedef enum {
T_NONE,
T_TUPLE,
T_BOOL,
T_INT,
T_PTR
} TypeKind;
typedef struct Type {
TypeLevel lvl;
TypeKind t;
struct Type *next;
} Type;
typedef struct Value {
Type type;
union {
int64_t i;
uint64_t u;
DYNARR(struct Value) tuple;
};
} Value;
struct Node;
int type_eql(Type *a, Type *b);
int type_base_eql(Type *a, Type *b);
int value_eql(Value *a, Value *b);
void type_check(struct Node *n, Lexer *l);
Str type_desc(Type *t, Arena *arena);
void type_err(struct Node *n, Lexer *l);
void type_expected(Type *want, struct Node *n, Lexer *l);
/* nodes */
#define NODE_TYPE_LIST\
X(NONE, "invalid node")\
X(DEAD, "dead node")\
X(START, "start")\
X(IF_ELSE, "if-else")\
X(REGION, "region")\
X(PHI, "phi")\
X(STOP, "stop")\
X(PROJ, "projection")\
X(RETURN, "return")\
X(KEEPALIVE, "keepalive")\
X(LIT, "literal")\
X(UNINIT, "uninitialized value")\
X(OP_ADD, "add")\
X(OP_SUB, "sub")\
X(OP_MUL, "mul")\
X(OP_DIV, "div")\
X(OP_AND, "and")\
X(OP_OR, "or")\
X(OP_XOR, "xor")\
X(OP_SHL, "shl")\
X(OP_SHR, "shr")\
X(OP_NEG, "neg")\
X(OP_NOT, "not")\
X(CMP_EQL, "equal")\
X(CMP_NEQ, "not-equal")\
X(CMP_LES, "less-than")\
X(CMP_GTR, "greater-than")\
X(CMP_LTE, "less-or-equal")\
X(CMP_GTE, "greater-or-equal")
typedef enum {
#define X(n, name) N_##n,
NODE_TYPE_LIST
#undef X
N_MAX
} NodeType;
#define NMASK(n) (1L << n)
typedef enum {
#define X(n, name) NM_##n = NMASK(N_##n),
NODE_TYPE_LIST
#undef X
} NodeMask;
const char *node_type_name(NodeType t);
typedef DYNARR(struct Node *) NodeList;
typedef NodeList NodeInputs, NodeOutputs;
typedef struct Node {
int id, refs;
union {
struct Node *prev_free;
struct {
int walked;
NodeType op;
LexSpan src_pos;
NodeInputs in; /* note: index 0 used for control flow */
NodeOutputs out;
union {
Type type;
Value val;
};
};
};
} Node;
/* convenience macros (lisp-inspired lol) */
#define IN(n, i) ((n)->in.data[i])
#define OUT(n, i) ((n)->out.data[i])
#define CTRL(n) IN(n, 0)
#define CAR(n) IN(n, 1)
#define CDR(n) IN(n, 2)
#define CAAR(n) CAR(CAR(n))
#define CADR(n) CDR(CAR(n))
#define CDAR(n) CAR(CDR(n))
#define CDDR(n) CDR(CDR(n))
#define CAAAR(n) CAR(CAAR(n))
#define CAADR(n) CDR(CAAR(n))
#define CADAR(n) CAR(CADR(n))
#define CADDR(n) CDR(CADR(n))
#define CDAAR(n) CAR(CDAR(n))
#define CDADR(n) CDR(CDAR(n))
#define CDDAR(n) CAR(CDDR(n))
#define CDDDR(n) CDR(CDDR(n))
#define T(a,b) ((a)->op == b)
#define T2(a,b,t) ((a)->op == t && (b)->op == t)
/* node graph */
typedef struct {
Arena *arena;
Node *free_list;
} NodePool;
typedef struct {
/* pointer so that e.g. there can be one pool per worker thread,
* instead of one per procedure graph */
NodePool *pool;
Node *start, *stop, *ctrl, *keepalive;
} Graph;
#define NODE_KEEP(p, n, ...)\
do { Node *keep_node = n;\
node_add_out(p, keep_node, p->keepalive); __VA_ARGS__;\
node_del_out(keep_node, p->keepalive); } while(0)
void node_kill(Node *n, Graph *p);
void node_die(Node *n, Graph *p);
void node_del_out(Node *n, Node *p);
void node_del_in(Node *n, Node *p);
void node_kill(Node *n, Graph *p);
void node_add(Graph *p, Node *src, Node *dest);
void node_add_out(Graph *p, Node *a, Node *b);
void node_add_in(Graph *p, Node *a, Node *b);
void node_set_in(Graph *p, Node *n, int idx, Node *to);
void node_remove(Graph *p, Node *src, Node *dest);
Node *node_new_empty(Graph *p, NodeType t);
Node *node_newv(Graph *p, NodeType t, Node *ctrl, ...);
Node *node_dedup_lit(Graph *p, Value v);
Node *node_new_lit(Graph *p, Value v);
Node *node_new_lit_bool(Graph *p, int b);
Node *node_new_lit_i64(Graph *p, int64_t i);
int node_uninit(Node *n);
int node_maybe_uninit(Node *n);
#define node_new(...) node_newv(__VA_ARGS__, NULL)
#endif
|