cpp: save macro names in a hash table
[neatcc.git] / gen.c
blobadc82d32ada270116b1a48902ad792e102120fcb
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 while (l--) {
95 *s++ = n;
96 n >>= 8;
100 static void os(char *s, int n)
102 if (nogen)
103 return;
104 while (n--)
105 *cur++ = *s++;
108 static void oi(long n, int l)
110 if (nogen)
111 return;
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 if (s1)
218 regop1(MOVSXD, r2, r1, sz2);
219 return;
221 if (r1 != r2 || sz1 > sz2)
222 regop1(MOV_R2X, r1, r2, BT_TMPBT(bt2));
225 static void mov_m2r(int dst, int base, int off, int bt1, int bt2)
227 if (BT_SZ(bt1) < 4) {
228 memop(movxx_x2r(bt1), 2, dst, base, off,
229 bt1 & BT_SIGNED && BT_SZ(bt2) == 8 ? 8 : 4);
230 mov_r2r(dst, dst, bt1, bt2);
231 } else {
232 memop1(MOV_M2R, dst, base, off, bt1);
233 mov_r2r(dst, dst, bt1, bt2);
237 static void num_cast(struct tmp *t, unsigned bt)
239 if (!(bt & BT_SIGNED) && BT_SZ(bt) != 8)
240 t->addr &= ((1l << (long) (BT_SZ(bt) * 8)) - 1);
241 if (bt & BT_SIGNED && BT_SZ(bt) != 8 &&
242 t->addr > (1l << (BT_SZ(bt) * 8 - 1)))
243 t->addr = -((1l << (BT_SZ(bt) * 8)) - t->addr);
244 t->bt = bt;
247 static void num_reg(int reg, unsigned bt, long num)
249 int op = MOV_I2R + (reg & 7);
250 if (BT_SZ(bt) == 8 && num >= 0 && num == (unsigned) num)
251 bt = 4;
252 o_op(&op, 1, 0, reg, bt);
253 oi(num, BT_SZ(bt));
256 static void tmp_reg(struct tmp *tmp, int dst, unsigned bt, int deref)
258 if (deref && tmp->flags & TMP_ADDR)
259 tmp->flags &= ~TMP_ADDR;
260 else
261 deref = 0;
262 if (tmp->flags & LOC_NUM) {
263 num_cast(tmp, bt);
264 tmp->bt = BT_TMPBT(bt);
265 num_reg(dst, tmp->bt, tmp->addr);
266 tmp->addr = dst;
267 regs[dst] = tmp;
268 tmp->flags = LOC_NEW(tmp->flags, LOC_REG);
270 if (tmp->flags & LOC_SYM) {
271 regop1(MOV_I2X, 0, dst, 4);
272 if (!nogen)
273 out_rela(tmp->addr, codeaddr(), 0);
274 oi(tmp->off, 4);
275 tmp->addr = dst;
276 regs[dst] = tmp;
277 tmp->flags = LOC_NEW(tmp->flags, LOC_REG);
279 if (tmp->flags & LOC_REG) {
280 if (deref)
281 mov_m2r(dst, tmp->addr, 0, tmp->bt, bt);
282 else
283 mov_r2r(tmp->addr, dst, TMP_BT(tmp),
284 tmp->flags & TMP_ADDR ? 8 : bt);
285 regs[tmp->addr] = NULL;
287 if (tmp->flags & LOC_LOCAL) {
288 if (deref)
289 mov_m2r(dst, R_RBP, tmp->addr, tmp->bt, bt);
290 else
291 memop1(LEA_M2R, dst, R_RBP, tmp->addr, 8);
293 if (tmp->flags & LOC_MEM) {
294 int nbt = deref ? 8 : TMP_BT(tmp);
295 mov_m2r(dst, R_RBP, tmp->addr, nbt, nbt);
296 if (deref)
297 mov_m2r(dst, dst, 0, tmp->bt, bt);
299 tmp->addr = dst;
300 tmp->bt = tmp->flags & TMP_ADDR ? bt : BT_TMPBT(bt);
301 regs[dst] = tmp;
302 tmp->flags = LOC_NEW(tmp->flags, LOC_REG);
305 static void reg_free(int reg)
307 int i;
308 if (!regs[reg])
309 return;
310 for (i = 0; i < ARRAY_SIZE(tmpregs); i++)
311 if (!regs[tmpregs[i]]) {
312 tmp_reg(regs[reg], tmpregs[i], regs[reg]->bt, 0);
313 return;
315 tmp_mem(regs[reg]);
318 static void reg_for(int reg, struct tmp *t)
320 if (regs[reg] && regs[reg] != t)
321 reg_free(reg);
324 static void tmp_mv(struct tmp *t, int reg)
326 reg_for(reg, t);
327 tmp_reg(t, reg, t->bt, 0);
330 static void tmp_to(struct tmp *t, int reg, int bt)
332 reg_for(reg, t);
333 tmp_reg(t, reg, bt ? bt : t->bt, 1);
336 static void tmp_drop(int n)
338 int i;
339 for (i = ntmp - n; i < ntmp; i++)
340 if (tmps[i].flags & LOC_REG)
341 regs[tmps[i].addr] = NULL;
342 cmp_last = -1;
343 ntmp -= n;
346 static int tmp_pop(int reg, int bt)
348 struct tmp *t = TMP(0);
349 tmp_to(t, reg, bt);
350 tmp_drop(1);
351 return t->bt;
354 static struct tmp *tmp_new(void)
356 cmp_last = -1;
357 return &tmps[ntmp++];
360 static void tmp_push(int reg, unsigned bt)
362 struct tmp *t = tmp_new();
363 t->addr = reg;
364 t->bt = bt;
365 t->flags = LOC_REG;
366 regs[reg] = t;
369 void o_local(long addr, unsigned bt)
371 struct tmp *t = tmp_new();
372 t->addr = -addr;
373 t->bt = bt;
374 t->flags = LOC_LOCAL | TMP_ADDR;
377 void o_num(long num, unsigned bt)
379 struct tmp *t = tmp_new();
380 t->addr = num;
381 t->bt = bt;
382 t->flags = LOC_NUM;
385 void o_symaddr(long addr, unsigned bt)
387 struct tmp *t = tmp_new();
388 t->bt = bt;
389 t->addr = addr;
390 t->flags = LOC_SYM | TMP_ADDR;
391 t->off = 0;
394 void o_tmpdrop(int n)
396 int i;
397 if (n == -1 || n > ntmp)
398 n = ntmp;
399 tmp_drop(n);
400 if (!ntmp) {
401 if (tmpsp != -1)
402 sp = tmpsp;
403 tmpsp = -1;
407 #define FORK_REG R_RAX
409 /* make sure tmps remain intact after a conditional expression */
410 void o_fork(void)
412 int i;
413 for (i = 0; i < ntmp - 1; i++)
414 tmp_mem(&tmps[i]);
417 void o_forkpush(void)
419 tmp_pop(R_RAX, 0);
422 void o_forkjoin(void)
424 tmp_push(FORK_REG, 0);
427 void o_tmpswap(void)
429 struct tmp *t1 = TMP(0);
430 struct tmp *t2 = TMP(1);
431 struct tmp t;
432 memcpy(&t, t1, sizeof(t));
433 memcpy(t1, t2, sizeof(t));
434 memcpy(t2, &t, sizeof(t));
435 if (t1->flags & LOC_REG)
436 regs[t1->addr] = t1;
437 if (t2->flags & LOC_REG)
438 regs[t2->addr] = t2;
441 static int reg_get(int mask)
443 int i;
444 for (i = 0; i < ARRAY_SIZE(tmpregs); i++)
445 if ((1 << tmpregs[i]) & mask && !regs[tmpregs[i]])
446 return tmpregs[i];
447 for (i = 0; i < ARRAY_SIZE(tmpregs); i++)
448 if ((1 << tmpregs[i]) & mask) {
449 reg_free(tmpregs[i]);
450 return tmpregs[i];
452 return 0;
455 void o_tmpcopy(void)
457 struct tmp *t1 = TMP(0);
458 struct tmp *t2 = tmp_new();
459 memcpy(t2, t1, sizeof(*t1));
460 if (!(t1->flags & (LOC_REG | LOC_MEM)))
461 return;
462 if (t1->flags & LOC_MEM) {
463 tmp_reg(t2, reg_get(~0), t2->bt, 0);
464 } else if (t1->flags & LOC_REG) {
465 t2->addr = reg_get(~(1 << t1->addr));
466 regop1(MOV_R2X, t1->addr, t2->addr, BT_TMPBT(TMP_BT(t1)));
467 regs[t2->addr] = t2;
469 t2->flags = t1->flags;
472 void o_cast(unsigned bt)
474 struct tmp *t = TMP(0);
475 int reg;
476 if (t->bt == bt)
477 return;
478 if (t->flags & LOC_NUM) {
479 num_cast(t, bt);
480 return;
482 reg = BT_SZ(bt) == 1 ? TMP_BYTEREG(t) : TMP_REG(t);
483 tmp_to(t, reg, bt);
486 long o_func_beg(char *name, int global)
488 long addr = out_func_beg(name, global);
489 cur = buf;
490 os("\x55", 1); /* push %rbp */
491 os("\x48\x89\xe5", 3); /* mov %rsp, %rbp */
492 sp = 0;
493 maxsp = 0;
494 ntmp = 0;
495 tmpsp = -1;
496 nret = 0;
497 cmp_last = -1;
498 memset(regs, 0, sizeof(regs));
499 os("\x48\x81\xec", 3); /* sub $xxx, %rsp */
500 spsub_addr = codeaddr();
501 oi(0, 4);
502 return addr;
505 void o_deref(unsigned bt)
507 struct tmp *t = TMP(0);
508 if (t->flags & TMP_ADDR)
509 tmp_to(t, TMP_REG(t), t->bt);
510 t->bt = bt;
511 t->flags |= TMP_ADDR;
514 void o_load(void)
516 struct tmp *t = TMP(0);
517 tmp_to(t, TMP_REG(t), t->bt);
520 static unsigned bt_op(unsigned bt1, unsigned bt2)
522 unsigned s1 = BT_SZ(bt1);
523 unsigned s2 = BT_SZ(bt2);
524 unsigned bt = (bt1 | bt2) & BT_SIGNED | (s1 > s2 ? s1 : s2);
525 return BT_TMPBT(bt);
528 #define TMP_CONST(t) ((t)->flags & LOC_NUM && !((t)->flags & TMP_ADDR))
529 #define LOCAL_PTR(t) ((t)->flags & LOC_LOCAL && !((t)->flags & TMP_ADDR))
530 #define SYM_PTR(t) ((t)->flags & LOC_SYM && !((t)->flags & TMP_ADDR))
532 int o_popnum(long *c)
534 struct tmp *t = TMP(0);
535 if (!TMP_CONST(t))
536 return 1;
537 *c = t->addr;
538 tmp_drop(1);
539 return 0;
542 static int c_binop(long (*cop)(long a, long b, unsigned bt), unsigned bt)
544 struct tmp *t1 = TMP(0);
545 struct tmp *t2 = TMP(1);
546 int locals = LOCAL_PTR(t1) + LOCAL_PTR(t2);
547 int syms = SYM_PTR(t1) + SYM_PTR(t2);
548 int nums = TMP_CONST(t1) + TMP_CONST(t2);
549 if (syms == 2 || syms && locals || syms + nums + locals != 2)
550 return 1;
551 if (!bt) {
552 if (!locals)
553 bt = syms ? 8 : bt_op(t1->bt, t2->bt);
554 if (locals == 1)
555 bt = 8;
556 if (locals == 2)
557 bt = 4 | BT_SIGNED;
559 if (syms) {
560 long o1 = SYM_PTR(t1) ? t1->off : t1->addr;
561 long o2 = SYM_PTR(t2) ? t2->off : t2->addr;
562 long ret = cop(o2, o1, bt);
563 if (!SYM_PTR(t2))
564 o_tmpswap();
565 t2->off = ret;
566 tmp_drop(1);
567 } else {
568 long ret = cop(t2->addr, t1->addr, locals ? 4 | BT_SIGNED : bt);
569 tmp_drop(2);
570 if (locals == 1) {
571 o_local(-ret, bt);
572 o_addr();
573 } else {
574 o_num(ret, bt);
577 return 0;
580 static int c_op(long (*cop)(long a, unsigned bt), unsigned bt)
582 struct tmp *t1 = TMP(0);
583 long ret;
584 if (!TMP_CONST(t1))
585 return 1;
586 if (!bt)
587 bt = t1->bt;
588 ret = cop(t1->addr, bt);
589 tmp_drop(1);
590 o_num(ret, bt);
591 return 0;
594 static void shx(int uop, int sop)
596 struct tmp *t2 = TMP(1);
597 int r2 = TMP_REG2(t2, R_RCX);
598 int bt;
599 tmp_pop(R_RCX, 0);
600 bt = tmp_pop(r2, 0);
601 regop1(SHX_REG, bt & BT_SIGNED ? sop : uop, r2, BT_TMPBT(bt));
602 tmp_push(r2, bt);
605 static long c_shl(long a, long b, unsigned bt)
607 return a << b;
610 void o_shl(void)
612 if (!c_binop(c_shl, 0))
613 return;
614 shx(4, 4);
617 static long c_shr(long a, long b, unsigned bt)
619 if (bt & BT_SIGNED)
620 return a >> b;
621 else
622 return (unsigned long) a >> b;
625 void o_shr(void)
627 if (!c_binop(c_shr, 0))
628 return;
629 shx(5, 7);
632 static int mulop(int uop, int sop, int reg)
634 struct tmp *t1 = TMP(0);
635 struct tmp *t2 = TMP(1);
636 int bt1 = TMP_BT(t1);
637 int bt2 = TMP_BT(t2);
638 int bt = bt_op(bt1, bt2);
639 if (t1->flags & LOC_REG && t1->addr != R_RAX && t1->addr != R_RDX)
640 reg = t1->addr;
641 tmp_to(t1, reg, bt);
642 tmp_to(t2, R_RAX, bt);
643 if (reg != R_RDX) {
644 int cqo = 0x99;
645 reg_free(R_RDX);
646 if (bt & BT_SIGNED)
647 o_op(&cqo, 1, R_RAX, R_RDX, bt);
648 else
649 regop1(XOR_R2X, R_RDX, R_RDX, bt);
651 tmp_drop(2);
652 regop1(MUL_A2X, bt & BT_SIGNED ? sop : uop, reg, BT_TMPBT(bt2));
653 return bt;
656 static long c_mul(long a, long b, unsigned bt)
658 return a * b;
661 void o_mul(void)
663 int bt;
664 if (!c_binop(c_mul, 0))
665 return;
666 bt = mulop(4, 5, R_RDX);
667 tmp_push(R_RAX, bt);
670 static long c_div(long a, long b, unsigned bt)
672 return a / b;
675 void o_div(void)
677 int bt;
678 if (!c_binop(c_div, 0))
679 return;
680 bt = mulop(6, 7, R_RCX);
681 tmp_push(R_RAX, bt);
684 static long c_mod(long a, long b, unsigned bt)
686 return a % b;
689 void o_mod(void)
691 int bt;
692 if (!c_binop(c_mod, 0))
693 return;
694 bt = mulop(6, 7, R_RCX);
695 tmp_push(R_RDX, bt);
698 void o_addr(void)
700 tmps[ntmp - 1].flags &= ~TMP_ADDR;
701 tmps[ntmp - 1].bt = 8;
704 void o_ret(unsigned bt)
706 if (bt)
707 tmp_pop(R_RAX, bt);
708 else
709 os("\x31\xc0", 2); /* xor %eax, %eax */
710 ret[nret++] = o_jmp(0);
713 static int binop(int op, int *reg)
715 struct tmp *t1 = TMP(0);
716 struct tmp *t2 = TMP(1);
717 int r1, r2;
718 unsigned bt = bt_op(t1->bt, t2->bt);
719 r1 = TMP_REG(t1);
720 r2 = TMP_REG2(t2, r1);
721 tmp_pop(r1, bt);
722 tmp_pop(r2, bt);
723 regop1(op, r2, r1, bt);
724 *reg = r2;
725 return bt;
728 static long c_add(long a, long b, unsigned bt)
730 return a + b;
733 void o_add(void)
735 int reg;
736 int bt;
737 if (!c_binop(c_add, 0))
738 return;
739 bt = binop(ADD_R2X, &reg);
740 tmp_push(reg, bt);
743 static long c_xor(long a, long b, unsigned bt)
745 return a ^ b;
748 void o_xor(void)
750 int reg;
751 int bt;
752 if (!c_binop(c_xor, 0))
753 return;
754 bt = binop(XOR_R2X, &reg);
755 tmp_push(reg, bt);
758 static long c_and(long a, long b, unsigned bt)
760 return a & b;
763 void o_and(void)
765 int reg;
766 int bt;
767 if (!c_binop(c_and, 0))
768 return;
769 bt = binop(AND_R2X, &reg);
770 tmp_push(reg, bt);
773 static long c_or(long a, long b, unsigned bt)
775 return a | b;
778 void o_or(void)
780 int reg;
781 int bt;
782 if (!c_binop(c_or, 0))
783 return;
784 bt = binop(OR_R2X, &reg);
785 tmp_push(reg, bt);
788 static long c_sub(long a, long b, unsigned bt)
790 return a - b;
793 void o_sub(void)
795 int reg;
796 int bt;
797 if (!c_binop(c_sub, 0))
798 return;
799 bt = binop(SUB_R2X, &reg);
800 tmp_push(reg, bt);
803 static void o_cmp(int uop, int sop)
805 struct tmp *t1 = TMP(0);
806 struct tmp *t2 = TMP(1);
807 char set[] = "\x0f\x00\xc0";
808 int reg;
809 int bt;
810 if (regs[R_RAX] && regs[R_RAX] != t1 && regs[R_RAX] != t2)
811 reg_free(R_RAX);
812 bt = binop(CMP_R2X, &reg);
813 set[1] = bt & BT_SIGNED ? sop : uop;
814 cmp_setl = codeaddr();
815 os(set, 3); /* setl %al */
816 os("\x0f\xb6\xc0", 3); /* movzbl %al, %eax */
817 tmp_push(R_RAX, 4 | BT_SIGNED);
818 cmp_last = codeaddr();
821 static long c_lt(long a, long b, unsigned bt)
823 return a < b;
826 void o_lt(void)
828 if (!c_binop(c_lt, 4))
829 return;
830 o_cmp(0x92, 0x9c);
833 static long c_gt(long a, long b, unsigned bt)
835 return a > b;
838 void o_gt(void)
840 if (!c_binop(c_gt, 4))
841 return;
842 o_cmp(0x97, 0x9f);
845 static long c_le(long a, long b, unsigned bt)
847 return a <= b;
850 void o_le(void)
852 if (!c_binop(c_le, 4))
853 return;
854 o_cmp(0x96, 0x9e);
857 static long c_ge(long a, long b, unsigned bt)
859 return a >= b;
862 void o_ge(void)
864 if (!c_binop(c_ge, 4))
865 return;
866 o_cmp(0x93, 0x9d);
869 static long c_eq(long a, long b, unsigned bt)
871 return a == b;
874 void o_eq(void)
876 if (!c_binop(c_eq, 4))
877 return;
878 o_cmp(0x94, 0x94);
881 static long c_neq(long a, long b, unsigned bt)
883 return a != b;
886 void o_neq(void)
888 if (!c_binop(c_neq, 4))
889 return;
890 o_cmp(0x95, 0x95);
893 static long c_lnot(long a, unsigned bt)
895 return !a;
898 void o_lnot(void)
900 if (!c_op(c_lnot, 4))
901 return;
902 if (cmp_last == codeaddr()) {
903 buf[cmp_setl + 1] ^= 0x01;
904 } else {
905 o_num(0, 4 | BT_SIGNED);
906 o_eq();
910 static long c_neg(long a, unsigned bt)
912 return -a;
915 void o_neg(void)
917 struct tmp *t = TMP(0);
918 int reg;
919 unsigned bt = BT_TMPBT(t->bt);
920 if (!c_op(c_neg, t->bt | BT_SIGNED))
921 return;
922 reg = TMP_REG(t);
923 tmp_to(t, reg, bt);
924 regop1(NEG_REG, 3, reg, bt);
927 static long c_not(long a, unsigned bt)
929 return ~a;
932 void o_not(void)
934 struct tmp *t = TMP(0);
935 int reg;
936 unsigned bt = BT_TMPBT(t->bt);
937 if (!c_op(c_not, 0))
938 return;
939 reg = TMP_REG(t);
940 tmp_to(t, reg, bt);
941 regop1(NOT_REG, 2, reg, bt);
944 void o_func_end(void)
946 int i;
947 for (i = 0; i < nret; i++)
948 o_filljmp(ret[i]);
949 os("\xc9\xc3", 2); /* leave; ret; */
950 putint(buf + spsub_addr, (maxsp + 7) & ~0x07, 4);
951 out_func_end(buf, cur - buf);
954 long o_mklocal(int size)
956 return sp_push((size + 7) & ~0x07);
959 void o_rmlocal(long addr, int sz)
961 sp = addr - sz;
964 static int arg_regs[] = {R_RDI, R_RSI, R_RDX, R_RCX, R_R8, R_R9};
966 #define R_NARGS ARRAY_SIZE(arg_regs)
968 long o_arg(int i, unsigned bt)
970 long addr;
971 if (i < R_NARGS) {
972 addr = o_mklocal(BT_SZ(bt));
973 memop1(MOV_R2X, arg_regs[i], R_RBP, -addr, bt);
974 } else {
975 addr = -8 * (i - R_NARGS + 2);
977 return addr;
980 void o_assign(unsigned bt)
982 struct tmp *t1 = TMP(0);
983 struct tmp *t2 = TMP(1);
984 int r1 = BT_SZ(bt) > 1 ? TMP_REG(t1) : TMP_BYTEREG(t1);
985 int reg;
986 int off;
987 tmp_to(t1, r1, BT_TMPBT(bt));
988 if (t2->flags & LOC_LOCAL) {
989 reg = R_RBP;
990 off = t2->addr;
991 } else {
992 reg = TMP_REG2(t2, r1);
993 off = 0;
994 tmp_mv(t2, reg);
996 tmp_drop(2);
997 memop1(MOV_R2X, r1, reg, off, bt);
998 tmp_push(r1, bt);
1001 void o_memcpy(int sz)
1003 struct tmp *t0 = TMP(-1);
1004 struct tmp *t1 = TMP(0);
1005 struct tmp *t2 = TMP(1);
1006 o_num(sz, 4);
1007 tmp_to(t0, R_RCX, 0);
1008 tmp_mv(t1, R_RSI);
1009 tmp_mv(t2, R_RDI);
1010 os("\xf3\xa4", 2); /* rep movs */
1011 tmp_drop(2);
1014 void o_memset(int x, int sz)
1016 struct tmp *t0 = TMP(-2);
1017 struct tmp *t1 = TMP(-1);
1018 struct tmp *t2 = TMP(0);
1019 o_num(sz, 4);
1020 o_num(x, 4);
1021 tmp_to(t0, R_RAX, 0);
1022 tmp_to(t1, R_RCX, 0);
1023 tmp_mv(t2, R_RDI);
1024 os("\xf3\xaa", 2); /* rep stosb */
1025 tmp_drop(2);
1028 long o_mklabel(void)
1030 return codeaddr();
1033 static long jx(int x, long addr)
1035 char op[2] = {0x0f};
1036 op[1] = x;
1037 os(op, 2); /* jx $addr */
1038 oi(addr - codeaddr() - 4, 4);
1039 return codeaddr() - 4;
1042 static long jxtest(int x, long addr)
1044 int bt = tmp_pop(R_RAX, 0);
1045 regop1(TEST_R2R, R_RAX, R_RAX, bt);
1046 return jx(x, addr);
1049 static long jxcmp(long addr, int inv)
1051 int x;
1052 if (codeaddr() != cmp_last)
1053 return -1;
1054 tmp_drop(1);
1055 cur = buf + cmp_setl;
1056 x = (unsigned char) buf[cmp_setl + 1];
1057 return jx((inv ? x : x ^ 0x01) & ~0x10, addr);
1060 long o_jz(long addr)
1062 long ret = jxcmp(addr, 0);
1063 return ret != -1 ? ret : jxtest(0x84, addr);
1066 long o_jnz(long addr)
1068 long ret = jxcmp(addr, 1);
1069 return ret != -1 ? ret : jxtest(0x85, addr);
1072 long o_jmp(long addr)
1074 os("\xe9", 1); /* jmp $addr */
1075 oi(addr - codeaddr() - 4, 4);
1076 return codeaddr() - 4;
1079 void o_filljmp2(long addr, long jmpdst)
1081 putint(buf + addr, jmpdst - addr - 4, 4);
1084 void o_filljmp(long addr)
1086 o_filljmp2(addr, codeaddr());
1089 void o_call(int argc, unsigned *bt, unsigned ret_bt)
1091 int i;
1092 struct tmp *t;
1093 for (i = 0; i < ARRAY_SIZE(tmpregs); i++)
1094 if (regs[tmpregs[i]] && regs[tmpregs[i]] - tmps < ntmp - argc)
1095 tmp_mem(regs[tmpregs[i]]);
1096 if (argc > R_NARGS) {
1097 long addr = sp_push(8 * (argc - R_NARGS));
1098 for (i = argc - 1; i >= R_NARGS; --i) {
1099 int reg = TMP_REG(TMP(0));
1100 tmp_pop(reg, bt[i]);
1101 memop1(MOV_R2X, reg, R_RBP,
1102 -(addr - (i - R_NARGS) * 8), BT_TMPBT(bt[i]));
1105 for (i = MIN(argc, R_NARGS) - 1; i >= 0; i--)
1106 tmp_pop(arg_regs[i], bt[i]);
1107 t = TMP(0);
1108 if (t->flags & LOC_SYM) {
1109 os("\x31\xc0", 2); /* xor %eax, %eax */
1110 os("\xe8", 1); /* call $x */
1111 if (!nogen)
1112 out_rela(t->addr, codeaddr(), 1);
1113 oi(-4 + t->off, 4);
1114 tmp_drop(1);
1115 } else {
1116 tmp_mv(TMP(0), R_RAX);
1117 tmp_drop(1);
1118 regop1(CALL_REG, 2, R_RAX, 4);
1120 if (ret_bt)
1121 tmp_push(R_RAX, ret_bt);
1124 int o_nogen(void)
1126 return nogen++;
1129 void o_dogen(void)
1131 nogen = 0;
1134 void o_datset(long addr, int off, unsigned bt)
1136 struct tmp *t = TMP(0);
1137 if (t->flags & LOC_NUM && !(t->flags & TMP_ADDR)) {
1138 num_cast(t, bt);
1139 out_datcpy(addr, off, (void *) &t->addr, BT_SZ(bt));
1141 if (t->flags & LOC_SYM && !(t->flags & TMP_ADDR)) {
1142 out_datrela(t->addr, addr, off);
1143 out_datcpy(addr, off, (void *) &t->off, BT_SZ(bt));
1145 tmp_drop(1);