From baab0aabb30f6f912547831e004e25abcdbbe2f7 Mon Sep 17 00:00:00 2001 From: nuno-lopes Date: Thu, 14 Aug 2008 16:55:27 +0000 Subject: [PATCH] generate slighlty better code when testing yyaccept --- re2c/code.cc | 22 +++++++++++++++++----- re2c/test/bug1479044.b.c | 4 ++-- re2c/test/bug1479044.c | 2 +- re2c/test/bug1479044.s.c | 4 ++-- re2c/test/bug1708378.ib.c | 4 ++-- re2c/test/c.c | 2 +- re2c/test/cmmap.c | 2 +- re2c/test/cnokw.c | 2 +- re2c/test/cunroll.c | 2 +- re2c/test/input8.b.c | 2 +- re2c/test/input8.c | 7 ++++--- re2c/test/input8.g.c | 2 +- re2c/test/modula.c | 2 +- re2c/test/parse_date.b.c | 22 +++++++++++----------- re2c/test/parse_date.c | 2 +- re2c/test/parse_date.db.c | 22 +++++++++++----------- re2c/test/parse_date.s.c | 22 +++++++++++----------- re2c/test/parse_date_1_78.db.c | 32 ++++++++++++++++---------------- re2c/test/repeater.c | 15 ++++++++------- re2c/test/scanner.c | 2 +- re2c/test/scanner.fs.c | 6 +++--- re2c/test/scanner.s.c | 6 +++--- re2c/test/yyaccept_missing.bci.c | 2 +- 23 files changed, 101 insertions(+), 87 deletions(-) diff --git a/re2c/code.cc b/re2c/code.cc index 4c81deed..c84f6f28 100644 --- a/re2c/code.cc +++ b/re2c/code.cc @@ -626,7 +626,7 @@ void Accept::emitBinary(std::ostream &o, uint ind, uint l, uint r, bool &readCh) uint m = (l + r) >> 1; assert(bUsedYYAccept); - o << indent(ind) << "if (" << mapCodeName["yyaccept"] << " <= " << m << ") {\n"; + o << indent(ind) << "if (" << mapCodeName["yyaccept"] << (r == l+1 ? " == " : " <= ") << m << ") {\n"; emitBinary(o, ++ind, l, m, readCh); o << indent(--ind) << "} else {\n"; emitBinary(o, ++ind, m + 1, r, readCh); @@ -671,7 +671,7 @@ void Accept::emit(std::ostream &o, uint ind, bool &readCh, const std::string&) c o << indent(ind) << "goto *" << mapCodeName["yytarget"] << "[" << mapCodeName["yyaccept"] << "];\n"; o << indent(--ind) << "}\n"; } - else if (sFlag) + else if (sFlag || mapRules.size() == 2) { emitBinary(o, ind, 0, mapRules.size() - 1, readCh); } @@ -686,11 +686,23 @@ void Accept::emit(std::ostream &o, uint ind, bool &readCh, const std::string&) c else { o << indent(ind) << "switch (" << mapCodeName["yyaccept"] << ") {\n"; + + RuleMap::const_iterator it = mapRules.begin(), end = mapRules.end(); - for (RuleMap::const_iterator it = mapRules.begin(); it != mapRules.end(); ++it) + while (it != end) { - o << indent(ind) << "case " << it->first << ": \t"; - genGoTo(o, 0, state, it->second, readCh); + RuleMap::const_iterator tmp = it; + + if (++it == end) + { + o << indent(ind) << "default:\t"; + } + else + { + o << indent(ind) << "case " << tmp->first << ": \t"; + } + + genGoTo(o, 0, state, tmp->second, readCh); } o << indent(ind) << "}\n"; diff --git a/re2c/test/bug1479044.b.c b/re2c/test/bug1479044.b.c index e15f97a6..ac013f49 100755 --- a/re2c/test/bug1479044.b.c +++ b/re2c/test/bug1479044.b.c @@ -224,13 +224,13 @@ yy18: yy19: YYCURSOR = YYMARKER; if (yyaccept <= 1) { - if (yyaccept <= 0) { + if (yyaccept == 0) { goto yy3; } else { goto yy63; } } else { - if (yyaccept <= 2) { + if (yyaccept == 2) { goto yy107; } else { goto yy132; diff --git a/re2c/test/bug1479044.c b/re2c/test/bug1479044.c index 915f2fed..4b88f5d0 100755 --- a/re2c/test/bug1479044.c +++ b/re2c/test/bug1479044.c @@ -181,7 +181,7 @@ yy19: case 0: goto yy3; case 1: goto yy63; case 2: goto yy107; - case 3: goto yy132; + default: goto yy132; } yy20: ++YYCURSOR; diff --git a/re2c/test/bug1479044.s.c b/re2c/test/bug1479044.s.c index 00a3b2b4..f264d250 100755 --- a/re2c/test/bug1479044.s.c +++ b/re2c/test/bug1479044.s.c @@ -155,13 +155,13 @@ yy18: yy19: YYCURSOR = YYMARKER; if (yyaccept <= 1) { - if (yyaccept <= 0) { + if (yyaccept == 0) { goto yy3; } else { goto yy63; } } else { - if (yyaccept <= 2) { + if (yyaccept == 2) { goto yy107; } else { goto yy132; diff --git a/re2c/test/bug1708378.ib.c b/re2c/test/bug1708378.ib.c index 77ff2650..416e5d9f 100755 --- a/re2c/test/bug1708378.ib.c +++ b/re2c/test/bug1708378.ib.c @@ -256,7 +256,7 @@ yy20: YYCURSOR = YYMARKER; if (yyaccept <= 2) { if (yyaccept <= 1) { - if (yyaccept <= 0) { + if (yyaccept == 0) { goto yy3; } else { goto yy46; @@ -265,7 +265,7 @@ yy20: goto yy738; } } else { - if (yyaccept <= 3) { + if (yyaccept == 3) { goto yy2116; } else { goto yy3187; diff --git a/re2c/test/c.c b/re2c/test/c.c index 3ef0bebe..304f7ca1 100644 --- a/re2c/test/c.c +++ b/re2c/test/c.c @@ -741,7 +741,7 @@ yy122: case 0: goto yy22; case 1: goto yy25; case 2: goto yy27; - case 3: goto yy134; + default: goto yy134; } yy123: ++YYCURSOR; diff --git a/re2c/test/cmmap.c b/re2c/test/cmmap.c index 9a78f8dc..93d51028 100644 --- a/re2c/test/cmmap.c +++ b/re2c/test/cmmap.c @@ -724,7 +724,7 @@ yy122: case 0: goto yy22; case 1: goto yy25; case 2: goto yy27; - case 3: goto yy134; + default: goto yy134; } yy123: ++YYCURSOR; diff --git a/re2c/test/cnokw.c b/re2c/test/cnokw.c index 849601b2..8dce4488 100644 --- a/re2c/test/cnokw.c +++ b/re2c/test/cnokw.c @@ -642,7 +642,7 @@ yy107: case 0: goto yy7; case 1: goto yy10; case 2: goto yy12; - case 3: goto yy119; + default: goto yy119; } yy108: ++YYCURSOR; diff --git a/re2c/test/cunroll.c b/re2c/test/cunroll.c index 486047aa..20e3e319 100644 --- a/re2c/test/cunroll.c +++ b/re2c/test/cunroll.c @@ -706,7 +706,7 @@ yy107: case 0: goto yy7; case 1: goto yy10; case 2: goto yy12; - case 3: goto yy119; + default: goto yy119; } yy108: ++YYCURSOR; diff --git a/re2c/test/input8.b.c b/re2c/test/input8.b.c index 3c6f2af7..70818baf 100755 --- a/re2c/test/input8.b.c +++ b/re2c/test/input8.b.c @@ -37,7 +37,7 @@ yy8: if (yych != '\n') goto yy11; yy9: YYCURSOR = YYMARKER; - if (yyaccept <= 0) { + if (yyaccept == 0) { goto yy3; } else { goto yy7; diff --git a/re2c/test/input8.c b/re2c/test/input8.c index b4e0bd7f..53570a6a 100755 --- a/re2c/test/input8.c +++ b/re2c/test/input8.c @@ -50,9 +50,10 @@ yy8: } yy9: YYCURSOR = YYMARKER; - switch (yyaccept) { - case 0: goto yy3; - case 1: goto yy7; + if (yyaccept == 0) { + goto yy3; + } else { + goto yy7; } yy10: yych = *++YYCURSOR; diff --git a/re2c/test/input8.g.c b/re2c/test/input8.g.c index 22ad7931..2b767cc8 100755 --- a/re2c/test/input8.g.c +++ b/re2c/test/input8.g.c @@ -37,7 +37,7 @@ yy8: if (yych != '\n') goto yy11; yy9: YYCURSOR = YYMARKER; - if (yyaccept <= 0) { + if (yyaccept == 0) { goto yy3; } else { goto yy7; diff --git a/re2c/test/modula.c b/re2c/test/modula.c index eea587a2..55fae6a7 100644 --- a/re2c/test/modula.c +++ b/re2c/test/modula.c @@ -4033,7 +4033,7 @@ yy270: case 1: goto yy8; case 2: goto yy276; case 3: goto yy294; - case 4: goto yy296; + default: goto yy296; } yy271: ++YYCURSOR; diff --git a/re2c/test/parse_date.b.c b/re2c/test/parse_date.b.c index 1e0ef84f..895b3d52 100755 --- a/re2c/test/parse_date.b.c +++ b/re2c/test/parse_date.b.c @@ -2181,13 +2181,13 @@ yy53: if (yyaccept <= 6) { if (yyaccept <= 3) { if (yyaccept <= 1) { - if (yyaccept <= 0) { + if (yyaccept == 0) { goto yy12; } else { goto yy3; } } else { - if (yyaccept <= 2) { + if (yyaccept == 2) { goto yy46; } else { goto yy69; @@ -2195,7 +2195,7 @@ yy53: } } else { if (yyaccept <= 5) { - if (yyaccept <= 4) { + if (yyaccept == 4) { goto yy172; } else { goto yy341; @@ -2207,13 +2207,13 @@ yy53: } else { if (yyaccept <= 10) { if (yyaccept <= 8) { - if (yyaccept <= 7) { + if (yyaccept == 7) { goto yy385; } else { goto yy420; } } else { - if (yyaccept <= 9) { + if (yyaccept == 9) { goto yy526; } else { goto yy620; @@ -2221,7 +2221,7 @@ yy53: } } else { if (yyaccept <= 12) { - if (yyaccept <= 11) { + if (yyaccept == 11) { goto yy634; } else { goto yy775; @@ -2235,13 +2235,13 @@ yy53: if (yyaccept <= 20) { if (yyaccept <= 17) { if (yyaccept <= 15) { - if (yyaccept <= 14) { + if (yyaccept == 14) { goto yy880; } else { goto yy916; } } else { - if (yyaccept <= 16) { + if (yyaccept == 16) { goto yy943; } else { goto yy889; @@ -2249,7 +2249,7 @@ yy53: } } else { if (yyaccept <= 19) { - if (yyaccept <= 18) { + if (yyaccept == 18) { goto yy1043; } else { goto yy937; @@ -2261,7 +2261,7 @@ yy53: } else { if (yyaccept <= 23) { if (yyaccept <= 22) { - if (yyaccept <= 21) { + if (yyaccept == 21) { goto yy1154; } else { goto yy1200; @@ -2271,7 +2271,7 @@ yy53: } } else { if (yyaccept <= 25) { - if (yyaccept <= 24) { + if (yyaccept == 24) { goto yy1279; } else { goto yy1287; diff --git a/re2c/test/parse_date.c b/re2c/test/parse_date.c index f999133c..8695cf02 100755 --- a/re2c/test/parse_date.c +++ b/re2c/test/parse_date.c @@ -3332,7 +3332,7 @@ yy53: case 23: goto yy1203; case 24: goto yy1279; case 25: goto yy1287; - case 26: goto yy1310; + default: goto yy1310; } yy54: ++YYCURSOR; diff --git a/re2c/test/parse_date.db.c b/re2c/test/parse_date.db.c index 7677d39e..4fb06190 100755 --- a/re2c/test/parse_date.db.c +++ b/re2c/test/parse_date.db.c @@ -2311,13 +2311,13 @@ yy53: if (yyaccept <= 6) { if (yyaccept <= 3) { if (yyaccept <= 1) { - if (yyaccept <= 0) { + if (yyaccept == 0) { goto yy3; } else { goto yy12; } } else { - if (yyaccept <= 2) { + if (yyaccept == 2) { goto yy46; } else { goto yy69; @@ -2325,7 +2325,7 @@ yy53: } } else { if (yyaccept <= 5) { - if (yyaccept <= 4) { + if (yyaccept == 4) { goto yy176; } else { goto yy405; @@ -2337,13 +2337,13 @@ yy53: } else { if (yyaccept <= 10) { if (yyaccept <= 8) { - if (yyaccept <= 7) { + if (yyaccept == 7) { goto yy448; } else { goto yy491; } } else { - if (yyaccept <= 9) { + if (yyaccept == 9) { goto yy601; } else { goto yy698; @@ -2351,7 +2351,7 @@ yy53: } } else { if (yyaccept <= 12) { - if (yyaccept <= 11) { + if (yyaccept == 11) { goto yy713; } else { goto yy860; @@ -2365,13 +2365,13 @@ yy53: if (yyaccept <= 20) { if (yyaccept <= 17) { if (yyaccept <= 15) { - if (yyaccept <= 14) { + if (yyaccept == 14) { goto yy971; } else { goto yy1005; } } else { - if (yyaccept <= 16) { + if (yyaccept == 16) { goto yy1032; } else { goto yy978; @@ -2379,7 +2379,7 @@ yy53: } } else { if (yyaccept <= 19) { - if (yyaccept <= 18) { + if (yyaccept == 18) { goto yy1134; } else { goto yy1026; @@ -2391,7 +2391,7 @@ yy53: } else { if (yyaccept <= 23) { if (yyaccept <= 22) { - if (yyaccept <= 21) { + if (yyaccept == 21) { goto yy1246; } else { goto yy1301; @@ -2401,7 +2401,7 @@ yy53: } } else { if (yyaccept <= 25) { - if (yyaccept <= 24) { + if (yyaccept == 24) { goto yy1382; } else { goto yy1390; diff --git a/re2c/test/parse_date.s.c b/re2c/test/parse_date.s.c index 2d7634ba..390626f0 100755 --- a/re2c/test/parse_date.s.c +++ b/re2c/test/parse_date.s.c @@ -2153,13 +2153,13 @@ yy53: if (yyaccept <= 6) { if (yyaccept <= 3) { if (yyaccept <= 1) { - if (yyaccept <= 0) { + if (yyaccept == 0) { goto yy12; } else { goto yy3; } } else { - if (yyaccept <= 2) { + if (yyaccept == 2) { goto yy46; } else { goto yy69; @@ -2167,7 +2167,7 @@ yy53: } } else { if (yyaccept <= 5) { - if (yyaccept <= 4) { + if (yyaccept == 4) { goto yy172; } else { goto yy341; @@ -2179,13 +2179,13 @@ yy53: } else { if (yyaccept <= 10) { if (yyaccept <= 8) { - if (yyaccept <= 7) { + if (yyaccept == 7) { goto yy385; } else { goto yy420; } } else { - if (yyaccept <= 9) { + if (yyaccept == 9) { goto yy526; } else { goto yy620; @@ -2193,7 +2193,7 @@ yy53: } } else { if (yyaccept <= 12) { - if (yyaccept <= 11) { + if (yyaccept == 11) { goto yy634; } else { goto yy775; @@ -2207,13 +2207,13 @@ yy53: if (yyaccept <= 20) { if (yyaccept <= 17) { if (yyaccept <= 15) { - if (yyaccept <= 14) { + if (yyaccept == 14) { goto yy880; } else { goto yy916; } } else { - if (yyaccept <= 16) { + if (yyaccept == 16) { goto yy943; } else { goto yy889; @@ -2221,7 +2221,7 @@ yy53: } } else { if (yyaccept <= 19) { - if (yyaccept <= 18) { + if (yyaccept == 18) { goto yy1043; } else { goto yy937; @@ -2233,7 +2233,7 @@ yy53: } else { if (yyaccept <= 23) { if (yyaccept <= 22) { - if (yyaccept <= 21) { + if (yyaccept == 21) { goto yy1154; } else { goto yy1200; @@ -2243,7 +2243,7 @@ yy53: } } else { if (yyaccept <= 25) { - if (yyaccept <= 24) { + if (yyaccept == 24) { goto yy1279; } else { goto yy1287; diff --git a/re2c/test/parse_date_1_78.db.c b/re2c/test/parse_date_1_78.db.c index 1d565e50..543a2101 100755 --- a/re2c/test/parse_date_1_78.db.c +++ b/re2c/test/parse_date_1_78.db.c @@ -2422,7 +2422,7 @@ yy54: if (yyaccept <= 4) { if (yyaccept <= 2) { if (yyaccept <= 1) { - if (yyaccept <= 0) { + if (yyaccept == 0) { goto yy3; } else { goto yy12; @@ -2431,7 +2431,7 @@ yy54: goto yy47; } } else { - if (yyaccept <= 3) { + if (yyaccept == 3) { goto yy70; } else { goto yy184; @@ -2439,13 +2439,13 @@ yy54: } } else { if (yyaccept <= 6) { - if (yyaccept <= 5) { + if (yyaccept == 5) { goto yy278; } else { goto yy288; } } else { - if (yyaccept <= 7) { + if (yyaccept == 7) { goto yy315; } else { goto yy320; @@ -2455,13 +2455,13 @@ yy54: } else { if (yyaccept <= 12) { if (yyaccept <= 10) { - if (yyaccept <= 9) { + if (yyaccept == 9) { goto yy344; } else { goto yy417; } } else { - if (yyaccept <= 11) { + if (yyaccept == 11) { goto yy573; } else { goto yy656; @@ -2469,13 +2469,13 @@ yy54: } } else { if (yyaccept <= 14) { - if (yyaccept <= 13) { + if (yyaccept == 13) { goto yy671; } else { goto yy787; } } else { - if (yyaccept <= 15) { + if (yyaccept == 15) { goto yy836; } else { goto yy846; @@ -2488,7 +2488,7 @@ yy54: if (yyaccept <= 21) { if (yyaccept <= 19) { if (yyaccept <= 18) { - if (yyaccept <= 17) { + if (yyaccept == 17) { goto yy937; } else { goto yy957; @@ -2497,7 +2497,7 @@ yy54: goto yy988; } } else { - if (yyaccept <= 20) { + if (yyaccept == 20) { goto yy995; } else { goto yy1022; @@ -2505,13 +2505,13 @@ yy54: } } else { if (yyaccept <= 23) { - if (yyaccept <= 22) { + if (yyaccept == 22) { goto yy967; } else { goto yy635; } } else { - if (yyaccept <= 24) { + if (yyaccept == 24) { goto yy1147; } else { goto yy1016; @@ -2521,13 +2521,13 @@ yy54: } else { if (yyaccept <= 29) { if (yyaccept <= 27) { - if (yyaccept <= 26) { + if (yyaccept == 26) { goto yy1241; } else { goto yy1249; } } else { - if (yyaccept <= 28) { + if (yyaccept == 28) { goto yy1278; } else { goto yy1364; @@ -2535,13 +2535,13 @@ yy54: } } else { if (yyaccept <= 31) { - if (yyaccept <= 30) { + if (yyaccept == 30) { goto yy1367; } else { goto yy1447; } } else { - if (yyaccept <= 32) { + if (yyaccept == 32) { goto yy1455; } else { goto yy1478; diff --git a/re2c/test/repeater.c b/re2c/test/repeater.c index 431be77d..c75cfa83 100644 --- a/re2c/test/repeater.c +++ b/re2c/test/repeater.c @@ -71,15 +71,16 @@ yy8: } yy9: YYCURSOR = YYMARKER; - switch (yyaccept) { - case 0: goto yy3; - case 1: goto yy5; + if (yyaccept == 0) { + goto yy3; + } else { + goto yy5; } yy10: ++YYCURSOR; #line 18 "repeater.re" {RET(1);} -#line 83 "" +#line 84 "" yy12: yych = *++YYCURSOR; switch (yych) { @@ -92,7 +93,7 @@ yy13: ++YYCURSOR; #line 19 "repeater.re" {RET(2);} -#line 96 "" +#line 97 "" yy15: yych = *++YYCURSOR; switch (yych) { @@ -113,7 +114,7 @@ yy17: ++YYCURSOR; #line 21 "repeater.re" {RET(4);} -#line 117 "" +#line 118 "" yy19: yych = *++YYCURSOR; switch (yych) { @@ -124,7 +125,7 @@ yy20: ++YYCURSOR; #line 20 "repeater.re" {RET(3);} -#line 128 "" +#line 129 "" yy22: ++YYCURSOR; if (YYLIMIT <= YYCURSOR) YYFILL(1); diff --git a/re2c/test/scanner.c b/re2c/test/scanner.c index bfae72cd..ac69d6b6 100644 --- a/re2c/test/scanner.c +++ b/re2c/test/scanner.c @@ -669,7 +669,7 @@ yy66: case 3: goto yy42; case 4: goto yy46; case 5: goto yy69; - case 6: goto yy98; + default: goto yy98; } yy67: yyaccept = 5; diff --git a/re2c/test/scanner.fs.c b/re2c/test/scanner.fs.c index d2f74d93..e741faf9 100755 --- a/re2c/test/scanner.fs.c +++ b/re2c/test/scanner.fs.c @@ -499,13 +499,13 @@ yy66: YYCURSOR = YYMARKER; if (yyaccept <= 3) { if (yyaccept <= 1) { - if (yyaccept <= 0) { + if (yyaccept == 0) { goto yy32; } else { goto yy38; } } else { - if (yyaccept <= 2) { + if (yyaccept == 2) { goto yy40; } else { goto yy42; @@ -513,7 +513,7 @@ yy66: } } else { if (yyaccept <= 5) { - if (yyaccept <= 4) { + if (yyaccept == 4) { goto yy46; } else { goto yy69; diff --git a/re2c/test/scanner.s.c b/re2c/test/scanner.s.c index 7231167e..0c5a9f85 100755 --- a/re2c/test/scanner.s.c +++ b/re2c/test/scanner.s.c @@ -454,13 +454,13 @@ yy66: YYCURSOR = YYMARKER; if (yyaccept <= 3) { if (yyaccept <= 1) { - if (yyaccept <= 0) { + if (yyaccept == 0) { goto yy32; } else { goto yy38; } } else { - if (yyaccept <= 2) { + if (yyaccept == 2) { goto yy40; } else { goto yy42; @@ -468,7 +468,7 @@ yy66: } } else { if (yyaccept <= 5) { - if (yyaccept <= 4) { + if (yyaccept == 4) { goto yy46; } else { goto yy69; diff --git a/re2c/test/yyaccept_missing.bci.c b/re2c/test/yyaccept_missing.bci.c index 2711e5a6..212c5fdf 100755 --- a/re2c/test/yyaccept_missing.bci.c +++ b/re2c/test/yyaccept_missing.bci.c @@ -124,7 +124,7 @@ yy12: if (yych <= 'z') goto yy16; yy13: YYCURSOR = YYMARKER; - if (yyaccept <= 0) { + if (yyaccept == 0) { goto yy11; } else { goto yy17; -- 2.40.0