gen: don't save variable registers before calls
[neatcc.git] / gen.c
blob139b5d3efea311127d08819925baeb4f10f4a5d9
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_TST, 0, rn, 1, 0, 14) | rn);
402 oi(BL(0, 0, 20));
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(ADD(I_SUB, rn, rn, 0, 1, 14) | 1);
406 oi(BL(14, 0, -20));
409 static void i_memset(int rd, int rs, int rn)
411 oi(ADD(I_TST, 0, rn, 1, 0, 14) | rn);
412 oi(BL(0, 0, 16));
413 oi(LDR(0, rs, rd, 1, 1, 0, 0) | 1);
414 oi(ADD(I_SUB, rn, rn, 0, 1, 14) | 1);
415 oi(BL(14, 0, -16));
418 static void i_call_reg(int rd)
420 i_mov(I_MOV, REG_LR, REG_PC);
421 i_mov(I_MOV, REG_PC, rd);
424 static void i_call(char *sym)
426 if (!nogen)
427 out_rel(sym, OUT_CS | OUT_REL24, cslen);
428 oi(BL(14, 1, 0));
431 static void i_prolog(void)
433 func_beg = cslen;
434 ndats = 0;
435 oi(0xe1a0c00d); /* mov r12, sp */
436 oi(0xe92d000f); /* stmfd sp!, {r0-r3} */
437 oi(0xe92d5ff0); /* stmfd sp!, {r0-r11, r12, lr} */
438 oi(0xe1a0b00d); /* mov fp, sp */
439 oi(0xe24dd000); /* sub sp, sp, xx */
440 oi(0xe28fa000); /* add dp, pc, xx */
443 static void i_epilog(void)
445 int dpoff;
446 oi(0xe89baff0); /* ldmfd fp, {r4-r11, sp, pc} */
447 dpoff = add_decimm(add_rndimm(add_encimm(cslen - func_beg - 28)));
448 cslen = func_beg + dpoff + 28;
449 maxsp = ALIGN(maxsp, 8);
450 maxsp = add_decimm(add_rndimm(add_encimm(maxsp)));
451 /* fill stack sub: sp = sp - xx */
452 *(long *) (cs + func_beg + 16) |= add_encimm(maxsp);
453 /* fill data ptr addition: dp = pc + xx */
454 *(long *) (cs + func_beg + 20) |= add_encimm(dpoff);
455 pool_write();
458 static long sp_push(int size)
460 sp += size;
461 if (sp > maxsp)
462 maxsp = sp;
463 return sp;
466 static void tmp_mem(struct tmp *tmp)
468 int src = tmp->addr;
469 if (!(tmp->loc == LOC_REG))
470 return;
471 if (tmpsp == -1)
472 tmpsp = sp;
473 tmp->addr = -sp_push(LONGSZ);
474 i_ldr(0, src, REG_FP, tmp->addr, LONGSZ);
475 regs[src] = NULL;
476 tmp->loc = LOC_MEM;
479 static void num_cast(struct tmp *t, unsigned bt)
481 if (!(bt & BT_SIGNED) && BT_SZ(bt) != LONGSZ)
482 t->addr &= ((1l << (long) (BT_SZ(bt) * 8)) - 1);
483 if (bt & BT_SIGNED && BT_SZ(bt) != LONGSZ &&
484 t->addr > (1l << (BT_SZ(bt) * 8 - 1)))
485 t->addr = -((1l << (BT_SZ(bt) * 8)) - t->addr);
488 static void tmp_reg(struct tmp *tmp, int dst, int deref)
490 int bt = tmp->bt;
491 if (!tmp->bt)
492 deref = 0;
493 if (deref)
494 tmp->bt = 0;
495 if (tmp->loc == LOC_NUM) {
496 i_num(dst, tmp->addr);
497 tmp->addr = dst;
498 regs[dst] = tmp;
499 tmp->loc = LOC_REG;
501 if (tmp->loc == LOC_SYM) {
502 i_sym(dst, tmp->sym, tmp->off);
503 tmp->addr = dst;
504 regs[dst] = tmp;
505 tmp->loc = LOC_REG;
507 if (tmp->loc == LOC_REG) {
508 if (deref)
509 i_ldr(1, dst, tmp->addr, 0, bt);
510 else if (dst != tmp->addr)
511 i_mov(I_MOV, dst, tmp->addr);
512 regs[tmp->addr] = NULL;
514 if (tmp->loc == LOC_LOCAL) {
515 if (deref)
516 i_ldr(1, dst, REG_FP, tmp->addr + tmp->off, bt);
517 else
518 i_add_imm(I_ADD, dst, REG_FP, tmp->addr + tmp->off);
520 if (tmp->loc == LOC_MEM) {
521 i_ldr(1, dst, REG_FP, tmp->addr, LONGSZ);
522 if (deref)
523 i_ldr(1, dst, dst, 0, bt);
525 tmp->addr = dst;
526 regs[dst] = tmp;
527 tmp->loc = LOC_REG;
530 static void reg_free(int reg)
532 int i;
533 if (!regs[reg])
534 return;
535 for (i = 0; i < ARRAY_SIZE(tmpregs); i++)
536 if (!regs[tmpregs[i]]) {
537 tmp_reg(regs[reg], tmpregs[i], 0);
538 return;
540 tmp_mem(regs[reg]);
543 static void reg_for(int reg, struct tmp *t)
545 if (regs[reg] && regs[reg] != t)
546 reg_free(reg);
549 static void tmp_mv(struct tmp *t, int reg)
551 reg_for(reg, t);
552 tmp_reg(t, reg, 0);
555 static void tmp_to(struct tmp *t, int reg)
557 reg_for(reg, t);
558 tmp_reg(t, reg, 1);
561 static void tmp_drop(int n)
563 int i;
564 for (i = ntmp - n; i < ntmp; i++)
565 if (tmps[i].loc == LOC_REG)
566 regs[tmps[i].addr] = NULL;
567 ntmp -= n;
570 static void tmp_pop(int reg)
572 struct tmp *t = TMP(0);
573 tmp_to(t, reg);
574 tmp_drop(1);
577 static struct tmp *tmp_new(void)
579 return &tmps[ntmp++];
582 static void tmp_push(int reg)
584 struct tmp *t = tmp_new();
585 t->addr = reg;
586 t->bt = 0;
587 t->loc = LOC_REG;
588 regs[reg] = t;
591 void o_local(long addr)
593 struct tmp *t = tmp_new();
594 t->addr = -addr;
595 t->loc = LOC_LOCAL;
596 t->bt = 0;
597 t->off = 0;
600 void o_num(long num)
602 struct tmp *t = tmp_new();
603 t->addr = num;
604 t->bt = 0;
605 t->loc = LOC_NUM;
608 void o_sym(char *name)
610 struct tmp *t = tmp_new();
611 strcpy(t->sym, name);
612 t->loc = LOC_SYM;
613 t->bt = 0;
614 t->off = 0;
617 void o_tmpdrop(int n)
619 if (n == -1 || n > ntmp)
620 n = ntmp;
621 tmp_drop(n);
622 if (!ntmp) {
623 if (tmpsp != -1)
624 sp = tmpsp;
625 tmpsp = -1;
629 #define FORK_REG 0x00
631 /* make sure tmps remain intact after a conditional expression */
632 void o_fork(void)
634 int i;
635 for (i = 0; i < ntmp - 1; i++)
636 tmp_mem(&tmps[i]);
639 void o_forkpush(void)
641 tmp_pop(FORK_REG);
644 void o_forkjoin(void)
646 tmp_push(FORK_REG);
649 void o_tmpswap(void)
651 struct tmp *t1 = TMP(0);
652 struct tmp *t2 = TMP(1);
653 struct tmp t;
654 memcpy(&t, t1, sizeof(t));
655 memcpy(t1, t2, sizeof(t));
656 memcpy(t2, &t, sizeof(t));
657 if (t1->loc == LOC_REG)
658 regs[t1->addr] = t1;
659 if (t2->loc == LOC_REG)
660 regs[t2->addr] = t2;
663 static int reg_get(int mask)
665 int i;
666 for (i = 0; i < ARRAY_SIZE(tmpregs); i++)
667 if ((1 << tmpregs[i]) & mask && !regs[tmpregs[i]])
668 return tmpregs[i];
669 for (i = 0; i < ARRAY_SIZE(tmpregs); i++)
670 if ((1 << tmpregs[i]) & mask) {
671 reg_free(tmpregs[i]);
672 return tmpregs[i];
674 return 0;
677 static void tmp_copy(struct tmp *t1)
679 struct tmp *t2 = tmp_new();
680 memcpy(t2, t1, sizeof(*t1));
681 if (!(t1->loc & (LOC_REG | LOC_MEM)))
682 return;
683 if (t1->loc == LOC_MEM) {
684 tmp_reg(t2, reg_get(~0), 0);
685 } else if (t1->loc == LOC_REG) {
686 t2->addr = TMP_REG2(t2, t1->addr);
687 i_mov(I_MOV, t2->addr, t1->addr);
688 regs[t2->addr] = t2;
692 void o_tmpcopy(void)
694 tmp_copy(TMP(0));
697 void o_cast(unsigned bt)
699 struct tmp *t = TMP(0);
700 if (t->bt) {
701 t->bt = bt;
702 return;
704 if (t->loc == LOC_NUM) {
705 num_cast(t, bt);
706 return;
708 if (BT_SZ(bt) != LONGSZ) {
709 int reg = TMP_REG(t);
710 tmp_to(t, reg);
711 if (bt & BT_SIGNED)
712 i_sx(reg, BT_SZ(bt) * 8);
713 else
714 i_zx(reg, BT_SZ(bt) * 8);
718 void o_func_beg(char *name, int global)
720 out_sym(name, (global ? OUT_GLOB : 0) | OUT_CS, cslen, 0);
721 i_prolog();
722 sp = 0;
723 maxsp = sp;
724 ntmp = 0;
725 tmpsp = -1;
726 nret = 0;
727 memset(regs, 0, sizeof(regs));
730 void o_deref(unsigned bt)
732 struct tmp *t = TMP(0);
733 if (t->bt)
734 tmp_to(t, TMP_REG(t));
735 t->bt = bt;
738 void o_load(void)
740 struct tmp *t = TMP(0);
741 tmp_to(t, TMP_REG(t));
744 #define TMP_NUM(t) ((t)->loc == LOC_NUM && !(t)->bt)
745 #define LOCAL_PTR(t) ((t)->loc == LOC_LOCAL && !(t)->bt)
746 #define SYM_PTR(t) ((t)->loc == LOC_SYM && !(t)->bt)
748 int o_popnum(long *c)
750 struct tmp *t = TMP(0);
751 if (!TMP_NUM(t))
752 return 1;
753 *c = t->addr;
754 tmp_drop(1);
755 return 0;
758 void o_ret(int rets)
760 if (rets)
761 tmp_pop(REG_RET);
762 else
763 i_num(REG_RET, 0);
764 ret[nret++] = o_jmp(0);
767 void o_func_end(void)
769 int i;
770 for (i = 0; i < nret; i++)
771 o_filljmp(ret[i]);
772 i_epilog();
775 long o_mklocal(int size)
777 return sp_push(ALIGN(size, LONGSZ));
780 void o_rmlocal(long addr, int sz)
782 sp = addr - sz;
785 long o_arg(int i)
787 return -(10 + i) << 2;
790 void o_assign(unsigned bt)
792 struct tmp *t1 = TMP(0);
793 struct tmp *t2 = TMP(1);
794 int r1 = TMP_REG(t1);
795 int r2 = TMP_REG2(t2, r1);
796 int off = 0;
797 tmp_to(t1, r1);
798 if (t2->bt)
799 tmp_to(t2, r2);
800 if (t2->loc == LOC_LOCAL) {
801 r2 = REG_FP;
802 off = t2->addr + t2->off;
803 } else {
804 tmp_mv(t2, r2);
806 tmp_drop(2);
807 i_ldr(0, r1, r2, off, bt);
808 tmp_push(r1);
811 static long cu(int op, long i)
813 switch (op) {
814 case O_NEG:
815 return -i;
816 case O_NOT:
817 return ~i;
818 case O_LNOT:
819 return !i;
821 return 0;
824 static int c_uop(int op)
826 struct tmp *t1 = TMP(0);
827 if (!TMP_NUM(t1))
828 return 1;
829 tmp_drop(1);
830 o_num(cu(op, t1->addr));
831 return 0;
834 static long cb(int op, long a, long b)
836 switch (op & 0xff) {
837 case O_ADD:
838 return a + b;
839 case O_SUB:
840 return a - b;
841 case O_AND:
842 return a & b;
843 case O_OR:
844 return a | b;
845 case O_XOR:
846 return a ^ b;
847 case O_MUL:
848 return a * b;
849 case O_DIV:
850 return a / b;
851 case O_MOD:
852 return a % b;
853 case O_SHL:
854 return a << b;
855 case O_SHR:
856 if (op & O_SIGNED)
857 return a >> b;
858 else
859 return (unsigned long) a >> b;
860 case O_LT:
861 return a < b;
862 case O_GT:
863 return a > b;
864 case O_LE:
865 return a <= b;
866 case O_GE:
867 return a >= b;
868 case O_EQ:
869 return a == b;
870 case O_NEQ:
871 return a != b;
873 return 0;
876 static int c_bop(int op)
878 struct tmp *t1 = TMP(0);
879 struct tmp *t2 = TMP(1);
880 int locals = LOCAL_PTR(t1) + LOCAL_PTR(t2);
881 int syms = SYM_PTR(t1) + SYM_PTR(t2);
882 int nums = TMP_NUM(t1) + TMP_NUM(t2);
883 if (syms + locals == 2 || syms + nums + locals != 2)
884 return 1;
885 if (nums == 1)
886 if ((op != O_ADD && op != O_SUB) || (op == O_SUB && TMP_NUM(t2)))
887 return 1;
888 if (nums == 1) {
889 long o1 = TMP_NUM(t1) ? t1->addr : t1->off;
890 long o2 = TMP_NUM(t2) ? t2->addr : t2->off;
891 long ret = cb(op, o2, o1);
892 if (!TMP_NUM(t1))
893 o_tmpswap();
894 t2->off = ret;
895 tmp_drop(1);
896 } else {
897 long ret = cb(op, t2->addr, t1->addr);
898 tmp_drop(2);
899 o_num(ret);
901 return 0;
904 void o_uop(int op)
906 int r1 = TMP_REG(TMP(0));
907 if (!c_uop(op))
908 return;
909 tmp_to(TMP(0), r1);
910 switch (op) {
911 case O_NEG:
912 i_neg(r1);
913 break;
914 case O_NOT:
915 i_not(r1);
916 break;
917 case O_LNOT:
918 i_lnot(r1);
919 break;
923 static void bin_regs(int *r1, int *r2)
925 struct tmp *t2 = TMP(0);
926 struct tmp *t1 = TMP(1);
927 *r2 = TMP_REG(t2);
928 tmp_to(t2, *r2);
929 *r1 = TMP_REG2(t1, *r2);
930 tmp_pop(*r2);
931 tmp_pop(*r1);
934 static void bin_add(int op)
936 /* opcode for O_ADD, O_SUB, O_AND, O_OR, O_XOR */
937 static int rx[] = {I_ADD, I_SUB, I_AND, I_ORR, I_EOR};
938 int r1, r2;
939 bin_regs(&r1, &r2);
940 i_add(rx[op & 0xff], r1, r1, r2);
941 tmp_push(r1);
944 static void bin_shx(int op)
946 int sm = SM_LSL;
947 int r1, r2;
948 bin_regs(&r1, &r2);
949 if ((op & 0x0f) == 1)
950 sm = op & O_SIGNED ? SM_ASR : SM_LSR;
951 i_shl(sm, r1, r1, r2);
952 tmp_push(r1);
955 static int log2a(unsigned long n)
957 int i = 0;
958 for (i = 0; i < LONGSZ * 8; i++)
959 if (n & (1u << i))
960 break;
961 if (i == LONGSZ * 8 || !(n >> (i + 1)))
962 return i;
963 return -1;
966 /* optimized version of mul/div/mod for powers of two */
967 static int mul_2(int op)
969 struct tmp *t1 = TMP(0);
970 struct tmp *t2 = TMP(1);
971 long n;
972 int r2;
973 int p;
974 if (op == O_MUL && t2->loc == LOC_NUM && !t2->bt)
975 o_tmpswap();
976 if (t1->loc != LOC_NUM || t1->bt)
977 return 1;
978 n = t1->addr;
979 p = log2a(n);
980 if (n && p == -1)
981 return 1;
982 if (op == O_MUL) {
983 tmp_drop(1);
984 if (n == 1)
985 return 0;
986 if (n == 0) {
987 tmp_drop(1);
988 o_num(0);
989 return 0;
991 r2 = TMP_REG(t2);
992 tmp_to(t2, r2);
993 i_shl_imm(SM_LSL, r2, p);
994 return 0;
996 if (op == O_DIV) {
997 tmp_drop(1);
998 if (n == 1)
999 return 0;
1000 r2 = TMP_REG(t2);
1001 tmp_to(t2, r2);
1002 i_shl_imm(SM_LSR, r2, p);
1003 return 0;
1005 if (op == O_MOD) {
1006 tmp_drop(1);
1007 if (n == 1) {
1008 tmp_drop(1);
1009 o_num(0);
1010 return 0;
1012 r2 = TMP_REG(t2);
1013 tmp_to(t2, r2);
1014 i_zx(r2, p);
1015 return 0;
1017 return 1;
1020 static void bin_div(int op)
1022 struct tmp *t2 = TMP(0);
1023 struct tmp *t1 = TMP(1);
1024 char *func;
1025 int i;
1026 putdiv = 1;
1027 if ((op & 0xff) == O_DIV)
1028 func = op & O_SIGNED ? "__divdi3" : "__udivdi3";
1029 else
1030 func = op & O_SIGNED ? "__moddi3" : "__umoddi3";
1031 for (i = 0; i < ARRAY_SIZE(argregs); i++)
1032 if (regs[argregs[i]] && regs[argregs[i]] - tmps < ntmp - 2)
1033 tmp_mem(regs[argregs[i]]);
1034 tmp_to(t1, argregs[0]);
1035 tmp_to(t2, argregs[1]);
1036 tmp_drop(2);
1037 i_call(func);
1038 tmp_push(REG_RET);
1041 static void bin_mul(int op)
1043 int r1, r2;
1044 if (!mul_2(op & 0xff))
1045 return;
1046 if ((op & 0xff) == O_DIV || (op & 0xff) == O_MOD) {
1047 bin_div(op);
1048 } else {
1049 bin_regs(&r1, &r2);
1050 i_mul(r1, r1, r2);
1051 tmp_push(r1);
1055 static void bin_cmp(int op)
1057 /* lt, gt, le, ge, eq, neq */
1058 static int ucond[] = {3, 8, 9, 2, 0, 1};
1059 static int scond[] = {11, 12, 13, 10, 0, 1};
1060 int r1, r2;
1061 bin_regs(&r1, &r2);
1062 i_cmp(I_CMP, r1, r2);
1063 i_set(op & O_SIGNED ? scond[op & 0x0f] : ucond[op & 0x0f], r1);
1064 tmp_push(r1);
1067 void o_bop(int op)
1069 if (!c_bop(op))
1070 return;
1071 if ((op & 0xf0) == 0x00)
1072 bin_add(op);
1073 if ((op & 0xf0) == 0x10)
1074 bin_shx(op);
1075 if ((op & 0xf0) == 0x20)
1076 bin_mul(op);
1077 if ((op & 0xf0) == 0x30)
1078 bin_cmp(op);
1081 static void load_regs2(int *r0, int *r1, int *r2)
1083 struct tmp *t0 = TMP(0);
1084 struct tmp *t1 = TMP(1);
1085 struct tmp *t2 = TMP(2);
1086 *r0 = TMP_REG(t0);
1087 *r1 = TMP_REG2(t1, *r0);
1088 *r2 = TMP_REG3(t2, *r0, *r1);
1089 tmp_to(t0, *r0);
1090 tmp_to(t1, *r1);
1091 tmp_to(t2, *r2);
1094 void o_memcpy(void)
1096 int rd, rs, rn;
1097 load_regs2(&rn, &rs, &rd);
1098 i_memcpy(rd, rs, rn);
1099 tmp_drop(2);
1102 void o_memset(void)
1104 int rd, rs, rn;
1105 load_regs2(&rn, &rs, &rd);
1106 i_memset(rd, rs, rn);
1107 tmp_drop(2);
1110 long o_mklabel(void)
1112 return cslen;
1115 static long jxz(long addr, int z)
1117 int r = TMP_REG(TMP(0));
1118 tmp_pop(r);
1119 i_b_if(addr, r, z);
1120 return cslen - 4;
1123 long o_jz(long addr)
1125 return jxz(addr, 1);
1128 long o_jnz(long addr)
1130 return jxz(addr, 0);
1133 long o_jmp(long addr)
1135 i_b(addr);
1136 return cslen - 4;
1139 void o_filljmp2(long addr, long jmpdst)
1141 i_b_fill((void *) cs + addr, jmpdst - addr);
1144 void o_filljmp(long addr)
1146 o_filljmp2(addr, cslen);
1149 void o_call(int argc, int rets)
1151 struct tmp *t;
1152 int i;
1153 int aregs = MIN(ARRAY_SIZE(argregs), argc);
1154 for (i = 0; i < ARRAY_SIZE(argregs); i++)
1155 if (regs[argregs[i]] && regs[argregs[i]] - tmps < ntmp - argc)
1156 tmp_mem(regs[argregs[i]]);
1157 if (argc > aregs) {
1158 sp_push(LONGSZ * (argc - aregs));
1159 for (i = argc - 1; i >= aregs; --i) {
1160 int reg = TMP_REG(TMP(0));
1161 tmp_pop(reg);
1162 i_ldr(0, reg, REG_SP, (i - aregs) * LONGSZ, LONGSZ);
1165 for (i = aregs - 1; i >= 0; --i)
1166 tmp_to(TMP(aregs - i - 1), argregs[i]);
1167 tmp_drop(aregs);
1168 t = TMP(0);
1169 if (t->loc == LOC_SYM && !t->bt) {
1170 i_call(t->sym);
1171 tmp_drop(1);
1172 } else {
1173 int reg = t->loc == LOC_REG ? t->addr : REG_TMP;
1174 tmp_pop(reg);
1175 i_call_reg(reg);
1177 if (rets)
1178 tmp_push(REG_RET);
1181 int o_nogen(void)
1183 return nogen++;
1186 void o_dogen(void)
1188 nogen = 0;
1191 void dat_bss(char *name, int size, int global)
1193 out_sym(name, OUT_BSS | (global ? OUT_GLOB : 0), bsslen, size);
1194 bsslen += ALIGN(size, LONGSZ);
1197 #define MAXDATS (1 << 10)
1198 static char dat_names[MAXDATS][NAMELEN];
1199 static int dat_offs[MAXDATS];
1200 static int ndats;
1202 void err(char *msg);
1203 void *dat_dat(char *name, int size, int global)
1205 void *addr = ds + dslen;
1206 int idx = ndats++;
1207 if (idx >= MAXDATS)
1208 err("nomem: MAXDATS reached!\n");
1209 strcpy(dat_names[idx], name);
1210 dat_offs[idx] = dslen;
1211 out_sym(name, OUT_DS | (global ? OUT_GLOB : 0), dslen, size);
1212 dslen += ALIGN(size, LONGSZ);
1213 return addr;
1216 static int dat_off(char *name)
1218 int i;
1219 for (i = 0; i < ndats; i++)
1220 if (!strcmp(name, dat_names[i]))
1221 return dat_offs[i];
1222 return 0;
1225 void o_datset(char *name, int off, unsigned bt)
1227 struct tmp *t = TMP(0);
1228 int sym_off = dat_off(name) + off;
1229 if (t->loc == LOC_NUM && !t->bt) {
1230 num_cast(t, bt);
1231 memcpy(ds + sym_off, &t->addr, BT_SZ(bt));
1233 if (t->loc == LOC_SYM && !t->bt) {
1234 out_rel(t->sym, OUT_DS, sym_off);
1235 memcpy(ds + sym_off, &t->off, BT_SZ(bt));
1237 tmp_drop(1);
1240 /* compiled division functions; div.s contains the source */
1241 static int udivdi3[] = {
1242 0xe3a02000, 0xe3a03000, 0xe1110001, 0x0a00000a,
1243 0xe1b0c211, 0xe2822001, 0x5afffffc, 0xe3a0c001,
1244 0xe2522001, 0x4a000004, 0xe1500211, 0x3afffffb,
1245 0xe0400211, 0xe083321c, 0xeafffff8, 0xe1a01000,
1246 0xe1a00003, 0xe1a0f00e,
1248 static int umoddi3[] = {
1249 0xe92d4000, 0xebffffeb, 0xe1a00001, 0xe8bd8000,
1251 static int divdi3[] = {
1252 0xe92d4010, 0xe1100000, 0x42600000, 0x43a04001,
1253 0xe1110001, 0x42600000, 0x42444001, 0xebffffe1,
1254 0xe1140004, 0x12400000, 0xe8bd8010,
1256 static int moddi3[] = {
1257 0xe92d4000, 0xebfffff2, 0xe1a00001, 0xe8bd8000,
1260 void o_write(int fd)
1262 if (putdiv) {
1263 out_sym("__udivdi3", OUT_CS, cslen, 0);
1264 os(udivdi3, sizeof(udivdi3));
1265 out_sym("__umoddi3", OUT_CS, cslen, 0);
1266 os(umoddi3, sizeof(umoddi3));
1267 out_sym("__divdi3", OUT_CS, cslen, 0);
1268 os(divdi3, sizeof(divdi3));
1269 out_sym("__moddi3", OUT_CS, cslen, 0);
1270 os(moddi3, sizeof(moddi3));
1272 out_write(fd, cs, cslen, ds, dslen);