summary refs log tree commit diff
path: root/lisc/emit.c
blob: 2e762091254438435d5f059d2a1201863fc679b7 (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
#include "lisc.h"


static char *rtoa[] = {
	[RXX] = "OH GOD!",
	[RAX] = "rax",
	[RBX] = "rbx",
	[RCX] = "rcx",
	[RDX] = "rdx",
	[RSI] = "rsi",
	[RDI] = "rdi",
	[RBP] = "rbp",
	[RSP] = "rsp",
	[R8] = "r8",
	[R9] = "r9",
	[R10] = "r10",
	[R11] = "r11",
	[R12] = "r12",
	[R13] = "r13",
	[R14] = "r14",
	[R15] = "r15",
	[EAX] = "eax",
	[EBX] = "ebx",
	[ECX] = "ecx",
	[EDX] = "edx",
	[ESI] = "esi",
	[EDI] = "edi",
	[R8D] = "r8d",
	[R9D] = "r9d",
	[R10D] = "r10d",
	[R11D] = "r11d",
	[R12D] = "r12d",
	[R13D] = "r13d",
	[R14D] = "r14d",
	[R15D] = "r15d",
};

static struct { char *s, *b; } rsub[] = {
	[RXX] = {"OH GOD!", "OH NO!"},
	[RAX] = {"ax", "al"},
	[RBX] = {"bx", "bl"},
	[RCX] = {"cx", "cl"},
	[RDX] = {"dx", "dl"},
	[RSI] = {"si", "sil"},
	[RDI] = {"di", "dil"},
	[RBP] = {"bp", "bpl"},
	[RSP] = {"sp", "spl"},
	[R8 ] = {"r8w", "r8b"},
	[R9 ] = {"r9w", "r9b"},
	[R10] = {"r10w", "r10b"},
	[R11] = {"r11w", "r11b"},
	[R12] = {"r12w", "r12b"},
	[R13] = {"r13w", "r13b"},
	[R14] = {"r14w", "r14b"},
	[R15] = {"r15w", "r15b"},
};

static char *ctoa[NCmp] = {
	[Ceq] = "e",
	[Csle] = "le",
	[Cslt] = "l",
	[Csgt] = "g",
	[Csge] = "ge",
	[Cne] = "ne",
};

static int
cneg(int cmp)
{
	switch (cmp) {
	default:   diag("cneg: unhandled comparison");
	case Ceq:  return Cne;
	case Csle: return Csgt;
	case Cslt: return Csge;
	case Csgt: return Csle;
	case Csge: return Cslt;
	case Cne:  return Ceq;
	}
}

static void
eref(Ref r, Fn *fn, FILE *f)
{
	Con *c;

	switch (rtype(r)) {
	case RReg:
		fprintf(f, "%%%s", rtoa[r.val]);
		break;
	case RSlot:
		fprintf(f, "-%d(%%rbp)", 8 * r.val);
		break;
	case RCon:
		c = &fn->con[r.val];
		switch (c->type) {
		case CAddr:
			fprintf(f, "$%s", c->label);
			if (c->val)
				fprintf(f, "%+"PRId64, c->val);
			break;
		case CNum:
			fprintf(f, "$%"PRId64, c->val);
			break;
		default:
			diag("emitref: invalid constant");
		}
		break;
	default:
		diag("emitref: invalid reference");
	}
}

static void
emem(Ref r, Fn *fn, FILE *f)
{
	if (rtype(r) == RSlot)
		eref(r, fn, f);
	else {
		assert(rtype(r)!=RReg || BASE(r.val)==r.val);
		fprintf(f, "(");
		eref(r, fn, f);
		fprintf(f, ")");
	}
}

static void
eop(char *op, Ref a, Ref b, Fn *fn, FILE *f)
{
	fprintf(f, "\t%s ", op);
	eref(a, fn, f);
	if (!req(b, R)) {
		fprintf(f, ", ");
		eref(b, fn, f);
	}
	fprintf(f, "\n");
}

static void
eins(Ins i, Fn *fn, FILE *f)
{
	static char *otoa[NOp] = {
		[OAdd]    = "add",
		[OSub]    = "sub",
		[OLoad]   = "mov",
		[OLoadss] = "movsw",
		[OLoadus] = "movzw",
		[OLoadsb] = "movsb",
		[OLoadub] = "movzb",
	};
	char *s;

	switch (i.op) {
	case OAdd:
	case OSub:
		if (req(i.to, i.arg[1])) {
			if (i.op == OSub) {
				eop("neg", i.to, R, fn, f);
				eop("add", i.arg[0], i.to, fn, f);
				break;
			}
			i.arg[1] = i.arg[0];
			i.arg[0] = i.to;
		}
		if (!req(i.to, i.arg[0]))
			eop("mov", i.arg[0], i.to, fn, f);
		eop(otoa[i.op], i.arg[1], i.to, fn, f);
		break;
	case OCopy:
		if (!req(i.arg[0], i.to))
			eop("mov", i.arg[0], i.to, fn, f);
		break;
	case OStore:
		if (rtype(i.arg[0]) == RCon) {
			fprintf(f, "\tmovl ");
			eref(i.arg[0], fn, f);
			fprintf(f, ", ");
		} else {
			assert(rtype(i.arg[0]) == RReg);
			fprintf(f, "\tmov %%%s, ", rtoa[i.arg[0].val]);
		}
		goto Store;
	case OStores:
		if (rtype(i.arg[0]) == RCon) {
			fprintf(f, "\tmovw ");
			eref(i.arg[0], fn, f);
			fprintf(f, ", ");
		} else {
			assert(rtype(i.arg[0]) == RReg);
			fprintf(f, "\tmovw %%%s, ", rsub[BASE(i.arg[0].val)].s);
		}
		goto Store;
	case OStoreb:
		if (rtype(i.arg[0]) == RCon) {
			fprintf(f, "\tmovb ");
			eref(i.arg[0], fn, f);
			fprintf(f, ", ");
		} else {
			assert(rtype(i.arg[0]) == RReg);
			fprintf(f, "\tmovb %%%s, ", rsub[BASE(i.arg[0].val)].b);
		}
	Store:
		emem(i.arg[1], fn, f);
		fprintf(f, "\n");
		break;
	case OLoad:
	case OLoadss:
	case OLoadus:
	case OLoadsb:
	case OLoadub:
		fprintf(f, "\t%s ", otoa[i.op]);
		emem(i.arg[0], fn, f);
		fprintf(f, ", ");
		eref(i.to, fn, f);
		fprintf(f, "\n");
		break;
	case OSwap:
		eop("xchg", i.arg[0], i.arg[1], fn, f);
		break;
	case OSign:
		if (req(i.to, REG(RDX)) && req(i.arg[0], REG(RAX)))
			fprintf(f, "\tcqto\n");
		else if (req(i.to, REG(EDX)) && req(i.arg[0], REG(EAX)))
			fprintf(f, "\tcltd\n");
		else
			diag("emit: unhandled instruction (2)");
		break;
	case OXDiv:
		eop("idiv", i.arg[0], R, fn, f);
		break;
	case OXCmpw:
	case OXCmpl:
		eop(i.op == OXCmpw ? "cmpl" : "cmpq",
			i.arg[0], i.arg[1], fn, f);
		break;
	case ONop:
		break;
	default:
		if (OXSet <= i.op && i.op <= OXSet1) {
			eop("mov $0,", i.to, R, fn, f);
			fprintf(f, "\tset%s %%%s\n",
				ctoa[i.op-OXSet],
				rsub[BASE(i.to.val)].b);
			break;
		}
		diag("emit: unhandled instruction (3)");
	}
}

void
emitfn(Fn *fn, FILE *f)
{
	Blk *b, *s;
	Ins *i;
	int c;

	fprintf(f,
		".text\n"
		".globl liscf\n"
		".type liscf, @function\n"
		"liscf:\n"
		"\tpush %%rbp\n"
		"\tmov %%rsp, %%rbp\n"
		"\tsub $%u, %%rsp\n",
		fn->nspill * 8
	);
	for (b=fn->start; b; b=b->link) {
		fprintf(f, ".L%s:\n", b->name);
		for (i=b->ins; i-b->ins < b->nins; i++)
			eins(*i, fn, f);
		switch (b->jmp.type) {
		case JRet:
			fprintf(f,
				"\tleave\n"
				"\tret\n"
			);
			break;
		case JJmp:
			if (b->s1 != b->link)
				fprintf(f, "\tjmp .L%s\n", b->s1->name);
			break;
		default:
			c = b->jmp.type - JXJc;
			if (0 <= c && c <= NCmp) {
				if (b->link == b->s2) {
					s = b->s1;
				} else if (b->link == b->s1) {
					c = cneg(c);
					s = b->s2;
				} else
					diag("emit: unhandled jump (1)");
				fprintf(f, "\tj%s .L%s\n", ctoa[c], s->name);
				break;
			}
			diag("emit: unhandled jump (2)");
		}
	}

}