~pixelherodev/c3lib

ref: 3a1f0d6788f8a8ae8864404b8eea0bf1cdc57362 c3lib/codegen/c.c -rw-r--r-- 10.9 KiB
3a1f0d67Noam Preil fix function names 2 months ago
                                                                                
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#include "../tags.h"
#include "../c3.h"
#include "../util.h"
#include "../stb.h"
#include "../stb_ds.h"

static struct {
	FILE *out;
	uint32_t next_block;
	struct {
		char *name;
		uint32_t block;
		uint32_t fn;
		int is_loop;
	} current;
	uint32_t *blocks;
	uint32_t nested;
	uint32_t counter;
	uint32_t indent;
	c3ctx c3;
	struct{
		uint32_t *node;
		uint32_t *tmp;
	} regalloc;
} ctx;

static int cg_block(uint32_t index);

static uint32_t
cg_find_reg(uint32_t node)
{
	uint32_t i;
	for(i = 0; i < stb_sb_count(ctx.regalloc.node); i += 1)
		if(ctx.regalloc.node[i] == node)
			return ctx.regalloc.tmp[i];
	return -1;
}

static int
cg_push_reg(uint32_t node, uint32_t reg)
{
	return stb_sb_push_u32(&ctx.regalloc.node, node) && stb_sb_push_u32(&ctx.regalloc.tmp, reg);
}

static int
cg_value(uint32_t index)
{
	c3tag tag = c3nodetag(ctx.c3, index);
	uint32_t tmp;
	char *buf;
	switch(tag){
	case C3_VALUE_TRUE:
		fprintf(ctx.out, "true");
		break;
	case C3_VALUE_FALSE:
		fprintf(ctx.out, "false");
		break;
	case C3_IR_CALL:
	case C3_IR_INVERT:
		tmp = cg_find_reg(index);
		if(tmp == -1)
			return 0;
		fprintf(ctx.out, "__zyg_tmp_%d", tmp);
		return 1;
	case C3_LINT:
		buf = h_c3str(ctx.c3, c3nodechild(ctx.c3, index, 0));
		if(buf == NULL){
			ERROR("Failed to read literal integer", 0);
			return 0;
		}
		fprintf(ctx.out, "%s", buf);
		free(buf);
		break;
	default:
		ERROR("TODO: generate value tag %s", c3tagstr(tag));
		return 0;
	}
	return 1;
}

static int
cg_params(uint32_t index)
{
	uint32_t count, i;
	uint32_t arg;
	count = c3nodekids(ctx.c3, index);
	for(i = 1; i < count; i += 1){
		arg = c3nodechild(ctx.c3, index, i);
		if(!cg_value(arg))
			return 0;
		if(i + 1 != count)
			fprintf(ctx.out, ", ");
	}
	return 1;
}

static int
cg_type(uint32_t index)
{
	c3tag tag;
	tag = c3nodetag(ctx.c3, index);
	switch(tag){
	case C3_TYPE_BOOL:
		fprintf(ctx.out, "bool");
		break;
	case C3_NORETURN:
		fprintf(ctx.out, "zyg_noreturn");
		break;
	case C3_VOID:
		fprintf(ctx.out, "void");
		break;
	case C3_TYPE_U32:
		fprintf(ctx.out, "uint32_t");
		break;
	default:
		ERROR("TODO cg_type tag %s", c3tagstr(tag));
		return 0;
	}
	return 1;
}

static int
cg_call(uint32_t index)
{
	uint32_t node;
	char *buf;
	uint32_t i;
	uint32_t fntype, rettype;
	for(i = 0; i < ctx.indent; i += 1)
		fprintf(ctx.out, "\t");
	// IR FN
	node = c3nodechild(ctx.c3, index, 0);
	// VALUE_FN
	node = c3nodechild(ctx.c3, index, 0);
	fntype = c3nodechild(ctx.c3, node, 0);
	rettype = c3nodechild(ctx.c3, fntype, 0);
	if(c3nodetag(ctx.c3, rettype) != C3_VOID && c3nodetag(ctx.c3, rettype) != C3_NORETURN){
		if(!cg_type(rettype)){
			ERROR("type gen failed", 0);
			return 0;
		}
		fprintf(ctx.out, " __zyg_tmp_%d = ", ctx.counter);
		if(!cg_push_reg(index, ctx.counter))
			return 0;
		ctx.counter += 1;
	}
	buf = h_c3str(ctx.c3, stbds_hmget(ctx.c3.analysis.names, index));
	fprintf(ctx.out, "%s(", buf);
	if(!cg_params(index))
		return 0;
	fprintf(ctx.out, ");\n");
	free(buf);
	return 1;
}

