1 /* neatcc intermediate code generation */
7 static struct ic
*ic
; /* intermediate code */
8 static long ic_n
, ic_sz
; /* number of instructions in ic[] */
9 static long iv
[NTMPS
]; /* operand stack */
10 static long iv_n
; /* number of values in iv[] */
11 static long *lab_loc
; /* label locations */
12 static long lab_n
, lab_sz
; /* number of labels in lab_loc[] */
13 static long lab_last
; /* the last label target */
15 static int io_num(void);
16 static int io_mul2(void);
17 static int io_cmp(void);
18 static int io_jmp(void);
19 static int io_addr(void);
20 static int io_loc(void);
21 static int io_imm(void);
22 static int io_call(void);
23 static void io_deadcode(void);
25 static void iv_put(long n
);
27 static struct ic
*ic_put(long op
, long arg1
, long arg2
, long arg3
)
31 ic_sz
= MAX(128, ic_sz
* 2);
32 ic
= mextend(ic
, ic_n
, ic_sz
, sizeof(*ic
));
44 static void ic_back(long pos
)
47 for (i
= pos
; i
< ic_n
; i
++)
48 if (ic
[i
].op
& O_CALL
)
53 static long iv_pop(void)
58 static long iv_get(int n
)
60 return iv
[iv_n
- n
- 1];
63 static void iv_put(long n
)
68 static void iv_drop(int n
)
70 iv_n
= MAX(0, iv_n
- n
);
73 static void iv_swap(int x
, int y
)
75 long v
= iv
[iv_n
- x
- 1];
76 iv
[iv_n
- x
- 1] = iv
[iv_n
- y
- 1];
80 static void iv_dup(void)
82 iv
[iv_n
] = iv
[iv_n
- 1];
88 ic_put(O_MOV
| O_NUM
, n
, 0, 0);
93 ic_put(O_MOV
| O_LOC
, id
, 0, 0);
98 ic_put(O_MOV
| O_SYM
, out_sym(sym
), 0, 0);
101 void o_tmpdrop(int n
)
103 iv_drop(n
>= 0 ? n
: iv_n
);
116 /* return one if the given value is constant */
117 static int ic_const(long iv
)
119 long oc
= O_C(ic
[iv
].op
);
120 return oc
& O_MOV
&& oc
& (O_NUM
| O_SYM
| O_LOC
);
123 /* return one if the given value is a simple load */
124 static int ic_load(long iv
)
126 long oc
= O_C(ic
[iv
].op
);
128 if (oc
& O_LD
&& oc
& (O_NUM
| O_SYM
| O_LOC
)) {
129 for (i
= iv
+ 1; i
< ic_n
; i
++)
141 if (ic_const(r2
) && !ic_const(r1
)) { /* load constants last */
142 ic_put(ic
[r2
].op
, ic
[r2
].a1
, ic
[r2
].a2
, ic
[r2
].a3
);
145 ic_put(op
, r2
, r1
, 0);
146 io_num() && io_mul2() && io_addr() && io_imm();
152 ic_put(op
, r1
, 0, 0);
157 void o_assign(long bt
)
161 if (ic_const(lv
) || ic_load(lv
)) { /* load constants last */
162 ic_put(ic
[lv
].op
, ic
[lv
].a1
, ic
[lv
].a2
, ic
[lv
].a3
);
165 ic_put(O_MK(O_ST
| O_NUM
, bt
), rv
, lv
, 0);
170 void o_deref(long bt
)
173 ic_put(O_MK(O_LD
| O_NUM
, bt
), r1
, 0, 0);
179 if (T_SZ(bt
) != ULNG
) {
181 ic_put(O_MK(O_MOV
, bt
), r1
, 0, 0);
191 ic_put(O_MCPY
, r0
, r1
, r2
);
199 ic_put(O_MSET
, r0
, r1
, r2
);
202 void o_call(int argc
, int ret
)
205 long *args
= malloc(argc
* sizeof(c
->args
[0]));
207 for (i
= argc
- 1; i
>= 0; --i
)
209 for (i
= argc
- 1; i
>= 0; --i
) {
211 if (ic_const(iv
) || ic_load(iv
)) { /* load constants last */
212 ic_put(ic
[iv
].op
, ic
[iv
].a1
, ic
[iv
].a2
, ic
[iv
].a3
);
217 c
= ic_put(O_CALL
, r1
, 0, argc
);
227 ic_put(O_RET
, iv_pop(), 0, 0);
230 void o_label(long id
)
232 while (id
>= lab_sz
) {
233 lab_sz
= MAX(128, lab_sz
* 2);
234 lab_loc
= mextend(lab_loc
, lab_n
, lab_sz
, sizeof(*lab_loc
));
237 lab_loc
[lab_n
++] = -1;
244 ic_put(O_JMP
, 0, 0, id
);
249 ic_put(O_JZ
, iv_pop(), 0, id
);
253 int o_popnum(long *n
)
255 if (ic_num(ic
, iv_get(0), n
))
261 int o_popsym(long *sym
, long *off
)
263 if (ic_sym(ic
, iv_get(0), sym
, off
))
274 void o_back(long mark
)
279 void ic_get(struct ic
**c
, long *n
)
282 if (!ic_n
|| ~ic
[ic_n
- 1].op
& O_RET
|| lab_last
== ic_n
)
284 for (i
= 0; i
< ic_n
; i
++) /* filling branch targets */
285 if (ic
[i
].op
& O_JXX
)
286 ic
[i
].a3
= lab_loc
[ic
[i
].a3
];
287 io_deadcode(); /* removing dead code */
301 void ic_free(struct ic
*ic
)
307 /* intermediate code queries */
309 static long cb(long op
, long a
, long b
)
331 return O_T(op
) & T_MSIGN
? a
>> b
: (unsigned long) a
>> b
;
348 static long cu(int op
, long i
)
361 static long c_cast(long n
, unsigned bt
)
363 if (!(bt
& T_MSIGN
) && T_SZ(bt
) != ULNG
)
364 n
&= ((1l << (long) (T_SZ(bt
) * 8)) - 1);
365 if (bt
& T_MSIGN
&& T_SZ(bt
) != ULNG
&&
366 n
> (1l << (T_SZ(bt
) * 8 - 1)))
367 n
= -((1l << (T_SZ(bt
) * 8)) - n
);
371 int ic_num(struct ic
*ic
, long iv
, long *n
)
374 long oc
= O_C(ic
[iv
].op
);
375 long bt
= O_T(ic
[iv
].op
);
376 if (oc
& O_MOV
&& oc
& O_NUM
) {
381 if (ic_num(ic
, ic
[iv
].a1
, &n1
))
383 if (ic_num(ic
, ic
[iv
].a2
, &n2
))
385 *n
= cb(ic
[iv
].op
, n1
, n2
);
389 if (ic_num(ic
, ic
[iv
].a1
, &n1
))
391 *n
= cu(ic
[iv
].op
, n1
);
394 if (oc
& O_MOV
&& !(oc
& (O_NUM
| O_LOC
| O_SYM
))) {
395 if (ic_num(ic
, ic
[iv
].a1
, &n1
))
403 int ic_sym(struct ic
*ic
, long iv
, long *sym
, long *off
)
406 long oc
= O_C(ic
[iv
].op
);
407 if (oc
& O_MOV
&& oc
& O_SYM
) {
413 if ((ic_sym(ic
, ic
[iv
].a1
, sym
, off
) ||
414 ic_num(ic
, ic
[iv
].a2
, &n
)) &&
415 (ic_sym(ic
, ic
[iv
].a2
, sym
, off
) ||
416 ic_num(ic
, ic
[iv
].a1
, &n
)))
422 if (ic_sym(ic
, ic
[iv
].a1
, sym
, off
) ||
423 ic_num(ic
, ic
[iv
].a2
, &n
))
431 static int ic_off(struct ic
*ic
, long iv
, long *base_iv
, long *off
)
434 long oc
= O_C(ic
[iv
].op
);
435 if (oc
!= O_ADD
&& oc
!= O_SUB
) {
441 if ((ic_off(ic
, ic
[iv
].a1
, base_iv
, off
) ||
442 ic_num(ic
, ic
[iv
].a2
, &n
)) &&
443 (ic_off(ic
, ic
[iv
].a2
, base_iv
, off
) ||
444 ic_num(ic
, ic
[iv
].a1
, &n
)))
450 if (ic_off(ic
, ic
[iv
].a1
, base_iv
, off
) ||
451 ic_num(ic
, ic
[iv
].a2
, &n
))
459 /* number of register arguments */
460 int ic_regcnt(struct ic
*ic
)
464 return o
& (O_NUM
| O_SYM
| O_LOC
) ? 1 : 2;
466 return o
& (O_NUM
| O_SYM
| O_LOC
) ? 0 : 1;
468 return o
& (O_NUM
| O_SYM
| O_LOC
) ? 0 : 1;
470 return o
& (O_NUM
| O_SYM
| O_LOC
) ? 0 : 1;
478 return o
& (O_NUM
| O_SYM
| O_LOC
) ? 1 : 2;
482 return ((o
& O_NUM
) != 0);
484 return 1 + ((o
& O_NUM
) != 0);
489 * The returned array indicates the last instruction in
490 * which the value produced by each instruction is used.
492 long *ic_lastuse(struct ic
*ic
, long ic_n
)
494 long *luse
= calloc(ic_n
, sizeof(luse
[0]));
496 for (i
= ic_n
- 1; i
>= 0; --i
) {
497 int n
= ic_regcnt(ic
+ i
);
499 if (!(ic
[i
].op
& O_OUT
) || ic
[i
].op
& O_CALL
)
503 if (n
>= 1 && !luse
[ic
[i
].a1
])
505 if (n
>= 2 && !luse
[ic
[i
].a2
])
507 if (n
>= 3 && !luse
[ic
[i
].a3
])
509 if (ic
[i
].op
& O_CALL
)
510 for (j
= 0; j
< ic
[i
].a3
; j
++)
511 if (!luse
[ic
[i
].args
[j
]])
512 luse
[ic
[i
].args
[j
]] = i
;
517 /* intermediate code optimisations */
519 /* constant folding */
520 static int io_num(void)
523 if (!ic_num(ic
, iv_get(0), &n
)) {
531 static int log2a(unsigned long n
)
534 for (i
= 0; i
< LONGSZ
* 8; i
++)
537 if (i
== LONGSZ
* 8 || !(n
>> (i
+ 1)))
542 static long iv_num(long n
)
548 /* optimised multiplication operations for powers of two */
549 static int io_mul2(void)
554 long oc
= O_C(ic
[iv
].op
);
555 long bt
= O_T(ic
[iv
].op
);
558 if (oc
== O_MUL
&& !ic_num(ic
, ic
[iv
].a1
, &n
)) {
560 ic
[iv
].a1
= ic
[iv
].a2
;
563 if (ic_num(ic
, ic
[iv
].a2
, &n
))
579 ic_put(O_MK(O_SHL
, ULNG
), ic
[iv
].a1
, r2
, 0);
582 if (oc
== O_DIV
&& ~bt
& T_MSIGN
) {
589 ic_put(O_MK(O_SHR
, ULNG
), ic
[iv
].a1
, r2
, 0);
592 if (oc
== O_MOD
&& ~bt
& T_MSIGN
) {
598 r2
= iv_num(LONGSZ
* 8 - p
);
599 ic_put(O_MK(O_SHL
, ULNG
), ic
[iv
].a1
, r2
, 0);
601 r2
= iv_num(LONGSZ
* 8 - p
);
602 ic_put(O_MK(O_SHR
, ULNG
), r1
, r2
, 0);
608 /* optimise comparison */
609 static int io_cmp(void)
612 long cmp
= ic
[iv
].a1
;
613 if (O_C(ic
[iv
].op
) == O_LNOT
&& ic
[cmp
].op
& O_CMP
) {
622 /* optimise branch instructions after comparison */
623 static int io_jmp(void)
625 struct ic
*c
= &ic
[ic_n
- 1];
626 long oc
= O_C(c
->op
);
627 if (oc
& O_JZ
&& O_C(ic
[c
->a1
].op
) == O_LNOT
) {
628 c
->a1
= ic
[c
->a1
].a1
;
632 if (oc
& O_JZ
&& O_C(ic
[c
->a1
].op
) & O_CMP
) {
633 long cop
= (ic
[c
->a1
].op
& ~O_CMP
) | O_JCC
;
634 c
->op
= O_C(c
->op
) == O_JZ
? cop
^ 1 : cop
;
635 c
->a2
= ic
[c
->a1
].a2
;
636 c
->a1
= ic
[c
->a1
].a1
;
642 /* optimise accessing locals or symbols with an offset */
643 static int io_addr(void)
646 if (ic_off(ic
, iv_get(0), &iv
, &off
) || iv
== iv_get(0))
648 if (ic
[iv
].op
& O_MOV
&& ic
[iv
].op
& O_LOC
) {
650 ic_put(O_MOV
| O_LOC
, ic
[iv
].a1
, ic
[iv
].a2
+ off
, 0);
653 if (ic
[iv
].op
& O_MOV
&& ic
[iv
].op
& O_SYM
) {
655 ic_put(O_MOV
| O_SYM
, ic
[iv
].a1
, ic
[iv
].a2
+ off
, 0);
661 /* optimise loading and storing locals */
662 static int io_loc(void)
664 struct ic
*c
= &ic
[ic_n
- 1];
666 if (c
->op
& O_LD
&& c
->op
& O_NUM
) {
667 if (ic_off(ic
, c
->a1
, &iv
, &off
))
669 if (ic
[iv
].op
& O_MOV
&& ic
[iv
].op
& O_LOC
) {
670 c
->op
= (c
->op
& ~O_NUM
) | O_LOC
;
672 c
->a2
+= ic
[iv
].a2
+ off
;
679 if (c
->op
& O_ST
&& c
->op
& O_NUM
) {
680 if (ic_off(ic
, c
->a2
, &iv
, &off
))
682 if (ic
[iv
].op
& O_MOV
&& ic
[iv
].op
& O_LOC
) {
683 c
->op
= (c
->op
& ~O_NUM
) | O_LOC
;
685 c
->a3
+= ic
[iv
].a2
+ off
;
695 static int imm_ok(long op
, long n
, int arg
)
697 long m0
, m1
, m2
, m3
, mt
;
698 if (i_reg(op
| O_NUM
, &m0
, &m1
, &m2
, &m3
, &mt
))
700 return i_imm(arg
== 2 ? m2
: m1
, n
);
703 /* use instruction immediates */
704 static int io_imm(void)
706 struct ic
*c
= &ic
[ic_n
- 1];
707 long oc
= O_C(c
->op
);
709 if (oc
& (O_NUM
| O_LOC
| O_SYM
))
711 if (oc
== O_ADD
|| oc
== O_MUL
|| oc
== O_AND
|| oc
== O_OR
||
712 oc
== O_XOR
|| oc
== O_EQ
|| oc
== O_NE
) {
713 if (!ic_num(ic
, c
->a1
, &n
)) {
719 if (oc
== O_LT
|| oc
== O_GE
|| oc
== O_LE
|| oc
== O_GT
) {
720 if (!ic_num(ic
, c
->a1
, &n
)) {
727 if (oc
& O_JCC
&& !ic_num(ic
, c
->a1
, &n
)) {
733 if (oc
& O_JCC
&& !ic_num(ic
, c
->a2
, &n
) && imm_ok(c
->op
, n
, 1)) {
738 if (!(oc
& O_BOP
) || ic_num(ic
, c
->a2
, &n
))
740 if ((oc
== O_ADD
|| oc
== O_SUB
|| oc
& O_SHL
) && n
== 0) {
745 if (imm_ok(c
->op
, n
, 2)) {
753 /* calling symbols */
754 static int io_call(void)
756 struct ic
*c
= &ic
[ic_n
- 1];
758 if (c
->op
& O_CALL
&& !ic_sym(ic
, c
->a1
, &sym
, &off
) && !off
) {
766 /* remove dead code */
767 static void io_deadcode(void)
771 long src
= 0, dst
= 0;
773 /* liveness analysis */
774 live
= calloc(ic_n
, sizeof(live
[0]));
775 for (i
= ic_n
- 1; i
>= 0; i
--) {
776 int n
= ic_regcnt(ic
+ i
);
777 if (!(ic
[i
].op
& O_OUT
) || ic
[i
].op
& O_CALL
)
787 if (ic
[i
].op
& O_CALL
)
788 for (j
= 0; j
< ic
[i
].a3
; j
++)
789 live
[ic
[i
].args
[j
]] = 1;
791 /* the new indices of intermediate instructions */
792 nidx
= calloc(ic_n
, sizeof(nidx
[0]));
794 while (src
< ic_n
&& !live
[src
]) {
801 memcpy(ic
+ dst
, ic
+ src
, sizeof(ic
[src
]));
807 /* adjusting arguments and branch targets */
808 for (i
= 0; i
< ic_n
; i
++) {
809 int n
= ic_regcnt(ic
+ i
);
811 ic
[i
].a1
= nidx
[ic
[i
].a1
];
813 ic
[i
].a2
= nidx
[ic
[i
].a2
];
815 ic
[i
].a3
= nidx
[ic
[i
].a3
];
816 if (ic
[i
].op
& O_JXX
)
817 ic
[i
].a3
= nidx
[ic
[i
].a3
];
818 if (ic
[i
].op
& O_CALL
)
819 for (j
= 0; j
< ic
[i
].a3
; j
++)
820 ic
[i
].args
[j
] = nidx
[ic
[i
].args
[j
]];