aboutsummaryrefslogtreecommitdiff
path: root/fptr_ast/struct.c
blob: f22fae41dbac94380460ab0d938609eceb331a1d (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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#include <stdarg.h>
#include <stdio.h>
#include <stddef.h>
#include <stdint.h>

#include "common.h"

typedef struct var {
	char *name;
	intptr_t v;
} var;

#define VEC_NAME vars
#define VEC_TYPE var
#include <conts/vec.h>
typedef struct vars vars;

typedef struct proc {
	vars vars;
} proc;

static proc *gen_proc(int header, ...)
{
	(void)header;

	proc *proc = calloc(1, sizeof(struct proc));
	if (!proc)
		return NULL;

	proc->vars = vars_create(0);

	va_list args;
	va_start(args, header);
	while (1) {
		char *name = va_arg(args, char *);

		/* NULL sentinel */
		if (!name)
			break;

		var var = {
			.name = name,
			.v = 0
		};
		vars_append(&proc->vars, var);
	}
	va_end(args);

	return proc;
}

#define gen_proc(...) gen_proc(0, __VA_ARGS__, NULL)

static void proc_set(proc *proc, char *name, intptr_t v)
{
	foreach(vars, var, &proc->vars) {
		if (strcmp(var->name, name) != 0)
			continue;

		var->v = v;
		return;
	}

	/* abort? */
}

/* forward declare */
typedef struct node node;

#define VEC_NAME nodes
#define VEC_TYPE node*
#include <conts/vec.h>
typedef struct nodes nodes;

typedef struct node {
	enum kind {
		NODE_LOAD = 1,
		NODE_STORE,
		NODE_ADD,
		NODE_MUL,
		NODE_VAR,
		NODE_VARREF,
		NODE_CONST,
		NODE_ASSIGN,
		NODE_FOR,
		NODE_LT,
		NODE_BODY,
	} kind;

	/* generic values */
	struct node *n0;
	struct node *n1;
	struct node *n2;
	struct node *n3;
	intptr_t v;

	/* list of nodes. If we really tried to save space, we could make all
	 * nodes just be indexes into this nodes array, but that would mean that
	 * execution has to go through more layers of indirection, so I'm
	 * reserving n0/n1/n2/n3 for quicker lookup */
	nodes nodes;

	/* used by var/varref nodes to point to variable in proc context */
	intptr_t *varref;
} node;

static node *gen_node(enum kind kind, node *n0, node *n1, node *n2, node *n3, intptr_t v)
{
	node *n = calloc(1, sizeof(node));
	if (!n)
		return NULL;

	/* unused by generic nodes */
	n->nodes = nodes_create(0);

	n->kind = kind;
	n->n0 = n0;
	n->n1 = n1;
	n->n2 = n2;
	n->n3 = n3;
	n->v = v;
	return n;
}


#define get_n0(k, x) ({assert((x)->kind == (k)); (x)->n0;})
#define get_n1(k, x) ({assert((x)->kind == (k)); (x)->n1;})
#define get_n2(k, x) ({assert((x)->kind == (k)); (x)->n2;})
#define get_n3(k, x) ({assert((x)->kind == (k)); (x)->n3;})
#define get_v(k, x) ({assert((x)->kind == (k)); (x)->v;})

/* generic defines for 'trivial' nodes */

#define gen_load(addr) gen_node(NODE_LOAD, (addr), NULL, NULL, NULL, 0)
#define load_addr(x) get_n0(NODE_LOAD, (x))

#define gen_store(addr, val) gen_node(NODE_STORE, (addr), (val), NULL, NULL, 0)
#define store_addr(x) get_n0(NODE_STORE, (x))
#define store_val(x) get_n1(NODE_STORE, (x))

#define gen_add(l, r) gen_node(NODE_ADD, (l), (r), NULL, NULL, 0)
#define add_l(x) get_n0(NODE_ADD, (x))
#define add_r(x) get_n1(NODE_ADD, (x))

#define gen_mul(l, r) gen_node(NODE_MUL, (l), (r), NULL, NULL, 0)
#define mul_l(x) get_n0(NODE_MUL, (x))
#define mul_r(x) get_n1(NODE_MUL, (x))

#define gen_const(c) gen_node(NODE_CONST, NULL, NULL, NULL, NULL, (c))
#define const_v(x) get_v(NODE_CONST, (x))

#define gen_assign(dst, src) gen_node(NODE_ASSIGN, (dst), (src), NULL, NULL, 0)
#define assign_dst(x) get_n0(NODE_ASSIGN, (x))
#define assign_src(x) get_n1(NODE_ASSIGN, (x))

#define gen_for(init, cond, post, body) gen_node(NODE_FOR, (init), (cond), (post), (body), 0)
#define for_init(x) get_n0(NODE_FOR, (x))
#define for_cond(x) get_n1(NODE_FOR, (x))
#define for_post(x) get_n2(NODE_FOR, (x))
#define for_body(x) get_n3(NODE_FOR, (x))

#define gen_lt(l, r) gen_node(NODE_LT, (l), (r), NULL, NULL, 0)
#define lt_l(x) get_n0(NODE_LT, (x))
#define lt_r(x) get_n1(NODE_LT, (x))

/* nodes that need special handling */
static node *gen_body(int header, ...)
{
	node *body = calloc(1, sizeof(node));
	if (!body)
		return NULL;

	body->kind = NODE_BODY;
	body->nodes = nodes_create(0);

	va_list args;
	va_start(args, header);
	while (1) {
		node *n = va_arg(args, node *);
		if (!n)
			break;

		nodes_append(&body->nodes, n);
	}
	va_end(args);

	return body;
}

#define gen_body(...) gen_body(0, __VA_ARGS__, NULL)
#define body_list(x) ({assert((x)->kind == NODE_BODY); (x)->nodes;})

static node *gen_var(proc *proc, char *name)
{
	assert(proc && name);

	node *n = calloc(1, sizeof(node));
	if (!n)
		return NULL;

	n->kind = NODE_VAR;
	foreach(vars, var, &proc->vars) {
		if (strcmp(var->name, name) != 0)
			continue;

		/* assumes that the var list is static, which it really should
		 * be by this point */
		n->varref = &var->v;
		return n;
	}

	/* no such var (abort or return some more obvious error) */
	free(n);
	return NULL;
}

#define var(x) ({assert((x)->kind == NODE_VAR); (x)->varref;})

static node *gen_varref(proc *proc, char *name)
{
	/* node generation logic is the same, it's just the runtime that's
	 * different from gen_var, so just change NODE_VAR -> NODE_VARREF */
	node *n = gen_var(proc, name);
	if (!n)
		return NULL;

	n->kind = NODE_VARREF;
	return n;
}

#define varref(x) ({assert((x)->kind == NODE_VARREF); (x)->varref;})

/* predeclare */
static intptr_t run(node *n);

static intptr_t run_load(node *n)
{
	return *(intptr_t* )run(load_addr(n));
}

static intptr_t run_store(node *n)
{
	intptr_t *dst = (intptr_t *)run(store_addr(n));
	intptr_t val = run(store_val(n));
	*dst = val;
	return 0;
}

static intptr_t run_add(node *n)
{
	intptr_t l = run(add_l(n));
	intptr_t r = run(add_r(n));
	return l + r;
}

static intptr_t run_mul(node *n)
{
	intptr_t l = run(mul_l(n));
	intptr_t r = run(mul_r(n));
	return l * r;
}

static intptr_t run_var(node *n)
{
	return *var(n);
}

static intptr_t run_varref(node *n)
{
	return (intptr_t)varref(n);
}

static intptr_t run_const(node *n)
{
	return const_v(n);
}

static intptr_t run_assign(node *n)
{
	intptr_t *dst = (intptr_t *)run(assign_dst(n));
	intptr_t src = run(assign_src(n));
	*dst = src;
	return 0;
}

static intptr_t run_for(node *n)
{
	for (run(for_init(n)); run(for_cond(n)); run(for_post(n)))
		run(for_body(n));

	return 0;
}

static intptr_t run_lt(node *n)
{
	intptr_t l = run(lt_l(n));
	intptr_t r = run(lt_r(n));
	return l < r;
}

static intptr_t run_body(node *n)
{
	nodes list = body_list(n);
	foreach(nodes, node, &list) {
		run(*node);
	}

	return 0;
}

static intptr_t run(node *n)
{
	switch (n->kind) {
	case NODE_LOAD:   return run_load(n);
	case NODE_STORE:  return run_store(n);
	case NODE_ADD:    return run_add(n);
	case NODE_MUL:    return run_mul(n);
	case NODE_VAR:    return run_var(n);
	case NODE_VARREF: return run_varref(n);
	case NODE_CONST:  return run_const(n);
	case NODE_ASSIGN: return run_assign(n);
	case NODE_FOR:    return run_for(n);
	case NODE_LT:     return run_lt(n);
	case NODE_BODY:   return run_body(n);
	default: fprintf(stderr, "unimp\n");
		 abort();
	}

	return 0;
}