static int
cg_asm(uint32_t index)
{
	uint32_t string;
	char *buf, *buf2;
	uint32_t i;
	c3tag tag;
	uint32_t tmp;
	char *new;
	string = c3nodechild(ctx.c3, index, 0);
	string = c3nodechild(ctx.c3, string, 0);
	buf = h_c3str(ctx.c3, string);
	new = NULL;
	for(i = 0; i < strlen(buf); i += 1){
		if(buf[i] == '$'){
			i += 1;
			if(i >= strlen(buf)){
				if(!stb_sb_push_char(&new, '$')){
					ERROR("ICE: OOM", 0);
					return 0;
				}
				break;
			}
			if(buf[i] >= '0' && buf[i] <= '9'){
				tmp = c3nodechild(ctx.c3, index, buf[i] - '0');
				tag = c3nodetag(ctx.c3, tmp);
				switch(tag){
				default:
					ERROR("TODO: asm substitute local register param %c tag %s", buf[i], c3tagstr(tag));
					return 0;
				}
			}
			else if(buf[i] == '*'){
				i += 1;
				if(i < strlen(buf) && buf[i] >= '0' && buf[i] <= '9'){
					tmp = c3nodechild(ctx.c3, index, buf[i] - '0');
					tag = c3nodetag(ctx.c3, tmp);
					switch(tag){
					case C3_LITERAL_STRING:
						buf2 = h_c3str(ctx.c3, c3nodechild(ctx.c3, tmp, 0));
						if(!stb_stringbuf_append(&new, buf2)){
							ERROR("ICE: OOM", 0);
							return 0;
						}
						free(buf2);
						break;
					default:
						ERROR("TODO: asm substitute comptime string param %c tag %s", buf[i], c3tagstr(tag));
						return 0;
					}
				}
			} else if(buf[i] == '>'){
				ERROR("TODO: asm output", 0);
				return 0;
			}
		}
		else {
			if(!stb_sb_push_char(&new, buf[i])){
				ERROR("ICE: OOM", 0);
				return 0;
			}
		}
	}
	free(buf);
	if(!stb_sb_push_char(&new, 0)){
		ERROR("ICE: OOM", 0);
		return 0;
	}
	fprintf(ctx.out, "\t%s\n", new);
	stb_sb_free(new);
	return 1;
}

static int
cg_cond_br(uint32_t index)
{
	uint32_t from = c3nodechild(ctx.c3, index, 0);
	uint32_t i;
	if(c3nodetag(ctx.c3, from) == C3_IR_INVERT){
		from = c3nodechild(ctx.c3, from, 0);
		if(c3nodetag(ctx.c3, from) == C3_VALUE_TRUE)
			return 1;
		for(i = 0; i < ctx.indent; i += 1)
			fprintf(ctx.out, "\t");
		fprintf(ctx.out, "if(");
	}
	else{
		for(i = 0; i < ctx.indent; i += 1)
			fprintf(ctx.out, "\t");
		fprintf(ctx.out, "if(!");
	}
	if(!cg_value(from))
		return 0;
	fprintf(ctx.out, "){\n");
	ctx.nested += 1;
	return 1;
}

static int
cg_invert(uint32_t index)
{
	uint32_t from = c3nodechild(ctx.c3, index, 0);
	uint32_t count = ctx.counter;
	uint32_t i;
	ctx.counter += 1;
	for(i = 0; i < ctx.indent; i += 1)
		fprintf(ctx.out, "\t");
	fprintf(ctx.out, "bool __zyg_tmp_%d = !", count);
	if(!cg_value(from))
		return 0;
	fprintf(ctx.out, ";\n");
	return cg_push_reg(index, count);
}

