From: Ulya Trofimovich Date: Tue, 10 Mar 2015 15:58:29 +0000 (+0000) Subject: Extracted common functionality in codegen -g and -b flags. X-Git-Tag: 0.15~362 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=35ec584b39d64b182139e46a3b165fe5261892d9;p=re2c Extracted common functionality in codegen -g and -b flags. --- diff --git a/re2c/code.cc b/re2c/code.cc index d0724018..83454a58 100644 --- a/re2c/code.cc +++ b/re2c/code.cc @@ -108,7 +108,7 @@ static std::string space(uint this_label) return std::string(std::max(1, nl - tl + 1), ' '); } - +/* void Go::compact() { // arrange so that adjacent spans have different targets @@ -127,7 +127,7 @@ void Go::compact() nSpans = i + 1; } - +*/ /* * Find all spans, that map to the given state. For each of them, * find upper adjacent span, that maps to another state (if such @@ -175,6 +175,7 @@ static void doGen(const Go *g, const State *s, uint *bm, uint f, uint m) } } +// All spans in g1 that lead to s1 are pairwise equal to that in g2 leading to s2 static bool matches(const Go *g1, const State *s1, const Go *g2, const State *s2) { Span *b1 = g1->span, *e1 = &b1[g1->nSpans]; @@ -970,31 +971,33 @@ void Go::genBase(OutputFile & o, uint ind, const State *from, const State *next, } } -void Go::genCpGoto(OutputFile & o, uint ind, const State *from, const State *next, bool &readCh) const +std::string Go::genGotoProlog(OutputFile & o, uint ind, const State *from, const State *next, bool &readCh) const { - std::string sYych; - - if (readCh) - { - sYych = "(" + input_api.expr_peek_save () + ")"; - } - else - { - sYych = mapCodeName["yych"]; - } - + std::string sYych = readCh + ? "(" + input_api.expr_peek_save () + ")" + : mapCodeName["yych"]; readCh = false; + if (encoding.szCodeUnit() > 1) { o << indent(ind) << "if (" << sYych <<" & ~0xFF) {\n"; - genBase(o, ind+1, from, next, readCh, 1); - o << indent(ind++) << "} else {\n"; + genBase(o, ind + 1, from, next, readCh, 1); + o << indent(ind) << "} else "; sYych = mapCodeName["yych"]; } else { - o << indent(ind++) << "{\n"; + o << indent(ind); } + + return sYych; +} + +void Go::genCpGoto(OutputFile & o, uint ind, const State *from, const State *next, bool &readCh) const +{ + const std::string sYych = genGotoProlog(o, ind, from, next, readCh); + o << "{\n"; + ++ind; o << indent(ind++) << "static void *" << mapCodeName["yytarget"] << "[256] = {\n"; o << indent(ind); @@ -1043,10 +1046,13 @@ void Go::genGoto(OutputFile & o, uint ind, const State *from, const State *next, dSpans = 0; for (uint i = 0; i < nSpans; ++i) { +// if (span[i].ub > 0x100) if (span[i].ub > 0xFF) { +//assert (span[i].ub == 0x100 || encoding.szCodeUnit() > 1); wSpans++; } +// if (span[i].ub <= 0x100 || (encoding.szCodeUnit() <= 1)) if (span[i].ub < 0x100 || (encoding.szCodeUnit() <= 1)) { lSpans++; @@ -1098,26 +1104,7 @@ void Go::genGoto(OutputFile & o, uint ind, const State *from, const State *next, Go go; go.span = new Span[nSpans]; go.unmap(this, to); - if (readCh) - { - sYych = "(" + input_api.expr_peek_save () + ")"; - } - else - { - sYych = mapCodeName["yych"]; - } - readCh = false; - if (encoding.szCodeUnit() > 1) - { - o << indent(ind) << "if (" << sYych << " & ~0xFF) {\n"; - sYych = mapCodeName["yych"]; - genBase(o, ind+1, from, next, readCh, 1); - o << indent(ind) << "} else "; - } - else - { - o << indent(ind); - } + const std::string sYych = genGotoProlog(o, ind, from, next, readCh); bUsedYYBitmap = true; o << "if (" << mapCodeName["yybm"] << "[" << b->i << "+" << sYych << "] & "; if (yybmHexTable) diff --git a/re2c/dfa.h b/re2c/dfa.h index e3fdb450..3ae6f42b 100644 --- a/re2c/dfa.h +++ b/re2c/dfa.h @@ -192,6 +192,7 @@ public: void genSwitchD(OutputFile &, const State *from) const; void genSwitch(OutputFile &, uint ind, const State *from, const State *next, bool &readCh, uint mask) const; void genCpGoto(OutputFile &, uint ind, const State *from, const State *next, bool &readCh) const; + std::string genGotoProlog(OutputFile & o, uint ind, const State *from, const State *next, bool &readCh) const; void compact(); void unmap(Go*, const State*); };