gen: call tmp_to() in for loading rhs in o_assing()
[neatcc/cc.git] / gen.c
blob5425a740565d2b792a9c784daed9e6ba21d5e639
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 #define TMP_REG(t) ((t)->loc == LOC_REG ? (t)->addr : reg_get(~0))
28 #define TMP_REG2(t, r) ((t)->loc == LOC_REG && (t)->addr != (r) ? \
29 (t)->addr : reg_get(~(1 << (r))))
30 #define TMP_REG3(t, r1, r2) ((t)->loc == LOC_REG && (t)->addr != (r1) && (t)->addr != (r2) ? \
31 (t)->addr : reg_get(~((1 << (r1)) | (1 << (r2)))))
33 static char cs[SECSIZE]; /* code segment */
34 static int cslen;
35 static char ds[SECSIZE]; /* data segment */
36 static int dslen;
37 static long bsslen; /* bss segment size */
39 static int nogen;
40 static long sp;
41 static long func_beg;
42 static long maxsp;
44 #define TMP(i) (((i) < ntmp) ? &tmps[ntmp - 1 - (i)] : NULL)
46 static struct tmp {
47 long addr;
48 char sym[NAMELEN];
49 long off; /* offset from a symbol or a local */
50 unsigned loc; /* variable location */
51 unsigned bt; /* type of address; zero when not a pointer */
52 } tmps[MAXTMP];
53 static int ntmp;
55 static int tmpsp;
57 static struct tmp *regs[NREGS];
58 static int tmpregs[] = {4, 5, 6, 7, 8, 9, 0, 1, 2, 3};
59 static int argregs[] = {0, 1, 2, 3};
61 #define I_AND 0x00
62 #define I_EOR 0x01
63 #define I_SUB 0x02
64 #define I_RSB 0x03
65 #define I_ADD 0x04
66 #define I_TST 0x08
67 #define I_CMP 0x0a
68 #define I_ORR 0x0c
69 #define I_MOV 0x0d
70 #define I_MVN 0x0f
72 #define MAXRET (1 << 8)
74 static long ret[MAXRET];
75 static int nret;
77 /* output div/mod functions */
78 static int putdiv = 0;
80 /* for optimizing cmp + bcc */
81 static long last_cmp = -1;
82 static long last_set = -1;
83 static long last_cond;
85 static void os(void *s, int n)
87 memcpy(cs + cslen, s, n);
88 cslen += n;
91 static void oi(long n)
93 if (nogen)
94 return;
95 *(int *) (cs + cslen) = n;
96 cslen += 4;
99 #define NDATS 1024
101 /* data pool */
102 static long num_offs[NDATS]; /* data immediate value */
103 static char num_names[NDATS][NAMELEN]; /* relocation data symbol name */
104 static int ndats;
106 static int pool_num(long num)
108 int idx = ndats++;
109 num_offs[idx] = num;
110 num_names[idx][0] = '\0';
111 return idx << 2;
114 static int pool_reloc(char *name, long off)
116 int idx = ndats++;
117 num_offs[idx] = off;
118 strcpy(num_names[idx], name);
119 return idx << 2;
122 static void pool_write(void)
124 int i;
125 for (i = 0; i < ndats; i++) {
126 if (num_names[i])
127 out_rel(num_names[i], OUT_CS, cslen);
128 oi(num_offs[i]);
133 * data processing:
134 * +---------------------------------------+
135 * |COND|00|I| op |S| Rn | Rd | operand2 |
136 * +---------------------------------------+
138 * S: set condition code
139 * Rn: first operand
140 * Rd: destination operand
142 * I=0 operand2=| shift | Rm |
143 * I=1 operand2=|rota| imm |
145 #define ADD(op, rd, rn, s, i, cond) \
146 (((cond) << 28) | ((i) << 25) | ((s) << 20) | \
147 ((op) << 21) | ((rn) << 16) | ((rd) << 12))
149 static void i_add(int op, int rd, int rn, int rm)
151 oi(ADD(op, rd, rn, 0, 0, 14) | rm);
154 static int add_encimm(unsigned n)
156 int i = 0;
157 while (i < 12 && (n >> ((4 + i) << 1)))
158 i++;
159 return (n >> (i << 1)) | (((16 - i) & 0x0f) << 8);
162 static int add_decimm(unsigned n)
164 int rot = (16 - ((n >> 8) & 0x0f)) & 0x0f;
165 return (n & 0xff) << (rot << 1);
168 static int add_rndimm(unsigned n)
170 int rot = (n >> 8) & 0x0f;
171 int num = n & 0xff;
172 if (rot == 0)
173 return n;
174 if (num == 0xff) {
175 num = 0;
176 rot = (rot + 12) & 0x0f;
178 return ((num + 1) & 0xff) | (rot << 8);
181 static void i_ldr(int l, int rd, int rn, int off, int bt);
183 static void i_num(int rd, long n)
185 int neg = n < 0;
186 int p = neg ? -n - 1 : n;
187 int enc = add_encimm(p);
188 if (p == add_decimm(enc)) {
189 oi(ADD(neg ? I_MVN : I_MOV, rd, 0, 0, 1, 14) | enc);
190 } else {
191 if (!nogen) {
192 int off = pool_num(n);
193 i_ldr(1, rd, REG_DP, off, LONGSZ);
198 static void i_add_imm(int op, int rd, int rn, int n)
200 int neg = n < 0;
201 int imm = add_encimm(neg ? -n : n);
202 if (imm == add_decimm(neg ? -n : n)) {
203 oi(ADD(neg ? I_SUB : I_ADD, rd, rn, 0, 1, 14) | imm);
204 } else {
205 i_num(rd, n);
206 i_add(I_ADD, rd, rd, rn);
211 * multiply
212 * +----------------------------------------+
213 * |COND|000000|A|S| Rd | Rn | Rs |1001| Rm |
214 * +----------------------------------------+
216 * Rd: destination
217 * A: accumulate
218 * C: set condition codes
220 * I=0 operand2=| shift | Rm |
221 * I=1 operand2=|rota| imm |
223 #define MUL(rd, rn, rs) \
224 ((14 << 28) | ((rd) << 16) | ((0) << 12) | ((rn) << 8) | ((9) << 4) | (rm))
226 static void i_mul(int rd, int rn, int rm)
228 oi(MUL(rd, rn, rm));
231 static void i_cmp(int op, int rn, int rm)
233 oi(ADD(op, 0, rn, 1, 0, 14) | rm);
234 last_cmp = cslen;
237 static void i_set(int cond, int rd)
239 last_set = cslen;
240 last_cond = cond;
241 oi(ADD(I_MOV, rd, 0, 0, 1, 14));
242 oi(ADD(I_MOV, rd, 0, 0, 1, cond) | 1);
245 #define SM_LSL 0
246 #define SM_LSR 1
247 #define SM_ASR 2
249 static void i_shl(int sm, int rd, int rm, int rs)
251 oi(ADD(I_MOV, rd, 0, 0, 0, 14) | (rs << 8) | (sm << 5) | (1 << 4) | rm);
254 static void i_shl_imm(int sm, int rd, int n)
256 oi(ADD(I_MOV, rd, 0, 0, 0, 14) | (n << 7) | (sm << 5) | rd);
259 static void i_mov(int op, int rd, int rn)
261 oi(ADD(op, rd, 0, 0, 0, 14) | rn);
265 * single data transfer:
266 * +------------------------------------------+
267 * |COND|01|I|P|U|B|W|L| Rn | Rd | offset |
268 * +------------------------------------------+
270 * I: immediate/offset
271 * P: post/pre indexing
272 * U: down/up
273 * B: byte/word
274 * W: writeback
275 * L: store/load
276 * Rn: base register
277 * Rd: source/destination register
279 * I=0 offset=| immediate |
280 * I=1 offset=| shift | Rm |
282 * halfword and signed data transfer
283 * +----------------------------------------------+
284 * |COND|000|P|U|0|W|L| Rn | Rd |0000|1|S|H|1| Rm |
285 * +----------------------------------------------+
287 * +----------------------------------------------+
288 * |COND|000|P|U|1|W|L| Rn | Rd |off1|1|S|H|1|off2|
289 * +----------------------------------------------+
291 * S: singed
292 * H: halfword
294 #define LDR(l, rd, rn, b, u, p, w) \
295 ((14 << 28) | (1 << 26) | ((p) << 24) | ((b) << 22) | ((u) << 23) | \
296 ((w) << 21) | ((l) << 20) | ((rn) << 16) | ((rd) << 12))
297 #define LDRH(l, rd, rn, s, h, u, i) \
298 ((14 << 28) | (1 << 24) | ((u) << 23) | ((i) << 22) | ((l) << 20) | \
299 ((rn) << 16) | ((rd) << 12) | ((s) << 6) | ((h) << 5) | (9 << 4))
301 static void i_ldr(int l, int rd, int rn, int off, int bt)
303 int b = BT_SZ(bt) == 1;
304 int h = BT_SZ(bt) == 2;
305 int s = l && (bt & BT_SIGNED);
306 int half = h || (b && s);
307 int maximm = half ? 0x100 : 0x1000;
308 int neg = off < 0;
309 if (neg)
310 off = -off;
311 while (off >= maximm) {
312 int imm = add_encimm(off);
313 oi(ADD(neg ? I_SUB : I_ADD, rd, rn, 0, 1, 14) | imm);
314 rn = rd;
315 off -= add_decimm(imm);
317 if (!half)
318 oi(LDR(l, rd, rn, b, !neg, 1, 0) | off);
319 else
320 oi(LDRH(l, rd, rn, s, h, !neg, 1) |
321 ((off & 0xf0) << 4) | (off & 0x0f));
324 static void i_sym(int rd, char *sym, int off)
326 if (!nogen) {
327 int doff = pool_reloc(sym, off);
328 i_ldr(1, rd, REG_DP, doff, LONGSZ);
332 static void i_neg(int rd)
334 oi(ADD(I_RSB, rd, rd, 0, 1, 14));
337 static void i_not(int rd)
339 oi(ADD(I_MVN, rd, 0, rd, 1, 14));
342 static void i_lnot(int rd)
344 i_cmp(I_TST, rd, rd);
345 oi(ADD(I_MOV, rd, 0, 0, 1, 14));
346 oi(ADD(I_MOV, rd, 0, 0, 1, 0) | 1);
349 /* rd = rd & ((1 << bits) - 1) */
350 static void i_zx(int rd, int bits)
352 if (bits <= 8) {
353 oi(ADD(I_AND, rd, rd, 0, 1, 14) | add_encimm((1 << bits) - 1));
354 } else {
355 i_shl_imm(SM_LSL, rd, 32 - bits);
356 i_shl_imm(SM_LSR, rd, 32 - bits);
360 static void i_sx(int rd, int bits)
362 i_shl_imm(SM_LSL, rd, 32 - bits);
363 i_shl_imm(SM_ASR, rd, 32 - bits);
367 * branch:
368 * +-----------------------------------+
369 * |COND|101|L| offset |
370 * +-----------------------------------+
372 * L: link
374 #define BL(cond, l, o) (((cond) << 28) | (5 << 25) | ((l) << 24) | \
375 ((((o) - 8) >> 2) & 0x00ffffff))
377 static void i_b(long addr)
379 oi(BL(14, 0, addr - cslen));
382 static void i_b_if(long addr, int rn, int z)
384 static int nots[] = {1, 0, 3, 2, -1, -1, -1, -1, 9, 8, 11, 10, 13, 12, -1};
385 if (last_cmp + 8 != cslen || last_set != last_cmp) {
386 i_cmp(I_TST, rn, rn);
387 oi(BL(z ? 0 : 1, 0, addr - cslen));
388 return;
390 cslen = last_cmp;
391 oi(BL(z ? nots[last_cond] : last_cond, 0, addr - cslen));
394 static void i_b_fill(long *dst, int diff)
396 *dst = (*dst & 0xff000000) | (((diff - 8) >> 2) & 0x00ffffff);
399 static void i_memcpy(int rd, int rs, int rn)
401 oi(ADD(I_SUB, rn, rn, 1, 1, 14) | 1);
402 oi(BL(4, 0, 16));
403 oi(LDR(1, REG_TMP, rs, 1, 1, 0, 0) | 1);
404 oi(LDR(0, REG_TMP, rd, 1, 1, 0, 0) | 1);
405 oi(BL(14, 0, -16));
408 static void i_memset(int rd, int rs, int rn)
410 oi(ADD(I_SUB, rn, rn, 1, 1, 14) | 1);
411 oi(BL(4, 0, 12));
412 oi(LDR(0, rs, rd, 1, 1, 0, 0) | 1);
413 oi(BL(14, 0, -12));
416 static void i_call_reg(int rd)
418 i_mov(I_MOV, REG_LR, REG_PC);
419 i_mov(I_MOV, REG_PC, rd);
422 static void i_call(char *sym)
424 if (!nogen)
425 out_rel(sym, OUT_CS | OUT_REL24, cslen);
426 oi(BL(14, 1, 0));
429 static void i_prolog(void)
431 func_beg = cslen;
432 ndats = 0;
433 oi(0xe1a0c00d); /* mov r12, sp */
434 oi(0xe92d000f); /* stmfd sp!, {r0-r3} */
435 oi(0xe92d5ff0); /* stmfd sp!, {r0-r11, r12, lr} */
436 oi(0xe1a0b00d); /* mov fp, sp */
437 oi(0xe24dd000); /* sub sp, sp, xx */
438 oi(0xe28fa000); /* add dp, pc, xx */
441 static void i_epilog(void)
443 int dpoff;
444 oi(0xe89baff0); /* ldmfd fp, {r4-r11, sp, pc} */
445 dpoff = add_decimm(add_rndimm(add_encimm(cslen - func_beg - 28)));
446 cslen = func_beg + dpoff + 28;
447 maxsp = ALIGN(maxsp, 8);
448 maxsp = add_decimm(add_rndimm(add_encimm(maxsp)));
449 /* fill stack sub: sp = sp - xx */
450 *(long *) (cs + func_beg + 16) |= add_encimm(maxsp);
451 /* fill data ptr addition: dp = pc + xx */
452 *(long *) (cs + func_beg + 20) |= add_encimm(dpoff);
453 pool_write();
456 static long sp_push(int size)
458 sp += size;
459 if (sp > maxsp)
460 maxsp = sp;
461 return sp;
464 static void tmp_mem(struct tmp *tmp)
466 int src = tmp->addr;
467 if (!(tmp->loc == LOC_REG))
468 return;
469 if (tmpsp == -1)
470 tmpsp = sp;
471 tmp->addr = -sp_push(LONGSZ);
472 i_ldr(0, src, REG_FP, tmp->addr, LONGSZ);
473 regs[src] = NULL;
474 tmp->loc = LOC_MEM;
477 static void num_cast(struct tmp *t, unsigned bt)
479 if (!(bt & BT_SIGNED) && BT_SZ(bt) != LONGSZ)
480 t->addr &= ((1l << (long) (BT_SZ(bt) * 8)) - 1);
481 if (bt & BT_SIGNED && BT_SZ(bt) != LONGSZ &&
482 t->addr > (1l << (BT_SZ(bt) * 8 - 1)))
483 t->addr = -((1l << (BT_SZ(bt) * 8)) - t->addr);
486 static void tmp_reg(struct tmp *tmp, int dst, int deref)
488 int bt = tmp->bt;
489 if (!tmp->bt)
490 deref = 0;
491 if (deref)
492 tmp->bt = 0;
493 if (tmp->loc == LOC_NUM) {
494 i_num(dst, tmp->addr);
495 tmp->addr = dst;
496 regs[dst] = tmp;
497 tmp->loc = LOC_REG;
499 if (tmp->loc == LOC_SYM) {
500 i_sym(dst, tmp->sym, tmp->off);
501 tmp->addr = dst;
502 regs[dst] = tmp;
503 tmp->loc = LOC_REG;
505 if (tmp->loc == LOC_REG) {
506 if (deref)
507 i_ldr(1, dst, tmp->addr, 0, bt);
508 else if (dst != tmp->addr)
509 i_mov(I_MOV, dst, tmp->addr);
510 regs[tmp->addr] = NULL;
512 if (tmp->loc == LOC_LOCAL) {
513 if (deref)
514 i_ldr(1, dst, REG_FP, tmp->addr + tmp->off, bt);
515 else
516 i_add_imm(I_ADD, dst, REG_FP, tmp->addr + tmp->off);
518 if (tmp->loc == LOC_MEM) {
519 i_ldr(1, dst, REG_FP, tmp->addr, LONGSZ);
520 if (deref)
521 i_ldr(1, dst, dst, 0, bt);
523 tmp->addr = dst;
524 regs[dst] = tmp;
525 tmp->loc = LOC_REG;
528 static void reg_free(int reg)
530 int i;
531 if (!regs[reg])
532 return;
533 for (i = 0; i < ARRAY_SIZE(tmpregs); i++)
534 if (!regs[tmpregs[i]]) {
535 tmp_reg(regs[reg], tmpregs[i], 0);
536 return;
538 tmp_mem(regs[reg]);
541 static void reg_for(int reg, struct tmp *t)
543 if (regs[reg] && regs[reg] != t)
544 reg_free(reg);
547 static void tmp_mv(struct tmp *t, int reg)
549 reg_for(reg, t);
550 tmp_reg(t, reg, 0);
553 static void tmp_to(struct tmp *t, int reg)
555 reg_for(reg, t);
556 tmp_reg(t, reg, 1);
559 static void tmp_drop(int n)
561 int i;
562 for (i = ntmp - n; i < ntmp; i++)
563 if (tmps[i].loc == LOC_REG)
564 regs[tmps[i].addr] = NULL;
565 ntmp -= n;
568 static void tmp_pop(int reg)
570 struct tmp *t = TMP(0);
571 tmp_to(t, reg);
572 tmp_drop(1);
575 static struct tmp *tmp_new(void)
577 return &tmps[ntmp++];
580 static void tmp_push(int reg)
582 struct tmp *t = tmp_new();
583 t->addr = reg;
584 t->bt = 0;
585 t->loc = LOC_REG;
586 regs[reg] = t;
589 void o_local(long addr)
591 struct tmp *t = tmp_new();
592 t->addr = -addr;
593 t->loc = LOC_LOCAL;
594 t->bt = 0;
595 t->off = 0;
598 void o_num(long num)
600 struct tmp *t = tmp_new();
601 t->addr = num;
602 t->bt = 0;
603 t->loc = LOC_NUM;
606 void o_sym(char *name)
608 struct tmp *t = tmp_new();
609 strcpy(t->sym, name);
610 t->loc = LOC_SYM;
611 t->bt = 0;
612 t->off = 0;
615 void o_tmpdrop(int n)
617 if (n == -1 || n > ntmp)
618 n = ntmp;
619 tmp_drop(n);
620 if (!ntmp) {
621 if (tmpsp != -1)
622 sp = tmpsp;
623 tmpsp = -1;
627 #define FORK_REG 0x00
629 /* make sure tmps remain intact after a conditional expression */
630 void o_fork(void)
632 int i;
633 for (i = 0; i < ntmp - 1; i++)
634 tmp_mem(&tmps[i]);
637 void o_forkpush(void)
639 tmp_pop(FORK_REG);
642 void o_forkjoin(void)
644 tmp_push(FORK_REG);
647 void o_tmpswap(void)
649 struct tmp *t1 = TMP(0);
650 struct tmp *t2 = TMP(1);
651 struct tmp t;
652 memcpy(&t, t1, sizeof(t));
653 memcpy(t1, t2, sizeof(t));
654 memcpy(t2, &t, sizeof(t));
655 if (t1->loc == LOC_REG)
656 regs[t1->addr] = t1;
657 if (t2->loc == LOC_REG)
658 regs[t2->addr] = t2;
661 static int reg_get(int mask)
663 int i;
664 for (i = 0; i < ARRAY_SIZE(tmpregs); i++)
665 if ((1 << tmpregs[i]) & mask && !regs[tmpregs[i]])
666 return tmpregs[i];
667 for (i = 0; i < ARRAY_SIZE(tmpregs); i++)
668 if ((1 << tmpregs[i]) & mask) {
669 reg_free(tmpregs[i]);
670 return tmpregs[i];
672 return 0;
675 static void tmp_copy(struct tmp *t1)
677 struct tmp *t2 = tmp_new();
678 memcpy(t2, t1, sizeof(*t1));
679 if (!(t1->loc & (LOC_REG | LOC_MEM)))
680 return;
681 if (t1->loc == LOC_MEM) {
682 tmp_mv(t2, reg_get(~0));
683 } else if (t1->loc == LOC_REG) {
684 t2->addr = TMP_REG2(t2, t1->addr);
685 i_mov(I_MOV, t2->addr, t1->addr);
686 regs[t2->addr] = t2;
690 void o_tmpcopy(void)
692 tmp_copy(TMP(0));
695 void o_cast(unsigned bt)
697 struct tmp *t = TMP(0);
698 if (t->bt) {
699 t->bt = bt;
700 return;
702 if (t->loc == LOC_NUM) {
703 num_cast(t, bt);
704 return;
706 if (BT_SZ(bt) != LONGSZ) {
707 int reg = TMP_REG(t);
708 tmp_to(t, reg);
709 if (bt & BT_SIGNED)
710 i_sx(reg, BT_SZ(bt) * 8);
711 else
712 i_zx(reg, BT_SZ(bt) * 8);
716 void o_func_beg(char *name, int global)
718 out_sym(name, (global ? OUT_GLOB : 0) | OUT_CS, cslen, 0);
719 i_prolog();
720 sp = 0;
721 maxsp = sp;
722 ntmp = 0;
723 tmpsp = -1;
724 nret = 0;
725 memset(regs, 0, sizeof(regs));
728 void o_deref(unsigned bt)
730 struct tmp *t = TMP(0);
731 if (t->bt)
732 tmp_to(t, TMP_REG(t));
733 t->bt = bt;
736 void o_load(void)
738 struct tmp *t = TMP(0);
739 tmp_to(t, TMP_REG(t));
742 #define TMP_NUM(t) ((t)->loc == LOC_NUM && !(t)->bt)
743 #define LOCAL_PTR(t) ((t)->loc == LOC_LOCAL && !(t)->bt)
744 #define SYM_PTR(t) ((t)->loc == LOC_SYM && !(t)->bt)
746 int o_popnum(long *c)
748 struct tmp *t = TMP(0);
749 if (!TMP_NUM(t))
750 return 1;
751 *c = t->addr;
752 tmp_drop(1);
753 return 0;
756 void o_ret(int rets)
758 if (rets)
759 tmp_pop(REG_RET);
760 else
761 i_num(REG_RET, 0);
762 ret[nret++] = o_jmp(0);
765 void o_func_end(void)
767 int i;
768 for (i = 0; i < nret; i++)
769 o_filljmp(ret[i]);
770 i_epilog();
773 long o_mklocal(int size)
775 return sp_push(ALIGN(size, LONGSZ));
778 void o_rmlocal(long addr, int sz)
780 sp = addr - sz;
783 long o_arg(int i)
785 return -(10 + i) << 2;
788 void o_assign(unsigned bt)
790 struct tmp *t1 = TMP(0);
791 struct tmp *t2 = TMP(1);
792 int r1 = TMP_REG(t1);
793 int r2 = TMP_REG2(t2, r1);
794 int off = 0;
795 tmp_to(t1, r1);
796 if (t2->bt)
797 tmp_to(t2, r2);
798 if (t2->loc == LOC_LOCAL) {
799 r2 = REG_FP;
800 off = t2->addr + t2->off;
801 } else {
802 tmp_to(t2, r2);
804 tmp_drop(2);
805 i_ldr(0, r1, r2, off, bt);
806 tmp_push(r1);
809 static long cu(int op, long i)
811 switch (op & 0xff) {
812 case O_NEG:
813 return -i;
814 case O_NOT:
815 return ~i;
816 case O_LNOT:
817 return !i;
819 return 0;
822 static int c_uop(int op)
824 struct tmp *t1 = TMP(0);
825 if (!TMP_NUM(t1))
826 return 1;
827 tmp_drop(1);
828 o_num(cu(op, t1->addr));
829 return 0;
832 static long cb(int op, long a, long b)
834 switch (op & 0xff) {
835 case O_ADD:
836 return a + b;
837 case O_SUB:
838 return a - b;
839 case O_AND:
840 return a & b;
841 case O_OR:
842 return a | b;
843 case O_XOR:
844 return a ^ b;
845 case O_MUL:
846 return a * b;
847 case O_DIV:
848 return a / b;
849 case O_MOD:
850 return a % b;
851 case O_SHL:
852 return a << b;
853 case O_SHR:
854 if (op & O_SIGNED)
855 return a >> b;
856 else
857 return (unsigned long) a >> b;
858 case O_LT:
859 return a < b;
860 case O_GT:
861 return a > b;
862 case O_LE:
863 return a <= b;
864 case O_GE:
865 return a >= b;
866 case O_EQ:
867 return a == b;
868 case O_NEQ:
869 return a != b;
871 return 0;
874 static int c_bop(int op)
876 struct tmp *t1 = TMP(0);
877 struct tmp *t2 = TMP(1);
878 int locals = LOCAL_PTR(t1) + LOCAL_PTR(t2);
879 int syms = SYM_PTR(t1) + SYM_PTR(t2);
880 int nums = TMP_NUM(t1) + TMP_NUM(t2);
881 if (syms + locals == 2 || syms + nums + locals != 2)
882 return 1;
883 if (nums == 1)
884 if ((op & 0xff) != O_ADD && ((op & 0xff) != O_SUB || TMP_NUM(t2)))
885 return 1;
886 if (nums == 1) {
887 long o1 = TMP_NUM(t1) ? t1->addr : t1->off;
888 long o2 = TMP_NUM(t2) ? t2->addr : t2->off;
889 long ret = cb(op, o2, o1);
890 if (!TMP_NUM(t1))
891 o_tmpswap();
892 t2->off = ret;
893 tmp_drop(1);
894 } else {
895 long ret = cb(op, t2->addr, t1->addr);
896 tmp_drop(2);
897 o_num(ret);
899 return 0;
902 void o_uop(int op)
904 int r1 = TMP_REG(TMP(0));
905 if (!c_uop(op))
906 return;
907 tmp_to(TMP(0), r1);
908 switch (op & 0xff) {
909 case O_NEG:
910 i_neg(r1);
911 break;
912 case O_NOT:
913 i_not(r1);
914 break;
915 case O_LNOT:
916 i_lnot(r1);
917 break;
921 static void bin_regs(int *r1, int *r2)
923 struct tmp *t2 = TMP(0);
924 struct tmp *t1 = TMP(1);
925 *r2 = TMP_REG(t2);
926 tmp_to(t2, *r2);
927 *r1 = TMP_REG2(t1, *r2);
928 tmp_pop(*r2);
929 tmp_pop(*r1);
932 static void bin_add(int op)
934 /* opcode for O_ADD, O_SUB, O_AND, O_OR, O_XOR */
935 static int rx[] = {I_ADD, I_SUB, I_AND, I_ORR, I_EOR};
936 int r1, r2;
937 bin_regs(&r1, &r2);
938 i_add(rx[op & 0x0f], r1, r1, r2);
939 tmp_push(r1);
942 static void bin_shx(int op)
944 int sm = SM_LSL;
945 int r1, r2;
946 bin_regs(&r1, &r2);
947 if ((op & 0x0f) == 1)
948 sm = op & O_SIGNED ? SM_ASR : SM_LSR;
949 i_shl(sm, r1, r1, r2);
950 tmp_push(r1);
953 static int log2a(unsigned long n)
955 int i = 0;
956 for (i = 0; i < LONGSZ * 8; i++)
957 if (n & (1u << i))
958 break;
959 if (i == LONGSZ * 8 || !(n >> (i + 1)))
960 return i;
961 return -1;
964 /* optimized version of mul/div/mod for powers of two */
965 static int mul_2(int op)
967 struct tmp *t1 = TMP(0);
968 struct tmp *t2 = TMP(1);
969 long n;
970 int r2;
971 int p;
972 if ((op & 0xff) == O_MUL && t2->loc == LOC_NUM && !t2->bt)
973 o_tmpswap();
974 if (t1->loc != LOC_NUM || t1->bt)
975 return 1;
976 n = t1->addr;
977 p = log2a(n);
978 if (n && p == -1)
979 return 1;
980 if ((op & 0xff) == O_MUL) {
981 tmp_drop(1);
982 if (n == 1)
983 return 0;
984 if (n == 0) {
985 tmp_drop(1);
986 o_num(0);
987 return 0;
989 r2 = TMP_REG(t2);
990 tmp_to(t2, r2);
991 i_shl_imm(SM_LSL, r2, p);
992 return 0;
994 if (op == O_DIV) {
995 tmp_drop(1);
996 if (n == 1)
997 return 0;
998 r2 = TMP_REG(t2);
999 tmp_to(t2, r2);
1000 i_shl_imm(op & O_SIGNED ? SM_ASR : SM_LSR, r2, p);
1001 return 0;
1003 if (op == O_MOD) {
1004 tmp_drop(1);
1005 if (n == 1) {
1006 tmp_drop(1);
1007 o_num(0);
1008 return 0;
1010 r2 = TMP_REG(t2);
1011 tmp_to(t2, r2);
1012 i_zx(r2, p);
1013 return 0;
1015 return 1;
1018 static void bin_div(int op)
1020 struct tmp *t2 = TMP(0);
1021 struct tmp *t1 = TMP(1);
1022 char *func;
1023 int i;
1024 putdiv = 1;
1025 if ((op & 0xff) == O_DIV)
1026 func = op & O_SIGNED ? "__divdi3" : "__udivdi3";
1027 else
1028 func = op & O_SIGNED ? "__moddi3" : "__umoddi3";
1029 for (i = 0; i < ARRAY_SIZE(argregs); i++)
1030 if (regs[argregs[i]] && regs[argregs[i]] - tmps < ntmp - 2)
1031 tmp_mem(regs[argregs[i]]);
1032 tmp_to(t1, argregs[0]);
1033 tmp_to(t2, argregs[1]);
1034 tmp_drop(2);
1035 i_call(func);
1036 tmp_push(REG_RET);
1039 static void bin_mul(int op)
1041 int r1, r2;
1042 if (!mul_2(op))
1043 return;
1044 if ((op & 0xff) == O_DIV || (op & 0xff) == O_MOD) {
1045 bin_div(op);
1046 } else {
1047 bin_regs(&r1, &r2);
1048 i_mul(r1, r1, r2);
1049 tmp_push(r1);
1053 static void bin_cmp(int op)
1055 /* lt, gt, le, ge, eq, neq */
1056 static int ucond[] = {3, 8, 9, 2, 0, 1};
1057 static int scond[] = {11, 12, 13, 10, 0, 1};
1058 int r1, r2;
1059 bin_regs(&r1, &r2);
1060 i_cmp(I_CMP, r1, r2);
1061 i_set(op & O_SIGNED ? scond[op & 0x0f] : ucond[op & 0x0f], r1);
1062 tmp_push(r1);
1065 void o_bop(int op)
1067 if (!c_bop(op))
1068 return;
1069 if ((op & 0xf0) == 0x00)
1070 bin_add(op);
1071 if ((op & 0xf0) == 0x10)
1072 bin_shx(op);
1073 if ((op & 0xf0) == 0x20)
1074 bin_mul(op);
1075 if ((op & 0xf0) == 0x30)
1076 bin_cmp(op);
1079 static void load_regs2(int *r0, int *r1, int *r2)
1081 struct tmp *t0 = TMP(0);
1082 struct tmp *t1 = TMP(1);
1083 struct tmp *t2 = TMP(2);
1084 *r0 = TMP_REG(t0);
1085 *r1 = TMP_REG2(t1, *r0);
1086 *r2 = TMP_REG3(t2, *r0, *r1);
1087 tmp_to(t0, *r0);
1088 tmp_to(t1, *r1);
1089 tmp_to(t2, *r2);
1092 void o_memcpy(void)
1094 int rd, rs, rn;
1095 load_regs2(&rn, &rs, &rd);
1096 i_memcpy(rd, rs, rn);
1097 tmp_drop(2);
1100 void o_memset(void)
1102 int rd, rs, rn;
1103 load_regs2(&rn, &rs, &rd);
1104 i_memset(rd, rs, rn);
1105 tmp_drop(2);
1108 long o_mklabel(void)
1110 return cslen;
1113 static long jxz(long addr, int z)
1115 int r = TMP_REG(TMP(0));
1116 tmp_pop(r);
1117 i_b_if(addr, r, z);
1118 return cslen - 4;
1121 long o_jz(long addr)
1123 return jxz(addr, 1);
1126 long o_jnz(long addr)
1128 return jxz(addr, 0);
1131 long o_jmp(long addr)
1133 i_b(addr);
1134 return cslen - 4;
1137 void o_filljmp2(long addr, long jmpdst)
1139 i_b_fill((void *) cs + addr, jmpdst - addr);
1142 void o_filljmp(long addr)
1144 o_filljmp2(addr, cslen);
1147 void o_call(int argc, int rets)
1149 struct tmp *t;
1150 int i;
1151 int aregs = MIN(ARRAY_SIZE(argregs), argc);
1152 for (i = 0; i < ARRAY_SIZE(argregs); i++)
1153 if (regs[argregs[i]] && regs[argregs[i]] - tmps < ntmp - argc)
1154 tmp_mem(regs[argregs[i]]);
1155 if (argc > aregs) {
1156 sp_push(LONGSZ * (argc - aregs));
1157 for (i = argc - 1; i >= aregs; --i) {
1158 int reg = TMP_REG(TMP(0));
1159 tmp_pop(reg);
1160 i_ldr(0, reg, REG_SP, (i - aregs) * LONGSZ, LONGSZ);
1163 for (i = aregs - 1; i >= 0; --i)
1164 tmp_to(TMP(aregs - i - 1), argregs[i]);
1165 tmp_drop(aregs);
1166 t = TMP(0);
1167 if (t->loc == LOC_SYM && !t->bt) {
1168 i_call(t->sym);
1169 tmp_drop(1);
1170 } else {
1171 int reg = t->loc == LOC_REG ? t->addr : REG_TMP;
1172 tmp_pop(reg);
1173 i_call_reg(reg);
1175 if (rets)
1176 tmp_push(REG_RET);
1179 int o_nogen(void)
1181 return nogen++;
1184 void o_dogen(void)
1186 nogen = 0;
1189 void dat_bss(char *name, int size, int global)
1191 out_sym(name, OUT_BSS | (global ? OUT_GLOB : 0), bsslen, size);
1192 bsslen += ALIGN(size, LONGSZ);
1195 #define MAXDATS (1 << 10)
1196 static char dat_names[MAXDATS][NAMELEN];
1197 static int dat_offs[MAXDATS];
1198 static int ndats;
1200 void err(char *msg);
1201 void *dat_dat(char *name, int size, int global)
1203 void *addr = ds + dslen;
1204 int idx = ndats++;
1205 if (idx >= MAXDATS)
1206 err("nomem: MAXDATS reached!\n");
1207 strcpy(dat_names[idx], name);
1208 dat_offs[idx] = dslen;
1209 out_sym(name, OUT_DS | (global ? OUT_GLOB : 0), dslen, size);
1210 dslen += ALIGN(size, LONGSZ);
1211 return addr;
1214 static int dat_off(char *name)
1216 int i;
1217 for (i = 0; i < ndats; i++)
1218 if (!strcmp(name, dat_names[i]))
1219 return dat_offs[i];
1220 return 0;
1223 void o_datset(char *name, int off, unsigned bt)
1225 struct tmp *t = TMP(0);
1226 int sym_off = dat_off(name) + off;
1227 if (t->loc == LOC_NUM && !t->bt) {
1228 num_cast(t, bt);
1229 memcpy(ds + sym_off, &t->addr, BT_SZ(bt));
1231 if (t->loc == LOC_SYM && !t->bt) {
1232 out_rel(t->sym, OUT_DS, sym_off);
1233 memcpy(ds + sym_off, &t->off, BT_SZ(bt));
1235 tmp_drop(1);
1238 /* compiled division functions; div.s contains the source */
1239 static int udivdi3[] = {
1240 0xe3a02000, 0xe3a03000, 0xe1110001, 0x0a00000a,
1241 0xe1b0c211, 0xe2822001, 0x5afffffc, 0xe3a0c001,
1242 0xe2522001, 0x4a000004, 0xe1500211, 0x3afffffb,
1243 0xe0400211, 0xe083321c, 0xeafffff8, 0xe1a01000,
1244 0xe1a00003, 0xe1a0f00e,
1246 static int umoddi3[] = {
1247 0xe92d4000, 0xebffffeb, 0xe1a00001, 0xe8bd8000,
1249 static int divdi3[] = {
1250 0xe92d4030, 0xe1a04000, 0xe1a05001, 0xe1100000,
1251 0x42600000, 0xe1110001, 0x42611000, 0xebffffe1,
1252 0xe1340005, 0x42600000, 0xe1140004, 0x42611000,
1253 0xe8bd8030,
1255 static int moddi3[] = {
1256 0xe92d4000, 0xebfffff0, 0xe1a00001, 0xe8bd8000,
1259 void o_write(int fd)
1261 if (putdiv) {
1262 out_sym("__udivdi3", OUT_CS, cslen, 0);
1263 os(udivdi3, sizeof(udivdi3));
1264 out_sym("__umoddi3", OUT_CS, cslen, 0);
1265 os(umoddi3, sizeof(umoddi3));
1266 out_sym("__divdi3", OUT_CS, cslen, 0);
1267 os(divdi3, sizeof(divdi3));
1268 out_sym("__moddi3", OUT_CS, cslen, 0);
1269 os(moddi3, sizeof(moddi3));
1271 out_write(fd, cs, cslen, ds, dslen);