From 9ab3663e89d26db7e2593e0f4b2e6ac6bb49c48a Mon Sep 17 00:00:00 2001 From: Ulya Trofimovich Date: Mon, 6 Aug 2018 22:37:38 +0100 Subject: [PATCH] Pack tag index and sign into one 32-bit field. --- re2c/src/dfa/closure.cc | 6 ++---- re2c/src/dfa/tagtree.cc | 8 ++++---- re2c/src/dfa/tagtree.h | 5 ++--- re2c/src/nfa/dump.cc | 4 ++-- re2c/src/nfa/nfa.h | 8 +++----- re2c/src/nfa/re_to_nfa.cc | 2 +- re2c/src/re/re.h | 12 +++++------- re2c/src/re/tag.h | 7 +++++++ 8 files changed, 26 insertions(+), 26 deletions(-) diff --git a/re2c/src/dfa/closure.cc b/re2c/src/dfa/closure.cc index d0f21e14..df498f73 100644 --- a/re2c/src/dfa/closure.cc +++ b/re2c/src/dfa/closure.cc @@ -159,8 +159,7 @@ static nfa_state_t *explore(nfa_state_t *q, closure_t &done, case nfa_state_t::TAG: if (q->arcidx == 0) { x.state = q->tag.out; - x.tlook = tagpool.history.push(x.tlook, q->tag.info, - q->tag.bottom ? TAGVER_BOTTOM : TAGVER_CURSOR); + x.tlook = tagpool.history.push(x.tlook, q->tag.info); p = relax(x, done, shadow, tagpool, tags); ++q->arcidx; } @@ -339,8 +338,7 @@ void closure_leftmost(const closure_t &init, closure_t &done, break; case nfa_state_t::TAG: x.state = n->tag.out; - x.tlook = tagpool.history.push(x.tlook, n->tag.info, - n->tag.bottom ? TAGVER_BOTTOM : TAGVER_CURSOR); + x.tlook = tagpool.history.push(x.tlook, n->tag.info); todo.push(x); break; case nfa_state_t::RAN: diff --git a/re2c/src/dfa/tagtree.cc b/re2c/src/dfa/tagtree.cc index a5966a18..caa86cb2 100644 --- a/re2c/src/dfa/tagtree.cc +++ b/re2c/src/dfa/tagtree.cc @@ -10,15 +10,15 @@ static const tagver_t DELIM = TAGVER_CURSOR - 1; tagtree_t::tagtree_t(): nodes(), path1(), path2() {} -tagver_t tagtree_t::elem(hidx_t i) const { return nodes[i].elem; } +tagver_t tagtree_t::elem(hidx_t i) const { return nodes[i].info.neg ? TAGVER_BOTTOM : TAGVER_CURSOR; } hidx_t tagtree_t::pred(hidx_t i) const { return nodes[i].pred; } -size_t tagtree_t::tag(hidx_t i) const { return nodes[i].tag; } +size_t tagtree_t::tag(hidx_t i) const { return nodes[i].info.idx; } -hidx_t tagtree_t::push(hidx_t i, size_t t, tagver_t v) +hidx_t tagtree_t::push(hidx_t idx, tag_info_t info) { - node_t x = {i, v, t}; + node_t x = {idx, info}; nodes.push_back(x); return static_cast(nodes.size() - 1); } diff --git a/re2c/src/dfa/tagtree.h b/re2c/src/dfa/tagtree.h index 7f3bf4bb..66b0b16a 100644 --- a/re2c/src/dfa/tagtree.h +++ b/re2c/src/dfa/tagtree.h @@ -22,8 +22,7 @@ struct tagtree_t // (a bunch of separate subtrees for each tag with common root) struct node_t { hidx_t pred; - tagver_t elem; - size_t tag; + tag_info_t info; }; std::vector nodes; @@ -35,7 +34,7 @@ struct tagtree_t hidx_t pred(hidx_t i) const; tagver_t elem(hidx_t i) const; size_t tag(hidx_t i) const; - hidx_t push(hidx_t i, size_t t, tagver_t v); + hidx_t push(hidx_t idx, tag_info_t info); int32_t compare_plain(hidx_t x, hidx_t y, size_t t); int32_t compare_histories(hidx_t x, hidx_t y, tagver_t ox, tagver_t oy, size_t t); int32_t compare_last_subhistories(hidx_t x, hidx_t y, tagver_t ox, tagver_t oy, size_t t); diff --git a/re2c/src/nfa/dump.cc b/re2c/src/nfa/dump.cc index 3af97d3f..8516b2fa 100644 --- a/re2c/src/nfa/dump.cc +++ b/re2c/src/nfa/dump.cc @@ -51,14 +51,14 @@ void dump_nfa(const nfa_t &nfa) break; } case nfa_state_t::TAG: { - const Tag &tag = nfa.tags[n->tag.info]; + const Tag &tag = nfa.tags[n->tag.info.idx]; fprintf(stderr, " n%u -> n%u [label=\"/", i, index(nfa, n->tag.out)); if (capture(tag)) { fprintf(stderr, "%u", (uint32_t)tag.ncap); } else if (!trailing(tag)) { fprintf(stderr, "%s", tag.name->c_str()); } - if (n->tag.bottom) { + if (n->tag.info.neg) { fprintf(stderr, "↓"); } else { fprintf(stderr, "↑"); diff --git a/re2c/src/nfa/nfa.h b/re2c/src/nfa/nfa.h index 7951255b..880f32fb 100644 --- a/re2c/src/nfa/nfa.h +++ b/re2c/src/nfa/nfa.h @@ -40,8 +40,7 @@ struct nfa_state_t struct { nfa_state_t *out; - size_t info; - bool bottom; + tag_info_t info; } tag; struct { @@ -81,12 +80,11 @@ struct nfa_state_t ran.ran = p; init(r); } - void make_tag(size_t r, nfa_state_t *s, size_t i, bool bottom) + void make_tag(size_t r, nfa_state_t *s, tag_info_t info) { type = TAG; tag.out = s; - tag.info = i; - tag.bottom = bottom; + tag.info = info; init(r); } void make_fin(size_t r) diff --git a/re2c/src/nfa/re_to_nfa.cc b/re2c/src/nfa/re_to_nfa.cc index 0153a16b..81e81726 100644 --- a/re2c/src/nfa/re_to_nfa.cc +++ b/re2c/src/nfa/re_to_nfa.cc @@ -81,7 +81,7 @@ static nfa_state_t *re_to_nfa(nfa_t &nfa, size_t nrule, const RE *re, nfa_state_ s = t; } else { s = &nfa.states[nfa.size++]; - s->make_tag(nrule, t, re->tag.idx, re->tag.bottom); + s->make_tag(nrule, t, re->tag); } break; } diff --git a/re2c/src/re/re.h b/re2c/src/re/re.h index 623f78f0..c30cd0ff 100644 --- a/re2c/src/re/re.h +++ b/re2c/src/re/re.h @@ -32,10 +32,7 @@ struct RE uint32_t min; uint32_t max; } iter; - struct { - size_t idx; - bool bottom; - } tag; + tag_info_t tag; }; }; @@ -110,12 +107,13 @@ inline RE *re_iter(RE::alc_t &alc, RE *x, uint32_t n, uint32_t m) return y; } -inline RE *re_tag(RE::alc_t &alc, size_t i, bool b) +inline RE *re_tag(RE::alc_t &alc, size_t idx, bool neg) { RE *x = alc.alloct(1); x->type = RE::TAG; - x->tag.idx = i; - x->tag.bottom = b; + x->tag.idx = idx & 0x7FFFffff; + assert(idx == x->tag.idx); + x->tag.neg = neg; return x; } diff --git a/re2c/src/re/tag.h b/re2c/src/re/tag.h index 24b25305..b6482a73 100644 --- a/re2c/src/re/tag.h +++ b/re2c/src/re/tag.h @@ -17,6 +17,13 @@ static const tagver_t TAGVER_ZERO = 0; // absense of tag static const tagver_t TAGVER_CURSOR = std::numeric_limits::max(); // current position, highest priority +struct tag_info_t +{ + uint32_t idx : 31; + uint32_t neg : 1; +}; + + struct Tag { static const size_t RIGHTMOST; -- 2.40.0