aboutsummaryrefslogtreecommitdiff
path: root/ir.c
blob: cf529dd5be01cf8d94fba636069c13276cfdb337 (plain)
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
#include <stdlib.h>
#include <assert.h>
#include "ir.h"


struct ir* ir_make(void) {
	struct ir *ir = malloc(sizeof(struct ir));
	ir->cap = 32;
	ir->len = 0;
	ir->inss = malloc(ir->cap * sizeof(struct irins*));
	ir->globspace = 0;
	return ir;
}

void ir_delete(struct ir *ir) {
	for (int i = 0; i < ir->len; i++) {
		irins_delete(ir->inss[i]);
	}
	free(ir->inss);
	free(ir);
}

void ir_print(struct ir *ir, FILE *f) {
	fprintf(f, "IR [globspace=%d]\n", ir->globspace);
	for (int i = 0; i < ir->len; i++) {
		irins_print(ir->inss[i], f);
	}
}

static const char* condcode_show(enum condcode condcode) {
	switch (condcode) {
		case CCZ: return "z";
		case CCNZ: return "nz";
		case CCL: return "l";
		case CCG: return "g";
		case CCLE: return "le";
		case CCGE: return "ge";
		default: assert(false);
	}
}

static const char* ref_show(struct ref ref) {
	static char cbuf[3][16];
	static int cbufi = 0;
	static const char *special[8] = {"A", "B", "C", "D", "X", "Y", "SP", "BP"};

	const char *suffix;
	if (ref.rel == REFREL_ZERO) suffix = "";
	else if (ref.rel == REFREL_DATA) suffix = ":data";
	else if (ref.rel == REFREL_HEAP) suffix = ":heap";
	else assert(false);

	switch (ref.type) {
		case REF_REG:
			if (ref.reg == REG_UNUSED) return "<<UNUSED?>>";
			if (ref.reg < 0) return special[-(ref.reg - REG_A)];
			cbufi = (cbufi + 1) % 3;
			sprintf(cbuf[cbufi], "t%d", ref.reg);
			return cbuf[cbufi];

		case REF_MEM:
			cbufi = (cbufi + 1) % 3;
			if (ref.reg == REG_UNUSED) {
				sprintf(cbuf[cbufi], "[%d%s]", ref.offset, suffix);
			} else if (ref.reg < 0) {
				sprintf(cbuf[cbufi], "[%s + %d%s]", special[-(ref.reg - REG_A)], ref.offset, suffix);
			} else {
				sprintf(cbuf[cbufi], "[t%d + %d%s]", ref.reg, ref.offset, suffix);
			}
			return cbuf[cbufi];

		case REF_IMM:
			cbufi = (cbufi + 1) % 3;
			sprintf(cbuf[cbufi], "%d", ref.imm);
			return cbuf[cbufi];

		default:
			assert(false);
	}
}

