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
|
#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 */
} TypeLevel;
typedef enum {
T_NONE,
T_TUPLE,
T_BOOL,
T_INT
} BaseType;
typedef struct Type {
TypeLevel lvl;
BaseType 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);
int type_check(struct Node *n);
Str type_desc(Type *t, Arena *arena);
void type_err(struct Node *n, Lexer *l);
/* nodes */
typedef enum {
N_START,
N_PROJ,
N_RETURN,
N_KEEPALIVE,
N_LIT,
N_OP_ADD, N_OP_SUB, N_OP_MUL, N_OP_DIV,
N_OP_AND, N_OP_OR, N_OP_XOR,
N_OP_SHL, N_OP_SHR,
N_OP_NEG, N_OP_NOT,
N_CMP_EQL, N_CMP_NEQ, N_CMP_LES, N_CMP_GTR, N_CMP_LTE, N_CMP_GTE,
N_VALUE
} NodeType;
const char *node_type_name(NodeType t);
#define NODE_INPUT_MAX 2
typedef struct {
struct Node *data[NODE_INPUT_MAX];
ptrdiff_t len;
} NodeInputs;
typedef DYNARR(struct Node *) NodeOutputs;
typedef struct Node {
union {
struct Node *prev_free;
struct {
int id, refs;
int walked;
NodeType type;
LexSpan src_pos;
NodeInputs in;
NodeOutputs out;
Value val;
};
};
} Node;
/* procedure graph */
typedef struct NameBinding {
struct NameBinding *prev;
LexSpan src_pos;
Str name;
Node *node;
} NameBinding;
typedef struct ScopeFrame {
struct ScopeFrame *prev;
NameBinding *latest;
} ScopeFrame;
typedef struct {
ScopeFrame *tail, *free_scope;
NameBinding *free_bind;
} Scope;
typedef struct {
Arena arena;
Str name;
Node *start, *stop, *keepalive;
Node *free_list;
Scope scope;
} Proc;
void node_kill(Node *n, Proc *p);
void node_die(Node *n, Proc *p);
void node_del_out(Node *n, Node *p);
void node_del_in(Node *n, Node *p);
void node_kill(Node *n, Proc *p);
void node_add(Proc *p, Node *src, Node *dest);
void node_add_out(Proc *p, Node *a, Node *b);
void node_add_in(Proc *p, Node *a, Node *b);
void node_remove(Proc *p, Node *src, Node *dest);
Node *node_new_empty(Proc *p, NodeType t);
Node *node_newv(Proc *p, NodeType t, ...);
Node *node_dedup_lit(Proc *p, Value v);
Value node_compute(Node *n, Lexer *l);
Node *node_peephole(Node *n, Proc *p, Lexer *l);
Node *node_new_lit(Proc *p, Value v);
Node *node_new_lit_bool(Proc *p, int b);
Node *node_new_lit_i64(Proc *p, int64_t i);
#define node_new(...) node_newv(__VA_ARGS__, NULL)
void proc_init(Proc *proc, Str name);
void proc_free(Proc *proc);
ScopeFrame *scope_push(Scope *scope, Proc *proc);
ScopeFrame *scope_pop(Scope *scope, Proc *proc);
NameBinding *scope_find(Scope *scope, Str name);
NameBinding *scope_bind(Scope *scope, Str name, Node *value, LexSpan pos, Proc *proc);
NameBinding *scope_update(Scope *scope, Str name, Node *to, Proc *proc);
#endif
|