static int
cg_set(uint32_t index)
{
	ERROR("TODO: codegen IR_SET", 0);
	return 0;
}		

static int
cg_return(uint32_t index)
{
	uint32_t i;
	uint32_t val;
	for(i = 0; i < ctx.indent; i += 1)
		fprintf(ctx.out, "\t");
	fprintf(ctx.out, "return");
	val = c3nodechild(ctx.c3, index, 1);
	if(c3nodetag(ctx.c3, val) != C3_VOID){
		fprintf(ctx.out, " ");
		if(!cg_value(val)){
			ERROR("Failed to gen return value", 0);
			return 0;
		}
	}
	fprintf(ctx.out, ";\n");
	return 1;
}

static int
cg_break(uint32_t index)
{
	uint32_t block;
	// id
	block = c3nodechild(ctx.c3, index, 0);
	if(block >= stb_sb_count(ctx.blocks)){
		ERROR("ICE: invalid break block", 0);
		return 0;
	}
	if(c3nodekids(ctx.c3, index) == 2){
		ERROR("TODO: break values", 0);
		return 0;
	}
	ERROR("TODO", 0);
	return 0;
}

static int
cg_arg(uint32_t index)
{
	ERROR("TODO: function parameters", 0);
	return 0;
}

static int
cg_panic(uint32_t index)
{
	uint32_t i;
	uint32_t arg = c3nodechild(ctx.c3, index, 0);
	uint32_t str = c3nodechild(ctx.c3, arg, 0);
	char *buf = h_c3str(ctx.c3, str);
	for(i = 0; i < ctx.indent; i += 1)
		fprintf(ctx.out, "\t");
	fprintf(ctx.out, "fprintf(stderr, \"%%s:%%d: %s\", __FILE__, __LINE__);\n", buf);
	for(i = 0; i < ctx.indent; i += 1)
		fprintf(ctx.out, "\t");
	fprintf(ctx.out, "abort();\n");
	free(buf);
	return 1;
}

static int
cg_container(uint32_t index)
{
	// Allocate space for each member of the container

		ERROR("TODO: containers", 0);
		return 0;
}

static int
cg_inst(uint32_t index)
{
	uint32_t i;
	c3tag tag = c3nodetag(ctx.c3, index);
	switch(tag){
	case C3_IR_RELOOP:
		for(i = 0; i < ctx.indent; i += 1)
			fprintf(ctx.out, "\t");
		fprintf(ctx.out, "continue;\n");
		return 1;
	case C3_BLOCK:
		return cg_block(index);
	case C3_IR_CALL:
		return cg_call(index);
	case C3_IR_SET:
		return cg_set(index);
	case C3_ASM:
		return cg_asm(index);
	case C3_IR_RETURN:
		return cg_return(index);
	case C3_IR_BREAK:
		return cg_break(index);
	case C3_IR_ARG:
		return cg_arg(index);
	case C3_IR_CONTAINER:
		return cg_container(index);
	case C3_IR_PANIC:
		return cg_panic(index);
	case C3_IR_INVERT:
		return cg_invert(index);
	case C3_IR_COND_BR:
		return cg_cond_br(index);
	default:
		ERROR("TODO: codegen inst tag %s", c3tagstr(tag));
		return 0;
	}
}

