cpp: use a simple hash table instead of tab struct
[neatcc/cc.git] / x86.c
blob4fd0b22218c8f95efdfa8ac8717f6bafa00c67cf
1 #include "tok.h"
2 #include "gen.h"
3 #include "out.h"
5 /* registers */
6 #define R_RAX 0x00
7 #define R_RCX 0x01
8 #define R_RDX 0x02
9 #define R_RBX 0x03
10 #define R_RSP 0x04
11 #define R_RBP 0x05
12 #define R_RSI 0x06
13 #define R_RDI 0x07
15 /* x86 opcodes */
16 #define I_MOV 0x89
17 #define I_MOVI 0xc7
18 #define I_MOVIR 0xb8
19 #define I_MOVR 0x8b
20 #define I_MOVSXD 0x63
21 #define I_SHX 0xd3
22 #define I_CMP 0x3b
23 #define I_TST 0x85
24 #define I_LEA 0x8d
25 #define I_NOT 0xf7
26 #define I_CALL 0xff
27 #define I_MUL 0xf7
28 #define I_XOR 0x33
29 #define I_TEST 0x85
30 #define I_CQO 0x99
31 #define I_PUSH 0x50
32 #define I_POP 0x58
34 #define MIN(a, b) ((a) < (b) ? (a) : (b))
35 #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
37 int tmpregs[] = {0, 1, 2, 6, 7, 3};
38 int argregs[] = {0};
40 #define OP2(o2, o1) (0x010000 | ((o2) << 8) | (o1))
41 #define O2(op) (((op) >> 8) & 0xff)
42 #define O1(op) ((op) & 0xff)
43 #define MODRM(m, r1, r2) ((m) << 6 | (r1) << 3 | (r2))
45 static void putint(char *s, long n, int l)
47 while (l--) {
48 *s++ = n;
49 n >>= 8;
53 static void op_x(int op, int r1, int r2, int bt)
55 int sz = BT_SZ(bt);
56 if (sz == 2)
57 oi(0x66, 1);
58 if (op & 0x10000)
59 oi(O2(op), 1);
60 oi(sz == 1 ? O1(op) & ~0x1 : O1(op), 1);
63 #define op_mr op_rm
65 /* op_*(): r=reg, m=mem, i=imm, s=sym */
66 static void op_rm(int op, int src, int base, int off, int bt)
68 int dis = off == (char) off ? 1 : 4;
69 int mod = dis == 4 ? 2 : 1;
70 if (!off && (base & 7) != R_RBP)
71 mod = 0;
72 op_x(op, src, base, bt);
73 oi(MODRM(mod, src & 0x07, base & 0x07), 1);
74 if ((base & 7) == R_RSP)
75 oi(0x24, 1);
76 if (mod)
77 oi(off, dis);
80 static void op_rr(int op, int src, int dst, int bt)
82 op_x(op, src, dst, bt);
83 oi(MODRM(3, src & 0x07, dst & 0x07), 1);
86 #define movrx_bt(bt) (LONGSZ)
88 static int movrx_op(int bt, int mov)
90 int sz = BT_SZ(bt);
91 if (sz == 2)
92 return OP2(0x0f, bt & BT_SIGNED ? 0xbf : 0xb7);
93 if (sz == 1)
94 return OP2(0x0f, bt & BT_SIGNED ? 0xbe : 0xb6);
95 return mov;
98 static void mov_r2r(int rd, int r1, unsigned bt)
100 if (rd != r1 || BT_SZ(bt) != LONGSZ)
101 op_rr(movrx_op(bt, I_MOVR), rd, r1, movrx_bt(bt));
104 static void mov_m2r(int dst, int base, int off, int bt)
106 op_rm(movrx_op(bt, I_MOVR), dst, base, off, movrx_bt(bt));
109 int i_imm(int op, long imm)
111 if ((op & 0xf0) == 0x20)
112 return 0;
113 return imm <= 127 && imm >= -128;
116 static void i_push(int reg)
118 op_x(I_PUSH | (reg & 0x7), 0, reg, 4);
121 static void i_pop(int reg)
123 op_x(I_POP | (reg & 0x7), 0, reg, 4);
126 void i_mov(int rd, int rn)
128 op_rr(movrx_op(LONGSZ, I_MOVR), rd, rn, movrx_bt(LONGSZ));
131 void i_load(int rd, int rn, int off, int bt)
133 mov_m2r(rd, rn, off, bt);
136 void i_save(int rd, int rn, int off, int bt)
138 op_rm(I_MOV, rd, rn, off, bt);
141 void i_reg(int op, int *rd, int *r1, int *r2, int *tmp)
143 *rd = 0;
144 *r1 = R_TMPS;
145 *r2 = op & O_IMM ? 0 : R_TMPS;
146 *tmp = 0;
147 if ((op & 0xf0) == 0x00) /* add */
148 return;
149 if ((op & 0xf0) == 0x10) { /* shl */
150 if (~op & O_IMM) {
151 *r2 = 1 << R_RCX;
152 *r1 = R_TMPS & ~*r2;
154 return;
156 if ((op & 0xf0) == 0x20) { /* mul */
157 *rd = (op & 0xff) == O_MOD ? (1 << R_RDX) : (1 << R_RAX);
158 *r1 = (1 << R_RAX);
159 *r2 = R_TMPS & ~*rd & ~*r1;
160 if ((op & 0xff) == O_DIV)
161 *r2 &= ~(1 << R_RDX);
162 *tmp = (1 << R_RDX) | (1 << R_RAX);
163 return;
165 if ((op & 0xf0) == 0x30) { /* cmp */
166 *rd = 1 << R_RAX;
167 return;
169 if ((op & 0xf0) == 0x40) { /* uop */
170 *r2 = 0;
171 if ((op & 0xff) == O_LNOT)
172 *r1 = 1 << R_RAX;
173 return;
175 if ((op & 0xf0) == 0x50) { /* etc */
176 if (op == O_MSET) {
177 *rd = 1 << R_RDI;
178 *r1 = 1 << R_RAX;
179 *r2 = 1 << R_RCX;
181 if (op == O_MCPY) {
182 *rd = 1 << R_RDI;
183 *r1 = 1 << R_RSI;
184 *r2 = 1 << R_RCX;
186 if (op == O_SX || op == O_ZX) {
187 *rd = R_TMPS;
188 *r1 = R_BYTE;
189 *r2 = 0;
191 if (op == O_MOV) {
192 *rd = R_TMPS;
193 *r2 = 0;
195 return;
199 static void i_add(int op, int rd, int r1, int r2)
201 /* opcode for O_ADD, O_SUB, O_AND, O_OR, O_XOR */
202 static int rx[] = {0003, 0053, 0043, 0013, 0063};
203 op_rr(rx[op & 0x0f], rd, r2, LONGSZ);
206 static void i_add_imm(int op, int rd, int rn, long n)
208 /* opcode for O_ADD, O_SUB, O_AND, O_OR, O_XOR */
209 static int rx[] = {0xc0, 0xe8, 0xe0, 0xc8, 0xf0};
210 unsigned char s[3] = {0x83, rx[op & 0x0f] | rd, n & 0xff};
211 os((void *) s, 3);
214 void i_num(int rd, long n)
216 if (!n) {
217 op_rr(I_XOR, rd, rd, 4);
218 return;
219 } else {
220 op_x(I_MOVIR + (rd & 7), 0, rd, LONGSZ);
221 oi(n, LONGSZ);
225 static void i_mul(int rd, int r1, int r2)
227 if (r2 != R_RDX)
228 i_num(R_RDX, 0);
229 op_rr(I_MUL, 4, r2, LONGSZ);
232 static void i_div(int op, int rd, int r1, int r2)
234 if (r2 != R_RDX) {
235 if (op & O_SIGNED)
236 op_x(I_CQO, R_RAX, R_RDX, LONGSZ);
237 else
238 i_num(R_RDX, 0);
240 op_rr(I_MUL, op & O_SIGNED ? 7 : 6, r2, LONGSZ);
243 static void i_tst(int rn, int rm)
245 op_rr(I_TST, rn, rm, LONGSZ);
248 static void i_cmp(int rn, int rm)
250 op_rr(I_CMP, rn, rm, LONGSZ);
253 static void i_cmp_imm(int rn, long n)
255 unsigned char s[3] = {0x83, 0xf8 | rn, n & 0xff};
256 os(s, 3);
259 static void i_shl(int op, int rd, int r1, int rs)
261 int sm = 4;
262 if ((op & 0x0f) == 1)
263 sm = op & O_SIGNED ? 7 : 5;
264 op_rr(I_SHX, sm, rd, LONGSZ);
267 static void i_shl_imm(int op, int rd, int rn, long n)
269 int sm = (op & 0x1) ? (op & O_SIGNED ? 0xf8 : 0xe8) : 0xe0;
270 char s[3] = {0xc1, sm | rn, n & 0xff};
271 os(s, 3);
274 void i_sym(int rd, char *sym, int off)
276 op_x(I_MOVIR + (rd & 7), 0, rd, LONGSZ);
277 if (!pass1)
278 out_rel(sym, OUT_CS, cslen);
279 oi(off, LONGSZ);
282 static void i_neg(int rd)
284 op_rr(I_NOT, 3, rd, LONGSZ);
287 static void i_not(int rd)
289 op_rr(I_NOT, 2, rd, LONGSZ);
292 /* for optimizing cmp + tst + jmp to cmp + jmp */
293 #define OPT_ISCMP() (last_set >= 0 && last_set + 6 == cslen)
294 #define OPT_CCOND() (cs[last_set + 1])
296 static long last_set = -1;
298 static void i_set(int op, int rd)
300 /* lt, gt, le, ge, eq, neq */
301 static int ucond[] = {0x92, 0x97, 0x96, 0x93, 0x94, 0x95};
302 static int scond[] = {0x9c, 0x9f, 0x9e, 0x9d, 0x94, 0x95};
303 int cond = op & O_SIGNED ? scond[op & 0x0f] : ucond[op & 0x0f];
304 char set[] = "\x0f\x00\xc0";
305 set[1] = cond;
306 last_set = cslen;
307 os(set, 3); /* setl al */
308 os("\x0f\xb6\xc0", 3); /* movzx rax, al */
311 static void i_lnot(int rd)
313 if (OPT_ISCMP()) {
314 cs[last_set + 1] ^= 0x01;
315 } else {
316 char cmp[] = "\x83\xf8\x00";
317 cmp[1] |= rd;
318 os(cmp, 3); /* cmp eax, 0 */
319 i_set(O_EQ, rd);
323 static void jx(int x, int nbytes)
325 char op[2] = {0x0f};
326 if (nbytes == 1) {
327 op[0] = 0x70 | (x & 0x0f);
328 os(op, 1); /* jx $addr */
329 } else {
330 op[1] = x;
331 os(op, 2); /* jx $addr */
333 oi(0, nbytes);
336 void i_jmp(int rn, int z, int nbytes)
338 if (!nbytes)
339 return;
340 if (nbytes > 1)
341 nbytes = 4;
342 if (rn >= 0) {
343 if (OPT_ISCMP()) {
344 int cond = OPT_CCOND();
345 cslen = last_set;
346 jx((!z ? cond : cond ^ 0x01) & ~0x10, nbytes);
347 last_set = -1;
348 } else {
349 i_tst(rn, rn);
350 jx(z ? 0x84 : 0x85, nbytes);
352 } else {
353 os(nbytes == 1 ? "\xeb" : "\xe9", 1); /* jmp $addr */
354 oi(0, nbytes);
358 long i_fill(long src, long dst, int nbytes)
360 if (!nbytes)
361 return 0;
362 if (nbytes > 1)
363 nbytes = 4;
364 putint((void *) (cs + src - nbytes), dst - src, nbytes);
365 return dst - src;
368 static void i_zx(int rd, int r1, int bits)
370 if (bits & 0x07) {
371 i_shl_imm(O_SHL, rd, rd, LONGSZ * 8 - bits);
372 i_shl_imm(O_SHR, rd, rd, LONGSZ * 8 - bits);
373 } else {
374 mov_r2r(rd, r1, bits >> 3);
378 static void i_sx(int rd, int r1, int bits)
380 mov_r2r(rd, r1, BT_SIGNED | (bits >> 3));
383 void i_op(int op, int rd, int r1, int r2)
385 if ((op & 0xf0) == 0x00)
386 i_add(op, r1, r1, r2);
387 if ((op & 0xf0) == 0x10)
388 i_shl(op, r1, r1, r2);
389 if ((op & 0xf0) == 0x20) {
390 if ((op & 0xff) == O_MUL)
391 i_mul(R_RAX, r1, r2);
392 if ((op & 0xff) == O_DIV)
393 i_div(op, R_RAX, r1, r2);
394 if ((op & 0xff) == O_MOD)
395 i_div(op, R_RDX, r1, r2);
396 return;
398 if ((op & 0xf0) == 0x30) {
399 i_cmp(r1, r2);
400 i_set(op, rd);
401 return;
403 if ((op & 0xf0) == 0x40) { /* uop */
404 if ((op & 0xff) == O_NEG)
405 i_neg(r1);
406 if ((op & 0xff) == O_NOT)
407 i_not(r1);
408 if ((op & 0xff) == O_LNOT)
409 i_lnot(r1);
410 return;
414 static void i_add_anyimm(int rd, int rn, long n)
416 op_rm(I_LEA, rd, rn, n, LONGSZ);
419 void i_op_imm(int op, int rd, int r1, long n)
421 if ((op & 0xf0) == 0x00) { /* add */
422 if (rd == r1 && i_imm(O_ADD, n))
423 i_add_imm(op, rd, r1, n);
424 else
425 i_add_anyimm(rd, r1, n);
427 if ((op & 0xf0) == 0x10) /* shl */
428 i_shl_imm(op, rd, r1, n);
429 if ((op & 0xf0) == 0x20) /* mul */
430 die("mul/imm not implemented");
431 if ((op & 0xf0) == 0x30) { /* imm */
432 i_cmp_imm(r1, n);
433 i_set(op, rd);
435 if ((op & 0xf0) == 0x50) { /* etc */
436 if ((op & 0xff) == O_ZX)
437 i_zx(rd, r1, n);
438 if ((op & 0xff) == O_SX)
439 i_sx(rd, r1, n);
440 if ((op & 0xff) == O_MOV)
441 i_mov(rd, r1);
445 void i_memcpy(int r0, int r1, int r2)
447 os("\xfc\xf3\xa4", 3); /* cld; rep movs */
450 void i_memset(int r0, int r1, int r2)
452 os("\xfc\xf3\xaa", 3); /* cld; rep stosb */
455 void i_call_reg(int rd)
457 op_rr(I_CALL, 2, rd, LONGSZ);
460 void i_call(char *sym, int off)
462 os("\xe8", 1); /* call $x */
463 if (!pass1)
464 out_rel(sym, OUT_CS | OUT_RLREL, cslen);
465 oi(-4 + off, 4);
468 static int func_argc;
469 static int func_varg;
470 static int func_spsub;
471 static int func_sargs;
472 static int func_sregs;
473 static int func_initfp;
474 static int spsub_addr;
476 int i_args(void)
478 return LONGSZ << 1;
481 int i_sp(void)
483 int i;
484 int n = 0;
485 for (i = 0; i < N_TMPS; i++)
486 if ((1 << tmpregs[i]) & func_sregs)
487 n += LONGSZ;
488 return -n;
491 void i_prolog(int argc, int varg, int sargs, int sregs, int initfp, int subsp)
493 int i;
494 last_set = -1;
495 func_argc = argc;
496 func_varg = varg;
497 func_sargs = sargs;
498 func_sregs = sregs;
499 func_initfp = initfp;
500 func_spsub = subsp;
501 if (initfp) {
502 os("\x55", 1); /* push rbp */
503 os("\x89\xe5", 2); /* mov rbp, rsp */
505 if (func_sregs) {
506 for (i = N_TMPS - 1; i >= 0; i--)
507 if ((1 << tmpregs[i]) & func_sregs)
508 i_push(tmpregs[i]);
510 if (func_spsub) {
511 os("\x81\xec", 2); /* sub rsp, $xxx */
512 spsub_addr = cslen;
513 oi(0, 4);
517 void i_epilog(int sp_max)
519 int diff;
520 int nsregs = 0;
521 int i;
522 for (i = 0; i < N_TMPS; i++)
523 if ((1 << tmpregs[i]) & func_sregs)
524 nsregs++;
525 diff = ALIGN(-sp_max - nsregs * LONGSZ, 16);
526 /* forcing 16-byte alignment */
527 diff = nsregs & 1 ? diff + LONGSZ : diff;
528 if (func_spsub && diff) {
529 i_add_anyimm(R_RSP, R_RBP, -nsregs * LONGSZ);
530 putint(cs + spsub_addr, diff, 4);
532 if (func_sregs) {
533 for (i = 0; i < N_TMPS; i++)
534 if ((1 << tmpregs[i]) & func_sregs)
535 i_pop(tmpregs[i]);
537 if (func_initfp)
538 os("\xc9", 1); /* leave */
539 os("\xc3", 1); /* ret */
542 void i_done(void)