git @ Cat's Eye Technologies Bhuna / master src / lib / icode.c
master

Tree @master (Download .tar.gz)

icode.c @masterraw · history · 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
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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>

#include "mem.h"
#include "icode.h"
#include "ast.h"
#include "vm.h"
#include "builtin.h"
#include "value.h"
#include "closure.h"
#include "utf8.h"

/*** iprograms ***/

struct iprogram	*
iprogram_new(void)
{
	struct iprogram *ip;

	ip = bhuna_malloc(sizeof(struct iprogram));
	ip->head = NULL;
	ip->tail = NULL;

	return(ip);
}
	
void
iprogram_free(struct iprogram *ip)
{
	struct icode *ic, *next;

	if (ip == NULL) return;

	for (ic = ip->head; ic != NULL; ic = next) {
		next = ic->next;
		icode_free(NULL, ic);
	}

	bhuna_free(ip);
}

/*** icodes ***/

/*
static void
iprogram_append(struct iprogram *ip, struct icode *ic)
{
	ic->next = NULL;
	ic->prev = ip->tail;

	if (ip->tail == NULL) {
		ip->head = ic;
		ip->tail = ic;
	} else {
		ip->tail->next = ic;
		ip->tail = ic;
	}

	return(ic);
}
*/

struct icode *
icode_new(struct iprogram *ip, int opcode)
{
	struct icode *ic;

	ic = bhuna_malloc(sizeof(struct icode));
	ic->opcode = opcode;
	ic->referrers = NULL;
	ic->label = NULL;

	/* leave operand unitialized */

	/* append to program */
	ic->next = NULL;
	ic->prev = ip->tail;

	if (ip->tail == NULL) {
		ip->head = ic;
		ip->tail = ic;
		ic->number = 1;
	} else {
		ip->tail->next = ic;
		ip->tail = ic;
		ic->number = ic->prev->number + 1;
	}

	return(ic);
}

struct icode *
icode_new_local(struct iprogram *ip, int opcode, int index, int upcount)
{
	struct icode *ic;

	ic = icode_new(ip, opcode);
	ic->operand.local.index = index;
	ic->operand.local.upcount = upcount;

	return(ic);
}

struct icode *
icode_new_value(struct iprogram *ip, int opcode, struct value value)
{
	struct icode *ic;

	ic = icode_new(ip, opcode);
	ic->operand.value = value;

	return(ic);
}

struct icode *
icode_new_builtin(struct iprogram *ip, struct builtin *bi)
{
	struct icode *ic;

	ic = icode_new(ip, INSTR_EXTERNAL);

	if (bi->index >= 0) {
		ic->opcode = bi->index;
	} else {
		ic->operand.builtin = bi;
	}

	return(ic);
}

/*** destructor ***/

/*
 * Since this might be done while optimizing, take care to leave the
 * rest of the program intact.
 */
void
icode_free(struct iprogram *ip, struct icode *ic)
{
	struct icomefrom *icf, *icf_next;

	if (ic == NULL) return;

	if (ic->opcode == INSTR_JMP || ic->opcode == INSTR_JZ) {
		referrer_unwire(ic, ic->operand.branch);
	}

	for (icf = ic->referrers; icf != NULL; icf = icf_next) {
		icf_next = icf->next;
		bhuna_free(icf);
	}

	if (ip != NULL) {
		if (ip->head == ic)
			ip->head = ic->next;
		else if (ic->prev != NULL)
			ic->prev->next = ic->next;

		if (ip->tail == ic)
			ip->tail = ic->prev;
		else if (ic->next != NULL)
			ic->next->prev = ic->prev;
	}

	bhuna_free(ic);
}

/*** util ***/

void
icode_set_branch(struct icode *ic, struct icode *branch)
{
	struct icomefrom *icf;

	ic->operand.branch = branch;
	icf = bhuna_malloc(sizeof(struct icomefrom));
	icf->type = ICOMEFROM_ICODE;
	icf->referrer.icode = ic;
	icf->next = branch->referrers;
	branch->referrers = icf;
}

