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 struct ic
*ic_new(void)
28 ic_sz
= MAX(128, ic_sz
* 2);
29 ic
= mextend(ic
, ic_n
, ic_sz
, sizeof(*ic
));
34 static struct ic
*ic_put(long op
, long arg0
, long arg1
, long arg2
)
36 struct ic
*c
= ic_new();
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 long iv_new(void)
69 static void iv_put(long n
)
74 static void iv_drop(int n
)
76 iv_n
= MAX(0, iv_n
- n
);
79 static void iv_swap(int x
, int y
)
81 long v
= iv
[iv_n
- x
- 1];
82 iv
[iv_n
- x
- 1] = iv
[iv_n
- y
- 1];
86 static void iv_dup(void)
88 iv
[iv_n
] = iv
[iv_n
- 1];
94 ic_put(O_MOV
| O_NUM
, iv_new(), n
, 0);
99 ic_put(O_MOV
| O_LOC
, iv_new(), id
, 0);
102 void o_sym(char *sym
)
104 ic_put(O_MOV
| O_SYM
, iv_new(), out_sym(sym
), 0);
107 void o_tmpdrop(int n
)
109 iv_drop(n
>= 0 ? n
: iv_n
);
126 ic_put(op
, iv_new(), r2
, r1
);
127 io_num() && io_mul2() && io_addr() && io_imm();
133 ic_put(op
, iv_new(), r1
, 0);
138 void o_assign(long bt
)
140 ic_put(O_MK(O_ST
| O_NUM
, bt
), iv_get(0), iv_get(1), 0);
146 void o_deref(long bt
)
149 ic_put(O_MK(O_LD
| O_NUM
, bt
), iv_new(), r1
, 0);
155 if (T_SZ(bt
) != ULNG
) {
157 ic_put(O_MK(O_MOV
, bt
), iv_new(), r1
, 0);
167 ic_put(O_MCPY
, r0
, r1
, r2
);
175 ic_put(O_MSET
, r0
, r1
, r2
);
178 void o_call(int argc
, int ret
)
181 long *args
= malloc(argc
* sizeof(c
->args
[0]));
183 for (i
= argc
- 1; i
>= 0; --i
)
186 c
= ic_put(O_CALL
, iv_new(), r1
, argc
);
196 ic_put(O_RET
, iv_pop(), 0, 0);
199 void o_label(long id
)
201 while (id
>= lab_sz
) {
202 lab_sz
= MAX(128, lab_sz
* 2);
203 lab_loc
= mextend(lab_loc
, lab_n
, lab_sz
, sizeof(*lab_loc
));
206 lab_loc
[lab_n
++] = -1;
213 ic_put(O_JMP
, 0, 0, id
);
218 ic_put(O_JZ
, iv_pop(), 0, id
);
222 int o_popnum(long *n
)
224 if (ic_num(ic
, iv_get(0), n
))
230 int o_popsym(long *sym
, long *off
)
232 if (ic_sym(ic
, iv_get(0), sym
, off
))
243 void o_back(long mark
)
248 void ic_get(struct ic
**c
, long *n
)
251 if (!ic_n
|| ~ic
[ic_n
- 1].op
& O_RET
|| lab_last
== ic_n
)
253 for (i
= 0; i
< ic_n
; i
++) /* filling branch targets */
254 if (ic
[i
].op
& O_JXX
)
255 ic
[i
].arg2
= lab_loc
[ic
[i
].arg2
];
256 io_deadcode(); /* removing dead code */
270 void ic_free(struct ic
*ic
)
276 /* intermediate code queries */
278 static long cb(long op
, long a
, long b
)
300 return O_T(op
) & T_MSIGN
? a
>> b
: (unsigned long) a
>> b
;
317 static long cu(int op
, long i
)
330 static long c_cast(long n
, unsigned bt
)
332 if (!(bt
& T_MSIGN
) && T_SZ(bt
) != ULNG
)
333 n
&= ((1l << (long) (T_SZ(bt
) * 8)) - 1);
334 if (bt
& T_MSIGN
&& T_SZ(bt
) != ULNG
&&
335 n
> (1l << (T_SZ(bt
) * 8 - 1)))
336 n
= -((1l << (T_SZ(bt
) * 8)) - n
);
340 int ic_num(struct ic
*ic
, long iv
, long *n
)
343 long oc
= O_C(ic
[iv
].op
);
344 long bt
= O_T(ic
[iv
].op
);
345 if (oc
& O_MOV
&& oc
& O_NUM
) {
350 if (ic_num(ic
, ic
[iv
].arg1
, &n1
))
352 if (ic_num(ic
, ic
[iv
].arg2
, &n2
))
354 *n
= cb(ic
[iv
].op
, n1
, n2
);
358 if (ic_num(ic
, ic
[iv
].arg1
, &n1
))
360 *n
= cu(ic
[iv
].op
, n1
);
363 if (oc
& O_MOV
&& !(oc
& (O_NUM
| O_LOC
| O_SYM
))) {
364 if (ic_num(ic
, ic
[iv
].arg1
, &n1
))
372 int ic_sym(struct ic
*ic
, long iv
, long *sym
, long *off
)
375 long oc
= O_C(ic
[iv
].op
);
376 if (oc
& O_MOV
&& oc
& O_SYM
) {
382 if ((ic_sym(ic
, ic
[iv
].arg1
, sym
, off
) ||
383 ic_num(ic
, ic
[iv
].arg2
, &n
)) &&
384 (ic_sym(ic
, ic
[iv
].arg2
, sym
, off
) ||
385 ic_num(ic
, ic
[iv
].arg1
, &n
)))
391 if (ic_sym(ic
, ic
[iv
].arg1
, sym
, off
) ||
392 ic_num(ic
, ic
[iv
].arg2
, &n
))
400 static int ic_off(struct ic
*ic
, long iv
, long *base_iv
, long *off
)
403 long oc
= O_C(ic
[iv
].op
);
404 if (oc
!= O_ADD
&& oc
!= O_SUB
) {
410 if ((ic_off(ic
, ic
[iv
].arg1
, base_iv
, off
) ||
411 ic_num(ic
, ic
[iv
].arg2
, &n
)) &&
412 (ic_off(ic
, ic
[iv
].arg2
, base_iv
, off
) ||
413 ic_num(ic
, ic
[iv
].arg1
, &n
)))
419 if (ic_off(ic
, ic
[iv
].arg1
, base_iv
, off
) ||
420 ic_num(ic
, ic
[iv
].arg2
, &n
))
428 /* number of register arguments */
429 int ic_regcnt(struct ic
*ic
)
433 return o
& (O_NUM
| O_SYM
| O_LOC
) ? 2 : 3;
435 return o
& (O_NUM
| O_SYM
| O_LOC
) ? 1 : 2;
437 return o
& (O_NUM
| O_SYM
| O_LOC
) ? 1 : 2;
439 return o
& (O_NUM
| O_SYM
| O_LOC
) ? 1 : 2;
447 return o
& (O_NUM
| O_SYM
| O_LOC
) ? 1 : 2;
450 if (o
& (O_LD
| O_ST
) && o
& (O_SYM
| O_LOC
))
452 if (o
& (O_LD
| O_ST
))
453 return o
& O_NUM
? 2 : 3;
457 /* return the values written to and read from in the given instruction */
458 static void ic_info(struct ic
*ic
, long **w
, long **r1
, long **r2
, long **r3
)
460 long n
= ic_regcnt(ic
);
461 long o
= ic
->op
& O_OUT
;
468 *r1
= n
>= 2 ? &ic
->arg1
: NULL
;
469 *r2
= n
>= 3 ? &ic
->arg2
: NULL
;
471 *r1
= n
>= 1 ? &ic
->arg0
: NULL
;
472 *r2
= n
>= 2 ? &ic
->arg1
: NULL
;
473 *r3
= n
>= 3 ? &ic
->arg2
: NULL
;
478 * The returned array indicates the last instruction in
479 * which the value produced by each instruction is used.
481 long *ic_lastuse(struct ic
*ic
, long ic_n
)
483 long *luse
= calloc(ic_n
, sizeof(luse
[0]));
485 for (i
= ic_n
- 1; i
>= 0; --i
) {
486 long *w
, *r1
, *r2
, *r3
;
487 ic_info(ic
+ i
, &w
, &r1
, &r2
, &r3
);
489 if (!w
|| ic
[i
].op
& O_CALL
)
493 if (r1
&& !luse
[*r1
])
495 if (r2
&& !luse
[*r2
])
497 if (r3
&& !luse
[*r3
])
499 if (ic
[i
].op
& O_CALL
)
500 for (j
= 0; j
< ic
[i
].arg2
; j
++)
501 if (!luse
[ic
[i
].args
[j
]])
502 luse
[ic
[i
].args
[j
]] = i
;
507 /* intermediate code optimisations */
509 /* constant folding */
510 static int io_num(void)
513 if (!ic_num(ic
, iv_get(0), &n
)) {
521 static int log2a(unsigned long n
)
524 for (i
= 0; i
< LONGSZ
* 8; i
++)
527 if (i
== LONGSZ
* 8 || !(n
>> (i
+ 1)))
532 static long iv_num(long n
)
538 /* optimised multiplication operations for powers of two */
539 static int io_mul2(void)
544 long oc
= O_C(ic
[iv
].op
);
545 long bt
= O_T(ic
[iv
].op
);
548 if (oc
== O_MUL
&& !ic_num(ic
, ic
[iv
].arg1
, &n
)) {
549 long t
= ic
[iv
].arg1
;
550 ic
[iv
].arg1
= ic
[iv
].arg2
;
553 if (ic_num(ic
, ic
[iv
].arg2
, &n
))
569 ic_put(O_MK(O_SHL
, ULNG
), iv_new(), ic
[iv
].arg1
, r2
);
572 if (oc
== O_DIV
&& ~bt
& T_MSIGN
) {
579 ic_put(O_MK(O_SHR
, ULNG
), iv_new(), ic
[iv
].arg1
, r2
);
582 if (oc
== O_MOD
&& ~bt
& T_MSIGN
) {
588 r2
= iv_num(LONGSZ
* 8 - p
);
589 ic_put(O_MK(O_SHL
, ULNG
), iv_new(), ic
[iv
].arg1
, r2
);
591 r2
= iv_num(LONGSZ
* 8 - p
);
592 ic_put(O_MK(O_SHR
, ULNG
), iv_new(), r1
, r2
);
598 /* optimise comparison */
599 static int io_cmp(void)
602 long cmp
= ic
[iv
].arg1
;
603 if (O_C(ic
[iv
].op
) == O_LNOT
&& ic
[cmp
].op
& O_CMP
) {
606 iv_put(ic
[cmp
].arg0
);
612 /* optimise branch instructions after comparison */
613 static int io_jmp(void)
615 struct ic
*c
= &ic
[ic_n
- 1];
616 long oc
= O_C(c
->op
);
617 if (oc
& O_JZ
&& O_C(ic
[c
->arg0
].op
) == O_LNOT
) {
618 c
->arg0
= ic
[c
->arg0
].arg1
;
622 if (oc
& O_JZ
&& O_C(ic
[c
->arg0
].op
) & O_CMP
) {
623 long cop
= (ic
[c
->arg0
].op
& ~O_CMP
) | O_JCC
;
624 c
->op
= O_C(c
->op
) == O_JZ
? cop
^ 1 : cop
;
625 c
->arg1
= ic
[c
->arg0
].arg2
;
626 c
->arg0
= ic
[c
->arg0
].arg1
;
632 /* optimise accessing locals or symbols with an offset */
633 static int io_addr(void)
636 if (ic_off(ic
, iv_get(0), &iv
, &off
) || iv
== iv_get(0))
638 if (ic
[iv
].op
& O_MOV
&& ic
[iv
].op
& O_LOC
) {
640 ic_put(O_MOV
| O_LOC
, iv_new(), ic
[iv
].arg1
, ic
[iv
].arg2
+ off
);
643 if (ic
[iv
].op
& O_MOV
&& ic
[iv
].op
& O_SYM
) {
645 ic_put(O_MOV
| O_SYM
, iv_new(), ic
[iv
].arg1
, ic
[iv
].arg2
+ off
);
651 /* optimise loading and storing locals */
652 static int io_loc(void)
654 struct ic
*c
= &ic
[ic_n
- 1];
656 if (!(c
->op
& (O_LD
| O_ST
)) || !(c
->op
& O_NUM
))
658 if (ic_off(ic
, c
->arg1
, &iv
, &off
))
660 if (ic
[iv
].op
& O_MOV
&& ic
[iv
].op
& O_LOC
) {
661 c
->op
= (c
->op
& ~O_NUM
) | O_LOC
;
662 c
->arg1
= ic
[iv
].arg1
;
663 c
->arg2
+= ic
[iv
].arg2
+ off
;
671 static int imm_ok(long op
, long n
, int arg
)
674 if (i_reg(op
| O_NUM
, &m0
, &m1
, &m2
, &mt
))
676 return i_imm(arg
== 2 ? m2
: m1
, n
);
679 /* use instruction immediates */
680 static int io_imm(void)
682 struct ic
*c
= &ic
[ic_n
- 1];
683 long oc
= O_C(c
->op
);
685 if (oc
& (O_NUM
| O_LOC
| O_SYM
))
687 if (oc
== O_ADD
|| oc
== O_MUL
|| oc
== O_AND
|| oc
== O_OR
||
688 oc
== O_XOR
|| oc
== O_EQ
|| oc
== O_NE
) {
689 if (!ic_num(ic
, c
->arg1
, &n
)) {
695 if (oc
== O_LT
|| oc
== O_GE
|| oc
== O_LE
|| oc
== O_GT
) {
696 if (!ic_num(ic
, c
->arg1
, &n
)) {
703 if (oc
& O_JCC
&& !ic_num(ic
, c
->arg0
, &n
)) {
709 if (oc
& O_JCC
&& !ic_num(ic
, c
->arg1
, &n
) && imm_ok(c
->op
, n
, 1)) {
714 if (!(oc
& O_BOP
) || ic_num(ic
, c
->arg2
, &n
))
716 if ((oc
== O_ADD
|| oc
== O_SUB
|| oc
& O_SHL
) && n
== 0) {
721 if (imm_ok(c
->op
, n
, 2)) {
729 /* calling symbols */
730 static int io_call(void)
732 struct ic
*c
= &ic
[ic_n
- 1];
734 if (c
->op
& O_CALL
&& !ic_sym(ic
, c
->arg1
, &sym
, &off
) && !off
) {
742 /* remove dead code */
743 static void io_deadcode(void)
747 long src
= 0, dst
= 0;
749 /* liveness analysis */
750 live
= calloc(ic_n
, sizeof(live
[0]));
751 for (i
= ic_n
- 1; i
>= 0; i
--) {
752 long *w
, *r1
, *r2
, *r3
;
753 ic_info(ic
+ i
, &w
, &r1
, &r2
, &r3
);
754 if (!w
|| ic
[i
].op
& O_CALL
)
764 if (ic
[i
].op
& O_CALL
)
765 for (j
= 0; j
< ic
[i
].arg2
; j
++)
766 live
[ic
[i
].args
[j
]] = 1;
768 /* the new indices of intermediate instructions */
769 nidx
= calloc(ic_n
, sizeof(nidx
[0]));
771 while (src
< ic_n
&& !live
[src
]) {
778 memcpy(ic
+ dst
, ic
+ src
, sizeof(ic
[src
]));
784 /* adjusting arguments and branch targets */
785 for (i
= 0; i
< ic_n
; i
++) {
786 long *w
, *r1
, *r2
, *r3
;
787 ic_info(ic
+ i
, &w
, &r1
, &r2
, &r3
);
796 if (ic
[i
].op
& O_JXX
)
797 ic
[i
].arg2
= nidx
[ic
[i
].arg2
];
798 if (ic
[i
].op
& O_CALL
)
799 for (j
= 0; j
< ic
[i
].arg2
; j
++)
800 ic
[i
].args
[j
] = nidx
[ic
[i
].args
[j
]];