static int
cg_block(uint32_t index)
{
	uint32_t i;
	uint32_t block;
	uint32_t prev_block;
	uint32_t prev_nested = ctx.nested;
	uint32_t last;
	uint16_t kids = c3nodekids(ctx.c3, index);
	if(kids == 0)
		return 1;
	last = c3nodechild(ctx.c3, index, kids - 1);
	prev_block = ctx.current.block;
	block = ctx.next_block;
	ctx.next_block += 1;
	ctx.nested = 0;
	ctx.current.block = block;
	if(!stb_sb_push_u32(&ctx.blocks, index)){
		ERROR("ICE: OOM", 0);
		return 0;
	}
	for(i = 0; i < ctx.indent; i += 1)
		fprintf(ctx.out, "\t");
	if(c3nodetag(ctx.c3, last) == C3_IR_RELOOP){
		ctx.current.is_loop = 1;
		fprintf(ctx.out, "while(true)");
	}
	fprintf(ctx.out, "{\n/* block %d */\n", block);
	ctx.indent += 1;
	for(i = 0; i < c3nodekids(ctx.c3, index); i += 1)
		if(!cg_inst(c3nodechild(ctx.c3, index, i)))
			return 0;
	ctx.current.block = prev_block;
	ctx.indent -= 1;
	for(i = 0; i < ctx.indent; i += 1)
		fprintf(ctx.out, "\t");
	for(i = 0; i < ctx.nested + 1; i += 1)
		fprintf(ctx.out, "}\n");
	ctx.nested = prev_nested;
	return 1;
}

static int
cg_fn(uint32_t index)
{
	uint32_t i;
	uint32_t fntype = stbds_hmget(ctx.c3.analysis.types, index);
	uint32_t rettype = c3nodechild(ctx.c3, fntype, 0);
	uint32_t params = c3nodechild(ctx.c3, fntype, 1);
	uint16_t param_count = c3nodekids(ctx.c3, params);
	int anykids = 0;
	uint32_t param;
	char *buf;
	if(rettype == -1)
		FATAL("oops", 0);
	ctx.blocks = NULL;
	ctx.counter = 0;
	ctx.regalloc.node = NULL;
	ctx.regalloc.tmp = NULL;
	ctx.current.name = h_c3str(ctx.c3, stbds_hmget(ctx.c3.analysis.names, index));
	if(!cg_type(rettype))
		return 0;
	fprintf(ctx.out, "\n%s(", ctx.current.name);
	for(i = 0; i < param_count; i += 1){
		param = c3nodechild(ctx.c3, params, i);
		if(!cg_type(c3nodechild(ctx.c3, param, 1)))
			return 0;
		buf = h_c3str(ctx.c3, c3nodechild(ctx.c3, c3nodechild(ctx.c3, param, 0), 0));
		if(buf == NULL){
			ERROR("Failed to read param name", 0);
			return 0;
		}
		fprintf(ctx.out, " %s", buf);
		free(buf);
	}
	if(param_count == 0)
		fprintf(ctx.out, "void");
	fprintf(ctx.out, ")\n");
	free(ctx.current.name);
	ctx.current.fn = index;
	ctx.indent = 0;
	ctx.current.block = 0;
	ctx.next_block = 0;
	for(i = 0; i < c3nodekids(ctx.c3, index) - 1; i += 1){
		if(c3nodekids(ctx.c3, c3nodechild(ctx.c3, index, i)) != 0)
			anykids = 1;
		if(!cg_block(c3nodechild(ctx.c3, index, i)))
			return 0;
	}
	if(anykids == 0)
		fprintf(ctx.out, "{}\n");
	fprintf(ctx.out, "\n");
	return 1;
}

int
codegen_c(c3ctx c3, FILE *file)
{
	uint32_t i;
	uint32_t index;
	uint16_t nodes;
	c3tag tag;
	nodes = stb_sb_count(c3.analysis.fns);
	ctx.c3 = c3;
	ctx.out = file;
	fprintf(ctx.out, "#include <stdio.h>\n#include <stdbool.h>\n#define zyg_noreturn void\n\n");
	for(i = 0; i < nodes; i += 1){
		index = c3.analysis.fns[i];
		tag = c3nodetag(ctx.c3, index);
		switch(tag){
		case C3_VALUE_FN:
			ERROR("ICE: received function value in codegen (node %d of %d) - should have been lowered to IR by semantic extraction!", i + 1, nodes);
			return 0;
		case C3_IR_FN:
			if(!cg_fn(index))
				return 0;
			break;
		case C3_BAD:
			ERROR("ICE: unable to read child node %d of call graph", i);
			return 0;
		default:
			ERROR("TODO: codegen root node type %s (node %d of %d)", c3tagstr(tag), i + 1, nodes);
			return 0;
		}
	}
	return 1;
}