void
icode_set_closure_entry_point(struct closure *k, struct icode *ic)
{
	struct icomefrom *icf;

	k->icode = ic;
	icf = bhuna_malloc(sizeof(struct icomefrom));
	icf->type = ICOMEFROM_CLOSURE;
	icf->referrer.closure = k;
	icf->next = ic->referrers;
	ic->referrers = icf;
}

/*************** intermediate code generator ****************/

static void
ast_gen_r(struct iprogram *ip, struct ast *a)
{
	struct icode *ic1, *ic2, *ic3, *ic4;
	struct value v;

	if (a == NULL)
		return;

	/* a->icode = gptr; */
	switch (a->type) {
	case AST_LOCAL:
		icode_new_local(ip, INSTR_PUSH_LOCAL,
		    a->u.local.index, a->u.local.upcount);
		break;
	case AST_VALUE:
		icode_new_value(ip, INSTR_PUSH_VALUE, a->u.value.value);
		if (a->u.value.value.type == VALUE_CLOSURE) {
			icode_new(ip, INSTR_SET_ACTIVATION);
			ic1 = icode_new(ip, INSTR_JMP);
			ic2 = icode_new(ip, INSTR_NOP);
			icode_set_closure_entry_point(a->u.value.value.v.s->v.k, ic2);
			ast_gen_r(ip, a->u.value.value.v.s->v.k->ast);
			icode_new(ip, INSTR_RET);
			ic3 = icode_new(ip, INSTR_NOP);
			icode_set_branch(ic1, ic3);
		}
		break;
	case AST_BUILTIN:
		if (a->u.builtin.bi->index == INDEX_BUILTIN_STORE) {
			struct ast *lvalue;
			
			lvalue = ast_find_local(a);
			icode_new_local(ip, INSTR_COW_LOCAL,
			    lvalue->u.local.index, lvalue->u.local.upcount);
		}
		ast_gen_r(ip, a->u.builtin.right);
		if (a->u.builtin.bi->arity == -1) {
			v = value_new_integer(ast_count_args(a->u.builtin.right));
			value_deregister(v);
			icode_new_value(ip, INSTR_PUSH_VALUE, v);
		}
		icode_new_builtin(ip, a->u.builtin.bi);
		break;
	case AST_APPLY:
		ast_gen_r(ip, a->u.apply.right);
		ast_gen_r(ip, a->u.apply.left);
		icode_new(ip, INSTR_CALL);
		break;
	case AST_ROUTINE:
		ast_gen_r(ip, a->u.routine.body);
		break;
	case AST_ARG:
		ast_gen_r(ip, a->u.arg.left);
		ast_gen_r(ip, a->u.arg.right);
		break;
	case AST_STATEMENT:
		ast_gen_r(ip, a->u.statement.left);
		ast_gen_r(ip, a->u.statement.right);
		break;
	case AST_ASSIGNMENT:
		assert(a->u.assignment.left != NULL);
		assert(a->u.assignment.left->type == AST_LOCAL);
		ast_gen_r(ip, a->u.assignment.right);
		if (a->u.assignment.defining) {
			icode_new_local(ip, INSTR_INIT_LOCAL,
			    a->u.assignment.left->u.local.index,
			    a->u.assignment.left->u.local.upcount);
		} else {
			icode_new_local(ip, INSTR_POP_LOCAL,
			    a->u.assignment.left->u.local.index,
			    a->u.assignment.left->u.local.upcount);
		}
		break;
	case AST_CONDITIONAL:
		ast_gen_r(ip, a->u.conditional.test);
		ic1 = icode_new(ip, INSTR_JZ);
		ast_gen_r(ip, a->u.conditional.yes);
		if (a->u.conditional.no != NULL) {
			ic2 = icode_new(ip, INSTR_JMP);
			ic3 = icode_new(ip, INSTR_NOP);
			icode_set_branch(ic1, ic3);
			ast_gen_r(ip, a->u.conditional.no);
			ic4 = icode_new(ip, INSTR_NOP);
			icode_set_branch(ic2, ic4);
		} else {
			ic3 = icode_new(ip, INSTR_NOP);
			icode_set_branch(ic1, ic3);
		}
		break;
	case AST_WHILE_LOOP:
		ic1 = icode_new(ip, INSTR_NOP);
		ast_gen_r(ip, a->u.while_loop.test);
		ic2 = icode_new(ip, INSTR_JZ);
		ast_gen_r(ip, a->u.while_loop.body);
		ic3 = icode_new(ip, INSTR_JMP);
		icode_set_branch(ic3, ic1);
		ic4 = icode_new(ip, INSTR_NOP);
		icode_set_branch(ic2, ic4);
		break;
	case AST_RETR:
		ast_gen_r(ip, a->u.retr.body);
		icode_new(ip, INSTR_RET);
		break;
	}
}

