]> granicus.if.org Git - re2c/commitdiff
Another part of tracking label usage moved out from codegen.
authorUlya Trofimovich <skvadrik@gmail.com>
Tue, 19 May 2015 17:19:24 +0000 (18:19 +0100)
committerUlya Trofimovich <skvadrik@gmail.com>
Tue, 19 May 2015 17:19:24 +0000 (18:19 +0100)
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.

re2c/src/codegen/emit_action.cc
re2c/src/codegen/emit_dfa.cc
re2c/src/codegen/prepare_dfa.cc
re2c/src/dfa/action.h
re2c/src/dfa/dfa.cc
re2c/src/dfa/dfa.h

index b203e570274a91a5f704e4cd7eeb6899d8ccfca3..7990d33aca3520493f7a1b1fe678794957462dd5 100644 (file)
@@ -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";
index da7f2a4cea60d796bb6b9c6d627c85a31a5bfa56..f711b618fa045cd7bc4622d2a2749097d6f3bcaf 100644 (file)
@@ -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.
index 0e4ff54a95fe644cfecfc88b693cbd772508f696..8e3cd3b22db32d8ee87781c532181bd6d7d5ae7e 100644 (file)
@@ -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)
index 9271918bb0bd9abfd87b9265de8f03871a24ad6b..8759d9bb79124c3af0d7026b6d3f2f4947cbf368 100644 (file)
@@ -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;
                }
index 91778ff826dfd7dee793f7de9d8ce90a5bf543ff..6110a55d16cff27f760c891379b3aa483fca37d8 100644 (file)
@@ -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)
index c56da009fcbab72904e2dca61bd7915ecdce06b9..86325cb54491f180f0ad1d4bd573e080cada4fd8 100644 (file)
@@ -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 *);