From: helly Date: Sun, 6 May 2007 21:21:55 +0000 (+0000) Subject: - Use nested if's for -bf X-Git-Tag: 0.13.6~141 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=323ca48df80808d4901301e728c72346f9bd3efc;p=re2c - Use nested if's for -bf --- diff --git a/re2c/bootstrap/scanner.cc b/re2c/bootstrap/scanner.cc index 2173e805..2bb3d6d9 100644 --- a/re2c/bootstrap/scanner.cc +++ b/re2c/bootstrap/scanner.cc @@ -1,4 +1,4 @@ -/* Generated by re2c 0.13.0.dev on Sun May 6 21:32:27 2007 */ +/* Generated by re2c 0.13.0.dev on Sun May 6 23:11:01 2007 */ #line 1 "scanner.re" /* $Id$ */ #include @@ -316,7 +316,7 @@ yy35: #line 134 "scanner.re" { tok = pos = cursor; - genGetState(out, topIndent, 0); + genGetStateGoto(out, topIndent, 0); ignore_eoc = true; goto echo; } diff --git a/re2c/code.cc b/re2c/code.cc index ea147f77..4a05b454 100644 --- a/re2c/code.cc +++ b/re2c/code.cc @@ -68,15 +68,27 @@ static void genYYFill(std::ostream &o, uint ind, uint need) } } -static void genGetCondition(std::ostream &o) +static std::string genGetState() +{ + if (bUseYYGetStateNaked) + { + return mapCodeName["YYGETSTATE"]; + } + else + { + return mapCodeName["YYGETSTATE"] + "()"; + } +} + +static std::string genGetCondition() { if (bUseYYGetConditionNaked) { - o << mapCodeName["YYGETCONDITION"]; + return mapCodeName["YYGETCONDITION"]; } else { - o << mapCodeName["YYGETCONDITION"] << "()"; + return mapCodeName["YYGETCONDITION"] + "()"; } } @@ -1664,7 +1676,7 @@ void DFA::emit(std::ostream &o, uint& ind, const RegExpMap* specMap, const std:: if (bProlog) { genCondTable(o, ind, *specMap); - genGetState(o, ind, start_label); + genGetStateGoto(o, ind, start_label); if (cFlag) { if (vUsedLabels.count(start_label)) @@ -1732,7 +1744,32 @@ void DFA::emit(std::ostream &o, uint& ind, const RegExpMap* specMap, const std:: bUseStartLabel = false; } -void genGetState(std::ostream &o, uint& ind, uint start_label) +void genGetStateGotoSub(std::ostream &o, uint ind, uint start_label, int cMin, int cMax) +{ + if (cMin == cMax) + { + if (cMin == -1) + { + o << indent(ind) << "goto " << labelPrefix << start_label << ";\n"; + } + else + { + o << indent(ind) << "goto " << mapCodeName["yyFillLabel"] << cMin << ";\n"; + } + } + else + { + int cMid = cMin + ((cMax - cMin + 1) / 2); + + o << indent(ind) << "if (" << genGetState() << " < " << cMid << ") {\n"; + genGetStateGotoSub(o, ind + 1, start_label, cMin, cMid - 1); + o << indent(ind) << "} else {\n"; + genGetStateGotoSub(o, ind + 1, start_label, cMid, cMax); + o << indent(ind) << "}\n"; + } +} + +void genGetStateGoto(std::ostream &o, uint& ind, uint start_label) { if (fFlag && !bWroteGetState) { @@ -1749,11 +1786,7 @@ void genGetState(std::ostream &o, uint& ind, uint start_label) o << indent(--ind) << "};\n"; o << "\n"; - o << indent(ind) << "if(" << mapCodeName["YYGETSTATE"]; - if (!bUseYYGetStateNaked) - { - o << "()"; - } + o << indent(ind) << "if(" << genGetState(); if (bUseStateAbort) { o << " == -1) {"; @@ -1766,31 +1799,24 @@ void genGetState(std::ostream &o, uint& ind, uint start_label) o << " goto " << labelPrefix << start_label << ";\n"; if (bUseStateAbort) { - o << indent(--ind) << "} else if (" << mapCodeName["YYGETSTATE"]; - if (!bUseYYGetStateNaked) - { - o << "()"; - } - o << " < -1) {\n"; + o << indent(--ind) << "} else if (" << genGetState() << " < -1) {\n"; o << indent(++ind) << "abort();\n"; o << indent(--ind) << "}\n"; } - o << indent(ind) << "goto *" << mapCodeName["yystable"] << "[" << mapCodeName["YYGETSTATE"]; - if (!bUseYYGetStateNaked) + o << indent(ind) << "goto *" << mapCodeName["yystable"] << "[" << genGetState() << "];\n"; + } + else if (bFlag) + { + genGetStateGotoSub(o, ind, start_label, -1, last_fill_index-1); + if (bUseStateAbort) { - o << "()"; + o << indent(ind) << "abort();\n"; } - o << "];\n"; } else { - o << indent(ind) << "switch(" << mapCodeName["YYGETSTATE"]; - if (!bUseYYGetStateNaked) - { - o << "()"; - } - o << ") {\n"; + o << indent(ind) << "switch(" << genGetState() << ") {\n"; if (bUseStateAbort) { o << indent(ind) << "default: abort();\n"; @@ -1847,9 +1873,7 @@ static void genCondGotoSub(std::ostream &o, uint ind, RegExpIndices& vCondList, { uint cMid = cMin + ((cMax - cMin + 1) / 2); - o << indent(ind) << "if ("; - genGetCondition(o); - o << " < " << cMid << ") {\n"; + o << indent(ind) << "if (" << genGetCondition() << " < " << cMid << ") {\n"; genCondGotoSub(o, ind + 1, vCondList, cMin, cMid - 1); o << indent(ind) << "} else {\n"; genCondGotoSub(o, ind + 1, vCondList, cMid, cMax); @@ -1863,9 +1887,7 @@ void genCondGoto(std::ostream &o, uint ind, const RegExpMap& specMap) { if (gFlag) { - o << indent(ind) << "goto *" << mapCodeName["yyctable"] << "["; - genGetCondition(o); - o << "];\n"; + o << indent(ind) << "goto *" << mapCodeName["yyctable"] << "[" << genGetCondition() << "];\n"; } else { @@ -1881,9 +1903,7 @@ void genCondGoto(std::ostream &o, uint ind, const RegExpMap& specMap) } else { - o << indent(ind) << "switch("; - genGetCondition(o); - o << ") {\n"; + o << indent(ind) << "switch(" << genGetCondition() << ") {\n"; for(RegExpMap::const_iterator it = specMap.begin(); it != specMap.end(); ++it) { diff --git a/re2c/re.h b/re2c/re.h index c6b927b6..5852cf92 100644 --- a/re2c/re.h +++ b/re2c/re.h @@ -495,7 +495,7 @@ typedef std::vector RegExpIndices; typedef std::list RuleOpList; extern void genCode(std::ostream&, uint&, RegExp*, const RegExpMap*, const std::string&, bool); -extern void genGetState(std::ostream&, uint&, uint); +extern void genGetStateGoto(std::ostream&, uint&, uint); extern void genCondTable(std::ostream&, uint, const RegExpMap&); extern void genCondGoto(std::ostream&, uint, const RegExpMap&); extern void genTypes(std::string&, uint, const RegExpMap&); diff --git a/re2c/scanner.re b/re2c/scanner.re index 5e2ff816..b2063644 100644 --- a/re2c/scanner.re +++ b/re2c/scanner.re @@ -133,7 +133,7 @@ echo: } "/*!getstate:re2c" { tok = pos = cursor; - genGetState(out, topIndent, 0); + genGetStateGoto(out, topIndent, 0); ignore_eoc = true; goto echo; } diff --git a/re2c/test/condition_08.cbif.c b/re2c/test/condition_08.cbif.c index ccaae1a2..d460715f 100755 --- a/re2c/test/condition_08.cbif.c +++ b/re2c/test/condition_08.cbif.c @@ -74,12 +74,22 @@ void scan(Scanner *s) { - switch(s->state) { - default: goto yy0; - case 0: goto yyFillLabel0; - case 1: goto yyFillLabel1; - case 2: goto yyFillLabel2; - case 3: goto yyFillLabel3; + if (s->state < 1) { + if (s->state < 0) { + goto yy0; + } else { + goto yyFillLabel0; + } + } else { + if (s->state < 2) { + goto yyFillLabel1; + } else { + if (s->state < 3) { + goto yyFillLabel2; + } else { + goto yyFillLabel3; + } + } } yy0: if (s->cond < 2) { diff --git a/re2c/test/condition_09.cbif.c b/re2c/test/condition_09.cbif.c index 93a9d4ac..92975e87 100755 --- a/re2c/test/condition_09.cbif.c +++ b/re2c/test/condition_09.cbif.c @@ -78,12 +78,22 @@ void scan(Scanner *s) { s->tok = s->cur; - switch(s->state) { - default: goto yy0; - case 0: goto yyFillLabel0; - case 1: goto yyFillLabel1; - case 2: goto yyFillLabel2; - case 3: goto yyFillLabel3; + if (s->state < 1) { + if (s->state < 0) { + goto yy0; + } else { + goto yyFillLabel0; + } + } else { + if (s->state < 2) { + goto yyFillLabel1; + } else { + if (s->state < 3) { + goto yyFillLabel2; + } else { + goto yyFillLabel3; + } + } } for(;;) { diff --git a/re2c/test/push.fb.c b/re2c/test/push.fb.c index 61f1ec66..06271474 100755 --- a/re2c/test/push.fb.c +++ b/re2c/test/push.fb.c @@ -266,11 +266,18 @@ public: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }; - switch(YYGETSTATE()) { - default: goto yy0; - case 0: goto yyFillLabel0; - case 1: goto yyFillLabel1; - case 2: goto yyFillLabel2; + if (YYGETSTATE() < 1) { + if (YYGETSTATE() < 0) { + goto yy0; + } else { + goto yyFillLabel0; + } + } else { + if (YYGETSTATE() < 2) { + goto yyFillLabel1; + } else { + goto yyFillLabel2; + } } yy0: YYSETSTATE(0); @@ -359,7 +366,7 @@ yyFillLabel0: yy3: #line 246 "push.fb.re" { SEND(kIdentifier); } -#line 363 "" +#line 370 "" yy4: yych = *++YYCURSOR; if(yych == 'o') goto yy64; @@ -397,62 +404,62 @@ yy12: yy13: #line 247 "push.fb.re" { SEND(kDecimalConstant);} -#line 401 "" +#line 408 "" yy14: ++YYCURSOR; #line 249 "push.fb.re" { SEND(kEqual); } -#line 406 "" +#line 413 "" yy16: ++YYCURSOR; #line 250 "push.fb.re" { SEND(kLeftParen); } -#line 411 "" +#line 418 "" yy18: ++YYCURSOR; #line 251 "push.fb.re" { SEND(kRightParen); } -#line 416 "" +#line 423 "" yy20: ++YYCURSOR; #line 252 "push.fb.re" { SEND(kMinus); } -#line 421 "" +#line 428 "" yy22: ++YYCURSOR; #line 253 "push.fb.re" { SEND(kPlus); } -#line 426 "" +#line 433 "" yy24: ++YYCURSOR; #line 254 "push.fb.re" { SEND(kStar); } -#line 431 "" +#line 438 "" yy26: ++YYCURSOR; #line 255 "push.fb.re" { SEND(kSlash); } -#line 436 "" +#line 443 "" yy28: ++YYCURSOR; #line 257 "push.fb.re" { SKIP(); } -#line 441 "" +#line 448 "" yy30: ++YYCURSOR; #line 258 "push.fb.re" { SKIP(); } -#line 446 "" +#line 453 "" yy32: ++YYCURSOR; #line 259 "push.fb.re" { send(kEOF); return 1; } -#line 451 "" +#line 458 "" yy34: ++YYCURSOR; #line 260 "push.fb.re" { SEND(kUnknown); } -#line 456 "" +#line 463 "" yy36: ++YYCURSOR; YYSETSTATE(1); @@ -490,7 +497,7 @@ yy40: } #line 245 "push.fb.re" { SEND(kReturn); } -#line 494 "" +#line 501 "" yy46: yych = *++YYCURSOR; if(yych != 'i') goto yy39; @@ -504,7 +511,7 @@ yy46: } #line 244 "push.fb.re" { SEND(kWhile); } -#line 508 "" +#line 515 "" yy51: yych = *++YYCURSOR; if(yych != 'e') goto yy39; @@ -518,7 +525,7 @@ yy51: } #line 243 "push.fb.re" { SEND(kBreak); } -#line 522 "" +#line 529 "" yy56: yych = *++YYCURSOR; if(yych != 't') goto yy39; @@ -530,7 +537,7 @@ yy56: } #line 242 "push.fb.re" { SEND(kGoto); } -#line 534 "" +#line 541 "" yy60: yych = *++YYCURSOR; if(yych != 's') goto yy39; @@ -542,7 +549,7 @@ yy60: } #line 241 "push.fb.re" { SEND(kElse); } -#line 546 "" +#line 553 "" yy64: yych = *++YYCURSOR; if(yych != 'r') goto yy39; @@ -552,7 +559,7 @@ yy64: } #line 240 "push.fb.re" { SEND(kFor); } -#line 556 "" +#line 563 "" yy67: ++YYCURSOR; if(yybm[0+(yych = *YYCURSOR)] & 128) { @@ -560,7 +567,7 @@ yy67: } #line 239 "push.fb.re" { SEND(kIf); } -#line 564 "" +#line 571 "" } #line 261 "push.fb.re"