gen: cleanup tmp handling and fix regs array in o_swap()
[neatcc.git] / gen.c
blobf20346bddbdce6e1c63af1a07e547588b4e8953e
1 #include <stdlib.h>
2 #include <string.h>
3 #include "gen.h"
4 #include "tok.h"
6 #define TMP_ADDR 0x0001
7 #define LOC_REG 0x0100
8 #define LOC_MEM 0x0200
9 #define LOC_NUM 0x0400
10 #define LOC_SYM 0x0800
11 #define LOC_LOCAL 0x1000
12 #define LOC_MASK 0xff00
14 #define R_RAX 0x00
15 #define R_RCX 0x01
16 #define R_RDX 0x02
17 #define R_RBX 0x03
18 #define R_RSP 0x04
19 #define R_RBP 0x05
20 #define R_RSI 0x06
21 #define R_RDI 0x07
22 #define R_R8 0x08
23 #define R_R9 0x09
24 #define R_R10 0x0a
25 #define R_R11 0x0b
26 #define R_R12 0x0c
27 #define R_R13 0x0d
28 #define R_R14 0x0e
29 #define R_R15 0x0f
30 #define NREGS 0x10
32 #define MOV_M2R 0x8b
33 #define MOV_R2X 0x89
34 #define MOV_I2X 0xc7
35 #define MOV_I2R 0xb8
36 #define ADD_R2X 0x03
37 #define SUB_R2X 0x2b
38 #define SHX_REG 0xd3
39 #define CMP_R2X 0x3b
40 #define LEA_M2R 0x8d
41 #define NEG_REG 0xf7
42 #define NOT_REG 0xf7
43 #define CALL_REG 0xff
44 #define MUL_A2X 0xf7
45 #define XOR_R2X 0x33
46 #define AND_R2X 0x23
47 #define OR_R2X 0x0b
48 #define TEST_R2R 0x85
50 #define MIN(a, b) ((a) < (b) ? (a) : (b))
52 #define R_BYTEMASK (1 << R_RAX | 1 << R_RDX | 1 << R_RCX)
53 #define TMP_BT(t) ((t)->flags & TMP_ADDR ? 8 : (t)->bt)
54 #define TMP_REG(t) ((t)->flags & LOC_REG ? (t)->addr : reg_get(~0))
55 #define TMP_REG2(t, r) ((t)->flags & LOC_REG && (t)->addr != r ? \
56 (t)->addr : reg_get(~(1 << r)))
57 #define TMP_BYTEREG(t) ((t)->flags & LOC_REG && \
58 (1 << (t)->addr) & R_BYTEMASK ? \
59 (t)->addr : reg_get(R_BYTEMASK))
60 #define BT_TMPBT(bt) (BT_SZ(bt) >= 4 ? (bt) : (bt) & BT_SIGNED | 4)
62 static char buf[SECSIZE];
63 static char *cur;
64 static int nogen;
65 static long sp;
66 static long spsub_addr;
67 static long maxsp;
69 #define TMP(i) (&tmps[ntmp - 1 - (i)])
71 static struct tmp {
72 long addr;
73 long off; /* used for LOC_SYM; offset from a symbol address */
74 unsigned flags;
75 unsigned bt;
76 } tmps[MAXTMP];
77 static int ntmp;
79 static int tmpsp;
81 static struct tmp *regs[NREGS];
82 static int tmpregs[] = {R_RAX, R_RDI, R_RSI, R_RDX, R_RCX, R_R8, R_R9};
84 #define MAXRET (1 << 8)
86 static long ret[MAXRET];
87 static int nret;
89 static long cmp_last;
90 static long cmp_setl;
92 static void putint(char *s, long n, int l)
94 if (nogen)
95 return;
96 while (l--) {
97 *s++ = n;
98 n >>= 8;
102 static void os(char *s, int n)
104 if (nogen)
105 return;
106 while (n--)
107 *cur++ = *s++;
110 static void oi(long n, int l)
112 while (l--) {
113 *cur++ = n;
114 n >>= 8;
118 static long codeaddr(void)
120 return cur - buf;
123 static void o_op(int *op, int nop, int r1, int r2, unsigned bt)
125 int rex = 0;
126 int i;
127 if (r1 & 0x8)
128 rex |= 4;
129 if (r2 & 0x8)
130 rex |= 1;
131 if (BT_SZ(bt) == 8)
132 rex |= 8;
133 if (rex || (bt & BT_SZMASK) == 8)
134 oi(0x40 | rex, 1);
135 if ((bt & BT_SZMASK) == 2)
136 oi(0x66, 1);
137 if ((bt & BT_SZMASK) == 1)
138 op[nop - 1] &= ~0x1;
139 for (i = 0; i < nop; i++)
140 oi(op[i], 1);
143 static void memop(int *op, int nop, int src, int base, int off, unsigned bt)
145 int dis = off == (char) off ? 1 : 4;
146 int mod = dis == 4 ? 2 : 1;
147 o_op(op, nop, src, base, bt);
148 if (!off)
149 mod = 0;
150 oi((mod << 6) | ((src & 0x07) << 3) | (base & 0x07), 1);
151 if (off)
152 oi(off, dis);
155 static void memop1(int op, int src, int base, int off, unsigned bt)
157 memop(&op, 1, src, base, off, bt);
160 static void regop(int *op, int nop, int src, int dst, unsigned bt)
162 o_op(op, nop, src, dst, bt);
163 oi((3 << 6) | (src << 3) | (dst & 0x07), 1);
166 static void regop1(int op, int src, int dst, unsigned bt)
168 regop(&op, 1, src, dst, bt);
171 static long sp_push(int size)
173 sp += size;
174 if (sp > maxsp)
175 maxsp = sp;
176 return sp;
179 #define LOC_NEW(f, l) (((f) & ~LOC_MASK) | (l))
181 static void tmp_mem(struct tmp *tmp)
183 int src = tmp->addr;
184 if (!(tmp->flags & LOC_REG))
185 return;
186 if (tmpsp == -1)
187 tmpsp = sp;
188 tmp->addr = -sp_push(8);
189 memop1(MOV_R2X, src, R_RBP, tmp->addr, BT_TMPBT(TMP_BT(tmp)));
190 regs[src] = NULL;
191 tmp->flags = LOC_NEW(tmp->flags, LOC_MEM);
194 static int *movxx_x2r(int bt)
196 static int movxx[2] = {0x0f};
197 if (BT_SZ(bt) == 1)
198 movxx[1] = bt & BT_SIGNED ? 0xbe : 0xb6;
199 else
200 movxx[1] = bt & BT_SIGNED ? 0xbf : 0xb7;
201 return movxx;
204 #define MOVSXD 0x63
206 static void mov_r2r(int r1, int r2, unsigned bt1, unsigned bt2)
208 int s1 = bt1 & BT_SIGNED;
209 int s2 = bt2 & BT_SIGNED;
210 int sz1 = BT_SZ(bt1);
211 int sz2 = BT_SZ(bt2);
212 if (sz2 < 4 && (sz1 >= sz2 && s1 != s2)) {
213 regop(movxx_x2r(bt2), 2, r1, r2, 4);
214 return;
216 if (sz1 == 4 && sz2 == 8) {
217 regop1(MOVSXD, r2, r1, sz2);
218 return;
220 if (r1 != r2 || sz1 > sz2)
221 regop1(MOV_R2X, r1, r2, BT_TMPBT(bt2));
224 static void mov_m2r(int dst, int base, int off, int bt1, int bt2)
226 if (BT_SZ(bt1) < 4) {
227 memop(movxx_x2r(bt1), 2, dst, base, off,
228 bt1 & BT_SIGNED && BT_SZ(bt2) == 8 ? 8 : 4);
229 mov_r2r(dst, dst, bt1, bt2);
230 } else {
231 memop1(MOV_M2R, dst, base, off, bt1);
232 mov_r2r(dst, dst, bt1, bt2);
236 static void num_cast(struct tmp *t, unsigned bt)
238 if (!(bt & BT_SIGNED) && BT_SZ(bt) != 8)
239 t->addr &= ((1l << (long) (BT_SZ(bt) * 8)) - 1);
240 if (bt & BT_SIGNED && BT_SZ(bt) != 8 &&
241 t->addr > (1l << (BT_SZ(bt) * 8 - 1)))
242 t->addr = -((1l << (BT_SZ(bt) * 8)) - t->addr);
243 t->bt = bt;
246 static void num_reg(int reg, unsigned bt, long num)
248 int op = MOV_I2R + (reg & 7);
249 if (BT_SZ(bt) == 8 && num >= 0 && num == (unsigned) num)
250 bt = 4;
251 o_op(&op, 1, 0, reg, bt);
252 oi(num, BT_SZ(bt));
255 static void tmp_reg(struct tmp *tmp, int dst, unsigned bt, int deref)
257 if (deref && tmp->flags & TMP_ADDR)
258 tmp->flags &= ~TMP_ADDR;
259 else
260 deref = 0;
261 if (tmp->flags & LOC_NUM) {
262 num_cast(tmp, bt);
263 tmp->bt = BT_TMPBT(bt);
264 num_reg(dst, tmp->bt, tmp->addr);
265 tmp->addr = dst;
266 regs[dst] = tmp;
267 tmp->flags = LOC_NEW(tmp->flags, LOC_REG);
269 if (tmp->flags & LOC_SYM) {
270 regop1(MOV_I2X, 0, dst, TMP_BT(tmp));
271 if (!nogen)
272 out_rela(tmp->addr, codeaddr(), 0);
273 oi(tmp->off, 4);
274 tmp->addr = dst;
275 regs[dst] = tmp;
276 tmp->flags = LOC_NEW(tmp->flags, LOC_REG);
278 if (tmp->flags & LOC_REG) {
279 if (deref)
280 mov_m2r(dst, tmp->addr, 0, tmp->bt, bt);
281 else
282 mov_r2r(tmp->addr, dst, TMP_BT(tmp),
283 tmp->flags & TMP_ADDR ? 8 : bt);
284 regs[tmp->addr] = NULL;
286 if (tmp->flags & LOC_LOCAL) {
287 if (deref)
288 mov_m2r(dst, R_RBP, tmp->addr, tmp->bt, bt);
289 else
290 memop1(LEA_M2R, dst, R_RBP, tmp->addr, 8);
292 if (tmp->flags & LOC_MEM) {
293 mov_m2r(dst, R_RBP, tmp->addr,
294 deref ? 8 : TMP_BT(tmp), deref ? 8 : bt);
295 if (deref)
296 mov_m2r(dst, dst, 0, tmp->bt, bt);
298 tmp->addr = dst;
299 tmp->bt = tmp->flags & TMP_ADDR ? bt : BT_TMPBT(bt);
300 regs[dst] = tmp;
301 tmp->flags = LOC_NEW(tmp->flags, LOC_REG);
304 static void reg_free(int reg)
306 int i;
307 if (!regs[reg])
308 return;
309 for (i = 0; i < ARRAY_SIZE(tmpregs); i++)
310 if (!regs[tmpregs[i]]) {
311 tmp_reg(regs[reg], tmpregs[i], regs[reg]->bt, 0);
312 return;
314 tmp_mem(regs[reg]);
317 static void reg_for(int reg, struct tmp *t)
319 if (regs[reg] && regs[reg] != t)
320 reg_free(reg);
323 static void tmp_mv(struct tmp *t, int reg)
325 reg_for(reg, t);
326 tmp_reg(t, reg, t->bt, 0);
329 static void tmp_to(struct tmp *t, int reg, int bt)
331 reg_for(reg, t);
332 tmp_reg(t, reg, bt ? bt : t->bt, 1);
335 static void tmp_drop(int n)
337 int i;
338 for (i = ntmp - n; i < ntmp; i++)
339 if (tmps[i].flags & LOC_REG)
340 regs[tmps[i].addr] = NULL;
341 cmp_last = -1;
342 ntmp -= n;
345 static int tmp_pop(int reg, int bt)
347 struct tmp *t = TMP(0);
348 tmp_to(t, reg, bt);
349 tmp_drop(1);
350 return t->bt;
353 static struct tmp *tmp_new(void)
355 cmp_last = -1;
356 return &tmps[ntmp++];
359 static void tmp_push(int reg, unsigned bt)
361 struct tmp *t = tmp_new();
362 t->addr = reg;
363 t->bt = bt;
364 t->flags = LOC_REG;
365 regs[reg] = t;
368 void o_local(long addr, unsigned bt)
370 struct tmp *t = tmp_new();
371 t->addr = -addr;
372 t->bt = bt;
373 t->flags = LOC_LOCAL | TMP_ADDR;
376 void o_num(long num, unsigned bt)
378 struct tmp *t = tmp_new();
379 t->addr = num;
380 t->bt = bt;
381 t->flags = LOC_NUM;
384 void o_symaddr(long addr, unsigned bt)
386 struct tmp *t = tmp_new();
387 t->bt = bt;
388 t->addr = addr;
389 t->flags = LOC_SYM | TMP_ADDR;
390 t->off = 0;
393 void o_tmpdrop(int n)
395 int i;
396 if (n == -1 || n > ntmp)
397 n = ntmp;
398 tmp_drop(n);
399 if (!ntmp) {
400 if (tmpsp != -1)
401 sp = tmpsp;
402 tmpsp = -1;
406 #define FORK_REG R_RAX
408 void o_tmpfork(void)
410 tmp_pop(FORK_REG, 0);
413 void o_tmpjoin(void)
415 tmp_to(TMP(0), FORK_REG, 0);
418 void o_tmpswap(void)
420 struct tmp *t1 = TMP(0);
421 struct tmp *t2 = TMP(1);
422 struct tmp t;
423 memcpy(&t, t1, sizeof(t));
424 memcpy(t1, t2, sizeof(t));
425 memcpy(t2, &t, sizeof(t));
426 if (t1->flags & LOC_REG)
427 regs[t1->addr] = t1;
428 if (t2->flags & LOC_REG)
429 regs[t2->addr] = t2;
432 static int reg_get(int mask)
434 int i;
435 for (i = 0; i < ARRAY_SIZE(tmpregs); i++)
436 if ((1 << tmpregs[i]) & mask && !regs[tmpregs[i]])
437 return tmpregs[i];
438 for (i = 0; i < ARRAY_SIZE(tmpregs); i++)
439 if ((1 << tmpregs[i]) & mask) {
440 reg_free(tmpregs[i]);
441 return tmpregs[i];
443 return 0;
446 void o_tmpcopy(void)
448 struct tmp *t1 = TMP(0);
449 struct tmp *t2 = tmp_new();
450 memcpy(t2, t1, sizeof(*t1));
451 if (!(t1->flags & (LOC_REG | LOC_MEM)))
452 return;
453 if (t1->flags & LOC_MEM) {
454 tmp_reg(t2, reg_get(~0), t2->bt, 0);
455 } else if (t1->flags & LOC_REG) {
456 t2->addr = reg_get(~(1 << t1->addr));
457 regop1(MOV_R2X, t1->addr, t2->addr, BT_TMPBT(TMP_BT(t1)));
458 regs[t2->addr] = t2;
460 t2->flags = t1->flags;
463 void o_cast(unsigned bt)
465 struct tmp *t = TMP(0);
466 int reg;
467 if (t->bt == bt)
468 return;
469 if (t->flags & LOC_NUM) {
470 num_cast(t, bt);
471 return;
473 reg = BT_SZ(bt) == 1 ? TMP_BYTEREG(t) : TMP_REG(t);
474 tmp_to(t, reg, bt);
477 long o_func_beg(char *name, int global)
479 long addr = out_func_beg(name, global);
480 cur = buf;
481 os("\x55", 1); /* push %rbp */
482 os("\x48\x89\xe5", 3); /* mov %rsp, %rbp */
483 sp = 0;
484 maxsp = 0;
485 ntmp = 0;
486 tmpsp = -1;
487 nret = 0;
488 cmp_last = -1;
489 memset(regs, 0, sizeof(regs));
490 os("\x48\x81\xec", 3); /* sub $xxx, %rsp */
491 spsub_addr = codeaddr();
492 oi(0, 4);
493 return addr;
496 void o_deref(unsigned bt)
498 struct tmp *t = TMP(0);
499 if (t->flags & TMP_ADDR)
500 tmp_to(t, TMP_REG(t), t->bt);
501 t->bt = bt;
502 t->flags |= TMP_ADDR;
505 void o_load(void)
507 struct tmp *t = TMP(0);
508 tmp_to(t, TMP_REG(t), t->bt);
511 static unsigned bt_op(unsigned bt1, unsigned bt2)
513 unsigned s1 = BT_SZ(bt1);
514 unsigned s2 = BT_SZ(bt2);
515 unsigned bt = (bt1 | bt2) & BT_SIGNED | (s1 > s2 ? s1 : s2);
516 return BT_TMPBT(bt);
519 #define TMP_CONST(t) ((t)->flags & LOC_NUM && !((t)->flags & TMP_ADDR))
520 #define LOCAL_PTR(t) ((t)->flags & LOC_LOCAL && !((t)->flags & TMP_ADDR))
521 #define SYM_PTR(t) ((t)->flags & LOC_LOCAL && !((t)->flags & TMP_ADDR))
523 int o_popnum(long *c)
525 struct tmp *t = TMP(0);
526 if (!TMP_CONST(t))
527 return 1;
528 *c = t->addr;
529 tmp_drop(1);
530 return 0;
533 static int c_binop(long (*cop)(long a, long b, unsigned bt), unsigned bt)
535 struct tmp *t1 = TMP(0);
536 struct tmp *t2 = TMP(1);
537 int locals = LOCAL_PTR(t1) + LOCAL_PTR(t2);
538 int syms = SYM_PTR(t1) + SYM_PTR(t2);
539 int nums = TMP_CONST(t1) + TMP_CONST(t2);
540 if (syms == 2 || syms && locals || syms + nums + locals != 2)
541 return 1;
542 if (!bt) {
543 if (!locals)
544 bt = syms ? 8 : bt_op(t1->bt, t2->bt);
545 if (locals == 1)
546 bt = 8;
547 if (locals == 2)
548 bt = 4 | BT_SIGNED;
550 if (syms) {
551 long o1 = SYM_PTR(t1) ? t1->off : t1->addr;
552 long o2 = SYM_PTR(t2) ? t2->off : t2->addr;
553 long ret = cop(o2, o1, bt);
554 if (!SYM_PTR(t2))
555 o_tmpswap();
556 t2->off = ret;
557 tmp_drop(1);
558 } else {
559 long ret = cop(t2->addr, t1->addr, locals ? 4 | BT_SIGNED : bt);
560 tmp_drop(2);
561 if (locals == 1) {
562 o_local(-ret, bt);
563 o_addr();
564 } else {
565 o_num(ret, bt);
568 return 0;
571 static int c_op(long (*cop)(long a, unsigned bt), unsigned bt)
573 struct tmp *t1 = TMP(0);
574 long ret;
575 if (!TMP_CONST(t1))
576 return 1;
577 if (!bt)
578 bt = t1->bt;
579 ret = cop(t1->addr, bt);
580 tmp_drop(1);
581 o_num(ret, bt);
582 return 0;
585 static void shx(int uop, int sop)
587 struct tmp *t2 = TMP(1);
588 int r2 = TMP_REG2(t2, R_RCX);
589 int bt;
590 tmp_pop(R_RCX, 0);
591 bt = tmp_pop(r2, 0);
592 regop1(SHX_REG, bt & BT_SIGNED ? sop : uop, r2, BT_TMPBT(bt));
593 tmp_push(r2, bt);
596 static long c_shl(long a, long b, unsigned bt)
598 return a << b;
601 void o_shl(void)
603 if (!c_binop(c_shl, 0))
604 return;
605 shx(4, 4);
608 static long c_shr(long a, long b, unsigned bt)
610 if (bt & BT_SIGNED)
611 return a >> b;
612 else
613 return (unsigned long) a >> b;
616 void o_shr(void)
618 if (!c_binop(c_shr, 0))
619 return;
620 shx(5, 7);
623 static int mulop(int uop, int sop, int reg)
625 struct tmp *t1 = TMP(0);
626 struct tmp *t2 = TMP(1);
627 int bt1 = TMP_BT(t1);
628 int bt2 = TMP_BT(t2);
629 int bt = bt_op(bt1, bt2);
630 if (t1->flags & LOC_REG && t1->addr != R_RAX && t1->addr != R_RDX)
631 reg = t1->addr;
632 tmp_to(t1, reg, bt);
633 tmp_to(t2, R_RAX, bt);
634 if (reg != R_RDX) {
635 int cqo = 0x99;
636 reg_free(R_RDX);
637 if (bt & BT_SIGNED)
638 o_op(&cqo, 1, R_RAX, R_RDX, bt);
639 else
640 regop1(XOR_R2X, R_RDX, R_RDX, bt);
642 tmp_drop(2);
643 regop1(MUL_A2X, bt & BT_SIGNED ? sop : uop, reg, BT_TMPBT(bt2));
644 return bt;
647 static long c_mul(long a, long b, unsigned bt)
649 return a * b;
652 void o_mul(void)
654 int bt;
655 if (!c_binop(c_mul, 0))
656 return;
657 bt = mulop(4, 5, R_RDX);
658 tmp_push(R_RAX, bt);
661 static long c_div(long a, long b, unsigned bt)
663 return a / b;
666 void o_div(void)
668 int bt;
669 if (!c_binop(c_div, 0))
670 return;
671 bt = mulop(6, 7, R_RCX);
672 tmp_push(R_RAX, bt);
675 static long c_mod(long a, long b, unsigned bt)
677 return a % b;
680 void o_mod(void)
682 int bt;
683 if (!c_binop(c_mod, 0))
684 return;
685 bt = mulop(6, 7, R_RCX);
686 tmp_push(R_RDX, bt);
689 void o_addr(void)
691 tmps[ntmp - 1].flags &= ~TMP_ADDR;
692 tmps[ntmp - 1].bt = 8;
695 void o_ret(unsigned bt)
697 if (bt)
698 tmp_pop(R_RAX, bt);
699 else
700 os("\x31\xc0", 2); /* xor %eax, %eax */
701 ret[nret++] = o_jmp(0);
704 static int binop(int op, int *reg)
706 struct tmp *t1 = TMP(0);
707 struct tmp *t2 = TMP(1);
708 int r1, r2;
709 unsigned bt = bt_op(t1->bt, t2->bt);
710 r1 = TMP_REG(t1);
711 r2 = TMP_REG2(t2, r1);
712 tmp_pop(r1, bt);
713 tmp_pop(r2, bt);
714 regop1(op, r2, r1, bt);
715 *reg = r2;
716 return bt;
719 static long c_add(long a, long b, unsigned bt)
721 return a + b;
724 void o_add(void)
726 int reg;
727 int bt;
728 if (!c_binop(c_add, 0))
729 return;
730 bt = binop(ADD_R2X, &reg);
731 tmp_push(reg, bt);
734 static long c_xor(long a, long b, unsigned bt)
736 return a ^ b;
739 void o_xor(void)
741 int reg;
742 int bt;
743 if (!c_binop(c_xor, 0))
744 return;
745 bt = binop(XOR_R2X, &reg);
746 tmp_push(reg, bt);
749 static long c_and(long a, long b, unsigned bt)
751 return a & b;
754 void o_and(void)
756 int reg;
757 int bt;
758 if (!c_binop(c_and, 0))
759 return;
760 bt = binop(AND_R2X, &reg);
761 tmp_push(reg, bt);
764 static long c_or(long a, long b, unsigned bt)
766 return a | b;
769 void o_or(void)
771 int reg;
772 int bt;
773 if (!c_binop(c_or, 0))
774 return;
775 bt = binop(OR_R2X, &reg);
776 tmp_push(reg, bt);
779 static long c_sub(long a, long b, unsigned bt)
781 return a - b;
784 void o_sub(void)
786 int reg;
787 int bt;
788 if (!c_binop(c_sub, 0))
789 return;
790 bt = binop(SUB_R2X, &reg);
791 tmp_push(reg, bt);
794 static void o_cmp(int uop, int sop)
796 struct tmp *t1 = TMP(0);
797 struct tmp *t2 = TMP(1);
798 char set[] = "\x0f\x00\xc0";
799 int reg;
800 int bt;
801 if (regs[R_RAX] && regs[R_RAX] != t1 && regs[R_RAX] != t2)
802 reg_free(R_RAX);
803 bt = binop(CMP_R2X, &reg);
804 set[1] = bt & BT_SIGNED ? sop : uop;
805 cmp_setl = codeaddr();
806 os(set, 3); /* setl %al */
807 os("\x0f\xb6\xc0", 3); /* movzbl %al, %eax */
808 tmp_push(R_RAX, 4 | BT_SIGNED);
809 cmp_last = codeaddr();
812 static long c_lt(long a, long b, unsigned bt)
814 return a < b;
817 void o_lt(void)
819 if (!c_binop(c_lt, 4))
820 return;
821 o_cmp(0x92, 0x9c);
824 static long c_gt(long a, long b, unsigned bt)
826 return a > b;
829 void o_gt(void)
831 if (!c_binop(c_gt, 4))
832 return;
833 o_cmp(0x97, 0x9f);
836 static long c_le(long a, long b, unsigned bt)
838 return a <= b;
841 void o_le(void)
843 if (!c_binop(c_le, 4))
844 return;
845 o_cmp(0x96, 0x9e);
848 static long c_ge(long a, long b, unsigned bt)
850 return a >= b;
853 void o_ge(void)
855 if (!c_binop(c_ge, 4))
856 return;
857 o_cmp(0x93, 0x9d);
860 static long c_eq(long a, long b, unsigned bt)
862 return a == b;
865 void o_eq(void)
867 if (!c_binop(c_eq, 4))
868 return;
869 o_cmp(0x94, 0x94);
872 static long c_neq(long a, long b, unsigned bt)
874 return a != b;
877 void o_neq(void)
879 if (!c_binop(c_neq, 4))
880 return;
881 o_cmp(0x95, 0x95);
884 static long c_lnot(long a, unsigned bt)
886 return !a;
889 void o_lnot(void)
891 if (!c_op(c_lnot, 4))
892 return;
893 if (cmp_last == codeaddr()) {
894 buf[cmp_setl + 1] ^= 0x10;
895 } else {
896 o_num(0, 4 | BT_SIGNED);
897 o_eq();
901 static long c_neg(long a, unsigned bt)
903 return -a;
906 void o_neg(void)
908 struct tmp *t = TMP(0);
909 int reg;
910 unsigned bt = BT_TMPBT(t->bt);
911 if (!c_op(c_neg, t->bt | BT_SIGNED))
912 return;
913 reg = TMP_REG(t);
914 tmp_to(t, reg, bt);
915 regop1(NEG_REG, 3, reg, bt);
918 static long c_not(long a, unsigned bt)
920 return ~a;
923 void o_not(void)
925 struct tmp *t = TMP(0);
926 int reg;
927 unsigned bt = BT_TMPBT(t->bt);
928 if (!c_op(c_not, 0))
929 return;
930 reg = TMP_REG(t);
931 tmp_to(t, reg, bt);
932 regop1(NOT_REG, 2, reg, bt);
935 void o_func_end(void)
937 int i;
938 for (i = 0; i < nret; i++)
939 o_filljmp(ret[i]);
940 os("\xc9\xc3", 2); /* leave; ret; */
941 putint(buf + spsub_addr, (maxsp + 7) & ~0x07, 4);
942 out_func_end(buf, cur - buf);
945 long o_mklocal(int size)
947 return sp_push((size + 7) & ~0x07);
950 void o_rmlocal(long addr, int sz)
952 sp = addr - sz;
955 static int arg_regs[] = {R_RDI, R_RSI, R_RDX, R_RCX, R_R8, R_R9};
957 #define R_NARGS ARRAY_SIZE(arg_regs)
959 long o_arg(int i, unsigned bt)
961 long addr;
962 if (i < R_NARGS) {
963 addr = o_mklocal(BT_SZ(bt));
964 memop1(MOV_R2X, arg_regs[i], R_RBP, -addr, bt);
965 } else {
966 addr = -8 * (i - R_NARGS + 2);
968 return addr;
971 void o_assign(unsigned bt)
973 struct tmp *t1 = TMP(0);
974 struct tmp *t2 = TMP(1);
975 int r1 = BT_SZ(bt) > 1 ? TMP_REG(t1) : TMP_BYTEREG(t1);
976 int reg;
977 int off;
978 tmp_to(t1, r1, BT_TMPBT(bt));
979 if (t2->flags & LOC_LOCAL) {
980 reg = R_RBP;
981 off = t2->addr;
982 } else {
983 reg = TMP_REG2(t2, r1);
984 off = 0;
985 tmp_mv(t2, reg);
987 tmp_drop(2);
988 memop1(MOV_R2X, r1, reg, off, bt);
989 tmp_push(r1, bt);
992 void o_memcpy(int sz)
994 struct tmp *t0 = TMP(-1);
995 struct tmp *t1 = TMP(0);
996 struct tmp *t2 = TMP(1);
997 o_num(sz, 4);
998 tmp_to(t0, R_RCX, 0);
999 tmp_mv(t1, R_RSI);
1000 tmp_mv(t2, R_RDI);
1001 os("\xf3\xa4", 2); /* rep movs */
1002 tmp_drop(2);
1005 void o_memset(int x, int sz)
1007 struct tmp *t0 = TMP(-2);
1008 struct tmp *t1 = TMP(-1);
1009 struct tmp *t2 = TMP(0);
1010 o_num(sz, 4);
1011 o_num(x, 4);
1012 tmp_to(t0, R_RAX, 0);
1013 tmp_to(t1, R_RCX, 0);
1014 tmp_mv(t2, R_RDI);
1015 os("\xf3\xaa", 2); /* rep stosb */
1016 tmp_drop(2);
1019 long o_mklabel(void)
1021 return codeaddr();
1024 static long jx(int x, long addr)
1026 char op[2] = {0x0f};
1027 op[1] = x;
1028 os(op, 2); /* jx $addr */
1029 oi(addr - codeaddr() - 4, 4);
1030 return codeaddr() - 4;
1033 static long jxtest(int x, long addr)
1035 int bt = tmp_pop(R_RAX, 0);
1036 regop1(TEST_R2R, R_RAX, R_RAX, bt);
1037 return jx(x, addr);
1040 static long jxcmp(long addr, int inv)
1042 int x;
1043 if (codeaddr() != cmp_last)
1044 return -1;
1045 tmp_drop(1);
1046 cur = buf + cmp_setl;
1047 x = (unsigned char) buf[cmp_setl + 1];
1048 if (!(x & 0x10))
1049 inv = !inv;
1050 return jx((inv ? x : x ^ 0x01) & ~0x10, addr);
1053 long o_jz(long addr)
1055 long ret = jxcmp(addr, 0);
1056 return ret != -1 ? ret : jxtest(0x84, addr);
1059 long o_jnz(long addr)
1061 long ret = jxcmp(addr, 1);
1062 return ret != -1 ? ret : jxtest(0x85, addr);
1065 long o_jmp(long addr)
1067 os("\xe9", 1); /* jmp $addr */
1068 oi(addr - codeaddr() - 4, 4);
1069 return codeaddr() - 4;
1072 void o_filljmp2(long addr, long jmpdst)
1074 putint(buf + addr, jmpdst - addr - 4, 4);
1077 void o_filljmp(long addr)
1079 o_filljmp2(addr, codeaddr());
1082 void o_call(int argc, unsigned *bt, unsigned ret_bt)
1084 int i;
1085 struct tmp *t;
1086 for (i = 0; i < ARRAY_SIZE(tmpregs); i++)
1087 if (regs[tmpregs[i]] && regs[tmpregs[i]] - tmps < ntmp - argc)
1088 tmp_mem(regs[tmpregs[i]]);
1089 if (argc > R_NARGS) {
1090 long addr = sp_push(8 * (argc - R_NARGS));
1091 for (i = argc - 1; i >= R_NARGS; --i) {
1092 int reg = TMP_REG(TMP(0));
1093 tmp_pop(reg, bt[i]);
1094 memop1(MOV_R2X, reg, R_RBP,
1095 -(addr - (i - R_NARGS) * 8), BT_TMPBT(bt[i]));
1098 for (i = MIN(argc, R_NARGS) - 1; i >= 0; i--)
1099 tmp_pop(arg_regs[i], bt[i]);
1100 t = TMP(0);
1101 if (t->flags & LOC_SYM) {
1102 os("\x31\xc0", 2); /* xor %eax, %eax */
1103 os("\xe8", 1); /* call $x */
1104 if (!nogen)
1105 out_rela(t->addr, codeaddr(), 1);
1106 oi(-4 + t->off, 4);
1107 tmp_drop(1);
1108 } else {
1109 tmp_mv(TMP(0), R_RAX);
1110 tmp_drop(1);
1111 regop1(CALL_REG, 2, R_RAX, 4);
1113 if (ret_bt)
1114 tmp_push(R_RAX, ret_bt);
1117 int o_nogen(void)
1119 return nogen++;
1122 void o_dogen(void)
1124 nogen = 0;
1127 void o_datset(long addr, int off, unsigned bt)
1129 struct tmp *t = TMP(0);
1130 if (t->flags & LOC_NUM && !(t->flags & TMP_ADDR)) {
1131 num_cast(t, bt);
1132 out_datcpy(addr, off, (void *) &t->addr, BT_SZ(bt));
1134 if (t->flags & LOC_SYM && !(t->flags & TMP_ADDR)) {
1135 out_datrela(t->addr, addr, off);
1136 out_datcpy(addr, off, (void *) &t->off, BT_SZ(bt));
1138 tmp_drop(1);