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 /* load constants as late as possible */
142 if (opt(1) && ic_const(r2
) && !ic_const(r1
)) {
143 ic_put(ic
[r2
].op
, ic
[r2
].a1
, ic
[r2
].a2
, ic
[r2
].a3
);
146 ic_put(op
, r2
, r1
, 0);
148 io_num() && io_mul2() && io_addr() && io_imm();
154 ic_put(op
, r1
, 0, 0);
156 io_num() && io_cmp();
159 void o_assign(long bt
)
163 /* load constants as late as possible */
164 if (opt(1) && (ic_const(lv
) || ic_load(lv
))) {
165 ic_put(ic
[lv
].op
, ic
[lv
].a1
, ic
[lv
].a2
, ic
[lv
].a3
);
168 ic_put(O_MK(O_ST
| O_NUM
, bt
), rv
, lv
, 0);
174 void o_deref(long bt
)
177 ic_put(O_MK(O_LD
| O_NUM
, bt
), r1
, 0, 0);
184 if (T_SZ(bt
) != ULNG
) {
186 ic_put(O_MK(O_MOV
, bt
), r1
, 0, 0);
197 ic_put(O_MCPY
, r0
, r1
, r2
);
205 ic_put(O_MSET
, r0
, r1
, r2
);
208 void o_call(int argc
, int ret
)
211 long *args
= malloc(argc
* sizeof(c
->args
[0]));
213 for (i
= argc
- 1; i
>= 0; --i
)
215 for (i
= argc
- 1; i
>= 0; --i
) {
217 /* load constants as late as possible */
218 if (opt(1) && (ic_const(iv
) || ic_load(iv
))) {
219 ic_put(ic
[iv
].op
, ic
[iv
].a1
, ic
[iv
].a2
, ic
[iv
].a3
);
224 c
= ic_put(O_CALL
, r1
, 0, argc
);
235 ic_put(O_RET
, iv_pop(), 0, 0);
238 void o_label(long id
)
240 while (id
>= lab_sz
) {
241 lab_sz
= MAX(128, lab_sz
* 2);
242 lab_loc
= mextend(lab_loc
, lab_n
, lab_sz
, sizeof(*lab_loc
));
245 lab_loc
[lab_n
++] = -1;
252 ic_put(O_JMP
, 0, 0, id
);
257 ic_put(O_JZ
, iv_pop(), 0, id
);
262 int o_popnum(long *n
)
264 if (ic_num(ic
, iv_get(0), n
))
270 int o_popsym(long *sym
, long *off
)
272 if (ic_sym(ic
, iv_get(0), sym
, off
))
283 void o_back(long mark
)
288 void ic_get(struct ic
**c
, long *n
)
291 if (!ic_n
|| ~ic
[ic_n
- 1].op
& O_RET
|| lab_last
== ic_n
)
293 for (i
= 0; i
< ic_n
; i
++) /* filling branch targets */
294 if (ic
[i
].op
& O_JXX
)
295 ic
[i
].a3
= lab_loc
[ic
[i
].a3
];
296 io_deadcode(); /* removing dead code */
310 void ic_free(struct ic
*ic
)
316 /* intermediate code queries */
318 static long cb(long op
, long a
, long b
)
340 return O_T(op
) & T_MSIGN
? a
>> b
: (unsigned long) a
>> b
;
357 static long cu(int op
, long i
)
370 static long c_cast(long n
, unsigned bt
)
372 if (!(bt
& T_MSIGN
) && T_SZ(bt
) != ULNG
)
373 n
&= ((1l << (long) (T_SZ(bt
) * 8)) - 1);
374 if (bt
& T_MSIGN
&& T_SZ(bt
) != ULNG
&&
375 n
> (1l << (T_SZ(bt
) * 8 - 1)))
376 n
= -((1l << (T_SZ(bt
) * 8)) - n
);
380 int ic_num(struct ic
*ic
, long iv
, long *n
)
383 long oc
= O_C(ic
[iv
].op
);
384 long bt
= O_T(ic
[iv
].op
);
385 if (oc
& O_MOV
&& oc
& O_NUM
) {
390 if (ic_num(ic
, ic
[iv
].a1
, &n1
))
392 if (ic_num(ic
, ic
[iv
].a2
, &n2
))
394 *n
= cb(ic
[iv
].op
, n1
, n2
);
398 if (ic_num(ic
, ic
[iv
].a1
, &n1
))
400 *n
= cu(ic
[iv
].op
, n1
);
403 if (oc
& O_MOV
&& !(oc
& (O_NUM
| O_LOC
| O_SYM
))) {
404 if (ic_num(ic
, ic
[iv
].a1
, &n1
))
412 int ic_sym(struct ic
*ic
, long iv
, long *sym
, long *off
)
415 long oc
= O_C(ic
[iv
].op
);
416 if (oc
& O_MOV
&& oc
& O_SYM
) {
422 if ((ic_sym(ic
, ic
[iv
].a1
, sym
, off
) ||
423 ic_num(ic
, ic
[iv
].a2
, &n
)) &&
424 (ic_sym(ic
, ic
[iv
].a2
, sym
, off
) ||
425 ic_num(ic
, ic
[iv
].a1
, &n
)))
431 if (ic_sym(ic
, ic
[iv
].a1
, sym
, off
) ||
432 ic_num(ic
, ic
[iv
].a2
, &n
))
440 static int ic_off(struct ic
*ic
, long iv
, long *base_iv
, long *off
)
443 long oc
= O_C(ic
[iv
].op
);
444 if (oc
== (O_ADD
| O_NUM
)) {
445 *base_iv
= ic
[iv
].a1
;
449 if (oc
== (O_SUB
| O_NUM
)) {
450 *base_iv
= ic
[iv
].a1
;
455 if ((ic_off(ic
, ic
[iv
].a1
, base_iv
, off
) ||
456 ic_num(ic
, ic
[iv
].a2
, &n
)) &&
457 (ic_off(ic
, ic
[iv
].a2
, base_iv
, off
) ||
458 ic_num(ic
, ic
[iv
].a1
, &n
)))
464 if (ic_off(ic
, ic
[iv
].a1
, base_iv
, off
) ||
465 ic_num(ic
, ic
[iv
].a2
, &n
))
475 /* number of register arguments */
476 int ic_regcnt(struct ic
*ic
)
480 return o
& (O_NUM
| O_SYM
| O_LOC
) ? 1 : 2;
482 return o
& (O_NUM
| O_SYM
| O_LOC
) ? 0 : 1;
484 return o
& (O_NUM
| O_SYM
| O_LOC
) ? 0 : 1;
486 return o
& (O_NUM
| O_SYM
| O_LOC
) ? 0 : 1;
494 return o
& (O_NUM
| O_SYM
| O_LOC
) ? 1 : 2;
498 return ((o
& O_NUM
) != 0);
500 return 1 + ((o
& O_NUM
) != 0);
505 * The returned array indicates the last instruction in
506 * which the value produced by each instruction is used.
508 long *ic_lastuse(struct ic
*ic
, long ic_n
)
510 long *luse
= calloc(ic_n
, sizeof(luse
[0]));
512 for (i
= ic_n
- 1; i
>= 0; --i
) {
513 int n
= ic_regcnt(ic
+ i
);
515 if (!(ic
[i
].op
& O_OUT
) || ic
[i
].op
& O_CALL
)
519 if (n
>= 1 && !luse
[ic
[i
].a1
])
521 if (n
>= 2 && !luse
[ic
[i
].a2
])
523 if (n
>= 3 && !luse
[ic
[i
].a3
])
525 if (ic
[i
].op
& O_CALL
)
526 for (j
= 0; j
< ic
[i
].a3
; j
++)
527 if (!luse
[ic
[i
].args
[j
]])
528 luse
[ic
[i
].args
[j
]] = i
;
533 /* intermediate code optimisations */
535 /* constant folding */
536 static int io_num(void)
539 if (!ic_num(ic
, iv_get(0), &n
)) {
547 static int log2a(unsigned long n
)
550 for (i
= 0; i
< LONGSZ
* 8; i
++)
553 if (i
== LONGSZ
* 8 || !(n
>> (i
+ 1)))
558 static long iv_num(long n
)
564 /* optimised multiplication operations for powers of two */
565 static int io_mul2(void)
570 long oc
= O_C(ic
[iv
].op
);
571 long bt
= O_T(ic
[iv
].op
);
574 if (oc
== O_MUL
&& !ic_num(ic
, ic
[iv
].a1
, &n
)) {
576 ic
[iv
].a1
= ic
[iv
].a2
;
579 if (ic_num(ic
, ic
[iv
].a2
, &n
))
595 ic_put(O_MK(O_SHL
, ULNG
), ic
[iv
].a1
, r2
, 0);
598 if (oc
== O_DIV
&& ~bt
& T_MSIGN
) {
605 ic_put(O_MK(O_SHR
, ULNG
), ic
[iv
].a1
, r2
, 0);
608 if (oc
== O_MOD
&& ~bt
& T_MSIGN
) {
614 r2
= iv_num(LONGSZ
* 8 - p
);
615 ic_put(O_MK(O_SHL
, ULNG
), ic
[iv
].a1
, r2
, 0);
617 r2
= iv_num(LONGSZ
* 8 - p
);
618 ic_put(O_MK(O_SHR
, ULNG
), r1
, r2
, 0);
624 /* optimise comparison */
625 static int io_cmp(void)
628 long cmp
= ic
[iv
].a1
;
629 if (O_C(ic
[iv
].op
) == O_LNOT
&& ic
[cmp
].op
& O_CMP
) {
638 /* optimise branch instructions after comparison */
639 static int io_jmp(void)
641 struct ic
*c
= &ic
[ic_n
- 1];
642 long oc
= O_C(c
->op
);
643 if (oc
& O_JZ
&& O_C(ic
[c
->a1
].op
) == O_LNOT
) {
644 c
->a1
= ic
[c
->a1
].a1
;
648 if (oc
& O_JZ
&& O_C(ic
[c
->a1
].op
) & O_CMP
) {
649 long cop
= (ic
[c
->a1
].op
& ~O_CMP
) | O_JCC
;
650 c
->op
= O_C(c
->op
) == O_JZ
? cop
^ 1 : cop
;
651 c
->a2
= ic
[c
->a1
].a2
;
652 c
->a1
= ic
[c
->a1
].a1
;
658 /* optimise accessing locals or symbols with an offset */
659 static int io_addr(void)
662 if (ic_off(ic
, iv_get(0), &iv
, &off
) || iv
== iv_get(0))
664 if (ic
[iv
].op
& O_MOV
&& ic
[iv
].op
& O_LOC
) {
666 ic_put(O_MOV
| O_LOC
, ic
[iv
].a1
, ic
[iv
].a2
+ off
, 0);
669 if (ic
[iv
].op
& O_MOV
&& ic
[iv
].op
& O_SYM
) {
671 ic_put(O_MOV
| O_SYM
, ic
[iv
].a1
, ic
[iv
].a2
+ off
, 0);
677 static int imm_ok(long op
, long n
, int arg
)
680 if (i_reg(op
| O_NUM
, m
+ 0, m
+ 1, m
+ 2, m
+ 3, m
+ 4))
682 return i_imm(m
[arg
], n
);
685 /* optimise loading and storing locals */
686 static int io_loc(void)
688 struct ic
*c
= &ic
[ic_n
- 1];
690 if (c
->op
& O_LD
&& c
->op
& O_NUM
) {
691 if (ic_off(ic
, c
->a1
, &iv
, &off
))
693 if (ic
[iv
].op
& O_MOV
&& ic
[iv
].op
& O_LOC
) {
694 c
->op
= (c
->op
& ~O_NUM
) | O_LOC
;
696 c
->a2
+= ic
[iv
].a2
+ off
;
699 if (imm_ok(c
->op
, off
, 2)) {
705 if (c
->op
& O_ST
&& c
->op
& O_NUM
) {
706 if (ic_off(ic
, c
->a2
, &iv
, &off
))
708 if (ic
[iv
].op
& O_MOV
&& ic
[iv
].op
& O_LOC
) {
709 c
->op
= (c
->op
& ~O_NUM
) | O_LOC
;
711 c
->a3
+= ic
[iv
].a2
+ off
;
714 if (imm_ok(c
->op
, off
, 3)) {
723 /* use instruction immediates */
724 static int io_imm(void)
726 struct ic
*c
= &ic
[ic_n
- 1];
727 long oc
= O_C(c
->op
);
729 if (oc
& (O_NUM
| O_LOC
| O_SYM
))
731 if (oc
== O_ADD
|| oc
== O_MUL
|| oc
== O_AND
|| oc
== O_OR
||
732 oc
== O_XOR
|| oc
== O_EQ
|| oc
== O_NE
) {
733 if (!ic_num(ic
, c
->a1
, &n
)) {
739 if (oc
== O_LT
|| oc
== O_GE
|| oc
== O_LE
|| oc
== O_GT
) {
740 if (!ic_num(ic
, c
->a1
, &n
)) {
747 if (oc
& O_JCC
&& !ic_num(ic
, c
->a1
, &n
)) {
753 if (oc
& O_JCC
&& !ic_num(ic
, c
->a2
, &n
) && imm_ok(c
->op
, n
, 1)) {
758 if (!(oc
& O_BOP
) || ic_num(ic
, c
->a2
, &n
))
760 if ((oc
== O_ADD
|| oc
== O_SUB
|| oc
& O_SHL
) && n
== 0) {
765 if (imm_ok(c
->op
, n
, 2)) {
773 /* calling symbols */
774 static int io_call(void)
776 struct ic
*c
= &ic
[ic_n
- 1];
778 if (c
->op
& O_CALL
&& !ic_sym(ic
, c
->a1
, &sym
, &off
) && !off
) {
786 /* remove dead code */
787 static void io_deadcode(void)
791 long src
= 0, dst
= 0;
793 /* liveness analysis */
794 live
= calloc(ic_n
, sizeof(live
[0]));
795 for (i
= ic_n
- 1; i
>= 0; i
--) {
796 int n
= ic_regcnt(ic
+ i
);
797 if (!(ic
[i
].op
& O_OUT
) || ic
[i
].op
& O_CALL
)
807 if (ic
[i
].op
& O_CALL
)
808 for (j
= 0; j
< ic
[i
].a3
; j
++)
809 live
[ic
[i
].args
[j
]] = 1;
811 /* the new indices of intermediate instructions */
812 nidx
= calloc(ic_n
, sizeof(nidx
[0]));
814 while (src
< ic_n
&& !live
[src
]) {
821 memcpy(ic
+ dst
, ic
+ src
, sizeof(ic
[src
]));
827 /* adjusting arguments and branch targets */
828 for (i
= 0; i
< ic_n
; i
++) {
829 int n
= ic_regcnt(ic
+ i
);
831 ic
[i
].a1
= nidx
[ic
[i
].a1
];
833 ic
[i
].a2
= nidx
[ic
[i
].a2
];
835 ic
[i
].a3
= nidx
[ic
[i
].a3
];
836 if (ic
[i
].op
& O_JXX
)
837 ic
[i
].a3
= nidx
[ic
[i
].a3
];
838 if (ic
[i
].op
& O_CALL
)
839 for (j
= 0; j
< ic
[i
].a3
; j
++)
840 ic
[i
].args
[j
] = nidx
[ic
[i
].args
[j
]];