History

Improve constant propagation with "&&" and "||".

Edmund Grimley Evans committed 10 years ago

Commit 3ff77a11 parent c7067ae3 files · +58 −52Browse files

Unified · Split

3 files
  1. TODO +0 −1
  2. tccgen.c +53 −50
  3. tests/tests2/78_vla_label.c +5 −1
TODO+0−1
@@ -68,7 +68,6 @@ Not critical: · 67 unmodified lines
6868 to suppress VT_LOCAL and use a base register instead).
6969 - interactive mode / integrated debugger
7070 - fix preprocessor symbol redefinition
71−- better constant opt (&&, ||, ?:)
7271 - add portable byte code generator and interpreter for other
7372 unsupported architectures.
7473 - C++: variable declaration in for, minimal 'class' support.
tccgen.c+53−50
@@ -4453,80 +4453,83 @@ static void expr_or(void) · 4452 unmodified lines
44534453 }
44544454 }
44554455
4456−/* XXX: fix this mess */
4457−static void expr_land_const(void)
4458−{
4459− expr_or();
4460− while (tok == TOK_LAND) {
4461− next();
4462− expr_or();
4463− gen_op(TOK_LAND);
4464− }
4465−}
4466−
4467−/* XXX: fix this mess */
4468−static void expr_lor_const(void)
4469−{
4470− expr_land_const();
4471− while (tok == TOK_LOR) {
4472− next();
4473− expr_land_const();
4474− gen_op(TOK_LOR);
4475− }
4476−}
4477−
4478−/* only used if non constant */
44794456 static void expr_land(void)
44804457 {
4481− int t;
4482−
44834458 expr_or();
44844459 if (tok == TOK_LAND) {
4485− t = 0;
4486− save_regs(1);
4487− for(;;) {
4488− t = gvtst(1, t);
4489− if (tok != TOK_LAND) {
4490− vseti(VT_JMPI, t);
4491− break;
4492− }
4460+ if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
4461+ CType ctb, cti;
4462+ ctb.t = VT_BOOL;
4463+ cti.t = VT_INT;
44934464 next();
4494− expr_or();
4465+ gen_cast(&ctb);
4466+ if (vtop->c.i) {
4467+ vpop();
4468+ expr_land();
4469+ gen_cast(&ctb);
4470+ } else {
4471+ expr_land();
4472+ vpop();
4473+ }
4474+ gen_cast(&cti);
4475+ } else {
4476+ int t = 0;
4477+ save_regs(1);
4478+ for(;;) {
4479+ t = gvtst(1, t);
4480+ if (tok != TOK_LAND) {
4481+ vseti(VT_JMPI, t);
4482+ break;
4483+ }
4484+ next();
4485+ expr_or();
4486+ }
44954487 }
44964488 }
44974489 }
44984490
44994491 static void expr_lor(void)
45004492 {
4501− int t;
4502−
45034493 expr_land();
45044494 if (tok == TOK_LOR) {
4505− t = 0;
4506− save_regs(1);
4507− for(;;) {
4508− t = gvtst(0, t);
4509− if (tok != TOK_LOR) {
4510− vseti(VT_JMP, t);
4511− break;
4512− }
4495+ if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
4496+ CType ctb, cti;
4497+ ctb.t = VT_BOOL;
4498+ cti.t = VT_INT;
45134499 next();
4514− expr_land();
4500+ gen_cast(&ctb);
4501+ if (vtop->c.i) {
4502+ expr_lor();
4503+ vpop();
4504+ } else {
4505+ vpop();
4506+ expr_lor();
4507+ gen_cast(&ctb);
4508+ }
4509+ gen_cast(&cti);
4510+ } else {
4511+ int t = 0;
4512+ save_regs(1);
4513+ for(;;) {
4514+ t = gvtst(0, t);
4515+ if (tok != TOK_LOR) {
4516+ vseti(VT_JMP, t);
4517+ break;
4518+ }
4519+ next();
4520+ expr_land();
4521+ }
45154522 }
45164523 }
45174524 }
45184525
4519−/* XXX: better constant handling */
45204526 static void expr_cond(void)
45214527 {
45224528 int tt, u, r1, r2, rc, t1, t2, bt1, bt2;
45234529 SValue sv;
45244530 CType type, type1, type2;
45254531
4526− if (const_wanted)
4527− expr_lor_const();
4528− else
4529− expr_lor();
4532+ expr_lor();
45304533 if (tok == '?') {
45314534 next();
45324535 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
tests/tests2/78_vla_label.c+5−1
@@ -17,9 +17,13 @@ void f2(void) · 16 unmodified lines
1717 {
1818 goto start;
1919 {
20− int a[1 ? 1 : 1]; /* not a variable-length array */
20+ int a[1 && 1]; /* not a variable-length array */
21+ int b[1 || 1]; /* not a variable-length array */
22+ int c[1 ? 1 : 1]; /* not a variable-length array */
2123 start:
2224 a[0] = 0;
25+ b[0] = 0;
26+ c[0] = 0;
2327 }
2428 }
2529

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t