From 5a794d5bc3569bc74e32d2eb8894d05be6bd8769 Mon Sep 17 00:00:00 2001 From: Ulya Trofimovich Date: Tue, 12 May 2015 11:26:04 +0100 Subject: [PATCH] Moved 're2c::BitMap' methods to a separate source file. --- re2c/Makefile.am | 3 +- re2c/src/codegen/bitmap.cc | 131 ++++++++++++++++++++++++++ re2c/src/codegen/{code.h => bitmap.h} | 10 +- re2c/src/codegen/code.cc | 121 +----------------------- re2c/src/codegen/go.h | 7 +- re2c/src/codegen/go_construct.cc | 1 + re2c/src/codegen/go_emit.cc | 1 + 7 files changed, 145 insertions(+), 129 deletions(-) create mode 100644 re2c/src/codegen/bitmap.cc rename re2c/src/codegen/{code.h => bitmap.h} (86%) diff --git a/re2c/Makefile.am b/re2c/Makefile.am index 1a2c5a98..382faa57 100644 --- a/re2c/Makefile.am +++ b/re2c/Makefile.am @@ -27,7 +27,7 @@ BOOTSTRAP_DOC_MAN = $(srcdir)/bootstrap/re2c.1 BOOTSTRAP_DOC_HTML = $(srcdir)/bootstrap/manual.html SRC_HDR = \ - $(srcdir)/src/codegen/code.h \ + $(srcdir)/src/codegen/bitmap.h \ $(srcdir)/src/codegen/code_names.h \ $(srcdir)/src/codegen/go.h \ $(srcdir)/src/codegen/indent.h \ @@ -60,6 +60,7 @@ SRC_HDR = \ SRC = \ $(SRC_SCANNER) \ + $(srcdir)/src/codegen/bitmap.cc \ $(srcdir)/src/codegen/code.cc \ $(srcdir)/src/codegen/code_names.cc \ $(srcdir)/src/codegen/go_construct.cc \ diff --git a/re2c/src/codegen/bitmap.cc b/re2c/src/codegen/bitmap.cc new file mode 100644 index 00000000..b66b1c0b --- /dev/null +++ b/re2c/src/codegen/bitmap.cc @@ -0,0 +1,131 @@ +#include // memset + +#include "src/codegen/bitmap.h" +#include "src/codegen/go.h" +#include "src/codegen/indent.h" +#include "src/globals.h" + +namespace re2c +{ + +BitMap *BitMap::first = NULL; + +BitMap::BitMap(const Go *g, const State *x) + : go(g) + , on(x) + , next(first) + , i(0) + , m(0) +{ + first = this; +} + +BitMap::~BitMap() +{ + delete next; +} + +const BitMap *BitMap::find(const Go *g, const State *x) +{ + for (const BitMap *b = first; b; b = b->next) + { + if (matches(b->go->span, b->go->nSpans, b->on, g->span, g->nSpans, x)) + { + return b; + } + } + + return new BitMap(g, x); +} + +const BitMap *BitMap::find(const State *x) +{ + for (const BitMap *b = first; b; b = b->next) + { + if (b->on == x) + { + return b; + } + } + + return NULL; +} + +static void doGen(const Go *g, const State *s, uint32_t *bm, uint32_t f, uint32_t m) +{ + Span *b = g->span, *e = &b[g->nSpans]; + uint32_t lb = 0; + + for (; b < e; ++b) + { + if (b->to == s) + { + for (; lb < b->ub && lb < 256; ++lb) + { + bm[lb-f] |= m; + } + } + + lb = b->ub; + } +} + +void BitMap::gen(OutputFile & o, uint32_t ind, uint32_t lb, uint32_t ub) +{ + if (first && bUsedYYBitmap) + { + o << indent(ind) << "static const unsigned char " << mapCodeName["yybm"] << "[] = {"; + + uint32_t c = 1, n = ub - lb; + const BitMap *cb = first; + + while((cb = cb->next) != NULL) { + ++c; + } + BitMap *b = first; + + uint32_t *bm = new uint32_t[n]; + + for (uint32_t i = 0, t = 1; b; i += n, t += 8) + { + memset(bm, 0, n * sizeof(uint32_t)); + + for (uint32_t m = 0x80; b && m; m >>= 1) + { + b->i = i; + b->m = m; + doGen(b->go, b->on, bm, lb, m); + b = const_cast(b->next); + } + + if (c > 8) + { + o << "\n" << indent(ind+1) << "/* table " << t << " .. " << std::min(c, t+7) << ": " << i << " */"; + } + + for (uint32_t j = 0; j < n; ++j) + { + if (j % 8 == 0) + { + o << "\n" << indent(ind+1); + } + + if (yybmHexTable) + { + o.write_hex (bm[j]); + } + else + { + o.write_uint32_t_width (bm[j], 3); + } + o << ", "; + } + } + + o << "\n" << indent(ind) << "};\n"; + + delete[] bm; + } +} + +} // end namespace re2c diff --git a/re2c/src/codegen/code.h b/re2c/src/codegen/bitmap.h similarity index 86% rename from re2c/src/codegen/code.h rename to re2c/src/codegen/bitmap.h index d75c0e4a..32ce85cb 100644 --- a/re2c/src/codegen/code.h +++ b/re2c/src/codegen/bitmap.h @@ -1,8 +1,8 @@ -/* $Id$ */ -#ifndef _code_h -#define _code_h +#ifndef _bitmap_h +#define _bitmap_h -#include "src/dfa/re.h" +#include "src/codegen/output.h" +#include "src/util/c99_stdint.h" namespace re2c { @@ -51,4 +51,4 @@ public: } // end namespace re2c -#endif +#endif // _bitmap_h diff --git a/re2c/src/codegen/code.cc b/re2c/src/codegen/code.cc index f3d3d6c1..50ddcb3e 100644 --- a/re2c/src/codegen/code.cc +++ b/re2c/src/codegen/code.cc @@ -1,5 +1,6 @@ #include +#include "src/codegen/bitmap.h" #include "src/codegen/go.h" #include "src/codegen/indent.h" #include "src/codegen/input_api.h" @@ -87,126 +88,6 @@ static void genSetCondition(OutputFile & o, uint32_t ind, const std::string& new } } -static void doGen(const Go *g, const State *s, uint32_t *bm, uint32_t f, uint32_t m) -{ - Span *b = g->span, *e = &b[g->nSpans]; - uint32_t lb = 0; - - for (; b < e; ++b) - { - if (b->to == s) - { - for (; lb < b->ub && lb < 256; ++lb) - { - bm[lb-f] |= m; - } - } - - lb = b->ub; - } -} - -BitMap *BitMap::first = NULL; - -BitMap::BitMap(const Go *g, const State *x) - : go(g) - , on(x) - , next(first) - , i(0) - , m(0) -{ - first = this; -} - -BitMap::~BitMap() -{ - delete next; -} - -const BitMap *BitMap::find(const Go *g, const State *x) -{ - for (const BitMap *b = first; b; b = b->next) - { - if (matches(b->go->span, b->go->nSpans, b->on, g->span, g->nSpans, x)) - { - return b; - } - } - - return new BitMap(g, x); -} - -const BitMap *BitMap::find(const State *x) -{ - for (const BitMap *b = first; b; b = b->next) - { - if (b->on == x) - { - return b; - } - } - - return NULL; -} - -void BitMap::gen(OutputFile & o, uint32_t ind, uint32_t lb, uint32_t ub) -{ - if (first && bUsedYYBitmap) - { - o << indent(ind) << "static const unsigned char " << mapCodeName["yybm"] << "[] = {"; - - uint32_t c = 1, n = ub - lb; - const BitMap *cb = first; - - while((cb = cb->next) != NULL) { - ++c; - } - BitMap *b = first; - - uint32_t *bm = new uint32_t[n]; - - for (uint32_t i = 0, t = 1; b; i += n, t += 8) - { - memset(bm, 0, n * sizeof(uint32_t)); - - for (uint32_t m = 0x80; b && m; m >>= 1) - { - b->i = i; - b->m = m; - doGen(b->go, b->on, bm, lb, m); - b = const_cast(b->next); - } - - if (c > 8) - { - o << "\n" << indent(ind+1) << "/* table " << t << " .. " << std::min(c, t+7) << ": " << i << " */"; - } - - for (uint32_t j = 0; j < n; ++j) - { - if (j % 8 == 0) - { - o << "\n" << indent(ind+1); - } - - if (yybmHexTable) - { - o.write_hex (bm[j]); - } - else - { - o.write_uint32_t_width (bm[j], 3); - } - o << ", "; - } - } - - o << "\n" << indent(ind) << "};\n"; - - delete[] bm; - } -} - static void genGoTo(OutputFile & o, uint32_t ind, const State *from, const State *to, bool & readCh) { if (DFlag) diff --git a/re2c/src/codegen/go.h b/re2c/src/codegen/go.h index a34a59f4..95716390 100644 --- a/re2c/src/codegen/go.h +++ b/re2c/src/codegen/go.h @@ -4,14 +4,15 @@ #include #include -#include "src/codegen/code.h" +#include "src/codegen/output.h" #include "src/util/c99_stdint.h" namespace re2c { -class State; // forward -struct If; // forward +class BitMap; +class State; +struct If; struct Span { diff --git a/re2c/src/codegen/go_construct.cc b/re2c/src/codegen/go_construct.cc index 43f91f2a..7027e533 100644 --- a/re2c/src/codegen/go_construct.cc +++ b/re2c/src/codegen/go_construct.cc @@ -1,3 +1,4 @@ +#include "src/codegen/bitmap.h" #include "src/codegen/go.h" #include "src/dfa/dfa.h" diff --git a/re2c/src/codegen/go_emit.cc b/re2c/src/codegen/go_emit.cc index f03e6985..9e0c50a1 100644 --- a/re2c/src/codegen/go_emit.cc +++ b/re2c/src/codegen/go_emit.cc @@ -1,3 +1,4 @@ +#include "src/codegen/bitmap.h" #include "src/codegen/go.h" #include "src/codegen/indent.h" #include "src/codegen/print.h" -- 2.40.0