void irins_print(struct irins *ins, FILE *f) {
	switch (ins->type) {
		case INS_ADD: fprintf(f, "\t%s <- %s + %s\n", ref_show(ins->r0), ref_show(ins->r1), ref_show(ins->r2)); break;
		case INS_SUB: fprintf(f, "\t%s <- %s - %s\n", ref_show(ins->r0), ref_show(ins->r1), ref_show(ins->r2)); break;
		case INS_MUL: fprintf(f, "\t%s <- %s * %s\n", ref_show(ins->r0), ref_show(ins->r1), ref_show(ins->r2)); break;
		case INS_DIV: fprintf(f, "\t%s <- %s / %s\n", ref_show(ins->r0), ref_show(ins->r1), ref_show(ins->r2)); break;
		case INS_MOD: fprintf(f, "\t%s <- %s %% %s\n", ref_show(ins->r0), ref_show(ins->r1), ref_show(ins->r2)); break;
		case INS_NEG: fprintf(f, "\t%s <- -%s\n", ref_show(ins->r0), ref_show(ins->r1)); break;
		case INS_NOT: fprintf(f, "\t%s <- !%s\n", ref_show(ins->r0), ref_show(ins->r1)); break;
		case INS_TEST: fprintf(f, "\ttest %s, %s\n", ref_show(ins->r1), ref_show(ins->r2)); break;
		case INS_PUSH: fprintf(f, "\tpush %s\n", ref_show(ins->r1)); break;
		case INS_POP: fprintf(f, "\t%s = pop\n", ref_show(ins->r0)); break;
		case INS_CMP: fprintf(f, "\tcmp %s, %s\n", ref_show(ins->r1), ref_show(ins->r2)); break;
		case INS_LBL: fprintf(f, "%s:\n", ins->name); break;
		case INS_JMP: fprintf(f, "\tjmp %s\n", ins->name); break;
		case INS_JCC: fprintf(f, "\tj%s %s\n", condcode_show(ins->condcode), ins->name); break;
		case INS_CALL: fprintf(f, "\tcall %s\n", ins->name); break;
		case INS_CALLV: fprintf(f, "\t%s <- call %s\n", ref_show(ins->r0), ins->name); break;
		case INS_RET: fprintf(f, "\tret\n"); break;
		case INS_RETV: fprintf(f, "\tret %s\n", ref_show(ins->r1)); break;
		case INS_MOV: fprintf(f, "\t%s <- %s\n", ref_show(ins->r0), ref_show(ins->r1)); break;
		default: assert(false);
	}
}

struct ref ir_reserve_global(struct ir *ir, int size) {
	int offset = ir->globspace;
	ir->globspace += size;
	return ref_mem(REG_UNUSED, offset, REFREL_DATA);
}

void ir_append(struct ir *ir, struct irins *ins) {
	if (ir->len == ir->cap) {
		ir->cap *= 2;
		ir->inss = realloc(ir->inss, ir->cap * sizeof(struct irins*));
	}
	ir->inss[ir->len++] = ins;
}

struct irins* irins_make(enum instype type) {
	struct irins *ins = malloc(sizeof(struct irins));
	ins->type = type;
	ins->name = NULL;
	return ins;
}

struct irins* irins_make_name(enum instype type, char *name) {
	struct irins *ins = irins_make(type);
	ins->name = name;
	return ins;
}

struct irins* irins_make_0(enum instype type, struct ref r0) {
	struct irins *ins = irins_make(type);
	ins->r0 = r0;
	return ins;
}

struct irins* irins_make_01(enum instype type, struct ref r0, struct ref r1) {
	struct irins *ins = irins_make(type);
	ins->r0 = r0;
	ins->r1 = r1;
	return ins;
}

struct irins* irins_make_012(enum instype type, struct ref r0, struct ref r1, struct ref r2) {
	struct irins *ins = irins_make_01(type, r0, r1);
	ins->r2 = r2;
	return ins;
}

struct irins* irins_make_1(enum instype type, struct ref r1) {
	struct irins *ins = irins_make(type);
	ins->r1 = r1;
	return ins;
}

struct irins* irins_make_12(enum instype type, struct ref r1, struct ref r2) {
	struct irins *ins = irins_make(type);
	ins->r1 = r1;
	ins->r2 = r2;
	return ins;
}

struct irins* irins_make_jcc(char *name, enum condcode condcode) {
	struct irins *ins = irins_make(INS_JCC);
	ins->name = name;
	ins->condcode = condcode;
	return ins;
}

void irins_delete(struct irins *ins) {
	if (ins->name) free(ins->name);
	free(ins);
}

char* gen_label_name(void) {
	static int next = 0;
	char *buf = malloc(16);
	sprintf(buf, "L%d", next++);
	return buf;
}

struct ref ref_reg(int reg) {
	return (struct ref){REF_REG, reg, 0, REFREL_ZERO, 0};
}

struct ref ref_mem(int reg, int offset, enum refrel rel) {
	return (struct ref){REF_MEM, reg, offset, rel, 0};
}

struct ref ref_imm(int imm) {
	return (struct ref){REF_IMM, REG_UNUSED, 0, REFREL_ZERO, imm};
}

struct ref ref_next_register(void) {
	static int next = 0;
	return ref_reg(next++);
}