mirror of
https://github.com/mirror/tinycc.git
synced 2025-02-24 07:50:12 +08:00
Improve constant propagation with "&&" and "||".
This commit is contained in:
parent
c7067aeb84
commit
3ff77a1d6f
1
TODO
1
TODO
@ -68,7 +68,6 @@ Not critical:
|
|||||||
to suppress VT_LOCAL and use a base register instead).
|
to suppress VT_LOCAL and use a base register instead).
|
||||||
- interactive mode / integrated debugger
|
- interactive mode / integrated debugger
|
||||||
- fix preprocessor symbol redefinition
|
- fix preprocessor symbol redefinition
|
||||||
- better constant opt (&&, ||, ?:)
|
|
||||||
- add portable byte code generator and interpreter for other
|
- add portable byte code generator and interpreter for other
|
||||||
unsupported architectures.
|
unsupported architectures.
|
||||||
- C++: variable declaration in for, minimal 'class' support.
|
- C++: variable declaration in for, minimal 'class' support.
|
||||||
|
103
tccgen.c
103
tccgen.c
@ -4453,80 +4453,83 @@ static void expr_or(void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* XXX: fix this mess */
|
|
||||||
static void expr_land_const(void)
|
|
||||||
{
|
|
||||||
expr_or();
|
|
||||||
while (tok == TOK_LAND) {
|
|
||||||
next();
|
|
||||||
expr_or();
|
|
||||||
gen_op(TOK_LAND);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* XXX: fix this mess */
|
|
||||||
static void expr_lor_const(void)
|
|
||||||
{
|
|
||||||
expr_land_const();
|
|
||||||
while (tok == TOK_LOR) {
|
|
||||||
next();
|
|
||||||
expr_land_const();
|
|
||||||
gen_op(TOK_LOR);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* only used if non constant */
|
|
||||||
static void expr_land(void)
|
static void expr_land(void)
|
||||||
{
|
{
|
||||||
int t;
|
|
||||||
|
|
||||||
expr_or();
|
expr_or();
|
||||||
if (tok == TOK_LAND) {
|
if (tok == TOK_LAND) {
|
||||||
t = 0;
|
if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
|
||||||
save_regs(1);
|
CType ctb, cti;
|
||||||
for(;;) {
|
ctb.t = VT_BOOL;
|
||||||
t = gvtst(1, t);
|
cti.t = VT_INT;
|
||||||
if (tok != TOK_LAND) {
|
|
||||||
vseti(VT_JMPI, t);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
next();
|
next();
|
||||||
expr_or();
|
gen_cast(&ctb);
|
||||||
|
if (vtop->c.i) {
|
||||||
|
vpop();
|
||||||
|
expr_land();
|
||||||
|
gen_cast(&ctb);
|
||||||
|
} else {
|
||||||
|
expr_land();
|
||||||
|
vpop();
|
||||||
|
}
|
||||||
|
gen_cast(&cti);
|
||||||
|
} else {
|
||||||
|
int t = 0;
|
||||||
|
save_regs(1);
|
||||||
|
for(;;) {
|
||||||
|
t = gvtst(1, t);
|
||||||
|
if (tok != TOK_LAND) {
|
||||||
|
vseti(VT_JMPI, t);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
next();
|
||||||
|
expr_or();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void expr_lor(void)
|
static void expr_lor(void)
|
||||||
{
|
{
|
||||||
int t;
|
|
||||||
|
|
||||||
expr_land();
|
expr_land();
|
||||||
if (tok == TOK_LOR) {
|
if (tok == TOK_LOR) {
|
||||||
t = 0;
|
if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
|
||||||
save_regs(1);
|
CType ctb, cti;
|
||||||
for(;;) {
|
ctb.t = VT_BOOL;
|
||||||
t = gvtst(0, t);
|
cti.t = VT_INT;
|
||||||
if (tok != TOK_LOR) {
|
|
||||||
vseti(VT_JMP, t);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
next();
|
next();
|
||||||
expr_land();
|
gen_cast(&ctb);
|
||||||
|
if (vtop->c.i) {
|
||||||
|
expr_lor();
|
||||||
|
vpop();
|
||||||
|
} else {
|
||||||
|
vpop();
|
||||||
|
expr_lor();
|
||||||
|
gen_cast(&ctb);
|
||||||
|
}
|
||||||
|
gen_cast(&cti);
|
||||||
|
} else {
|
||||||
|
int t = 0;
|
||||||
|
save_regs(1);
|
||||||
|
for(;;) {
|
||||||
|
t = gvtst(0, t);
|
||||||
|
if (tok != TOK_LOR) {
|
||||||
|
vseti(VT_JMP, t);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
next();
|
||||||
|
expr_land();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* XXX: better constant handling */
|
|
||||||
static void expr_cond(void)
|
static void expr_cond(void)
|
||||||
{
|
{
|
||||||
int tt, u, r1, r2, rc, t1, t2, bt1, bt2;
|
int tt, u, r1, r2, rc, t1, t2, bt1, bt2;
|
||||||
SValue sv;
|
SValue sv;
|
||||||
CType type, type1, type2;
|
CType type, type1, type2;
|
||||||
|
|
||||||
if (const_wanted)
|
expr_lor();
|
||||||
expr_lor_const();
|
|
||||||
else
|
|
||||||
expr_lor();
|
|
||||||
if (tok == '?') {
|
if (tok == '?') {
|
||||||
next();
|
next();
|
||||||
if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
|
if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
|
||||||
|
@ -17,9 +17,13 @@ void f2(void)
|
|||||||
{
|
{
|
||||||
goto start;
|
goto start;
|
||||||
{
|
{
|
||||||
int a[1 ? 1 : 1]; /* not a variable-length array */
|
int a[1 && 1]; /* not a variable-length array */
|
||||||
|
int b[1 || 1]; /* not a variable-length array */
|
||||||
|
int c[1 ? 1 : 1]; /* not a variable-length array */
|
||||||
start:
|
start:
|
||||||
a[0] = 0;
|
a[0] = 0;
|
||||||
|
b[0] = 0;
|
||||||
|
c[0] = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user