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


enum { SLong, SWord, SShort, SByte };
static char *rsub[][4] = {
	[RXX] = {"BLACK CAT", "BROKEN MIRROR", "666", "NOOOO!"},
	[RAX] = {"rax", "eax", "ax", "al"},
	[RBX] = {"rbx", "ebx", "bx", "bl"},
	[RCX] = {"rcx", "ecx", "cx", "cl"},
	[RDX] = {"rdx", "edx", "dx", "dl"},
	[RSI] = {"rsi", "esi", "si", "sil"},
	[RDI] = {"rdi", "edi", "di", "dil"},
	[RBP] = {"rbp", "ebp", "bp", "bpl"},
	[RSP] = {"rsp", "esp", "sp", "spl"},
	[R8 ] = {"r8" , "r8d", "r8w", "r8b"},
	[R9 ] = {"r9" , "r9d", "r9w", "r9b"},
	[R10] = {"r10", "r10d", "r10w", "r10b"},
	[R11] = {"r11", "r11d", "r11w", "r11b"},
	[R12] = {"r12", "r12d", "r12w", "r12b"},
	[R13] = {"r13", "r13d", "r13w", "r13b"},
	[R14] = {"r14", "r14d", "r14w", "r14b"},
	[R15] = {"r15", "r15d", "r15w", "r15b"},
};

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

static char *
rtoa(int r)
{
	if (r < EAX)
		return rsub[r][SLong];
	else
		return rsub[RBASE(r)][SWord];
}

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
econ(Con *c, FILE *f)
{
	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("econ: invalid constant");
	}
}

static void
eref(Ref r, Fn *fn, FILE *f)
{
	switch (rtype(r)) {
	case RReg:
		fprintf(f, "%%%s", rtoa(r.val));
		break;
	case RSlot:
		fprintf(f, "-%d(%%rbp)", 8 * r.val);
		break;
	case RCon:
		fprintf(f, "$");
		econ(&fn->con[r.val], f);
		break;
	default:
		diag("emitref: invalid reference");
	}
}

static void
emem(Ref r, Fn *fn, FILE *f)
{
	switch (rtype(r)) {
	default:
		diag("emem: invalid memory reference");
	case RSlot:
		eref(r, fn, f);
		break;
	case RCon:
		econ(&fn->con[r.val], f);
		break;
	case RReg:
		assert(r.val < EAX);
		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",
	};
	static char *stoa[] = {
		[OStorel - OStorel] = "q",
		[OStorew - OStorel] = "l",
		[OStores - OStorel] = "w",
		[OStoreb - OStorel] = "b",
	};
	int r;

	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 OStorel:
	case OStorew:
	case OStores:
	case OStoreb:
		fprintf(f, "\tmov%s ", stoa[i.op - OStorel]);
		if (rtype(i.arg[0]) == RReg) {
			r = RBASE(i.arg[0].val);
			fprintf(f, "%%%s", rsub[r][i.op - OStorel]);
		} else
			eref(i.arg[0], fn, f);
		fprintf(f, ", ");
		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]);
		if (i.to.val < EAX)
			fprintf(f, "q ");
		else
			fprintf(f, "l ");
		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[RBASE(i.to.val)][SByte]);
			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)");
		}
	}

}