struct iprogram *
ast_gen_iprogram(struct ast *a)
{
	struct iprogram *ip;

	ip = iprogram_new();
	ast_gen_r(ip, a);
	icode_new(ip, INSTR_HALT);

	return(ip);
}

char *
icode_addr(struct icode *ic, vm_label_t program)
{
	static char s[256];

	if (program == NULL) {
		snprintf(s, 256, "No.%d", ic->number);
	} else {
		snprintf(s, 256, "#%3d", ic->label - program);
	}

	return(s);
}
	
void
iprogram_dump(struct iprogram *ip, vm_label_t program)
{
	struct icode *ic;
	struct icomefrom *icf;

	for (ic = ip->head; ic != NULL; ic = ic->next) {
		printf("%s: ", icode_addr(ic, program));
		if (ic->referrers != NULL) {
			printf("{");
			for (icf = ic->referrers; icf != NULL; icf = icf->next) {
				if (icf->type == ICOMEFROM_ICODE) {
					printf("%s", icode_addr(icf->referrer.icode, program));
				} else {
					printf("#(closure)");
				}
				if (icf->next != NULL)
					printf(", ");
			}
			printf("} ");
		}
		switch (ic->opcode) {
		case INSTR_HALT:
			printf("HALT");
			break;
		case INSTR_PUSH_ZERO:
			printf("PUSH_ZERO");
			break;
		case INSTR_PUSH_ONE:
			printf("PUSH_ONE");
			break;
		case INSTR_PUSH_TWO:
			printf("PUSH_TWO");
			break;
		case INSTR_PUSH_VALUE:
			printf("PUSH_VALUE ");
			value_print(ic->operand.value);
			break;
		case INSTR_PUSH_LOCAL:
			printf("PUSH_LOCAL (%d,%d)",
			    ic->operand.local.index, ic->operand.local.upcount);
			break;
		case INSTR_POP_LOCAL:
			printf("POP_LOCAL (%d,%d)",
			    ic->operand.local.index, ic->operand.local.upcount);
			break;
		case INSTR_INIT_LOCAL:
			printf("INIT_LOCAL (%d,%d)",
			    ic->operand.local.index, ic->operand.local.upcount);
			break;
		case INSTR_JZ:
			printf("JZ %s", icode_addr(ic->operand.branch, program));
			break;
		case INSTR_JMP:
			printf("JMP %s", icode_addr(ic->operand.branch, program));
			break;
		case INSTR_CALL:
			printf("CALL");
			break;
		case INSTR_RET:
			printf("RET");
			break;
		case INSTR_GOTO:
			printf("GOTO");
			break;
		case INSTR_SET_ACTIVATION:
			printf("SET_ACTIVATION");
			break;
		case INSTR_COW_LOCAL:
			printf("COW_LOCAL (%d,%d)",
			    ic->operand.local.index, ic->operand.local.upcount);
		case INSTR_EXTERNAL:
			printf("EXTERNAL `"),
			fputsu8(stdout, ic->operand.builtin->name);
			printf("'");
			break;
		case INSTR_NOP:
			printf("NOP");
			break;

		default:
			printf("BUILTIN `");
			fputsu8(stdout, builtins[ic->opcode].name);
			printf("'");
		}
		printf("\n");
	}
}

/*** rewiring ***/

