From: Ulya Trofimovich Date: Tue, 19 May 2015 17:19:24 +0000 (+0100) Subject: Another part of tracking label usage moved out from codegen. X-Git-Tag: 0.15~257 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=350d5bf3b0ac5e56428f898e54326e186adab42b;p=re2c Another part of tracking label usage moved out from codegen. This is part of effort to reduce code generation to 'null device' (the same code is generated twice only to gather statistics on label usage). In order to reduce this evil pass, we should be able to track label usage before code generation. --- diff --git a/re2c/src/codegen/emit_action.cc b/re2c/src/codegen/emit_action.cc index b203e570..7990d33a 100644 --- a/re2c/src/codegen/emit_action.cc +++ b/re2c/src/codegen/emit_action.cc @@ -195,7 +195,6 @@ void emit_accept (OutputFile & o, uint32_t ind, bool & readCh, const State * con for (accept_t::const_iterator it = accept.begin(); it != accept.end(); ++it) { o << indent(ind) << "&&" << labelPrefix << it->second->label << ",\n"; - vUsedLabels.insert(it->second->label); } o << indent(--ind) << "};\n"; o << indent(ind) << "goto *" << mapCodeName["yytarget"] << "[" << mapCodeName["yyaccept"] << "];\n"; diff --git a/re2c/src/codegen/emit_dfa.cc b/re2c/src/codegen/emit_dfa.cc index da7f2a4c..f711b618 100644 --- a/re2c/src/codegen/emit_dfa.cc +++ b/re2c/src/codegen/emit_dfa.cc @@ -43,24 +43,26 @@ void genGoTo(OutputFile & o, uint32_t ind, const State *from, const State *to, b } o << indent(ind) << "goto " << labelPrefix << to->label << ";\n"; - vUsedLabels.insert(to->label); } void State::emit(Output & output, uint32_t ind, bool &readCh, const std::string& condName) const { OutputFile & o = output.source; - if (vUsedLabels.count(label)) + if (!DFlag) { - o << labelPrefix << label << ":\n"; - } - if (dFlag && (action.type != Action::INITIAL)) - { - o << indent(ind) << mapCodeName["YYDEBUG"] << "(" << label << ", " << input_api.expr_peek () << ");\n"; - } - if (isPreCtxt && !DFlag) - { - o << input_api.stmt_backupctx (ind); + if (vUsedLabels.count(label)) + { + o << labelPrefix << label << ":\n"; + } + if (dFlag && (action.type != Action::INITIAL)) + { + o << indent(ind) << mapCodeName["YYDEBUG"] << "(" << label << ", " << input_api.expr_peek () << ");\n"; + } + if (isPreCtxt) + { + o << input_api.stmt_backupctx (ind); + } } emit_action (action, o, ind, readCh, this, condName); } @@ -106,6 +108,10 @@ void DFA::emit(Output & output, uint32_t& ind, const RegExpMap* specMap, const s { s->go.used_labels (); } + for (accept_t::const_iterator it = accept_map.begin(); it != accept_map.end(); ++it) + { + vUsedLabels.insert(it->second->label); + } // Save 'next_fill_index' and compute information about code generation // while writing to null device. diff --git a/re2c/src/codegen/prepare_dfa.cc b/re2c/src/codegen/prepare_dfa.cc index 0e4ff54a..8e3cd3b2 100644 --- a/re2c/src/codegen/prepare_dfa.cc +++ b/re2c/src/codegen/prepare_dfa.cc @@ -179,7 +179,7 @@ void DFA::prepare(uint32_t & max_fill) } uint32_t nSaves = 0; - saves = new uint32_t[nRules]; + uint32_t * saves = new uint32_t[nRules]; memset(saves, ~0, (nRules)*sizeof(*saves)); // mark backtracking points @@ -206,8 +206,7 @@ void DFA::prepare(uint32_t & max_fill) } // insert actions - rules = new State * [nRules]; - + State ** rules = new State * [nRules]; memset(rules, 0, (nRules)*sizeof(*rules)); State *accept = NULL; @@ -251,8 +250,17 @@ void DFA::prepare(uint32_t & max_fill) if (accfixup) { - accfixup->action.set_accept (nRules, saves, rules); + for (uint32_t i = 0; i < nRules; ++i) + { + if (saves[i] != ~0u) + { + accept_map[saves[i]] = rules[i]; + } + } + accfixup->action.set_accept (&accept_map); } + delete [] saves; + delete [] rules; // split ``base'' states into two parts for (s = head; s; s = s->next) diff --git a/re2c/src/dfa/action.h b/re2c/src/dfa/action.h index 9271918b..8759d9bb 100644 --- a/re2c/src/dfa/action.h +++ b/re2c/src/dfa/action.h @@ -44,7 +44,7 @@ private: { Initial * initial; uint32_t save; - accept_t * accept; + const accept_t * accept; const RuleOp * rule; } info; @@ -74,18 +74,11 @@ public: clear (); type = MOVE; } - void set_accept (uint32_t rules_count, uint32_t * saves, State * const * rules) + void set_accept (const accept_t * accept) { clear (); type = ACCEPT; - info.accept = new accept_t (); - for (uint32_t i = 0; i < rules_count; ++i) - { - if (saves[i] != ~0u) - { - (* info.accept)[saves[i]] = rules[i]; - } - } + info.accept = accept; } void set_rule (const RuleOp * const rule) { @@ -103,12 +96,10 @@ private: case INITIAL: delete info.initial; break; - case ACCEPT: - delete info.accept; - break; case MATCH: case SAVE: case MOVE: + case ACCEPT: case RULE: break; } diff --git a/re2c/src/dfa/dfa.cc b/re2c/src/dfa/dfa.cc index 91778ff8..6110a55d 100644 --- a/re2c/src/dfa/dfa.cc +++ b/re2c/src/dfa/dfa.cc @@ -45,8 +45,7 @@ DFA::DFA(Ins *ins, uint32_t ni, uint32_t lb, uint32_t ub, const Char *rep) , free_ins(ins) , free_rep(rep) , bSaveOnHead (false) - , saves (NULL) - , rules (NULL) + , accept_map () { Ins **work = new Ins * [ni + 1]; @@ -139,8 +138,6 @@ DFA::~DFA() } delete [] free_ins; delete [] free_rep; - delete [] saves; - delete [] rules; } void DFA::addState(State **a, State *s) diff --git a/re2c/src/dfa/dfa.h b/re2c/src/dfa/dfa.h index c56da009..86325cb5 100644 --- a/re2c/src/dfa/dfa.h +++ b/re2c/src/dfa/dfa.h @@ -25,8 +25,7 @@ public: protected: bool bSaveOnHead; - uint32_t * saves; - State ** rules; + accept_t accept_map; public: DFA (Ins *, uint32_t, uint32_t, uint32_t, const Char *);