gen: swap the immediate if needed for == and !=
[neatcc.git] / gen.c
blob74b94f66e15c92aeccf2776068148176e580db16
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include "gen.h"
5 #include "out.h"
6 #include "tok.h"
8 #define LOC_REG 0x01
9 #define LOC_MEM 0x02
10 #define LOC_NUM 0x04
11 #define LOC_SYM 0x08
12 #define LOC_LOCAL 0x10
14 #define NREGS 16
16 #define REG_PC 15 /* program counter */
17 #define REG_LR 14 /* link register */
18 #define REG_SP 13 /* stack pointer */
19 #define REG_TMP 12 /* temporary register */
20 #define REG_FP 11 /* frame pointer register */
21 #define REG_DP 10 /* data pointer register */
22 #define REG_RET 0 /* returned value register */
24 #define MIN(a, b) ((a) < (b) ? (a) : (b))
25 #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
27 static char cs[SECSIZE]; /* code segment */
28 static int cslen;
29 static char ds[SECSIZE]; /* data segment */
30 static int dslen;
31 static long bsslen; /* bss segment size */
33 static int nogen; /* don't generate code */
34 static long sp;
35 static long func_beg;
36 static long maxsp;
38 #define TMP(i) (((i) < ntmp) ? &tmps[ntmp - 1 - (i)] : NULL)
40 static struct tmp {
41 long addr;
42 char sym[NAMELEN];
43 long off; /* offset from a symbol or a local */
44 unsigned loc; /* variable location */
45 unsigned bt; /* type of address; zero when not a pointer */
46 } tmps[MAXTMP];
47 static int ntmp;
49 static int tmpsp;
51 static struct tmp *regs[NREGS];
52 static int tmpregs[] = {4, 5, 6, 7, 8, 9, 0, 1, 2, 3};
53 static int argregs[] = {0, 1, 2, 3};
55 #define I_AND 0x00
56 #define I_EOR 0x01
57 #define I_SUB 0x02
58 #define I_RSB 0x03
59 #define I_ADD 0x04
60 #define I_TST 0x08
61 #define I_CMP 0x0a
62 #define I_ORR 0x0c
63 #define I_MOV 0x0d
64 #define I_MVN 0x0f
66 #define MAXRET (1 << 8)
68 static long ret[MAXRET];
69 static int nret;
71 /* output div/mod functions */
72 static int putdiv = 0;
74 static void os(void *s, int n)
76 memcpy(cs + cslen, s, n);
77 cslen += n;
80 static void oi(long n)
82 if (nogen)
83 return;
84 *(int *) (cs + cslen) = n;
85 cslen += 4;
88 #define MAXNUMS 1024
90 /* data pool */
91 static long num_offs[MAXNUMS]; /* data immediate value */
92 static char num_names[MAXNUMS][NAMELEN]; /* relocation data symbol name */
93 static int nums;
95 static int pool_find(char *name, int off)
97 int i;
98 for (i = 0; i < nums; i++)
99 if (!strcmp(name, num_names[i]) && off == num_offs[i])
100 return i;
101 return -1;
104 static int pool_num(long num)
106 int idx = pool_find("", num);
107 if (idx < 0) {
108 idx = nums++;
109 num_offs[idx] = num;
110 num_names[idx][0] = '\0';
112 return idx << 2;
115 static int pool_reloc(char *name, long off)
117 int idx = pool_find(name, off);
118 if (idx < 0) {
119 idx = nums++;
120 num_offs[idx] = off;
121 strcpy(num_names[idx], name);
123 return idx << 2;
126 static void pool_write(void)
128 int i;
129 for (i = 0; i < nums; i++) {
130 if (num_names[i])
131 out_rel(num_names[i], OUT_CS, cslen);
132 oi(num_offs[i]);
137 * data processing:
138 * +---------------------------------------+
139 * |COND|00|I| op |S| Rn | Rd | operand2 |
140 * +---------------------------------------+
142 * S: set condition code
143 * Rn: first operand
144 * Rd: destination operand
146 * I=0 operand2=| shift | Rm |
147 * I=1 operand2=|rota| imm |
149 #define ADD(op, rd, rn, s, i, cond) \
150 (((cond) << 28) | ((i) << 25) | ((s) << 20) | \
151 ((op) << 21) | ((rn) << 16) | ((rd) << 12))
153 static int add_encimm(unsigned n)
155 int i = 0;
156 while (i < 12 && (n >> ((4 + i) << 1)))
157 i++;
158 return (n >> (i << 1)) | (((16 - i) & 0x0f) << 8);
161 static int add_decimm(unsigned n)
163 int rot = (16 - ((n >> 8) & 0x0f)) & 0x0f;
164 return (n & 0xff) << (rot << 1);
167 static int add_rndimm(unsigned n)
169 int rot = (n >> 8) & 0x0f;
170 int num = n & 0xff;
171 if (rot == 0)
172 return n;
173 if (num == 0xff) {
174 num = 0;
175 rot = (rot + 12) & 0x0f;
177 return ((num + 1) & 0xff) | (rot << 8);
180 static void i_add(int op, int rd, int rn, int rm)
182 oi(ADD(op, rd, rn, 0, 0, 14) | rm);
185 static void i_add_imm(int op, int rd, int rn, long n)
187 oi(ADD(op, rd, rn, 0, 1, 14) | add_encimm(n));
190 static void i_ldr(int l, int rd, int rn, int off, int bt);
192 static void i_num(int rd, long n)
194 int neg = n < 0;
195 int p = neg ? -n - 1 : n;
196 int enc = add_encimm(p);
197 if (p == add_decimm(enc)) {
198 oi(ADD(neg ? I_MVN : I_MOV, rd, 0, 0, 1, 14) | enc);
199 } else {
200 if (!nogen) {
201 int off = pool_num(n);
202 i_ldr(1, rd, REG_DP, off, LONGSZ);
207 static void i_add_anyimm(int rd, int rn, long n)
209 int neg = n < 0;
210 int imm = add_encimm(neg ? -n : n);
211 if (imm == add_decimm(neg ? -n : n)) {
212 oi(ADD(neg ? I_SUB : I_ADD, rd, rn, 0, 1, 14) | imm);
213 } else {
214 i_num(rd, n);
215 i_add(I_ADD, rd, rd, rn);
220 * multiply
221 * +----------------------------------------+
222 * |COND|000000|A|S| Rd | Rn | Rs |1001| Rm |
223 * +----------------------------------------+
225 * Rd: destination
226 * A: accumulate
227 * C: set condition codes
229 * I=0 operand2=| shift | Rm |
230 * I=1 operand2=|rota| imm |
232 #define MUL(rd, rn, rs) \
233 ((14 << 28) | ((rd) << 16) | ((0) << 12) | ((rn) << 8) | ((9) << 4) | (rm))
235 static void i_mul(int rd, int rn, int rm)
237 oi(MUL(rd, rn, rm));
240 static void i_cmp(int op, int rn, int rm)
242 oi(ADD(op, 0, rn, 1, 0, 14) | rm);
245 static void i_cmp_imm(int op, int rn, long n)
247 oi(ADD(op, 0, rn, 1, 1, 14) | add_encimm(n));
250 static void i_set(int cond, int rd)
252 oi(ADD(I_MOV, rd, 0, 0, 1, 14));
253 oi(ADD(I_MOV, rd, 0, 0, 1, cond) | 1);
256 #define SM_LSL 0
257 #define SM_LSR 1
258 #define SM_ASR 2
260 static void i_shl(int sm, int rd, int rm, int rs)
262 oi(ADD(I_MOV, rd, 0, 0, 0, 14) | (rs << 8) | (sm << 5) | (1 << 4) | rm);
265 static void i_shl_imm(int sm, int rd, int rn, long n)
267 oi(ADD(I_MOV, rd, 0, 0, 0, 14) | (n << 7) | (sm << 5) | rn);
270 static void i_mov(int op, int rd, int rn)
272 oi(ADD(op, rd, 0, 0, 0, 14) | rn);
276 * single data transfer:
277 * +------------------------------------------+
278 * |COND|01|I|P|U|B|W|L| Rn | Rd | offset |
279 * +------------------------------------------+
281 * I: immediate/offset
282 * P: post/pre indexing
283 * U: down/up
284 * B: byte/word
285 * W: writeback
286 * L: store/load
287 * Rn: base register
288 * Rd: source/destination register
290 * I=0 offset=| immediate |
291 * I=1 offset=| shift | Rm |
293 * halfword and signed data transfer
294 * +----------------------------------------------+
295 * |COND|000|P|U|0|W|L| Rn | Rd |0000|1|S|H|1| Rm |
296 * +----------------------------------------------+
298 * +----------------------------------------------+
299 * |COND|000|P|U|1|W|L| Rn | Rd |off1|1|S|H|1|off2|
300 * +----------------------------------------------+
302 * S: singed
303 * H: halfword
305 #define LDR(l, rd, rn, b, u, p, w) \
306 ((14 << 28) | (1 << 26) | ((p) << 24) | ((b) << 22) | ((u) << 23) | \
307 ((w) << 21) | ((l) << 20) | ((rn) << 16) | ((rd) << 12))
308 #define LDRH(l, rd, rn, s, h, u, i) \
309 ((14 << 28) | (1 << 24) | ((u) << 23) | ((i) << 22) | ((l) << 20) | \
310 ((rn) << 16) | ((rd) << 12) | ((s) << 6) | ((h) << 5) | (9 << 4))
312 static void i_ldr(int l, int rd, int rn, int off, int bt)
314 int b = BT_SZ(bt) == 1;
315 int h = BT_SZ(bt) == 2;
316 int s = l && (bt & BT_SIGNED);
317 int half = h || (b && s);
318 int maximm = half ? 0x100 : 0x1000;
319 int neg = off < 0;
320 if (neg)
321 off = -off;
322 while (off >= maximm) {
323 int imm = add_encimm(off);
324 oi(ADD(neg ? I_SUB : I_ADD, REG_TMP, rn, 0, 1, 14) | imm);
325 rn = REG_TMP;
326 off -= add_decimm(imm);
328 if (!half)
329 oi(LDR(l, rd, rn, b, !neg, 1, 0) | off);
330 else
331 oi(LDRH(l, rd, rn, s, h, !neg, 1) |
332 ((off & 0xf0) << 4) | (off & 0x0f));
335 static void i_sym(int rd, char *sym, int off)
337 if (!nogen) {
338 int doff = pool_reloc(sym, off);
339 i_ldr(1, rd, REG_DP, doff, LONGSZ);
343 static void i_neg(int rd)
345 oi(ADD(I_RSB, rd, rd, 0, 1, 14));
348 static void i_not(int rd)
350 oi(ADD(I_MVN, rd, 0, 0, 0, 14) | rd);
353 static void i_lnot(int rd)
355 i_cmp(I_TST, rd, rd);
356 oi(ADD(I_MOV, rd, 0, 0, 1, 14));
357 oi(ADD(I_MOV, rd, 0, 0, 1, 0) | 1);
360 /* rd = rd & ((1 << bits) - 1) */
361 static void i_zx(int rd, int bits)
363 if (bits <= 8) {
364 oi(ADD(I_AND, rd, rd, 0, 1, 14) | add_encimm((1 << bits) - 1));
365 } else {
366 i_shl_imm(SM_LSL, rd, rd, 32 - bits);
367 i_shl_imm(SM_LSR, rd, rd, 32 - bits);
371 static void i_sx(int rd, int bits)
373 i_shl_imm(SM_LSL, rd, rd, 32 - bits);
374 i_shl_imm(SM_ASR, rd, rd, 32 - bits);
378 * branch:
379 * +-----------------------------------+
380 * |COND|101|L| offset |
381 * +-----------------------------------+
383 * L: link
385 #define BL(cond, l, o) (((cond) << 28) | (5 << 25) | ((l) << 24) | \
386 ((((o) - 8) >> 2) & 0x00ffffff))
388 static void i_b(long addr)
390 oi(BL(14, 0, addr - cslen));
393 static void i_b_if(long addr, int rn, int z)
395 i_cmp(I_TST, rn, rn);
396 oi(BL(z ? 0 : 1, 0, addr - cslen));
399 static void i_b_fill(long *dst, int diff)
401 *dst = (*dst & 0xff000000) | (((diff - 8) >> 2) & 0x00ffffff);
404 static void i_memcpy(int rd, int rs, int rn)
406 oi(ADD(I_SUB, rn, rn, 1, 1, 14) | 1);
407 oi(BL(4, 0, 16));
408 oi(LDR(1, REG_TMP, rs, 1, 1, 0, 0) | 1);
409 oi(LDR(0, REG_TMP, rd, 1, 1, 0, 0) | 1);
410 oi(BL(14, 0, -16));
413 static void i_memset(int rd, int rs, int rn)
415 oi(ADD(I_SUB, rn, rn, 1, 1, 14) | 1);
416 oi(BL(4, 0, 12));
417 oi(LDR(0, rs, rd, 1, 1, 0, 0) | 1);
418 oi(BL(14, 0, -12));
421 static void i_call_reg(int rd)
423 i_mov(I_MOV, REG_LR, REG_PC);
424 i_mov(I_MOV, REG_PC, rd);
427 static void i_call(char *sym)
429 if (!nogen)
430 out_rel(sym, OUT_CS | OUT_REL24, cslen);
431 oi(BL(14, 1, 0));
434 static void i_prolog(void)
436 func_beg = cslen;
437 nums = 0;
438 oi(0xe1a0c00d); /* mov r12, sp */
439 oi(0xe92d000f); /* stmfd sp!, {r0-r3} */
440 oi(0xe92d5ff0); /* stmfd sp!, {r0-r11, r12, lr} */
441 oi(0xe1a0b00d); /* mov fp, sp */
442 oi(0xe24dd000); /* sub sp, sp, xx */
443 oi(0xe28fa000); /* add dp, pc, xx */
446 static void i_epilog(void)
448 int dpoff;
449 oi(0xe89baff0); /* ldmfd fp, {r4-r11, sp, pc} */
450 dpoff = add_decimm(add_rndimm(add_encimm(cslen - func_beg - 28)));
451 cslen = func_beg + dpoff + 28;
452 maxsp = ALIGN(maxsp, 8);
453 maxsp = add_decimm(add_rndimm(add_encimm(maxsp)));
454 /* fill stack sub: sp = sp - xx */
455 *(long *) (cs + func_beg + 16) |= add_encimm(maxsp);
456 /* fill data ptr addition: dp = pc + xx */
457 *(long *) (cs + func_beg + 20) |= add_encimm(dpoff);
458 pool_write();
461 static long sp_push(int size)
463 sp += size;
464 if (sp > maxsp)
465 maxsp = sp;
466 return sp;
469 static void tmp_mem(struct tmp *tmp)
471 int src = tmp->addr;
472 if (!(tmp->loc == LOC_REG))
473 return;
474 if (tmpsp == -1)
475 tmpsp = sp;
476 tmp->addr = -sp_push(LONGSZ);
477 i_ldr(0, src, REG_FP, tmp->addr, LONGSZ);
478 regs[src] = NULL;
479 tmp->loc = LOC_MEM;
482 static void num_cast(struct tmp *t, unsigned bt)
484 if (!(bt & BT_SIGNED) && BT_SZ(bt) != LONGSZ)
485 t->addr &= ((1l << (long) (BT_SZ(bt) * 8)) - 1);
486 if (bt & BT_SIGNED && BT_SZ(bt) != LONGSZ &&
487 t->addr > (1l << (BT_SZ(bt) * 8 - 1)))
488 t->addr = -((1l << (BT_SZ(bt) * 8)) - t->addr);
491 static void tmp_reg(struct tmp *tmp, int dst, int deref)
493 int bt = tmp->bt;
494 if (!tmp->bt)
495 deref = 0;
496 if (deref)
497 tmp->bt = 0;
498 if (tmp->loc == LOC_NUM) {
499 i_num(dst, tmp->addr);
500 tmp->addr = dst;
501 regs[dst] = tmp;
502 tmp->loc = LOC_REG;
504 if (tmp->loc == LOC_SYM) {
505 i_sym(dst, tmp->sym, tmp->off);
506 tmp->addr = dst;
507 regs[dst] = tmp;
508 tmp->loc = LOC_REG;
510 if (tmp->loc == LOC_REG) {
511 if (deref)
512 i_ldr(1, dst, tmp->addr, 0, bt);
513 else if (dst != tmp->addr)
514 i_mov(I_MOV, dst, tmp->addr);
515 regs[tmp->addr] = NULL;
517 if (tmp->loc == LOC_LOCAL) {
518 if (deref)
519 i_ldr(1, dst, REG_FP, tmp->addr + tmp->off, bt);
520 else
521 i_add_anyimm(dst, REG_FP, tmp->addr + tmp->off);
523 if (tmp->loc == LOC_MEM) {
524 i_ldr(1, dst, REG_FP, tmp->addr, LONGSZ);
525 if (deref)
526 i_ldr(1, dst, dst, 0, bt);
528 tmp->addr = dst;
529 regs[dst] = tmp;
530 tmp->loc = LOC_REG;
533 static void reg_free(int reg)
535 int i;
536 if (!regs[reg])
537 return;
538 for (i = 0; i < ARRAY_SIZE(tmpregs); i++)
539 if (!regs[tmpregs[i]]) {
540 tmp_reg(regs[reg], tmpregs[i], 0);
541 return;
543 tmp_mem(regs[reg]);
546 static void reg_for(int reg, struct tmp *t)
548 if (regs[reg] && regs[reg] != t)
549 reg_free(reg);
552 static void tmp_mv(struct tmp *t, int reg)
554 reg_for(reg, t);
555 tmp_reg(t, reg, 0);
558 static void tmp_to(struct tmp *t, int reg)
560 reg_for(reg, t);
561 tmp_reg(t, reg, 1);
564 static void tmp_drop(int n)
566 int i;
567 for (i = ntmp - n; i < ntmp; i++)
568 if (tmps[i].loc == LOC_REG)
569 regs[tmps[i].addr] = NULL;
570 ntmp -= n;
573 static void tmp_pop(int reg)
575 struct tmp *t = TMP(0);
576 tmp_to(t, reg);
577 tmp_drop(1);
580 static struct tmp *tmp_new(void)
582 return &tmps[ntmp++];
585 static void tmp_push(int reg)
587 struct tmp *t = tmp_new();
588 t->addr = reg;
589 t->bt = 0;
590 t->loc = LOC_REG;
591 regs[reg] = t;
594 void o_local(long addr)
596 struct tmp *t = tmp_new();
597 t->addr = -addr;
598 t->loc = LOC_LOCAL;
599 t->bt = 0;
600 t->off = 0;
603 void o_num(long num)
605 struct tmp *t = tmp_new();
606 t->addr = num;
607 t->bt = 0;
608 t->loc = LOC_NUM;
611 void o_sym(char *name)
613 struct tmp *t = tmp_new();
614 strcpy(t->sym, name);
615 t->loc = LOC_SYM;
616 t->bt = 0;
617 t->off = 0;
620 void o_tmpdrop(int n)
622 if (n == -1 || n > ntmp)
623 n = ntmp;
624 tmp_drop(n);
625 if (!ntmp) {
626 if (tmpsp != -1)
627 sp = tmpsp;
628 tmpsp = -1;
632 #define FORK_REG 0x00
634 /* make sure tmps remain intact after a conditional expression */
635 void o_fork(void)
637 int i;
638 for (i = 0; i < ntmp - 1; i++)
639 tmp_mem(&tmps[i]);
642 void o_forkpush(void)
644 tmp_pop(FORK_REG);
647 void o_forkjoin(void)
649 tmp_push(FORK_REG);
652 void o_tmpswap(void)
654 struct tmp *t1 = TMP(0);
655 struct tmp *t2 = TMP(1);
656 struct tmp t;
657 memcpy(&t, t1, sizeof(t));
658 memcpy(t1, t2, sizeof(t));
659 memcpy(t2, &t, sizeof(t));
660 if (t1->loc == LOC_REG)
661 regs[t1->addr] = t1;
662 if (t2->loc == LOC_REG)
663 regs[t2->addr] = t2;
666 static int reg_get(int mask)
668 int i;
669 for (i = 0; i < ARRAY_SIZE(tmpregs); i++)
670 if ((1 << tmpregs[i]) & mask && !regs[tmpregs[i]])
671 return tmpregs[i];
672 for (i = 0; i < ARRAY_SIZE(tmpregs); i++)
673 if ((1 << tmpregs[i]) & mask) {
674 reg_free(tmpregs[i]);
675 return tmpregs[i];
677 return 0;
680 static int reg_fortmp(struct tmp *t, int notmask)
682 if (t->loc == LOC_REG && !(notmask & (1 << t->addr)))
683 return t->addr;
684 return reg_get(~notmask);
687 static void tmp_copy(struct tmp *t1)
689 struct tmp *t2 = tmp_new();
690 memcpy(t2, t1, sizeof(*t1));
691 if (!(t1->loc & (LOC_REG | LOC_MEM)))
692 return;
693 if (t1->loc == LOC_MEM) {
694 tmp_mv(t2, reg_get(~0));
695 } else if (t1->loc == LOC_REG) {
696 t2->addr = reg_fortmp(t2, 1 << t1->addr);
697 i_mov(I_MOV, t2->addr, t1->addr);
698 regs[t2->addr] = t2;
702 void o_tmpcopy(void)
704 tmp_copy(TMP(0));
707 void o_cast(unsigned bt)
709 struct tmp *t = TMP(0);
710 if (!t->bt && t->loc == LOC_NUM) {
711 num_cast(t, bt);
712 return;
714 if (BT_SZ(bt) != LONGSZ) {
715 int reg = reg_fortmp(t, 0);
716 tmp_to(t, reg);
717 if (bt & BT_SIGNED)
718 i_sx(reg, BT_SZ(bt) * 8);
719 else
720 i_zx(reg, BT_SZ(bt) * 8);
724 void o_func_beg(char *name, int global)
726 out_sym(name, (global ? OUT_GLOB : 0) | OUT_CS, cslen, 0);
727 i_prolog();
728 sp = 0;
729 maxsp = sp;
730 ntmp = 0;
731 tmpsp = -1;
732 nret = 0;
733 memset(regs, 0, sizeof(regs));
736 void o_deref(unsigned bt)
738 struct tmp *t = TMP(0);
739 if (t->bt)
740 tmp_to(t, reg_fortmp(t, 0));
741 t->bt = bt;
744 void o_load(void)
746 struct tmp *t = TMP(0);
747 tmp_to(t, reg_fortmp(t, 0));
750 #define TMP_NUM(t) ((t)->loc == LOC_NUM && !(t)->bt)
751 #define LOCAL_PTR(t) ((t)->loc == LOC_LOCAL && !(t)->bt)
752 #define SYM_PTR(t) ((t)->loc == LOC_SYM && !(t)->bt)
754 int o_popnum(long *c)
756 struct tmp *t = TMP(0);
757 if (!TMP_NUM(t))
758 return 1;
759 *c = t->addr;
760 tmp_drop(1);
761 return 0;
764 void o_ret(int rets)
766 if (rets)
767 tmp_pop(REG_RET);
768 else
769 i_num(REG_RET, 0);
770 ret[nret++] = o_jmp(0);
773 void o_func_end(void)
775 int i;
776 for (i = 0; i < nret; i++)
777 o_filljmp(ret[i]);
778 i_epilog();
781 long o_mklocal(int size)
783 return sp_push(ALIGN(size, LONGSZ));
786 void o_rmlocal(long addr, int sz)
788 sp = addr - sz;
791 long o_arg2loc(int i)
793 return -(10 + i) << 2;
796 void o_assign(unsigned bt)
798 struct tmp *t1 = TMP(0);
799 struct tmp *t2 = TMP(1);
800 int r1 = reg_fortmp(t1, 0);
801 int r2 = reg_fortmp(t2, 1 << r1);
802 int off = 0;
803 tmp_to(t1, r1);
804 if (t2->bt)
805 tmp_to(t2, r2);
806 if (t2->loc == LOC_LOCAL) {
807 r2 = REG_FP;
808 off = t2->addr + t2->off;
809 } else {
810 tmp_to(t2, r2);
812 tmp_drop(2);
813 i_ldr(0, r1, r2, off, bt);
814 tmp_push(r1);
817 static long cu(int op, long i)
819 switch (op & 0xff) {
820 case O_NEG:
821 return -i;
822 case O_NOT:
823 return ~i;
824 case O_LNOT:
825 return !i;
827 return 0;
830 static int c_uop(int op)
832 struct tmp *t1 = TMP(0);
833 if (!TMP_NUM(t1))
834 return 1;
835 tmp_drop(1);
836 o_num(cu(op, t1->addr));
837 return 0;
840 static long cb(int op, long a, long b)
842 switch (op & 0xff) {
843 case O_ADD:
844 return a + b;
845 case O_SUB:
846 return a - b;
847 case O_AND:
848 return a & b;
849 case O_OR:
850 return a | b;
851 case O_XOR:
852 return a ^ b;
853 case O_MUL:
854 return a * b;
855 case O_DIV:
856 return a / b;
857 case O_MOD:
858 return a % b;
859 case O_SHL:
860 return a << b;
861 case O_SHR:
862 if (op & O_SIGNED)
863 return a >> b;
864 else
865 return (unsigned long) a >> b;
866 case O_LT:
867 return a < b;
868 case O_GT:
869 return a > b;
870 case O_LE:
871 return a <= b;
872 case O_GE:
873 return a >= b;
874 case O_EQ:
875 return a == b;
876 case O_NEQ:
877 return a != b;
879 return 0;
882 static int c_bop(int op)
884 struct tmp *t1 = TMP(0);
885 struct tmp *t2 = TMP(1);
886 int locals = LOCAL_PTR(t1) + LOCAL_PTR(t2);
887 int syms = SYM_PTR(t1) + SYM_PTR(t2);
888 int nums = TMP_NUM(t1) + TMP_NUM(t2);
889 if (syms + locals == 2 || syms + nums + locals != 2)
890 return 1;
891 if (nums == 1)
892 if ((op & 0xff) != O_ADD && ((op & 0xff) != O_SUB || TMP_NUM(t2)))
893 return 1;
894 if (nums == 1) {
895 long o1 = TMP_NUM(t1) ? t1->addr : t1->off;
896 long o2 = TMP_NUM(t2) ? t2->addr : t2->off;
897 long ret = cb(op, o2, o1);
898 if (!TMP_NUM(t1))
899 o_tmpswap();
900 t2->off = ret;
901 tmp_drop(1);
902 } else {
903 long ret = cb(op, t2->addr, t1->addr);
904 tmp_drop(2);
905 o_num(ret);
907 return 0;
910 void o_uop(int op)
912 int r1 = reg_fortmp(TMP(0), 0);
913 if (!c_uop(op))
914 return;
915 tmp_to(TMP(0), r1);
916 switch (op & 0xff) {
917 case O_NEG:
918 i_neg(r1);
919 break;
920 case O_NOT:
921 i_not(r1);
922 break;
923 case O_LNOT:
924 i_lnot(r1);
925 break;
929 static void bin_regs(int *r1, int *r2)
931 struct tmp *t2 = TMP(0);
932 struct tmp *t1 = TMP(1);
933 *r2 = reg_fortmp(t2, 0);
934 tmp_to(t2, *r2);
935 *r1 = reg_fortmp(t1, 1 << *r2);
936 tmp_pop(*r2);
937 tmp_pop(*r1);
940 static int bop_imm(int *r1, long *n, int swap)
942 struct tmp *t1 = TMP(0);
943 struct tmp *t2 = TMP(1);
944 if (!TMP_NUM(t1) && (!swap || !TMP_NUM(t2)))
945 return 1;
946 *n = TMP_NUM(t1) ? t1->addr : t2->addr;
947 if (*n < 0 || add_decimm(add_encimm(*n)) != *n)
948 return 1;
949 if (!TMP_NUM(t1))
950 o_tmpswap();
951 *r1 = reg_fortmp(t2, 0);
952 tmp_drop(1);
953 tmp_pop(*r1);
954 return 0;
957 static void bin_add(int op)
959 /* opcode for O_ADD, O_SUB, O_AND, O_OR, O_XOR */
960 static int rx[] = {I_ADD, I_SUB, I_AND, I_ORR, I_EOR};
961 int r1, r2;
962 long n;
963 if (!bop_imm(&r1, &n, (op & 0xff) != O_SUB)) {
964 i_add_imm(rx[op & 0x0f], r1, r1, n);
965 } else {
966 bin_regs(&r1, &r2);
967 i_add(rx[op & 0x0f], r1, r1, r2);
969 tmp_push(r1);
972 static void bin_shx(int op)
974 int sm = SM_LSL;
975 int r1, r2;
976 long n;
977 if ((op & 0x0f) == 1)
978 sm = op & O_SIGNED ? SM_ASR : SM_LSR;
979 if (!bop_imm(&r1, &n, 0)) {
980 i_shl_imm(sm, r1, r1, n);
981 } else {
982 bin_regs(&r1, &r2);
983 i_shl(sm, r1, r1, r2);
985 tmp_push(r1);
988 static int log2a(unsigned long n)
990 int i = 0;
991 for (i = 0; i < LONGSZ * 8; i++)
992 if (n & (1u << i))
993 break;
994 if (i == LONGSZ * 8 || !(n >> (i + 1)))
995 return i;
996 return -1;
999 /* optimized version of mul/div/mod for powers of two */
1000 static int mul_2(int op)
1002 struct tmp *t1 = TMP(0);
1003 struct tmp *t2 = TMP(1);
1004 long n;
1005 int r2;
1006 int p;
1007 if ((op & 0xff) == O_MUL && t2->loc == LOC_NUM && !t2->bt)
1008 o_tmpswap();
1009 if (t1->loc != LOC_NUM || t1->bt)
1010 return 1;
1011 n = t1->addr;
1012 p = log2a(n);
1013 if (n && p == -1)
1014 return 1;
1015 if ((op & 0xff) == O_MUL) {
1016 tmp_drop(1);
1017 if (n == 1)
1018 return 0;
1019 if (n == 0) {
1020 tmp_drop(1);
1021 o_num(0);
1022 return 0;
1024 r2 = reg_fortmp(t2, 0);
1025 tmp_to(t2, r2);
1026 i_shl_imm(SM_LSL, r2, r2, p);
1027 return 0;
1029 if (op == O_DIV) {
1030 tmp_drop(1);
1031 if (n == 1)
1032 return 0;
1033 r2 = reg_fortmp(t2, 0);
1034 tmp_to(t2, r2);
1035 i_shl_imm(op & O_SIGNED ? SM_ASR : SM_LSR, r2, r2, p);
1036 return 0;
1038 if (op == O_MOD) {
1039 tmp_drop(1);
1040 if (n == 1) {
1041 tmp_drop(1);
1042 o_num(0);
1043 return 0;
1045 r2 = reg_fortmp(t2, 0);
1046 tmp_to(t2, r2);
1047 i_zx(r2, p);
1048 return 0;
1050 return 1;
1053 static void bin_div(int op)
1055 struct tmp *t2 = TMP(0);
1056 struct tmp *t1 = TMP(1);
1057 char *func;
1058 int i;
1059 putdiv = 1;
1060 if ((op & 0xff) == O_DIV)
1061 func = op & O_SIGNED ? "__divdi3" : "__udivdi3";
1062 else
1063 func = op & O_SIGNED ? "__moddi3" : "__umoddi3";
1064 for (i = 0; i < ARRAY_SIZE(argregs); i++)
1065 if (regs[argregs[i]] && regs[argregs[i]] - tmps < ntmp - 2)
1066 tmp_mem(regs[argregs[i]]);
1067 tmp_to(t1, argregs[0]);
1068 tmp_to(t2, argregs[1]);
1069 tmp_drop(2);
1070 i_call(func);
1071 tmp_push(REG_RET);
1074 static void bin_mul(int op)
1076 int r1, r2;
1077 if (!mul_2(op))
1078 return;
1079 if ((op & 0xff) == O_DIV || (op & 0xff) == O_MOD) {
1080 bin_div(op);
1081 } else {
1082 bin_regs(&r1, &r2);
1083 i_mul(r1, r1, r2);
1084 tmp_push(r1);
1088 static void bin_cmp(int op)
1090 /* lt, gt, le, ge, eq, neq */
1091 static int ucond[] = {3, 8, 9, 2, 0, 1};
1092 static int scond[] = {11, 12, 13, 10, 0, 1};
1093 int r1, r2;
1094 long n;
1095 if (!bop_imm(&r1, &n, (op & 0xff) == O_EQ || (op & 0xff) == O_NEQ)) {
1096 i_cmp_imm(I_CMP, r1, n);
1097 } else {
1098 bin_regs(&r1, &r2);
1099 i_cmp(I_CMP, r1, r2);
1101 i_set(op & O_SIGNED ? scond[op & 0x0f] : ucond[op & 0x0f], r1);
1102 tmp_push(r1);
1105 void o_bop(int op)
1107 if (!c_bop(op))
1108 return;
1109 if ((op & 0xf0) == 0x00)
1110 bin_add(op);
1111 if ((op & 0xf0) == 0x10)
1112 bin_shx(op);
1113 if ((op & 0xf0) == 0x20)
1114 bin_mul(op);
1115 if ((op & 0xf0) == 0x30)
1116 bin_cmp(op);
1119 static void load_regs2(int *r0, int *r1, int *r2)
1121 struct tmp *t0 = TMP(0);
1122 struct tmp *t1 = TMP(1);
1123 struct tmp *t2 = TMP(2);
1124 *r0 = reg_fortmp(t0, 0);
1125 *r1 = reg_fortmp(t1, 1 << *r0);
1126 *r2 = reg_fortmp(t2, (1 << *r0) | (1 << *r1));
1127 tmp_to(t0, *r0);
1128 tmp_to(t1, *r1);
1129 tmp_to(t2, *r2);
1132 void o_memcpy(void)
1134 int rd, rs, rn;
1135 load_regs2(&rn, &rs, &rd);
1136 i_memcpy(rd, rs, rn);
1137 tmp_drop(2);
1140 void o_memset(void)
1142 int rd, rs, rn;
1143 load_regs2(&rn, &rs, &rd);
1144 i_memset(rd, rs, rn);
1145 tmp_drop(2);
1148 long o_mklabel(void)
1150 return cslen;
1153 static long jxz(long addr, int z)
1155 int r = reg_fortmp(TMP(0), 0);
1156 tmp_pop(r);
1157 i_b_if(addr, r, z);
1158 return cslen - 4;
1161 long o_jz(long addr)
1163 return jxz(addr, 1);
1166 long o_jnz(long addr)
1168 return jxz(addr, 0);
1171 long o_jmp(long addr)
1173 i_b(addr);
1174 return cslen - 4;
1177 void o_filljmp2(long addr, long jmpdst)
1179 i_b_fill((void *) cs + addr, jmpdst - addr);
1182 void o_filljmp(long addr)
1184 o_filljmp2(addr, cslen);
1187 void o_call(int argc, int rets)
1189 struct tmp *t;
1190 int i;
1191 int aregs = MIN(ARRAY_SIZE(argregs), argc);
1192 for (i = 0; i < ARRAY_SIZE(argregs); i++)
1193 if (regs[argregs[i]] && regs[argregs[i]] - tmps < ntmp - argc)
1194 tmp_mem(regs[argregs[i]]);
1195 if (argc > aregs) {
1196 sp_push(LONGSZ * (argc - aregs));
1197 for (i = argc - 1; i >= aregs; --i) {
1198 int reg = reg_fortmp(TMP(0), 0);
1199 tmp_pop(reg);
1200 i_ldr(0, reg, REG_SP, (i - aregs) * LONGSZ, LONGSZ);
1203 for (i = aregs - 1; i >= 0; --i)
1204 tmp_to(TMP(aregs - i - 1), argregs[i]);
1205 tmp_drop(aregs);
1206 t = TMP(0);
1207 if (t->loc == LOC_SYM && !t->bt) {
1208 i_call(t->sym);
1209 tmp_drop(1);
1210 } else {
1211 int reg = t->loc == LOC_REG ? t->addr : REG_TMP;
1212 tmp_pop(reg);
1213 i_call_reg(reg);
1215 if (rets)
1216 tmp_push(REG_RET);
1219 void o_nogen(void)
1221 nogen++;
1224 void o_dogen(void)
1226 nogen--;
1229 void o_mkbss(char *name, int size, int global)
1231 out_sym(name, OUT_BSS | (global ? OUT_GLOB : 0), bsslen, size);
1232 bsslen += ALIGN(size, LONGSZ);
1235 #define MAXDATS (1 << 10)
1236 static char dat_names[MAXDATS][NAMELEN];
1237 static int dat_offs[MAXDATS];
1238 static int ndats;
1240 void err(char *msg);
1241 void *o_mkdat(char *name, int size, int global)
1243 void *addr = ds + dslen;
1244 int idx = ndats++;
1245 if (idx >= MAXDATS)
1246 err("nomem: MAXDATS reached!\n");
1247 strcpy(dat_names[idx], name);
1248 dat_offs[idx] = dslen;
1249 out_sym(name, OUT_DS | (global ? OUT_GLOB : 0), dslen, size);
1250 dslen += ALIGN(size, LONGSZ);
1251 return addr;
1254 static int dat_off(char *name)
1256 int i;
1257 for (i = 0; i < ndats; i++)
1258 if (!strcmp(name, dat_names[i]))
1259 return dat_offs[i];
1260 return 0;
1263 void o_datset(char *name, int off, unsigned bt)
1265 struct tmp *t = TMP(0);
1266 int sym_off = dat_off(name) + off;
1267 if (t->loc == LOC_NUM && !t->bt) {
1268 num_cast(t, bt);
1269 memcpy(ds + sym_off, &t->addr, BT_SZ(bt));
1271 if (t->loc == LOC_SYM && !t->bt) {
1272 out_rel(t->sym, OUT_DS, sym_off);
1273 memcpy(ds + sym_off, &t->off, BT_SZ(bt));
1275 tmp_drop(1);
1278 /* compiled division functions; div.s contains the source */
1279 static int udivdi3[] = {
1280 0xe3a02000, 0xe3a03000, 0xe1110001, 0x0a00000a,
1281 0xe1b0c211, 0xe2822001, 0x5afffffc, 0xe3a0c001,
1282 0xe2522001, 0x4a000004, 0xe1500211, 0x3afffffb,
1283 0xe0400211, 0xe083321c, 0xeafffff8, 0xe1a01000,
1284 0xe1a00003, 0xe1a0f00e,
1286 static int umoddi3[] = {
1287 0xe92d4000, 0xebffffeb, 0xe1a00001, 0xe8bd8000,
1289 static int divdi3[] = {
1290 0xe92d4030, 0xe1a04000, 0xe1a05001, 0xe1100000,
1291 0x42600000, 0xe1110001, 0x42611000, 0xebffffe1,
1292 0xe1340005, 0x42600000, 0xe1140004, 0x42611000,
1293 0xe8bd8030,
1295 static int moddi3[] = {
1296 0xe92d4000, 0xebfffff0, 0xe1a00001, 0xe8bd8000,
1299 void o_write(int fd)
1301 if (putdiv) {
1302 out_sym("__udivdi3", OUT_CS, cslen, 0);
1303 os(udivdi3, sizeof(udivdi3));
1304 out_sym("__umoddi3", OUT_CS, cslen, 0);
1305 os(umoddi3, sizeof(umoddi3));
1306 out_sym("__divdi3", OUT_CS, cslen, 0);
1307 os(divdi3, sizeof(divdi3));
1308 out_sym("__moddi3", OUT_CS, cslen, 0);
1309 os(moddi3, sizeof(moddi3));
1311 out_write(fd, cs, cslen, ds, dslen);