void
referrer_unwire(struct icode *from, struct icode *to)
{
	struct icomefrom *icf, *icf_next, *icf_prev;

	icf_prev = NULL;
	for (icf = to->referrers; icf != NULL; icf = icf_next) {
		icf_next = icf->next;
		if (icf->type == ICOMEFROM_ICODE &&
		    icf->referrer.icode == from) {
			if (icf_prev != NULL)
				icf_prev->next = icf_next;
			else
				to->referrers = icf_next;
			bhuna_free(icf);
		} else {
			icf_prev = icf;
		}
	}
}

void
referrers_rewire(struct icode *from, struct icode *to)
{
	struct icomefrom *icf, *icf_next;

	for (icf = from->referrers; icf != NULL; icf = icf_next) {
		icf_next = icf->next;
		if (icf->type == ICOMEFROM_ICODE) {
			icf->referrer.icode->operand.branch = to;
		} else {
			icf->referrer.closure->icode = to;
		}
		icf->next = to->referrers;
		to->referrers = icf;
	}
	from->referrers = NULL;
}

/********** OPTIMIZATIONS **********/

void
iprogram_eliminate_nops(struct iprogram *ip)
{
	struct icode *ic, *ic_next;

	for (ic = ip->head; ic != NULL; ic = ic_next) {
		ic_next = ic->next;
		if (ic->opcode == INSTR_NOP) {
			assert(ic->next != NULL);
			referrers_rewire(ic, ic->next);
			icode_free(ip, ic);
		}
	}
}

void
iprogram_eliminate_dead_code(struct iprogram *ip)
{
	struct icode *ic, *ic_next;
	int dead = 0;

	for (ic = ip->head; ic != NULL; ic = ic_next) {
		ic_next = ic->next;
		/* pre */
		if (ic->referrers != NULL) {
			dead = 0;
		}
		/* in */
		if (dead) {
			/*printf("#%3d is DEAD\n", ic->number);*/
			icode_free(ip, ic);
			/*ic = ip->head;
			dead = 0;
			continue;*/
		}

		/* post */
		if (ic->opcode == INSTR_JMP || ic->opcode == INSTR_RET ||
		    ic->opcode == INSTR_GOTO || ic->opcode == INSTR_HALT) {
			dead = 1;
		}
	}
}

void
iprogram_optimize_tail_calls(struct iprogram *ip)
{
	struct icode *ic, *ic_next;

	for (ic = ip->head; ic != NULL; ic = ic_next) {
		ic_next = ic->next;
		if (ic->opcode == INSTR_RET && ic->referrers == NULL &&
		    ic->prev != NULL && ic->prev->opcode == INSTR_CALL) {
			ic->prev->opcode = INSTR_GOTO;
			icode_free(ip, ic);
		}
	}
}

void
iprogram_optimize_push_small_ints(struct iprogram *ip)
{
	struct icode *ic, *ic_next;

	for (ic = ip->head; ic != NULL; ic = ic_next) {
		ic_next = ic->next;
		if (ic->opcode == INSTR_PUSH_VALUE &&
		    ic->operand.value.type == VALUE_INTEGER) {
			switch (ic->operand.value.v.i) {
			case 0:
				ic->opcode = INSTR_PUSH_ZERO;
				break;
			case 1:
				ic->opcode = INSTR_PUSH_ONE;
				break;
			case 2:
				ic->opcode = INSTR_PUSH_TWO;
				break;
			}
		}
	}
}

int
count_referrers(struct icode *ic)
{
	struct icomefrom *icf;
	int i = 0;

	for (icf = ic->referrers; icf != NULL; icf = icf->next)
		i++;

	return(i);
}

void
iprogram_eliminate_useless_jumps(struct iprogram *ip)
{
	struct icode *ic, *ic_next;

	for (ic = ip->head; ic != NULL; ic = ic_next) {
		ic_next = ic->next;
		if (ic->opcode == INSTR_JMP &&
		    ic->operand.branch->opcode == INSTR_RET) {
			referrer_unwire(ic, ic->operand.branch);
			ic->opcode = INSTR_RET;
		}
	}
}